1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * UBSAN error reporting functions
4  *
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/bug.h>
11 #include <linux/ctype.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/sched.h>
16 #include <linux/uaccess.h>
17 #include <linux/ubsan.h>
18 #include <kunit/test-bug.h>
19 
20 #include "ubsan.h"
21 
22 #ifdef CONFIG_UBSAN_TRAP
23 /*
24  * Only include matches for UBSAN checks that are actually compiled in.
25  * The mappings of struct SanitizerKind (the -fsanitize=xxx args) to
26  * enum SanitizerHandler (the traps) in Clang is in clang/lib/CodeGen/.
27  */
report_ubsan_failure(struct pt_regs * regs,u32 check_type)28 const char *report_ubsan_failure(struct pt_regs *regs, u32 check_type)
29 {
30 	switch (check_type) {
31 #ifdef CONFIG_UBSAN_BOUNDS
32 	/*
33 	 * SanitizerKind::ArrayBounds and SanitizerKind::LocalBounds
34 	 * emit SanitizerHandler::OutOfBounds.
35 	 */
36 	case ubsan_out_of_bounds:
37 		return "UBSAN: array index out of bounds";
38 #endif
39 #ifdef CONFIG_UBSAN_SHIFT
40 	/*
41 	 * SanitizerKind::ShiftBase and SanitizerKind::ShiftExponent
42 	 * emit SanitizerHandler::ShiftOutOfBounds.
43 	 */
44 	case ubsan_shift_out_of_bounds:
45 		return "UBSAN: shift out of bounds";
46 #endif
47 #if defined(CONFIG_UBSAN_DIV_ZERO) || defined(CONFIG_UBSAN_INTEGER_WRAP)
48 	/*
49 	 * SanitizerKind::IntegerDivideByZero and
50 	 * SanitizerKind::SignedIntegerOverflow emit
51 	 * SanitizerHandler::DivremOverflow.
52 	 */
53 	case ubsan_divrem_overflow:
54 		return "UBSAN: divide/remainder overflow";
55 #endif
56 #ifdef CONFIG_UBSAN_UNREACHABLE
57 	/*
58 	 * SanitizerKind::Unreachable emits
59 	 * SanitizerHandler::BuiltinUnreachable.
60 	 */
61 	case ubsan_builtin_unreachable:
62 		return "UBSAN: unreachable code";
63 #endif
64 #if defined(CONFIG_UBSAN_BOOL) || defined(CONFIG_UBSAN_ENUM)
65 	/*
66 	 * SanitizerKind::Bool and SanitizerKind::Enum emit
67 	 * SanitizerHandler::LoadInvalidValue.
68 	 */
69 	case ubsan_load_invalid_value:
70 		return "UBSAN: loading invalid value";
71 #endif
72 #ifdef CONFIG_UBSAN_ALIGNMENT
73 	/*
74 	 * SanitizerKind::Alignment emits SanitizerHandler::TypeMismatch
75 	 * or SanitizerHandler::AlignmentAssumption.
76 	 */
77 	case ubsan_alignment_assumption:
78 		return "UBSAN: alignment assumption";
79 	case ubsan_type_mismatch:
80 		return "UBSAN: type mismatch";
81 #endif
82 #ifdef CONFIG_UBSAN_INTEGER_WRAP
83 	/*
84 	 * SanitizerKind::SignedIntegerOverflow emits
85 	 * SanitizerHandler::AddOverflow, SanitizerHandler::SubOverflow,
86 	 * or SanitizerHandler::MulOverflow.
87 	 */
88 	case ubsan_add_overflow:
89 		return "UBSAN: integer addition overflow";
90 	case ubsan_sub_overflow:
91 		return "UBSAN: integer subtraction overflow";
92 	case ubsan_mul_overflow:
93 		return "UBSAN: integer multiplication overflow";
94 #endif
95 	default:
96 		return "UBSAN: unrecognized failure code";
97 	}
98 }
99 
100 #else
101 static const char * const type_check_kinds[] = {
102 	"load of",
103 	"store to",
104 	"reference binding to",
105 	"member access within",
106 	"member call on",
107 	"constructor call on",
108 	"downcast of",
109 	"downcast of"
110 };
111 
112 #define REPORTED_BIT 31
113 
114 #if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
115 #define COLUMN_MASK (~(1U << REPORTED_BIT))
116 #define LINE_MASK   (~0U)
117 #else
118 #define COLUMN_MASK   (~0U)
119 #define LINE_MASK (~(1U << REPORTED_BIT))
120 #endif
121 
122 #define VALUE_LENGTH 40
123 
was_reported(struct source_location * location)124 static bool was_reported(struct source_location *location)
125 {
126 	return test_and_set_bit(REPORTED_BIT, &location->reported);
127 }
128 
suppress_report(struct source_location * loc)129 static bool suppress_report(struct source_location *loc)
130 {
131 	return current->in_ubsan || was_reported(loc);
132 }
133 
type_is_int(struct type_descriptor * type)134 static bool type_is_int(struct type_descriptor *type)
135 {
136 	return type->type_kind == type_kind_int;
137 }
138 
type_is_signed(struct type_descriptor * type)139 static bool type_is_signed(struct type_descriptor *type)
140 {
141 	WARN_ON(!type_is_int(type));
142 	return  type->type_info & 1;
143 }
144 
type_bit_width(struct type_descriptor * type)145 static unsigned type_bit_width(struct type_descriptor *type)
146 {
147 	return 1 << (type->type_info >> 1);
148 }
149 
is_inline_int(struct type_descriptor * type)150 static bool is_inline_int(struct type_descriptor *type)
151 {
152 	unsigned inline_bits = sizeof(unsigned long)*8;
153 	unsigned bits = type_bit_width(type);
154 
155 	WARN_ON(!type_is_int(type));
156 
157 	return bits <= inline_bits;
158 }
159 
get_signed_val(struct type_descriptor * type,void * val)160 static s_max get_signed_val(struct type_descriptor *type, void *val)
161 {
162 	if (is_inline_int(type)) {
163 		unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
164 		unsigned long ulong_val = (unsigned long)val;
165 
166 		return ((s_max)ulong_val) << extra_bits >> extra_bits;
167 	}
168 
169 	if (type_bit_width(type) == 64)
170 		return *(s64 *)val;
171 
172 	return *(s_max *)val;
173 }
174 
val_is_negative(struct type_descriptor * type,void * val)175 static bool val_is_negative(struct type_descriptor *type, void *val)
176 {
177 	return type_is_signed(type) && get_signed_val(type, val) < 0;
178 }
179 
get_unsigned_val(struct type_descriptor * type,void * val)180 static u_max get_unsigned_val(struct type_descriptor *type, void *val)
181 {
182 	if (is_inline_int(type))
183 		return (unsigned long)val;
184 
185 	if (type_bit_width(type) == 64)
186 		return *(u64 *)val;
187 
188 	return *(u_max *)val;
189 }
190 
val_to_string(char * str,size_t size,struct type_descriptor * type,void * value)191 static void val_to_string(char *str, size_t size, struct type_descriptor *type,
192 			void *value)
193 {
194 	if (type_is_int(type)) {
195 		if (type_bit_width(type) == 128) {
196 #if defined(CONFIG_ARCH_SUPPORTS_INT128)
197 			u_max val = get_unsigned_val(type, value);
198 
199 			scnprintf(str, size, "0x%08x%08x%08x%08x",
200 				(u32)(val >> 96),
201 				(u32)(val >> 64),
202 				(u32)(val >> 32),
203 				(u32)(val));
204 #else
205 			WARN_ON(1);
206 #endif
207 		} else if (type_is_signed(type)) {
208 			scnprintf(str, size, "%lld",
209 				(s64)get_signed_val(type, value));
210 		} else {
211 			scnprintf(str, size, "%llu",
212 				(u64)get_unsigned_val(type, value));
213 		}
214 	}
215 }
216 
ubsan_prologue(struct source_location * loc,const char * reason)217 static void ubsan_prologue(struct source_location *loc, const char *reason)
218 {
219 	current->in_ubsan++;
220 
221 	pr_warn(CUT_HERE);
222 
223 	pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name,
224 		loc->line & LINE_MASK, loc->column & COLUMN_MASK);
225 
226 	kunit_fail_current_test("%s in %s", reason, loc->file_name);
227 }
228 
ubsan_epilogue(void)229 static void ubsan_epilogue(void)
230 {
231 	dump_stack();
232 	pr_warn("---[ end trace ]---\n");
233 
234 	current->in_ubsan--;
235 
236 	check_panic_on_warn("UBSAN");
237 }
238 
handle_overflow(struct overflow_data * data,void * lhs,void * rhs,char op)239 static void handle_overflow(struct overflow_data *data, void *lhs,
240 			void *rhs, char op)
241 {
242 
243 	struct type_descriptor *type = data->type;
244 	char lhs_val_str[VALUE_LENGTH];
245 	char rhs_val_str[VALUE_LENGTH];
246 
247 	if (suppress_report(&data->location))
248 		return;
249 
250 	ubsan_prologue(&data->location, type_is_signed(type) ?
251 			"signed-integer-overflow" :
252 			"unsigned-integer-overflow");
253 
254 	val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
255 	val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
256 	pr_err("%s %c %s cannot be represented in type %s\n",
257 		lhs_val_str,
258 		op,
259 		rhs_val_str,
260 		type->type_name);
261 
262 	ubsan_epilogue();
263 }
264 
__ubsan_handle_add_overflow(void * data,void * lhs,void * rhs)265 void __ubsan_handle_add_overflow(void *data,
266 				void *lhs, void *rhs)
267 {
268 
269 	handle_overflow(data, lhs, rhs, '+');
270 }
271 EXPORT_SYMBOL(__ubsan_handle_add_overflow);
272 
__ubsan_handle_sub_overflow(void * data,void * lhs,void * rhs)273 void __ubsan_handle_sub_overflow(void *data,
274 				void *lhs, void *rhs)
275 {
276 	handle_overflow(data, lhs, rhs, '-');
277 }
278 EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
279 
__ubsan_handle_mul_overflow(void * data,void * lhs,void * rhs)280 void __ubsan_handle_mul_overflow(void *data,
281 				void *lhs, void *rhs)
282 {
283 	handle_overflow(data, lhs, rhs, '*');
284 }
285 EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
286 
__ubsan_handle_negate_overflow(void * _data,void * old_val)287 void __ubsan_handle_negate_overflow(void *_data, void *old_val)
288 {
289 	struct overflow_data *data = _data;
290 	char old_val_str[VALUE_LENGTH];
291 
292 	if (suppress_report(&data->location))
293 		return;
294 
295 	ubsan_prologue(&data->location, "negation-overflow");
296 
297 	val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
298 
299 	pr_err("negation of %s cannot be represented in type %s:\n",
300 		old_val_str, data->type->type_name);
301 
302 	ubsan_epilogue();
303 }
304 EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
305 
__ubsan_handle_implicit_conversion(void * _data,void * from_val,void * to_val)306 void __ubsan_handle_implicit_conversion(void *_data, void *from_val, void *to_val)
307 {
308 	struct implicit_conversion_data *data = _data;
309 	char from_val_str[VALUE_LENGTH];
310 	char to_val_str[VALUE_LENGTH];
311 
312 	if (suppress_report(&data->location))
313 		return;
314 
315 	val_to_string(from_val_str, sizeof(from_val_str), data->from_type, from_val);
316 	val_to_string(to_val_str, sizeof(to_val_str), data->to_type, to_val);
317 
318 	ubsan_prologue(&data->location, "implicit-conversion");
319 
320 	pr_err("cannot represent %s value %s during %s %s, truncated to %s\n",
321 		data->from_type->type_name,
322 		from_val_str,
323 		type_check_kinds[data->type_check_kind],
324 		data->to_type->type_name,
325 		to_val_str);
326 
327 	ubsan_epilogue();
328 }
329 EXPORT_SYMBOL(__ubsan_handle_implicit_conversion);
330 
__ubsan_handle_divrem_overflow(void * _data,void * lhs,void * rhs)331 void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
332 {
333 	struct overflow_data *data = _data;
334 	char rhs_val_str[VALUE_LENGTH];
335 
336 	if (suppress_report(&data->location))
337 		return;
338 
339 	ubsan_prologue(&data->location, "division-overflow");
340 
341 	val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
342 
343 	if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
344 		pr_err("division of %s by -1 cannot be represented in type %s\n",
345 			rhs_val_str, data->type->type_name);
346 	else
347 		pr_err("division by zero\n");
348 
349 	ubsan_epilogue();
350 }
351 EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
352 
handle_null_ptr_deref(struct type_mismatch_data_common * data)353 static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
354 {
355 	if (suppress_report(data->location))
356 		return;
357 
358 	ubsan_prologue(data->location, "null-ptr-deref");
359 
360 	pr_err("%s null pointer of type %s\n",
361 		type_check_kinds[data->type_check_kind],
362 		data->type->type_name);
363 
364 	ubsan_epilogue();
365 }
366 
handle_misaligned_access(struct type_mismatch_data_common * data,unsigned long ptr)367 static void handle_misaligned_access(struct type_mismatch_data_common *data,
368 				unsigned long ptr)
369 {
370 	if (suppress_report(data->location))
371 		return;
372 
373 	ubsan_prologue(data->location, "misaligned-access");
374 
375 	pr_err("%s misaligned address %p for type %s\n",
376 		type_check_kinds[data->type_check_kind],
377 		(void *)ptr, data->type->type_name);
378 	pr_err("which requires %ld byte alignment\n", data->alignment);
379 
380 	ubsan_epilogue();
381 }
382 
handle_object_size_mismatch(struct type_mismatch_data_common * data,unsigned long ptr)383 static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
384 					unsigned long ptr)
385 {
386 	if (suppress_report(data->location))
387 		return;
388 
389 	ubsan_prologue(data->location, "object-size-mismatch");
390 	pr_err("%s address %p with insufficient space\n",
391 		type_check_kinds[data->type_check_kind],
392 		(void *) ptr);
393 	pr_err("for an object of type %s\n", data->type->type_name);
394 	ubsan_epilogue();
395 }
396 
ubsan_type_mismatch_common(struct type_mismatch_data_common * data,unsigned long ptr)397 static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
398 				unsigned long ptr)
399 {
400 	unsigned long flags = user_access_save();
401 
402 	if (!ptr)
403 		handle_null_ptr_deref(data);
404 	else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
405 		handle_misaligned_access(data, ptr);
406 	else
407 		handle_object_size_mismatch(data, ptr);
408 
409 	user_access_restore(flags);
410 }
411 
__ubsan_handle_type_mismatch(struct type_mismatch_data * data,void * ptr)412 void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
413 				void *ptr)
414 {
415 	struct type_mismatch_data_common common_data = {
416 		.location = &data->location,
417 		.type = data->type,
418 		.alignment = data->alignment,
419 		.type_check_kind = data->type_check_kind
420 	};
421 
422 	ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
423 }
424 EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
425 
__ubsan_handle_type_mismatch_v1(void * _data,void * ptr)426 void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
427 {
428 	struct type_mismatch_data_v1 *data = _data;
429 	struct type_mismatch_data_common common_data = {
430 		.location = &data->location,
431 		.type = data->type,
432 		.alignment = 1UL << data->log_alignment,
433 		.type_check_kind = data->type_check_kind
434 	};
435 
436 	ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
437 }
438 EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
439 
__ubsan_handle_out_of_bounds(void * _data,void * index)440 void __ubsan_handle_out_of_bounds(void *_data, void *index)
441 {
442 	struct out_of_bounds_data *data = _data;
443 	char index_str[VALUE_LENGTH];
444 
445 	if (suppress_report(&data->location))
446 		return;
447 
448 	ubsan_prologue(&data->location, "array-index-out-of-bounds");
449 
450 	val_to_string(index_str, sizeof(index_str), data->index_type, index);
451 	pr_err("index %s is out of range for type %s\n", index_str,
452 		data->array_type->type_name);
453 	ubsan_epilogue();
454 }
455 EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
456 
__ubsan_handle_shift_out_of_bounds(void * _data,void * lhs,void * rhs)457 void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
458 {
459 	struct shift_out_of_bounds_data *data = _data;
460 	struct type_descriptor *rhs_type = data->rhs_type;
461 	struct type_descriptor *lhs_type = data->lhs_type;
462 	char rhs_str[VALUE_LENGTH];
463 	char lhs_str[VALUE_LENGTH];
464 	unsigned long ua_flags = user_access_save();
465 
466 	if (suppress_report(&data->location))
467 		goto out;
468 
469 	ubsan_prologue(&data->location, "shift-out-of-bounds");
470 
471 	val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
472 	val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
473 
474 	if (val_is_negative(rhs_type, rhs))
475 		pr_err("shift exponent %s is negative\n", rhs_str);
476 
477 	else if (get_unsigned_val(rhs_type, rhs) >=
478 		type_bit_width(lhs_type))
479 		pr_err("shift exponent %s is too large for %u-bit type %s\n",
480 			rhs_str,
481 			type_bit_width(lhs_type),
482 			lhs_type->type_name);
483 	else if (val_is_negative(lhs_type, lhs))
484 		pr_err("left shift of negative value %s\n",
485 			lhs_str);
486 	else
487 		pr_err("left shift of %s by %s places cannot be"
488 			" represented in type %s\n",
489 			lhs_str, rhs_str,
490 			lhs_type->type_name);
491 
492 	ubsan_epilogue();
493 out:
494 	user_access_restore(ua_flags);
495 }
496 EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
497 
498 
__ubsan_handle_builtin_unreachable(void * _data)499 void __ubsan_handle_builtin_unreachable(void *_data)
500 {
501 	struct unreachable_data *data = _data;
502 	ubsan_prologue(&data->location, "unreachable");
503 	pr_err("calling __builtin_unreachable()\n");
504 	ubsan_epilogue();
505 	panic("can't return from __builtin_unreachable()");
506 }
507 EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
508 
__ubsan_handle_load_invalid_value(void * _data,void * val)509 void __ubsan_handle_load_invalid_value(void *_data, void *val)
510 {
511 	struct invalid_value_data *data = _data;
512 	char val_str[VALUE_LENGTH];
513 	unsigned long ua_flags = user_access_save();
514 
515 	if (suppress_report(&data->location))
516 		goto out;
517 
518 	ubsan_prologue(&data->location, "invalid-load");
519 
520 	val_to_string(val_str, sizeof(val_str), data->type, val);
521 
522 	pr_err("load of value %s is not a valid value for type %s\n",
523 		val_str, data->type->type_name);
524 
525 	ubsan_epilogue();
526 out:
527 	user_access_restore(ua_flags);
528 }
529 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
530 
__ubsan_handle_alignment_assumption(void * _data,unsigned long ptr,unsigned long align,unsigned long offset)531 void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
532 					 unsigned long align,
533 					 unsigned long offset)
534 {
535 	struct alignment_assumption_data *data = _data;
536 	unsigned long real_ptr;
537 
538 	if (suppress_report(&data->location))
539 		return;
540 
541 	ubsan_prologue(&data->location, "alignment-assumption");
542 
543 	if (offset)
544 		pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
545 		       align, offset, data->type->type_name);
546 	else
547 		pr_err("assumption of %lu byte alignment for pointer of type %s failed",
548 		       align, data->type->type_name);
549 
550 	real_ptr = ptr - offset;
551 	pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
552 	       offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
553 	       real_ptr & (align - 1));
554 
555 	ubsan_epilogue();
556 }
557 EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
558 
559 #endif /* !CONFIG_UBSAN_TRAP */
560