Commit | Line | Data |
---|---|---|
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 | ||
12 | void fortify_panic(const char *name) __noreturn __cold; | |
13 | void __read_overflow(void) __compiletime_error("detected read beyond size of object (1st parameter)"); | |
14 | void __read_overflow2(void) __compiletime_error("detected read beyond size of object (2nd parameter)"); | |
f68f2ff9 | 15 | void __read_overflow2_field(size_t avail, size_t wanted) __compiletime_warning("detected read beyond size of field (2nd parameter); maybe use struct_group()?"); |
c430f600 | 16 | void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)"); |
f68f2ff9 | 17 | void __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 | ({ \ | |
21 | unsigned char *__p = (unsigned 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) |
35 | extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr); | |
36 | extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp); | |
37 | extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy); | |
38 | extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove); | |
39 | extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset); | |
40 | extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat); | |
41 | extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy); | |
42 | extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen); | |
43 | extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat); | |
44 | extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy); | |
45 | #else | |
46 | #define __underlying_memchr __builtin_memchr | |
47 | #define __underlying_memcmp __builtin_memcmp | |
48 | #define __underlying_memcpy __builtin_memcpy | |
49 | #define __underlying_memmove __builtin_memmove | |
50 | #define __underlying_memset __builtin_memset | |
51 | #define __underlying_strcat __builtin_strcat | |
52 | #define __underlying_strcpy __builtin_strcpy | |
53 | #define __underlying_strlen __builtin_strlen | |
54 | #define __underlying_strncat __builtin_strncat | |
55 | #define __underlying_strncpy __builtin_strncpy | |
56 | #endif | |
57 | ||
43213dae KC |
58 | /** |
59 | * unsafe_memcpy - memcpy implementation with no FORTIFY bounds checking | |
60 | * | |
61 | * @dst: Destination memory address to write to | |
62 | * @src: Source memory address to read from | |
63 | * @bytes: How many bytes to write to @dst from @src | |
64 | * @justification: Free-form text or comment describing why the use is needed | |
65 | * | |
66 | * This should be used for corner cases where the compiler cannot do the | |
67 | * right thing, or during transitions between APIs, etc. It should be used | |
68 | * very rarely, and includes a place for justification detailing where bounds | |
69 | * checking has happened, and why existing solutions cannot be employed. | |
70 | */ | |
71 | #define unsafe_memcpy(dst, src, bytes, justification) \ | |
72 | __underlying_memcpy(dst, src, bytes) | |
73 | ||
281d0c96 | 74 | /* |
9f7d69c5 KC |
75 | * Clang's use of __builtin_*object_size() within inlines needs hinting via |
76 | * __pass_*object_size(). The preference is to only ever use type 1 (member | |
281d0c96 KC |
77 | * size, rather than struct size), but there remain some stragglers using |
78 | * type 0 that will be converted in the future. | |
79 | */ | |
9f7d69c5 KC |
80 | #define POS __pass_object_size(1) |
81 | #define POS0 __pass_object_size(0) | |
82 | #define __struct_size(p) __builtin_object_size(p, 0) | |
83 | #define __member_size(p) __builtin_object_size(p, 1) | |
281d0c96 | 84 | |
fa35198f KC |
85 | #define __compiletime_lessthan(bounds, length) ( \ |
86 | __builtin_constant_p((bounds) < (length)) && \ | |
87 | (bounds) < (length) \ | |
88 | ) | |
89 | ||
dfbafa70 KC |
90 | /** |
91 | * strncpy - Copy a string to memory with non-guaranteed NUL padding | |
92 | * | |
93 | * @p: pointer to destination of copy | |
94 | * @q: pointer to NUL-terminated source string to copy | |
95 | * @size: bytes to write at @p | |
96 | * | |
97 | * If strlen(@q) >= @size, the copy of @q will stop after @size bytes, | |
98 | * and @p will NOT be NUL-terminated | |
99 | * | |
100 | * If strlen(@q) < @size, following the copy of @q, trailing NUL bytes | |
101 | * will be written to @p until @size total bytes have been written. | |
102 | * | |
103 | * Do not use this function. While FORTIFY_SOURCE tries to avoid | |
104 | * over-reads of @q, it cannot defend against writing unterminated | |
105 | * results to @p. Using strncpy() remains ambiguous and fragile. | |
106 | * Instead, please choose an alternative, so that the expectation | |
107 | * of @p's contents is unambiguous: | |
108 | * | |
109 | * +--------------------+-----------------+------------+ | |
110 | * | @p needs to be: | padded to @size | not padded | | |
111 | * +====================+=================+============+ | |
112 | * | NUL-terminated | strscpy_pad() | strscpy() | | |
113 | * +--------------------+-----------------+------------+ | |
114 | * | not NUL-terminated | strtomem_pad() | strtomem() | | |
115 | * +--------------------+-----------------+------------+ | |
116 | * | |
117 | * Note strscpy*()'s differing return values for detecting truncation, | |
118 | * and strtomem*()'s expectation that the destination is marked with | |
119 | * __nonstring when it is a character array. | |
120 | * | |
121 | */ | |
92df138a | 122 | __FORTIFY_INLINE __diagnose_as(__builtin_strncpy, 1, 2, 3) |
281d0c96 | 123 | char *strncpy(char * const POS p, const char *q, __kernel_size_t size) |
a28a6e86 | 124 | { |
9f7d69c5 | 125 | size_t p_size = __member_size(p); |
a28a6e86 | 126 | |
fa35198f | 127 | if (__compiletime_lessthan(p_size, size)) |
a28a6e86 FL |
128 | __write_overflow(); |
129 | if (p_size < size) | |
130 | fortify_panic(__func__); | |
131 | return __underlying_strncpy(p, q, size); | |
132 | } | |
133 | ||
92df138a | 134 | __FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2) |
281d0c96 | 135 | char *strcat(char * const POS p, const char *q) |
a28a6e86 | 136 | { |
9f7d69c5 | 137 | size_t p_size = __member_size(p); |
a28a6e86 | 138 | |
311fb40a | 139 | if (p_size == SIZE_MAX) |
a28a6e86 FL |
140 | return __underlying_strcat(p, q); |
141 | if (strlcat(p, q, p_size) >= p_size) | |
142 | fortify_panic(__func__); | |
143 | return p; | |
144 | } | |
145 | ||
369cd216 | 146 | extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen); |
281d0c96 | 147 | __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size_t maxlen) |
369cd216 | 148 | { |
9f7d69c5 | 149 | size_t p_size = __member_size(p); |
3009f891 KC |
150 | size_t p_len = __compiletime_strlen(p); |
151 | size_t ret; | |
152 | ||
153 | /* We can take compile-time actions when maxlen is const. */ | |
311fb40a | 154 | if (__builtin_constant_p(maxlen) && p_len != SIZE_MAX) { |
3009f891 KC |
155 | /* If p is const, we can use its compile-time-known len. */ |
156 | if (maxlen >= p_size) | |
157 | return p_len; | |
158 | } | |
369cd216 | 159 | |
3009f891 KC |
160 | /* Do not check characters beyond the end of p. */ |
161 | ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size); | |
369cd216 KC |
162 | if (p_size <= ret && maxlen != ret) |
163 | fortify_panic(__func__); | |
164 | return ret; | |
165 | } | |
166 | ||
67ebc3ab KC |
167 | /* |
168 | * Defined after fortified strnlen to reuse it. However, it must still be | |
169 | * possible for strlen() to be used on compile-time strings for use in | |
170 | * static initializers (i.e. as a constant expression). | |
171 | */ | |
172 | #define strlen(p) \ | |
173 | __builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \ | |
174 | __builtin_strlen(p), __fortify_strlen(p)) | |
92df138a | 175 | __FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1) |
281d0c96 | 176 | __kernel_size_t __fortify_strlen(const char * const POS p) |
a28a6e86 FL |
177 | { |
178 | __kernel_size_t ret; | |
9f7d69c5 | 179 | size_t p_size = __member_size(p); |
a28a6e86 | 180 | |
3009f891 | 181 | /* Give up if we don't know how large p is. */ |
311fb40a | 182 | if (p_size == SIZE_MAX) |
a28a6e86 FL |
183 | return __underlying_strlen(p); |
184 | ret = strnlen(p, p_size); | |
185 | if (p_size <= ret) | |
186 | fortify_panic(__func__); | |
187 | return ret; | |
188 | } | |
189 | ||
a28a6e86 FL |
190 | /* defined after fortified strlen to reuse it */ |
191 | extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy); | |
281d0c96 | 192 | __FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, size_t size) |
a28a6e86 | 193 | { |
9f7d69c5 KC |
194 | size_t p_size = __member_size(p); |
195 | size_t q_size = __member_size(q); | |
3009f891 KC |
196 | size_t q_len; /* Full count of source string length. */ |
197 | size_t len; /* Count of characters going into destination. */ | |
a28a6e86 | 198 | |
311fb40a | 199 | if (p_size == SIZE_MAX && q_size == SIZE_MAX) |
a28a6e86 | 200 | return __real_strlcpy(p, q, size); |
3009f891 KC |
201 | q_len = strlen(q); |
202 | len = (q_len >= size) ? size - 1 : q_len; | |
203 | if (__builtin_constant_p(size) && __builtin_constant_p(q_len) && size) { | |
204 | /* Write size is always larger than destination. */ | |
205 | if (len >= p_size) | |
a28a6e86 | 206 | __write_overflow(); |
3009f891 KC |
207 | } |
208 | if (size) { | |
a28a6e86 FL |
209 | if (len >= p_size) |
210 | fortify_panic(__func__); | |
211 | __underlying_memcpy(p, q, len); | |
212 | p[len] = '\0'; | |
213 | } | |
3009f891 | 214 | return q_len; |
a28a6e86 FL |
215 | } |
216 | ||
217 | /* defined after fortified strnlen to reuse it */ | |
218 | extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy); | |
281d0c96 | 219 | __FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, size_t size) |
a28a6e86 FL |
220 | { |
221 | size_t len; | |
222 | /* Use string size rather than possible enclosing struct size. */ | |
9f7d69c5 KC |
223 | size_t p_size = __member_size(p); |
224 | size_t q_size = __member_size(q); | |
a28a6e86 FL |
225 | |
226 | /* If we cannot get size of p and q default to call strscpy. */ | |
311fb40a | 227 | if (p_size == SIZE_MAX && q_size == SIZE_MAX) |
a28a6e86 FL |
228 | return __real_strscpy(p, q, size); |
229 | ||
230 | /* | |
231 | * If size can be known at compile time and is greater than | |
232 | * p_size, generate a compile time write overflow error. | |
233 | */ | |
fa35198f | 234 | if (__compiletime_lessthan(p_size, size)) |
a28a6e86 FL |
235 | __write_overflow(); |
236 | ||
237 | /* | |
238 | * This call protects from read overflow, because len will default to q | |
239 | * length if it smaller than size. | |
240 | */ | |
241 | len = strnlen(q, size); | |
242 | /* | |
243 | * If len equals size, we will copy only size bytes which leads to | |
244 | * -E2BIG being returned. | |
245 | * Otherwise we will copy len + 1 because of the final '\O'. | |
246 | */ | |
247 | len = len == size ? size : len + 1; | |
248 | ||
249 | /* | |
250 | * Generate a runtime write overflow error if len is greater than | |
251 | * p_size. | |
252 | */ | |
253 | if (len > p_size) | |
254 | fortify_panic(__func__); | |
255 | ||
256 | /* | |
257 | * We can now safely call vanilla strscpy because we are protected from: | |
258 | * 1. Read overflow thanks to call to strnlen(). | |
259 | * 2. Write overflow thanks to above ifs. | |
260 | */ | |
261 | return __real_strscpy(p, q, len); | |
262 | } | |
263 | ||
264 | /* defined after fortified strlen and strnlen to reuse them */ | |
92df138a | 265 | __FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3) |
281d0c96 | 266 | char *strncat(char * const POS p, const char * const POS q, __kernel_size_t count) |
a28a6e86 FL |
267 | { |
268 | size_t p_len, copy_len; | |
9f7d69c5 KC |
269 | size_t p_size = __member_size(p); |
270 | size_t q_size = __member_size(q); | |
a28a6e86 | 271 | |
311fb40a | 272 | if (p_size == SIZE_MAX && q_size == SIZE_MAX) |
a28a6e86 FL |
273 | return __underlying_strncat(p, q, count); |
274 | p_len = strlen(p); | |
275 | copy_len = strnlen(q, count); | |
276 | if (p_size < p_len + copy_len + 1) | |
277 | fortify_panic(__func__); | |
278 | __underlying_memcpy(p + p_len, q, copy_len); | |
279 | p[p_len + copy_len] = '\0'; | |
280 | return p; | |
281 | } | |
282 | ||
28e77cc1 KC |
283 | __FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size, |
284 | const size_t p_size, | |
285 | const size_t p_size_field) | |
a28a6e86 | 286 | { |
28e77cc1 KC |
287 | if (__builtin_constant_p(size)) { |
288 | /* | |
289 | * Length argument is a constant expression, so we | |
290 | * can perform compile-time bounds checking where | |
fa35198f | 291 | * buffer sizes are also known at compile time. |
28e77cc1 | 292 | */ |
a28a6e86 | 293 | |
28e77cc1 | 294 | /* Error when size is larger than enclosing struct. */ |
fa35198f KC |
295 | if (__compiletime_lessthan(p_size_field, p_size) && |
296 | __compiletime_lessthan(p_size, size)) | |
28e77cc1 KC |
297 | __write_overflow(); |
298 | ||
299 | /* Warn when write size is larger than dest field. */ | |
fa35198f | 300 | if (__compiletime_lessthan(p_size_field, size)) |
28e77cc1 KC |
301 | __write_overflow_field(p_size_field, size); |
302 | } | |
303 | /* | |
304 | * At this point, length argument may not be a constant expression, | |
305 | * so run-time bounds checking can be done where buffer sizes are | |
306 | * known. (This is not an "else" because the above checks may only | |
307 | * be compile-time warnings, and we want to still warn for run-time | |
308 | * overflows.) | |
309 | */ | |
310 | ||
311 | /* | |
312 | * Always stop accesses beyond the struct that contains the | |
313 | * field, when the buffer's remaining size is known. | |
311fb40a | 314 | * (The SIZE_MAX test is to optimize away checks where the buffer |
28e77cc1 KC |
315 | * lengths are unknown.) |
316 | */ | |
311fb40a | 317 | if (p_size != SIZE_MAX && p_size < size) |
28e77cc1 | 318 | fortify_panic("memset"); |
a28a6e86 FL |
319 | } |
320 | ||
28e77cc1 KC |
321 | #define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({ \ |
322 | size_t __fortify_size = (size_t)(size); \ | |
323 | fortify_memset_chk(__fortify_size, p_size, p_size_field), \ | |
324 | __underlying_memset(p, c, __fortify_size); \ | |
325 | }) | |
326 | ||
327 | /* | |
9f7d69c5 KC |
328 | * __struct_size() vs __member_size() must be captured here to avoid |
329 | * evaluating argument side-effects further into the macro layers. | |
28e77cc1 | 330 | */ |
ff901d80 | 331 | #ifndef CONFIG_KMSAN |
28e77cc1 | 332 | #define memset(p, c, s) __fortify_memset_chk(p, c, s, \ |
9f7d69c5 | 333 | __struct_size(p), __member_size(p)) |
ff901d80 | 334 | #endif |
28e77cc1 | 335 | |
f68f2ff9 KC |
336 | /* |
337 | * To make sure the compiler can enforce protection against buffer overflows, | |
338 | * memcpy(), memmove(), and memset() must not be used beyond individual | |
339 | * struct members. If you need to copy across multiple members, please use | |
340 | * struct_group() to create a named mirror of an anonymous struct union. | |
341 | * (e.g. see struct sk_buff.) Read overflow checking is currently only | |
342 | * done when a write overflow is also present, or when building with W=1. | |
343 | * | |
344 | * Mitigation coverage matrix | |
345 | * Bounds checking at: | |
346 | * +-------+-------+-------+-------+ | |
347 | * | Compile time | Run time | | |
348 | * memcpy() argument sizes: | write | read | write | read | | |
349 | * dest source length +-------+-------+-------+-------+ | |
350 | * memcpy(known, known, constant) | y | y | n/a | n/a | | |
351 | * memcpy(known, unknown, constant) | y | n | n/a | V | | |
352 | * memcpy(known, known, dynamic) | n | n | B | B | | |
353 | * memcpy(known, unknown, dynamic) | n | n | B | V | | |
354 | * memcpy(unknown, known, constant) | n | y | V | n/a | | |
355 | * memcpy(unknown, unknown, constant) | n | n | V | V | | |
356 | * memcpy(unknown, known, dynamic) | n | n | V | B | | |
357 | * memcpy(unknown, unknown, dynamic) | n | n | V | V | | |
358 | * +-------+-------+-------+-------+ | |
359 | * | |
360 | * y = perform deterministic compile-time bounds checking | |
361 | * n = cannot perform deterministic compile-time bounds checking | |
362 | * n/a = no run-time bounds checking needed since compile-time deterministic | |
363 | * B = can perform run-time bounds checking (currently unimplemented) | |
364 | * V = vulnerable to run-time overflow (will need refactoring to solve) | |
365 | * | |
366 | */ | |
54d9469b | 367 | __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size, |
f68f2ff9 KC |
368 | const size_t p_size, |
369 | const size_t q_size, | |
370 | const size_t p_size_field, | |
371 | const size_t q_size_field, | |
372 | const char *func) | |
a28a6e86 | 373 | { |
a28a6e86 | 374 | if (__builtin_constant_p(size)) { |
f68f2ff9 KC |
375 | /* |
376 | * Length argument is a constant expression, so we | |
377 | * can perform compile-time bounds checking where | |
fa35198f | 378 | * buffer sizes are also known at compile time. |
f68f2ff9 KC |
379 | */ |
380 | ||
381 | /* Error when size is larger than enclosing struct. */ | |
fa35198f KC |
382 | if (__compiletime_lessthan(p_size_field, p_size) && |
383 | __compiletime_lessthan(p_size, size)) | |
a28a6e86 | 384 | __write_overflow(); |
fa35198f KC |
385 | if (__compiletime_lessthan(q_size_field, q_size) && |
386 | __compiletime_lessthan(q_size, size)) | |
a28a6e86 | 387 | __read_overflow2(); |
f68f2ff9 KC |
388 | |
389 | /* Warn when write size argument larger than dest field. */ | |
fa35198f | 390 | if (__compiletime_lessthan(p_size_field, size)) |
f68f2ff9 KC |
391 | __write_overflow_field(p_size_field, size); |
392 | /* | |
393 | * Warn for source field over-read when building with W=1 | |
394 | * or when an over-write happened, so both can be fixed at | |
395 | * the same time. | |
396 | */ | |
fa35198f KC |
397 | if ((IS_ENABLED(KBUILD_EXTRA_WARN1) || |
398 | __compiletime_lessthan(p_size_field, size)) && | |
399 | __compiletime_lessthan(q_size_field, size)) | |
f68f2ff9 | 400 | __read_overflow2_field(q_size_field, size); |
a28a6e86 | 401 | } |
f68f2ff9 KC |
402 | /* |
403 | * At this point, length argument may not be a constant expression, | |
404 | * so run-time bounds checking can be done where buffer sizes are | |
405 | * known. (This is not an "else" because the above checks may only | |
406 | * be compile-time warnings, and we want to still warn for run-time | |
407 | * overflows.) | |
408 | */ | |
409 | ||
410 | /* | |
411 | * Always stop accesses beyond the struct that contains the | |
412 | * field, when the buffer's remaining size is known. | |
311fb40a | 413 | * (The SIZE_MAX test is to optimize away checks where the buffer |
f68f2ff9 KC |
414 | * lengths are unknown.) |
415 | */ | |
311fb40a KC |
416 | if ((p_size != SIZE_MAX && p_size < size) || |
417 | (q_size != SIZE_MAX && q_size < size)) | |
f68f2ff9 | 418 | fortify_panic(func); |
54d9469b KC |
419 | |
420 | /* | |
421 | * Warn when writing beyond destination field size. | |
422 | * | |
423 | * We must ignore p_size_field == 0 for existing 0-element | |
424 | * fake flexible arrays, until they are all converted to | |
425 | * proper flexible arrays. | |
426 | * | |
9f7d69c5 | 427 | * The implementation of __builtin_*object_size() behaves |
54d9469b KC |
428 | * like sizeof() when not directly referencing a flexible |
429 | * array member, which means there will be many bounds checks | |
430 | * that will appear at run-time, without a way for them to be | |
431 | * detected at compile-time (as can be done when the destination | |
432 | * is specifically the flexible array member). | |
433 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832 | |
434 | */ | |
435 | if (p_size_field != 0 && p_size_field != SIZE_MAX && | |
436 | p_size != p_size_field && p_size_field < size) | |
437 | return true; | |
438 | ||
439 | return false; | |
a28a6e86 FL |
440 | } |
441 | ||
f68f2ff9 KC |
442 | #define __fortify_memcpy_chk(p, q, size, p_size, q_size, \ |
443 | p_size_field, q_size_field, op) ({ \ | |
6f7630b1 KC |
444 | const size_t __fortify_size = (size_t)(size); \ |
445 | const size_t __p_size = (p_size); \ | |
446 | const size_t __q_size = (q_size); \ | |
447 | const size_t __p_size_field = (p_size_field); \ | |
448 | const size_t __q_size_field = (q_size_field); \ | |
449 | WARN_ONCE(fortify_memcpy_chk(__fortify_size, __p_size, \ | |
450 | __q_size, __p_size_field, \ | |
451 | __q_size_field, #op), \ | |
54d9469b KC |
452 | #op ": detected field-spanning write (size %zu) of single %s (size %zu)\n", \ |
453 | __fortify_size, \ | |
454 | "field \"" #p "\" at " __FILE__ ":" __stringify(__LINE__), \ | |
6f7630b1 | 455 | __p_size_field); \ |
f68f2ff9 KC |
456 | __underlying_##op(p, q, __fortify_size); \ |
457 | }) | |
458 | ||
54d9469b KC |
459 | /* |
460 | * Notes about compile-time buffer size detection: | |
461 | * | |
462 | * With these types... | |
463 | * | |
464 | * struct middle { | |
465 | * u16 a; | |
466 | * u8 middle_buf[16]; | |
467 | * int b; | |
468 | * }; | |
469 | * struct end { | |
470 | * u16 a; | |
471 | * u8 end_buf[16]; | |
472 | * }; | |
473 | * struct flex { | |
474 | * int a; | |
475 | * u8 flex_buf[]; | |
476 | * }; | |
477 | * | |
478 | * void func(TYPE *ptr) { ... } | |
479 | * | |
480 | * Cases where destination size cannot be currently detected: | |
481 | * - the size of ptr's object (seemingly by design, gcc & clang fail): | |
482 | * __builtin_object_size(ptr, 1) == SIZE_MAX | |
483 | * - the size of flexible arrays in ptr's obj (by design, dynamic size): | |
484 | * __builtin_object_size(ptr->flex_buf, 1) == SIZE_MAX | |
485 | * - the size of ANY array at the end of ptr's obj (gcc and clang bug): | |
486 | * __builtin_object_size(ptr->end_buf, 1) == SIZE_MAX | |
487 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101836 | |
488 | * | |
489 | * Cases where destination size is currently detected: | |
490 | * - the size of non-array members within ptr's object: | |
491 | * __builtin_object_size(ptr->a, 1) == 2 | |
492 | * - the size of non-flexible-array in the middle of ptr's obj: | |
493 | * __builtin_object_size(ptr->middle_buf, 1) == 16 | |
494 | * | |
495 | */ | |
496 | ||
f68f2ff9 | 497 | /* |
9f7d69c5 KC |
498 | * __struct_size() vs __member_size() must be captured here to avoid |
499 | * evaluating argument side-effects further into the macro layers. | |
f68f2ff9 KC |
500 | */ |
501 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ | |
9f7d69c5 KC |
502 | __struct_size(p), __struct_size(q), \ |
503 | __member_size(p), __member_size(q), \ | |
f68f2ff9 | 504 | memcpy) |
938a000e | 505 | #define memmove(p, q, s) __fortify_memcpy_chk(p, q, s, \ |
9f7d69c5 KC |
506 | __struct_size(p), __struct_size(q), \ |
507 | __member_size(p), __member_size(q), \ | |
938a000e | 508 | memmove) |
a28a6e86 FL |
509 | |
510 | extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan); | |
281d0c96 | 511 | __FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size) |
a28a6e86 | 512 | { |
9f7d69c5 | 513 | size_t p_size = __struct_size(p); |
a28a6e86 | 514 | |
fa35198f | 515 | if (__compiletime_lessthan(p_size, size)) |
a28a6e86 FL |
516 | __read_overflow(); |
517 | if (p_size < size) | |
518 | fortify_panic(__func__); | |
519 | return __real_memscan(p, c, size); | |
520 | } | |
521 | ||
92df138a | 522 | __FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3) |
281d0c96 | 523 | int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t size) |
a28a6e86 | 524 | { |
9f7d69c5 KC |
525 | size_t p_size = __struct_size(p); |
526 | size_t q_size = __struct_size(q); | |
a28a6e86 FL |
527 | |
528 | if (__builtin_constant_p(size)) { | |
fa35198f | 529 | if (__compiletime_lessthan(p_size, size)) |
a28a6e86 | 530 | __read_overflow(); |
fa35198f | 531 | if (__compiletime_lessthan(q_size, size)) |
a28a6e86 FL |
532 | __read_overflow2(); |
533 | } | |
534 | if (p_size < size || q_size < size) | |
535 | fortify_panic(__func__); | |
536 | return __underlying_memcmp(p, q, size); | |
537 | } | |
538 | ||
92df138a | 539 | __FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3) |
281d0c96 | 540 | void *memchr(const void * const POS0 p, int c, __kernel_size_t size) |
a28a6e86 | 541 | { |
9f7d69c5 | 542 | size_t p_size = __struct_size(p); |
a28a6e86 | 543 | |
fa35198f | 544 | if (__compiletime_lessthan(p_size, size)) |
a28a6e86 FL |
545 | __read_overflow(); |
546 | if (p_size < size) | |
547 | fortify_panic(__func__); | |
548 | return __underlying_memchr(p, c, size); | |
549 | } | |
550 | ||
551 | void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv); | |
281d0c96 | 552 | __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size) |
a28a6e86 | 553 | { |
9f7d69c5 | 554 | size_t p_size = __struct_size(p); |
a28a6e86 | 555 | |
fa35198f | 556 | if (__compiletime_lessthan(p_size, size)) |
a28a6e86 FL |
557 | __read_overflow(); |
558 | if (p_size < size) | |
559 | fortify_panic(__func__); | |
560 | return __real_memchr_inv(p, c, size); | |
561 | } | |
562 | ||
563 | extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup); | |
281d0c96 | 564 | __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp) |
a28a6e86 | 565 | { |
9f7d69c5 | 566 | size_t p_size = __struct_size(p); |
a28a6e86 | 567 | |
fa35198f | 568 | if (__compiletime_lessthan(p_size, size)) |
a28a6e86 FL |
569 | __read_overflow(); |
570 | if (p_size < size) | |
571 | fortify_panic(__func__); | |
572 | return __real_kmemdup(p, size, gfp); | |
573 | } | |
574 | ||
f68f2ff9 | 575 | /* Defined after fortified strlen to reuse it. */ |
92df138a | 576 | __FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2) |
281d0c96 | 577 | char *strcpy(char * const POS p, const char * const POS q) |
a28a6e86 | 578 | { |
9f7d69c5 KC |
579 | size_t p_size = __member_size(p); |
580 | size_t q_size = __member_size(q); | |
a28a6e86 FL |
581 | size_t size; |
582 | ||
f68f2ff9 | 583 | /* If neither buffer size is known, immediately give up. */ |
fa35198f KC |
584 | if (__builtin_constant_p(p_size) && |
585 | __builtin_constant_p(q_size) && | |
586 | p_size == SIZE_MAX && q_size == SIZE_MAX) | |
a28a6e86 FL |
587 | return __underlying_strcpy(p, q); |
588 | size = strlen(q) + 1; | |
072af0c6 | 589 | /* Compile-time check for const size overflow. */ |
fa35198f | 590 | if (__compiletime_lessthan(p_size, size)) |
072af0c6 KC |
591 | __write_overflow(); |
592 | /* Run-time check for dynamic size overflow. */ | |
a28a6e86 FL |
593 | if (p_size < size) |
594 | fortify_panic(__func__); | |
f68f2ff9 | 595 | __underlying_memcpy(p, q, size); |
a28a6e86 FL |
596 | return p; |
597 | } | |
598 | ||
599 | /* Don't use these outside the FORITFY_SOURCE implementation */ | |
600 | #undef __underlying_memchr | |
601 | #undef __underlying_memcmp | |
a28a6e86 FL |
602 | #undef __underlying_strcat |
603 | #undef __underlying_strcpy | |
604 | #undef __underlying_strlen | |
605 | #undef __underlying_strncat | |
606 | #undef __underlying_strncpy | |
607 | ||
281d0c96 KC |
608 | #undef POS |
609 | #undef POS0 | |
610 | ||
a28a6e86 | 611 | #endif /* _LINUX_FORTIFY_STRING_H_ */ |