整数オーバーフローの初歩の初歩

とりあえずサンプルを作る

#include 
#include 
#include 

int main(int argc, char **argv)
{
	char *p;
	int len = atoi(argv[1]);

	if (len < 10) {
		printf("do copy\n");
		p = malloc(len);
		strcpy(p, argv[2]);
	} else 
		printf("the number which you inputed is too big\n");
		
	
	return 0;
}

それでテスト。

[masami@moon:~/dvl/test]% ./vuln2 100 foobar
the number which you inputed is too big
[masami@moon:~/dvl/test]% ./vuln2 4294967296 foobar
do copy
[masami@moon:~/dvl/test]%

gdbで動作を見てみる。

[masami@moon:~/dvl/test]% gdb vuln2
GNU gdb 6.7.1-debian
Copyright (C) 2007 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-linux-gnu"...
Using host libthread_db library "/lib/libthread_db.so.1".
gdb $ list
1       #include 
2       #include 
3       #include 
4
5       int main(int argc, char **argv)
6       {
7               char *p;
8               int len = atoi(argv[1]);
9
10              if (len < 10) {
gdb $ 
11                      printf("do copy\n");
12                      p = malloc(len);
13                      strcpy(p, argv[2]);
14              } else
15                      printf("the number which you inputed is too big\n");
16
17
18              return 0;
19      }
20
gdb $ b 13
Breakpoint 1 at 0x4005b9: file vuln2.c, line 13.
gdb $ r 4294967296 foobar
do copy

Breakpoint 1, main (argc=0x3, argv=0x7fffa1354b78) at vuln2.c:13
13                      strcpy(p, argv[2]);
gdb $ p len
$1 = 0x0
gdb $ s
18              return 0;
gdb $ p p
$2 = 0x601010 "foobar"
gdb $ p argv[1]
$3 = 0x7fffa1355af9 "4294967296"
gdb $ c

Program exited normally.
gdb $ q

まず、なんで10行めのif文をパスできるかというとargv[1]に入れてる値は2^32なのでintだと収まらないので整数オーバーフローが発生。
その後lenの値は0になってるのでif文の比較はtrueが返ることになると。
そして、0バイトの領域をmalloc(3)で確保して、strpcy(3)で0バイトの領域にargv[2]をコピーする。

原理的には分かるけどそこから次の手段がまだ良く分かってない・・
時間があったらここを読むか。