File tree 1 file changed +57
-11
lines changed
1 file changed +57
-11
lines changed Original file line number Diff line number Diff line change 1
1
# Answer
2
2
3
- Name:
4
- ID:
3
+ Name: [ 黃崇銘 ]
4
+ ID: [ 512558012 ]
5
5
6
6
## Test Valgrind and ASan
7
- ### Result
8
- | | Valgrind | Asan |
9
- | -------------------- | -------- | ---- |
10
- | Heap out-of-bounds | | |
11
- | Stack out-of-bounds | | |
12
- | Global out-of-bounds | | |
13
- | Use-after-free | | |
14
- | Use-after-return | | |
7
+ | Vulnerability | Result (Valgrind) | Result (ASan) |
8
+ | ---------------------| -------------------| ---------------|
9
+ | Heap out-of-bounds | Detected | Detected |
10
+ | Stack out-of-bounds | Detected | Detected |
11
+ | Global out-of-bounds| Detected | Detected |
12
+ | Use-after-free | Detected | Detected |
13
+ | Use-after-return | Detected | Detected |
15
14
16
15
### Heap out-of-bounds
16
+ ** Source code** :
17
+ ``` c
18
+ #include < stdlib.h>
19
+
20
+ void heap_out_of_bounds () {
21
+ int *arr = malloc(10 * sizeof(int));
22
+ arr[10] = 0; // 越界寫
23
+ free (arr);
24
+ }
25
+
17
26
#### Source code
18
27
```
19
-
28
+ void stack_out_of_bounds() {
29
+ int arr[ 10] ;
30
+ arr[ 10] = 0; // 越界寫
31
+ }
20
32
```
21
33
#### Valgrind Report
22
34
```
30
42
### Stack out-of-bounds
31
43
#### Source code
32
44
```
45
+ int global_arr[ 10] ;
46
+
47
+ void global_out_of_bounds() {
48
+ global_arr[ 10] = 0; // 越界寫
49
+ }
33
50
34
51
```
35
52
#### Valgrind Report
44
61
### Global out-of-bounds
45
62
#### Source code
46
63
```
64
+ #include <stdlib.h>
65
+
66
+ void use_after_free() {
67
+ int * arr = malloc(10 * sizeof(int));
68
+ free(arr);
69
+ arr[ 0] = 0; // 使用已釋放內存
70
+ }
47
71
48
72
```
49
73
#### Valgrind Report
58
82
### Use-after-free
59
83
#### Source code
60
84
```
85
+ int * ptr;
86
+
87
+ void use_after_return_helper() {
88
+ int local_arr[ 10] ;
89
+ ptr = &local_arr[ 0] ;
90
+ }
91
+
92
+ void use_after_return() {
93
+ use_after_return_helper();
94
+ ptr[ 0] = 0; // 使用返回後的局部變量
95
+ }
61
96
62
97
```
63
98
#### Valgrind Report
72
107
### Use-after-return
73
108
#### Source code
74
109
```
110
+ void bypass_redzone() {
111
+ int arr1[ 8] ;
112
+ int arr2[ 8] ;
113
+ * ((volatile int * )(&arr1[ 8] )) = 0; // 繞過紅區寫
114
+ }
115
+
75
116
76
117
```
77
118
#### Valgrind Report
90
131
```
91
132
### Why
92
133
134
+ void bypass_redzone() {
135
+ int arr1[8];
136
+ int arr2[8];
137
+ *((volatile int *)(&arr1[8])) = 0; // 繞過紅區寫
138
+ }
You can’t perform that action at this time.
0 commit comments