.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * UBSAN error reporting functions |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. |
---|
5 | 6 | * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> |
---|
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 | 7 | */ |
---|
12 | 8 | |
---|
13 | 9 | #include <linux/bitops.h> |
---|
.. | .. |
---|
17 | 13 | #include <linux/kernel.h> |
---|
18 | 14 | #include <linux/types.h> |
---|
19 | 15 | #include <linux/sched.h> |
---|
| 16 | +#include <linux/uaccess.h> |
---|
20 | 17 | |
---|
21 | 18 | #include "ubsan.h" |
---|
22 | 19 | |
---|
.. | .. |
---|
46 | 43 | static bool was_reported(struct source_location *location) |
---|
47 | 44 | { |
---|
48 | 45 | return test_and_set_bit(REPORTED_BIT, &location->reported); |
---|
49 | | -} |
---|
50 | | - |
---|
51 | | -static void print_source_location(const char *prefix, |
---|
52 | | - struct source_location *loc) |
---|
53 | | -{ |
---|
54 | | - pr_err("%s %s:%d:%d\n", prefix, loc->file_name, |
---|
55 | | - loc->line & LINE_MASK, loc->column & COLUMN_MASK); |
---|
56 | 46 | } |
---|
57 | 47 | |
---|
58 | 48 | static bool suppress_report(struct source_location *loc) |
---|
.. | .. |
---|
122 | 112 | { |
---|
123 | 113 | if (type_is_int(type)) { |
---|
124 | 114 | if (type_bit_width(type) == 128) { |
---|
125 | | -#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__) |
---|
| 115 | +#if defined(CONFIG_ARCH_SUPPORTS_INT128) |
---|
126 | 116 | u_max val = get_unsigned_val(type, value); |
---|
127 | 117 | |
---|
128 | 118 | scnprintf(str, size, "0x%08x%08x%08x%08x", |
---|
.. | .. |
---|
143 | 133 | } |
---|
144 | 134 | } |
---|
145 | 135 | |
---|
146 | | -static void ubsan_prologue(struct source_location *location) |
---|
| 136 | +static void ubsan_prologue(struct source_location *loc, const char *reason) |
---|
147 | 137 | { |
---|
148 | 138 | current->in_ubsan++; |
---|
149 | 139 | |
---|
150 | 140 | pr_err("========================================" |
---|
151 | 141 | "========================================\n"); |
---|
152 | | - print_source_location("UBSAN: Undefined behaviour in", location); |
---|
| 142 | + pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name, |
---|
| 143 | + loc->line & LINE_MASK, loc->column & COLUMN_MASK); |
---|
153 | 144 | } |
---|
154 | 145 | |
---|
155 | 146 | static void ubsan_epilogue(void) |
---|
.. | .. |
---|
159 | 150 | "========================================\n"); |
---|
160 | 151 | |
---|
161 | 152 | current->in_ubsan--; |
---|
| 153 | + |
---|
| 154 | + check_panic_on_warn("UBSAN"); |
---|
162 | 155 | } |
---|
163 | 156 | |
---|
164 | | -static void handle_overflow(struct overflow_data *data, void *lhs, |
---|
165 | | - void *rhs, char op) |
---|
| 157 | +void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs) |
---|
166 | 158 | { |
---|
167 | | - |
---|
168 | | - struct type_descriptor *type = data->type; |
---|
169 | | - char lhs_val_str[VALUE_LENGTH]; |
---|
| 159 | + struct overflow_data *data = _data; |
---|
170 | 160 | char rhs_val_str[VALUE_LENGTH]; |
---|
171 | 161 | |
---|
172 | 162 | if (suppress_report(&data->location)) |
---|
173 | 163 | return; |
---|
174 | 164 | |
---|
175 | | - ubsan_prologue(&data->location); |
---|
176 | | - |
---|
177 | | - val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs); |
---|
178 | | - val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs); |
---|
179 | | - pr_err("%s integer overflow:\n", |
---|
180 | | - type_is_signed(type) ? "signed" : "unsigned"); |
---|
181 | | - pr_err("%s %c %s cannot be represented in type %s\n", |
---|
182 | | - lhs_val_str, |
---|
183 | | - op, |
---|
184 | | - rhs_val_str, |
---|
185 | | - type->type_name); |
---|
186 | | - |
---|
187 | | - ubsan_epilogue(); |
---|
188 | | -} |
---|
189 | | - |
---|
190 | | -void __ubsan_handle_add_overflow(struct overflow_data *data, |
---|
191 | | - void *lhs, void *rhs) |
---|
192 | | -{ |
---|
193 | | - |
---|
194 | | - handle_overflow(data, lhs, rhs, '+'); |
---|
195 | | -} |
---|
196 | | -EXPORT_SYMBOL(__ubsan_handle_add_overflow); |
---|
197 | | - |
---|
198 | | -void __ubsan_handle_sub_overflow(struct overflow_data *data, |
---|
199 | | - void *lhs, void *rhs) |
---|
200 | | -{ |
---|
201 | | - handle_overflow(data, lhs, rhs, '-'); |
---|
202 | | -} |
---|
203 | | -EXPORT_SYMBOL(__ubsan_handle_sub_overflow); |
---|
204 | | - |
---|
205 | | -void __ubsan_handle_mul_overflow(struct overflow_data *data, |
---|
206 | | - void *lhs, void *rhs) |
---|
207 | | -{ |
---|
208 | | - handle_overflow(data, lhs, rhs, '*'); |
---|
209 | | -} |
---|
210 | | -EXPORT_SYMBOL(__ubsan_handle_mul_overflow); |
---|
211 | | - |
---|
212 | | -void __ubsan_handle_negate_overflow(struct overflow_data *data, |
---|
213 | | - void *old_val) |
---|
214 | | -{ |
---|
215 | | - char old_val_str[VALUE_LENGTH]; |
---|
216 | | - |
---|
217 | | - if (suppress_report(&data->location)) |
---|
218 | | - return; |
---|
219 | | - |
---|
220 | | - ubsan_prologue(&data->location); |
---|
221 | | - |
---|
222 | | - val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val); |
---|
223 | | - |
---|
224 | | - pr_err("negation of %s cannot be represented in type %s:\n", |
---|
225 | | - old_val_str, data->type->type_name); |
---|
226 | | - |
---|
227 | | - ubsan_epilogue(); |
---|
228 | | -} |
---|
229 | | -EXPORT_SYMBOL(__ubsan_handle_negate_overflow); |
---|
230 | | - |
---|
231 | | - |
---|
232 | | -void __ubsan_handle_divrem_overflow(struct overflow_data *data, |
---|
233 | | - void *lhs, void *rhs) |
---|
234 | | -{ |
---|
235 | | - char rhs_val_str[VALUE_LENGTH]; |
---|
236 | | - |
---|
237 | | - if (suppress_report(&data->location)) |
---|
238 | | - return; |
---|
239 | | - |
---|
240 | | - ubsan_prologue(&data->location); |
---|
| 165 | + ubsan_prologue(&data->location, "division-overflow"); |
---|
241 | 166 | |
---|
242 | 167 | val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs); |
---|
243 | 168 | |
---|
.. | .. |
---|
256 | 181 | if (suppress_report(data->location)) |
---|
257 | 182 | return; |
---|
258 | 183 | |
---|
259 | | - ubsan_prologue(data->location); |
---|
| 184 | + ubsan_prologue(data->location, "null-ptr-deref"); |
---|
260 | 185 | |
---|
261 | 186 | pr_err("%s null pointer of type %s\n", |
---|
262 | 187 | type_check_kinds[data->type_check_kind], |
---|
.. | .. |
---|
271 | 196 | if (suppress_report(data->location)) |
---|
272 | 197 | return; |
---|
273 | 198 | |
---|
274 | | - ubsan_prologue(data->location); |
---|
| 199 | + ubsan_prologue(data->location, "misaligned-access"); |
---|
275 | 200 | |
---|
276 | 201 | pr_err("%s misaligned address %p for type %s\n", |
---|
277 | 202 | type_check_kinds[data->type_check_kind], |
---|
.. | .. |
---|
287 | 212 | if (suppress_report(data->location)) |
---|
288 | 213 | return; |
---|
289 | 214 | |
---|
290 | | - ubsan_prologue(data->location); |
---|
| 215 | + ubsan_prologue(data->location, "object-size-mismatch"); |
---|
291 | 216 | pr_err("%s address %p with insufficient space\n", |
---|
292 | 217 | type_check_kinds[data->type_check_kind], |
---|
293 | 218 | (void *) ptr); |
---|
.. | .. |
---|
298 | 223 | static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data, |
---|
299 | 224 | unsigned long ptr) |
---|
300 | 225 | { |
---|
| 226 | + unsigned long flags = user_access_save(); |
---|
301 | 227 | |
---|
302 | 228 | if (!ptr) |
---|
303 | 229 | handle_null_ptr_deref(data); |
---|
.. | .. |
---|
305 | 231 | handle_misaligned_access(data, ptr); |
---|
306 | 232 | else |
---|
307 | 233 | handle_object_size_mismatch(data, ptr); |
---|
| 234 | + |
---|
| 235 | + user_access_restore(flags); |
---|
308 | 236 | } |
---|
309 | 237 | |
---|
310 | 238 | void __ubsan_handle_type_mismatch(struct type_mismatch_data *data, |
---|
.. | .. |
---|
321 | 249 | } |
---|
322 | 250 | EXPORT_SYMBOL(__ubsan_handle_type_mismatch); |
---|
323 | 251 | |
---|
324 | | -void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data, |
---|
325 | | - void *ptr) |
---|
| 252 | +void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr) |
---|
326 | 253 | { |
---|
327 | | - |
---|
| 254 | + struct type_mismatch_data_v1 *data = _data; |
---|
328 | 255 | struct type_mismatch_data_common common_data = { |
---|
329 | 256 | .location = &data->location, |
---|
330 | 257 | .type = data->type, |
---|
.. | .. |
---|
336 | 263 | } |
---|
337 | 264 | EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1); |
---|
338 | 265 | |
---|
339 | | -void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data, |
---|
340 | | - void *bound) |
---|
| 266 | +void __ubsan_handle_out_of_bounds(void *_data, void *index) |
---|
341 | 267 | { |
---|
342 | | - char bound_str[VALUE_LENGTH]; |
---|
343 | | - |
---|
344 | | - if (suppress_report(&data->location)) |
---|
345 | | - return; |
---|
346 | | - |
---|
347 | | - ubsan_prologue(&data->location); |
---|
348 | | - |
---|
349 | | - val_to_string(bound_str, sizeof(bound_str), data->type, bound); |
---|
350 | | - pr_err("variable length array bound value %s <= 0\n", bound_str); |
---|
351 | | - |
---|
352 | | - ubsan_epilogue(); |
---|
353 | | -} |
---|
354 | | -EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive); |
---|
355 | | - |
---|
356 | | -void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data, void *index) |
---|
357 | | -{ |
---|
| 268 | + struct out_of_bounds_data *data = _data; |
---|
358 | 269 | char index_str[VALUE_LENGTH]; |
---|
359 | 270 | |
---|
360 | 271 | if (suppress_report(&data->location)) |
---|
361 | 272 | return; |
---|
362 | 273 | |
---|
363 | | - ubsan_prologue(&data->location); |
---|
| 274 | + ubsan_prologue(&data->location, "array-index-out-of-bounds"); |
---|
364 | 275 | |
---|
365 | 276 | val_to_string(index_str, sizeof(index_str), data->index_type, index); |
---|
366 | 277 | pr_err("index %s is out of range for type %s\n", index_str, |
---|
.. | .. |
---|
369 | 280 | } |
---|
370 | 281 | EXPORT_SYMBOL(__ubsan_handle_out_of_bounds); |
---|
371 | 282 | |
---|
372 | | -void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data, |
---|
373 | | - void *lhs, void *rhs) |
---|
| 283 | +void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs) |
---|
374 | 284 | { |
---|
| 285 | + struct shift_out_of_bounds_data *data = _data; |
---|
375 | 286 | struct type_descriptor *rhs_type = data->rhs_type; |
---|
376 | 287 | struct type_descriptor *lhs_type = data->lhs_type; |
---|
377 | 288 | char rhs_str[VALUE_LENGTH]; |
---|
378 | 289 | char lhs_str[VALUE_LENGTH]; |
---|
| 290 | + unsigned long ua_flags = user_access_save(); |
---|
379 | 291 | |
---|
380 | 292 | if (suppress_report(&data->location)) |
---|
381 | | - return; |
---|
| 293 | + goto out; |
---|
382 | 294 | |
---|
383 | | - ubsan_prologue(&data->location); |
---|
| 295 | + ubsan_prologue(&data->location, "shift-out-of-bounds"); |
---|
384 | 296 | |
---|
385 | 297 | val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs); |
---|
386 | 298 | val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs); |
---|
.. | .. |
---|
404 | 316 | lhs_type->type_name); |
---|
405 | 317 | |
---|
406 | 318 | ubsan_epilogue(); |
---|
| 319 | +out: |
---|
| 320 | + user_access_restore(ua_flags); |
---|
407 | 321 | } |
---|
408 | 322 | EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds); |
---|
409 | 323 | |
---|
410 | 324 | |
---|
411 | | -void __ubsan_handle_builtin_unreachable(struct unreachable_data *data) |
---|
| 325 | +void __ubsan_handle_builtin_unreachable(void *_data) |
---|
412 | 326 | { |
---|
413 | | - ubsan_prologue(&data->location); |
---|
| 327 | + struct unreachable_data *data = _data; |
---|
| 328 | + ubsan_prologue(&data->location, "unreachable"); |
---|
414 | 329 | pr_err("calling __builtin_unreachable()\n"); |
---|
415 | 330 | ubsan_epilogue(); |
---|
416 | 331 | panic("can't return from __builtin_unreachable()"); |
---|
417 | 332 | } |
---|
418 | 333 | EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable); |
---|
419 | 334 | |
---|
420 | | -void __ubsan_handle_load_invalid_value(struct invalid_value_data *data, |
---|
421 | | - void *val) |
---|
| 335 | +void __ubsan_handle_load_invalid_value(void *_data, void *val) |
---|
422 | 336 | { |
---|
| 337 | + struct invalid_value_data *data = _data; |
---|
423 | 338 | char val_str[VALUE_LENGTH]; |
---|
424 | 339 | |
---|
425 | 340 | if (suppress_report(&data->location)) |
---|
426 | 341 | return; |
---|
427 | 342 | |
---|
428 | | - ubsan_prologue(&data->location); |
---|
| 343 | + ubsan_prologue(&data->location, "invalid-load"); |
---|
429 | 344 | |
---|
430 | 345 | val_to_string(val_str, sizeof(val_str), data->type, val); |
---|
431 | 346 | |
---|
.. | .. |
---|
435 | 350 | ubsan_epilogue(); |
---|
436 | 351 | } |
---|
437 | 352 | EXPORT_SYMBOL(__ubsan_handle_load_invalid_value); |
---|
| 353 | + |
---|
| 354 | +void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr, |
---|
| 355 | + unsigned long align, |
---|
| 356 | + unsigned long offset); |
---|
| 357 | +void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr, |
---|
| 358 | + unsigned long align, |
---|
| 359 | + unsigned long offset) |
---|
| 360 | +{ |
---|
| 361 | + struct alignment_assumption_data *data = _data; |
---|
| 362 | + unsigned long real_ptr; |
---|
| 363 | + |
---|
| 364 | + if (suppress_report(&data->location)) |
---|
| 365 | + return; |
---|
| 366 | + |
---|
| 367 | + ubsan_prologue(&data->location, "alignment-assumption"); |
---|
| 368 | + |
---|
| 369 | + if (offset) |
---|
| 370 | + pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed", |
---|
| 371 | + align, offset, data->type->type_name); |
---|
| 372 | + else |
---|
| 373 | + pr_err("assumption of %lu byte alignment for pointer of type %s failed", |
---|
| 374 | + align, data->type->type_name); |
---|
| 375 | + |
---|
| 376 | + real_ptr = ptr - offset; |
---|
| 377 | + pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes", |
---|
| 378 | + offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0), |
---|
| 379 | + real_ptr & (align - 1)); |
---|
| 380 | + |
---|
| 381 | + ubsan_epilogue(); |
---|
| 382 | +} |
---|
| 383 | +EXPORT_SYMBOL(__ubsan_handle_alignment_assumption); |
---|