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