-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_overflow.c
287 lines (247 loc) · 8.61 KB
/
buffer_overflow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* BSD 3-Clause License
*
* Copyright (c) 2024, Zhuo Ying Jiang Li
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "utils.h"
static int __attribute__((no_stack_protector))
buffer_overflow_stack(void * __capability ubuf, size_t ubufsize, int preserve_cheri_caps, int is_disclosure)
{
char kbuf[KBUFSIZE]; // uninitialized
int error;
if (is_disclosure) {
if (preserve_cheri_caps) {
error = copyoutcap(kbuf, ubuf, ubufsize);
} else {
error = copyout(kbuf, ubuf, ubufsize);
}
} else {
if (preserve_cheri_caps) {
error = copyincap(ubuf, kbuf, ubufsize);
} else {
error = copyin(ubuf, kbuf, ubufsize);
}
}
return (error);
}
static int
buffer_overflow_stack_subobject(void * __capability ubuf, size_t ubufsize, int preserve_cheri_caps, int is_disclosure)
{
struct buffer_overflow_obj bo_obj;
int error;
// bo_obj.bo_buf; // uninitialized
bo_obj.bo_td = curthread;
if (is_disclosure) {
if (preserve_cheri_caps) {
error = copyoutcap(bo_obj.bo_buf, ubuf, ubufsize);
} else {
error = copyout(bo_obj.bo_buf, ubuf, ubufsize);
}
} else {
if (preserve_cheri_caps) {
error = copyincap(ubuf, bo_obj.bo_buf, ubufsize);
} else {
error = copyin(ubuf, bo_obj.bo_buf, ubufsize);
}
}
return (error);
}
int
buffer_overflow_stack_ioctl_handler(struct dvkm_io *io, int bo_subobject)
{
size_t ubufsize;
void * __capability ubuf = NULL;
int preserve_cheri_caps, is_disclosure;
int error;
ubuf = io->input_buffer;
ubufsize = io->input_buffer_size;
preserve_cheri_caps = io->preserve_cheri_caps;
is_disclosure = io->is_disclosure;
if (ubuf == NULL) {
return (EINVAL);
}
if (bo_subobject) {
error = buffer_overflow_stack_subobject(ubuf, ubufsize, preserve_cheri_caps, is_disclosure);
} else {
error = buffer_overflow_stack(ubuf, ubufsize, preserve_cheri_caps, is_disclosure);
}
return (error);
}
/// Kernel malloc(9)
static int
buffer_overflow_heap(void * __capability ubuf, size_t ubufsize, void *hbuf, int preserve_cheri_caps, int is_disclosure)
{
int error;
if (is_disclosure) {
if (preserve_cheri_caps) {
error = copyoutcap(hbuf, ubuf, ubufsize);
} else {
error = copyout(hbuf, ubuf, ubufsize);
}
} else {
if (preserve_cheri_caps) {
error = copyincap(ubuf, hbuf, ubufsize);
} else {
error = copyin(ubuf, hbuf, ubufsize);
}
}
return (error);
}
// hardcoded heap buffer size
static int
buffer_overflow_heap_subobject(void * __capability ubuf, size_t ubufsize, int preserve_cheri_caps, int is_disclosure)
{
int error;
struct buffer_overflow_obj *bo_obj;
bo_obj = (struct buffer_overflow_obj *)malloc(sizeof(struct buffer_overflow_obj), M_DVKM, M_WAITOK);
if (is_disclosure) {
if (preserve_cheri_caps) {
error = copyoutcap(bo_obj->bo_buf, ubuf, ubufsize);
} else {
error = copyout(bo_obj->bo_buf, ubuf, ubufsize);
}
} else {
if (preserve_cheri_caps) {
error = copyincap(ubuf, bo_obj->bo_buf, ubufsize);
} else {
error = copyin(ubuf, bo_obj->bo_buf, ubufsize);
}
}
return (error);
}
int
buffer_overflow_heap_ioctl_handler(struct dvkm_io *io, int bo_subobject)
{
size_t ubufsize, alloc_size;
void * __capability ubuf = NULL;
int preserve_cheri_caps, is_disclosure;
void *hbuf;
int error;
ubuf = io->input_buffer;
ubufsize = io->input_buffer_size;
alloc_size = io->alloc_size;
preserve_cheri_caps = io->preserve_cheri_caps;
is_disclosure = io->is_disclosure;
if (ubuf == NULL) {
return (EINVAL);
}
if (bo_subobject) {
error = buffer_overflow_heap_subobject(ubuf, ubufsize, preserve_cheri_caps, is_disclosure);
} else {
hbuf = malloc(alloc_size, M_DVKM, M_WAITOK); // no M_ZERO
error = buffer_overflow_heap(ubuf, ubufsize, hbuf, preserve_cheri_caps, is_disclosure);
}
return (error);
}
/// Specific UMA zone
static int
buffer_overflow_uma(void * __capability ubuf, size_t ubufsize, uma_zone_t dvkm_zone, int preserve_cheri_caps, int is_disclosure)
{
int error;
void *hbuf;
hbuf = uma_zalloc(dvkm_zone, M_WAITOK);
if (is_disclosure) {
if (preserve_cheri_caps) {
error = copyoutcap(hbuf, ubuf, ubufsize);
} else {
error = copyout(hbuf, ubuf, ubufsize);
}
} else {
if (preserve_cheri_caps) {
error = copyincap(ubuf, hbuf, ubufsize);
} else {
error = copyin(ubuf, hbuf, ubufsize);
}
}
return (error);
}
static int
buffer_overflow_uma_subobject(void * __capability ubuf, size_t ubufsize, uma_zone_t dvkm_zone, int preserve_cheri_caps, int is_disclosure)
{
int error;
struct buffer_overflow_obj *bo_obj;
bo_obj = (struct buffer_overflow_obj *)uma_zalloc(dvkm_zone, M_WAITOK);
if (is_disclosure) {
if (preserve_cheri_caps) {
error = copyoutcap(bo_obj->bo_buf, ubuf, ubufsize);
} else {
error = copyout(bo_obj->bo_buf, ubuf, ubufsize);
}
} else {
if (preserve_cheri_caps) {
error = copyincap(ubuf, bo_obj->bo_buf, ubufsize);
} else {
error = copyin(ubuf, bo_obj->bo_buf, ubufsize);
}
}
return (error);
}
int
buffer_overflow_uma_ioctl_handler(struct dvkm_io *io, int bo_subobject)
{
size_t ubufsize, alloc_size, dvkm_zone_idx;
void * __capability ubuf = NULL;
int preserve_cheri_caps, is_disclosure;
char zone_name[ZONE_NAME_MAXLEN + 1];
uma_zone_t dvkm_zone;
int error;
ubuf = io->input_buffer;
ubufsize = io->input_buffer_size;
preserve_cheri_caps = io->preserve_cheri_caps;
is_disclosure = io->is_disclosure;
alloc_size = io->alloc_size;
if (ubuf == NULL) {
return (EINVAL);
}
if (bo_subobject) {
// hardcoded dvkm_zone
dvkm_zone = dvkm_zones[0];
error = buffer_overflow_uma_subobject(ubuf, ubufsize, dvkm_zone, preserve_cheri_caps, is_disclosure);
} else {
error = copyinstr(io->zone_name, zone_name, ZONE_NAME_MAXLEN + 1, NULL);
if (error) {
return (error);
}
// search for the requested zone in the existing dvkm zones
for (dvkm_zone_idx = 0; dvkm_zone_idx < dvkm_zones_count; ++dvkm_zone_idx) {
if (strcmp(dvkm_zones[dvkm_zone_idx]->uz_name, zone_name) == 0) {
break;
}
}
if (dvkm_zone_idx == dvkm_zones_count && dvkm_zones_count < MAX_DVKM_ZONES) {
dvkm_zone = uma_zcreate(zone_name, alloc_size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
dvkm_zones[dvkm_zones_count++] = dvkm_zone;
} else if (dvkm_zone_idx < dvkm_zones_count) { // found
dvkm_zone = dvkm_zones[dvkm_zone_idx];
} else {
return (EBUSY);
}
error = buffer_overflow_uma(ubuf, ubufsize, dvkm_zone, preserve_cheri_caps, is_disclosure);
}
return (error);
}