ubsan.c 9.7 KB

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