I know that you can overflow regular code:
char string [9];
scanf ("% s" string).
But is scanf overflow possible ("% 8s" string)? 8 is just an example.
I know that "% 8s" works as a delimiter, but I also notice that when you enter a string longer than 8 characters, the program will end because:
stack detection detected * : ./ a.out terminated
======= Backtrace: ===========
...
Obviously, a flag that detects stack breaks is enabled by GCC by default. Since this is a stack break, I assume that it is still possible to overflow and execute arbitrary code.
Unlike the usual overflow, which controls the caller scanf ("% s"), if scanf ("% 8s") can overflow, it will overflow in the scanf function, so that when scanf tries to return, control will be gained.
But scanf is syscall, which requires a switch mode (switching from user mode to kernel mode), and internally it will cause things like reading in stdin, etc. Therefore, Iām not sure if we can overflow in kernel mode or something like that.
Comments are welcome!
UPDATE ā
char string [9] is assumed in the above example. char string [8] in the following valid code.
The question is really about the seemingly contradictory story between safe scanf ("% 8s") and GCC abortion due to a stack crash.
Simplified code:
void foo(pass some pointer) { char input[8]; int input_number = 0; while (1) { // looping console printf some info; scanf("%8s", input); input_number = atoi(input); if ((strlen(input) == 1) && (strncmp(input, "q", 1) == 0)) { input_number = -1; } switch (input_number) { case -1: to quit the console if input = 'q'; default: to print info that pointer refers to; ... } } }
Note:
- foo is being called by someone else.
- Although the string has a value of 8 bytes in real-time code with "% 8s", I do not think that this leads to a rout.