ubsan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. #if defined(CONFIG_UBSAN_TRAP) || defined(CONFIG_UBSAN_KVM_EL2)
  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(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_INTEGER_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_INTEGER_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. #endif
  97. #ifndef CONFIG_UBSAN_TRAP
  98. static const char * const type_check_kinds[] = {
  99. "load of",
  100. "store to",
  101. "reference binding to",
  102. "member access within",
  103. "member call on",
  104. "constructor call on",
  105. "downcast of",
  106. "downcast of"
  107. };
  108. #define REPORTED_BIT 31
  109. #if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
  110. #define COLUMN_MASK (~(1U << REPORTED_BIT))
  111. #define LINE_MASK (~0U)
  112. #else
  113. #define COLUMN_MASK (~0U)
  114. #define LINE_MASK (~(1U << REPORTED_BIT))
  115. #endif
  116. #define VALUE_LENGTH 40
  117. static bool was_reported(struct source_location *location)
  118. {
  119. return test_and_set_bit(REPORTED_BIT, &location->reported);
  120. }
  121. static bool suppress_report(struct source_location *loc)
  122. {
  123. return current->in_ubsan || was_reported(loc);
  124. }
  125. static bool type_is_int(struct type_descriptor *type)
  126. {
  127. return type->type_kind == type_kind_int;
  128. }
  129. static bool type_is_signed(struct type_descriptor *type)
  130. {
  131. WARN_ON(!type_is_int(type));
  132. return type->type_info & 1;
  133. }
  134. static unsigned type_bit_width(struct type_descriptor *type)
  135. {
  136. return 1 << (type->type_info >> 1);
  137. }
  138. static bool is_inline_int(struct type_descriptor *type)
  139. {
  140. unsigned inline_bits = sizeof(unsigned long)*8;
  141. unsigned bits = type_bit_width(type);
  142. WARN_ON(!type_is_int(type));
  143. return bits <= inline_bits;
  144. }
  145. static s_max get_signed_val(struct type_descriptor *type, void *val)
  146. {
  147. if (is_inline_int(type)) {
  148. unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
  149. unsigned long ulong_val = (unsigned long)val;
  150. return ((s_max)ulong_val) << extra_bits >> extra_bits;
  151. }
  152. if (type_bit_width(type) == 64)
  153. return *(s64 *)val;
  154. return *(s_max *)val;
  155. }
  156. static bool val_is_negative(struct type_descriptor *type, void *val)
  157. {
  158. return type_is_signed(type) && get_signed_val(type, val) < 0;
  159. }
  160. static u_max get_unsigned_val(struct type_descriptor *type, void *val)
  161. {
  162. if (is_inline_int(type))
  163. return (unsigned long)val;
  164. if (type_bit_width(type) == 64)
  165. return *(u64 *)val;
  166. return *(u_max *)val;
  167. }
  168. static void val_to_string(char *str, size_t size, struct type_descriptor *type,
  169. void *value)
  170. {
  171. if (type_is_int(type)) {
  172. if (type_bit_width(type) == 128) {
  173. #if defined(CONFIG_ARCH_SUPPORTS_INT128)
  174. u_max val = get_unsigned_val(type, value);
  175. scnprintf(str, size, "0x%08x%08x%08x%08x",
  176. (u32)(val >> 96),
  177. (u32)(val >> 64),
  178. (u32)(val >> 32),
  179. (u32)(val));
  180. #else
  181. WARN_ON(1);
  182. #endif
  183. } else if (type_is_signed(type)) {
  184. scnprintf(str, size, "%lld",
  185. (s64)get_signed_val(type, value));
  186. } else {
  187. scnprintf(str, size, "%llu",
  188. (u64)get_unsigned_val(type, value));
  189. }
  190. }
  191. }
  192. static void ubsan_prologue(struct source_location *loc, const char *reason)
  193. {
  194. current->in_ubsan++;
  195. pr_warn(CUT_HERE);
  196. pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name,
  197. loc->line & LINE_MASK, loc->column & COLUMN_MASK);
  198. kunit_fail_current_test("%s in %s", reason, loc->file_name);
  199. }
  200. static void ubsan_epilogue(void)
  201. {
  202. dump_stack();
  203. pr_warn("---[ end trace ]---\n");
  204. current->in_ubsan--;
  205. check_panic_on_warn("UBSAN");
  206. }
  207. static void handle_overflow(struct overflow_data *data, void *lhs,
  208. void *rhs, char op)
  209. {
  210. struct type_descriptor *type = data->type;
  211. char lhs_val_str[VALUE_LENGTH];
  212. char rhs_val_str[VALUE_LENGTH];
  213. if (suppress_report(&data->location))
  214. return;
  215. ubsan_prologue(&data->location, type_is_signed(type) ?
  216. "signed-integer-overflow" :
  217. "unsigned-integer-overflow");
  218. val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
  219. val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
  220. pr_err("%s %c %s cannot be represented in type %s\n",
  221. lhs_val_str,
  222. op,
  223. rhs_val_str,
  224. type->type_name);
  225. ubsan_epilogue();
  226. }
  227. void __ubsan_handle_add_overflow(void *data,
  228. void *lhs, void *rhs)
  229. {
  230. handle_overflow(data, lhs, rhs, '+');
  231. }
  232. EXPORT_SYMBOL(__ubsan_handle_add_overflow);
  233. void __ubsan_handle_sub_overflow(void *data,
  234. void *lhs, void *rhs)
  235. {
  236. handle_overflow(data, lhs, rhs, '-');
  237. }
  238. EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
  239. void __ubsan_handle_mul_overflow(void *data,
  240. void *lhs, void *rhs)
  241. {
  242. handle_overflow(data, lhs, rhs, '*');
  243. }
  244. EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
  245. void __ubsan_handle_negate_overflow(void *_data, void *old_val)
  246. {
  247. struct overflow_data *data = _data;
  248. char old_val_str[VALUE_LENGTH];
  249. if (suppress_report(&data->location))
  250. return;
  251. ubsan_prologue(&data->location, "negation-overflow");
  252. val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
  253. pr_err("negation of %s cannot be represented in type %s:\n",
  254. old_val_str, data->type->type_name);
  255. ubsan_epilogue();
  256. }
  257. EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
  258. void __ubsan_handle_implicit_conversion(void *_data, void *from_val, void *to_val)
  259. {
  260. struct implicit_conversion_data *data = _data;
  261. char from_val_str[VALUE_LENGTH];
  262. char to_val_str[VALUE_LENGTH];
  263. if (suppress_report(&data->location))
  264. return;
  265. val_to_string(from_val_str, sizeof(from_val_str), data->from_type, from_val);
  266. val_to_string(to_val_str, sizeof(to_val_str), data->to_type, to_val);
  267. ubsan_prologue(&data->location, "implicit-conversion");
  268. pr_err("cannot represent %s value %s during %s %s, truncated to %s\n",
  269. data->from_type->type_name,
  270. from_val_str,
  271. type_check_kinds[data->type_check_kind],
  272. data->to_type->type_name,
  273. to_val_str);
  274. ubsan_epilogue();
  275. }
  276. EXPORT_SYMBOL(__ubsan_handle_implicit_conversion);
  277. void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
  278. {
  279. struct overflow_data *data = _data;
  280. char lhs_val_str[VALUE_LENGTH];
  281. if (suppress_report(&data->location))
  282. return;
  283. ubsan_prologue(&data->location, "division-overflow");
  284. val_to_string(lhs_val_str, sizeof(lhs_val_str), data->type, lhs);
  285. if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
  286. pr_err("division of %s by -1 cannot be represented in type %s\n",
  287. lhs_val_str, data->type->type_name);
  288. else
  289. pr_err("division by zero\n");
  290. ubsan_epilogue();
  291. }
  292. EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
  293. static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
  294. {
  295. if (suppress_report(data->location))
  296. return;
  297. ubsan_prologue(data->location, "null-ptr-deref");
  298. pr_err("%s null pointer of type %s\n",
  299. type_check_kinds[data->type_check_kind],
  300. data->type->type_name);
  301. ubsan_epilogue();
  302. }
  303. static void handle_misaligned_access(struct type_mismatch_data_common *data,
  304. unsigned long ptr)
  305. {
  306. if (suppress_report(data->location))
  307. return;
  308. ubsan_prologue(data->location, "misaligned-access");
  309. pr_err("%s misaligned address %p for type %s\n",
  310. type_check_kinds[data->type_check_kind],
  311. (void *)ptr, data->type->type_name);
  312. pr_err("which requires %ld byte alignment\n", data->alignment);
  313. ubsan_epilogue();
  314. }
  315. static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
  316. unsigned long ptr)
  317. {
  318. if (suppress_report(data->location))
  319. return;
  320. ubsan_prologue(data->location, "object-size-mismatch");
  321. pr_err("%s address %p with insufficient space\n",
  322. type_check_kinds[data->type_check_kind],
  323. (void *) ptr);
  324. pr_err("for an object of type %s\n", data->type->type_name);
  325. ubsan_epilogue();
  326. }
  327. static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
  328. unsigned long ptr)
  329. {
  330. unsigned long flags = user_access_save();
  331. if (!ptr)
  332. handle_null_ptr_deref(data);
  333. else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
  334. handle_misaligned_access(data, ptr);
  335. else
  336. handle_object_size_mismatch(data, ptr);
  337. user_access_restore(flags);
  338. }
  339. void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
  340. void *ptr)
  341. {
  342. struct type_mismatch_data_common common_data = {
  343. .location = &data->location,
  344. .type = data->type,
  345. .alignment = data->alignment,
  346. .type_check_kind = data->type_check_kind
  347. };
  348. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  349. }
  350. EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
  351. void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
  352. {
  353. struct type_mismatch_data_v1 *data = _data;
  354. struct type_mismatch_data_common common_data = {
  355. .location = &data->location,
  356. .type = data->type,
  357. .alignment = 1UL << data->log_alignment,
  358. .type_check_kind = data->type_check_kind
  359. };
  360. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  361. }
  362. EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
  363. void __ubsan_handle_out_of_bounds(void *_data, void *index)
  364. {
  365. struct out_of_bounds_data *data = _data;
  366. char index_str[VALUE_LENGTH];
  367. if (suppress_report(&data->location))
  368. return;
  369. ubsan_prologue(&data->location, "array-index-out-of-bounds");
  370. val_to_string(index_str, sizeof(index_str), data->index_type, index);
  371. pr_err("index %s is out of range for type %s\n", index_str,
  372. data->array_type->type_name);
  373. ubsan_epilogue();
  374. }
  375. EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
  376. void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
  377. {
  378. struct shift_out_of_bounds_data *data = _data;
  379. struct type_descriptor *rhs_type = data->rhs_type;
  380. struct type_descriptor *lhs_type = data->lhs_type;
  381. char rhs_str[VALUE_LENGTH];
  382. char lhs_str[VALUE_LENGTH];
  383. unsigned long ua_flags = user_access_save();
  384. if (suppress_report(&data->location))
  385. goto out;
  386. ubsan_prologue(&data->location, "shift-out-of-bounds");
  387. val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
  388. val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
  389. if (val_is_negative(rhs_type, rhs))
  390. pr_err("shift exponent %s is negative\n", rhs_str);
  391. else if (get_unsigned_val(rhs_type, rhs) >=
  392. type_bit_width(lhs_type))
  393. pr_err("shift exponent %s is too large for %u-bit type %s\n",
  394. rhs_str,
  395. type_bit_width(lhs_type),
  396. lhs_type->type_name);
  397. else if (val_is_negative(lhs_type, lhs))
  398. pr_err("left shift of negative value %s\n",
  399. lhs_str);
  400. else
  401. pr_err("left shift of %s by %s places cannot be"
  402. " represented in type %s\n",
  403. lhs_str, rhs_str,
  404. lhs_type->type_name);
  405. ubsan_epilogue();
  406. out:
  407. user_access_restore(ua_flags);
  408. }
  409. EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
  410. void __ubsan_handle_builtin_unreachable(void *_data)
  411. {
  412. struct unreachable_data *data = _data;
  413. ubsan_prologue(&data->location, "unreachable");
  414. pr_err("calling __builtin_unreachable()\n");
  415. ubsan_epilogue();
  416. panic("can't return from __builtin_unreachable()");
  417. }
  418. EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
  419. void __ubsan_handle_load_invalid_value(void *_data, void *val)
  420. {
  421. struct invalid_value_data *data = _data;
  422. char val_str[VALUE_LENGTH];
  423. unsigned long ua_flags = user_access_save();
  424. if (suppress_report(&data->location))
  425. goto out;
  426. ubsan_prologue(&data->location, "invalid-load");
  427. val_to_string(val_str, sizeof(val_str), data->type, val);
  428. pr_err("load of value %s is not a valid value for type %s\n",
  429. val_str, data->type->type_name);
  430. ubsan_epilogue();
  431. out:
  432. user_access_restore(ua_flags);
  433. }
  434. EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
  435. void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
  436. unsigned long align,
  437. unsigned long offset)
  438. {
  439. struct alignment_assumption_data *data = _data;
  440. unsigned long real_ptr;
  441. if (suppress_report(&data->location))
  442. return;
  443. ubsan_prologue(&data->location, "alignment-assumption");
  444. if (offset)
  445. pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
  446. align, offset, data->type->type_name);
  447. else
  448. pr_err("assumption of %lu byte alignment for pointer of type %s failed",
  449. align, data->type->type_name);
  450. real_ptr = ptr - offset;
  451. pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
  452. offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
  453. real_ptr & (align - 1));
  454. ubsan_epilogue();
  455. }
  456. EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
  457. #endif /* !CONFIG_UBSAN_TRAP */