Local variable position on stack not changing

Andy Woerpel

I am currently reading a book security vulnerabilities and have come to the section on stack-based buffer overflows. It gives an example similar to the one that follows.

//overFlowTest.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void main(int argv, char* argv[])
{
    int i = 0;
    char buffer[4];
    strcpy(buffer, argv[1]);
    if(i)
    {
        printf("overwrote i\n");
    }
}

When I compile and run the program with an input argument that is longer than the available space allocated for that variable "AAAAA", I get the following as expected (because I overwrote the i variable since it has a numerically larger address (lower in the stack) on the stack than "buffer").

# gcc overFlowTest.c
# ./a.out AAAAA
overwrote buffer
#

But then when I change the order of how the local variables are created, I would think they would get pushed to the stack in the opposite order and the buffer overflow would not work.

//overFlowTest.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void main(int argv, char* argv[])
{
    char buffer[4];
    int i = 0;
    strcpy(buffer, argv[1]);
    if(i)
    {
        printf("overwrote i\n");
    }
}

But this does not seem to be the case, as I get the same result.

# gcc overFlowTest.c
# ./a.out AAAAA
overwrote buffer
#

Any ideas on why this is happening?

Kevin W.

So, I found some interesting information while exploring this problem.

One, this problem is not reproduced with clang. When I compile the second program, I don't see the print statement as shown here:

$ clang so.c -o so.out
so.c:4:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
void main(int arg, char* argv[])
^
so.c:4:1: note: change return type to 'int'
void main(int arg, char* argv[])
^~~~
int
1 warning generated.

$ clang so2.c -o so2.out
so2.c:4:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
void main(int arg, char* argv[])
^
so2.c:4:1: note: change return type to 'int'
void main(int arg, char* argv[])
^~~~
int
1 warning generated.

$ ./so.out AAAAAAAAAAAAAAAAAAA
overwrote i

$ ./so2.out AAAAAAAAAAAAAAAAAAA

$

However, if we do the same with gcc, we see that they both fail.

$ gcc so.c -o so.exe

$ gcc so2.c -o so2.exe

$ ./so.exe AAAAAAAAAAAAAAAA
overwrote i

$ ./so2.exe AAAAAAAAAAAAAAAA
overwrote i

$

Looking a bit further, let's look at the assembly for these

$ gcc so.c -S -masm=intel

$ gcc so2.c -S -masm=intel

$ diff so.s so2.s
1c1
<       .file   "so.c"
---
>       .file   "so2.c"

$

As you can see, the only difference here is the filename (I also tested with all optimizations off, with the same result).

Now, let's try with clang.

$ clang -S -mllvm --x86-asm-syntax=intel so.c
so.c:4:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
void main(int arg, char* argv[])
^
so.c:4:1: note: change return type to 'int'
void main(int arg, char* argv[])
^~~~
int
1 warning generated.

$ clang -S -mllvm --x86-asm-syntax=intel so2.c
so2.c:4:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
void main(int arg, char* argv[])
^
so2.c:4:1: note: change return type to 'int'
void main(int arg, char* argv[])
^~~~
int
1 warning generated.

$ diff so.s so2.s
26c26
<       lea     rax, qword ptr [rbp - 24]
---
>       lea     rax, qword ptr [rbp - 20]
29c29
<       mov     dword ptr [rbp - 20], 0
---
>       mov     dword ptr [rbp - 24], 0
34c34
<       cmp     dword ptr [rbp - 20], 0
---
>       cmp     dword ptr [rbp - 24], 0

$

It looks like clang compiles the two files to different versions; this prevents i from being overwritten.

In conclusion, the reason why these two files produce different output is because the code produces undefined behaviour - the compiler is not bound by any standards and can produce whatever is easiest.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Variable position in echo is changing the result

From Dev

Changing a local variable w/ a global function in lua

From Dev

Changing behavior of "uninitialized local variable" error

From Dev

call local storage using changing variable

From Dev

When local variable stack gets created?

From Dev

Is a pointer variable created for stack local array?

From Dev

When local variable stack gets created?

From Dev

Java : Why is changing a local variable affecting a global variable original copy?

From Dev

Why Changing local variable value affect the global copy

From Dev

Why Changing local variable value affect the global copy

From Dev

Where is the local final variable in method stored (Stack/Heap)?

From Dev

how the local variable organized in stack when function call?

From Dev

Work around error 'Address of stack memory associated with local variable returned'

From Dev

C - Avoiding warning "address of stack memory associated with local variable returned"

From Dev

.NET CLI: How is a local variable popped off the stack if not on top?

From Dev

how the local variable organized in stack when function call?

From Dev

Writing to stack as local variable in _start function (x86 ASM)

From Dev

What part of an array local variable is stored on the stack? And which part is on the heap?

From Dev

Changing background of a Stack in Shoes

From Dev

Variable Not Changing

From Dev

Compilation error: Smart cast to '<type>' is impossible, because '<variable>' is a local variable that is captured by a changing closure

From Dev

How can I change a local variable assigned to the value of a global one without changing the global variable?

From Dev

changing the position of headings

From Dev

Default camera position not changing?

From Dev

Changing position during animation?

From Dev

Parameter ValueFromPipeline and changing position

From Dev

Changing position of Sprite in unity

From Dev

Changing x position of SKSpriteNode

From Dev

Changing label position in UICollectionViewCell

Related Related

  1. 1

    Variable position in echo is changing the result

  2. 2

    Changing a local variable w/ a global function in lua

  3. 3

    Changing behavior of "uninitialized local variable" error

  4. 4

    call local storage using changing variable

  5. 5

    When local variable stack gets created?

  6. 6

    Is a pointer variable created for stack local array?

  7. 7

    When local variable stack gets created?

  8. 8

    Java : Why is changing a local variable affecting a global variable original copy?

  9. 9

    Why Changing local variable value affect the global copy

  10. 10

    Why Changing local variable value affect the global copy

  11. 11

    Where is the local final variable in method stored (Stack/Heap)?

  12. 12

    how the local variable organized in stack when function call?

  13. 13

    Work around error 'Address of stack memory associated with local variable returned'

  14. 14

    C - Avoiding warning "address of stack memory associated with local variable returned"

  15. 15

    .NET CLI: How is a local variable popped off the stack if not on top?

  16. 16

    how the local variable organized in stack when function call?

  17. 17

    Writing to stack as local variable in _start function (x86 ASM)

  18. 18

    What part of an array local variable is stored on the stack? And which part is on the heap?

  19. 19

    Changing background of a Stack in Shoes

  20. 20

    Variable Not Changing

  21. 21

    Compilation error: Smart cast to '<type>' is impossible, because '<variable>' is a local variable that is captured by a changing closure

  22. 22

    How can I change a local variable assigned to the value of a global one without changing the global variable?

  23. 23

    changing the position of headings

  24. 24

    Default camera position not changing?

  25. 25

    Changing position during animation?

  26. 26

    Parameter ValueFromPipeline and changing position

  27. 27

    Changing position of Sprite in unity

  28. 28

    Changing x position of SKSpriteNode

  29. 29

    Changing label position in UICollectionViewCell

HotTag

Archive