ubsan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * UBSAN error reporting functions
  3. *
  4. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  5. * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
  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, unsigned long val)
  74. {
  75. if (is_inline_int(type)) {
  76. unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
  77. return ((s_max)val) << extra_bits >> extra_bits;
  78. }
  79. if (type_bit_width(type) == 64)
  80. return *(s64 *)val;
  81. return *(s_max *)val;
  82. }
  83. static bool val_is_negative(struct type_descriptor *type, unsigned long val)
  84. {
  85. return type_is_signed(type) && get_signed_val(type, val) < 0;
  86. }
  87. static u_max get_unsigned_val(struct type_descriptor *type, unsigned long val)
  88. {
  89. if (is_inline_int(type))
  90. return val;
  91. if (type_bit_width(type) == 64)
  92. return *(u64 *)val;
  93. return *(u_max *)val;
  94. }
  95. static void val_to_string(char *str, size_t size, struct type_descriptor *type,
  96. unsigned long value)
  97. {
  98. if (type_is_int(type)) {
  99. if (type_bit_width(type) == 128) {
  100. #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
  101. u_max val = get_unsigned_val(type, value);
  102. scnprintf(str, size, "0x%08x%08x%08x%08x",
  103. (u32)(val >> 96),
  104. (u32)(val >> 64),
  105. (u32)(val >> 32),
  106. (u32)(val));
  107. #else
  108. WARN_ON(1);
  109. #endif
  110. } else if (type_is_signed(type)) {
  111. scnprintf(str, size, "%lld",
  112. (s64)get_signed_val(type, value));
  113. } else {
  114. scnprintf(str, size, "%llu",
  115. (u64)get_unsigned_val(type, value));
  116. }
  117. }
  118. }
  119. static bool location_is_valid(struct source_location *loc)
  120. {
  121. return loc->file_name != NULL;
  122. }
  123. static DEFINE_SPINLOCK(report_lock);
  124. static void ubsan_prologue(struct source_location *location,
  125. unsigned long *flags)
  126. {
  127. current->in_ubsan++;
  128. spin_lock_irqsave(&report_lock, *flags);
  129. pr_err("========================================"
  130. "========================================\n");
  131. print_source_location("UBSAN: Undefined behaviour in", location);
  132. }
  133. static void ubsan_epilogue(unsigned long *flags)
  134. {
  135. dump_stack();
  136. pr_err("========================================"
  137. "========================================\n");
  138. spin_unlock_irqrestore(&report_lock, *flags);
  139. current->in_ubsan--;
  140. }
  141. static void handle_overflow(struct overflow_data *data, unsigned long lhs,
  142. unsigned long rhs, char op)
  143. {
  144. struct type_descriptor *type = data->type;
  145. unsigned long flags;
  146. char lhs_val_str[VALUE_LENGTH];
  147. char rhs_val_str[VALUE_LENGTH];
  148. if (suppress_report(&data->location))
  149. return;
  150. ubsan_prologue(&data->location, &flags);
  151. val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
  152. val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
  153. pr_err("%s integer overflow:\n",
  154. type_is_signed(type) ? "signed" : "unsigned");
  155. pr_err("%s %c %s cannot be represented in type %s\n",
  156. lhs_val_str,
  157. op,
  158. rhs_val_str,
  159. type->type_name);
  160. ubsan_epilogue(&flags);
  161. }
  162. void __ubsan_handle_add_overflow(struct overflow_data *data,
  163. unsigned long lhs,
  164. unsigned long 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. unsigned long lhs,
  171. unsigned long rhs)
  172. {
  173. handle_overflow(data, lhs, rhs, '-');
  174. }
  175. EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
  176. void __ubsan_handle_mul_overflow(struct overflow_data *data,
  177. unsigned long lhs,
  178. unsigned long rhs)
  179. {
  180. handle_overflow(data, lhs, rhs, '*');
  181. }
  182. EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
  183. void __ubsan_handle_negate_overflow(struct overflow_data *data,
  184. unsigned long old_val)
  185. {
  186. unsigned long flags;
  187. char old_val_str[VALUE_LENGTH];
  188. if (suppress_report(&data->location))
  189. return;
  190. ubsan_prologue(&data->location, &flags);
  191. val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
  192. pr_err("negation of %s cannot be represented in type %s:\n",
  193. old_val_str, data->type->type_name);
  194. ubsan_epilogue(&flags);
  195. }
  196. EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
  197. void __ubsan_handle_divrem_overflow(struct overflow_data *data,
  198. unsigned long lhs,
  199. unsigned long rhs)
  200. {
  201. unsigned long flags;
  202. char rhs_val_str[VALUE_LENGTH];
  203. if (suppress_report(&data->location))
  204. return;
  205. ubsan_prologue(&data->location, &flags);
  206. val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
  207. if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
  208. pr_err("division of %s by -1 cannot be represented in type %s\n",
  209. rhs_val_str, data->type->type_name);
  210. else
  211. pr_err("division by zero\n");
  212. ubsan_epilogue(&flags);
  213. }
  214. EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
  215. static void handle_null_ptr_deref(struct type_mismatch_data *data)
  216. {
  217. unsigned long flags;
  218. if (suppress_report(&data->location))
  219. return;
  220. ubsan_prologue(&data->location, &flags);
  221. pr_err("%s null pointer of type %s\n",
  222. type_check_kinds[data->type_check_kind],
  223. data->type->type_name);
  224. ubsan_epilogue(&flags);
  225. }
  226. static void handle_missaligned_access(struct type_mismatch_data *data,
  227. unsigned long ptr)
  228. {
  229. unsigned long flags;
  230. if (suppress_report(&data->location))
  231. return;
  232. ubsan_prologue(&data->location, &flags);
  233. pr_err("%s misaligned address %p for type %s\n",
  234. type_check_kinds[data->type_check_kind],
  235. (void *)ptr, data->type->type_name);
  236. pr_err("which requires %ld byte alignment\n", data->alignment);
  237. ubsan_epilogue(&flags);
  238. }
  239. static void handle_object_size_mismatch(struct type_mismatch_data *data,
  240. unsigned long ptr)
  241. {
  242. unsigned long flags;
  243. if (suppress_report(&data->location))
  244. return;
  245. ubsan_prologue(&data->location, &flags);
  246. pr_err("%s address %p with insufficient space\n",
  247. type_check_kinds[data->type_check_kind],
  248. (void *) ptr);
  249. pr_err("for an object of type %s\n", data->type->type_name);
  250. ubsan_epilogue(&flags);
  251. }
  252. void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
  253. unsigned long ptr)
  254. {
  255. if (!ptr)
  256. handle_null_ptr_deref(data);
  257. else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
  258. handle_missaligned_access(data, ptr);
  259. else
  260. handle_object_size_mismatch(data, ptr);
  261. }
  262. EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
  263. void __ubsan_handle_nonnull_return(struct nonnull_return_data *data)
  264. {
  265. unsigned long flags;
  266. if (suppress_report(&data->location))
  267. return;
  268. ubsan_prologue(&data->location, &flags);
  269. pr_err("null pointer returned from function declared to never return null\n");
  270. if (location_is_valid(&data->attr_location))
  271. print_source_location("returns_nonnull attribute specified in",
  272. &data->attr_location);
  273. ubsan_epilogue(&flags);
  274. }
  275. EXPORT_SYMBOL(__ubsan_handle_nonnull_return);
  276. void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
  277. unsigned long bound)
  278. {
  279. unsigned long flags;
  280. char bound_str[VALUE_LENGTH];
  281. if (suppress_report(&data->location))
  282. return;
  283. ubsan_prologue(&data->location, &flags);
  284. val_to_string(bound_str, sizeof(bound_str), data->type, bound);
  285. pr_err("variable length array bound value %s <= 0\n", bound_str);
  286. ubsan_epilogue(&flags);
  287. }
  288. EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive);
  289. void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data,
  290. unsigned long index)
  291. {
  292. unsigned long flags;
  293. char index_str[VALUE_LENGTH];
  294. if (suppress_report(&data->location))
  295. return;
  296. ubsan_prologue(&data->location, &flags);
  297. val_to_string(index_str, sizeof(index_str), data->index_type, index);
  298. pr_err("index %s is out of range for type %s\n", index_str,
  299. data->array_type->type_name);
  300. ubsan_epilogue(&flags);
  301. }
  302. EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
  303. void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
  304. unsigned long lhs, unsigned long rhs)
  305. {
  306. unsigned long flags;
  307. struct type_descriptor *rhs_type = data->rhs_type;
  308. struct type_descriptor *lhs_type = data->lhs_type;
  309. char rhs_str[VALUE_LENGTH];
  310. char lhs_str[VALUE_LENGTH];
  311. if (suppress_report(&data->location))
  312. return;
  313. ubsan_prologue(&data->location, &flags);
  314. val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
  315. val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
  316. if (val_is_negative(rhs_type, rhs))
  317. pr_err("shift exponent %s is negative\n", rhs_str);
  318. else if (get_unsigned_val(rhs_type, rhs) >=
  319. type_bit_width(lhs_type))
  320. pr_err("shift exponent %s is too large for %u-bit type %s\n",
  321. rhs_str,
  322. type_bit_width(lhs_type),
  323. lhs_type->type_name);
  324. else if (val_is_negative(lhs_type, lhs))
  325. pr_err("left shift of negative value %s\n",
  326. lhs_str);
  327. else
  328. pr_err("left shift of %s by %s places cannot be"
  329. " represented in type %s\n",
  330. lhs_str, rhs_str,
  331. lhs_type->type_name);
  332. ubsan_epilogue(&flags);
  333. }
  334. EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
  335. void __noreturn
  336. __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
  337. {
  338. unsigned long flags;
  339. ubsan_prologue(&data->location, &flags);
  340. pr_err("calling __builtin_unreachable()\n");
  341. ubsan_epilogue(&flags);
  342. panic("can't return from __builtin_unreachable()");
  343. }
  344. EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
  345. void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
  346. unsigned long val)
  347. {
  348. unsigned long flags;
  349. char val_str[VALUE_LENGTH];
  350. if (suppress_report(&data->location))
  351. return;
  352. ubsan_prologue(&data->location, &flags);
  353. val_to_string(val_str, sizeof(val_str), data->type, val);
  354. pr_err("load of value %s is not a valid value for type %s\n",
  355. val_str, data->type->type_name);
  356. ubsan_epilogue(&flags);
  357. }
  358. EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);