xref: /linux-6.15/include/linux/fortify-string.h (revision f68f2ff9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FORTIFY_STRING_H_
3 #define _LINUX_FORTIFY_STRING_H_
4 
5 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
6 #define __RENAME(x) __asm__(#x)
7 
8 void fortify_panic(const char *name) __noreturn __cold;
9 void __read_overflow(void) __compiletime_error("detected read beyond size of object (1st parameter)");
10 void __read_overflow2(void) __compiletime_error("detected read beyond size of object (2nd parameter)");
11 void __read_overflow2_field(size_t avail, size_t wanted) __compiletime_warning("detected read beyond size of field (2nd parameter); maybe use struct_group()?");
12 void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)");
13 void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning("detected write beyond size of field (1st parameter); maybe use struct_group()?");
14 
15 #define __compiletime_strlen(p)					\
16 ({								\
17 	unsigned char *__p = (unsigned char *)(p);		\
18 	size_t __ret = (size_t)-1;				\
19 	size_t __p_size = __builtin_object_size(p, 1);		\
20 	if (__p_size != (size_t)-1) {				\
21 		size_t __p_len = __p_size - 1;			\
22 		if (__builtin_constant_p(__p[__p_len]) &&	\
23 		    __p[__p_len] == '\0')			\
24 			__ret = __builtin_strlen(__p);		\
25 	}							\
26 	__ret;							\
27 })
28 
29 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
30 extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
31 extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
32 extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
33 extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
34 extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
35 extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
36 extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
37 extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
38 extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
39 extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
40 #else
41 #define __underlying_memchr	__builtin_memchr
42 #define __underlying_memcmp	__builtin_memcmp
43 #define __underlying_memcpy	__builtin_memcpy
44 #define __underlying_memmove	__builtin_memmove
45 #define __underlying_memset	__builtin_memset
46 #define __underlying_strcat	__builtin_strcat
47 #define __underlying_strcpy	__builtin_strcpy
48 #define __underlying_strlen	__builtin_strlen
49 #define __underlying_strncat	__builtin_strncat
50 #define __underlying_strncpy	__builtin_strncpy
51 #endif
52 
53 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
54 {
55 	size_t p_size = __builtin_object_size(p, 1);
56 
57 	if (__builtin_constant_p(size) && p_size < size)
58 		__write_overflow();
59 	if (p_size < size)
60 		fortify_panic(__func__);
61 	return __underlying_strncpy(p, q, size);
62 }
63 
64 __FORTIFY_INLINE char *strcat(char *p, const char *q)
65 {
66 	size_t p_size = __builtin_object_size(p, 1);
67 
68 	if (p_size == (size_t)-1)
69 		return __underlying_strcat(p, q);
70 	if (strlcat(p, q, p_size) >= p_size)
71 		fortify_panic(__func__);
72 	return p;
73 }
74 
75 extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
76 __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
77 {
78 	size_t p_size = __builtin_object_size(p, 1);
79 	size_t p_len = __compiletime_strlen(p);
80 	size_t ret;
81 
82 	/* We can take compile-time actions when maxlen is const. */
83 	if (__builtin_constant_p(maxlen) && p_len != (size_t)-1) {
84 		/* If p is const, we can use its compile-time-known len. */
85 		if (maxlen >= p_size)
86 			return p_len;
87 	}
88 
89 	/* Do not check characters beyond the end of p. */
90 	ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
91 	if (p_size <= ret && maxlen != ret)
92 		fortify_panic(__func__);
93 	return ret;
94 }
95 
96 /* defined after fortified strnlen to reuse it. */
97 __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
98 {
99 	__kernel_size_t ret;
100 	size_t p_size = __builtin_object_size(p, 1);
101 
102 	/* Give up if we don't know how large p is. */
103 	if (p_size == (size_t)-1)
104 		return __underlying_strlen(p);
105 	ret = strnlen(p, p_size);
106 	if (p_size <= ret)
107 		fortify_panic(__func__);
108 	return ret;
109 }
110 
111 /* defined after fortified strlen to reuse it */
112 extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
113 __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
114 {
115 	size_t p_size = __builtin_object_size(p, 1);
116 	size_t q_size = __builtin_object_size(q, 1);
117 	size_t q_len;	/* Full count of source string length. */
118 	size_t len;	/* Count of characters going into destination. */
119 
120 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
121 		return __real_strlcpy(p, q, size);
122 	q_len = strlen(q);
123 	len = (q_len >= size) ? size - 1 : q_len;
124 	if (__builtin_constant_p(size) && __builtin_constant_p(q_len) && size) {
125 		/* Write size is always larger than destination. */
126 		if (len >= p_size)
127 			__write_overflow();
128 	}
129 	if (size) {
130 		if (len >= p_size)
131 			fortify_panic(__func__);
132 		__underlying_memcpy(p, q, len);
133 		p[len] = '\0';
134 	}
135 	return q_len;
136 }
137 
138 /* defined after fortified strnlen to reuse it */
139 extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy);
140 __FORTIFY_INLINE ssize_t strscpy(char *p, const char *q, size_t size)
141 {
142 	size_t len;
143 	/* Use string size rather than possible enclosing struct size. */
144 	size_t p_size = __builtin_object_size(p, 1);
145 	size_t q_size = __builtin_object_size(q, 1);
146 
147 	/* If we cannot get size of p and q default to call strscpy. */
148 	if (p_size == (size_t) -1 && q_size == (size_t) -1)
149 		return __real_strscpy(p, q, size);
150 
151 	/*
152 	 * If size can be known at compile time and is greater than
153 	 * p_size, generate a compile time write overflow error.
154 	 */
155 	if (__builtin_constant_p(size) && size > p_size)
156 		__write_overflow();
157 
158 	/*
159 	 * This call protects from read overflow, because len will default to q
160 	 * length if it smaller than size.
161 	 */
162 	len = strnlen(q, size);
163 	/*
164 	 * If len equals size, we will copy only size bytes which leads to
165 	 * -E2BIG being returned.
166 	 * Otherwise we will copy len + 1 because of the final '\O'.
167 	 */
168 	len = len == size ? size : len + 1;
169 
170 	/*
171 	 * Generate a runtime write overflow error if len is greater than
172 	 * p_size.
173 	 */
174 	if (len > p_size)
175 		fortify_panic(__func__);
176 
177 	/*
178 	 * We can now safely call vanilla strscpy because we are protected from:
179 	 * 1. Read overflow thanks to call to strnlen().
180 	 * 2. Write overflow thanks to above ifs.
181 	 */
182 	return __real_strscpy(p, q, len);
183 }
184 
185 /* defined after fortified strlen and strnlen to reuse them */
186 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
187 {
188 	size_t p_len, copy_len;
189 	size_t p_size = __builtin_object_size(p, 1);
190 	size_t q_size = __builtin_object_size(q, 1);
191 
192 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
193 		return __underlying_strncat(p, q, count);
194 	p_len = strlen(p);
195 	copy_len = strnlen(q, count);
196 	if (p_size < p_len + copy_len + 1)
197 		fortify_panic(__func__);
198 	__underlying_memcpy(p + p_len, q, copy_len);
199 	p[p_len + copy_len] = '\0';
200 	return p;
201 }
202 
203 __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
204 {
205 	size_t p_size = __builtin_object_size(p, 0);
206 
207 	if (__builtin_constant_p(size) && p_size < size)
208 		__write_overflow();
209 	if (p_size < size)
210 		fortify_panic(__func__);
211 	return __underlying_memset(p, c, size);
212 }
213 
214 /*
215  * To make sure the compiler can enforce protection against buffer overflows,
216  * memcpy(), memmove(), and memset() must not be used beyond individual
217  * struct members. If you need to copy across multiple members, please use
218  * struct_group() to create a named mirror of an anonymous struct union.
219  * (e.g. see struct sk_buff.) Read overflow checking is currently only
220  * done when a write overflow is also present, or when building with W=1.
221  *
222  * Mitigation coverage matrix
223  *					Bounds checking at:
224  *					+-------+-------+-------+-------+
225  *					| Compile time  |   Run time    |
226  * memcpy() argument sizes:		| write | read  | write | read  |
227  *        dest     source   length      +-------+-------+-------+-------+
228  * memcpy(known,   known,   constant)	|   y   |   y   |  n/a  |  n/a  |
229  * memcpy(known,   unknown, constant)	|   y   |   n   |  n/a  |   V   |
230  * memcpy(known,   known,   dynamic)	|   n   |   n   |   B   |   B   |
231  * memcpy(known,   unknown, dynamic)	|   n   |   n   |   B   |   V   |
232  * memcpy(unknown, known,   constant)	|   n   |   y   |   V   |  n/a  |
233  * memcpy(unknown, unknown, constant)	|   n   |   n   |   V   |   V   |
234  * memcpy(unknown, known,   dynamic)	|   n   |   n   |   V   |   B   |
235  * memcpy(unknown, unknown, dynamic)	|   n   |   n   |   V   |   V   |
236  *					+-------+-------+-------+-------+
237  *
238  * y = perform deterministic compile-time bounds checking
239  * n = cannot perform deterministic compile-time bounds checking
240  * n/a = no run-time bounds checking needed since compile-time deterministic
241  * B = can perform run-time bounds checking (currently unimplemented)
242  * V = vulnerable to run-time overflow (will need refactoring to solve)
243  *
244  */
245 __FORTIFY_INLINE void fortify_memcpy_chk(__kernel_size_t size,
246 					 const size_t p_size,
247 					 const size_t q_size,
248 					 const size_t p_size_field,
249 					 const size_t q_size_field,
250 					 const char *func)
251 {
252 	if (__builtin_constant_p(size)) {
253 		/*
254 		 * Length argument is a constant expression, so we
255 		 * can perform compile-time bounds checking where
256 		 * buffer sizes are known.
257 		 */
258 
259 		/* Error when size is larger than enclosing struct. */
260 		if (p_size > p_size_field && p_size < size)
261 			__write_overflow();
262 		if (q_size > q_size_field && q_size < size)
263 			__read_overflow2();
264 
265 		/* Warn when write size argument larger than dest field. */
266 		if (p_size_field < size)
267 			__write_overflow_field(p_size_field, size);
268 		/*
269 		 * Warn for source field over-read when building with W=1
270 		 * or when an over-write happened, so both can be fixed at
271 		 * the same time.
272 		 */
273 		if ((IS_ENABLED(KBUILD_EXTRA_WARN1) || p_size_field < size) &&
274 		    q_size_field < size)
275 			__read_overflow2_field(q_size_field, size);
276 	}
277 	/*
278 	 * At this point, length argument may not be a constant expression,
279 	 * so run-time bounds checking can be done where buffer sizes are
280 	 * known. (This is not an "else" because the above checks may only
281 	 * be compile-time warnings, and we want to still warn for run-time
282 	 * overflows.)
283 	 */
284 
285 	/*
286 	 * Always stop accesses beyond the struct that contains the
287 	 * field, when the buffer's remaining size is known.
288 	 * (The -1 test is to optimize away checks where the buffer
289 	 * lengths are unknown.)
290 	 */
291 	if ((p_size != (size_t)(-1) && p_size < size) ||
292 	    (q_size != (size_t)(-1) && q_size < size))
293 		fortify_panic(func);
294 }
295 
296 #define __fortify_memcpy_chk(p, q, size, p_size, q_size,		\
297 			     p_size_field, q_size_field, op) ({		\
298 	size_t __fortify_size = (size_t)(size);				\
299 	fortify_memcpy_chk(__fortify_size, p_size, q_size,		\
300 			   p_size_field, q_size_field, #op);		\
301 	__underlying_##op(p, q, __fortify_size);			\
302 })
303 
304 /*
305  * __builtin_object_size() must be captured here to avoid evaluating argument
306  * side-effects further into the macro layers.
307  */
308 #define memcpy(p, q, s)  __fortify_memcpy_chk(p, q, s,			\
309 		__builtin_object_size(p, 0), __builtin_object_size(q, 0), \
310 		__builtin_object_size(p, 1), __builtin_object_size(q, 1), \
311 		memcpy)
312 
313 __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
314 {
315 	size_t p_size = __builtin_object_size(p, 0);
316 	size_t q_size = __builtin_object_size(q, 0);
317 
318 	if (__builtin_constant_p(size)) {
319 		if (p_size < size)
320 			__write_overflow();
321 		if (q_size < size)
322 			__read_overflow2();
323 	}
324 	if (p_size < size || q_size < size)
325 		fortify_panic(__func__);
326 	return __underlying_memmove(p, q, size);
327 }
328 
329 extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
330 __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
331 {
332 	size_t p_size = __builtin_object_size(p, 0);
333 
334 	if (__builtin_constant_p(size) && p_size < size)
335 		__read_overflow();
336 	if (p_size < size)
337 		fortify_panic(__func__);
338 	return __real_memscan(p, c, size);
339 }
340 
341 __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
342 {
343 	size_t p_size = __builtin_object_size(p, 0);
344 	size_t q_size = __builtin_object_size(q, 0);
345 
346 	if (__builtin_constant_p(size)) {
347 		if (p_size < size)
348 			__read_overflow();
349 		if (q_size < size)
350 			__read_overflow2();
351 	}
352 	if (p_size < size || q_size < size)
353 		fortify_panic(__func__);
354 	return __underlying_memcmp(p, q, size);
355 }
356 
357 __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
358 {
359 	size_t p_size = __builtin_object_size(p, 0);
360 
361 	if (__builtin_constant_p(size) && p_size < size)
362 		__read_overflow();
363 	if (p_size < size)
364 		fortify_panic(__func__);
365 	return __underlying_memchr(p, c, size);
366 }
367 
368 void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
369 __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
370 {
371 	size_t p_size = __builtin_object_size(p, 0);
372 
373 	if (__builtin_constant_p(size) && p_size < size)
374 		__read_overflow();
375 	if (p_size < size)
376 		fortify_panic(__func__);
377 	return __real_memchr_inv(p, c, size);
378 }
379 
380 extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
381 __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
382 {
383 	size_t p_size = __builtin_object_size(p, 0);
384 
385 	if (__builtin_constant_p(size) && p_size < size)
386 		__read_overflow();
387 	if (p_size < size)
388 		fortify_panic(__func__);
389 	return __real_kmemdup(p, size, gfp);
390 }
391 
392 /* Defined after fortified strlen to reuse it. */
393 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
394 {
395 	size_t p_size = __builtin_object_size(p, 1);
396 	size_t q_size = __builtin_object_size(q, 1);
397 	size_t size;
398 
399 	/* If neither buffer size is known, immediately give up. */
400 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
401 		return __underlying_strcpy(p, q);
402 	size = strlen(q) + 1;
403 	/* Compile-time check for const size overflow. */
404 	if (__builtin_constant_p(size) && p_size < size)
405 		__write_overflow();
406 	/* Run-time check for dynamic size overflow. */
407 	if (p_size < size)
408 		fortify_panic(__func__);
409 	__underlying_memcpy(p, q, size);
410 	return p;
411 }
412 
413 /* Don't use these outside the FORITFY_SOURCE implementation */
414 #undef __underlying_memchr
415 #undef __underlying_memcmp
416 #undef __underlying_memmove
417 #undef __underlying_memset
418 #undef __underlying_strcat
419 #undef __underlying_strcpy
420 #undef __underlying_strlen
421 #undef __underlying_strncat
422 #undef __underlying_strncpy
423 
424 #endif /* _LINUX_FORTIFY_STRING_H_ */
425