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