xref: /linux-6.15/include/linux/fortify-string.h (revision 239d8732)
1a28a6e86SFrancis Laniel /* SPDX-License-Identifier: GPL-2.0 */
2a28a6e86SFrancis Laniel #ifndef _LINUX_FORTIFY_STRING_H_
3a28a6e86SFrancis Laniel #define _LINUX_FORTIFY_STRING_H_
4a28a6e86SFrancis Laniel 
5475ddf1fSKees Cook #include <linux/bitfield.h>
654d9469bSKees Cook #include <linux/bug.h>
767ebc3abSKees Cook #include <linux/const.h>
8311fb40aSKees Cook #include <linux/limits.h>
967ebc3abSKees Cook 
10281d0c96SKees Cook #define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable
11c430f600SKees Cook #define __RENAME(x) __asm__(#x)
12c430f600SKees Cook 
13475ddf1fSKees Cook #define FORTIFY_REASON_DIR(r)		FIELD_GET(BIT(0), r)
14475ddf1fSKees Cook #define FORTIFY_REASON_FUNC(r)		FIELD_GET(GENMASK(7, 1), r)
15475ddf1fSKees Cook #define FORTIFY_REASON(func, write)	(FIELD_PREP(BIT(0), write) | \
16475ddf1fSKees Cook 					 FIELD_PREP(GENMASK(7, 1), func))
17475ddf1fSKees Cook 
1826f812baSKees Cook /* Overridden by KUnit tests. */
194ce615e7SKees Cook #ifndef fortify_panic
203d965b33SKees Cook # define fortify_panic(func, write, avail, size, retfail)	\
213d965b33SKees Cook 	 __fortify_panic(FORTIFY_REASON(func, write), avail, size)
224ce615e7SKees Cook #endif
2326f812baSKees Cook #ifndef fortify_warn_once
2426f812baSKees Cook # define fortify_warn_once(x...)	WARN_ONCE(x)
2526f812baSKees Cook #endif
26475ddf1fSKees Cook 
27475ddf1fSKees Cook #define FORTIFY_READ		 0
28475ddf1fSKees Cook #define FORTIFY_WRITE		 1
29475ddf1fSKees Cook 
30475ddf1fSKees Cook #define EACH_FORTIFY_FUNC(macro)	\
31475ddf1fSKees Cook 	macro(strncpy),			\
32475ddf1fSKees Cook 	macro(strnlen),			\
33475ddf1fSKees Cook 	macro(strlen),			\
34475ddf1fSKees Cook 	macro(strscpy),			\
35475ddf1fSKees Cook 	macro(strlcat),			\
36475ddf1fSKees Cook 	macro(strcat),			\
37475ddf1fSKees Cook 	macro(strncat),			\
38475ddf1fSKees Cook 	macro(memset),			\
39475ddf1fSKees Cook 	macro(memcpy),			\
40475ddf1fSKees Cook 	macro(memmove),			\
41475ddf1fSKees Cook 	macro(memscan),			\
42475ddf1fSKees Cook 	macro(memcmp),			\
43475ddf1fSKees Cook 	macro(memchr),			\
44475ddf1fSKees Cook 	macro(memchr_inv),		\
45475ddf1fSKees Cook 	macro(kmemdup),			\
46475ddf1fSKees Cook 	macro(strcpy),			\
47475ddf1fSKees Cook 	macro(UNKNOWN),
48475ddf1fSKees Cook 
49475ddf1fSKees Cook #define MAKE_FORTIFY_FUNC(func)	FORTIFY_FUNC_##func
50475ddf1fSKees Cook 
51475ddf1fSKees Cook enum fortify_func {
52475ddf1fSKees Cook 	EACH_FORTIFY_FUNC(MAKE_FORTIFY_FUNC)
53475ddf1fSKees Cook };
54475ddf1fSKees Cook 
553d965b33SKees Cook void __fortify_report(const u8 reason, const size_t avail, const size_t size);
563d965b33SKees Cook void __fortify_panic(const u8 reason, const size_t avail, const size_t size) __cold __noreturn;
57c430f600SKees Cook void __read_overflow(void) __compiletime_error("detected read beyond size of object (1st parameter)");
58c430f600SKees Cook void __read_overflow2(void) __compiletime_error("detected read beyond size of object (2nd parameter)");
59f68f2ff9SKees Cook void __read_overflow2_field(size_t avail, size_t wanted) __compiletime_warning("detected read beyond size of field (2nd parameter); maybe use struct_group()?");
60c430f600SKees Cook void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)");
61f68f2ff9SKees Cook void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning("detected write beyond size of field (1st parameter); maybe use struct_group()?");
62a28a6e86SFrancis Laniel 
633009f891SKees Cook #define __compiletime_strlen(p)					\
643009f891SKees Cook ({								\
65e9a40e15SKees Cook 	char *__p = (char *)(p);				\
66311fb40aSKees Cook 	size_t __ret = SIZE_MAX;				\
6721a2c74bSKees Cook 	const size_t __p_size = __member_size(p);		\
68311fb40aSKees Cook 	if (__p_size != SIZE_MAX &&				\
69d07c0acbSKees Cook 	    __builtin_constant_p(*__p)) {			\
7095cadae3SQian Cai 		size_t __p_len = __p_size - 1;			\
7195cadae3SQian Cai 		if (__builtin_constant_p(__p[__p_len]) &&	\
7295cadae3SQian Cai 		    __p[__p_len] == '\0')			\
7395cadae3SQian Cai 			__ret = __builtin_strlen(__p);		\
743009f891SKees Cook 	}							\
7595cadae3SQian Cai 	__ret;							\
763009f891SKees Cook })
773009f891SKees Cook 
782e577732SAndrey Konovalov #if defined(__SANITIZE_ADDRESS__)
792e577732SAndrey Konovalov 
802e577732SAndrey Konovalov #if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY)
812e577732SAndrey Konovalov extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
822e577732SAndrey Konovalov extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
832e577732SAndrey Konovalov extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
842e577732SAndrey Konovalov #elif defined(CONFIG_KASAN_GENERIC)
852e577732SAndrey Konovalov extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__asan_memset);
862e577732SAndrey Konovalov extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memmove);
872e577732SAndrey Konovalov extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memcpy);
882e577732SAndrey Konovalov #else /* CONFIG_KASAN_SW_TAGS */
892e577732SAndrey Konovalov extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__hwasan_memset);
902e577732SAndrey Konovalov extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memmove);
912e577732SAndrey Konovalov extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memcpy);
922e577732SAndrey Konovalov #endif
932e577732SAndrey Konovalov 
94a28a6e86SFrancis Laniel extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
95a28a6e86SFrancis Laniel extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
96a28a6e86SFrancis Laniel extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
97a28a6e86SFrancis Laniel extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
98a28a6e86SFrancis Laniel extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
99a28a6e86SFrancis Laniel extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
100a28a6e86SFrancis Laniel extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
1012e577732SAndrey Konovalov 
102a28a6e86SFrancis Laniel #else
10378a498c3SAlexander Potapenko 
10478a498c3SAlexander Potapenko #if defined(__SANITIZE_MEMORY__)
10578a498c3SAlexander Potapenko /*
10678a498c3SAlexander Potapenko  * For KMSAN builds all memcpy/memset/memmove calls should be replaced by the
10778a498c3SAlexander Potapenko  * corresponding __msan_XXX functions.
10878a498c3SAlexander Potapenko  */
10978a498c3SAlexander Potapenko #include <linux/kmsan_string.h>
11078a498c3SAlexander Potapenko #define __underlying_memcpy	__msan_memcpy
11178a498c3SAlexander Potapenko #define __underlying_memmove	__msan_memmove
11278a498c3SAlexander Potapenko #define __underlying_memset	__msan_memset
11378a498c3SAlexander Potapenko #else
114a28a6e86SFrancis Laniel #define __underlying_memcpy	__builtin_memcpy
115a28a6e86SFrancis Laniel #define __underlying_memmove	__builtin_memmove
116a28a6e86SFrancis Laniel #define __underlying_memset	__builtin_memset
11778a498c3SAlexander Potapenko #endif
11878a498c3SAlexander Potapenko 
11978a498c3SAlexander Potapenko #define __underlying_memchr	__builtin_memchr
12078a498c3SAlexander Potapenko #define __underlying_memcmp	__builtin_memcmp
121a28a6e86SFrancis Laniel #define __underlying_strcat	__builtin_strcat
122a28a6e86SFrancis Laniel #define __underlying_strcpy	__builtin_strcpy
123a28a6e86SFrancis Laniel #define __underlying_strlen	__builtin_strlen
124a28a6e86SFrancis Laniel #define __underlying_strncat	__builtin_strncat
125a28a6e86SFrancis Laniel #define __underlying_strncpy	__builtin_strncpy
1262e577732SAndrey Konovalov 
127a28a6e86SFrancis Laniel #endif
128a28a6e86SFrancis Laniel 
12943213daeSKees Cook /**
13043213daeSKees Cook  * unsafe_memcpy - memcpy implementation with no FORTIFY bounds checking
13143213daeSKees Cook  *
13243213daeSKees Cook  * @dst: Destination memory address to write to
13343213daeSKees Cook  * @src: Source memory address to read from
13443213daeSKees Cook  * @bytes: How many bytes to write to @dst from @src
13543213daeSKees Cook  * @justification: Free-form text or comment describing why the use is needed
13643213daeSKees Cook  *
13743213daeSKees Cook  * This should be used for corner cases where the compiler cannot do the
13843213daeSKees Cook  * right thing, or during transitions between APIs, etc. It should be used
13943213daeSKees Cook  * very rarely, and includes a place for justification detailing where bounds
14043213daeSKees Cook  * checking has happened, and why existing solutions cannot be employed.
14143213daeSKees Cook  */
14243213daeSKees Cook #define unsafe_memcpy(dst, src, bytes, justification)		\
14343213daeSKees Cook 	__underlying_memcpy(dst, src, bytes)
14443213daeSKees Cook 
145281d0c96SKees Cook /*
1469f7d69c5SKees Cook  * Clang's use of __builtin_*object_size() within inlines needs hinting via
1479f7d69c5SKees Cook  * __pass_*object_size(). The preference is to only ever use type 1 (member
148281d0c96SKees Cook  * size, rather than struct size), but there remain some stragglers using
149281d0c96SKees Cook  * type 0 that will be converted in the future.
150281d0c96SKees Cook  */
151439a1bcaSKees Cook #if __has_builtin(__builtin_dynamic_object_size)
152439a1bcaSKees Cook #define POS			__pass_dynamic_object_size(1)
153439a1bcaSKees Cook #define POS0			__pass_dynamic_object_size(0)
154439a1bcaSKees Cook #else
155281d0c96SKees Cook #define POS			__pass_object_size(1)
156281d0c96SKees Cook #define POS0			__pass_object_size(0)
157439a1bcaSKees Cook #endif
158281d0c96SKees Cook 
159fa35198fSKees Cook #define __compiletime_lessthan(bounds, length)	(	\
160fa35198fSKees Cook 	__builtin_constant_p((bounds) < (length)) &&	\
161fa35198fSKees Cook 	(bounds) < (length)				\
162fa35198fSKees Cook )
163fa35198fSKees Cook 
164dfbafa70SKees Cook /**
165dfbafa70SKees Cook  * strncpy - Copy a string to memory with non-guaranteed NUL padding
166dfbafa70SKees Cook  *
167dfbafa70SKees Cook  * @p: pointer to destination of copy
168dfbafa70SKees Cook  * @q: pointer to NUL-terminated source string to copy
169dfbafa70SKees Cook  * @size: bytes to write at @p
170dfbafa70SKees Cook  *
171dfbafa70SKees Cook  * If strlen(@q) >= @size, the copy of @q will stop after @size bytes,
172dfbafa70SKees Cook  * and @p will NOT be NUL-terminated
173dfbafa70SKees Cook  *
174dfbafa70SKees Cook  * If strlen(@q) < @size, following the copy of @q, trailing NUL bytes
175dfbafa70SKees Cook  * will be written to @p until @size total bytes have been written.
176dfbafa70SKees Cook  *
177dfbafa70SKees Cook  * Do not use this function. While FORTIFY_SOURCE tries to avoid
178dfbafa70SKees Cook  * over-reads of @q, it cannot defend against writing unterminated
179dfbafa70SKees Cook  * results to @p. Using strncpy() remains ambiguous and fragile.
180dfbafa70SKees Cook  * Instead, please choose an alternative, so that the expectation
181dfbafa70SKees Cook  * of @p's contents is unambiguous:
182dfbafa70SKees Cook  *
18303699f27SKees Cook  * +--------------------+--------------------+------------+
18403699f27SKees Cook  * | **p** needs to be: | padded to **size** | not padded |
18503699f27SKees Cook  * +====================+====================+============+
186dfbafa70SKees Cook  * |     NUL-terminated | strscpy_pad()      | strscpy()  |
18703699f27SKees Cook  * +--------------------+--------------------+------------+
188dfbafa70SKees Cook  * | not NUL-terminated | strtomem_pad()     | strtomem() |
18903699f27SKees Cook  * +--------------------+--------------------+------------+
190dfbafa70SKees Cook  *
191dfbafa70SKees Cook  * Note strscpy*()'s differing return values for detecting truncation,
192dfbafa70SKees Cook  * and strtomem*()'s expectation that the destination is marked with
193dfbafa70SKees Cook  * __nonstring when it is a character array.
194dfbafa70SKees Cook  *
195dfbafa70SKees Cook  */
19692df138aSKees Cook __FORTIFY_INLINE __diagnose_as(__builtin_strncpy, 1, 2, 3)
strncpy(char * const POS p,const char * q,__kernel_size_t size)197281d0c96SKees Cook char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
198a28a6e86SFrancis Laniel {
19921a2c74bSKees Cook 	const size_t p_size = __member_size(p);
200a28a6e86SFrancis Laniel 
201fa35198fSKees Cook 	if (__compiletime_lessthan(p_size, size))
202a28a6e86SFrancis Laniel 		__write_overflow();
203a28a6e86SFrancis Laniel 	if (p_size < size)
2043d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strncpy, FORTIFY_WRITE, p_size, size, p);
205a28a6e86SFrancis Laniel 	return __underlying_strncpy(p, q, size);
206a28a6e86SFrancis Laniel }
207a28a6e86SFrancis Laniel 
208369cd216SKees Cook extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
20903699f27SKees Cook /**
21003699f27SKees Cook  * strnlen - Return bounded count of characters in a NUL-terminated string
21103699f27SKees Cook  *
21203699f27SKees Cook  * @p: pointer to NUL-terminated string to count.
21303699f27SKees Cook  * @maxlen: maximum number of characters to count.
21403699f27SKees Cook  *
21503699f27SKees Cook  * Returns number of characters in @p (NOT including the final NUL), or
21603699f27SKees Cook  * @maxlen, if no NUL has been found up to there.
21703699f27SKees Cook  *
21803699f27SKees Cook  */
strnlen(const char * const POS p,__kernel_size_t maxlen)219281d0c96SKees Cook __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size_t maxlen)
220369cd216SKees Cook {
22121a2c74bSKees Cook 	const size_t p_size = __member_size(p);
22221a2c74bSKees Cook 	const size_t p_len = __compiletime_strlen(p);
2233009f891SKees Cook 	size_t ret;
224369cd216SKees Cook 
2253009f891SKees Cook 	/* We can take compile-time actions when maxlen is const. */
226311fb40aSKees Cook 	if (__builtin_constant_p(maxlen) && p_len != SIZE_MAX) {
2273009f891SKees Cook 		/* If p is const, we can use its compile-time-known len. */
2283009f891SKees Cook 		if (maxlen >= p_size)
2293009f891SKees Cook 			return p_len;
2303009f891SKees Cook 	}
2313009f891SKees Cook 
2323009f891SKees Cook 	/* Do not check characters beyond the end of p. */
2333009f891SKees Cook 	ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
234369cd216SKees Cook 	if (p_size <= ret && maxlen != ret)
2353d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strnlen, FORTIFY_READ, p_size, ret + 1, ret);
236369cd216SKees Cook 	return ret;
237369cd216SKees Cook }
238369cd216SKees Cook 
23967ebc3abSKees Cook /*
24067ebc3abSKees Cook  * Defined after fortified strnlen to reuse it. However, it must still be
24167ebc3abSKees Cook  * possible for strlen() to be used on compile-time strings for use in
24267ebc3abSKees Cook  * static initializers (i.e. as a constant expression).
24367ebc3abSKees Cook  */
24403699f27SKees Cook /**
24503699f27SKees Cook  * strlen - Return count of characters in a NUL-terminated string
24603699f27SKees Cook  *
24703699f27SKees Cook  * @p: pointer to NUL-terminated string to count.
24803699f27SKees Cook  *
24903699f27SKees Cook  * Do not use this function unless the string length is known at
25003699f27SKees Cook  * compile-time. When @p is unterminated, this function may crash
25103699f27SKees Cook  * or return unexpected counts that could lead to memory content
25203699f27SKees Cook  * exposures. Prefer strnlen().
25303699f27SKees Cook  *
25403699f27SKees Cook  * Returns number of characters in @p (NOT including the final NUL).
25503699f27SKees Cook  *
25603699f27SKees Cook  */
25767ebc3abSKees Cook #define strlen(p)							\
25867ebc3abSKees Cook 	__builtin_choose_expr(__is_constexpr(__builtin_strlen(p)),	\
25967ebc3abSKees Cook 		__builtin_strlen(p), __fortify_strlen(p))
26092df138aSKees Cook __FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1)
__fortify_strlen(const char * const POS p)261281d0c96SKees Cook __kernel_size_t __fortify_strlen(const char * const POS p)
262a28a6e86SFrancis Laniel {
26321a2c74bSKees Cook 	const size_t p_size = __member_size(p);
264a28a6e86SFrancis Laniel 	__kernel_size_t ret;
265a28a6e86SFrancis Laniel 
2663009f891SKees Cook 	/* Give up if we don't know how large p is. */
267311fb40aSKees Cook 	if (p_size == SIZE_MAX)
268a28a6e86SFrancis Laniel 		return __underlying_strlen(p);
269a28a6e86SFrancis Laniel 	ret = strnlen(p, p_size);
270a28a6e86SFrancis Laniel 	if (p_size <= ret)
2713d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strlen, FORTIFY_READ, p_size, ret + 1, ret);
272a28a6e86SFrancis Laniel 	return ret;
273a28a6e86SFrancis Laniel }
274a28a6e86SFrancis Laniel 
27503699f27SKees Cook /* Defined after fortified strnlen() to reuse it. */
276e6584c39SKees Cook extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(sized_strscpy);
sized_strscpy(char * const POS p,const char * const POS q,size_t size)277e6584c39SKees Cook __FORTIFY_INLINE ssize_t sized_strscpy(char * const POS p, const char * const POS q, size_t size)
278a28a6e86SFrancis Laniel {
279a28a6e86SFrancis Laniel 	/* Use string size rather than possible enclosing struct size. */
28021a2c74bSKees Cook 	const size_t p_size = __member_size(p);
28121a2c74bSKees Cook 	const size_t q_size = __member_size(q);
28221a2c74bSKees Cook 	size_t len;
283a28a6e86SFrancis Laniel 
284a28a6e86SFrancis Laniel 	/* If we cannot get size of p and q default to call strscpy. */
285311fb40aSKees Cook 	if (p_size == SIZE_MAX && q_size == SIZE_MAX)
286a28a6e86SFrancis Laniel 		return __real_strscpy(p, q, size);
287a28a6e86SFrancis Laniel 
288a28a6e86SFrancis Laniel 	/*
289a28a6e86SFrancis Laniel 	 * If size can be known at compile time and is greater than
290a28a6e86SFrancis Laniel 	 * p_size, generate a compile time write overflow error.
291a28a6e86SFrancis Laniel 	 */
292fa35198fSKees Cook 	if (__compiletime_lessthan(p_size, size))
293a28a6e86SFrancis Laniel 		__write_overflow();
294a28a6e86SFrancis Laniel 
29562e1cbfcSKees Cook 	/* Short-circuit for compile-time known-safe lengths. */
29662e1cbfcSKees Cook 	if (__compiletime_lessthan(p_size, SIZE_MAX)) {
29762e1cbfcSKees Cook 		len = __compiletime_strlen(q);
29862e1cbfcSKees Cook 
29962e1cbfcSKees Cook 		if (len < SIZE_MAX && __compiletime_lessthan(len, size)) {
30062e1cbfcSKees Cook 			__underlying_memcpy(p, q, len + 1);
30162e1cbfcSKees Cook 			return len;
30262e1cbfcSKees Cook 		}
30362e1cbfcSKees Cook 	}
30462e1cbfcSKees Cook 
305a28a6e86SFrancis Laniel 	/*
306a28a6e86SFrancis Laniel 	 * This call protects from read overflow, because len will default to q
307a28a6e86SFrancis Laniel 	 * length if it smaller than size.
308a28a6e86SFrancis Laniel 	 */
309a28a6e86SFrancis Laniel 	len = strnlen(q, size);
310a28a6e86SFrancis Laniel 	/*
311a28a6e86SFrancis Laniel 	 * If len equals size, we will copy only size bytes which leads to
312a28a6e86SFrancis Laniel 	 * -E2BIG being returned.
313a28a6e86SFrancis Laniel 	 * Otherwise we will copy len + 1 because of the final '\O'.
314a28a6e86SFrancis Laniel 	 */
315a28a6e86SFrancis Laniel 	len = len == size ? size : len + 1;
316a28a6e86SFrancis Laniel 
317a28a6e86SFrancis Laniel 	/*
318a28a6e86SFrancis Laniel 	 * Generate a runtime write overflow error if len is greater than
319a28a6e86SFrancis Laniel 	 * p_size.
320a28a6e86SFrancis Laniel 	 */
3213d965b33SKees Cook 	if (p_size < len)
3223d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strscpy, FORTIFY_WRITE, p_size, len, -E2BIG);
323a28a6e86SFrancis Laniel 
324a28a6e86SFrancis Laniel 	/*
325a28a6e86SFrancis Laniel 	 * We can now safely call vanilla strscpy because we are protected from:
326a28a6e86SFrancis Laniel 	 * 1. Read overflow thanks to call to strnlen().
327a28a6e86SFrancis Laniel 	 * 2. Write overflow thanks to above ifs.
328a28a6e86SFrancis Laniel 	 */
329a28a6e86SFrancis Laniel 	return __real_strscpy(p, q, len);
330a28a6e86SFrancis Laniel }
331a28a6e86SFrancis Laniel 
332605395cdSKees Cook /* Defined after fortified strlen() to reuse it. */
333605395cdSKees Cook extern size_t __real_strlcat(char *p, const char *q, size_t avail) __RENAME(strlcat);
334605395cdSKees Cook /**
335605395cdSKees Cook  * strlcat - Append a string to an existing string
336605395cdSKees Cook  *
337605395cdSKees Cook  * @p: pointer to %NUL-terminated string to append to
338605395cdSKees Cook  * @q: pointer to %NUL-terminated string to append from
339605395cdSKees Cook  * @avail: Maximum bytes available in @p
340605395cdSKees Cook  *
341605395cdSKees Cook  * Appends %NUL-terminated string @q after the %NUL-terminated
342605395cdSKees Cook  * string at @p, but will not write beyond @avail bytes total,
343605395cdSKees Cook  * potentially truncating the copy from @q. @p will stay
344605395cdSKees Cook  * %NUL-terminated only if a %NUL already existed within
345605395cdSKees Cook  * the @avail bytes of @p. If so, the resulting number of
346605395cdSKees Cook  * bytes copied from @q will be at most "@avail - strlen(@p) - 1".
347605395cdSKees Cook  *
348605395cdSKees Cook  * Do not use this function. While FORTIFY_SOURCE tries to avoid
349605395cdSKees Cook  * read and write overflows, this is only possible when the sizes
350605395cdSKees Cook  * of @p and @q are known to the compiler. Prefer building the
351605395cdSKees Cook  * string with formatting, via scnprintf(), seq_buf, or similar.
352605395cdSKees Cook  *
353605395cdSKees Cook  * Returns total bytes that _would_ have been contained by @p
354605395cdSKees Cook  * regardless of truncation, similar to snprintf(). If return
355605395cdSKees Cook  * value is >= @avail, the string has been truncated.
356605395cdSKees Cook  *
357605395cdSKees Cook  */
358605395cdSKees Cook __FORTIFY_INLINE
strlcat(char * const POS p,const char * const POS q,size_t avail)359605395cdSKees Cook size_t strlcat(char * const POS p, const char * const POS q, size_t avail)
360605395cdSKees Cook {
361605395cdSKees Cook 	const size_t p_size = __member_size(p);
362605395cdSKees Cook 	const size_t q_size = __member_size(q);
363605395cdSKees Cook 	size_t p_len, copy_len;
364605395cdSKees Cook 	size_t actual, wanted;
365605395cdSKees Cook 
366605395cdSKees Cook 	/* Give up immediately if both buffer sizes are unknown. */
367605395cdSKees Cook 	if (p_size == SIZE_MAX && q_size == SIZE_MAX)
368605395cdSKees Cook 		return __real_strlcat(p, q, avail);
369605395cdSKees Cook 
370605395cdSKees Cook 	p_len = strnlen(p, avail);
371605395cdSKees Cook 	copy_len = strlen(q);
372605395cdSKees Cook 	wanted = actual = p_len + copy_len;
373605395cdSKees Cook 
374605395cdSKees Cook 	/* Cannot append any more: report truncation. */
375605395cdSKees Cook 	if (avail <= p_len)
376605395cdSKees Cook 		return wanted;
377605395cdSKees Cook 
378605395cdSKees Cook 	/* Give up if string is already overflowed. */
379605395cdSKees Cook 	if (p_size <= p_len)
3803d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strlcat, FORTIFY_READ, p_size, p_len + 1, wanted);
381605395cdSKees Cook 
382605395cdSKees Cook 	if (actual >= avail) {
383605395cdSKees Cook 		copy_len = avail - p_len - 1;
384605395cdSKees Cook 		actual = p_len + copy_len;
385605395cdSKees Cook 	}
386605395cdSKees Cook 
387605395cdSKees Cook 	/* Give up if copy will overflow. */
388605395cdSKees Cook 	if (p_size <= actual)
3893d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strlcat, FORTIFY_WRITE, p_size, actual + 1, wanted);
390605395cdSKees Cook 	__underlying_memcpy(p + p_len, q, copy_len);
391605395cdSKees Cook 	p[actual] = '\0';
392605395cdSKees Cook 
393605395cdSKees Cook 	return wanted;
394605395cdSKees Cook }
395605395cdSKees Cook 
39655c84a5cSKees Cook /* Defined after fortified strlcat() to reuse it. */
39755c84a5cSKees Cook /**
39855c84a5cSKees Cook  * strcat - Append a string to an existing string
39955c84a5cSKees Cook  *
40055c84a5cSKees Cook  * @p: pointer to NUL-terminated string to append to
40155c84a5cSKees Cook  * @q: pointer to NUL-terminated source string to append from
40255c84a5cSKees Cook  *
40355c84a5cSKees Cook  * Do not use this function. While FORTIFY_SOURCE tries to avoid
40455c84a5cSKees Cook  * read and write overflows, this is only possible when the
40555c84a5cSKees Cook  * destination buffer size is known to the compiler. Prefer
40655c84a5cSKees Cook  * building the string with formatting, via scnprintf() or similar.
40755c84a5cSKees Cook  * At the very least, use strncat().
40855c84a5cSKees Cook  *
40955c84a5cSKees Cook  * Returns @p.
41055c84a5cSKees Cook  *
41155c84a5cSKees Cook  */
41255c84a5cSKees Cook __FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2)
strcat(char * const POS p,const char * q)41355c84a5cSKees Cook char *strcat(char * const POS p, const char *q)
41455c84a5cSKees Cook {
41555c84a5cSKees Cook 	const size_t p_size = __member_size(p);
4163d965b33SKees Cook 	const size_t wanted = strlcat(p, q, p_size);
41755c84a5cSKees Cook 
4183d965b33SKees Cook 	if (p_size <= wanted)
4193d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strcat, FORTIFY_WRITE, p_size, wanted + 1, p);
42055c84a5cSKees Cook 	return p;
42155c84a5cSKees Cook }
42255c84a5cSKees Cook 
42303699f27SKees Cook /**
42403699f27SKees Cook  * strncat - Append a string to an existing string
42503699f27SKees Cook  *
42603699f27SKees Cook  * @p: pointer to NUL-terminated string to append to
42703699f27SKees Cook  * @q: pointer to source string to append from
42803699f27SKees Cook  * @count: Maximum bytes to read from @q
42903699f27SKees Cook  *
43003699f27SKees Cook  * Appends at most @count bytes from @q (stopping at the first
43103699f27SKees Cook  * NUL byte) after the NUL-terminated string at @p. @p will be
43203699f27SKees Cook  * NUL-terminated.
43303699f27SKees Cook  *
43403699f27SKees Cook  * Do not use this function. While FORTIFY_SOURCE tries to avoid
43503699f27SKees Cook  * read and write overflows, this is only possible when the sizes
43603699f27SKees Cook  * of @p and @q are known to the compiler. Prefer building the
43703699f27SKees Cook  * string with formatting, via scnprintf() or similar.
43803699f27SKees Cook  *
43903699f27SKees Cook  * Returns @p.
44003699f27SKees Cook  *
44103699f27SKees Cook  */
44203699f27SKees Cook /* Defined after fortified strlen() and strnlen() to reuse them. */
44392df138aSKees Cook __FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3)
strncat(char * const POS p,const char * const POS q,__kernel_size_t count)444281d0c96SKees Cook char *strncat(char * const POS p, const char * const POS q, __kernel_size_t count)
445a28a6e86SFrancis Laniel {
44621a2c74bSKees Cook 	const size_t p_size = __member_size(p);
44721a2c74bSKees Cook 	const size_t q_size = __member_size(q);
4483d965b33SKees Cook 	size_t p_len, copy_len, total;
449a28a6e86SFrancis Laniel 
450311fb40aSKees Cook 	if (p_size == SIZE_MAX && q_size == SIZE_MAX)
451a28a6e86SFrancis Laniel 		return __underlying_strncat(p, q, count);
452a28a6e86SFrancis Laniel 	p_len = strlen(p);
453a28a6e86SFrancis Laniel 	copy_len = strnlen(q, count);
4543d965b33SKees Cook 	total = p_len + copy_len + 1;
4553d965b33SKees Cook 	if (p_size < total)
4563d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strncat, FORTIFY_WRITE, p_size, total, p);
457a28a6e86SFrancis Laniel 	__underlying_memcpy(p + p_len, q, copy_len);
458a28a6e86SFrancis Laniel 	p[p_len + copy_len] = '\0';
459a28a6e86SFrancis Laniel 	return p;
460a28a6e86SFrancis Laniel }
461a28a6e86SFrancis Laniel 
fortify_memset_chk(__kernel_size_t size,const size_t p_size,const size_t p_size_field)4624ce615e7SKees Cook __FORTIFY_INLINE bool fortify_memset_chk(__kernel_size_t size,
46328e77cc1SKees Cook 					 const size_t p_size,
46428e77cc1SKees Cook 					 const size_t p_size_field)
465a28a6e86SFrancis Laniel {
46628e77cc1SKees Cook 	if (__builtin_constant_p(size)) {
46728e77cc1SKees Cook 		/*
46828e77cc1SKees Cook 		 * Length argument is a constant expression, so we
46928e77cc1SKees Cook 		 * can perform compile-time bounds checking where
470fa35198fSKees Cook 		 * buffer sizes are also known at compile time.
47128e77cc1SKees Cook 		 */
472a28a6e86SFrancis Laniel 
47328e77cc1SKees Cook 		/* Error when size is larger than enclosing struct. */
474fa35198fSKees Cook 		if (__compiletime_lessthan(p_size_field, p_size) &&
475fa35198fSKees Cook 		    __compiletime_lessthan(p_size, size))
476a28a6e86SFrancis Laniel 			__write_overflow();
47728e77cc1SKees Cook 
47828e77cc1SKees Cook 		/* Warn when write size is larger than dest field. */
479fa35198fSKees Cook 		if (__compiletime_lessthan(p_size_field, size))
48028e77cc1SKees Cook 			__write_overflow_field(p_size_field, size);
481a28a6e86SFrancis Laniel 	}
48228e77cc1SKees Cook 	/*
48328e77cc1SKees Cook 	 * At this point, length argument may not be a constant expression,
48428e77cc1SKees Cook 	 * so run-time bounds checking can be done where buffer sizes are
48528e77cc1SKees Cook 	 * known. (This is not an "else" because the above checks may only
48628e77cc1SKees Cook 	 * be compile-time warnings, and we want to still warn for run-time
48728e77cc1SKees Cook 	 * overflows.)
48828e77cc1SKees Cook 	 */
48928e77cc1SKees Cook 
49028e77cc1SKees Cook 	/*
49128e77cc1SKees Cook 	 * Always stop accesses beyond the struct that contains the
49228e77cc1SKees Cook 	 * field, when the buffer's remaining size is known.
493311fb40aSKees Cook 	 * (The SIZE_MAX test is to optimize away checks where the buffer
49428e77cc1SKees Cook 	 * lengths are unknown.)
49528e77cc1SKees Cook 	 */
496311fb40aSKees Cook 	if (p_size != SIZE_MAX && p_size < size)
4973d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_memset, FORTIFY_WRITE, p_size, size, true);
4984ce615e7SKees Cook 	return false;
49928e77cc1SKees Cook }
50028e77cc1SKees Cook 
50128e77cc1SKees Cook #define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({	\
50228e77cc1SKees Cook 	size_t __fortify_size = (size_t)(size);				\
50328e77cc1SKees Cook 	fortify_memset_chk(__fortify_size, p_size, p_size_field),	\
50428e77cc1SKees Cook 	__underlying_memset(p, c, __fortify_size);			\
50528e77cc1SKees Cook })
50628e77cc1SKees Cook 
50728e77cc1SKees Cook /*
5089f7d69c5SKees Cook  * __struct_size() vs __member_size() must be captured here to avoid
5099f7d69c5SKees Cook  * evaluating argument side-effects further into the macro layers.
51028e77cc1SKees Cook  */
511ff901d80SAlexander Potapenko #ifndef CONFIG_KMSAN
51228e77cc1SKees Cook #define memset(p, c, s) __fortify_memset_chk(p, c, s,			\
5139f7d69c5SKees Cook 		__struct_size(p), __member_size(p))
514ff901d80SAlexander Potapenko #endif
515a28a6e86SFrancis Laniel 
516f68f2ff9SKees Cook /*
517f68f2ff9SKees Cook  * To make sure the compiler can enforce protection against buffer overflows,
518f68f2ff9SKees Cook  * memcpy(), memmove(), and memset() must not be used beyond individual
519f68f2ff9SKees Cook  * struct members. If you need to copy across multiple members, please use
520f68f2ff9SKees Cook  * struct_group() to create a named mirror of an anonymous struct union.
521f68f2ff9SKees Cook  * (e.g. see struct sk_buff.) Read overflow checking is currently only
522f68f2ff9SKees Cook  * done when a write overflow is also present, or when building with W=1.
523f68f2ff9SKees Cook  *
524f68f2ff9SKees Cook  * Mitigation coverage matrix
525f68f2ff9SKees Cook  *					Bounds checking at:
526f68f2ff9SKees Cook  *					+-------+-------+-------+-------+
527f68f2ff9SKees Cook  *					| Compile time  |   Run time    |
528f68f2ff9SKees Cook  * memcpy() argument sizes:		| write | read  | write | read  |
529f68f2ff9SKees Cook  *        dest     source   length      +-------+-------+-------+-------+
530f68f2ff9SKees Cook  * memcpy(known,   known,   constant)	|   y   |   y   |  n/a  |  n/a  |
531f68f2ff9SKees Cook  * memcpy(known,   unknown, constant)	|   y   |   n   |  n/a  |   V   |
532f68f2ff9SKees Cook  * memcpy(known,   known,   dynamic)	|   n   |   n   |   B   |   B   |
533f68f2ff9SKees Cook  * memcpy(known,   unknown, dynamic)	|   n   |   n   |   B   |   V   |
534f68f2ff9SKees Cook  * memcpy(unknown, known,   constant)	|   n   |   y   |   V   |  n/a  |
535f68f2ff9SKees Cook  * memcpy(unknown, unknown, constant)	|   n   |   n   |   V   |   V   |
536f68f2ff9SKees Cook  * memcpy(unknown, known,   dynamic)	|   n   |   n   |   V   |   B   |
537f68f2ff9SKees Cook  * memcpy(unknown, unknown, dynamic)	|   n   |   n   |   V   |   V   |
538f68f2ff9SKees Cook  *					+-------+-------+-------+-------+
539f68f2ff9SKees Cook  *
540f68f2ff9SKees Cook  * y = perform deterministic compile-time bounds checking
541f68f2ff9SKees Cook  * n = cannot perform deterministic compile-time bounds checking
542f68f2ff9SKees Cook  * n/a = no run-time bounds checking needed since compile-time deterministic
543f68f2ff9SKees Cook  * B = can perform run-time bounds checking (currently unimplemented)
544f68f2ff9SKees Cook  * V = vulnerable to run-time overflow (will need refactoring to solve)
545f68f2ff9SKees Cook  *
546f68f2ff9SKees Cook  */
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 u8 func)54754d9469bSKees Cook __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
548f68f2ff9SKees Cook 					 const size_t p_size,
549f68f2ff9SKees Cook 					 const size_t q_size,
550f68f2ff9SKees Cook 					 const size_t p_size_field,
551f68f2ff9SKees Cook 					 const size_t q_size_field,
552475ddf1fSKees Cook 					 const u8 func)
553a28a6e86SFrancis Laniel {
554a28a6e86SFrancis Laniel 	if (__builtin_constant_p(size)) {
555f68f2ff9SKees Cook 		/*
556f68f2ff9SKees Cook 		 * Length argument is a constant expression, so we
557f68f2ff9SKees Cook 		 * can perform compile-time bounds checking where
558fa35198fSKees Cook 		 * buffer sizes are also known at compile time.
559f68f2ff9SKees Cook 		 */
560f68f2ff9SKees Cook 
561f68f2ff9SKees Cook 		/* Error when size is larger than enclosing struct. */
562fa35198fSKees Cook 		if (__compiletime_lessthan(p_size_field, p_size) &&
563fa35198fSKees Cook 		    __compiletime_lessthan(p_size, size))
564a28a6e86SFrancis Laniel 			__write_overflow();
565fa35198fSKees Cook 		if (__compiletime_lessthan(q_size_field, q_size) &&
566fa35198fSKees Cook 		    __compiletime_lessthan(q_size, size))
567a28a6e86SFrancis Laniel 			__read_overflow2();
568f68f2ff9SKees Cook 
569f68f2ff9SKees Cook 		/* Warn when write size argument larger than dest field. */
570fa35198fSKees Cook 		if (__compiletime_lessthan(p_size_field, size))
571f68f2ff9SKees Cook 			__write_overflow_field(p_size_field, size);
572f68f2ff9SKees Cook 		/*
573f68f2ff9SKees Cook 		 * Warn for source field over-read when building with W=1
574f68f2ff9SKees Cook 		 * or when an over-write happened, so both can be fixed at
575f68f2ff9SKees Cook 		 * the same time.
576f68f2ff9SKees Cook 		 */
577fa35198fSKees Cook 		if ((IS_ENABLED(KBUILD_EXTRA_WARN1) ||
578fa35198fSKees Cook 		     __compiletime_lessthan(p_size_field, size)) &&
579fa35198fSKees Cook 		    __compiletime_lessthan(q_size_field, size))
580f68f2ff9SKees Cook 			__read_overflow2_field(q_size_field, size);
581a28a6e86SFrancis Laniel 	}
582f68f2ff9SKees Cook 	/*
583f68f2ff9SKees Cook 	 * At this point, length argument may not be a constant expression,
584f68f2ff9SKees Cook 	 * so run-time bounds checking can be done where buffer sizes are
585f68f2ff9SKees Cook 	 * known. (This is not an "else" because the above checks may only
586f68f2ff9SKees Cook 	 * be compile-time warnings, and we want to still warn for run-time
587f68f2ff9SKees Cook 	 * overflows.)
588f68f2ff9SKees Cook 	 */
589f68f2ff9SKees Cook 
590f68f2ff9SKees Cook 	/*
591f68f2ff9SKees Cook 	 * Always stop accesses beyond the struct that contains the
592f68f2ff9SKees Cook 	 * field, when the buffer's remaining size is known.
593311fb40aSKees Cook 	 * (The SIZE_MAX test is to optimize away checks where the buffer
594f68f2ff9SKees Cook 	 * lengths are unknown.)
595f68f2ff9SKees Cook 	 */
596475ddf1fSKees Cook 	if (p_size != SIZE_MAX && p_size < size)
5973d965b33SKees Cook 		fortify_panic(func, FORTIFY_WRITE, p_size, size, true);
598475ddf1fSKees Cook 	else if (q_size != SIZE_MAX && q_size < size)
5993d965b33SKees Cook 		fortify_panic(func, FORTIFY_READ, p_size, size, true);
60054d9469bSKees Cook 
60154d9469bSKees Cook 	/*
60254d9469bSKees Cook 	 * Warn when writing beyond destination field size.
60354d9469bSKees Cook 	 *
6042003e483SKees Cook 	 * Note the implementation of __builtin_*object_size() behaves
60554d9469bSKees Cook 	 * like sizeof() when not directly referencing a flexible
60654d9469bSKees Cook 	 * array member, which means there will be many bounds checks
60754d9469bSKees Cook 	 * that will appear at run-time, without a way for them to be
60854d9469bSKees Cook 	 * detected at compile-time (as can be done when the destination
60954d9469bSKees Cook 	 * is specifically the flexible array member).
61054d9469bSKees Cook 	 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832
61154d9469bSKees Cook 	 */
6122003e483SKees Cook 	if (p_size_field != SIZE_MAX &&
61354d9469bSKees Cook 	    p_size != p_size_field && p_size_field < size)
61454d9469bSKees Cook 		return true;
61554d9469bSKees Cook 
61654d9469bSKees Cook 	return false;
617a28a6e86SFrancis Laniel }
618a28a6e86SFrancis Laniel 
619*239d8732SKees Cook /*
620*239d8732SKees Cook  * To work around what seems to be an optimizer bug, the macro arguments
621*239d8732SKees Cook  * need to have const copies or the values end up changed by the time they
622*239d8732SKees Cook  * reach fortify_warn_once(). See commit 6f7630b1b5bc ("fortify: Capture
623*239d8732SKees Cook  * __bos() results in const temp vars") for more details.
624*239d8732SKees Cook  */
625f68f2ff9SKees Cook #define __fortify_memcpy_chk(p, q, size, p_size, q_size,		\
626f68f2ff9SKees Cook 			     p_size_field, q_size_field, op) ({		\
6276f7630b1SKees Cook 	const size_t __fortify_size = (size_t)(size);			\
6286f7630b1SKees Cook 	const size_t __p_size = (p_size);				\
6296f7630b1SKees Cook 	const size_t __q_size = (q_size);				\
6306f7630b1SKees Cook 	const size_t __p_size_field = (p_size_field);			\
6316f7630b1SKees Cook 	const size_t __q_size_field = (q_size_field);			\
632*239d8732SKees Cook 	/* Keep a mutable version of the size for the final copy. */	\
633*239d8732SKees Cook 	size_t __copy_size = __fortify_size;				\
63426f812baSKees Cook 	fortify_warn_once(fortify_memcpy_chk(__fortify_size, __p_size,	\
6356f7630b1SKees Cook 				     __q_size, __p_size_field,		\
636475ddf1fSKees Cook 				     __q_size_field, FORTIFY_FUNC_ ##op), \
63754d9469bSKees Cook 		  #op ": detected field-spanning write (size %zu) of single %s (size %zu)\n", \
63854d9469bSKees Cook 		  __fortify_size,					\
6395097a69dSAlexey Dobriyan 		  "field \"" #p "\" at " FILE_LINE,			\
6406f7630b1SKees Cook 		  __p_size_field);					\
641*239d8732SKees Cook 	/* Hide only the run-time size from value range tracking to */	\
642*239d8732SKees Cook 	/* silence compile-time false positive bounds warnings. */	\
643*239d8732SKees Cook 	if (!__builtin_constant_p(__copy_size))				\
644*239d8732SKees Cook 		OPTIMIZER_HIDE_VAR(__copy_size);			\
645*239d8732SKees Cook 	__underlying_##op(p, q, __copy_size);				\
646f68f2ff9SKees Cook })
647f68f2ff9SKees Cook 
648f68f2ff9SKees Cook /*
64954d9469bSKees Cook  * Notes about compile-time buffer size detection:
65054d9469bSKees Cook  *
65154d9469bSKees Cook  * With these types...
65254d9469bSKees Cook  *
65354d9469bSKees Cook  *	struct middle {
65454d9469bSKees Cook  *		u16 a;
65554d9469bSKees Cook  *		u8 middle_buf[16];
65654d9469bSKees Cook  *		int b;
65754d9469bSKees Cook  *	};
65854d9469bSKees Cook  *	struct end {
65954d9469bSKees Cook  *		u16 a;
66054d9469bSKees Cook  *		u8 end_buf[16];
66154d9469bSKees Cook  *	};
66254d9469bSKees Cook  *	struct flex {
66354d9469bSKees Cook  *		int a;
66454d9469bSKees Cook  *		u8 flex_buf[];
66554d9469bSKees Cook  *	};
66654d9469bSKees Cook  *
66754d9469bSKees Cook  *	void func(TYPE *ptr) { ... }
66854d9469bSKees Cook  *
66954d9469bSKees Cook  * Cases where destination size cannot be currently detected:
67054d9469bSKees Cook  * - the size of ptr's object (seemingly by design, gcc & clang fail):
67154d9469bSKees Cook  *	__builtin_object_size(ptr, 1) == SIZE_MAX
67254d9469bSKees Cook  * - the size of flexible arrays in ptr's obj (by design, dynamic size):
67354d9469bSKees Cook  *	__builtin_object_size(ptr->flex_buf, 1) == SIZE_MAX
67454d9469bSKees Cook  * - the size of ANY array at the end of ptr's obj (gcc and clang bug):
67554d9469bSKees Cook  *	__builtin_object_size(ptr->end_buf, 1) == SIZE_MAX
67654d9469bSKees Cook  *	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836
67754d9469bSKees Cook  *
67854d9469bSKees Cook  * Cases where destination size is currently detected:
67954d9469bSKees Cook  * - the size of non-array members within ptr's object:
68054d9469bSKees Cook  *	__builtin_object_size(ptr->a, 1) == 2
68154d9469bSKees Cook  * - the size of non-flexible-array in the middle of ptr's obj:
68254d9469bSKees Cook  *	__builtin_object_size(ptr->middle_buf, 1) == 16
68354d9469bSKees Cook  *
68454d9469bSKees Cook  */
68554d9469bSKees Cook 
68654d9469bSKees Cook /*
6879f7d69c5SKees Cook  * __struct_size() vs __member_size() must be captured here to avoid
6889f7d69c5SKees Cook  * evaluating argument side-effects further into the macro layers.
689f68f2ff9SKees Cook  */
690f68f2ff9SKees Cook #define memcpy(p, q, s)  __fortify_memcpy_chk(p, q, s,			\
6919f7d69c5SKees Cook 		__struct_size(p), __struct_size(q),			\
6929f7d69c5SKees Cook 		__member_size(p), __member_size(q),			\
693f68f2ff9SKees Cook 		memcpy)
694938a000eSKees Cook #define memmove(p, q, s)  __fortify_memcpy_chk(p, q, s,			\
6959f7d69c5SKees Cook 		__struct_size(p), __struct_size(q),			\
6969f7d69c5SKees Cook 		__member_size(p), __member_size(q),			\
697938a000eSKees Cook 		memmove)
698a28a6e86SFrancis Laniel 
699a28a6e86SFrancis Laniel extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
memscan(void * const POS0 p,int c,__kernel_size_t size)700281d0c96SKees Cook __FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
701a28a6e86SFrancis Laniel {
70221a2c74bSKees Cook 	const size_t p_size = __struct_size(p);
703a28a6e86SFrancis Laniel 
704fa35198fSKees Cook 	if (__compiletime_lessthan(p_size, size))
705a28a6e86SFrancis Laniel 		__read_overflow();
706a28a6e86SFrancis Laniel 	if (p_size < size)
7073d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_memscan, FORTIFY_READ, p_size, size, NULL);
708a28a6e86SFrancis Laniel 	return __real_memscan(p, c, size);
709a28a6e86SFrancis Laniel }
710a28a6e86SFrancis Laniel 
71192df138aSKees Cook __FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3)
memcmp(const void * const POS0 p,const void * const POS0 q,__kernel_size_t size)712281d0c96SKees Cook int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t size)
713a28a6e86SFrancis Laniel {
71421a2c74bSKees Cook 	const size_t p_size = __struct_size(p);
71521a2c74bSKees Cook 	const size_t q_size = __struct_size(q);
716a28a6e86SFrancis Laniel 
717a28a6e86SFrancis Laniel 	if (__builtin_constant_p(size)) {
718fa35198fSKees Cook 		if (__compiletime_lessthan(p_size, size))
719a28a6e86SFrancis Laniel 			__read_overflow();
720fa35198fSKees Cook 		if (__compiletime_lessthan(q_size, size))
721a28a6e86SFrancis Laniel 			__read_overflow2();
722a28a6e86SFrancis Laniel 	}
7233d965b33SKees Cook 	if (p_size < size)
7243d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_memcmp, FORTIFY_READ, p_size, size, INT_MIN);
7253d965b33SKees Cook 	else if (q_size < size)
7263d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_memcmp, FORTIFY_READ, q_size, size, INT_MIN);
727a28a6e86SFrancis Laniel 	return __underlying_memcmp(p, q, size);
728a28a6e86SFrancis Laniel }
729a28a6e86SFrancis Laniel 
73092df138aSKees Cook __FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3)
memchr(const void * const POS0 p,int c,__kernel_size_t size)731281d0c96SKees Cook void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
732a28a6e86SFrancis Laniel {
73321a2c74bSKees Cook 	const size_t p_size = __struct_size(p);
734a28a6e86SFrancis Laniel 
735fa35198fSKees Cook 	if (__compiletime_lessthan(p_size, size))
736a28a6e86SFrancis Laniel 		__read_overflow();
737a28a6e86SFrancis Laniel 	if (p_size < size)
7383d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_memchr, FORTIFY_READ, p_size, size, NULL);
739a28a6e86SFrancis Laniel 	return __underlying_memchr(p, c, size);
740a28a6e86SFrancis Laniel }
741a28a6e86SFrancis Laniel 
742a28a6e86SFrancis Laniel 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)743281d0c96SKees Cook __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
744a28a6e86SFrancis Laniel {
74521a2c74bSKees Cook 	const size_t p_size = __struct_size(p);
746a28a6e86SFrancis Laniel 
747fa35198fSKees Cook 	if (__compiletime_lessthan(p_size, size))
748a28a6e86SFrancis Laniel 		__read_overflow();
749a28a6e86SFrancis Laniel 	if (p_size < size)
7503d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_memchr_inv, FORTIFY_READ, p_size, size, NULL);
751a28a6e86SFrancis Laniel 	return __real_memchr_inv(p, c, size);
752a28a6e86SFrancis Laniel }
753a28a6e86SFrancis Laniel 
7547bd230a2SSuren Baghdasaryan extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup_noprof)
7559e4a6177SKees Cook 								    __realloc_size(2);
kmemdup_noprof(const void * const POS0 p,size_t size,gfp_t gfp)7567bd230a2SSuren Baghdasaryan __FORTIFY_INLINE void *kmemdup_noprof(const void * const POS0 p, size_t size, gfp_t gfp)
757a28a6e86SFrancis Laniel {
75821a2c74bSKees Cook 	const size_t p_size = __struct_size(p);
759a28a6e86SFrancis Laniel 
760fa35198fSKees Cook 	if (__compiletime_lessthan(p_size, size))
761a28a6e86SFrancis Laniel 		__read_overflow();
762a28a6e86SFrancis Laniel 	if (p_size < size)
76374df2245SKees Cook 		fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size,
76474df2245SKees Cook 			      __real_kmemdup(p, 0, gfp));
765a28a6e86SFrancis Laniel 	return __real_kmemdup(p, size, gfp);
766a28a6e86SFrancis Laniel }
7677bd230a2SSuren Baghdasaryan #define kmemdup(...)	alloc_hooks(kmemdup_noprof(__VA_ARGS__))
768a28a6e86SFrancis Laniel 
76903699f27SKees Cook /**
77003699f27SKees Cook  * strcpy - Copy a string into another string buffer
77103699f27SKees Cook  *
77203699f27SKees Cook  * @p: pointer to destination of copy
77303699f27SKees Cook  * @q: pointer to NUL-terminated source string to copy
77403699f27SKees Cook  *
77503699f27SKees Cook  * Do not use this function. While FORTIFY_SOURCE tries to avoid
77603699f27SKees Cook  * overflows, this is only possible when the sizes of @q and @p are
77703699f27SKees Cook  * known to the compiler. Prefer strscpy(), though note its different
77803699f27SKees Cook  * return values for detecting truncation.
77903699f27SKees Cook  *
78003699f27SKees Cook  * Returns @p.
78103699f27SKees Cook  *
78203699f27SKees Cook  */
783f68f2ff9SKees Cook /* Defined after fortified strlen to reuse it. */
78492df138aSKees Cook __FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2)
strcpy(char * const POS p,const char * const POS q)785281d0c96SKees Cook char *strcpy(char * const POS p, const char * const POS q)
786a28a6e86SFrancis Laniel {
78721a2c74bSKees Cook 	const size_t p_size = __member_size(p);
78821a2c74bSKees Cook 	const size_t q_size = __member_size(q);
789a28a6e86SFrancis Laniel 	size_t size;
790a28a6e86SFrancis Laniel 
791f68f2ff9SKees Cook 	/* If neither buffer size is known, immediately give up. */
792fa35198fSKees Cook 	if (__builtin_constant_p(p_size) &&
793fa35198fSKees Cook 	    __builtin_constant_p(q_size) &&
794fa35198fSKees Cook 	    p_size == SIZE_MAX && q_size == SIZE_MAX)
795a28a6e86SFrancis Laniel 		return __underlying_strcpy(p, q);
796a28a6e86SFrancis Laniel 	size = strlen(q) + 1;
797072af0c6SKees Cook 	/* Compile-time check for const size overflow. */
798fa35198fSKees Cook 	if (__compiletime_lessthan(p_size, size))
799072af0c6SKees Cook 		__write_overflow();
800072af0c6SKees Cook 	/* Run-time check for dynamic size overflow. */
801a28a6e86SFrancis Laniel 	if (p_size < size)
8023d965b33SKees Cook 		fortify_panic(FORTIFY_FUNC_strcpy, FORTIFY_WRITE, p_size, size, p);
803f68f2ff9SKees Cook 	__underlying_memcpy(p, q, size);
804a28a6e86SFrancis Laniel 	return p;
805a28a6e86SFrancis Laniel }
806a28a6e86SFrancis Laniel 
807a28a6e86SFrancis Laniel /* Don't use these outside the FORITFY_SOURCE implementation */
808a28a6e86SFrancis Laniel #undef __underlying_memchr
809a28a6e86SFrancis Laniel #undef __underlying_memcmp
810a28a6e86SFrancis Laniel #undef __underlying_strcat
811a28a6e86SFrancis Laniel #undef __underlying_strcpy
812a28a6e86SFrancis Laniel #undef __underlying_strlen
813a28a6e86SFrancis Laniel #undef __underlying_strncat
814a28a6e86SFrancis Laniel #undef __underlying_strncpy
815a28a6e86SFrancis Laniel 
816281d0c96SKees Cook #undef POS
817281d0c96SKees Cook #undef POS0
818281d0c96SKees Cook 
819a28a6e86SFrancis Laniel #endif /* _LINUX_FORTIFY_STRING_H_ */
820