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