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 *memdup_user_nul(const void __user *, size_t);
17 * Include machine specific inline routines
19 #include <asm/string.h>
21 #ifndef __HAVE_ARCH_STRCPY
22 extern char * strcpy(char *,const char *);
24 #ifndef __HAVE_ARCH_STRNCPY
25 extern char * strncpy(char *,const char *, __kernel_size_t);
27 #ifndef __HAVE_ARCH_STRLCPY
28 size_t strlcpy(char *, const char *, size_t);
30 #ifndef __HAVE_ARCH_STRSCPY
31 ssize_t strscpy(char *, const char *, size_t);
34 /* Wraps calls to strscpy()/memset(), no arch specific code required */
35 ssize_t strscpy_pad(char *dest, const char *src, size_t count);
37 #ifndef __HAVE_ARCH_STRCAT
38 extern char * strcat(char *, const char *);
40 #ifndef __HAVE_ARCH_STRNCAT
41 extern char * strncat(char *, const char *, __kernel_size_t);
43 #ifndef __HAVE_ARCH_STRLCAT
44 extern size_t strlcat(char *, const char *, __kernel_size_t);
46 #ifndef __HAVE_ARCH_STRCMP
47 extern int strcmp(const char *,const char *);
49 #ifndef __HAVE_ARCH_STRNCMP
50 extern int strncmp(const char *,const char *,__kernel_size_t);
52 #ifndef __HAVE_ARCH_STRCASECMP
53 extern int strcasecmp(const char *s1, const char *s2);
55 #ifndef __HAVE_ARCH_STRNCASECMP
56 extern int strncasecmp(const char *s1, const char *s2, size_t n);
58 #ifndef __HAVE_ARCH_STRCHR
59 extern char * strchr(const char *,int);
61 #ifndef __HAVE_ARCH_STRCHRNUL
62 extern char * strchrnul(const char *,int);
64 #ifndef __HAVE_ARCH_STRNCHR
65 extern char * strnchr(const char *, size_t, int);
67 #ifndef __HAVE_ARCH_STRRCHR
68 extern char * strrchr(const char *,int);
70 extern char * __must_check skip_spaces(const char *);
72 extern char *strim(char *);
74 static inline __must_check char *strstrip(char *str)
79 #ifndef __HAVE_ARCH_STRSTR
80 extern char * strstr(const char *, const char *);
82 #ifndef __HAVE_ARCH_STRNSTR
83 extern char * strnstr(const char *, const char *, size_t);
85 #ifndef __HAVE_ARCH_STRLEN
86 extern __kernel_size_t strlen(const char *);
88 #ifndef __HAVE_ARCH_STRNLEN
89 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
91 #ifndef __HAVE_ARCH_STRPBRK
92 extern char * strpbrk(const char *,const char *);
94 #ifndef __HAVE_ARCH_STRSEP
95 extern char * strsep(char **,const char *);
97 #ifndef __HAVE_ARCH_STRSPN
98 extern __kernel_size_t strspn(const char *,const char *);
100 #ifndef __HAVE_ARCH_STRCSPN
101 extern __kernel_size_t strcspn(const char *,const char *);
104 #ifndef __HAVE_ARCH_MEMSET
105 extern void * memset(void *,int,__kernel_size_t);
108 #ifndef __HAVE_ARCH_MEMSET16
109 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
112 #ifndef __HAVE_ARCH_MEMSET32
113 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
116 #ifndef __HAVE_ARCH_MEMSET64
117 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
120 static inline void *memset_l(unsigned long *p, unsigned long v,
123 if (BITS_PER_LONG == 32)
124 return memset32((uint32_t *)p, v, n);
126 return memset64((uint64_t *)p, v, n);
129 static inline void *memset_p(void **p, void *v, __kernel_size_t n)
131 if (BITS_PER_LONG == 32)
132 return memset32((uint32_t *)p, (uintptr_t)v, n);
134 return memset64((uint64_t *)p, (uintptr_t)v, n);
137 #ifndef __HAVE_ARCH_MEMCPY
138 extern void * memcpy(void *,const void *,__kernel_size_t);
140 #ifndef __HAVE_ARCH_MEMMOVE
141 extern void * memmove(void *,const void *,__kernel_size_t);
143 #ifndef __HAVE_ARCH_MEMSCAN
144 extern void * memscan(void *,int,__kernel_size_t);
146 #ifndef __HAVE_ARCH_MEMCMP
147 extern int memcmp(const void *,const void *,__kernel_size_t);
149 #ifndef __HAVE_ARCH_BCMP
150 extern int bcmp(const void *,const void *,__kernel_size_t);
152 #ifndef __HAVE_ARCH_MEMCHR
153 extern void * memchr(const void *,int,__kernel_size_t);
155 #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
156 static inline __must_check int memcpy_mcsafe(void *dst, const void *src,
159 memcpy(dst, src, cnt);
163 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
164 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
166 memcpy(dst, src, cnt);
169 void *memchr_inv(const void *s, int c, size_t n);
170 char *strreplace(char *s, char old, char new);
172 extern void kfree_const(const void *x);
174 extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
175 extern const char *kstrdup_const(const char *s, gfp_t gfp);
176 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
177 extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
178 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
180 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
181 extern void argv_free(char **argv);
183 extern bool sysfs_streq(const char *s1, const char *s2);
184 extern int kstrtobool(const char *s, bool *res);
185 static inline int strtobool(const char *s, bool *res)
187 return kstrtobool(s, res);
190 int match_string(const char * const *array, size_t n, const char *string);
191 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
194 * sysfs_match_string - matches given string in an array
195 * @_a: array of strings
196 * @_s: string to match with
198 * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
200 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
202 #ifdef CONFIG_BINARY_PRINTF
203 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
204 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
205 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
208 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
209 const void *from, size_t available);
212 * strstarts - does @str start with @prefix?
213 * @str: string to examine
214 * @prefix: prefix to look for.
216 static inline bool strstarts(const char *str, const char *prefix)
218 return strncmp(str, prefix, strlen(prefix)) == 0;
221 size_t memweight(const void *ptr, size_t bytes);
222 void memzero_explicit(void *s, size_t count);
225 * kbasename - return the last part of a pathname.
227 * @path: path to extract the filename from.
229 static inline const char *kbasename(const char *path)
231 const char *tail = strrchr(path, '/');
232 return tail ? tail + 1 : path;
235 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
236 #define __RENAME(x) __asm__(#x)
238 void fortify_panic(const char *name) __noreturn __cold;
239 void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
240 void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
241 void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
242 void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
244 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
247 extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
248 extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
249 extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
250 extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
251 extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
252 extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
253 extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
254 extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
255 extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
256 extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
258 #define __underlying_memchr __builtin_memchr
259 #define __underlying_memcmp __builtin_memcmp
260 #define __underlying_memcpy __builtin_memcpy
261 #define __underlying_memmove __builtin_memmove
262 #define __underlying_memset __builtin_memset
263 #define __underlying_strcat __builtin_strcat
264 #define __underlying_strcpy __builtin_strcpy
265 #define __underlying_strlen __builtin_strlen
266 #define __underlying_strncat __builtin_strncat
267 #define __underlying_strncpy __builtin_strncpy
270 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
272 size_t p_size = __builtin_object_size(p, 0);
273 if (__builtin_constant_p(size) && p_size < size)
276 fortify_panic(__func__);
277 return __underlying_strncpy(p, q, size);
280 __FORTIFY_INLINE char *strcat(char *p, const char *q)
282 size_t p_size = __builtin_object_size(p, 0);
283 if (p_size == (size_t)-1)
284 return __underlying_strcat(p, q);
285 if (strlcat(p, q, p_size) >= p_size)
286 fortify_panic(__func__);
290 __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
293 size_t p_size = __builtin_object_size(p, 0);
295 /* Work around gcc excess stack consumption issue */
296 if (p_size == (size_t)-1 ||
297 (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0'))
298 return __underlying_strlen(p);
299 ret = strnlen(p, p_size);
301 fortify_panic(__func__);
305 extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
306 __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
308 size_t p_size = __builtin_object_size(p, 0);
309 __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
310 if (p_size <= ret && maxlen != ret)
311 fortify_panic(__func__);
315 /* defined after fortified strlen to reuse it */
316 extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
317 __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
320 size_t p_size = __builtin_object_size(p, 0);
321 size_t q_size = __builtin_object_size(q, 0);
322 if (p_size == (size_t)-1 && q_size == (size_t)-1)
323 return __real_strlcpy(p, q, size);
326 size_t len = (ret >= size) ? size - 1 : ret;
327 if (__builtin_constant_p(len) && len >= p_size)
330 fortify_panic(__func__);
331 __underlying_memcpy(p, q, len);
337 /* defined after fortified strlen and strnlen to reuse them */
338 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
340 size_t p_len, copy_len;
341 size_t p_size = __builtin_object_size(p, 0);
342 size_t q_size = __builtin_object_size(q, 0);
343 if (p_size == (size_t)-1 && q_size == (size_t)-1)
344 return __underlying_strncat(p, q, count);
346 copy_len = strnlen(q, count);
347 if (p_size < p_len + copy_len + 1)
348 fortify_panic(__func__);
349 __underlying_memcpy(p + p_len, q, copy_len);
350 p[p_len + copy_len] = '\0';
354 __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
356 size_t p_size = __builtin_object_size(p, 0);
357 if (__builtin_constant_p(size) && p_size < size)
360 fortify_panic(__func__);
361 return __underlying_memset(p, c, size);
364 __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
366 size_t p_size = __builtin_object_size(p, 0);
367 size_t q_size = __builtin_object_size(q, 0);
368 if (__builtin_constant_p(size)) {
374 if (p_size < size || q_size < size)
375 fortify_panic(__func__);
376 return __underlying_memcpy(p, q, size);
379 __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
381 size_t p_size = __builtin_object_size(p, 0);
382 size_t q_size = __builtin_object_size(q, 0);
383 if (__builtin_constant_p(size)) {
389 if (p_size < size || q_size < size)
390 fortify_panic(__func__);
391 return __underlying_memmove(p, q, size);
394 extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
395 __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
397 size_t p_size = __builtin_object_size(p, 0);
398 if (__builtin_constant_p(size) && p_size < size)
401 fortify_panic(__func__);
402 return __real_memscan(p, c, size);
405 __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
407 size_t p_size = __builtin_object_size(p, 0);
408 size_t q_size = __builtin_object_size(q, 0);
409 if (__builtin_constant_p(size)) {
415 if (p_size < size || q_size < size)
416 fortify_panic(__func__);
417 return __underlying_memcmp(p, q, size);
420 __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
422 size_t p_size = __builtin_object_size(p, 0);
423 if (__builtin_constant_p(size) && p_size < size)
426 fortify_panic(__func__);
427 return __underlying_memchr(p, c, size);
430 void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
431 __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
433 size_t p_size = __builtin_object_size(p, 0);
434 if (__builtin_constant_p(size) && p_size < size)
437 fortify_panic(__func__);
438 return __real_memchr_inv(p, c, size);
441 extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
442 __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
444 size_t p_size = __builtin_object_size(p, 0);
445 if (__builtin_constant_p(size) && p_size < size)
448 fortify_panic(__func__);
449 return __real_kmemdup(p, size, gfp);
452 /* defined after fortified strlen and memcpy to reuse them */
453 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
455 size_t p_size = __builtin_object_size(p, 0);
456 size_t q_size = __builtin_object_size(q, 0);
457 if (p_size == (size_t)-1 && q_size == (size_t)-1)
458 return __underlying_strcpy(p, q);
459 memcpy(p, q, strlen(q) + 1);
463 /* Don't use these outside the FORITFY_SOURCE implementation */
464 #undef __underlying_memchr
465 #undef __underlying_memcmp
466 #undef __underlying_memcpy
467 #undef __underlying_memmove
468 #undef __underlying_memset
469 #undef __underlying_strcat
470 #undef __underlying_strcpy
471 #undef __underlying_strlen
472 #undef __underlying_strncat
473 #undef __underlying_strncpy
477 * memcpy_and_pad - Copy one buffer to another with padding
478 * @dest: Where to copy to
479 * @dest_len: The destination buffer size
480 * @src: Where to copy from
481 * @count: The number of bytes to copy
482 * @pad: Character to use for padding if space is left in destination.
484 static inline void memcpy_and_pad(void *dest, size_t dest_len,
485 const void *src, size_t count, int pad)
487 if (dest_len > count) {
488 memcpy(dest, src, count);
489 memset(dest + count, pad, dest_len - count);
491 memcpy(dest, src, dest_len);
494 #endif /* _LINUX_STRING_H_ */