r/C_Programming • u/Right_Nuh • Oct 17 '26
Question Can code injection be countered by randomizing the base address of the stack?
In your answer include a description of the assumption you have made about the binary code and memory layout of the program. Explain if it can be countered by randomizing the base address of the stack.
a) Explain a mechanism that can be used by the attacker to force the program to execute arbitrary code.
1 void printInput(char *input) {
2 char buffer[50];
3 char secret[20] = "Pwd123456";
4 if (strcmp(input, secret) == 0)
5 print(secret);
6 else
7 printf(input);
8 strcpy(buffer, input);
9 }
10 int main(int argc, char *argv[]) {
11 if (argc != 2) {
12 return 1;
13 }
14 printInput(argv[1]);
15 return 0;
16 }What I don't understand:
The solution says that it can be countered by ASLR because we need the address of the buffer. I sort of understand that we need to inject our code somewhere in the buffer thus if we want to overwrite the return address to point to the beginning of the buffer, where our code is, we need to know the address? Although I wonder do we really need the address? Can we not use offset?
Either way the solution seems to suggest that we should overwrite the return address and put the code immediately after and set the return address to point right after it. So I am not at all sure what they mean. Can someone explain to me why ALSR works here? I'd appracite it.
Solution in the answer sheet:
If the input contains 50 bytes, then <address of return pointer + 8>, then binary code, strcpy at line 9 over- flows the stack variables and overwrites the return pointer with the address immediately after the return pointer. After the return pointer, the overflow additionally writes the bi- nary code. Therefore, when the function| returns it jumps to the address above the return pointer on the stack. This attack can be countered by base stack randomization, since it prevents the attacker from knowing the address of buffer and therefore the address where the binary code has been injected. This makes not possible to know the value that should be written in the return pointer.
6
u/aioeu Oct 17 '26 edited Oct 17 '26
When a function is called, the return address pushed onto the stack is an absolute address. When execution reaches the
retinstruction in the function, the CPU expects to be able to pop an absolute address off the top of the stack. You cannot use an offset, because that's simply not what the CPU uses there.ASLR is just one mitigation against this particular kind of attack. In practice, most systems will use multiple mitigations all at once.
1
u/Electrical-Meat-1717 Oct 17 '26
Yes is says so right in the answer sheet? what are you confused about?
2
u/aocregacc Oct 17 '26
When the vulnerable function returns, it pops the return address off the stack and jumps there.
That's the step where we want to take over and redirect it to our injected code, so we need to know the address of the injected code.
1
u/darkry Oct 17 '26
If you want to really understand these concepts you should step through this in a debugger and play with different inputs. You'll also quickly learn to play with different compiler options.
There are all sorts of fun nuances to a tiny little example like this: Endianess, aslr, canaries, the fact that the stack isn't marked executable.
Try it, you'll learn a lot!
1
u/grobblebar Oct 17 '26
Because pushing and popping the return address to/from the stack is handled by hardware instructions, and these only work with absolute addresses.
Modern operating systems have text and data in different memory segments, which are usually separated far enough apart that a full overwrite of the return address is necessary.
1
u/sciencekm Oct 17 '26
One should be able to do this even without compiler modification, by using setjmp/longjmp. The reason why the return address is easy to overwrite is because it is the last value to be pushed on to the stack. If the return address is pushed first (that is, the jmp buffer), the attacker won't be able to overwrite this.
1
u/UnrealHallucinator Oct 17 '26
The problem isn't knowing the return address, the problem is knowing where to jump. Aka when you overwrite the return address you need to write an address that points to code that you own (aka shellcode). But to do that you need to know what the address of your shellcode is. With ASLR enabled, you cant find that without an address leak to then calculate offsets and figure out where to jump. Hope this answers your question.
1
u/Remarkable_Pace8101 Oct 19 '26 edited Oct 19 '26
Imagine a burglar told you to go to a specific street at night and burglarize the house with address 1234 because he picked the lock on the front door for you and disabled the alarm system.
Then imagine that the neighbors got wise and every night randomized all their house addresses. You wouldn't be able to know which door to walk up to, and if you try the wrong door the police get called.
Probably not the best example, but that's what's happening here... the address of your payload is at a random location so you don't know what address to jump to when you overwrite the return address on the stack