Over a million developers have joined DZone.

Anatomy of an Exploit: Stack Smashing and Registers

DZone's Guide to

Anatomy of an Exploit: Stack Smashing and Registers

Christopher Lamb talks stack smashing again, building off an image of a stack to discuss storing data and about being the boss in a buffer overflow situation.

· Performance Zone ·
Free Resource

Maintain Application Performance with real-time monitoring and instrumentation for any application. Learn More!

So in my last piece on buffer overflows and stack smashing, I didn't really discuss why the return pointer's important. I'm going to try to fix that today.

First, let's pull up that image of a typical stack again:

Image title

Above, we have the stack in a typical program (again!). Original image By R. S. Shaw (own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=1956587)

So what is this "return address" and why do I care?

Calling Conventions

Remember from the first piece that the stack is set up this way by convention; there's no reason to set the stack up this way other than somebody, at some time, decided this is how they wanted to do it. That's it. When whoever it was came up with this particular design (or convention, as we call it today), they knew they needed to do a couple of things.

First, they needed to store a couple of things. Second, they needed to store the arguments passed to a given function. We pass arguments to functions—it's what we do—and we need to store the data somewhere. All those nifty argument lists we're accustomed to, with various data types, are really just shoved into allocated stack memory as ones and zeros. Those exact ones and zeros are based on the data type, but at the end of the day, it's all just a mess of binary.

We likewise need to store local variables. And we need some way to return to the caller when we're finished—that's where the return address comes in. Keep in mind, it doesn't really matter that much where we store that return address. If I can allocate a buffer on the stack, I can overwrite that return pointer.

(Or, at least, a return pointer. I don't need to get control of the pointer associated with my current function; a pointer associated with the calling function is okay too).

So why is this address important?

Read the Instructions!

Computers have a specific register that holds the address of the next instruction. On x86, this is the EIP register. This is how the processor keeps track of where it is and what it's doing. And programs can write and read to that register via various assembly commands. So what happens when you call a function? Well, you need to set up the stack, and then you branch out to the function's address. This is what really happens behind the scenes, and the compiler will take care of all this left over junk for you. Basically, you reserve space for the parameters to a function—in this case, DrawLine(.)—and then you save the address you'll start executing from when the function is completed. Then you allocate stack space for local variables. In normal operation, you save the return address, execute the function, and return to the return address.

In a buffer overflow situation though, you inject code into the locally variable space on the stack and overwrite the return address with whatever address you want. That's right, you're the boss! use whatever address you want to. Then, when the function completes, it'll start executing from wherever you've told it to.

In a classic buffer overflow exploit, we'll insert and address that points at the next word (or the word just after that) as we've been able to write our own code there. So now, we've told the processor to begin to execute our injected code.

This is why that return address is important, and why it exists in the first place.

Collect, analyze, and visualize performance data from mobile to mainframe with AutoPilot APM. Learn More!

stack overflow ,stack ,malware ,performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}