Given the code snippet:
int main()
{
printf("Val: %d", 5);
return 0;
}
is there any guarantee that the compiler would store "Val: %d"
and '5'
contiguously? For example:
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| ... | %d | ' ' | ':' | 'l' | 'a' | 'V' | '5' | ... |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
^ ^ ^
| Format String | int |
Exactly how does are these parameters allocated in memory?
Furthermore, does the printf function access the int relative to the format string or by absolute value? So for example, in the data
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| ... | %d | ' ' | ':' | 'l' | 'a' | 'V' | '5' | ... |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+
^ ^ ^
| Format String | int |
when the function encounters %d
would there already be a stored memory address for the first parameter of the function which would be referenced or would the value be calculated relative to the first element of the format string?
Sorry if I'm being confusing, my primary goal is to understand string formatting exploits where the user is allowed to supply the format string as described in this document
http://www.cis.syr.edu/~wedu/Teaching/cis643/LectureNotes_New/Format_String.pdf
My concerns arise on the attack described on page 3 and 4. I figured that the %x
's are to skip the 16 bits that the string takes up which would indicate that the function allocated contiguously and references relatively but other sources indicate that there is not guaranteed that the compiler must allocate contiguously and I was concerned that the paper was a simplification.