1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STRING_H_
3 #define _LINUX_STRING_H_
6 #include <linux/compiler.h> /* for inline */
7 #include <linux/types.h> /* for size_t */
8 #include <linux/stddef.h> /* for NULL */
10 #include <uapi/linux/string.h>
12 extern char *strndup_user(const char __user
*, long);
13 extern void *memdup_user(const void __user
*, size_t);
14 extern void *vmemdup_user(const void __user
*, size_t);
15 extern void *memdup_user_nul(const void __user
*, size_t);
18 * Include machine specific inline routines
20 #include <asm/string.h>
22 #ifndef __HAVE_ARCH_STRCPY
23 extern char * strcpy(char *,const char *);
25 #ifndef __HAVE_ARCH_STRNCPY
26 extern char * strncpy(char *,const char *, __kernel_size_t
);
28 #ifndef __HAVE_ARCH_STRLCPY
29 size_t strlcpy(char *, const char *, size_t);
31 #ifndef __HAVE_ARCH_STRSCPY
32 ssize_t
strscpy(char *, const char *, size_t);
35 /* Wraps calls to strscpy()/memset(), no arch specific code required */
36 ssize_t
strscpy_pad(char *dest
, const char *src
, size_t count
);
38 #ifndef __HAVE_ARCH_STRCAT
39 extern char * strcat(char *, const char *);
41 #ifndef __HAVE_ARCH_STRNCAT
42 extern char * strncat(char *, const char *, __kernel_size_t
);
44 #ifndef __HAVE_ARCH_STRLCAT
45 extern size_t strlcat(char *, const char *, __kernel_size_t
);
47 #ifndef __HAVE_ARCH_STRCMP
48 extern int strcmp(const char *,const char *);
50 #ifndef __HAVE_ARCH_STRNCMP
51 extern int strncmp(const char *,const char *,__kernel_size_t
);
53 #ifndef __HAVE_ARCH_STRCASECMP
54 extern int strcasecmp(const char *s1
, const char *s2
);
56 #ifndef __HAVE_ARCH_STRNCASECMP
57 extern int strncasecmp(const char *s1
, const char *s2
, size_t n
);
59 #ifndef __HAVE_ARCH_STRCHR
60 extern char * strchr(const char *,int);
62 #ifndef __HAVE_ARCH_STRCHRNUL
63 extern char * strchrnul(const char *,int);
65 #ifndef __HAVE_ARCH_STRNCHR
66 extern char * strnchr(const char *, size_t, int);
68 #ifndef __HAVE_ARCH_STRRCHR
69 extern char * strrchr(const char *,int);
71 extern char * __must_check
skip_spaces(const char *);
73 extern char *strim(char *);
75 static inline __must_check
char *strstrip(char *str
)
80 #ifndef __HAVE_ARCH_STRSTR
81 extern char * strstr(const char *, const char *);
83 #ifndef __HAVE_ARCH_STRNSTR
84 extern char * strnstr(const char *, const char *, size_t);
86 #ifndef __HAVE_ARCH_STRLEN
87 extern __kernel_size_t
strlen(const char *);
89 #ifndef __HAVE_ARCH_STRNLEN
90 extern __kernel_size_t
strnlen(const char *,__kernel_size_t
);
92 #ifndef __HAVE_ARCH_STRPBRK
93 extern char * strpbrk(const char *,const char *);
95 #ifndef __HAVE_ARCH_STRSEP
96 extern char * strsep(char **,const char *);
98 #ifndef __HAVE_ARCH_STRSPN
99 extern __kernel_size_t
strspn(const char *,const char *);
101 #ifndef __HAVE_ARCH_STRCSPN
102 extern __kernel_size_t
strcspn(const char *,const char *);
105 #ifndef __HAVE_ARCH_MEMSET
106 extern void * memset(void *,int,__kernel_size_t
);
109 #ifndef __HAVE_ARCH_MEMSET16
110 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t
);
113 #ifndef __HAVE_ARCH_MEMSET32
114 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t
);
117 #ifndef __HAVE_ARCH_MEMSET64
118 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t
);
121 static inline void *memset_l(unsigned long *p
, unsigned long v
,
124 if (BITS_PER_LONG
== 32)
125 return memset32((uint32_t *)p
, v
, n
);
127 return memset64((uint64_t *)p
, v
, n
);
130 static inline void *memset_p(void **p
, void *v
, __kernel_size_t n
)
132 if (BITS_PER_LONG
== 32)
133 return memset32((uint32_t *)p
, (uintptr_t)v
, n
);
135 return memset64((uint64_t *)p
, (uintptr_t)v
, n
);
138 extern void **__memcat_p(void **a
, void **b
);
139 #define memcat_p(a, b) ({ \
140 BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
141 "type mismatch in memcat_p()"); \
142 (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
145 #ifndef __HAVE_ARCH_MEMCPY
146 extern void * memcpy(void *,const void *,__kernel_size_t
);
148 #ifndef __HAVE_ARCH_MEMMOVE
149 extern void * memmove(void *,const void *,__kernel_size_t
);
151 #ifndef __HAVE_ARCH_MEMSCAN
152 extern void * memscan(void *,int,__kernel_size_t
);
154 #ifndef __HAVE_ARCH_MEMCMP
155 extern int memcmp(const void *,const void *,__kernel_size_t
);
157 #ifndef __HAVE_ARCH_BCMP
158 extern int bcmp(const void *,const void *,__kernel_size_t
);
160 #ifndef __HAVE_ARCH_MEMCHR
161 extern void * memchr(const void *,int,__kernel_size_t
);
163 #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
164 static inline __must_check
unsigned long memcpy_mcsafe(void *dst
,
165 const void *src
, size_t cnt
)
167 memcpy(dst
, src
, cnt
);
171 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
172 static inline void memcpy_flushcache(void *dst
, const void *src
, size_t cnt
)
174 memcpy(dst
, src
, cnt
);
177 void *memchr_inv(const void *s
, int c
, size_t n
);
178 char *strreplace(char *s
, char old
, char new);
180 extern void kfree_const(const void *x
);
182 extern char *kstrdup(const char *s
, gfp_t gfp
) __malloc
;
183 extern const char *kstrdup_const(const char *s
, gfp_t gfp
);
184 extern char *kstrndup(const char *s
, size_t len
, gfp_t gfp
);
185 extern void *kmemdup(const void *src
, size_t len
, gfp_t gfp
);
186 extern char *kmemdup_nul(const char *s
, size_t len
, gfp_t gfp
);
188 extern char **argv_split(gfp_t gfp
, const char *str
, int *argcp
);
189 extern void argv_free(char **argv
);
191 extern bool sysfs_streq(const char *s1
, const char *s2
);
192 extern int kstrtobool(const char *s
, bool *res
);
193 static inline int strtobool(const char *s
, bool *res
)
195 return kstrtobool(s
, res
);
198 int match_string(const char * const *array
, size_t n
, const char *string
);
199 int __sysfs_match_string(const char * const *array
, size_t n
, const char *s
);
202 * sysfs_match_string - matches given string in an array
203 * @_a: array of strings
204 * @_s: string to match with
206 * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
208 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
210 #ifdef CONFIG_BINARY_PRINTF
211 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
);
212 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
);
213 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...) __printf(3, 4);
216 extern ssize_t
memory_read_from_buffer(void *to
, size_t count
, loff_t
*ppos
,
217 const void *from
, size_t available
);
218 extern ssize_t
memory_read_from_io_buffer(void *to
, size_t count
, loff_t
*ppos
,
219 const void *from
, size_t available
);
222 * strstarts - does @str start with @prefix?
223 * @str: string to examine
224 * @prefix: prefix to look for.
226 static inline bool strstarts(const char *str
, const char *prefix
)
228 return strncmp(str
, prefix
, strlen(prefix
)) == 0;
231 size_t memweight(const void *ptr
, size_t bytes
);
234 * memzero_explicit - Fill a region of memory (e.g. sensitive
235 * keying data) with 0s.
236 * @s: Pointer to the start of the area.
237 * @count: The size of the area.
239 * Note: usually using memset() is just fine (!), but in cases
240 * where clearing out _local_ data at the end of a scope is
241 * necessary, memzero_explicit() should be used instead in
242 * order to prevent the compiler from optimising away zeroing.
244 * memzero_explicit() doesn't need an arch-specific version as
245 * it just invokes the one of memset() implicitly.
247 static inline void memzero_explicit(void *s
, size_t count
)
254 * kbasename - return the last part of a pathname.
256 * @path: path to extract the filename from.
258 static inline const char *kbasename(const char *path
)
260 const char *tail
= strrchr(path
, '/');
261 return tail
? tail
+ 1 : path
;
264 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
265 #define __RENAME(x) __asm__(#x)
267 void fortify_panic(const char *name
) __noreturn __cold
;
268 void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
269 void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
270 void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
271 void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
273 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
276 extern void *__underlying_memchr(const void *p
, int c
, __kernel_size_t size
) __RENAME(memchr
);
277 extern int __underlying_memcmp(const void *p
, const void *q
, __kernel_size_t size
) __RENAME(memcmp
);
278 extern void *__underlying_memcpy(void *p
, const void *q
, __kernel_size_t size
) __RENAME(memcpy
);
279 extern void *__underlying_memmove(void *p
, const void *q
, __kernel_size_t size
) __RENAME(memmove
);
280 extern void *__underlying_memset(void *p
, int c
, __kernel_size_t size
) __RENAME(memset
);
281 extern char *__underlying_strcat(char *p
, const char *q
) __RENAME(strcat
);
282 extern char *__underlying_strcpy(char *p
, const char *q
) __RENAME(strcpy
);
283 extern __kernel_size_t
__underlying_strlen(const char *p
) __RENAME(strlen
);
284 extern char *__underlying_strncat(char *p
, const char *q
, __kernel_size_t count
) __RENAME(strncat
);
285 extern char *__underlying_strncpy(char *p
, const char *q
, __kernel_size_t size
) __RENAME(strncpy
);
287 #define __underlying_memchr __builtin_memchr
288 #define __underlying_memcmp __builtin_memcmp
289 #define __underlying_memcpy __builtin_memcpy
290 #define __underlying_memmove __builtin_memmove
291 #define __underlying_memset __builtin_memset
292 #define __underlying_strcat __builtin_strcat
293 #define __underlying_strcpy __builtin_strcpy
294 #define __underlying_strlen __builtin_strlen
295 #define __underlying_strncat __builtin_strncat
296 #define __underlying_strncpy __builtin_strncpy
299 __FORTIFY_INLINE
char *strncpy(char *p
, const char *q
, __kernel_size_t size
)
301 size_t p_size
= __builtin_object_size(p
, 0);
302 if (__builtin_constant_p(size
) && p_size
< size
)
305 fortify_panic(__func__
);
306 return __underlying_strncpy(p
, q
, size
);
309 __FORTIFY_INLINE
char *strcat(char *p
, const char *q
)
311 size_t p_size
= __builtin_object_size(p
, 0);
312 if (p_size
== (size_t)-1)
313 return __underlying_strcat(p
, q
);
314 if (strlcat(p
, q
, p_size
) >= p_size
)
315 fortify_panic(__func__
);
319 __FORTIFY_INLINE __kernel_size_t
strlen(const char *p
)
322 size_t p_size
= __builtin_object_size(p
, 0);
324 /* Work around gcc excess stack consumption issue */
325 if (p_size
== (size_t)-1 ||
326 (__builtin_constant_p(p
[p_size
- 1]) && p
[p_size
- 1] == '\0'))
327 return __underlying_strlen(p
);
328 ret
= strnlen(p
, p_size
);
330 fortify_panic(__func__
);
334 extern __kernel_size_t
__real_strnlen(const char *, __kernel_size_t
) __RENAME(strnlen
);
335 __FORTIFY_INLINE __kernel_size_t
strnlen(const char *p
, __kernel_size_t maxlen
)
337 size_t p_size
= __builtin_object_size(p
, 0);
338 __kernel_size_t ret
= __real_strnlen(p
, maxlen
< p_size
? maxlen
: p_size
);
339 if (p_size
<= ret
&& maxlen
!= ret
)
340 fortify_panic(__func__
);
344 /* defined after fortified strlen to reuse it */
345 extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy
);
346 __FORTIFY_INLINE
size_t strlcpy(char *p
, const char *q
, size_t size
)
349 size_t p_size
= __builtin_object_size(p
, 0);
350 size_t q_size
= __builtin_object_size(q
, 0);
351 if (p_size
== (size_t)-1 && q_size
== (size_t)-1)
352 return __real_strlcpy(p
, q
, size
);
355 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
356 if (__builtin_constant_p(len
) && len
>= p_size
)
359 fortify_panic(__func__
);
360 __underlying_memcpy(p
, q
, len
);
366 /* defined after fortified strlen and strnlen to reuse them */
367 __FORTIFY_INLINE
char *strncat(char *p
, const char *q
, __kernel_size_t count
)
369 size_t p_len
, copy_len
;
370 size_t p_size
= __builtin_object_size(p
, 0);
371 size_t q_size
= __builtin_object_size(q
, 0);
372 if (p_size
== (size_t)-1 && q_size
== (size_t)-1)
373 return __underlying_strncat(p
, q
, count
);
375 copy_len
= strnlen(q
, count
);
376 if (p_size
< p_len
+ copy_len
+ 1)
377 fortify_panic(__func__
);
378 __underlying_memcpy(p
+ p_len
, q
, copy_len
);
379 p
[p_len
+ copy_len
] = '\0';
383 __FORTIFY_INLINE
void *memset(void *p
, int c
, __kernel_size_t size
)
385 size_t p_size
= __builtin_object_size(p
, 0);
386 if (__builtin_constant_p(size
) && p_size
< size
)
389 fortify_panic(__func__
);
390 return __underlying_memset(p
, c
, size
);
393 __FORTIFY_INLINE
void *memcpy(void *p
, const void *q
, __kernel_size_t size
)
395 size_t p_size
= __builtin_object_size(p
, 0);
396 size_t q_size
= __builtin_object_size(q
, 0);
397 if (__builtin_constant_p(size
)) {
403 if (p_size
< size
|| q_size
< size
)
404 fortify_panic(__func__
);
405 return __underlying_memcpy(p
, q
, size
);
408 __FORTIFY_INLINE
void *memmove(void *p
, const void *q
, __kernel_size_t size
)
410 size_t p_size
= __builtin_object_size(p
, 0);
411 size_t q_size
= __builtin_object_size(q
, 0);
412 if (__builtin_constant_p(size
)) {
418 if (p_size
< size
|| q_size
< size
)
419 fortify_panic(__func__
);
420 return __underlying_memmove(p
, q
, size
);
423 extern void *__real_memscan(void *, int, __kernel_size_t
) __RENAME(memscan
);
424 __FORTIFY_INLINE
void *memscan(void *p
, int c
, __kernel_size_t size
)
426 size_t p_size
= __builtin_object_size(p
, 0);
427 if (__builtin_constant_p(size
) && p_size
< size
)
430 fortify_panic(__func__
);
431 return __real_memscan(p
, c
, size
);
434 __FORTIFY_INLINE
int memcmp(const void *p
, const void *q
, __kernel_size_t size
)
436 size_t p_size
= __builtin_object_size(p
, 0);
437 size_t q_size
= __builtin_object_size(q
, 0);
438 if (__builtin_constant_p(size
)) {
444 if (p_size
< size
|| q_size
< size
)
445 fortify_panic(__func__
);
446 return __underlying_memcmp(p
, q
, size
);
449 __FORTIFY_INLINE
void *memchr(const void *p
, int c
, __kernel_size_t size
)
451 size_t p_size
= __builtin_object_size(p
, 0);
452 if (__builtin_constant_p(size
) && p_size
< size
)
455 fortify_panic(__func__
);
456 return __underlying_memchr(p
, c
, size
);
459 void *__real_memchr_inv(const void *s
, int c
, size_t n
) __RENAME(memchr_inv
);
460 __FORTIFY_INLINE
void *memchr_inv(const void *p
, int c
, size_t size
)
462 size_t p_size
= __builtin_object_size(p
, 0);
463 if (__builtin_constant_p(size
) && p_size
< size
)
466 fortify_panic(__func__
);
467 return __real_memchr_inv(p
, c
, size
);
470 extern void *__real_kmemdup(const void *src
, size_t len
, gfp_t gfp
) __RENAME(kmemdup
);
471 __FORTIFY_INLINE
void *kmemdup(const void *p
, size_t size
, gfp_t gfp
)
473 size_t p_size
= __builtin_object_size(p
, 0);
474 if (__builtin_constant_p(size
) && p_size
< size
)
477 fortify_panic(__func__
);
478 return __real_kmemdup(p
, size
, gfp
);
481 /* defined after fortified strlen and memcpy to reuse them */
482 __FORTIFY_INLINE
char *strcpy(char *p
, const char *q
)
484 size_t p_size
= __builtin_object_size(p
, 0);
485 size_t q_size
= __builtin_object_size(q
, 0);
486 if (p_size
== (size_t)-1 && q_size
== (size_t)-1)
487 return __underlying_strcpy(p
, q
);
488 memcpy(p
, q
, strlen(q
) + 1);
492 /* Don't use these outside the FORITFY_SOURCE implementation */
493 #undef __underlying_memchr
494 #undef __underlying_memcmp
495 #undef __underlying_memcpy
496 #undef __underlying_memmove
497 #undef __underlying_memset
498 #undef __underlying_strcat
499 #undef __underlying_strcpy
500 #undef __underlying_strlen
501 #undef __underlying_strncat
502 #undef __underlying_strncpy
506 * memcpy_and_pad - Copy one buffer to another with padding
507 * @dest: Where to copy to
508 * @dest_len: The destination buffer size
509 * @src: Where to copy from
510 * @count: The number of bytes to copy
511 * @pad: Character to use for padding if space is left in destination.
513 static inline void memcpy_and_pad(void *dest
, size_t dest_len
,
514 const void *src
, size_t count
, int pad
)
516 if (dest_len
> count
) {
517 memcpy(dest
, src
, count
);
518 memset(dest
+ count
, pad
, dest_len
- count
);
520 memcpy(dest
, src
, dest_len
);
524 * str_has_prefix - Test if a string has a given prefix
525 * @str: The string to test
526 * @prefix: The string to see if @str starts with
528 * A common way to test a prefix of a string is to do:
529 * strncmp(str, prefix, sizeof(prefix) - 1)
531 * But this can lead to bugs due to typos, or if prefix is a pointer
532 * and not a constant. Instead use str_has_prefix().
535 * * strlen(@prefix) if @str starts with @prefix
536 * * 0 if @str does not start with @prefix
538 static __always_inline
size_t str_has_prefix(const char *str
, const char *prefix
)
540 size_t len
= strlen(prefix
);
541 return strncmp(str
, prefix
, len
) == 0 ? len
: 0;
544 #endif /* _LINUX_STRING_H_ */