Advertisement Jump to content
Sign in to follow this  
DracoLacertae

stack overflows: why not turn the stack upside down?

This topic is 1803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

We all know about stack overflows.  You overwrite your buffer on the stack, and you are now trashing another buffer, or even worse, the return address.  Then when 'ret' (I'm using x86 lingo here), runs, the cpu jumps to a bad place.  This is because on most platforms the stack grows downward.  When you exceed your stack frame's bouundary, you start tracking the previous stack frame.

 

This downward growth is mostly hisorical.  On older computers, before virtual memory, your stack started at the top of RAM, and grew downwards.  The heap started somewhere else and grew up.  You ran out of memory when the two pointer met.  With virtual memory, you can put the stack 'anywhere' and even grow it on a page fault in some cases.  And I think it's time to turn the stack around:

 

 

Suppose f() calls g() and that calls h():

classic stack:  'R' is where the return address goes:
low addr  [empty-----------------R][--f--]   high addr
low addr  [empty------------[--g-R][--f--]   high addr
low addr  [empty-----[--h-R][--g-R][--f--]   high addr

How H overflows:

low addr  [empty-----[--h--*R*****][--f--]   high addr


we crash, or worse, run an explot from h inserting an return address to who-knows-where


upside-down stack:
low addr  [--f--][-empty------------------]   high addr
low addr  [--f--][R-g--][empty-----------]   high addr
low addr  [--f--][R-g--][R-h--][empty----]   high addr

Now H overflows:
low addr  [--f--][R-g--][R-h***********--]

We've overflowed into empty space.  Return address and previous stack frames are safe

This wouldn't be too hard to do.  Most stack access during a function is done pointer arithmetic:

mov eax, [esp-4]

mov eax, [esp+4]

 

I know there are some hardware platforms that already have stacks that grow upwards.  Redefining the ABI for x86 would break using standard libraries, but for inside your own application, this might enhance security.  I suppose its even possible to use an 'upwards' stack for your application, and then when you call a 3rd party library, switch the stack pointer to a seperate area where you have a standard downwards stack defined.   I imagine this would involve a lot of hacking around in gcc or llvm to make it work.  In an open OS like linux, maybe you could recompile the whole system to use upwards stacks. 

 

Just a thought.  Downvote if it stinks! 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

Redefining the ABI for x86.


The ABI is partly defined by the hardware itself. call, push, pop, etc. explicitly modify ESP to go in a certain direction. Not only would a compiler have to use different opcodes, they'd very likely be less efficient.

Share this post


Link to post
Share on other sites
(Singing) "Whichever way the stack grows doesn't really matter to me!"


The heap grows the other way though... stack at one end, heap at other, of addressable memory. When they meet in the middle it's Bummed In The Gob Time (R)


This doesn't apply to many modern systems. Consider that each thread in a multithreaded system needs its own stack.

Share this post


Link to post
Share on other sites
There's also the whole split stack thing that some runtimes can do (if the stack bounds are exceeded, new pages are allocated somewhere else and life carries on). This is mostly done for microthreads/coroutines that only need a single page of stack (usually) due to the performance overhead incurred by all the stuff necessary to actually use split stacks.

Share this post


Link to post
Share on other sites

 

(I'd even go so far as to say putting buffers on the stack at all is something to avoid.)

I like this.

 

I also like the idea of seperate data and program control stacks.  I've worked with FORTH, and there's two parallel stacks.  That wouldn't be hard to do with x86 either.  Call and ret can use the hardware stack, and it's downward growth wouldn't be a concern.  Then argument passing and temporary data would be on a completely different stack, one that has nothing to do the 'esp' stack pointer.  Actually, the data stack could just be managed using the 'ebp' pointer; you don't really need a base pointer anyway.  You sort of do to make debugging easier, but you can get by without it.

 

 

The ABI is partly defined by the hardware itself. call, push, pop, etc. explicitly modify ESP to go in a certain direction. Not only would a compiler have to use different opcodes, they'd very likely be less efficient.

 

On x86 I don't think it would be too bad, at least for push/pop.   This page puts them at about 1% of each other.  http://qinsb.blogspot.com/2011/03/x86-register-save-performance.html

Doing your own call and return might be a little bit of a hit, but I hope most of the program's time is spent doing something other than function call overhead.

 

But, I've found something that makes the whole thing moot.  Having an upwards growing stack is not the panacea of stack buffer overruns.  from

 

the tl;dr is that you can allocate a buffer too small on the stack, and then call a function that's supposed to write into that buffer.  As it writes into the previous frame's buffer, it will blast past the buffer and trash its own, including the return address.

Share this post


Link to post
Share on other sites

 

(I'd even go so far as to say putting buffers on the stack at all is something to avoid.)

I also like the idea of seperate data and program control stacks.  I've worked with FORTH, and there's two parallel stacks.  That wouldn't be hard to do with x86 either.  Call and ret can use the hardware stack, and it's downward growth wouldn't be a concern.  Then argument passing and temporary data would be on a completely different stack, one that has nothing to do the 'esp' stack pointer.  Actually, the data stack could just be managed using the 'ebp' pointer; you don't really need a base pointer anyway.  You sort of do to make debugging easier, but you can get by without it.

Having your own stack, separate from "the stack" is quite common in the game engines that I've used. It's easier to catch bugs, because you can verify stack contents yourself (e.g. memsetting and memcmping regions with marker patterns at key moments), and if there are bugs you don't have the traditional problem of "I'm trying to decypher this crash dump, but I can't get a call-stack because it's corrupted". It's also ridiculously fast to allocate from (when you strip all your debugging code from the shipping build) and fast to deallocate (opting in to stack-unwinding when required).

I wrote a bit about my implementation here: http://eighthengine.com/2013/08/30/a-memory-management-adventure-2/

Edited by Hodgman

Share this post


Link to post
Share on other sites

Stack overflows are actually underflows, since the stack pointer goes down, not up. It was specifically designed that way because it's easier to let a program crash when it reaches the underflow, than to add a check for an upper-limit of the stack.

If you were to use an increasing stack pointer, you'd still need a way to detect overflows, otherwise you're just inviting hackers to use the stack for buffer-overflow type attacks.

So the "downward growth" that causes underflows is not just a historical side-effect - it is a safety feature.

 

Also, I think older programs (DOS 16 bit programs) used segments for memory access, which were the same thing as virtual memory - they certainly did not access memory using physical addresses like "the top of RAM". The stack and data segments could be placed anywhere in memory below 640K (and later 1MB with virtual x86, but never higher than that). The heap was only later introduced, and the heap memory addresses were served by a memory-manager, which usually allocated it's memory above the 1MB mark (and below,in the "upper-memory blocks"), where the stack could not reach, so the stack and heap would never intersect. The stack was limited to the size of a segment - 64k. The heap was limited to the size of extended memory + upper-memory. So this is not true: "You ran out of memory when the two pointer met". Until virtual memory came along, there was never a moment when the stack and heap shared the same address space.

Edited by tonemgub

Share this post


Link to post
Share on other sites

What is the actual problem you are trying to solve here? Running out of stack space seems the only one to me that would not be masking a program error of some kind. What makes you think that the default stack size on whatever hardware you are using is a problem?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!