ubsan.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "ubsan.h"
  17. const char *type_check_kinds[] = {
  18. "load of",
  19. "store to",
  20. "reference binding to",
  21. "member access within",
  22. "member call on",
  23. "constructor call on",
  24. "downcast of",
  25. "downcast of"
  26. };
  27. #define REPORTED_BIT 31
  28. #if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
  29. #define COLUMN_MASK (~(1U << REPORTED_BIT))
  30. #define LINE_MASK (~0U)
  31. #else
  32. #define COLUMN_MASK (~0U)
  33. #define LINE_MASK (~(1U << REPORTED_BIT))
  34. #endif
  35. #define VALUE_LENGTH 40
  36. static bool was_reported(struct source_location *location)
  37. {
  38. return test_and_set_bit(REPORTED_BIT, &location->reported);
  39. }
  40. static bool suppress_report(struct source_location *loc)
  41. {
  42. return current->in_ubsan || was_reported(loc);
  43. }
  44. static bool type_is_int(struct type_descriptor *type)
  45. {
  46. return type->type_kind == type_kind_int;
  47. }
  48. static bool type_is_signed(struct type_descriptor *type)
  49. {
  50. WARN_ON(!type_is_int(type));
  51. return type->type_info & 1;
  52. }
  53. static unsigned type_bit_width(struct type_descriptor *type)
  54. {
  55. return 1 << (type->type_info >> 1);
  56. }
  57. static bool is_inline_int(struct type_descriptor *type)
  58. {
  59. unsigned inline_bits = sizeof(unsigned long)*8;
  60. unsigned bits = type_bit_width(type);
  61. WARN_ON(!type_is_int(type));
  62. return bits <= inline_bits;
  63. }
  64. static s_max get_signed_val(struct type_descriptor *type, void *val)
  65. {
  66. if (is_inline_int(type)) {
  67. unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
  68. unsigned long ulong_val = (unsigned long)val;
  69. return ((s_max)ulong_val) << extra_bits >> extra_bits;
  70. }
  71. if (type_bit_width(type) == 64)
  72. return *(s64 *)val;
  73. return *(s_max *)val;
  74. }
  75. static bool val_is_negative(struct type_descriptor *type, void *val)
  76. {
  77. return type_is_signed(type) && get_signed_val(type, val) < 0;
  78. }
  79. static u_max get_unsigned_val(struct type_descriptor *type, void *val)
  80. {
  81. if (is_inline_int(type))
  82. return (unsigned long)val;
  83. if (type_bit_width(type) == 64)
  84. return *(u64 *)val;
  85. return *(u_max *)val;
  86. }
  87. static void val_to_string(char *str, size_t size, struct type_descriptor *type,
  88. void *value)
  89. {
  90. if (type_is_int(type)) {
  91. if (type_bit_width(type) == 128) {
  92. #if defined(CONFIG_ARCH_SUPPORTS_INT128)
  93. u_max val = get_unsigned_val(type, value);
  94. scnprintf(str, size, "0x%08x%08x%08x%08x",
  95. (u32)(val >> 96),
  96. (u32)(val >> 64),
  97. (u32)(val >> 32),
  98. (u32)(val));
  99. #else
  100. WARN_ON(1);
  101. #endif
  102. } else if (type_is_signed(type)) {
  103. scnprintf(str, size, "%lld",
  104. (s64)get_signed_val(type, value));
  105. } else {
  106. scnprintf(str, size, "%llu",
  107. (u64)get_unsigned_val(type, value));
  108. }
  109. }
  110. }
  111. static void ubsan_prologue(struct source_location *loc, const char *reason)
  112. {
  113. current->in_ubsan++;
  114. pr_err("========================================"
  115. "========================================\n");
  116. pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name,
  117. loc->line & LINE_MASK, loc->column & COLUMN_MASK);
  118. }
  119. static void ubsan_epilogue(void)
  120. {
  121. dump_stack();
  122. pr_err("========================================"
  123. "========================================\n");
  124. current->in_ubsan--;
  125. if (panic_on_warn) {
  126. /*
  127. * This thread may hit another WARN() in the panic path.
  128. * Resetting this prevents additional WARN() from panicking the
  129. * system on this thread. Other threads are blocked by the
  130. * panic_mutex in panic().
  131. */
  132. panic_on_warn = 0;
  133. panic("panic_on_warn set ...\n");
  134. }
  135. }
  136. void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
  137. {
  138. struct overflow_data *data = _data;
  139. char rhs_val_str[VALUE_LENGTH];
  140. if (suppress_report(&data->location))
  141. return;
  142. ubsan_prologue(&data->location, "division-overflow");
  143. val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
  144. if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
  145. pr_err("division of %s by -1 cannot be represented in type %s\n",
  146. rhs_val_str, data->type->type_name);
  147. else
  148. pr_err("division by zero\n");
  149. ubsan_epilogue();
  150. }
  151. EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
  152. static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
  153. {
  154. if (suppress_report(data->location))
  155. return;
  156. ubsan_prologue(data->location, "null-ptr-deref");
  157. pr_err("%s null pointer of type %s\n",
  158. type_check_kinds[data->type_check_kind],
  159. data->type->type_name);
  160. ubsan_epilogue();
  161. }
  162. static void handle_misaligned_access(struct type_mismatch_data_common *data,
  163. unsigned long ptr)
  164. {
  165. if (suppress_report(data->location))
  166. return;
  167. ubsan_prologue(data->location, "misaligned-access");
  168. pr_err("%s misaligned address %p for type %s\n",
  169. type_check_kinds[data->type_check_kind],
  170. (void *)ptr, data->type->type_name);
  171. pr_err("which requires %ld byte alignment\n", data->alignment);
  172. ubsan_epilogue();
  173. }
  174. static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
  175. unsigned long ptr)
  176. {
  177. if (suppress_report(data->location))
  178. return;
  179. ubsan_prologue(data->location, "object-size-mismatch");
  180. pr_err("%s address %p with insufficient space\n",
  181. type_check_kinds[data->type_check_kind],
  182. (void *) ptr);
  183. pr_err("for an object of type %s\n", data->type->type_name);
  184. ubsan_epilogue();
  185. }
  186. static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
  187. unsigned long ptr)
  188. {
  189. unsigned long flags = user_access_save();
  190. if (!ptr)
  191. handle_null_ptr_deref(data);
  192. else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
  193. handle_misaligned_access(data, ptr);
  194. else
  195. handle_object_size_mismatch(data, ptr);
  196. user_access_restore(flags);
  197. }
  198. void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
  199. void *ptr)
  200. {
  201. struct type_mismatch_data_common common_data = {
  202. .location = &data->location,
  203. .type = data->type,
  204. .alignment = data->alignment,
  205. .type_check_kind = data->type_check_kind
  206. };
  207. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  208. }
  209. EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
  210. void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
  211. {
  212. struct type_mismatch_data_v1 *data = _data;
  213. struct type_mismatch_data_common common_data = {
  214. .location = &data->location,
  215. .type = data->type,
  216. .alignment = 1UL << data->log_alignment,
  217. .type_check_kind = data->type_check_kind
  218. };
  219. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  220. }
  221. EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
  222. void __ubsan_handle_out_of_bounds(void *_data, void *index)
  223. {
  224. struct out_of_bounds_data *data = _data;
  225. char index_str[VALUE_LENGTH];
  226. if (suppress_report(&data->location))
  227. return;
  228. ubsan_prologue(&data->location, "array-index-out-of-bounds");
  229. val_to_string(index_str, sizeof(index_str), data->index_type, index);
  230. pr_err("index %s is out of range for type %s\n", index_str,
  231. data->array_type->type_name);
  232. ubsan_epilogue();
  233. }
  234. EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
  235. void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
  236. {
  237. struct shift_out_of_bounds_data *data = _data;
  238. struct type_descriptor *rhs_type = data->rhs_type;
  239. struct type_descriptor *lhs_type = data->lhs_type;
  240. char rhs_str[VALUE_LENGTH];
  241. char lhs_str[VALUE_LENGTH];
  242. unsigned long ua_flags = user_access_save();
  243. if (suppress_report(&data->location))
  244. goto out;
  245. ubsan_prologue(&data->location, "shift-out-of-bounds");
  246. val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
  247. val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
  248. if (val_is_negative(rhs_type, rhs))
  249. pr_err("shift exponent %s is negative\n", rhs_str);
  250. else if (get_unsigned_val(rhs_type, rhs) >=
  251. type_bit_width(lhs_type))
  252. pr_err("shift exponent %s is too large for %u-bit type %s\n",
  253. rhs_str,
  254. type_bit_width(lhs_type),
  255. lhs_type->type_name);
  256. else if (val_is_negative(lhs_type, lhs))
  257. pr_err("left shift of negative value %s\n",
  258. lhs_str);
  259. else
  260. pr_err("left shift of %s by %s places cannot be"
  261. " represented in type %s\n",
  262. lhs_str, rhs_str,
  263. lhs_type->type_name);
  264. ubsan_epilogue();
  265. out:
  266. user_access_restore(ua_flags);
  267. }
  268. EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
  269. void __ubsan_handle_builtin_unreachable(void *_data)
  270. {
  271. struct unreachable_data *data = _data;
  272. ubsan_prologue(&data->location, "unreachable");
  273. pr_err("calling __builtin_unreachable()\n");
  274. ubsan_epilogue();
  275. panic("can't return from __builtin_unreachable()");
  276. }
  277. EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
  278. void __ubsan_handle_load_invalid_value(void *_data, void *val)
  279. {
  280. struct invalid_value_data *data = _data;
  281. char val_str[VALUE_LENGTH];
  282. if (suppress_report(&data->location))
  283. return;
  284. ubsan_prologue(&data->location, "invalid-load");
  285. val_to_string(val_str, sizeof(val_str), data->type, val);
  286. pr_err("load of value %s is not a valid value for type %s\n",
  287. val_str, data->type->type_name);
  288. ubsan_epilogue();
  289. }
  290. EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
  291. void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
  292. unsigned long align,
  293. unsigned long offset);
  294. void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
  295. unsigned long align,
  296. unsigned long offset)
  297. {
  298. struct alignment_assumption_data *data = _data;
  299. unsigned long real_ptr;
  300. if (suppress_report(&data->location))
  301. return;
  302. ubsan_prologue(&data->location, "alignment-assumption");
  303. if (offset)
  304. pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
  305. align, offset, data->type->type_name);
  306. else
  307. pr_err("assumption of %lu byte alignment for pointer of type %s failed",
  308. align, data->type->type_name);
  309. real_ptr = ptr - offset;
  310. pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
  311. offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
  312. real_ptr & (align - 1));
  313. ubsan_epilogue();
  314. }
  315. EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);