Skip to content

Commit 328572e

Browse files
feat: finish lab5 part 2,3
1 parent 8dca069 commit 328572e

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

lab5/Answer.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,5 @@ int main(void)
483483
}
484484
```
485485
### Why
486-
486+
The way ASan detects out-of-bound read/write by adding shadow memory that maps to variables in the program, and by inserting redzones between these variables.
487+
In this program, the out-of-bound write operation on string `a` bypasses the redzones and directly into string `b`. Because the redzones are not accessed, no error is produced.

lab5/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
all: uaf_asan
33

44
uaf_asan: uaf.c libantiasan.so
5-
gcc -Og -g -o $@ $< -lantiasan -L.
5+
gcc -fsanitize=address -Og -g -o $@ $< -lantiasan -L.
66

77
libantiasan.so: antiasan.c
88
gcc -g -fPIC -c antiasan.c

lab5/antiasan.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
#include <stdio.h>
22
#include <string.h>
3+
#include <unistd.h>
4+
#include <errno.h>
5+
#include <sys/mman.h>
36

7+
/*
8+
* Overwrite the value of target variable in shadow
9+
*/
410
void antiasan(unsigned long addr)
511
{
6-
unsigned long rz = (addr >> 3) + 0x7ffff8000;
7-
//printf("%lx\n", *(unsigned long*)rz);
8-
//unsigned long t = 0x0;
9-
//memcpy((unsigned long*)rz, &t, 8);
12+
/* calculate memory address of s[0x10] */
13+
unsigned long rz = (addr >> 3) + 0x7fff8000;
14+
15+
/* mprotect need to perform on aligned page address, the page size is x^12 bytes */
16+
unsigned long b_addr = ((rz>>12)<<12);
17+
// printf("%p\n%p\n", (unsigned long*)rz, (unsigned long*)b_addr);
18+
19+
/* change the access protection for memory page so that it can be read and write */
20+
if(mprotect((void*)b_addr, getpagesize(), PROT_READ | PROT_WRITE) == -1) {
21+
fprintf(stderr, "antiasan: failed to mprotect shadow - %s, %d\n", strerror(errno), errno);
22+
}
23+
24+
/* change value in shadow to which s[0x10] map */
25+
/* first printf: fafafafafafafafd, second printf: fafafafafafafa00 */
26+
// printf("%lx\n", *(unsigned long*)rz);
27+
*(char*)rz = 0x0;
28+
// printf("%lx\n", *(unsigned long*)rz);
1029
}
30+
// 0xc067fff8002: 0xfafafafafafdfdfd

lab5/antiasan.o

1.13 KB
Binary file not shown.

lab5/libantiasan.so

1.02 KB
Binary file not shown.

lab5/uaf_asan

9.84 KB
Binary file not shown.

0 commit comments

Comments
 (0)