GNU Linux-libre 4.14.302-gnu1
[releases.git] / include / linux / string.h
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
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);
15
16 /*
17  * Include machine specific inline routines
18  */
19 #include <asm/string.h>
20
21 #ifndef __HAVE_ARCH_STRCPY
22 extern char * strcpy(char *,const char *);
23 #endif
24 #ifndef __HAVE_ARCH_STRNCPY
25 extern char * strncpy(char *,const char *, __kernel_size_t);
26 #endif
27 #ifndef __HAVE_ARCH_STRLCPY
28 size_t strlcpy(char *, const char *, size_t);
29 #endif
30 #ifndef __HAVE_ARCH_STRSCPY
31 ssize_t strscpy(char *, const char *, size_t);
32 #endif
33
34 /* Wraps calls to strscpy()/memset(), no arch specific code required */
35 ssize_t strscpy_pad(char *dest, const char *src, size_t count);
36
37 #ifndef __HAVE_ARCH_STRCAT
38 extern char * strcat(char *, const char *);
39 #endif
40 #ifndef __HAVE_ARCH_STRNCAT
41 extern char * strncat(char *, const char *, __kernel_size_t);
42 #endif
43 #ifndef __HAVE_ARCH_STRLCAT
44 extern size_t strlcat(char *, const char *, __kernel_size_t);
45 #endif
46 #ifndef __HAVE_ARCH_STRCMP
47 extern int strcmp(const char *,const char *);
48 #endif
49 #ifndef __HAVE_ARCH_STRNCMP
50 extern int strncmp(const char *,const char *,__kernel_size_t);
51 #endif
52 #ifndef __HAVE_ARCH_STRCASECMP
53 extern int strcasecmp(const char *s1, const char *s2);
54 #endif
55 #ifndef __HAVE_ARCH_STRNCASECMP
56 extern int strncasecmp(const char *s1, const char *s2, size_t n);
57 #endif
58 #ifndef __HAVE_ARCH_STRCHR
59 extern char * strchr(const char *,int);
60 #endif
61 #ifndef __HAVE_ARCH_STRCHRNUL
62 extern char * strchrnul(const char *,int);
63 #endif
64 #ifndef __HAVE_ARCH_STRNCHR
65 extern char * strnchr(const char *, size_t, int);
66 #endif
67 #ifndef __HAVE_ARCH_STRRCHR
68 extern char * strrchr(const char *,int);
69 #endif
70 extern char * __must_check skip_spaces(const char *);
71
72 extern char *strim(char *);
73
74 static inline __must_check char *strstrip(char *str)
75 {
76         return strim(str);
77 }
78
79 #ifndef __HAVE_ARCH_STRSTR
80 extern char * strstr(const char *, const char *);
81 #endif
82 #ifndef __HAVE_ARCH_STRNSTR
83 extern char * strnstr(const char *, const char *, size_t);
84 #endif
85 #ifndef __HAVE_ARCH_STRLEN
86 extern __kernel_size_t strlen(const char *);
87 #endif
88 #ifndef __HAVE_ARCH_STRNLEN
89 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
90 #endif
91 #ifndef __HAVE_ARCH_STRPBRK
92 extern char * strpbrk(const char *,const char *);
93 #endif
94 #ifndef __HAVE_ARCH_STRSEP
95 extern char * strsep(char **,const char *);
96 #endif
97 #ifndef __HAVE_ARCH_STRSPN
98 extern __kernel_size_t strspn(const char *,const char *);
99 #endif
100 #ifndef __HAVE_ARCH_STRCSPN
101 extern __kernel_size_t strcspn(const char *,const char *);
102 #endif
103
104 #ifndef __HAVE_ARCH_MEMSET
105 extern void * memset(void *,int,__kernel_size_t);
106 #endif
107
108 #ifndef __HAVE_ARCH_MEMSET16
109 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
110 #endif
111
112 #ifndef __HAVE_ARCH_MEMSET32
113 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
114 #endif
115
116 #ifndef __HAVE_ARCH_MEMSET64
117 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
118 #endif
119
120 static inline void *memset_l(unsigned long *p, unsigned long v,
121                 __kernel_size_t n)
122 {
123         if (BITS_PER_LONG == 32)
124                 return memset32((uint32_t *)p, v, n);
125         else
126                 return memset64((uint64_t *)p, v, n);
127 }
128
129 static inline void *memset_p(void **p, void *v, __kernel_size_t n)
130 {
131         if (BITS_PER_LONG == 32)
132                 return memset32((uint32_t *)p, (uintptr_t)v, n);
133         else
134                 return memset64((uint64_t *)p, (uintptr_t)v, n);
135 }
136
137 #ifndef __HAVE_ARCH_MEMCPY
138 extern void * memcpy(void *,const void *,__kernel_size_t);
139 #endif
140 #ifndef __HAVE_ARCH_MEMMOVE
141 extern void * memmove(void *,const void *,__kernel_size_t);
142 #endif
143 #ifndef __HAVE_ARCH_MEMSCAN
144 extern void * memscan(void *,int,__kernel_size_t);
145 #endif
146 #ifndef __HAVE_ARCH_MEMCMP
147 extern int memcmp(const void *,const void *,__kernel_size_t);
148 #endif
149 #ifndef __HAVE_ARCH_BCMP
150 extern int bcmp(const void *,const void *,__kernel_size_t);
151 #endif
152 #ifndef __HAVE_ARCH_MEMCHR
153 extern void * memchr(const void *,int,__kernel_size_t);
154 #endif
155 #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
156 static inline __must_check int memcpy_mcsafe(void *dst, const void *src,
157                 size_t cnt)
158 {
159         memcpy(dst, src, cnt);
160         return 0;
161 }
162 #endif
163 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
164 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
165 {
166         memcpy(dst, src, cnt);
167 }
168 #endif
169 void *memchr_inv(const void *s, int c, size_t n);
170 char *strreplace(char *s, char old, char new);
171
172 extern void kfree_const(const void *x);
173
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);
179
180 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
181 extern void argv_free(char **argv);
182
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)
186 {
187         return kstrtobool(s, res);
188 }
189
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);
192
193 /**
194  * sysfs_match_string - matches given string in an array
195  * @_a: array of strings
196  * @_s: string to match with
197  *
198  * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
199  */
200 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
201
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);
206 #endif
207
208 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
209                                        const void *from, size_t available);
210
211 /**
212  * strstarts - does @str start with @prefix?
213  * @str: string to examine
214  * @prefix: prefix to look for.
215  */
216 static inline bool strstarts(const char *str, const char *prefix)
217 {
218         return strncmp(str, prefix, strlen(prefix)) == 0;
219 }
220
221 size_t memweight(const void *ptr, size_t bytes);
222 void memzero_explicit(void *s, size_t count);
223
224 /**
225  * kbasename - return the last part of a pathname.
226  *
227  * @path: path to extract the filename from.
228  */
229 static inline const char *kbasename(const char *path)
230 {
231         const char *tail = strrchr(path, '/');
232         return tail ? tail + 1 : path;
233 }
234
235 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
236 #define __RENAME(x) __asm__(#x)
237
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");
243
244 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
245
246 #ifdef CONFIG_KASAN
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);
257 #else
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
268 #endif
269
270 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
271 {
272         size_t p_size = __builtin_object_size(p, 0);
273         if (__builtin_constant_p(size) && p_size < size)
274                 __write_overflow();
275         if (p_size < size)
276                 fortify_panic(__func__);
277         return __underlying_strncpy(p, q, size);
278 }
279
280 __FORTIFY_INLINE char *strcat(char *p, const char *q)
281 {
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__);
287         return p;
288 }
289
290 __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
291 {
292         __kernel_size_t ret;
293         size_t p_size = __builtin_object_size(p, 0);
294
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);
300         if (p_size <= ret)
301                 fortify_panic(__func__);
302         return ret;
303 }
304
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)
307 {
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__);
312         return ret;
313 }
314
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)
318 {
319         size_t ret;
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);
324         ret = strlen(q);
325         if (size) {
326                 size_t len = (ret >= size) ? size - 1 : ret;
327                 if (__builtin_constant_p(len) && len >= p_size)
328                         __write_overflow();
329                 if (len >= p_size)
330                         fortify_panic(__func__);
331                 __underlying_memcpy(p, q, len);
332                 p[len] = '\0';
333         }
334         return ret;
335 }
336
337 /* defined after fortified strlen and strnlen to reuse them */
338 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
339 {
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);
345         p_len = strlen(p);
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';
351         return p;
352 }
353
354 __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
355 {
356         size_t p_size = __builtin_object_size(p, 0);
357         if (__builtin_constant_p(size) && p_size < size)
358                 __write_overflow();
359         if (p_size < size)
360                 fortify_panic(__func__);
361         return __underlying_memset(p, c, size);
362 }
363
364 __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
365 {
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)) {
369                 if (p_size < size)
370                         __write_overflow();
371                 if (q_size < size)
372                         __read_overflow2();
373         }
374         if (p_size < size || q_size < size)
375                 fortify_panic(__func__);
376         return __underlying_memcpy(p, q, size);
377 }
378
379 __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
380 {
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)) {
384                 if (p_size < size)
385                         __write_overflow();
386                 if (q_size < size)
387                         __read_overflow2();
388         }
389         if (p_size < size || q_size < size)
390                 fortify_panic(__func__);
391         return __underlying_memmove(p, q, size);
392 }
393
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)
396 {
397         size_t p_size = __builtin_object_size(p, 0);
398         if (__builtin_constant_p(size) && p_size < size)
399                 __read_overflow();
400         if (p_size < size)
401                 fortify_panic(__func__);
402         return __real_memscan(p, c, size);
403 }
404
405 __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
406 {
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)) {
410                 if (p_size < size)
411                         __read_overflow();
412                 if (q_size < size)
413                         __read_overflow2();
414         }
415         if (p_size < size || q_size < size)
416                 fortify_panic(__func__);
417         return __underlying_memcmp(p, q, size);
418 }
419
420 __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
421 {
422         size_t p_size = __builtin_object_size(p, 0);
423         if (__builtin_constant_p(size) && p_size < size)
424                 __read_overflow();
425         if (p_size < size)
426                 fortify_panic(__func__);
427         return __underlying_memchr(p, c, size);
428 }
429
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)
432 {
433         size_t p_size = __builtin_object_size(p, 0);
434         if (__builtin_constant_p(size) && p_size < size)
435                 __read_overflow();
436         if (p_size < size)
437                 fortify_panic(__func__);
438         return __real_memchr_inv(p, c, size);
439 }
440
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)
443 {
444         size_t p_size = __builtin_object_size(p, 0);
445         if (__builtin_constant_p(size) && p_size < size)
446                 __read_overflow();
447         if (p_size < size)
448                 fortify_panic(__func__);
449         return __real_kmemdup(p, size, gfp);
450 }
451
452 /* defined after fortified strlen and memcpy to reuse them */
453 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
454 {
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);
460         return p;
461 }
462
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
474 #endif
475
476 /**
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.
483  */
484 static inline void memcpy_and_pad(void *dest, size_t dest_len,
485                                   const void *src, size_t count, int pad)
486 {
487         if (dest_len > count) {
488                 memcpy(dest, src, count);
489                 memset(dest + count, pad,  dest_len - count);
490         } else
491                 memcpy(dest, src, dest_len);
492 }
493
494 #endif /* _LINUX_STRING_H_ */