forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/lib/ubsan.c
....@@ -1,13 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * UBSAN error reporting functions
34 *
45 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
56 * 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
- *
117 */
128
139 #include <linux/bitops.h>
....@@ -17,6 +13,7 @@
1713 #include <linux/kernel.h>
1814 #include <linux/types.h>
1915 #include <linux/sched.h>
16
+#include <linux/uaccess.h>
2017
2118 #include "ubsan.h"
2219
....@@ -46,13 +43,6 @@
4643 static bool was_reported(struct source_location *location)
4744 {
4845 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);
5646 }
5747
5848 static bool suppress_report(struct source_location *loc)
....@@ -122,7 +112,7 @@
122112 {
123113 if (type_is_int(type)) {
124114 if (type_bit_width(type) == 128) {
125
-#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
115
+#if defined(CONFIG_ARCH_SUPPORTS_INT128)
126116 u_max val = get_unsigned_val(type, value);
127117
128118 scnprintf(str, size, "0x%08x%08x%08x%08x",
....@@ -143,13 +133,14 @@
143133 }
144134 }
145135
146
-static void ubsan_prologue(struct source_location *location)
136
+static void ubsan_prologue(struct source_location *loc, const char *reason)
147137 {
148138 current->in_ubsan++;
149139
150140 pr_err("========================================"
151141 "========================================\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);
153144 }
154145
155146 static void ubsan_epilogue(void)
....@@ -159,85 +150,28 @@
159150 "========================================\n");
160151
161152 current->in_ubsan--;
153
+
154
+ if (panic_on_warn) {
155
+ /*
156
+ * This thread may hit another WARN() in the panic path.
157
+ * Resetting this prevents additional WARN() from panicking the
158
+ * system on this thread. Other threads are blocked by the
159
+ * panic_mutex in panic().
160
+ */
161
+ panic_on_warn = 0;
162
+ panic("panic_on_warn set ...\n");
163
+ }
162164 }
163165
164
-static void handle_overflow(struct overflow_data *data, void *lhs,
165
- void *rhs, char op)
166
+void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
166167 {
167
-
168
- struct type_descriptor *type = data->type;
169
- char lhs_val_str[VALUE_LENGTH];
168
+ struct overflow_data *data = _data;
170169 char rhs_val_str[VALUE_LENGTH];
171170
172171 if (suppress_report(&data->location))
173172 return;
174173
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);
174
+ ubsan_prologue(&data->location, "division-overflow");
241175
242176 val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
243177
....@@ -256,7 +190,7 @@
256190 if (suppress_report(data->location))
257191 return;
258192
259
- ubsan_prologue(data->location);
193
+ ubsan_prologue(data->location, "null-ptr-deref");
260194
261195 pr_err("%s null pointer of type %s\n",
262196 type_check_kinds[data->type_check_kind],
....@@ -271,7 +205,7 @@
271205 if (suppress_report(data->location))
272206 return;
273207
274
- ubsan_prologue(data->location);
208
+ ubsan_prologue(data->location, "misaligned-access");
275209
276210 pr_err("%s misaligned address %p for type %s\n",
277211 type_check_kinds[data->type_check_kind],
....@@ -287,7 +221,7 @@
287221 if (suppress_report(data->location))
288222 return;
289223
290
- ubsan_prologue(data->location);
224
+ ubsan_prologue(data->location, "object-size-mismatch");
291225 pr_err("%s address %p with insufficient space\n",
292226 type_check_kinds[data->type_check_kind],
293227 (void *) ptr);
....@@ -298,6 +232,7 @@
298232 static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
299233 unsigned long ptr)
300234 {
235
+ unsigned long flags = user_access_save();
301236
302237 if (!ptr)
303238 handle_null_ptr_deref(data);
....@@ -305,6 +240,8 @@
305240 handle_misaligned_access(data, ptr);
306241 else
307242 handle_object_size_mismatch(data, ptr);
243
+
244
+ user_access_restore(flags);
308245 }
309246
310247 void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
....@@ -321,10 +258,9 @@
321258 }
322259 EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
323260
324
-void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data,
325
- void *ptr)
261
+void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
326262 {
327
-
263
+ struct type_mismatch_data_v1 *data = _data;
328264 struct type_mismatch_data_common common_data = {
329265 .location = &data->location,
330266 .type = data->type,
....@@ -336,31 +272,15 @@
336272 }
337273 EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
338274
339
-void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
340
- void *bound)
275
+void __ubsan_handle_out_of_bounds(void *_data, void *index)
341276 {
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
-{
277
+ struct out_of_bounds_data *data = _data;
358278 char index_str[VALUE_LENGTH];
359279
360280 if (suppress_report(&data->location))
361281 return;
362282
363
- ubsan_prologue(&data->location);
283
+ ubsan_prologue(&data->location, "array-index-out-of-bounds");
364284
365285 val_to_string(index_str, sizeof(index_str), data->index_type, index);
366286 pr_err("index %s is out of range for type %s\n", index_str,
....@@ -369,18 +289,19 @@
369289 }
370290 EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
371291
372
-void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
373
- void *lhs, void *rhs)
292
+void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
374293 {
294
+ struct shift_out_of_bounds_data *data = _data;
375295 struct type_descriptor *rhs_type = data->rhs_type;
376296 struct type_descriptor *lhs_type = data->lhs_type;
377297 char rhs_str[VALUE_LENGTH];
378298 char lhs_str[VALUE_LENGTH];
299
+ unsigned long ua_flags = user_access_save();
379300
380301 if (suppress_report(&data->location))
381
- return;
302
+ goto out;
382303
383
- ubsan_prologue(&data->location);
304
+ ubsan_prologue(&data->location, "shift-out-of-bounds");
384305
385306 val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
386307 val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
....@@ -404,28 +325,31 @@
404325 lhs_type->type_name);
405326
406327 ubsan_epilogue();
328
+out:
329
+ user_access_restore(ua_flags);
407330 }
408331 EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
409332
410333
411
-void __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
334
+void __ubsan_handle_builtin_unreachable(void *_data)
412335 {
413
- ubsan_prologue(&data->location);
336
+ struct unreachable_data *data = _data;
337
+ ubsan_prologue(&data->location, "unreachable");
414338 pr_err("calling __builtin_unreachable()\n");
415339 ubsan_epilogue();
416340 panic("can't return from __builtin_unreachable()");
417341 }
418342 EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
419343
420
-void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
421
- void *val)
344
+void __ubsan_handle_load_invalid_value(void *_data, void *val)
422345 {
346
+ struct invalid_value_data *data = _data;
423347 char val_str[VALUE_LENGTH];
424348
425349 if (suppress_report(&data->location))
426350 return;
427351
428
- ubsan_prologue(&data->location);
352
+ ubsan_prologue(&data->location, "invalid-load");
429353
430354 val_to_string(val_str, sizeof(val_str), data->type, val);
431355
....@@ -435,3 +359,34 @@
435359 ubsan_epilogue();
436360 }
437361 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
362
+
363
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
364
+ unsigned long align,
365
+ unsigned long offset);
366
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
367
+ unsigned long align,
368
+ unsigned long offset)
369
+{
370
+ struct alignment_assumption_data *data = _data;
371
+ unsigned long real_ptr;
372
+
373
+ if (suppress_report(&data->location))
374
+ return;
375
+
376
+ ubsan_prologue(&data->location, "alignment-assumption");
377
+
378
+ if (offset)
379
+ pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
380
+ align, offset, data->type->type_name);
381
+ else
382
+ pr_err("assumption of %lu byte alignment for pointer of type %s failed",
383
+ align, data->type->type_name);
384
+
385
+ real_ptr = ptr - offset;
386
+ pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
387
+ offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
388
+ real_ptr & (align - 1));
389
+
390
+ ubsan_epilogue();
391
+}
392
+EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);