Today, I took a short "C++ skills test" from Elance.com. One question was the following:
What is the security vulnerability of the following line of code:
printf("%s", argv[1]);
Option 1: Format String
Option 2: Stack Overflow <-- This was marked by Elance as the correct answer
The user was provided 10 seconds to answer this question after an initial few seconds of seeing the question (or automatically fail the question). (There were also two other clearly irrelevant answers that were not marked as the correct answer by Elance.)
I was looking for buffer overrun or buffer overflow as an option.
I instinctively did not like the answer stack overflow, because in my 10 seconds I mentally used what I believe is the standard definition of "Stack Overflow":
In software, a stack overflow occurs when the stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program ...
According to this definition of "Stack Overflow", a buffer overrun is entirely possible without a stack overflow; it is a stack overflow only if the program attempts to write outside the calling program's total stack allocation (whether due to a buffer overrun, or whether it would otherwise be a legitimate write, such as allocating memory for stack-based variables an excessive number of times).
My 10-second instinct told me that "buffer overrun" is a more accurate description of the problematic line of code, above - because often (in my experience) there are sufficient null characters ('\0'
) peppered through garbage data in RAM to often avoid an actual stack overflow in cases like this, but a buffer overrun in the implementation seems reasonably possible or even likely. (But the possibility that printf
reads garbage here might assume that argc == 1
, such that there was no user-provided argv[1]
; if argv[1]
is present, perhaps one can assume it's likely that the calling function has not inserted NULL
's. It was not stated in the problem whether argv[1]
was present.)
Because I imagined that there could be a buffer overrun problem here, even without a stack overflow, I answered Format String, because simply by passing a different format string such as "%.8s"
, the problem can be mostly avoided, so it seemed like an overall more generic, and therefore better, answer.
My answer was marked as wrong. The correct answer was marked as "Stack Overflow".
It now occurs to me that perhaps if one assumes that argv[1]
is present, that the only possible buffer overrun is a stack overflow, in which case, stack overflow might in fact be the correct answer. However, even in this case, would it not be considered odd to call this a stack overflow? Isn't buffer overflow a better way to describe this problem, even assuming argv[1]
is present? And, if argv[1]
is not present, isn't it pretty much incorrect to state that the problem is stack overflow, rather than the more accurate buffer overrun?
I would like the opinion of professionals on this site: Is "stack overflow" the proper way to define the memory safety problem with the above line of code? Or, rather, is "buffer overflow" or "buffer overrun" clearly a better way to describe the problem? Finally, given the two options provided for the question's answer (above), is the answer ambiguous, or is "stack overflow" (or "format string") clearly the better answer?
Tangential Comments regarding the Elance test (Not related to the question in this posting)
None of the Elance "C++ skills test" questions pertained to any C++-specific features such as classes, templates, anything in the STL, or any aspect of polymorphism. Every question was a down-and-out, straight-from-C question.
Because there were many (at least 3) other questions in Elance's so-called "C++ skills test" that were unarguably wrong (such as this question: given sizeof(int) == sizeof(int*)
and sizeof(int) == 4
, then in the code int *a, *b; a=b; b++; b-a;
, what is b-a
, with the correct answer listed as 4
, rather than the actual correct answer of 1
), and given the fact that there were no C++-specific questions on the test, I have contacted Elance and plan to seriously pursue their problematic test with the organization. However, for the question discussed in this posting, I am uncertain whether the question/answers are problematic.