at v5.10 17 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_STRING_H_ 3#define _LINUX_STRING_H_ 4 5 6#include <linux/compiler.h> /* for inline */ 7#include <linux/types.h> /* for size_t */ 8#include <linux/stddef.h> /* for NULL */ 9#include <stdarg.h> 10#include <uapi/linux/string.h> 11 12extern char *strndup_user(const char __user *, long); 13extern void *memdup_user(const void __user *, size_t); 14extern void *vmemdup_user(const void __user *, size_t); 15extern void *memdup_user_nul(const void __user *, size_t); 16 17/* 18 * Include machine specific inline routines 19 */ 20#include <asm/string.h> 21 22#ifndef __HAVE_ARCH_STRCPY 23extern char * strcpy(char *,const char *); 24#endif 25#ifndef __HAVE_ARCH_STRNCPY 26extern char * strncpy(char *,const char *, __kernel_size_t); 27#endif 28#ifndef __HAVE_ARCH_STRLCPY 29size_t strlcpy(char *, const char *, size_t); 30#endif 31#ifndef __HAVE_ARCH_STRSCPY 32ssize_t strscpy(char *, const char *, size_t); 33#endif 34 35/* Wraps calls to strscpy()/memset(), no arch specific code required */ 36ssize_t strscpy_pad(char *dest, const char *src, size_t count); 37 38#ifndef __HAVE_ARCH_STRCAT 39extern char * strcat(char *, const char *); 40#endif 41#ifndef __HAVE_ARCH_STRNCAT 42extern char * strncat(char *, const char *, __kernel_size_t); 43#endif 44#ifndef __HAVE_ARCH_STRLCAT 45extern size_t strlcat(char *, const char *, __kernel_size_t); 46#endif 47#ifndef __HAVE_ARCH_STRCMP 48extern int strcmp(const char *,const char *); 49#endif 50#ifndef __HAVE_ARCH_STRNCMP 51extern int strncmp(const char *,const char *,__kernel_size_t); 52#endif 53#ifndef __HAVE_ARCH_STRCASECMP 54extern int strcasecmp(const char *s1, const char *s2); 55#endif 56#ifndef __HAVE_ARCH_STRNCASECMP 57extern int strncasecmp(const char *s1, const char *s2, size_t n); 58#endif 59#ifndef __HAVE_ARCH_STRCHR 60extern char * strchr(const char *,int); 61#endif 62#ifndef __HAVE_ARCH_STRCHRNUL 63extern char * strchrnul(const char *,int); 64#endif 65extern char * strnchrnul(const char *, size_t, int); 66#ifndef __HAVE_ARCH_STRNCHR 67extern char * strnchr(const char *, size_t, int); 68#endif 69#ifndef __HAVE_ARCH_STRRCHR 70extern char * strrchr(const char *,int); 71#endif 72extern char * __must_check skip_spaces(const char *); 73 74extern char *strim(char *); 75 76static inline __must_check char *strstrip(char *str) 77{ 78 return strim(str); 79} 80 81#ifndef __HAVE_ARCH_STRSTR 82extern char * strstr(const char *, const char *); 83#endif 84#ifndef __HAVE_ARCH_STRNSTR 85extern char * strnstr(const char *, const char *, size_t); 86#endif 87#ifndef __HAVE_ARCH_STRLEN 88extern __kernel_size_t strlen(const char *); 89#endif 90#ifndef __HAVE_ARCH_STRNLEN 91extern __kernel_size_t strnlen(const char *,__kernel_size_t); 92#endif 93#ifndef __HAVE_ARCH_STRPBRK 94extern char * strpbrk(const char *,const char *); 95#endif 96#ifndef __HAVE_ARCH_STRSEP 97extern char * strsep(char **,const char *); 98#endif 99#ifndef __HAVE_ARCH_STRSPN 100extern __kernel_size_t strspn(const char *,const char *); 101#endif 102#ifndef __HAVE_ARCH_STRCSPN 103extern __kernel_size_t strcspn(const char *,const char *); 104#endif 105 106#ifndef __HAVE_ARCH_MEMSET 107extern void * memset(void *,int,__kernel_size_t); 108#endif 109 110#ifndef __HAVE_ARCH_MEMSET16 111extern void *memset16(uint16_t *, uint16_t, __kernel_size_t); 112#endif 113 114#ifndef __HAVE_ARCH_MEMSET32 115extern void *memset32(uint32_t *, uint32_t, __kernel_size_t); 116#endif 117 118#ifndef __HAVE_ARCH_MEMSET64 119extern void *memset64(uint64_t *, uint64_t, __kernel_size_t); 120#endif 121 122static inline void *memset_l(unsigned long *p, unsigned long v, 123 __kernel_size_t n) 124{ 125 if (BITS_PER_LONG == 32) 126 return memset32((uint32_t *)p, v, n); 127 else 128 return memset64((uint64_t *)p, v, n); 129} 130 131static inline void *memset_p(void **p, void *v, __kernel_size_t n) 132{ 133 if (BITS_PER_LONG == 32) 134 return memset32((uint32_t *)p, (uintptr_t)v, n); 135 else 136 return memset64((uint64_t *)p, (uintptr_t)v, n); 137} 138 139extern void **__memcat_p(void **a, void **b); 140#define memcat_p(a, b) ({ \ 141 BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \ 142 "type mismatch in memcat_p()"); \ 143 (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \ 144}) 145 146#ifndef __HAVE_ARCH_MEMCPY 147extern void * memcpy(void *,const void *,__kernel_size_t); 148#endif 149#ifndef __HAVE_ARCH_MEMMOVE 150extern void * memmove(void *,const void *,__kernel_size_t); 151#endif 152#ifndef __HAVE_ARCH_MEMSCAN 153extern void * memscan(void *,int,__kernel_size_t); 154#endif 155#ifndef __HAVE_ARCH_MEMCMP 156extern int memcmp(const void *,const void *,__kernel_size_t); 157#endif 158#ifndef __HAVE_ARCH_BCMP 159extern int bcmp(const void *,const void *,__kernel_size_t); 160#endif 161#ifndef __HAVE_ARCH_MEMCHR 162extern void * memchr(const void *,int,__kernel_size_t); 163#endif 164#ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE 165static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) 166{ 167 memcpy(dst, src, cnt); 168} 169#endif 170 171void *memchr_inv(const void *s, int c, size_t n); 172char *strreplace(char *s, char old, char new); 173 174extern void kfree_const(const void *x); 175 176extern char *kstrdup(const char *s, gfp_t gfp) __malloc; 177extern const char *kstrdup_const(const char *s, gfp_t gfp); 178extern char *kstrndup(const char *s, size_t len, gfp_t gfp); 179extern void *kmemdup(const void *src, size_t len, gfp_t gfp); 180extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); 181 182extern char **argv_split(gfp_t gfp, const char *str, int *argcp); 183extern void argv_free(char **argv); 184 185extern bool sysfs_streq(const char *s1, const char *s2); 186extern int kstrtobool(const char *s, bool *res); 187static inline int strtobool(const char *s, bool *res) 188{ 189 return kstrtobool(s, res); 190} 191 192int match_string(const char * const *array, size_t n, const char *string); 193int __sysfs_match_string(const char * const *array, size_t n, const char *s); 194 195/** 196 * sysfs_match_string - matches given string in an array 197 * @_a: array of strings 198 * @_s: string to match with 199 * 200 * Helper for __sysfs_match_string(). Calculates the size of @a automatically. 201 */ 202#define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s) 203 204#ifdef CONFIG_BINARY_PRINTF 205int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args); 206int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf); 207int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4); 208#endif 209 210extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 211 const void *from, size_t available); 212 213int ptr_to_hashval(const void *ptr, unsigned long *hashval_out); 214 215/** 216 * strstarts - does @str start with @prefix? 217 * @str: string to examine 218 * @prefix: prefix to look for. 219 */ 220static inline bool strstarts(const char *str, const char *prefix) 221{ 222 return strncmp(str, prefix, strlen(prefix)) == 0; 223} 224 225size_t memweight(const void *ptr, size_t bytes); 226 227/** 228 * memzero_explicit - Fill a region of memory (e.g. sensitive 229 * keying data) with 0s. 230 * @s: Pointer to the start of the area. 231 * @count: The size of the area. 232 * 233 * Note: usually using memset() is just fine (!), but in cases 234 * where clearing out _local_ data at the end of a scope is 235 * necessary, memzero_explicit() should be used instead in 236 * order to prevent the compiler from optimising away zeroing. 237 * 238 * memzero_explicit() doesn't need an arch-specific version as 239 * it just invokes the one of memset() implicitly. 240 */ 241static inline void memzero_explicit(void *s, size_t count) 242{ 243 memset(s, 0, count); 244 barrier_data(s); 245} 246 247/** 248 * kbasename - return the last part of a pathname. 249 * 250 * @path: path to extract the filename from. 251 */ 252static inline const char *kbasename(const char *path) 253{ 254 const char *tail = strrchr(path, '/'); 255 return tail ? tail + 1 : path; 256} 257 258#define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline)) 259#define __RENAME(x) __asm__(#x) 260 261void fortify_panic(const char *name) __noreturn __cold; 262void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter"); 263void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter"); 264void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter"); 265void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter"); 266 267#if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE) 268 269#ifdef CONFIG_KASAN 270extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr); 271extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp); 272extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy); 273extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove); 274extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset); 275extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat); 276extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy); 277extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen); 278extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat); 279extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy); 280#else 281#define __underlying_memchr __builtin_memchr 282#define __underlying_memcmp __builtin_memcmp 283#define __underlying_memcpy __builtin_memcpy 284#define __underlying_memmove __builtin_memmove 285#define __underlying_memset __builtin_memset 286#define __underlying_strcat __builtin_strcat 287#define __underlying_strcpy __builtin_strcpy 288#define __underlying_strlen __builtin_strlen 289#define __underlying_strncat __builtin_strncat 290#define __underlying_strncpy __builtin_strncpy 291#endif 292 293__FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size) 294{ 295 size_t p_size = __builtin_object_size(p, 0); 296 if (__builtin_constant_p(size) && p_size < size) 297 __write_overflow(); 298 if (p_size < size) 299 fortify_panic(__func__); 300 return __underlying_strncpy(p, q, size); 301} 302 303__FORTIFY_INLINE char *strcat(char *p, const char *q) 304{ 305 size_t p_size = __builtin_object_size(p, 0); 306 if (p_size == (size_t)-1) 307 return __underlying_strcat(p, q); 308 if (strlcat(p, q, p_size) >= p_size) 309 fortify_panic(__func__); 310 return p; 311} 312 313__FORTIFY_INLINE __kernel_size_t strlen(const char *p) 314{ 315 __kernel_size_t ret; 316 size_t p_size = __builtin_object_size(p, 0); 317 318 /* Work around gcc excess stack consumption issue */ 319 if (p_size == (size_t)-1 || 320 (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0')) 321 return __underlying_strlen(p); 322 ret = strnlen(p, p_size); 323 if (p_size <= ret) 324 fortify_panic(__func__); 325 return ret; 326} 327 328extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen); 329__FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen) 330{ 331 size_t p_size = __builtin_object_size(p, 0); 332 __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size); 333 if (p_size <= ret && maxlen != ret) 334 fortify_panic(__func__); 335 return ret; 336} 337 338/* defined after fortified strlen to reuse it */ 339extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy); 340__FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size) 341{ 342 size_t ret; 343 size_t p_size = __builtin_object_size(p, 0); 344 size_t q_size = __builtin_object_size(q, 0); 345 if (p_size == (size_t)-1 && q_size == (size_t)-1) 346 return __real_strlcpy(p, q, size); 347 ret = strlen(q); 348 if (size) { 349 size_t len = (ret >= size) ? size - 1 : ret; 350 if (__builtin_constant_p(len) && len >= p_size) 351 __write_overflow(); 352 if (len >= p_size) 353 fortify_panic(__func__); 354 __underlying_memcpy(p, q, len); 355 p[len] = '\0'; 356 } 357 return ret; 358} 359 360/* defined after fortified strlen and strnlen to reuse them */ 361__FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) 362{ 363 size_t p_len, copy_len; 364 size_t p_size = __builtin_object_size(p, 0); 365 size_t q_size = __builtin_object_size(q, 0); 366 if (p_size == (size_t)-1 && q_size == (size_t)-1) 367 return __underlying_strncat(p, q, count); 368 p_len = strlen(p); 369 copy_len = strnlen(q, count); 370 if (p_size < p_len + copy_len + 1) 371 fortify_panic(__func__); 372 __underlying_memcpy(p + p_len, q, copy_len); 373 p[p_len + copy_len] = '\0'; 374 return p; 375} 376 377__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size) 378{ 379 size_t p_size = __builtin_object_size(p, 0); 380 if (__builtin_constant_p(size) && p_size < size) 381 __write_overflow(); 382 if (p_size < size) 383 fortify_panic(__func__); 384 return __underlying_memset(p, c, size); 385} 386 387__FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) 388{ 389 size_t p_size = __builtin_object_size(p, 0); 390 size_t q_size = __builtin_object_size(q, 0); 391 if (__builtin_constant_p(size)) { 392 if (p_size < size) 393 __write_overflow(); 394 if (q_size < size) 395 __read_overflow2(); 396 } 397 if (p_size < size || q_size < size) 398 fortify_panic(__func__); 399 return __underlying_memcpy(p, q, size); 400} 401 402__FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) 403{ 404 size_t p_size = __builtin_object_size(p, 0); 405 size_t q_size = __builtin_object_size(q, 0); 406 if (__builtin_constant_p(size)) { 407 if (p_size < size) 408 __write_overflow(); 409 if (q_size < size) 410 __read_overflow2(); 411 } 412 if (p_size < size || q_size < size) 413 fortify_panic(__func__); 414 return __underlying_memmove(p, q, size); 415} 416 417extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan); 418__FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size) 419{ 420 size_t p_size = __builtin_object_size(p, 0); 421 if (__builtin_constant_p(size) && p_size < size) 422 __read_overflow(); 423 if (p_size < size) 424 fortify_panic(__func__); 425 return __real_memscan(p, c, size); 426} 427 428__FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size) 429{ 430 size_t p_size = __builtin_object_size(p, 0); 431 size_t q_size = __builtin_object_size(q, 0); 432 if (__builtin_constant_p(size)) { 433 if (p_size < size) 434 __read_overflow(); 435 if (q_size < size) 436 __read_overflow2(); 437 } 438 if (p_size < size || q_size < size) 439 fortify_panic(__func__); 440 return __underlying_memcmp(p, q, size); 441} 442 443__FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size) 444{ 445 size_t p_size = __builtin_object_size(p, 0); 446 if (__builtin_constant_p(size) && p_size < size) 447 __read_overflow(); 448 if (p_size < size) 449 fortify_panic(__func__); 450 return __underlying_memchr(p, c, size); 451} 452 453void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv); 454__FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size) 455{ 456 size_t p_size = __builtin_object_size(p, 0); 457 if (__builtin_constant_p(size) && p_size < size) 458 __read_overflow(); 459 if (p_size < size) 460 fortify_panic(__func__); 461 return __real_memchr_inv(p, c, size); 462} 463 464extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup); 465__FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp) 466{ 467 size_t p_size = __builtin_object_size(p, 0); 468 if (__builtin_constant_p(size) && p_size < size) 469 __read_overflow(); 470 if (p_size < size) 471 fortify_panic(__func__); 472 return __real_kmemdup(p, size, gfp); 473} 474 475/* defined after fortified strlen and memcpy to reuse them */ 476__FORTIFY_INLINE char *strcpy(char *p, const char *q) 477{ 478 size_t p_size = __builtin_object_size(p, 0); 479 size_t q_size = __builtin_object_size(q, 0); 480 if (p_size == (size_t)-1 && q_size == (size_t)-1) 481 return __underlying_strcpy(p, q); 482 memcpy(p, q, strlen(q) + 1); 483 return p; 484} 485 486/* Don't use these outside the FORITFY_SOURCE implementation */ 487#undef __underlying_memchr 488#undef __underlying_memcmp 489#undef __underlying_memcpy 490#undef __underlying_memmove 491#undef __underlying_memset 492#undef __underlying_strcat 493#undef __underlying_strcpy 494#undef __underlying_strlen 495#undef __underlying_strncat 496#undef __underlying_strncpy 497#endif 498 499/** 500 * memcpy_and_pad - Copy one buffer to another with padding 501 * @dest: Where to copy to 502 * @dest_len: The destination buffer size 503 * @src: Where to copy from 504 * @count: The number of bytes to copy 505 * @pad: Character to use for padding if space is left in destination. 506 */ 507static inline void memcpy_and_pad(void *dest, size_t dest_len, 508 const void *src, size_t count, int pad) 509{ 510 if (dest_len > count) { 511 memcpy(dest, src, count); 512 memset(dest + count, pad, dest_len - count); 513 } else 514 memcpy(dest, src, dest_len); 515} 516 517/** 518 * str_has_prefix - Test if a string has a given prefix 519 * @str: The string to test 520 * @prefix: The string to see if @str starts with 521 * 522 * A common way to test a prefix of a string is to do: 523 * strncmp(str, prefix, sizeof(prefix) - 1) 524 * 525 * But this can lead to bugs due to typos, or if prefix is a pointer 526 * and not a constant. Instead use str_has_prefix(). 527 * 528 * Returns: 529 * * strlen(@prefix) if @str starts with @prefix 530 * * 0 if @str does not start with @prefix 531 */ 532static __always_inline size_t str_has_prefix(const char *str, const char *prefix) 533{ 534 size_t len = strlen(prefix); 535 return strncmp(str, prefix, len) == 0 ? len : 0; 536} 537 538#endif /* _LINUX_STRING_H_ */