Page 1 of 1

How are data of different sizes written to the stack?

Posted: Tue Sep 29, 2020 1:56 pm
by mrjbom
Hi.
Previously, I encountered this problem, at first it scared me, but the problem was that the esp for the problem was calculated as follows: esp = stack_bottom + stack_size, as a result, the first addition of data to the stack overwritten the data outside it.
To solve this problem, I changed the esp calculation formula to this: esp = stack_bottom + stack_size - X

And I had a question about what number should I substitute instead of X?

Since my kernel is designed to run in protected mode on x86 processors, that means x = 4 bytes(word size), right?
And what if they try to add 6 bytes of data to the stack at the start, then 2 bytes will get out of the stack border? Or will the first 4 bytes be added first, then 4 bytes will be subtracted from the esp and 4 bytes will be added again?

Re: How are data of different sizes written to the stack?

Posted: Tue Sep 29, 2020 2:05 pm
by Octocontrabass
mrjbom wrote:And I had a question about what number should I substitute instead of X?
Zero. If data is being overwritten at addresses higher than your stack, you have a bug somewhere else.
mrjbom wrote:And what if they try to add 6 bytes of data to the stack at the start, then 2 bytes will get out of the stack border? Or will the first 4 bytes be added first, then 4 bytes will be subtracted from the esp and 4 bytes will be added again?
That depends on how you're adding data to the stack. The PUSH instruction subtracts 4 from ESP first, and then writes data to the address in ESP.

Re: How are data of different sizes written to the stack?

Posted: Tue Sep 29, 2020 2:56 pm
by mrjbom
Octocontrabass wrote:
mrjbom wrote:And I had a question about what number should I substitute instead of X?
Zero. If data is being overwritten at addresses higher than your stack, you have a bug somewhere else.
mrjbom wrote:And what if they try to add 6 bytes of data to the stack at the start, then 2 bytes will get out of the stack border? Or will the first 4 bytes be added first, then 4 bytes will be subtracted from the esp and 4 bytes will be added again?
That depends on how you're adding data to the stack. The PUSH instruction subtracts 4 from ESP first, and then writes data to the address in ESP.
You're right. I expected that because of my esp value, push overwrites some area of memory, it turns out that pop was to blame for everything, as soon as I loaded a new stack, I restored values for General-purpose registers using pop, it increased values for esp above the top of the stack and subsequent pushes overwritten memory outside the stack.

I just shouldn't try to do pop the first time I first start a thread.
Thanks!