xref: /linux-6.15/lib/ubsan.c (revision d08965a2)
1 /*
2  * UBSAN error reporting functions
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  * Author: Andrey Ryabinin <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12 
13 #include <linux/bitops.h>
14 #include <linux/bug.h>
15 #include <linux/ctype.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/sched.h>
20 #include <linux/uaccess.h>
21 
22 #include "ubsan.h"
23 
24 const char *type_check_kinds[] = {
25 	"load of",
26 	"store to",
27 	"reference binding to",
28 	"member access within",
29 	"member call on",
30 	"constructor call on",
31 	"downcast of",
32 	"downcast of"
33 };
34 
35 #define REPORTED_BIT 31
36 
37 #if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
38 #define COLUMN_MASK (~(1U << REPORTED_BIT))
39 #define LINE_MASK   (~0U)
40 #else
41 #define COLUMN_MASK   (~0U)
42 #define LINE_MASK (~(1U << REPORTED_BIT))
43 #endif
44 
45 #define VALUE_LENGTH 40
46 
47 static bool was_reported(struct source_location *location)
48 {
49 	return test_and_set_bit(REPORTED_BIT, &location->reported);
50 }
51 
52 static void print_source_location(const char *prefix,
53 				struct source_location *loc)
54 {
55 	pr_err("%s %s:%d:%d\n", prefix, loc->file_name,
56 		loc->line & LINE_MASK, loc->column & COLUMN_MASK);
57 }
58 
59 static bool suppress_report(struct source_location *loc)
60 {
61 	return current->in_ubsan || was_reported(loc);
62 }
63 
64 static bool type_is_int(struct type_descriptor *type)
65 {
66 	return type->type_kind == type_kind_int;
67 }
68 
69 static bool type_is_signed(struct type_descriptor *type)
70 {
71 	WARN_ON(!type_is_int(type));
72 	return  type->type_info & 1;
73 }
74 
75 static unsigned type_bit_width(struct type_descriptor *type)
76 {
77 	return 1 << (type->type_info >> 1);
78 }
79 
80 static bool is_inline_int(struct type_descriptor *type)
81 {
82 	unsigned inline_bits = sizeof(unsigned long)*8;
83 	unsigned bits = type_bit_width(type);
84 
85 	WARN_ON(!type_is_int(type));
86 
87 	return bits <= inline_bits;
88 }
89 
90 static s_max get_signed_val(struct type_descriptor *type, unsigned long val)
91 {
92 	if (is_inline_int(type)) {
93 		unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
94 		return ((s_max)val) << extra_bits >> extra_bits;
95 	}
96 
97 	if (type_bit_width(type) == 64)
98 		return *(s64 *)val;
99 
100 	return *(s_max *)val;
101 }
102 
103 static bool val_is_negative(struct type_descriptor *type, unsigned long val)
104 {
105 	return type_is_signed(type) && get_signed_val(type, val) < 0;
106 }
107 
108 static u_max get_unsigned_val(struct type_descriptor *type, unsigned long val)
109 {
110 	if (is_inline_int(type))
111 		return val;
112 
113 	if (type_bit_width(type) == 64)
114 		return *(u64 *)val;
115 
116 	return *(u_max *)val;
117 }
118 
119 static void val_to_string(char *str, size_t size, struct type_descriptor *type,
120 	unsigned long value)
121 {
122 	if (type_is_int(type)) {
123 		if (type_bit_width(type) == 128) {
124 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
125 			u_max val = get_unsigned_val(type, value);
126 
127 			scnprintf(str, size, "0x%08x%08x%08x%08x",
128 				(u32)(val >> 96),
129 				(u32)(val >> 64),
130 				(u32)(val >> 32),
131 				(u32)(val));
132 #else
133 			WARN_ON(1);
134 #endif
135 		} else if (type_is_signed(type)) {
136 			scnprintf(str, size, "%lld",
137 				(s64)get_signed_val(type, value));
138 		} else {
139 			scnprintf(str, size, "%llu",
140 				(u64)get_unsigned_val(type, value));
141 		}
142 	}
143 }
144 
145 static DEFINE_SPINLOCK(report_lock);
146 
147 static void ubsan_prologue(struct source_location *location,
148 			unsigned long *flags)
149 {
150 	current->in_ubsan++;
151 	spin_lock_irqsave(&report_lock, *flags);
152 
153 	pr_err("========================================"
154 		"========================================\n");
155 	print_source_location("UBSAN: Undefined behaviour in", location);
156 }
157 
158 static void ubsan_epilogue(unsigned long *flags)
159 {
160 	dump_stack();
161 	pr_err("========================================"
162 		"========================================\n");
163 	spin_unlock_irqrestore(&report_lock, *flags);
164 	current->in_ubsan--;
165 }
166 
167 static void handle_overflow(struct overflow_data *data, unsigned long lhs,
168 			unsigned long rhs, char op)
169 {
170 
171 	struct type_descriptor *type = data->type;
172 	unsigned long flags;
173 	char lhs_val_str[VALUE_LENGTH];
174 	char rhs_val_str[VALUE_LENGTH];
175 
176 	if (suppress_report(&data->location))
177 		return;
178 
179 	ubsan_prologue(&data->location, &flags);
180 
181 	val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
182 	val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
183 	pr_err("%s integer overflow:\n",
184 		type_is_signed(type) ? "signed" : "unsigned");
185 	pr_err("%s %c %s cannot be represented in type %s\n",
186 		lhs_val_str,
187 		op,
188 		rhs_val_str,
189 		type->type_name);
190 
191 	ubsan_epilogue(&flags);
192 }
193 
194 void __ubsan_handle_add_overflow(struct overflow_data *data,
195 				unsigned long lhs,
196 				unsigned long rhs)
197 {
198 
199 	handle_overflow(data, lhs, rhs, '+');
200 }
201 EXPORT_SYMBOL(__ubsan_handle_add_overflow);
202 
203 void __ubsan_handle_sub_overflow(struct overflow_data *data,
204 				unsigned long lhs,
205 				unsigned long rhs)
206 {
207 	handle_overflow(data, lhs, rhs, '-');
208 }
209 EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
210 
211 void __ubsan_handle_mul_overflow(struct overflow_data *data,
212 				unsigned long lhs,
213 				unsigned long rhs)
214 {
215 	handle_overflow(data, lhs, rhs, '*');
216 }
217 EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
218 
219 void __ubsan_handle_negate_overflow(struct overflow_data *data,
220 				unsigned long old_val)
221 {
222 	unsigned long flags;
223 	char old_val_str[VALUE_LENGTH];
224 
225 	if (suppress_report(&data->location))
226 		return;
227 
228 	ubsan_prologue(&data->location, &flags);
229 
230 	val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
231 
232 	pr_err("negation of %s cannot be represented in type %s:\n",
233 		old_val_str, data->type->type_name);
234 
235 	ubsan_epilogue(&flags);
236 }
237 EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
238 
239 
240 void __ubsan_handle_divrem_overflow(struct overflow_data *data,
241 				unsigned long lhs,
242 				unsigned long rhs)
243 {
244 	unsigned long flags;
245 	char rhs_val_str[VALUE_LENGTH];
246 
247 	if (suppress_report(&data->location))
248 		return;
249 
250 	ubsan_prologue(&data->location, &flags);
251 
252 	val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
253 
254 	if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
255 		pr_err("division of %s by -1 cannot be represented in type %s\n",
256 			rhs_val_str, data->type->type_name);
257 	else
258 		pr_err("division by zero\n");
259 
260 	ubsan_epilogue(&flags);
261 }
262 EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
263 
264 static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
265 {
266 	unsigned long flags;
267 
268 	if (suppress_report(data->location))
269 		return;
270 
271 	ubsan_prologue(data->location, &flags);
272 
273 	pr_err("%s null pointer of type %s\n",
274 		type_check_kinds[data->type_check_kind],
275 		data->type->type_name);
276 
277 	ubsan_epilogue(&flags);
278 }
279 
280 static void handle_misaligned_access(struct type_mismatch_data_common *data,
281 				unsigned long ptr)
282 {
283 	unsigned long flags;
284 
285 	if (suppress_report(data->location))
286 		return;
287 
288 	ubsan_prologue(data->location, &flags);
289 
290 	pr_err("%s misaligned address %p for type %s\n",
291 		type_check_kinds[data->type_check_kind],
292 		(void *)ptr, data->type->type_name);
293 	pr_err("which requires %ld byte alignment\n", data->alignment);
294 
295 	ubsan_epilogue(&flags);
296 }
297 
298 static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
299 					unsigned long ptr)
300 {
301 	unsigned long flags;
302 
303 	if (suppress_report(data->location))
304 		return;
305 
306 	ubsan_prologue(data->location, &flags);
307 	pr_err("%s address %p with insufficient space\n",
308 		type_check_kinds[data->type_check_kind],
309 		(void *) ptr);
310 	pr_err("for an object of type %s\n", data->type->type_name);
311 	ubsan_epilogue(&flags);
312 }
313 
314 static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
315 				unsigned long ptr)
316 {
317 	unsigned long flags = user_access_save();
318 
319 	if (!ptr)
320 		handle_null_ptr_deref(data);
321 	else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
322 		handle_misaligned_access(data, ptr);
323 	else
324 		handle_object_size_mismatch(data, ptr);
325 
326 	user_access_restore(flags);
327 }
328 
329 void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
330 				unsigned long ptr)
331 {
332 	struct type_mismatch_data_common common_data = {
333 		.location = &data->location,
334 		.type = data->type,
335 		.alignment = data->alignment,
336 		.type_check_kind = data->type_check_kind
337 	};
338 
339 	ubsan_type_mismatch_common(&common_data, ptr);
340 }
341 EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
342 
343 void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data,
344 				unsigned long ptr)
345 {
346 
347 	struct type_mismatch_data_common common_data = {
348 		.location = &data->location,
349 		.type = data->type,
350 		.alignment = 1UL << data->log_alignment,
351 		.type_check_kind = data->type_check_kind
352 	};
353 
354 	ubsan_type_mismatch_common(&common_data, ptr);
355 }
356 EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
357 
358 void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
359 					unsigned long bound)
360 {
361 	unsigned long flags;
362 	char bound_str[VALUE_LENGTH];
363 
364 	if (suppress_report(&data->location))
365 		return;
366 
367 	ubsan_prologue(&data->location, &flags);
368 
369 	val_to_string(bound_str, sizeof(bound_str), data->type, bound);
370 	pr_err("variable length array bound value %s <= 0\n", bound_str);
371 
372 	ubsan_epilogue(&flags);
373 }
374 EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive);
375 
376 void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data,
377 				unsigned long index)
378 {
379 	unsigned long flags;
380 	char index_str[VALUE_LENGTH];
381 
382 	if (suppress_report(&data->location))
383 		return;
384 
385 	ubsan_prologue(&data->location, &flags);
386 
387 	val_to_string(index_str, sizeof(index_str), data->index_type, index);
388 	pr_err("index %s is out of range for type %s\n", index_str,
389 		data->array_type->type_name);
390 	ubsan_epilogue(&flags);
391 }
392 EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
393 
394 void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
395 					unsigned long lhs, unsigned long rhs)
396 {
397 	unsigned long flags;
398 	struct type_descriptor *rhs_type = data->rhs_type;
399 	struct type_descriptor *lhs_type = data->lhs_type;
400 	char rhs_str[VALUE_LENGTH];
401 	char lhs_str[VALUE_LENGTH];
402 
403 	if (suppress_report(&data->location))
404 		return;
405 
406 	ubsan_prologue(&data->location, &flags);
407 
408 	val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
409 	val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
410 
411 	if (val_is_negative(rhs_type, rhs))
412 		pr_err("shift exponent %s is negative\n", rhs_str);
413 
414 	else if (get_unsigned_val(rhs_type, rhs) >=
415 		type_bit_width(lhs_type))
416 		pr_err("shift exponent %s is too large for %u-bit type %s\n",
417 			rhs_str,
418 			type_bit_width(lhs_type),
419 			lhs_type->type_name);
420 	else if (val_is_negative(lhs_type, lhs))
421 		pr_err("left shift of negative value %s\n",
422 			lhs_str);
423 	else
424 		pr_err("left shift of %s by %s places cannot be"
425 			" represented in type %s\n",
426 			lhs_str, rhs_str,
427 			lhs_type->type_name);
428 
429 	ubsan_epilogue(&flags);
430 }
431 EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
432 
433 
434 void __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
435 {
436 	unsigned long flags;
437 
438 	ubsan_prologue(&data->location, &flags);
439 	pr_err("calling __builtin_unreachable()\n");
440 	ubsan_epilogue(&flags);
441 	panic("can't return from __builtin_unreachable()");
442 }
443 EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
444 
445 void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
446 				unsigned long val)
447 {
448 	unsigned long flags;
449 	char val_str[VALUE_LENGTH];
450 
451 	if (suppress_report(&data->location))
452 		return;
453 
454 	ubsan_prologue(&data->location, &flags);
455 
456 	val_to_string(val_str, sizeof(val_str), data->type, val);
457 
458 	pr_err("load of value %s is not a valid value for type %s\n",
459 		val_str, data->type->type_name);
460 
461 	ubsan_epilogue(&flags);
462 }
463 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
464