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