ubsan.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 void print_source_location(const char *prefix,
  41. struct source_location *loc)
  42. {
  43. pr_err("%s %s:%d:%d\n", prefix, loc->file_name,
  44. loc->line & LINE_MASK, loc->column & COLUMN_MASK);
  45. }
  46. static bool suppress_report(struct source_location *loc)
  47. {
  48. return current->in_ubsan || was_reported(loc);
  49. }
  50. static bool type_is_int(struct type_descriptor *type)
  51. {
  52. return type->type_kind == type_kind_int;
  53. }
  54. static bool type_is_signed(struct type_descriptor *type)
  55. {
  56. WARN_ON(!type_is_int(type));
  57. return type->type_info & 1;
  58. }
  59. static unsigned type_bit_width(struct type_descriptor *type)
  60. {
  61. return 1 << (type->type_info >> 1);
  62. }
  63. static bool is_inline_int(struct type_descriptor *type)
  64. {
  65. unsigned inline_bits = sizeof(unsigned long)*8;
  66. unsigned bits = type_bit_width(type);
  67. WARN_ON(!type_is_int(type));
  68. return bits <= inline_bits;
  69. }
  70. static s_max get_signed_val(struct type_descriptor *type, void *val)
  71. {
  72. if (is_inline_int(type)) {
  73. unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
  74. unsigned long ulong_val = (unsigned long)val;
  75. return ((s_max)ulong_val) << extra_bits >> extra_bits;
  76. }
  77. if (type_bit_width(type) == 64)
  78. return *(s64 *)val;
  79. return *(s_max *)val;
  80. }
  81. static bool val_is_negative(struct type_descriptor *type, void *val)
  82. {
  83. return type_is_signed(type) && get_signed_val(type, val) < 0;
  84. }
  85. static u_max get_unsigned_val(struct type_descriptor *type, void *val)
  86. {
  87. if (is_inline_int(type))
  88. return (unsigned long)val;
  89. if (type_bit_width(type) == 64)
  90. return *(u64 *)val;
  91. return *(u_max *)val;
  92. }
  93. static void val_to_string(char *str, size_t size, struct type_descriptor *type,
  94. void *value)
  95. {
  96. if (type_is_int(type)) {
  97. if (type_bit_width(type) == 128) {
  98. #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
  99. u_max val = get_unsigned_val(type, value);
  100. scnprintf(str, size, "0x%08x%08x%08x%08x",
  101. (u32)(val >> 96),
  102. (u32)(val >> 64),
  103. (u32)(val >> 32),
  104. (u32)(val));
  105. #else
  106. WARN_ON(1);
  107. #endif
  108. } else if (type_is_signed(type)) {
  109. scnprintf(str, size, "%lld",
  110. (s64)get_signed_val(type, value));
  111. } else {
  112. scnprintf(str, size, "%llu",
  113. (u64)get_unsigned_val(type, value));
  114. }
  115. }
  116. }
  117. static void ubsan_prologue(struct source_location *location)
  118. {
  119. current->in_ubsan++;
  120. pr_err("========================================"
  121. "========================================\n");
  122. print_source_location("UBSAN: Undefined behaviour in", location);
  123. }
  124. static void ubsan_epilogue(void)
  125. {
  126. dump_stack();
  127. pr_err("========================================"
  128. "========================================\n");
  129. current->in_ubsan--;
  130. }
  131. static void handle_overflow(struct overflow_data *data, void *lhs,
  132. void *rhs, char op)
  133. {
  134. struct type_descriptor *type = data->type;
  135. char lhs_val_str[VALUE_LENGTH];
  136. char rhs_val_str[VALUE_LENGTH];
  137. if (suppress_report(&data->location))
  138. return;
  139. ubsan_prologue(&data->location);
  140. val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
  141. val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
  142. pr_err("%s integer overflow:\n",
  143. type_is_signed(type) ? "signed" : "unsigned");
  144. pr_err("%s %c %s cannot be represented in type %s\n",
  145. lhs_val_str,
  146. op,
  147. rhs_val_str,
  148. type->type_name);
  149. ubsan_epilogue();
  150. }
  151. void __ubsan_handle_add_overflow(struct overflow_data *data,
  152. void *lhs, void *rhs)
  153. {
  154. handle_overflow(data, lhs, rhs, '+');
  155. }
  156. EXPORT_SYMBOL(__ubsan_handle_add_overflow);
  157. void __ubsan_handle_sub_overflow(struct overflow_data *data,
  158. void *lhs, void *rhs)
  159. {
  160. handle_overflow(data, lhs, rhs, '-');
  161. }
  162. EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
  163. void __ubsan_handle_mul_overflow(struct overflow_data *data,
  164. void *lhs, void *rhs)
  165. {
  166. handle_overflow(data, lhs, rhs, '*');
  167. }
  168. EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
  169. void __ubsan_handle_negate_overflow(struct overflow_data *data,
  170. void *old_val)
  171. {
  172. char old_val_str[VALUE_LENGTH];
  173. if (suppress_report(&data->location))
  174. return;
  175. ubsan_prologue(&data->location);
  176. val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
  177. pr_err("negation of %s cannot be represented in type %s:\n",
  178. old_val_str, data->type->type_name);
  179. ubsan_epilogue();
  180. }
  181. EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
  182. void __ubsan_handle_divrem_overflow(struct overflow_data *data,
  183. void *lhs, void *rhs)
  184. {
  185. char rhs_val_str[VALUE_LENGTH];
  186. if (suppress_report(&data->location))
  187. return;
  188. ubsan_prologue(&data->location);
  189. val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
  190. if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
  191. pr_err("division of %s by -1 cannot be represented in type %s\n",
  192. rhs_val_str, data->type->type_name);
  193. else
  194. pr_err("division by zero\n");
  195. ubsan_epilogue();
  196. }
  197. EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
  198. static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
  199. {
  200. if (suppress_report(data->location))
  201. return;
  202. ubsan_prologue(data->location);
  203. pr_err("%s null pointer of type %s\n",
  204. type_check_kinds[data->type_check_kind],
  205. data->type->type_name);
  206. ubsan_epilogue();
  207. }
  208. static void handle_misaligned_access(struct type_mismatch_data_common *data,
  209. unsigned long ptr)
  210. {
  211. if (suppress_report(data->location))
  212. return;
  213. ubsan_prologue(data->location);
  214. pr_err("%s misaligned address %p for type %s\n",
  215. type_check_kinds[data->type_check_kind],
  216. (void *)ptr, data->type->type_name);
  217. pr_err("which requires %ld byte alignment\n", data->alignment);
  218. ubsan_epilogue();
  219. }
  220. static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
  221. unsigned long ptr)
  222. {
  223. if (suppress_report(data->location))
  224. return;
  225. ubsan_prologue(data->location);
  226. pr_err("%s address %p with insufficient space\n",
  227. type_check_kinds[data->type_check_kind],
  228. (void *) ptr);
  229. pr_err("for an object of type %s\n", data->type->type_name);
  230. ubsan_epilogue();
  231. }
  232. static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
  233. unsigned long ptr)
  234. {
  235. unsigned long flags = user_access_save();
  236. if (!ptr)
  237. handle_null_ptr_deref(data);
  238. else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
  239. handle_misaligned_access(data, ptr);
  240. else
  241. handle_object_size_mismatch(data, ptr);
  242. user_access_restore(flags);
  243. }
  244. void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
  245. void *ptr)
  246. {
  247. struct type_mismatch_data_common common_data = {
  248. .location = &data->location,
  249. .type = data->type,
  250. .alignment = data->alignment,
  251. .type_check_kind = data->type_check_kind
  252. };
  253. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  254. }
  255. EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
  256. void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data,
  257. void *ptr)
  258. {
  259. struct type_mismatch_data_common common_data = {
  260. .location = &data->location,
  261. .type = data->type,
  262. .alignment = 1UL << data->log_alignment,
  263. .type_check_kind = data->type_check_kind
  264. };
  265. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  266. }
  267. EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
  268. void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data, void *index)
  269. {
  270. char index_str[VALUE_LENGTH];
  271. if (suppress_report(&data->location))
  272. return;
  273. ubsan_prologue(&data->location);
  274. val_to_string(index_str, sizeof(index_str), data->index_type, index);
  275. pr_err("index %s is out of range for type %s\n", index_str,
  276. data->array_type->type_name);
  277. ubsan_epilogue();
  278. }
  279. EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
  280. void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
  281. void *lhs, void *rhs)
  282. {
  283. struct type_descriptor *rhs_type = data->rhs_type;
  284. struct type_descriptor *lhs_type = data->lhs_type;
  285. char rhs_str[VALUE_LENGTH];
  286. char lhs_str[VALUE_LENGTH];
  287. unsigned long ua_flags = user_access_save();
  288. if (suppress_report(&data->location))
  289. goto out;
  290. ubsan_prologue(&data->location);
  291. val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
  292. val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
  293. if (val_is_negative(rhs_type, rhs))
  294. pr_err("shift exponent %s is negative\n", rhs_str);
  295. else if (get_unsigned_val(rhs_type, rhs) >=
  296. type_bit_width(lhs_type))
  297. pr_err("shift exponent %s is too large for %u-bit type %s\n",
  298. rhs_str,
  299. type_bit_width(lhs_type),
  300. lhs_type->type_name);
  301. else if (val_is_negative(lhs_type, lhs))
  302. pr_err("left shift of negative value %s\n",
  303. lhs_str);
  304. else
  305. pr_err("left shift of %s by %s places cannot be"
  306. " represented in type %s\n",
  307. lhs_str, rhs_str,
  308. lhs_type->type_name);
  309. ubsan_epilogue();
  310. out:
  311. user_access_restore(ua_flags);
  312. }
  313. EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
  314. void __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
  315. {
  316. ubsan_prologue(&data->location);
  317. pr_err("calling __builtin_unreachable()\n");
  318. ubsan_epilogue();
  319. panic("can't return from __builtin_unreachable()");
  320. }
  321. EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
  322. void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
  323. void *val)
  324. {
  325. char val_str[VALUE_LENGTH];
  326. if (suppress_report(&data->location))
  327. return;
  328. ubsan_prologue(&data->location);
  329. val_to_string(val_str, sizeof(val_str), data->type, val);
  330. pr_err("load of value %s is not a valid value for type %s\n",
  331. val_str, data->type->type_name);
  332. ubsan_epilogue();
  333. }
  334. EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);