| @@ -2,13 +2,17 @@ · 1 unmodified line |
| 2 | 2 | #ifndef _LINUX_FORTIFY_STRING_H_ |
| 3 | 3 | #define _LINUX_FORTIFY_STRING_H_ |
| 4 | 4 | |
| 5 | | −#define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline)) |
| 5 | +#include <linux/const.h> |
| 6 | + |
| 7 | +#define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable |
| 6 | 8 | #define __RENAME(x) __asm__(#x) |
| 7 | 9 | |
| 8 | 10 | void fortify_panic(const char *name) __noreturn __cold; |
| 9 | 11 | void __read_overflow(void) __compiletime_error("detected read beyond size of object (1st parameter)"); |
| 10 | 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()?"); |
| 11 | 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()?"); |
| 12 | 16 | |
| 13 | 17 | #define __compiletime_strlen(p) \ |
| 14 | 18 | ({ \ |
| @@ -48,7 +52,17 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) · 33 unmodified lines |
| 48 | 52 | #define __underlying_strncpy __builtin_strncpy |
| 49 | 53 | #endif |
| 50 | 54 | |
| 51 | | −__FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size) |
| 55 | +/* |
| 56 | + * Clang's use of __builtin_object_size() within inlines needs hinting via |
| 57 | + * __pass_object_size(). The preference is to only ever use type 1 (member |
| 58 | + * size, rather than struct size), but there remain some stragglers using |
| 59 | + * type 0 that will be converted in the future. |
| 60 | + */ |
| 61 | +#define POS __pass_object_size(1) |
| 62 | +#define POS0 __pass_object_size(0) |
| 63 | + |
| 64 | +__FORTIFY_INLINE __diagnose_as(__builtin_strncpy, 1, 2, 3) |
| 65 | +char *strncpy(char * const POS p, const char *q, __kernel_size_t size) |
| 52 | 66 | { |
| 53 | 67 | size_t p_size = __builtin_object_size(p, 1); |
| 54 | 68 | |
| @@ -59,7 +73,8 @@ __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size) · 4 unmodified lines |
| 59 | 73 | return __underlying_strncpy(p, q, size); |
| 60 | 74 | } |
| 61 | 75 | |
| 62 | | −__FORTIFY_INLINE char *strcat(char *p, const char *q) |
| 76 | +__FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2) |
| 77 | +char *strcat(char * const POS p, const char *q) |
| 63 | 78 | { |
| 64 | 79 | size_t p_size = __builtin_object_size(p, 1); |
| 65 | 80 | |
| @@ -71,7 +86,7 @@ __FORTIFY_INLINE char *strcat(char *p, const char *q) · 5 unmodified lines |
| 71 | 86 | } |
| 72 | 87 | |
| 73 | 88 | extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen); |
| 74 | | −__FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen) |
| 89 | +__FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size_t maxlen) |
| 75 | 90 | { |
| 76 | 91 | size_t p_size = __builtin_object_size(p, 1); |
| 77 | 92 | size_t p_len = __compiletime_strlen(p); |
| @@ -91,8 +106,16 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen) · 13 unmodified lines |
| 91 | 106 | return ret; |
| 92 | 107 | } |
| 93 | 108 | |
| 94 | | −/* defined after fortified strnlen to reuse it. */ |
| 95 | | −__FORTIFY_INLINE __kernel_size_t strlen(const char *p) |
| 109 | +/* |
| 110 | + * Defined after fortified strnlen to reuse it. However, it must still be |
| 111 | + * possible for strlen() to be used on compile-time strings for use in |
| 112 | + * static initializers (i.e. as a constant expression). |
| 113 | + */ |
| 114 | +#define strlen(p) \ |
| 115 | + __builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \ |
| 116 | + __builtin_strlen(p), __fortify_strlen(p)) |
| 117 | +__FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1) |
| 118 | +__kernel_size_t __fortify_strlen(const char * const POS p) |
| 96 | 119 | { |
| 97 | 120 | __kernel_size_t ret; |
| 98 | 121 | size_t p_size = __builtin_object_size(p, 1); |
| @@ -108,7 +131,7 @@ __FORTIFY_INLINE __kernel_size_t strlen(const char *p) · 9 unmodified lines |
| 108 | 131 | |
| 109 | 132 | /* defined after fortified strlen to reuse it */ |
| 110 | 133 | extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy); |
| 111 | | −__FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size) |
| 134 | +__FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, size_t size) |
| 112 | 135 | { |
| 113 | 136 | size_t p_size = __builtin_object_size(p, 1); |
| 114 | 137 | size_t q_size = __builtin_object_size(q, 1); |
| @@ -135,7 +158,7 @@ __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size) · 20 unmodified lines |
| 135 | 158 | |
| 136 | 159 | /* defined after fortified strnlen to reuse it */ |
| 137 | 160 | extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy); |
| 138 | | −__FORTIFY_INLINE ssize_t strscpy(char *p, const char *q, size_t size) |
| 161 | +__FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, size_t size) |
| 139 | 162 | { |
| 140 | 163 | size_t len; |
| 141 | 164 | /* Use string size rather than possible enclosing struct size. */ |
| @@ -181,7 +204,8 @@ __FORTIFY_INLINE ssize_t strscpy(char *p, const char *q, size_t size) · 39 unmodified lines |
| 181 | 204 | } |
| 182 | 205 | |
| 183 | 206 | /* defined after fortified strlen and strnlen to reuse them */ |
| 184 | | −__FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) |
| 207 | +__FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3) |
| 208 | +char *strncat(char * const POS p, const char * const POS q, __kernel_size_t count) |
| 185 | 209 | { |
| 186 | 210 | size_t p_len, copy_len; |
| 187 | 211 | size_t p_size = __builtin_object_size(p, 1); |
| @@ -198,51 +222,161 @@ __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) · 10 unmodified lines |
| 198 | 222 | return p; |
| 199 | 223 | } |
| 200 | 224 | |
| 201 | | −__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size) |
| 225 | +__FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size, |
| 226 | + const size_t p_size, |
| 227 | + const size_t p_size_field) |
| 202 | 228 | { |
| 203 | | − size_t p_size = __builtin_object_size(p, 0); |
| 204 | | − |
| 205 | | − if (__builtin_constant_p(size) && p_size < size) |
| 206 | | − __write_overflow(); |
| 207 | | − if (p_size < size) |
| 208 | | − fortify_panic(__func__); |
| 209 | | − return __underlying_memset(p, c, size); |
| 210 | | −} |
| 211 | | − |
| 212 | | −__FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) |
| 213 | | −{ |
| 214 | | − size_t p_size = __builtin_object_size(p, 0); |
| 215 | | − size_t q_size = __builtin_object_size(q, 0); |
| 216 | | − |
| 217 | 229 | if (__builtin_constant_p(size)) { |
| 218 | | − if (p_size < size) |
| 230 | + /* |
| 231 | + * Length argument is a constant expression, so we |
| 232 | + * can perform compile-time bounds checking where |
| 233 | + * buffer sizes are known. |
| 234 | + */ |
| 235 | + |
| 236 | + /* Error when size is larger than enclosing struct. */ |
| 237 | + if (p_size > p_size_field && p_size < size) |
| 219 | 238 | __write_overflow(); |
| 220 | | − if (q_size < size) |
| 221 | | − __read_overflow2(); |
| 239 | + |
| 240 | + /* Warn when write size is larger than dest field. */ |
| 241 | + if (p_size_field < size) |
| 242 | + __write_overflow_field(p_size_field, size); |
| 222 | 243 | } |
| 223 | | − if (p_size < size || q_size < size) |
| 224 | | − fortify_panic(__func__); |
| 225 | | − return __underlying_memcpy(p, q, size); |
| 244 | + /* |
| 245 | + * At this point, length argument may not be a constant expression, |
| 246 | + * so run-time bounds checking can be done where buffer sizes are |
| 247 | + * known. (This is not an "else" because the above checks may only |
| 248 | + * be compile-time warnings, and we want to still warn for run-time |
| 249 | + * overflows.) |
| 250 | + */ |
| 251 | + |
| 252 | + /* |
| 253 | + * Always stop accesses beyond the struct that contains the |
| 254 | + * field, when the buffer's remaining size is known. |
| 255 | + * (The -1 test is to optimize away checks where the buffer |
| 256 | + * lengths are unknown.) |
| 257 | + */ |
| 258 | + if (p_size != (size_t)(-1) && p_size < size) |
| 259 | + fortify_panic("memset"); |
| 226 | 260 | } |
| 227 | 261 | |
| 228 | | −__FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) |
| 229 | | −{ |
| 230 | | − size_t p_size = __builtin_object_size(p, 0); |
| 231 | | − size_t q_size = __builtin_object_size(q, 0); |
| 262 | +#define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({ \ |
| 263 | + size_t __fortify_size = (size_t)(size); \ |
| 264 | + fortify_memset_chk(__fortify_size, p_size, p_size_field), \ |
| 265 | + __underlying_memset(p, c, __fortify_size); \ |
| 266 | +}) |
| 232 | 267 | |
| 268 | +/* |
| 269 | + * __builtin_object_size() must be captured here to avoid evaluating argument |
| 270 | + * side-effects further into the macro layers. |
| 271 | + */ |
| 272 | +#define memset(p, c, s) __fortify_memset_chk(p, c, s, \ |
| 273 | + __builtin_object_size(p, 0), __builtin_object_size(p, 1)) |
| 274 | + |
| 275 | +/* |
| 276 | + * To make sure the compiler can enforce protection against buffer overflows, |
| 277 | + * memcpy(), memmove(), and memset() must not be used beyond individual |
| 278 | + * struct members. If you need to copy across multiple members, please use |
| 279 | + * struct_group() to create a named mirror of an anonymous struct union. |
| 280 | + * (e.g. see struct sk_buff.) Read overflow checking is currently only |
| 281 | + * done when a write overflow is also present, or when building with W=1. |
| 282 | + * |
| 283 | + * Mitigation coverage matrix |
| 284 | + * Bounds checking at: |
| 285 | + * +-------+-------+-------+-------+ |
| 286 | + * | Compile time | Run time | |
| 287 | + * memcpy() argument sizes: | write | read | write | read | |
| 288 | + * dest source length +-------+-------+-------+-------+ |
| 289 | + * memcpy(known, known, constant) | y | y | n/a | n/a | |
| 290 | + * memcpy(known, unknown, constant) | y | n | n/a | V | |
| 291 | + * memcpy(known, known, dynamic) | n | n | B | B | |
| 292 | + * memcpy(known, unknown, dynamic) | n | n | B | V | |
| 293 | + * memcpy(unknown, known, constant) | n | y | V | n/a | |
| 294 | + * memcpy(unknown, unknown, constant) | n | n | V | V | |
| 295 | + * memcpy(unknown, known, dynamic) | n | n | V | B | |
| 296 | + * memcpy(unknown, unknown, dynamic) | n | n | V | V | |
| 297 | + * +-------+-------+-------+-------+ |
| 298 | + * |
| 299 | + * y = perform deterministic compile-time bounds checking |
| 300 | + * n = cannot perform deterministic compile-time bounds checking |
| 301 | + * n/a = no run-time bounds checking needed since compile-time deterministic |
| 302 | + * B = can perform run-time bounds checking (currently unimplemented) |
| 303 | + * V = vulnerable to run-time overflow (will need refactoring to solve) |
| 304 | + * |
| 305 | + */ |
| 306 | +__FORTIFY_INLINE void fortify_memcpy_chk(__kernel_size_t size, |
| 307 | + const size_t p_size, |
| 308 | + const size_t q_size, |
| 309 | + const size_t p_size_field, |
| 310 | + const size_t q_size_field, |
| 311 | + const char *func) |
| 312 | +{ |
| 233 | 313 | if (__builtin_constant_p(size)) { |
| 234 | | − if (p_size < size) |
| 314 | + /* |
| 315 | + * Length argument is a constant expression, so we |
| 316 | + * can perform compile-time bounds checking where |
| 317 | + * buffer sizes are known. |
| 318 | + */ |
| 319 | + |
| 320 | + /* Error when size is larger than enclosing struct. */ |
| 321 | + if (p_size > p_size_field && p_size < size) |
| 235 | 322 | __write_overflow(); |
| 236 | | − if (q_size < size) |
| 323 | + if (q_size > q_size_field && q_size < size) |
| 237 | 324 | __read_overflow2(); |
| 325 | + |
| 326 | + /* Warn when write size argument larger than dest field. */ |
| 327 | + if (p_size_field < size) |
| 328 | + __write_overflow_field(p_size_field, size); |
| 329 | + /* |
| 330 | + * Warn for source field over-read when building with W=1 |
| 331 | + * or when an over-write happened, so both can be fixed at |
| 332 | + * the same time. |
| 333 | + */ |
| 334 | + if ((IS_ENABLED(KBUILD_EXTRA_WARN1) || p_size_field < size) && |
| 335 | + q_size_field < size) |
| 336 | + __read_overflow2_field(q_size_field, size); |
| 238 | 337 | } |
| 239 | | − if (p_size < size || q_size < size) |
| 240 | | − fortify_panic(__func__); |
| 241 | | − return __underlying_memmove(p, q, size); |
| 338 | + /* |
| 339 | + * At this point, length argument may not be a constant expression, |
| 340 | + * so run-time bounds checking can be done where buffer sizes are |
| 341 | + * known. (This is not an "else" because the above checks may only |
| 342 | + * be compile-time warnings, and we want to still warn for run-time |
| 343 | + * overflows.) |
| 344 | + */ |
| 345 | + |
| 346 | + /* |
| 347 | + * Always stop accesses beyond the struct that contains the |
| 348 | + * field, when the buffer's remaining size is known. |
| 349 | + * (The -1 test is to optimize away checks where the buffer |
| 350 | + * lengths are unknown.) |
| 351 | + */ |
| 352 | + if ((p_size != (size_t)(-1) && p_size < size) || |
| 353 | + (q_size != (size_t)(-1) && q_size < size)) |
| 354 | + fortify_panic(func); |
| 242 | 355 | } |
| 243 | 356 | |
| 357 | +#define __fortify_memcpy_chk(p, q, size, p_size, q_size, \ |
| 358 | + p_size_field, q_size_field, op) ({ \ |
| 359 | + size_t __fortify_size = (size_t)(size); \ |
| 360 | + fortify_memcpy_chk(__fortify_size, p_size, q_size, \ |
| 361 | + p_size_field, q_size_field, #op); \ |
| 362 | + __underlying_##op(p, q, __fortify_size); \ |
| 363 | +}) |
| 364 | + |
| 365 | +/* |
| 366 | + * __builtin_object_size() must be captured here to avoid evaluating argument |
| 367 | + * side-effects further into the macro layers. |
| 368 | + */ |
| 369 | +#define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ |
| 370 | + __builtin_object_size(p, 0), __builtin_object_size(q, 0), \ |
| 371 | + __builtin_object_size(p, 1), __builtin_object_size(q, 1), \ |
| 372 | + memcpy) |
| 373 | +#define memmove(p, q, s) __fortify_memcpy_chk(p, q, s, \ |
| 374 | + __builtin_object_size(p, 0), __builtin_object_size(q, 0), \ |
| 375 | + __builtin_object_size(p, 1), __builtin_object_size(q, 1), \ |
| 376 | + memmove) |
| 377 | + |
| 244 | 378 | extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan); |
| 245 | | −__FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size) |
| 379 | +__FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size) |
| 246 | 380 | { |
| 247 | 381 | size_t p_size = __builtin_object_size(p, 0); |
| 248 | 382 | |
| @@ -253,7 +387,8 @@ __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size) · 4 unmodified lines |
| 253 | 387 | return __real_memscan(p, c, size); |
| 254 | 388 | } |
| 255 | 389 | |
| 256 | | −__FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size) |
| 390 | +__FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3) |
| 391 | +int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t size) |
| 257 | 392 | { |
| 258 | 393 | size_t p_size = __builtin_object_size(p, 0); |
| 259 | 394 | size_t q_size = __builtin_object_size(q, 0); |
| @@ -269,7 +404,8 @@ __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size) · 9 unmodified lines |
| 269 | 404 | return __underlying_memcmp(p, q, size); |
| 270 | 405 | } |
| 271 | 406 | |
| 272 | | −__FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size) |
| 407 | +__FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3) |
| 408 | +void *memchr(const void * const POS0 p, int c, __kernel_size_t size) |
| 273 | 409 | { |
| 274 | 410 | size_t p_size = __builtin_object_size(p, 0); |
| 275 | 411 | |
| @@ -281,7 +417,7 @@ __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size) · 5 unmodified lines |
| 281 | 417 | } |
| 282 | 418 | |
| 283 | 419 | void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv); |
| 284 | | −__FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size) |
| 420 | +__FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size) |
| 285 | 421 | { |
| 286 | 422 | size_t p_size = __builtin_object_size(p, 0); |
| 287 | 423 | |
| @@ -293,7 +429,7 @@ __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size) · 5 unmodified lines |
| 293 | 429 | } |
| 294 | 430 | |
| 295 | 431 | extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup); |
| 296 | | −__FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp) |
| 432 | +__FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp) |
| 297 | 433 | { |
| 298 | 434 | size_t p_size = __builtin_object_size(p, 0); |
| 299 | 435 | |
| @@ -304,13 +440,15 @@ __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp) · 4 unmodified lines |
| 304 | 440 | return __real_kmemdup(p, size, gfp); |
| 305 | 441 | } |
| 306 | 442 | |
| 307 | | −/* defined after fortified strlen and memcpy to reuse them */ |
| 308 | | −__FORTIFY_INLINE char *strcpy(char *p, const char *q) |
| 443 | +/* Defined after fortified strlen to reuse it. */ |
| 444 | +__FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2) |
| 445 | +char *strcpy(char * const POS p, const char * const POS q) |
| 309 | 446 | { |
| 310 | 447 | size_t p_size = __builtin_object_size(p, 1); |
| 311 | 448 | size_t q_size = __builtin_object_size(q, 1); |
| 312 | 449 | size_t size; |
| 313 | 450 | |
| 451 | + /* If neither buffer size is known, immediately give up. */ |
| 314 | 452 | if (p_size == (size_t)-1 && q_size == (size_t)-1) |
| 315 | 453 | return __underlying_strcpy(p, q); |
| 316 | 454 | size = strlen(q) + 1; |
| @@ -320,20 +458,20 @@ __FORTIFY_INLINE char *strcpy(char *p, const char *q) · 3 unmodified lines |
| 320 | 458 | /* Run-time check for dynamic size overflow. */ |
| 321 | 459 | if (p_size < size) |
| 322 | 460 | fortify_panic(__func__); |
| 323 | | − memcpy(p, q, size); |
| 461 | + __underlying_memcpy(p, q, size); |
| 324 | 462 | return p; |
| 325 | 463 | } |
| 326 | 464 | |
| 327 | 465 | /* Don't use these outside the FORITFY_SOURCE implementation */ |
| 328 | 466 | #undef __underlying_memchr |
| 329 | 467 | #undef __underlying_memcmp |
| 330 | | −#undef __underlying_memcpy |
| 331 | | −#undef __underlying_memmove |
| 332 | | −#undef __underlying_memset |
| 333 | 468 | #undef __underlying_strcat |
| 334 | 469 | #undef __underlying_strcpy |
| 335 | 470 | #undef __underlying_strlen |
| 336 | 471 | #undef __underlying_strncat |
| 337 | 472 | #undef __underlying_strncpy |
| 338 | 473 | |
| 474 | +#undef POS |
| 475 | +#undef POS0 |
| 476 | + |
| 339 | 477 | #endif /* _LINUX_FORTIFY_STRING_H_ */ |