Multiple choice technology security

What is the vulnerability in this code? char output[20]; /* Assume data is a character array with value %200d asdf */ sprintf(output, data);

  1. Buffer overflow

  2. Off by one error

  3. Format string vulnerability

  4. No vulnerabilities are present in this code

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The code has a format string vulnerability because sprintf() uses the 'data' array as the format string. If data contains format specifiers like %200d, they will be interpreted as format directives rather than literal text, potentially leaking stack data or causing crashes. Buffer overflow is not the primary issue.

AI explanation

sprintf(output, data) passes attacker-controlled data directly as the format string argument instead of using a fixed format like sprintf(output, "%s", data). If data contains format specifiers (like the given %200d), sprintf will interpret them, reading/writing memory or producing huge output based on attacker input — this is the classic format string vulnerability. It happens to also overflow the 20-byte buffer here, but the root vulnerability class being tested is the uncontrolled format string, not a plain buffer overflow (where the format string itself would be fixed/safe) or an off-by-one.