r/C_Programming • • 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.

0 Upvotes

26 comments sorted by

6

u/aioeu Oct 17 '26 edited Oct 17 '26

Although I wonder do we really need the address can we not use offset?

When a function is called, the return address pushed onto the stack is an absolute address. When execution reaches the ret instruction 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.

0

u/Right_Nuh Oct 17 '26

I meant to say if we can overwrite the return address by overflowing the buffer then we sort of know the distance between return address and buffer so if we wanna point to the beginning of the buffer, can we not just do - (number of bytes it took us to get to return address). That should give us the address of the start of the buffer? Or am I completely way off?

Either way does this mean that we actually put the injected code in teh buffer?

1

u/aioeu Oct 17 '26 edited Oct 17 '26

You already know the distance between the return address and the start of the buffer. You can just look at the machine code of the program to work that out. That distance isn't an "unknown" here. It's probably going to be something like 50 bytes, or maybe 70 bytes, depending on how the compiler has decided to lay out those two local variables.

What you don't know — at least until the program is actually running — is where these things are in memory. That's the whole point of ASLR. Even if you know the buffer is at address x and the return value is at x + 50, that tells you nothing about what x is. And that's kind of important, since x would be what you want to replace the return value with if you wanted to inject your own code through that input argument.

1

u/Right_Nuh Oct 17 '26 edited Oct 17 '26

Thanks for clearing somethings up, it is starting to make sense now. The attacker can't infer address from previous run because x is random but the attacker needs to input values before the program runs. Subconsciously I had given the attacker more power than they actually have.

But: On a similar problem but where we redirect the code to another address, can we not use the same logic there tho? We want it to be redirected to say another function's address. We don't know the address of that function until we run the code which could be random.

In this case the solution says ASLR won't solve it so I am still a bit confused.

1

u/dack42 Oct 17 '26

I think you are over complicating it. In any of these attacks you are describing, the attacker is overwriting the return address that is stored on the stack. When the CPU hits a return, it loads that address into the instruction pointer and jumped there. This is an absolute address, because that's how the instruction pointer works. So, in order to have control over what gets executed, the attacker must know some absolute address that they want the CPU to jump to. Randomizing the memory segment locations each time the program runs prevents the attacker from knowing this (unless there are flaws in the randomization, or some memory address leak vulnerability).

1

u/Right_Nuh Oct 17 '26

I sort of agree but it is not consistent or at least I don't think so, after attempting to solve exercises like this, it says that if the attacker injects code then ASLR will help, if attacker redirects it to an existing function or place in the code, then ASLR won't prevent anything. But in both cases the attacker must provide an absolute address.

1

u/dack42 Oct 18 '26

if attacker redirects it to an existing function or place in the code, then ASLR won't prevent anything

Yes, it will do something to prevent it. If you are overwriting the return address to redirect anywhere, you need to know an absolute address you want to jump to. This is the case regardless of the destination. The destination could be shell code on the stack, an existing function, libc, a ROP gadget, or anything else - you still need to know an address.

There are ways to work around ASLR without an address leak, but the don't work in all cases. For example, brute forcing (on weak ASLR implementations) or partial overwrites. But in the case of a good ASLR implementation and a full return address overwrite - you need to know an address to make it work.

1

u/aioeu Oct 17 '26

In this case the solution says ASLR won't solve it

What does it specifically say?

1

u/Right_Nuh Oct 17 '26

So this exercise is directly related to the main one and both evaluating the same code. The question is whether the attacker can print the password.

We assume the architecture is 64 bits (i.e., pointers are 8 bytes) If the input contains 50 bytes, then <address of printf(secret)>, strcpy at line 9 overflows the stack variable and overwrite the return pointer with the address of printf(secret). Therefore, whe the function| returns it jumps to the printf(secret). This attack cannot be countered by randomizing the base address of the stack, since this will not affect the distance between buffer and the return pointer, neither the position of the code of printf(secret)

1

u/aioeu Oct 18 '26 edited Oct 18 '26

This attack cannot be countered by randomizing the base address of the stack

Note carefully what this is saying here. It didn't say "ASLR won't solve it", it said "stack base address randomization won't solve it".

ASLR is broader than that. ASLR can be used to randomize where code is loaded into memory. Where the program's code is in memory has nothing to do with the base address of the stack.

So what the solution said is correct... but it's only part of the story. It's only talking about stack base address randomization, and that's only one aspect of ASLR.

1

u/grobblebar Oct 17 '26

You don’t know the address of the buffer. This is chosen at runtime, by the OS, and is not embedded in the binary.

1

u/Right_Nuh Oct 17 '26

But: On a similar problem but where we redirect the code to another address, can we not use the same logic there tho? We want it to be redirected to say another function's address. We don't know the address of that function until we run the code which could be random.

In this case the solution says ASLR won't solve it so I am still a bit confused.

1

u/Neither_Dream_7402 Oct 17 '26 edited Oct 17 '26

No, in this case you want to manipulate where the CPU jumps once it's done executing printInput not inject your own code.

On a side note, attacks that spray shellcode via buffer overflows are typically no longer that effective on their own on modern systems since memory pages can be marked as not executable/NX and most of the times the stack is marked as such, that along with a plethora of other mitigations (e.g., stack canaries) employed by compilers, runtimes, the operating system and the hardware

The normal flow would be:

main -> printInput-> main

but if you manage to spray into printInput's stack frame's return address you could make it do:

main -> printInput -> someNormalFunctionThatCanBeUsedForEvil

This would be easy if the address of someNormalFunctionThatCanBeUsedForEvil is fixed, you can just bake that address into the payload but ASLR defeats this by randomizing that function's location.

1

u/aioeu Oct 17 '26

No, in this case you want to manipulate the return address of the function not inject your own code.

In practice, yes, because of noexec stack among other reasons... but it's clear the OP's question and solution were talking about injecting code through the stack. Let's not confuse the OP by talking about a slightly different kind of attack at the same time.

1

u/Electrical-Meat-1717 Oct 17 '26

Yes is says so right in the answer sheet? what are you confused about?

1

u/Right_Nuh Oct 17 '26

That I don't understand the answer sheet. I posted the solution so you guys know I am not making you solve an assignment. I feel like I wanna look away from the answer sheet and see other explanations because I thought I understood this until I started to get all similar exercises wrong so I wanna start from 0. I already have a section that describes what I don't understand.

1

u/Electrical-Meat-1717 Oct 17 '26

basically, without aslr you could just look at the binaries to determine where to inject what you want but once aslr is added they get mixed up each time so you can't match it up with anything and you'd just have to guess until you luck into it.

1

u/Right_Nuh Oct 17 '26

On a similar problem but where we redirect the code to another address, can we not use the same logic there tho? We want it to be redirected to say another function's address. We don't know the address of that function until we run the code which could be random.

In this case the solution says ASLR won't solve it so I am still a bit confused.

1

u/agmatine Oct 17 '26

Post the "similar" problem, then. It is impossible to answer those questions without knowing was asked in the first place.

1

u/Right_Nuh Oct 17 '26

The same problem on (b) it asks us if an attacker can print the password. Here is the solution.

We assume the architecture is 64 bits (i.e., pointers are 8 bytes) If the input contains 50 bytes, then <address of printf(secret)>, strcpy at line 9 overflows the stack variable and overwrite the return pointer with the address of printf(secret). Therefore, whe the function| returns it jumps to the printf(secret). This attack cannot be countered by randomizing the base address of the stack, since this will not affect the distance between buffer and the return pointer, neither the position of the code of printf(secret)

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