.. | .. |
---|
| 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 DEFINE_SPINLOCK(report_lock); |
---|
147 | | - |
---|
148 | | -static void ubsan_prologue(struct source_location *location, |
---|
149 | | - unsigned long *flags) |
---|
| 136 | +static void ubsan_prologue(struct source_location *loc, const char *reason) |
---|
150 | 137 | { |
---|
151 | 138 | current->in_ubsan++; |
---|
152 | | - spin_lock_irqsave(&report_lock, *flags); |
---|
153 | 139 | |
---|
154 | 140 | pr_err("========================================" |
---|
155 | 141 | "========================================\n"); |
---|
156 | | - 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); |
---|
157 | 144 | } |
---|
158 | 145 | |
---|
159 | | -static void ubsan_epilogue(unsigned long *flags) |
---|
| 146 | +static void ubsan_epilogue(void) |
---|
160 | 147 | { |
---|
161 | 148 | dump_stack(); |
---|
162 | 149 | pr_err("========================================" |
---|
163 | 150 | "========================================\n"); |
---|
164 | | - spin_unlock_irqrestore(&report_lock, *flags); |
---|
| 151 | + |
---|
165 | 152 | current->in_ubsan--; |
---|
| 153 | + |
---|
| 154 | + check_panic_on_warn("UBSAN"); |
---|
166 | 155 | } |
---|
167 | 156 | |
---|
168 | | -static void handle_overflow(struct overflow_data *data, void *lhs, |
---|
169 | | - void *rhs, char op) |
---|
| 157 | +void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs) |
---|
170 | 158 | { |
---|
171 | | - |
---|
172 | | - struct type_descriptor *type = data->type; |
---|
173 | | - unsigned long flags; |
---|
174 | | - char lhs_val_str[VALUE_LENGTH]; |
---|
| 159 | + struct overflow_data *data = _data; |
---|
175 | 160 | char rhs_val_str[VALUE_LENGTH]; |
---|
176 | 161 | |
---|
177 | 162 | if (suppress_report(&data->location)) |
---|
178 | 163 | return; |
---|
179 | 164 | |
---|
180 | | - ubsan_prologue(&data->location, &flags); |
---|
181 | | - |
---|
182 | | - val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs); |
---|
183 | | - val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs); |
---|
184 | | - pr_err("%s integer overflow:\n", |
---|
185 | | - type_is_signed(type) ? "signed" : "unsigned"); |
---|
186 | | - pr_err("%s %c %s cannot be represented in type %s\n", |
---|
187 | | - lhs_val_str, |
---|
188 | | - op, |
---|
189 | | - rhs_val_str, |
---|
190 | | - type->type_name); |
---|
191 | | - |
---|
192 | | - ubsan_epilogue(&flags); |
---|
193 | | -} |
---|
194 | | - |
---|
195 | | -void __ubsan_handle_add_overflow(struct overflow_data *data, |
---|
196 | | - void *lhs, void *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 | | - void *lhs, void *rhs) |
---|
205 | | -{ |
---|
206 | | - handle_overflow(data, lhs, rhs, '-'); |
---|
207 | | -} |
---|
208 | | -EXPORT_SYMBOL(__ubsan_handle_sub_overflow); |
---|
209 | | - |
---|
210 | | -void __ubsan_handle_mul_overflow(struct overflow_data *data, |
---|
211 | | - void *lhs, void *rhs) |
---|
212 | | -{ |
---|
213 | | - handle_overflow(data, lhs, rhs, '*'); |
---|
214 | | -} |
---|
215 | | -EXPORT_SYMBOL(__ubsan_handle_mul_overflow); |
---|
216 | | - |
---|
217 | | -void __ubsan_handle_negate_overflow(struct overflow_data *data, |
---|
218 | | - void *old_val) |
---|
219 | | -{ |
---|
220 | | - unsigned long flags; |
---|
221 | | - char old_val_str[VALUE_LENGTH]; |
---|
222 | | - |
---|
223 | | - if (suppress_report(&data->location)) |
---|
224 | | - return; |
---|
225 | | - |
---|
226 | | - ubsan_prologue(&data->location, &flags); |
---|
227 | | - |
---|
228 | | - val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val); |
---|
229 | | - |
---|
230 | | - pr_err("negation of %s cannot be represented in type %s:\n", |
---|
231 | | - old_val_str, data->type->type_name); |
---|
232 | | - |
---|
233 | | - ubsan_epilogue(&flags); |
---|
234 | | -} |
---|
235 | | -EXPORT_SYMBOL(__ubsan_handle_negate_overflow); |
---|
236 | | - |
---|
237 | | - |
---|
238 | | -void __ubsan_handle_divrem_overflow(struct overflow_data *data, |
---|
239 | | - void *lhs, void *rhs) |
---|
240 | | -{ |
---|
241 | | - unsigned long flags; |
---|
242 | | - char rhs_val_str[VALUE_LENGTH]; |
---|
243 | | - |
---|
244 | | - if (suppress_report(&data->location)) |
---|
245 | | - return; |
---|
246 | | - |
---|
247 | | - ubsan_prologue(&data->location, &flags); |
---|
| 165 | + ubsan_prologue(&data->location, "division-overflow"); |
---|
248 | 166 | |
---|
249 | 167 | val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs); |
---|
250 | 168 | |
---|
.. | .. |
---|
254 | 172 | else |
---|
255 | 173 | pr_err("division by zero\n"); |
---|
256 | 174 | |
---|
257 | | - ubsan_epilogue(&flags); |
---|
| 175 | + ubsan_epilogue(); |
---|
258 | 176 | } |
---|
259 | 177 | EXPORT_SYMBOL(__ubsan_handle_divrem_overflow); |
---|
260 | 178 | |
---|
261 | 179 | static void handle_null_ptr_deref(struct type_mismatch_data_common *data) |
---|
262 | 180 | { |
---|
263 | | - unsigned long flags; |
---|
264 | | - |
---|
265 | 181 | if (suppress_report(data->location)) |
---|
266 | 182 | return; |
---|
267 | 183 | |
---|
268 | | - ubsan_prologue(data->location, &flags); |
---|
| 184 | + ubsan_prologue(data->location, "null-ptr-deref"); |
---|
269 | 185 | |
---|
270 | 186 | pr_err("%s null pointer of type %s\n", |
---|
271 | 187 | type_check_kinds[data->type_check_kind], |
---|
272 | 188 | data->type->type_name); |
---|
273 | 189 | |
---|
274 | | - ubsan_epilogue(&flags); |
---|
| 190 | + ubsan_epilogue(); |
---|
275 | 191 | } |
---|
276 | 192 | |
---|
277 | 193 | static void handle_misaligned_access(struct type_mismatch_data_common *data, |
---|
278 | 194 | unsigned long ptr) |
---|
279 | 195 | { |
---|
280 | | - unsigned long flags; |
---|
281 | | - |
---|
282 | 196 | if (suppress_report(data->location)) |
---|
283 | 197 | return; |
---|
284 | 198 | |
---|
285 | | - ubsan_prologue(data->location, &flags); |
---|
| 199 | + ubsan_prologue(data->location, "misaligned-access"); |
---|
286 | 200 | |
---|
287 | 201 | pr_err("%s misaligned address %p for type %s\n", |
---|
288 | 202 | type_check_kinds[data->type_check_kind], |
---|
289 | 203 | (void *)ptr, data->type->type_name); |
---|
290 | 204 | pr_err("which requires %ld byte alignment\n", data->alignment); |
---|
291 | 205 | |
---|
292 | | - ubsan_epilogue(&flags); |
---|
| 206 | + ubsan_epilogue(); |
---|
293 | 207 | } |
---|
294 | 208 | |
---|
295 | 209 | static void handle_object_size_mismatch(struct type_mismatch_data_common *data, |
---|
296 | 210 | unsigned long ptr) |
---|
297 | 211 | { |
---|
298 | | - unsigned long flags; |
---|
299 | | - |
---|
300 | 212 | if (suppress_report(data->location)) |
---|
301 | 213 | return; |
---|
302 | 214 | |
---|
303 | | - ubsan_prologue(data->location, &flags); |
---|
| 215 | + ubsan_prologue(data->location, "object-size-mismatch"); |
---|
304 | 216 | pr_err("%s address %p with insufficient space\n", |
---|
305 | 217 | type_check_kinds[data->type_check_kind], |
---|
306 | 218 | (void *) ptr); |
---|
307 | 219 | pr_err("for an object of type %s\n", data->type->type_name); |
---|
308 | | - ubsan_epilogue(&flags); |
---|
| 220 | + ubsan_epilogue(); |
---|
309 | 221 | } |
---|
310 | 222 | |
---|
311 | 223 | static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data, |
---|
312 | 224 | unsigned long ptr) |
---|
313 | 225 | { |
---|
| 226 | + unsigned long flags = user_access_save(); |
---|
314 | 227 | |
---|
315 | 228 | if (!ptr) |
---|
316 | 229 | handle_null_ptr_deref(data); |
---|
.. | .. |
---|
318 | 231 | handle_misaligned_access(data, ptr); |
---|
319 | 232 | else |
---|
320 | 233 | handle_object_size_mismatch(data, ptr); |
---|
| 234 | + |
---|
| 235 | + user_access_restore(flags); |
---|
321 | 236 | } |
---|
322 | 237 | |
---|
323 | 238 | void __ubsan_handle_type_mismatch(struct type_mismatch_data *data, |
---|
.. | .. |
---|
334 | 249 | } |
---|
335 | 250 | EXPORT_SYMBOL(__ubsan_handle_type_mismatch); |
---|
336 | 251 | |
---|
337 | | -void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data, |
---|
338 | | - void *ptr) |
---|
| 252 | +void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr) |
---|
339 | 253 | { |
---|
340 | | - |
---|
| 254 | + struct type_mismatch_data_v1 *data = _data; |
---|
341 | 255 | struct type_mismatch_data_common common_data = { |
---|
342 | 256 | .location = &data->location, |
---|
343 | 257 | .type = data->type, |
---|
.. | .. |
---|
349 | 263 | } |
---|
350 | 264 | EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1); |
---|
351 | 265 | |
---|
352 | | -void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data, |
---|
353 | | - void *bound) |
---|
| 266 | +void __ubsan_handle_out_of_bounds(void *_data, void *index) |
---|
354 | 267 | { |
---|
355 | | - unsigned long flags; |
---|
356 | | - char bound_str[VALUE_LENGTH]; |
---|
357 | | - |
---|
358 | | - if (suppress_report(&data->location)) |
---|
359 | | - return; |
---|
360 | | - |
---|
361 | | - ubsan_prologue(&data->location, &flags); |
---|
362 | | - |
---|
363 | | - val_to_string(bound_str, sizeof(bound_str), data->type, bound); |
---|
364 | | - pr_err("variable length array bound value %s <= 0\n", bound_str); |
---|
365 | | - |
---|
366 | | - ubsan_epilogue(&flags); |
---|
367 | | -} |
---|
368 | | -EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive); |
---|
369 | | - |
---|
370 | | -void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data, void *index) |
---|
371 | | -{ |
---|
372 | | - unsigned long flags; |
---|
| 268 | + struct out_of_bounds_data *data = _data; |
---|
373 | 269 | char index_str[VALUE_LENGTH]; |
---|
374 | 270 | |
---|
375 | 271 | if (suppress_report(&data->location)) |
---|
376 | 272 | return; |
---|
377 | 273 | |
---|
378 | | - ubsan_prologue(&data->location, &flags); |
---|
| 274 | + ubsan_prologue(&data->location, "array-index-out-of-bounds"); |
---|
379 | 275 | |
---|
380 | 276 | val_to_string(index_str, sizeof(index_str), data->index_type, index); |
---|
381 | 277 | pr_err("index %s is out of range for type %s\n", index_str, |
---|
382 | 278 | data->array_type->type_name); |
---|
383 | | - ubsan_epilogue(&flags); |
---|
| 279 | + ubsan_epilogue(); |
---|
384 | 280 | } |
---|
385 | 281 | EXPORT_SYMBOL(__ubsan_handle_out_of_bounds); |
---|
386 | 282 | |
---|
387 | | -void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data, |
---|
388 | | - void *lhs, void *rhs) |
---|
| 283 | +void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs) |
---|
389 | 284 | { |
---|
390 | | - unsigned long flags; |
---|
| 285 | + struct shift_out_of_bounds_data *data = _data; |
---|
391 | 286 | struct type_descriptor *rhs_type = data->rhs_type; |
---|
392 | 287 | struct type_descriptor *lhs_type = data->lhs_type; |
---|
393 | 288 | char rhs_str[VALUE_LENGTH]; |
---|
394 | 289 | char lhs_str[VALUE_LENGTH]; |
---|
| 290 | + unsigned long ua_flags = user_access_save(); |
---|
395 | 291 | |
---|
396 | 292 | if (suppress_report(&data->location)) |
---|
397 | | - return; |
---|
| 293 | + goto out; |
---|
398 | 294 | |
---|
399 | | - ubsan_prologue(&data->location, &flags); |
---|
| 295 | + ubsan_prologue(&data->location, "shift-out-of-bounds"); |
---|
400 | 296 | |
---|
401 | 297 | val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs); |
---|
402 | 298 | val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs); |
---|
.. | .. |
---|
419 | 315 | lhs_str, rhs_str, |
---|
420 | 316 | lhs_type->type_name); |
---|
421 | 317 | |
---|
422 | | - ubsan_epilogue(&flags); |
---|
| 318 | + ubsan_epilogue(); |
---|
| 319 | +out: |
---|
| 320 | + user_access_restore(ua_flags); |
---|
423 | 321 | } |
---|
424 | 322 | EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds); |
---|
425 | 323 | |
---|
426 | 324 | |
---|
427 | | -void __ubsan_handle_builtin_unreachable(struct unreachable_data *data) |
---|
| 325 | +void __ubsan_handle_builtin_unreachable(void *_data) |
---|
428 | 326 | { |
---|
429 | | - unsigned long flags; |
---|
430 | | - |
---|
431 | | - ubsan_prologue(&data->location, &flags); |
---|
| 327 | + struct unreachable_data *data = _data; |
---|
| 328 | + ubsan_prologue(&data->location, "unreachable"); |
---|
432 | 329 | pr_err("calling __builtin_unreachable()\n"); |
---|
433 | | - ubsan_epilogue(&flags); |
---|
| 330 | + ubsan_epilogue(); |
---|
434 | 331 | panic("can't return from __builtin_unreachable()"); |
---|
435 | 332 | } |
---|
436 | 333 | EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable); |
---|
437 | 334 | |
---|
438 | | -void __ubsan_handle_load_invalid_value(struct invalid_value_data *data, |
---|
439 | | - void *val) |
---|
| 335 | +void __ubsan_handle_load_invalid_value(void *_data, void *val) |
---|
440 | 336 | { |
---|
441 | | - unsigned long flags; |
---|
| 337 | + struct invalid_value_data *data = _data; |
---|
442 | 338 | char val_str[VALUE_LENGTH]; |
---|
443 | 339 | |
---|
444 | 340 | if (suppress_report(&data->location)) |
---|
445 | 341 | return; |
---|
446 | 342 | |
---|
447 | | - ubsan_prologue(&data->location, &flags); |
---|
| 343 | + ubsan_prologue(&data->location, "invalid-load"); |
---|
448 | 344 | |
---|
449 | 345 | val_to_string(val_str, sizeof(val_str), data->type, val); |
---|
450 | 346 | |
---|
451 | 347 | pr_err("load of value %s is not a valid value for type %s\n", |
---|
452 | 348 | val_str, data->type->type_name); |
---|
453 | 349 | |
---|
454 | | - ubsan_epilogue(&flags); |
---|
| 350 | + ubsan_epilogue(); |
---|
455 | 351 | } |
---|
456 | 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); |
---|