ubsan.c 13 KB

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