ubsan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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, 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 DEFINE_SPINLOCK(report_lock);
  121. static void ubsan_prologue(struct source_location *location,
  122. unsigned long *flags)
  123. {
  124. current->in_ubsan++;
  125. spin_lock_irqsave(&report_lock, *flags);
  126. pr_err("========================================"
  127. "========================================\n");
  128. print_source_location("UBSAN: Undefined behaviour in", location);
  129. }
  130. static void ubsan_epilogue(unsigned long *flags)
  131. {
  132. dump_stack();
  133. pr_err("========================================"
  134. "========================================\n");
  135. spin_unlock_irqrestore(&report_lock, *flags);
  136. current->in_ubsan--;
  137. }
  138. static void handle_overflow(struct overflow_data *data, void *lhs,
  139. void *rhs, char op)
  140. {
  141. struct type_descriptor *type = data->type;
  142. unsigned long flags;
  143. char lhs_val_str[VALUE_LENGTH];
  144. char rhs_val_str[VALUE_LENGTH];
  145. if (suppress_report(&data->location))
  146. return;
  147. ubsan_prologue(&data->location, &flags);
  148. val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
  149. val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
  150. pr_err("%s integer overflow:\n",
  151. type_is_signed(type) ? "signed" : "unsigned");
  152. pr_err("%s %c %s cannot be represented in type %s\n",
  153. lhs_val_str,
  154. op,
  155. rhs_val_str,
  156. type->type_name);
  157. ubsan_epilogue(&flags);
  158. }
  159. void __ubsan_handle_add_overflow(struct overflow_data *data,
  160. void *lhs, void *rhs)
  161. {
  162. handle_overflow(data, lhs, rhs, '+');
  163. }
  164. EXPORT_SYMBOL(__ubsan_handle_add_overflow);
  165. void __ubsan_handle_sub_overflow(struct overflow_data *data,
  166. void *lhs, void *rhs)
  167. {
  168. handle_overflow(data, lhs, rhs, '-');
  169. }
  170. EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
  171. void __ubsan_handle_mul_overflow(struct overflow_data *data,
  172. void *lhs, void *rhs)
  173. {
  174. handle_overflow(data, lhs, rhs, '*');
  175. }
  176. EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
  177. void __ubsan_handle_negate_overflow(struct overflow_data *data,
  178. void *old_val)
  179. {
  180. unsigned long flags;
  181. char old_val_str[VALUE_LENGTH];
  182. if (suppress_report(&data->location))
  183. return;
  184. ubsan_prologue(&data->location, &flags);
  185. val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
  186. pr_err("negation of %s cannot be represented in type %s:\n",
  187. old_val_str, data->type->type_name);
  188. ubsan_epilogue(&flags);
  189. }
  190. EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
  191. void __ubsan_handle_divrem_overflow(struct overflow_data *data,
  192. void *lhs, void *rhs)
  193. {
  194. unsigned long flags;
  195. char rhs_val_str[VALUE_LENGTH];
  196. if (suppress_report(&data->location))
  197. return;
  198. ubsan_prologue(&data->location, &flags);
  199. val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
  200. if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
  201. pr_err("division of %s by -1 cannot be represented in type %s\n",
  202. rhs_val_str, data->type->type_name);
  203. else
  204. pr_err("division by zero\n");
  205. ubsan_epilogue(&flags);
  206. }
  207. EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
  208. static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
  209. {
  210. unsigned long flags;
  211. if (suppress_report(data->location))
  212. return;
  213. ubsan_prologue(data->location, &flags);
  214. pr_err("%s null pointer of type %s\n",
  215. type_check_kinds[data->type_check_kind],
  216. data->type->type_name);
  217. ubsan_epilogue(&flags);
  218. }
  219. static void handle_misaligned_access(struct type_mismatch_data_common *data,
  220. unsigned long ptr)
  221. {
  222. unsigned long flags;
  223. if (suppress_report(data->location))
  224. return;
  225. ubsan_prologue(data->location, &flags);
  226. pr_err("%s misaligned address %p for type %s\n",
  227. type_check_kinds[data->type_check_kind],
  228. (void *)ptr, data->type->type_name);
  229. pr_err("which requires %ld byte alignment\n", data->alignment);
  230. ubsan_epilogue(&flags);
  231. }
  232. static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
  233. unsigned long ptr)
  234. {
  235. unsigned long flags;
  236. if (suppress_report(data->location))
  237. return;
  238. ubsan_prologue(data->location, &flags);
  239. pr_err("%s address %p with insufficient space\n",
  240. type_check_kinds[data->type_check_kind],
  241. (void *) ptr);
  242. pr_err("for an object of type %s\n", data->type->type_name);
  243. ubsan_epilogue(&flags);
  244. }
  245. static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
  246. unsigned long ptr)
  247. {
  248. if (!ptr)
  249. handle_null_ptr_deref(data);
  250. else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
  251. handle_misaligned_access(data, ptr);
  252. else
  253. handle_object_size_mismatch(data, ptr);
  254. }
  255. void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
  256. void *ptr)
  257. {
  258. struct type_mismatch_data_common common_data = {
  259. .location = &data->location,
  260. .type = data->type,
  261. .alignment = data->alignment,
  262. .type_check_kind = data->type_check_kind
  263. };
  264. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  265. }
  266. EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
  267. void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data,
  268. void *ptr)
  269. {
  270. struct type_mismatch_data_common common_data = {
  271. .location = &data->location,
  272. .type = data->type,
  273. .alignment = 1UL << data->log_alignment,
  274. .type_check_kind = data->type_check_kind
  275. };
  276. ubsan_type_mismatch_common(&common_data, (unsigned long)ptr);
  277. }
  278. EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
  279. void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
  280. void *bound)
  281. {
  282. unsigned long flags;
  283. char bound_str[VALUE_LENGTH];
  284. if (suppress_report(&data->location))
  285. return;
  286. ubsan_prologue(&data->location, &flags);
  287. val_to_string(bound_str, sizeof(bound_str), data->type, bound);
  288. pr_err("variable length array bound value %s <= 0\n", bound_str);
  289. ubsan_epilogue(&flags);
  290. }
  291. EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive);
  292. void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data, void *index)
  293. {
  294. unsigned long flags;
  295. char index_str[VALUE_LENGTH];
  296. if (suppress_report(&data->location))
  297. return;
  298. ubsan_prologue(&data->location, &flags);
  299. val_to_string(index_str, sizeof(index_str), data->index_type, index);
  300. pr_err("index %s is out of range for type %s\n", index_str,
  301. data->array_type->type_name);
  302. ubsan_epilogue(&flags);
  303. }
  304. EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
  305. void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
  306. void *lhs, void *rhs)
  307. {
  308. unsigned long flags;
  309. struct type_descriptor *rhs_type = data->rhs_type;
  310. struct type_descriptor *lhs_type = data->lhs_type;
  311. char rhs_str[VALUE_LENGTH];
  312. char lhs_str[VALUE_LENGTH];
  313. if (suppress_report(&data->location))
  314. return;
  315. ubsan_prologue(&data->location, &flags);
  316. val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
  317. val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
  318. if (val_is_negative(rhs_type, rhs))
  319. pr_err("shift exponent %s is negative\n", rhs_str);
  320. else if (get_unsigned_val(rhs_type, rhs) >=
  321. type_bit_width(lhs_type))
  322. pr_err("shift exponent %s is too large for %u-bit type %s\n",
  323. rhs_str,
  324. type_bit_width(lhs_type),
  325. lhs_type->type_name);
  326. else if (val_is_negative(lhs_type, lhs))
  327. pr_err("left shift of negative value %s\n",
  328. lhs_str);
  329. else
  330. pr_err("left shift of %s by %s places cannot be"
  331. " represented in type %s\n",
  332. lhs_str, rhs_str,
  333. lhs_type->type_name);
  334. ubsan_epilogue(&flags);
  335. }
  336. EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
  337. void __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
  338. {
  339. unsigned long flags;
  340. ubsan_prologue(&data->location, &flags);
  341. pr_err("calling __builtin_unreachable()\n");
  342. ubsan_epilogue(&flags);
  343. panic("can't return from __builtin_unreachable()");
  344. }
  345. EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
  346. void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
  347. void *val)
  348. {
  349. unsigned long flags;
  350. char val_str[VALUE_LENGTH];
  351. if (suppress_report(&data->location))
  352. return;
  353. ubsan_prologue(&data->location, &flags);
  354. val_to_string(val_str, sizeof(val_str), data->type, val);
  355. pr_err("load of value %s is not a valid value for type %s\n",
  356. val_str, data->type->type_name);
  357. ubsan_epilogue(&flags);
  358. }
  359. EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);