fortify-string.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_FORTIFY_STRING_H_
  3. #define _LINUX_FORTIFY_STRING_H_
  4. #include <linux/bug.h>
  5. #include <linux/const.h>
  6. #include <linux/limits.h>
  7. #define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable
  8. #define __RENAME(x) __asm__(#x)
  9. void fortify_panic(const char *name) __noreturn __cold;
  10. void __read_overflow(void) __compiletime_error("detected read beyond size of object (1st parameter)");
  11. void __read_overflow2(void) __compiletime_error("detected read beyond size of object (2nd parameter)");
  12. void __read_overflow2_field(size_t avail, size_t wanted) __compiletime_warning("detected read beyond size of field (2nd parameter); maybe use struct_group()?");
  13. void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)");
  14. void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning("detected write beyond size of field (1st parameter); maybe use struct_group()?");
  15. #define __compiletime_strlen(p) \
  16. ({ \
  17. char *__p = (char *)(p); \
  18. size_t __ret = SIZE_MAX; \
  19. size_t __p_size = __member_size(p); \
  20. if (__p_size != SIZE_MAX && \
  21. __builtin_constant_p(*__p)) { \
  22. size_t __p_len = __p_size - 1; \
  23. if (__builtin_constant_p(__p[__p_len]) && \
  24. __p[__p_len] == '\0') \
  25. __ret = __builtin_strlen(__p); \
  26. } \
  27. __ret; \
  28. })
  29. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  30. extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
  31. extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
  32. extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
  33. extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
  34. extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
  35. extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
  36. extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
  37. extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
  38. extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
  39. extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
  40. #else
  41. #if defined(__SANITIZE_MEMORY__)
  42. /*
  43. * For KMSAN builds all memcpy/memset/memmove calls should be replaced by the
  44. * corresponding __msan_XXX functions.
  45. */
  46. #include <linux/kmsan_string.h>
  47. #define __underlying_memcpy __msan_memcpy
  48. #define __underlying_memmove __msan_memmove
  49. #define __underlying_memset __msan_memset
  50. #else
  51. #define __underlying_memcpy __builtin_memcpy
  52. #define __underlying_memmove __builtin_memmove
  53. #define __underlying_memset __builtin_memset
  54. #endif
  55. #define __underlying_memchr __builtin_memchr
  56. #define __underlying_memcmp __builtin_memcmp
  57. #define __underlying_strcat __builtin_strcat
  58. #define __underlying_strcpy __builtin_strcpy
  59. #define __underlying_strlen __builtin_strlen
  60. #define __underlying_strncat __builtin_strncat
  61. #define __underlying_strncpy __builtin_strncpy
  62. #endif
  63. /**
  64. * unsafe_memcpy - memcpy implementation with no FORTIFY bounds checking
  65. *
  66. * @dst: Destination memory address to write to
  67. * @src: Source memory address to read from
  68. * @bytes: How many bytes to write to @dst from @src
  69. * @justification: Free-form text or comment describing why the use is needed
  70. *
  71. * This should be used for corner cases where the compiler cannot do the
  72. * right thing, or during transitions between APIs, etc. It should be used
  73. * very rarely, and includes a place for justification detailing where bounds
  74. * checking has happened, and why existing solutions cannot be employed.
  75. */
  76. #define unsafe_memcpy(dst, src, bytes, justification) \
  77. __underlying_memcpy(dst, src, bytes)
  78. /*
  79. * Clang's use of __builtin_*object_size() within inlines needs hinting via
  80. * __pass_*object_size(). The preference is to only ever use type 1 (member
  81. * size, rather than struct size), but there remain some stragglers using
  82. * type 0 that will be converted in the future.
  83. */
  84. #define POS __pass_object_size(1)
  85. #define POS0 __pass_object_size(0)
  86. #define __struct_size(p) __builtin_object_size(p, 0)
  87. #define __member_size(p) __builtin_object_size(p, 1)
  88. #define __compiletime_lessthan(bounds, length) ( \
  89. __builtin_constant_p((bounds) < (length)) && \
  90. (bounds) < (length) \
  91. )
  92. /**
  93. * strncpy - Copy a string to memory with non-guaranteed NUL padding
  94. *
  95. * @p: pointer to destination of copy
  96. * @q: pointer to NUL-terminated source string to copy
  97. * @size: bytes to write at @p
  98. *
  99. * If strlen(@q) >= @size, the copy of @q will stop after @size bytes,
  100. * and @p will NOT be NUL-terminated
  101. *
  102. * If strlen(@q) < @size, following the copy of @q, trailing NUL bytes
  103. * will be written to @p until @size total bytes have been written.
  104. *
  105. * Do not use this function. While FORTIFY_SOURCE tries to avoid
  106. * over-reads of @q, it cannot defend against writing unterminated
  107. * results to @p. Using strncpy() remains ambiguous and fragile.
  108. * Instead, please choose an alternative, so that the expectation
  109. * of @p's contents is unambiguous:
  110. *
  111. * +--------------------+-----------------+------------+
  112. * | @p needs to be: | padded to @size | not padded |
  113. * +====================+=================+============+
  114. * | NUL-terminated | strscpy_pad() | strscpy() |
  115. * +--------------------+-----------------+------------+
  116. * | not NUL-terminated | strtomem_pad() | strtomem() |
  117. * +--------------------+-----------------+------------+
  118. *
  119. * Note strscpy*()'s differing return values for detecting truncation,
  120. * and strtomem*()'s expectation that the destination is marked with
  121. * __nonstring when it is a character array.
  122. *
  123. */
  124. __FORTIFY_INLINE __diagnose_as(__builtin_strncpy, 1, 2, 3)
  125. char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
  126. {
  127. size_t p_size = __member_size(p);
  128. if (__compiletime_lessthan(p_size, size))
  129. __write_overflow();
  130. if (p_size < size)
  131. fortify_panic(__func__);
  132. return __underlying_strncpy(p, q, size);
  133. }
  134. __FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2)
  135. char *strcat(char * const POS p, const char *q)
  136. {
  137. size_t p_size = __member_size(p);
  138. if (p_size == SIZE_MAX)
  139. return __underlying_strcat(p, q);
  140. if (strlcat(p, q, p_size) >= p_size)
  141. fortify_panic(__func__);
  142. return p;
  143. }
  144. extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
  145. __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size_t maxlen)
  146. {
  147. size_t p_size = __member_size(p);
  148. size_t p_len = __compiletime_strlen(p);
  149. size_t ret;
  150. /* We can take compile-time actions when maxlen is const. */
  151. if (__builtin_constant_p(maxlen) && p_len != SIZE_MAX) {
  152. /* If p is const, we can use its compile-time-known len. */
  153. if (maxlen >= p_size)
  154. return p_len;
  155. }
  156. /* Do not check characters beyond the end of p. */
  157. ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
  158. if (p_size <= ret && maxlen != ret)
  159. fortify_panic(__func__);
  160. return ret;
  161. }
  162. /*
  163. * Defined after fortified strnlen to reuse it. However, it must still be
  164. * possible for strlen() to be used on compile-time strings for use in
  165. * static initializers (i.e. as a constant expression).
  166. */
  167. #define strlen(p) \
  168. __builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \
  169. __builtin_strlen(p), __fortify_strlen(p))
  170. __FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1)
  171. __kernel_size_t __fortify_strlen(const char * const POS p)
  172. {
  173. __kernel_size_t ret;
  174. size_t p_size = __member_size(p);
  175. /* Give up if we don't know how large p is. */
  176. if (p_size == SIZE_MAX)
  177. return __underlying_strlen(p);
  178. ret = strnlen(p, p_size);
  179. if (p_size <= ret)
  180. fortify_panic(__func__);
  181. return ret;
  182. }
  183. /* defined after fortified strlen to reuse it */
  184. extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
  185. __FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, size_t size)
  186. {
  187. size_t p_size = __member_size(p);
  188. size_t q_size = __member_size(q);
  189. size_t q_len; /* Full count of source string length. */
  190. size_t len; /* Count of characters going into destination. */
  191. if (p_size == SIZE_MAX && q_size == SIZE_MAX)
  192. return __real_strlcpy(p, q, size);
  193. q_len = strlen(q);
  194. len = (q_len >= size) ? size - 1 : q_len;
  195. if (__builtin_constant_p(size) && __builtin_constant_p(q_len) && size) {
  196. /* Write size is always larger than destination. */
  197. if (len >= p_size)
  198. __write_overflow();
  199. }
  200. if (size) {
  201. if (len >= p_size)
  202. fortify_panic(__func__);
  203. __underlying_memcpy(p, q, len);
  204. p[len] = '\0';
  205. }
  206. return q_len;
  207. }
  208. /* defined after fortified strnlen to reuse it */
  209. extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy);
  210. __FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, size_t size)
  211. {
  212. size_t len;
  213. /* Use string size rather than possible enclosing struct size. */
  214. size_t p_size = __member_size(p);
  215. size_t q_size = __member_size(q);
  216. /* If we cannot get size of p and q default to call strscpy. */
  217. if (p_size == SIZE_MAX && q_size == SIZE_MAX)
  218. return __real_strscpy(p, q, size);
  219. /*
  220. * If size can be known at compile time and is greater than
  221. * p_size, generate a compile time write overflow error.
  222. */
  223. if (__compiletime_lessthan(p_size, size))
  224. __write_overflow();
  225. /*
  226. * This call protects from read overflow, because len will default to q
  227. * length if it smaller than size.
  228. */
  229. len = strnlen(q, size);
  230. /*
  231. * If len equals size, we will copy only size bytes which leads to
  232. * -E2BIG being returned.
  233. * Otherwise we will copy len + 1 because of the final '\O'.
  234. */
  235. len = len == size ? size : len + 1;
  236. /*
  237. * Generate a runtime write overflow error if len is greater than
  238. * p_size.
  239. */
  240. if (len > p_size)
  241. fortify_panic(__func__);
  242. /*
  243. * We can now safely call vanilla strscpy because we are protected from:
  244. * 1. Read overflow thanks to call to strnlen().
  245. * 2. Write overflow thanks to above ifs.
  246. */
  247. return __real_strscpy(p, q, len);
  248. }
  249. /* defined after fortified strlen and strnlen to reuse them */
  250. __FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3)
  251. char *strncat(char * const POS p, const char * const POS q, __kernel_size_t count)
  252. {
  253. size_t p_len, copy_len;
  254. size_t p_size = __member_size(p);
  255. size_t q_size = __member_size(q);
  256. if (p_size == SIZE_MAX && q_size == SIZE_MAX)
  257. return __underlying_strncat(p, q, count);
  258. p_len = strlen(p);
  259. copy_len = strnlen(q, count);
  260. if (p_size < p_len + copy_len + 1)
  261. fortify_panic(__func__);
  262. __underlying_memcpy(p + p_len, q, copy_len);
  263. p[p_len + copy_len] = '\0';
  264. return p;
  265. }
  266. __FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size,
  267. const size_t p_size,
  268. const size_t p_size_field)
  269. {
  270. if (__builtin_constant_p(size)) {
  271. /*
  272. * Length argument is a constant expression, so we
  273. * can perform compile-time bounds checking where
  274. * buffer sizes are also known at compile time.
  275. */
  276. /* Error when size is larger than enclosing struct. */
  277. if (__compiletime_lessthan(p_size_field, p_size) &&
  278. __compiletime_lessthan(p_size, size))
  279. __write_overflow();
  280. /* Warn when write size is larger than dest field. */
  281. if (__compiletime_lessthan(p_size_field, size))
  282. __write_overflow_field(p_size_field, size);
  283. }
  284. /*
  285. * At this point, length argument may not be a constant expression,
  286. * so run-time bounds checking can be done where buffer sizes are
  287. * known. (This is not an "else" because the above checks may only
  288. * be compile-time warnings, and we want to still warn for run-time
  289. * overflows.)
  290. */
  291. /*
  292. * Always stop accesses beyond the struct that contains the
  293. * field, when the buffer's remaining size is known.
  294. * (The SIZE_MAX test is to optimize away checks where the buffer
  295. * lengths are unknown.)
  296. */
  297. if (p_size != SIZE_MAX && p_size < size)
  298. fortify_panic("memset");
  299. }
  300. #define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({ \
  301. size_t __fortify_size = (size_t)(size); \
  302. fortify_memset_chk(__fortify_size, p_size, p_size_field), \
  303. __underlying_memset(p, c, __fortify_size); \
  304. })
  305. /*
  306. * __struct_size() vs __member_size() must be captured here to avoid
  307. * evaluating argument side-effects further into the macro layers.
  308. */
  309. #ifndef CONFIG_KMSAN
  310. #define memset(p, c, s) __fortify_memset_chk(p, c, s, \
  311. __struct_size(p), __member_size(p))
  312. #endif
  313. /*
  314. * To make sure the compiler can enforce protection against buffer overflows,
  315. * memcpy(), memmove(), and memset() must not be used beyond individual
  316. * struct members. If you need to copy across multiple members, please use
  317. * struct_group() to create a named mirror of an anonymous struct union.
  318. * (e.g. see struct sk_buff.) Read overflow checking is currently only
  319. * done when a write overflow is also present, or when building with W=1.
  320. *
  321. * Mitigation coverage matrix
  322. * Bounds checking at:
  323. * +-------+-------+-------+-------+
  324. * | Compile time | Run time |
  325. * memcpy() argument sizes: | write | read | write | read |
  326. * dest source length +-------+-------+-------+-------+
  327. * memcpy(known, known, constant) | y | y | n/a | n/a |
  328. * memcpy(known, unknown, constant) | y | n | n/a | V |
  329. * memcpy(known, known, dynamic) | n | n | B | B |
  330. * memcpy(known, unknown, dynamic) | n | n | B | V |
  331. * memcpy(unknown, known, constant) | n | y | V | n/a |
  332. * memcpy(unknown, unknown, constant) | n | n | V | V |
  333. * memcpy(unknown, known, dynamic) | n | n | V | B |
  334. * memcpy(unknown, unknown, dynamic) | n | n | V | V |
  335. * +-------+-------+-------+-------+
  336. *
  337. * y = perform deterministic compile-time bounds checking
  338. * n = cannot perform deterministic compile-time bounds checking
  339. * n/a = no run-time bounds checking needed since compile-time deterministic
  340. * B = can perform run-time bounds checking (currently unimplemented)
  341. * V = vulnerable to run-time overflow (will need refactoring to solve)
  342. *
  343. */
  344. __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
  345. const size_t p_size,
  346. const size_t q_size,
  347. const size_t p_size_field,
  348. const size_t q_size_field,
  349. const char *func)
  350. {
  351. if (__builtin_constant_p(size)) {
  352. /*
  353. * Length argument is a constant expression, so we
  354. * can perform compile-time bounds checking where
  355. * buffer sizes are also known at compile time.
  356. */
  357. /* Error when size is larger than enclosing struct. */
  358. if (__compiletime_lessthan(p_size_field, p_size) &&
  359. __compiletime_lessthan(p_size, size))
  360. __write_overflow();
  361. if (__compiletime_lessthan(q_size_field, q_size) &&
  362. __compiletime_lessthan(q_size, size))
  363. __read_overflow2();
  364. /* Warn when write size argument larger than dest field. */
  365. if (__compiletime_lessthan(p_size_field, size))
  366. __write_overflow_field(p_size_field, size);
  367. /*
  368. * Warn for source field over-read when building with W=1
  369. * or when an over-write happened, so both can be fixed at
  370. * the same time.
  371. */
  372. if ((IS_ENABLED(KBUILD_EXTRA_WARN1) ||
  373. __compiletime_lessthan(p_size_field, size)) &&
  374. __compiletime_lessthan(q_size_field, size))
  375. __read_overflow2_field(q_size_field, size);
  376. }
  377. /*
  378. * At this point, length argument may not be a constant expression,
  379. * so run-time bounds checking can be done where buffer sizes are
  380. * known. (This is not an "else" because the above checks may only
  381. * be compile-time warnings, and we want to still warn for run-time
  382. * overflows.)
  383. */
  384. /*
  385. * Always stop accesses beyond the struct that contains the
  386. * field, when the buffer's remaining size is known.
  387. * (The SIZE_MAX test is to optimize away checks where the buffer
  388. * lengths are unknown.)
  389. */
  390. if ((p_size != SIZE_MAX && p_size < size) ||
  391. (q_size != SIZE_MAX && q_size < size))
  392. fortify_panic(func);
  393. /*
  394. * Warn when writing beyond destination field size.
  395. *
  396. * We must ignore p_size_field == 0 for existing 0-element
  397. * fake flexible arrays, until they are all converted to
  398. * proper flexible arrays.
  399. *
  400. * The implementation of __builtin_*object_size() behaves
  401. * like sizeof() when not directly referencing a flexible
  402. * array member, which means there will be many bounds checks
  403. * that will appear at run-time, without a way for them to be
  404. * detected at compile-time (as can be done when the destination
  405. * is specifically the flexible array member).
  406. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832
  407. */
  408. if (p_size_field != 0 && p_size_field != SIZE_MAX &&
  409. p_size != p_size_field && p_size_field < size)
  410. return true;
  411. return false;
  412. }
  413. #define __fortify_memcpy_chk(p, q, size, p_size, q_size, \
  414. p_size_field, q_size_field, op) ({ \
  415. const size_t __fortify_size = (size_t)(size); \
  416. const size_t __p_size = (p_size); \
  417. const size_t __q_size = (q_size); \
  418. const size_t __p_size_field = (p_size_field); \
  419. const size_t __q_size_field = (q_size_field); \
  420. WARN_ONCE(fortify_memcpy_chk(__fortify_size, __p_size, \
  421. __q_size, __p_size_field, \
  422. __q_size_field, #op), \
  423. #op ": detected field-spanning write (size %zu) of single %s (size %zu)\n", \
  424. __fortify_size, \
  425. "field \"" #p "\" at " __FILE__ ":" __stringify(__LINE__), \
  426. __p_size_field); \
  427. __underlying_##op(p, q, __fortify_size); \
  428. })
  429. /*
  430. * Notes about compile-time buffer size detection:
  431. *
  432. * With these types...
  433. *
  434. * struct middle {
  435. * u16 a;
  436. * u8 middle_buf[16];
  437. * int b;
  438. * };
  439. * struct end {
  440. * u16 a;
  441. * u8 end_buf[16];
  442. * };
  443. * struct flex {
  444. * int a;
  445. * u8 flex_buf[];
  446. * };
  447. *
  448. * void func(TYPE *ptr) { ... }
  449. *
  450. * Cases where destination size cannot be currently detected:
  451. * - the size of ptr's object (seemingly by design, gcc & clang fail):
  452. * __builtin_object_size(ptr, 1) == SIZE_MAX
  453. * - the size of flexible arrays in ptr's obj (by design, dynamic size):
  454. * __builtin_object_size(ptr->flex_buf, 1) == SIZE_MAX
  455. * - the size of ANY array at the end of ptr's obj (gcc and clang bug):
  456. * __builtin_object_size(ptr->end_buf, 1) == SIZE_MAX
  457. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836
  458. *
  459. * Cases where destination size is currently detected:
  460. * - the size of non-array members within ptr's object:
  461. * __builtin_object_size(ptr->a, 1) == 2
  462. * - the size of non-flexible-array in the middle of ptr's obj:
  463. * __builtin_object_size(ptr->middle_buf, 1) == 16
  464. *
  465. */
  466. /*
  467. * __struct_size() vs __member_size() must be captured here to avoid
  468. * evaluating argument side-effects further into the macro layers.
  469. */
  470. #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \
  471. __struct_size(p), __struct_size(q), \
  472. __member_size(p), __member_size(q), \
  473. memcpy)
  474. #define memmove(p, q, s) __fortify_memcpy_chk(p, q, s, \
  475. __struct_size(p), __struct_size(q), \
  476. __member_size(p), __member_size(q), \
  477. memmove)
  478. extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
  479. __FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
  480. {
  481. size_t p_size = __struct_size(p);
  482. if (__compiletime_lessthan(p_size, size))
  483. __read_overflow();
  484. if (p_size < size)
  485. fortify_panic(__func__);
  486. return __real_memscan(p, c, size);
  487. }
  488. __FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3)
  489. int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t size)
  490. {
  491. size_t p_size = __struct_size(p);
  492. size_t q_size = __struct_size(q);
  493. if (__builtin_constant_p(size)) {
  494. if (__compiletime_lessthan(p_size, size))
  495. __read_overflow();
  496. if (__compiletime_lessthan(q_size, size))
  497. __read_overflow2();
  498. }
  499. if (p_size < size || q_size < size)
  500. fortify_panic(__func__);
  501. return __underlying_memcmp(p, q, size);
  502. }
  503. __FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3)
  504. void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
  505. {
  506. size_t p_size = __struct_size(p);
  507. if (__compiletime_lessthan(p_size, size))
  508. __read_overflow();
  509. if (p_size < size)
  510. fortify_panic(__func__);
  511. return __underlying_memchr(p, c, size);
  512. }
  513. void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
  514. __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
  515. {
  516. size_t p_size = __struct_size(p);
  517. if (__compiletime_lessthan(p_size, size))
  518. __read_overflow();
  519. if (p_size < size)
  520. fortify_panic(__func__);
  521. return __real_memchr_inv(p, c, size);
  522. }
  523. extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
  524. __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp)
  525. {
  526. size_t p_size = __struct_size(p);
  527. if (__compiletime_lessthan(p_size, size))
  528. __read_overflow();
  529. if (p_size < size)
  530. fortify_panic(__func__);
  531. return __real_kmemdup(p, size, gfp);
  532. }
  533. /* Defined after fortified strlen to reuse it. */
  534. __FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2)
  535. char *strcpy(char * const POS p, const char * const POS q)
  536. {
  537. size_t p_size = __member_size(p);
  538. size_t q_size = __member_size(q);
  539. size_t size;
  540. /* If neither buffer size is known, immediately give up. */
  541. if (__builtin_constant_p(p_size) &&
  542. __builtin_constant_p(q_size) &&
  543. p_size == SIZE_MAX && q_size == SIZE_MAX)
  544. return __underlying_strcpy(p, q);
  545. size = strlen(q) + 1;
  546. /* Compile-time check for const size overflow. */
  547. if (__compiletime_lessthan(p_size, size))
  548. __write_overflow();
  549. /* Run-time check for dynamic size overflow. */
  550. if (p_size < size)
  551. fortify_panic(__func__);
  552. __underlying_memcpy(p, q, size);
  553. return p;
  554. }
  555. /* Don't use these outside the FORITFY_SOURCE implementation */
  556. #undef __underlying_memchr
  557. #undef __underlying_memcmp
  558. #undef __underlying_strcat
  559. #undef __underlying_strcpy
  560. #undef __underlying_strlen
  561. #undef __underlying_strncat
  562. #undef __underlying_strncpy
  563. #undef POS
  564. #undef POS0
  565. #endif /* _LINUX_FORTIFY_STRING_H_ */