Skip to content

Instantly share code, notes, and snippets.

@bennofs
Created July 26, 2017 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennofs/84b5007fe807d59bbadc13c0b6ae2849 to your computer and use it in GitHub Desktop.
Save bennofs/84b5007fe807d59bbadc13c0b6ae2849 to your computer and use it in GitHub Desktop.
GDB bug
// compile with: gcc -m32 test.c -oexperiment -mpreferred-stack-boundary=4
// run with: gdb experiment
#include <unistd.h>
volatile int global;
int main(int argc, char** argv) {
register int bp asm ("bp");
sleep(1); // allow debugger to attach + force stack alignment
volatile int* stack = (int*)bp;
// clobber saved bp
*stack = 0xdeadbeef;
if(stack != 0) goto first; // use if to avoid dead code elimination of again:
again:
__asm__("int $3");
global = 2; // just so that we have some code here
global += 3;
first:
// clobber return address
*(stack + 5) = (int)&&again;
}
GNU gdb (GDB) 8.0
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from experiment...done.
(gdb) r
Starting program: /tmp/experiment
Program received signal SIGTRAP, Trace/breakpoint trap.
0x080485ee in main ()
(gdb) display/i $pc
1: x/i $pc
=> 0x80485ee <main+51>: mov DWORD PTR ds:0x804a020,0x2
(gdb) si
Cannot access memory at address 0xdeadbeeb
(gdb) si
Cannot access memory at address 0xdeadbeeb
(gdb) bt
Python Exception <class 'gdb.MemoryError'> Cannot access memory at address 0xdeadbeeb:
#0 0x080485ee in main ()
Cannot access memory at address 0xdeadbeeb
(gdb) p/x $pc
$1 = 0x80485ee
(gdb) si
Cannot access memory at address 0xdeadbeeb
(gdb) p/x $pc
$2 = 0x80485ee
(gdb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment