ubsan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * UBSAN error reporting functions
  3. *
  4. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  5. * Author: Andrey Ryabinin <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/bug.h>
  14. #include <linux/ctype.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/sched.h>
  19. #include "ubsan.h"
  20. const char *type_check_kinds[] = {
  21. "load of",
  22. "store to",
  23. "reference binding to",
  24. "member access within",
  25. "member call on",
  26. "constructor call on",
  27. "downcast of",
  28. "downcast of"
  29. };
  30. #define REPORTED_BIT 31
  31. #if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
  32. #define COLUMN_MASK (~(1U << REPORTED_BIT))
  33. #define LINE_MASK (~0U)
  34. #else
  35. #define COLUMN_MASK (~0U)
  36. #define LINE_MASK (~(1U << REPORTED_BIT))
  37. #endif
  38. #define VALUE_LENGTH 40
  39. static bool was_reported(struct source_location *location)
  40. {
  41. return test_and_set_bit(REPORTED_BIT, &location->reported);
  42. }
  43. static void print_source_location(const char *prefix,
  44. struct source_location *loc)
  45. {
  46. pr_err("%s %s:%d:%d\n", prefix, loc->file_name,
  47. loc->line & LINE_MASK, loc->column & COLUMN_MASK);
  48. }
  49. static bool suppress_report(struct source_location *loc)
  50. {
  51. return current->in_ubsan || was_reported(loc);
  52. }
  53. static bool type_is_int(struct type_descriptor *type)
  54. {
  55. return type->type_kind == type_kind_int;
  56. }
  57. static bool type_is_signed(struct type_descriptor *type)
  58. {
  59. WARN_ON(!type_is_int(type));
  60. return type->type_info & 1;
  61. }
  62. static unsigned type_bit_width(struct type_descriptor *type)
  63. {
  64. return 1 << (type->type_info >> 1);
  65. }
  66. static bool is_inline_int(struct type_descriptor *type)
  67. {
  68. unsigned inline_bits = sizeof(unsigned long)*8;
  69. unsigned bits = type_bit_width(type);
  70. WARN_ON(!type_is_int(type));
  71. return bits <= inline_bits;
  72. }
  73. static s_max get_signed_val(struct type_descriptor *type, void *val)
  74. {
  75. if (is_inline_int(type)) {
  76. unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
  77. unsigned long ulong_val = (unsigned long)val;
  78. return ((s_max)ulong_val) << extra_bits >> extra_bits;
  79. }
  80. if (type_bit_width(type) == 64)
  81. return *(s64 *)val;
  82. return *(s_max *)val;
  83. }
  84. static bool val_is_negative(struct type_descriptor *type, void *val)
  85. {
  86. return type_is_signed(type) && get_signed_val(type, val) < 0;
  87. }
  88. static u_max get_unsigned_val(struct type_descriptor *type, void *val)
  89. {
  90. if (is_inline_int(type))
  91. return (unsigned long)val;
  92. if (type_bit_width(type) == 64)
  93. return *(u64 *)val;
  94. return *(u_max *)val;
  95. }
  96. static void val_to_string(char *str, size_t size, struct type_descriptor *type,
  97. void *value)
  98. {
  99. if (type_is_int(type)) {
  100. if (type_bit_width(type) == 128) {
  101. #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
  102. u_max val = get_unsigned_val(type, value);
  103. scnprintf(str, size, "0x%08x%08x%08x%08x",
  104. (u32)(val >> 96),
  105. (u32)(val >> 64),
  106. (u32)(val >> 32),
  107. (u32)(val));
  108. #else
  109. WARN_ON(1);
  110. #endif
  111. } else if (type_is_signed(type)) {
  112. scnprintf(str, size, "%lld",
  113. (s64)get_signed_val(type, value));
  114. } else {
  115. scnprintf(str, size, "%llu",
  116. (u64)get_unsigned_val(type, value));
  117. }
  118. }
  119. }
  120. static bool location_is_valid(struct source_location *loc)
  121. {
  122. return loc->file_name != NULL;
  123. }
  124. static DEFINE_SPINLOCK(report_lock);
  125. static void ubsan_prologue(struct source_location *location,
  126. unsigned long *flags)
  127. {
  128. current->in_ubsan++;
  129. spin_lock_irqsave(&report_lock, *flags);
  130. pr_err("========================================"
  131. "========================================\n");
  132. print_source_location("UBSAN: Undefined behaviour in", location);
  133. }
  134. static void ubsan_epilogue(unsigned long *flags)
  135. {
  136. dump_stack();
  137. pr_err("========================================"
  138. "========================================\n");
  139. spin_unlock_irqrestore(&report_lock, *flags);
  140. current->in_ubsan--;
  141. }
  142. static void handle_overflow(struct overflow_data *data, void *lhs,
  143. void *rhs, char op)
  144. {
  145. struct type_descriptor *type = data->type;
  146. unsigned long flags;
  147. char lhs_val_str[VALUE_LENGTH];
  148. char rhs_val_str[VALUE_LENGTH];
  149. if (suppress_report(&data->location))
  150. return;
  151. ubsan_prologue(&data->location, &flags);
  152. val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
  153. val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
  154. pr_err("%s integer overflow:\n",
  155. type_is_signed(type) ? "signed" : "unsigned");
  156. pr_err("%s %c %s cannot be represented in type %s\n",
  157. lhs_val_str,
  158. op,
  159. rhs_val_str,
  160. type->type_name);
  161. ubsan_epilogue(&flags);
  162. }
  163. void __ubsan_handle_add_overflow(struct overflow_data *data,
  164. void *lhs, void *rhs)
  165. {
  166. handle_overflow(data, lhs, rhs, '+');
  167. }
  168. EXPORT_SYMBOL(__ubsan_handle_add_overflow);
  169. void __ubsan_handle_sub_overflow(struct overflow_data *data,
  170. void *lhs, void *rhs)
  171. {
  172. handle_overflow(data, lhs, rhs, '-');
  173. }
  174. EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
  175. void __ubsan_handle_mul_overflow(struct overflow_data *data,
  176. void *lhs, void *rhs)
  177. {
  178. handle_overflow(data, lhs, rhs, '*');
  179. }
  180. EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
  181. void __ubsan_handle_negate_overflow(struct overflow_data *data,
  182. void *old_val)
  183. {
  184. unsigned long flags;
  185. char old_val_str[VALUE_LENGTH];
  186. if (suppress_report(&data->location))
  187. return;
  188. ubsan_prologue(&data->location, &flags);
  189. val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
  190. pr_err("negation of %s cannot be represented in type %s:\n",
  191. old_val_str, data->type->type_name);
  192. ubsan_epilogue(&flags);
  193. }
  194. EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
  195. void __ubsan_handle_divrem_overflow(struct overflow_data *data,
  196. void *lhs, void *rhs)
  197. {
  198. unsigned long flags;
  199. char rhs_val_str[VALUE_LENGTH];
  200. if (suppress_report(&data->location))
  201. return;
  202. ubsan_prologue(&data->location, &flags);
  203. val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
  204. if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
  205. pr_err("division of %s by -1 cannot be represented in type %s\n",
  206. rhs_val_str, data->type->type_name);
  207. else
  208. pr_err("division by zero\n");
  209. ubsan_epilogue(&flags);
  210. }
  211. EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
  212. static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
  213. {
  214. unsigned long flags;
  215. if (suppress_report(data->location))
  216. return;
  217. ubsan_prologue(data->location, &flags);
  218. pr_err("%s null pointer of type %s\n",
  219. type_check_kinds[data->type_check_kind],
  220. data->type->type_name);
  221. ubsan_epilogue(&flags);
  222. }
  223. static void handle_misaligned_access(struct type_mismatch_data_common *data,
  224. unsigned long ptr)
  225. {
  226. unsigned long flags;
  227. if (suppress_report(data->location))
  228. return;
  229. ubsan_prologue(data->location, &flags);
  230. pr_err("%s misaligned address %p for type %s\n",
  231. type_check_kinds[data->type_check_kind],
  232. (void *)ptr, data->type->type_name);
  233. pr_err("which requires %ld byte alignment\n", data->alignment);
  234. ubsan_epilogue(&flags);
  235. }
  236. static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
  237. unsigned long ptr)
  238. {
  239. unsigned long flags;
  240. if (suppress_report(data->location))
  241. return;
  242. ubsan_prologue(data->location, &flags);
  243. pr_err("%s address %p with insufficient space\n",
  244. type_check_kinds[data->type_check_kind],
  245. (void *) ptr);
  246. pr_err("for an object of type %s\n", data->type->type_name);
  247. ubsan_epilogue(&flags);
  248. }
  249. static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
  250. unsigned long ptr)
  251. {
  252. if (!ptr)
  253. handle_null_ptr_deref(data);
  254. else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
  255. handle_misaligned_access(data, ptr);
  256. else
  257. handle_object_size_mismatch(data, ptr);
  258. }
  259. void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
  260. void *ptr)
  261. {
  262. struct type_mismatch_data_common common_data = {
  263. .location = &data->location,
  264. .type = data->type,
  265. .alignment = data->alignment,
  266. .type_check_kind = data->type_check_kind
  267. };
  268. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  269. }
  270. EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
  271. void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data,
  272. void *ptr)
  273. {
  274. struct type_mismatch_data_common common_data = {
  275. .location = &data->location,
  276. .type = data->type,
  277. .alignment = 1UL << data->log_alignment,
  278. .type_check_kind = data->type_check_kind
  279. };
  280. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  281. }
  282. EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
  283. void __ubsan_handle_nonnull_return(struct nonnull_return_data *data)
  284. {
  285. unsigned long flags;
  286. if (suppress_report(&data->location))
  287. return;
  288. ubsan_prologue(&data->location, &flags);
  289. pr_err("null pointer returned from function declared to never return null\n");
  290. if (location_is_valid(&data->attr_location))
  291. print_source_location("returns_nonnull attribute specified in",
  292. &data->attr_location);
  293. ubsan_epilogue(&flags);
  294. }
  295. EXPORT_SYMBOL(__ubsan_handle_nonnull_return);
  296. void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
  297. void *bound)
  298. {
  299. unsigned long flags;
  300. char bound_str[VALUE_LENGTH];
  301. if (suppress_report(&data->location))
  302. return;
  303. ubsan_prologue(&data->location, &flags);
  304. val_to_string(bound_str, sizeof(bound_str), data->type, bound);
  305. pr_err("variable length array bound value %s <= 0\n", bound_str);
  306. ubsan_epilogue(&flags);
  307. }
  308. EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive);
  309. void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data, void *index)
  310. {
  311. unsigned long flags;
  312. char index_str[VALUE_LENGTH];
  313. if (suppress_report(&data->location))
  314. return;
  315. ubsan_prologue(&data->location, &flags);
  316. val_to_string(index_str, sizeof(index_str), data->index_type, index);
  317. pr_err("index %s is out of range for type %s\n", index_str,
  318. data->array_type->type_name);
  319. ubsan_epilogue(&flags);
  320. }
  321. EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
  322. void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
  323. void *lhs, void *rhs)
  324. {
  325. unsigned long flags;
  326. struct type_descriptor *rhs_type = data->rhs_type;
  327. struct type_descriptor *lhs_type = data->lhs_type;
  328. char rhs_str[VALUE_LENGTH];
  329. char lhs_str[VALUE_LENGTH];
  330. if (suppress_report(&data->location))
  331. return;
  332. ubsan_prologue(&data->location, &flags);
  333. val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
  334. val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
  335. if (val_is_negative(rhs_type, rhs))
  336. pr_err("shift exponent %s is negative\n", rhs_str);
  337. else if (get_unsigned_val(rhs_type, rhs) >=
  338. type_bit_width(lhs_type))
  339. pr_err("shift exponent %s is too large for %u-bit type %s\n",
  340. rhs_str,
  341. type_bit_width(lhs_type),
  342. lhs_type->type_name);
  343. else if (val_is_negative(lhs_type, lhs))
  344. pr_err("left shift of negative value %s\n",
  345. lhs_str);
  346. else
  347. pr_err("left shift of %s by %s places cannot be"
  348. " represented in type %s\n",
  349. lhs_str, rhs_str,
  350. lhs_type->type_name);
  351. ubsan_epilogue(&flags);
  352. }
  353. EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
  354. void __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
  355. {
  356. unsigned long flags;
  357. ubsan_prologue(&data->location, &flags);
  358. pr_err("calling __builtin_unreachable()\n");
  359. ubsan_epilogue(&flags);
  360. panic("can't return from __builtin_unreachable()");
  361. }
  362. EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
  363. void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
  364. void *val)
  365. {
  366. unsigned long flags;
  367. char val_str[VALUE_LENGTH];
  368. if (suppress_report(&data->location))
  369. return;
  370. ubsan_prologue(&data->location, &flags);
  371. val_to_string(val_str, sizeof(val_str), data->type, val);
  372. pr_err("load of value %s is not a valid value for type %s\n",
  373. val_str, data->type->type_name);
  374. ubsan_epilogue(&flags);
  375. }
  376. EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);