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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/* $OpenBSD: subr_kubsan.c,v 1.2 2019/03/19 20:13:54 anton Exp $ */
/*
* Copyright (c) 2019 Anton Lindqvist <anton@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/syslimits.h>
#include <sys/systm.h>
#define NUMBER_BUFSIZ 32
#define LOCATION_BUFSIZ (PATH_MAX + 32) /* filename:line:column */
#define LOCATION_REPORTED (1U << 31)
#define NBITS(typ) (1 << ((typ)->t_info >> 1))
#define SIGNED(typ) ((typ)->t_info & 1)
struct type_descriptor {
uint16_t t_kind;
uint16_t t_info;
char t_name[1]; /* type name as variable length array */
};
struct source_location {
const char *sl_filename;
uint32_t sl_line;
uint32_t sl_column;
};
struct invalid_value_data {
struct source_location d_src;
struct type_descriptor *d_type;
};
struct out_of_bounds_data {
struct source_location d_src;
struct type_descriptor *d_atype; /* array type */
struct type_descriptor *d_itype; /* index type */
};
struct overflow_data {
struct source_location d_src;
struct type_descriptor *d_type;
};
struct pointer_overflow_data {
struct source_location d_src;
};
struct shift_out_of_bounds_data {
struct source_location d_src;
struct type_descriptor *d_ltype;
struct type_descriptor *d_rtype;
};
struct unreachable_data {
struct source_location d_src;
};
struct type_mismatch {
struct source_location d_src;
struct type_descriptor *d_type;
uint8_t d_align; /* log2 alignment */
uint8_t d_kind;
};
void kubsan_handle_load_invalid_value(struct invalid_value_data *,
unsigned long);
void kubsan_handle_negate_overflow(struct overflow_data *, unsigned long);
void kubsan_handle_out_of_bounds(struct out_of_bounds_data *, unsigned long);
void kubsan_handle_overflow(struct overflow_data *, unsigned long,
unsigned long, char);
void kubsan_handle_pointer_overflow(struct pointer_overflow_data *,
unsigned long, unsigned long);
void kubsan_handle_type_mismatch(struct type_mismatch *, unsigned long);
void kubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *,
unsigned long, unsigned long);
void kubsan_handle_ureachable(struct unreachable_data *);
int64_t kubsan_deserialize_int(struct type_descriptor *,
unsigned long);
uint64_t kubsan_deserialize_uint(struct type_descriptor *,
unsigned long);
void kubsan_format_int(struct type_descriptor *, unsigned long,
char *, size_t);
void kubsan_format_location(struct source_location *, char *,
size_t);
int kubsan_is_reported(struct source_location *);
const char *kubsan_kind(uint8_t);
void kubsan_report(const char *, ...)
__attribute__((__format__(__kprintf__, 1, 2)));
static int is_negative(struct type_descriptor *, unsigned long);
static int is_shift_exponent_too_large(struct type_descriptor *,
unsigned long);
#ifdef KUBSAN_WATCH
int kubsan_watch = 2;
#else
int kubsan_watch = 1;
#endif
/*
* Compiling the kernel with `-fsanitize=undefined' will cause the following
* functions to be called when a sanitizer detects undefined behavior.
* Some sanitizers are omitted since they are only applicable to C++.
*
* Every __ubsan_*() sanitizer function also has a corresponding
* __ubsan_*_abort() function as part of the ABI provided by Clang.
* But, since the kernel never is compiled with `fno-sanitize-recover' for
* obvious reasons, they are also omitted.
*/
void
__ubsan_handle_add_overflow(struct overflow_data *data,
unsigned long lhs, unsigned long rhs)
{
kubsan_handle_overflow(data, lhs, rhs, '+');
}
void
__ubsan_handle_builtin_unreachable(struct unreachable_data *data)
{
kubsan_handle_ureachable(data);
}
void
__ubsan_handle_divrem_overflow(struct overflow_data *data,
unsigned long lhs, unsigned long rhs)
{
kubsan_handle_overflow(data, lhs, rhs, '/');
}
void
__ubsan_handle_load_invalid_value(struct invalid_value_data *data,
unsigned long val)
{
kubsan_handle_load_invalid_value(data, val);
}
void
__ubsan_handle_mul_overflow(struct overflow_data *data,
unsigned long lhs, unsigned long rhs)
{
kubsan_handle_overflow(data, lhs, rhs, '*');
}
void
__ubsan_handle_negate_overflow(struct overflow_data *data, unsigned long val)
{
kubsan_handle_negate_overflow(data, val);
}
void
__ubsan_handle_out_of_bounds(struct out_of_bounds_data *data,
unsigned long idx)
{
kubsan_handle_out_of_bounds(data, idx);
}
void
__ubsan_handle_pointer_overflow(struct pointer_overflow_data *data,
unsigned long base, unsigned long res)
{
kubsan_handle_pointer_overflow(data, base, res);
}
void
__ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
unsigned long lhs, unsigned long rhs)
{
kubsan_handle_shift_out_of_bounds(data, lhs, rhs);
}
void
__ubsan_handle_sub_overflow(struct overflow_data *data,
unsigned long lhs, unsigned long rhs)
{
kubsan_handle_overflow(data, lhs, rhs, '-');
}
void
__ubsan_handle_type_mismatch_v1(struct type_mismatch *data,
unsigned long ptr)
{
kubsan_handle_type_mismatch(data, ptr);
}
void
kubsan_handle_load_invalid_value(struct invalid_value_data *data,
unsigned long val)
{
char bloc[LOCATION_BUFSIZ];
char bval[NUMBER_BUFSIZ];
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
kubsan_format_int(data->d_type, val, bval, sizeof(bval));
kubsan_report("kubsan: %s: load invalid value: load of value %s is "
"not a valid value for type %s\n",
bloc, bval, data->d_type->t_name);
}
void
kubsan_handle_negate_overflow(struct overflow_data *data, unsigned long val)
{
char bloc[LOCATION_BUFSIZ];
char bval[NUMBER_BUFSIZ];
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
kubsan_format_int(data->d_type, val, bval, sizeof(bval));
kubsan_report("kubsan: %s: negate overflow: negation of %s cannot be "
"represented in type %s\n",
bloc, bval, data->d_type->t_name);
}
void
kubsan_handle_out_of_bounds(struct out_of_bounds_data *data,
unsigned long idx)
{
char bloc[LOCATION_BUFSIZ];
char bidx[NUMBER_BUFSIZ];
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
kubsan_format_int(data->d_itype, idx, bidx, sizeof(bidx));
kubsan_report("kubsan: %s: out of bounds: index %s is out of range "
"for type %s\n",
bloc, bidx, data->d_atype->t_name);
}
void
kubsan_handle_overflow(struct overflow_data *data, unsigned long rhs,
unsigned long lhs, char op)
{
char bloc[LOCATION_BUFSIZ];
char blhs[NUMBER_BUFSIZ];
char brhs[NUMBER_BUFSIZ];
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
kubsan_format_int(data->d_type, lhs, blhs, sizeof(blhs));
kubsan_format_int(data->d_type, rhs, brhs, sizeof(brhs));
kubsan_report("kubsan: %s: %s integer overflow: %s %c %s cannot "
"be represented in type %s\n",
bloc, SIGNED(data->d_type) ? "signed" : "unsigned",
blhs, op, brhs, data->d_type->t_name);
}
void
kubsan_handle_pointer_overflow(struct pointer_overflow_data *data,
unsigned long base, unsigned long res)
{
char bloc[LOCATION_BUFSIZ];
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
kubsan_report("kubsan: %s: pointer overflow: pointer expression with"
" base %#lx overflowed to %#lx\n",
bloc, base, res);
}
void
kubsan_handle_type_mismatch(struct type_mismatch *data, unsigned long ptr)
{
char bloc[LOCATION_BUFSIZ];
unsigned long align = 1UL << data->d_align;
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
if (ptr == 0UL)
kubsan_report("kubsan: %s: type mismatch: %s null pointer of "
"type %s\n",
bloc, kubsan_kind(data->d_kind), data->d_type->t_name);
else if (ptr & (align - 1))
kubsan_report("kubsan: %s: type mismatch: %s misaligned address "
"%p for type %s which requires %lu byte alignment\n",
bloc, kubsan_kind(data->d_kind), (void *)ptr,
data->d_type->t_name, align);
else
kubsan_report("kubsan: %s: type mismatch: %s address %p with "
"insufficient space for an object of type %s\n",
bloc, kubsan_kind(data->d_kind), (void *)ptr,
data->d_type->t_name);
}
void
kubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
unsigned long lhs, unsigned long rhs)
{
char bloc[LOCATION_BUFSIZ];
char blhs[NUMBER_BUFSIZ];
char brhs[NUMBER_BUFSIZ];
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
kubsan_format_int(data->d_ltype, lhs, blhs, sizeof(blhs));
kubsan_format_int(data->d_rtype, rhs, brhs, sizeof(brhs));
if (is_negative(data->d_rtype, rhs))
kubsan_report("kubsan: %s: shift: shift exponent %s is "
"negative\n",
bloc, brhs);
else if (is_shift_exponent_too_large(data->d_rtype, rhs))
kubsan_report("kubsan: %s: shift: shift exponent %s is too "
"large for %u-bit type\n",
bloc, brhs, NBITS(data->d_rtype));
else if (is_negative(data->d_ltype, lhs))
kubsan_report("kubsan: %s: shift: left shift of negative "
"value %s\n",
bloc, blhs);
else
kubsan_report("kubsan: %s: shift: left shift of %s by %s "
"places cannot be represented in type %s\n",
bloc, blhs, brhs, data->d_ltype->t_name);
}
void
kubsan_handle_ureachable(struct unreachable_data *data)
{
char bloc[LOCATION_BUFSIZ];
if (kubsan_is_reported(&data->d_src))
return;
kubsan_format_location(&data->d_src, bloc, sizeof(bloc));
kubsan_report("kubsan: %s: unreachable: calling "
"__builtin_unreachable()\n",
bloc);
}
int64_t
kubsan_deserialize_int(struct type_descriptor *typ, unsigned long val)
{
switch (NBITS(typ)) {
case 8:
return ((int8_t)val);
case 16:
return ((int16_t)val);
case 32:
return ((int32_t)val);
case 64:
default:
return ((int64_t)val);
}
}
uint64_t
kubsan_deserialize_uint(struct type_descriptor *typ, unsigned long val)
{
switch (NBITS(typ)) {
case 8:
return ((uint8_t)val);
case 16:
return ((uint16_t)val);
case 32:
return ((uint32_t)val);
case 64:
default:
return ((uint64_t)val);
}
}
void
kubsan_format_int(struct type_descriptor *typ, unsigned long val,
char *buf, size_t bufsiz)
{
switch (typ->t_kind) {
case 0: /* integer */
if (SIGNED(typ)) {
int64_t i = kubsan_deserialize_int(typ, val);
snprintf(buf, bufsiz, "%lld", i);
} else {
uint64_t u = kubsan_deserialize_uint(typ, val);
snprintf(buf, bufsiz, "%llu", u);
}
break;
default:
snprintf(buf, bufsiz, "%#x<NaN>", typ->t_kind);
}
}
void
kubsan_format_location(struct source_location *src, char *buf,
size_t bufsiz)
{
snprintf(buf, bufsiz, "%s:%u:%u",
src->sl_filename, src->sl_line & ~LOCATION_REPORTED,
src->sl_column);
}
int
kubsan_is_reported(struct source_location *src)
{
uint32_t *line = &src->sl_line;
uint32_t prev;
/*
* Treat everything as reported when disabled.
* Otherwise, new violations would go by unnoticed.
*/
if (__predict_false(kubsan_watch == 0))
return (1);
do {
prev = *line;
/* If already reported, avoid redundant atomic operation. */
if (prev & LOCATION_REPORTED)
break;
} while (atomic_cas_uint(line, prev, prev | LOCATION_REPORTED) != prev);
return (prev & LOCATION_REPORTED);
}
const char *
kubsan_kind(uint8_t kind)
{
static const char *kinds[] = {
"load of",
"store to",
"reference binding to",
"member access within",
"member call on",
"constructor call on",
"downcast of",
"downcast of",
"upcast of",
"cast to virtual base of",
"_Nonnull binding to",
"dynamic operation on"
};
if (kind >= nitems(kinds))
return ("?");
return (kinds[kind]);
}
void
kubsan_report(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
#ifdef DDB
if (kubsan_watch == 2)
db_enter();
#endif
}
static int
is_negative(struct type_descriptor *typ, unsigned long val)
{
return (SIGNED(typ) && kubsan_deserialize_int(typ, val) < 0);
}
static int
is_shift_exponent_too_large(struct type_descriptor *typ, unsigned long val)
{
return (kubsan_deserialize_int(typ, val) >= NBITS(typ));
}
|