xref: /linux-6.15/include/linux/fortify-string.h (revision 28e77cc1)
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 fortify_memset_chk(__kernel_size_t size,
204 					 const size_t p_size,
205 					 const size_t p_size_field)
206 {
207 	if (__builtin_constant_p(size)) {
208 		/*
209 		 * Length argument is a constant expression, so we
210 		 * can perform compile-time bounds checking where
211 		 * buffer sizes are known.
212 		 */
213 
214 		/* Error when size is larger than enclosing struct. */
215 		if (p_size > p_size_field && p_size < size)
216 			__write_overflow();
217 
218 		/* Warn when write size is larger than dest field. */
219 		if (p_size_field < size)
220 			__write_overflow_field(p_size_field, size);
221 	}
222 	/*
223 	 * At this point, length argument may not be a constant expression,
224 	 * so run-time bounds checking can be done where buffer sizes are
225 	 * known. (This is not an "else" because the above checks may only
226 	 * be compile-time warnings, and we want to still warn for run-time
227 	 * overflows.)
228 	 */
229 
230 	/*
231 	 * Always stop accesses beyond the struct that contains the
232 	 * field, when the buffer's remaining size is known.
233 	 * (The -1 test is to optimize away checks where the buffer
234 	 * lengths are unknown.)
235 	 */
236 	if (p_size != (size_t)(-1) && p_size < size)
237 		fortify_panic("memset");
238 }
239 
240 #define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({	\
241 	size_t __fortify_size = (size_t)(size);				\
242 	fortify_memset_chk(__fortify_size, p_size, p_size_field),	\
243 	__underlying_memset(p, c, __fortify_size);			\
244 })
245 
246 /*
247  * __builtin_object_size() must be captured here to avoid evaluating argument
248  * side-effects further into the macro layers.
249  */
250 #define memset(p, c, s) __fortify_memset_chk(p, c, s,			\
251 		__builtin_object_size(p, 0), __builtin_object_size(p, 1))
252 
253 /*
254  * To make sure the compiler can enforce protection against buffer overflows,
255  * memcpy(), memmove(), and memset() must not be used beyond individual
256  * struct members. If you need to copy across multiple members, please use
257  * struct_group() to create a named mirror of an anonymous struct union.
258  * (e.g. see struct sk_buff.) Read overflow checking is currently only
259  * done when a write overflow is also present, or when building with W=1.
260  *
261  * Mitigation coverage matrix
262  *					Bounds checking at:
263  *					+-------+-------+-------+-------+
264  *					| Compile time  |   Run time    |
265  * memcpy() argument sizes:		| write | read  | write | read  |
266  *        dest     source   length      +-------+-------+-------+-------+
267  * memcpy(known,   known,   constant)	|   y   |   y   |  n/a  |  n/a  |
268  * memcpy(known,   unknown, constant)	|   y   |   n   |  n/a  |   V   |
269  * memcpy(known,   known,   dynamic)	|   n   |   n   |   B   |   B   |
270  * memcpy(known,   unknown, dynamic)	|   n   |   n   |   B   |   V   |
271  * memcpy(unknown, known,   constant)	|   n   |   y   |   V   |  n/a  |
272  * memcpy(unknown, unknown, constant)	|   n   |   n   |   V   |   V   |
273  * memcpy(unknown, known,   dynamic)	|   n   |   n   |   V   |   B   |
274  * memcpy(unknown, unknown, dynamic)	|   n   |   n   |   V   |   V   |
275  *					+-------+-------+-------+-------+
276  *
277  * y = perform deterministic compile-time bounds checking
278  * n = cannot perform deterministic compile-time bounds checking
279  * n/a = no run-time bounds checking needed since compile-time deterministic
280  * B = can perform run-time bounds checking (currently unimplemented)
281  * V = vulnerable to run-time overflow (will need refactoring to solve)
282  *
283  */
284 __FORTIFY_INLINE void fortify_memcpy_chk(__kernel_size_t size,
285 					 const size_t p_size,
286 					 const size_t q_size,
287 					 const size_t p_size_field,
288 					 const size_t q_size_field,
289 					 const char *func)
290 {
291 	if (__builtin_constant_p(size)) {
292 		/*
293 		 * Length argument is a constant expression, so we
294 		 * can perform compile-time bounds checking where
295 		 * buffer sizes are known.
296 		 */
297 
298 		/* Error when size is larger than enclosing struct. */
299 		if (p_size > p_size_field && p_size < size)
300 			__write_overflow();
301 		if (q_size > q_size_field && q_size < size)
302 			__read_overflow2();
303 
304 		/* Warn when write size argument larger than dest field. */
305 		if (p_size_field < size)
306 			__write_overflow_field(p_size_field, size);
307 		/*
308 		 * Warn for source field over-read when building with W=1
309 		 * or when an over-write happened, so both can be fixed at
310 		 * the same time.
311 		 */
312 		if ((IS_ENABLED(KBUILD_EXTRA_WARN1) || p_size_field < size) &&
313 		    q_size_field < size)
314 			__read_overflow2_field(q_size_field, size);
315 	}
316 	/*
317 	 * At this point, length argument may not be a constant expression,
318 	 * so run-time bounds checking can be done where buffer sizes are
319 	 * known. (This is not an "else" because the above checks may only
320 	 * be compile-time warnings, and we want to still warn for run-time
321 	 * overflows.)
322 	 */
323 
324 	/*
325 	 * Always stop accesses beyond the struct that contains the
326 	 * field, when the buffer's remaining size is known.
327 	 * (The -1 test is to optimize away checks where the buffer
328 	 * lengths are unknown.)
329 	 */
330 	if ((p_size != (size_t)(-1) && p_size < size) ||
331 	    (q_size != (size_t)(-1) && q_size < size))
332 		fortify_panic(func);
333 }
334 
335 #define __fortify_memcpy_chk(p, q, size, p_size, q_size,		\
336 			     p_size_field, q_size_field, op) ({		\
337 	size_t __fortify_size = (size_t)(size);				\
338 	fortify_memcpy_chk(__fortify_size, p_size, q_size,		\
339 			   p_size_field, q_size_field, #op);		\
340 	__underlying_##op(p, q, __fortify_size);			\
341 })
342 
343 /*
344  * __builtin_object_size() must be captured here to avoid evaluating argument
345  * side-effects further into the macro layers.
346  */
347 #define memcpy(p, q, s)  __fortify_memcpy_chk(p, q, s,			\
348 		__builtin_object_size(p, 0), __builtin_object_size(q, 0), \
349 		__builtin_object_size(p, 1), __builtin_object_size(q, 1), \
350 		memcpy)
351 #define memmove(p, q, s)  __fortify_memcpy_chk(p, q, s,			\
352 		__builtin_object_size(p, 0), __builtin_object_size(q, 0), \
353 		__builtin_object_size(p, 1), __builtin_object_size(q, 1), \
354 		memmove)
355 
356 extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
357 __FORTIFY_INLINE void *memscan(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 __real_memscan(p, c, size);
366 }
367 
368 __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
369 {
370 	size_t p_size = __builtin_object_size(p, 0);
371 	size_t q_size = __builtin_object_size(q, 0);
372 
373 	if (__builtin_constant_p(size)) {
374 		if (p_size < size)
375 			__read_overflow();
376 		if (q_size < size)
377 			__read_overflow2();
378 	}
379 	if (p_size < size || q_size < size)
380 		fortify_panic(__func__);
381 	return __underlying_memcmp(p, q, size);
382 }
383 
384 __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
385 {
386 	size_t p_size = __builtin_object_size(p, 0);
387 
388 	if (__builtin_constant_p(size) && p_size < size)
389 		__read_overflow();
390 	if (p_size < size)
391 		fortify_panic(__func__);
392 	return __underlying_memchr(p, c, size);
393 }
394 
395 void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
396 __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
397 {
398 	size_t p_size = __builtin_object_size(p, 0);
399 
400 	if (__builtin_constant_p(size) && p_size < size)
401 		__read_overflow();
402 	if (p_size < size)
403 		fortify_panic(__func__);
404 	return __real_memchr_inv(p, c, size);
405 }
406 
407 extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
408 __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
409 {
410 	size_t p_size = __builtin_object_size(p, 0);
411 
412 	if (__builtin_constant_p(size) && p_size < size)
413 		__read_overflow();
414 	if (p_size < size)
415 		fortify_panic(__func__);
416 	return __real_kmemdup(p, size, gfp);
417 }
418 
419 /* Defined after fortified strlen to reuse it. */
420 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
421 {
422 	size_t p_size = __builtin_object_size(p, 1);
423 	size_t q_size = __builtin_object_size(q, 1);
424 	size_t size;
425 
426 	/* If neither buffer size is known, immediately give up. */
427 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
428 		return __underlying_strcpy(p, q);
429 	size = strlen(q) + 1;
430 	/* Compile-time check for const size overflow. */
431 	if (__builtin_constant_p(size) && p_size < size)
432 		__write_overflow();
433 	/* Run-time check for dynamic size overflow. */
434 	if (p_size < size)
435 		fortify_panic(__func__);
436 	__underlying_memcpy(p, q, size);
437 	return p;
438 }
439 
440 /* Don't use these outside the FORITFY_SOURCE implementation */
441 #undef __underlying_memchr
442 #undef __underlying_memcmp
443 #undef __underlying_strcat
444 #undef __underlying_strcpy
445 #undef __underlying_strlen
446 #undef __underlying_strncat
447 #undef __underlying_strncpy
448 
449 #endif /* _LINUX_FORTIFY_STRING_H_ */
450