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