GNU Linux-libre 4.19.245-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 *vmemdup_user(const void __user *, size_t);
15 extern 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
23 extern char * strcpy(char *,const char *);
24 #endif
25 #ifndef __HAVE_ARCH_STRNCPY
26 extern char * strncpy(char *,const char *, __kernel_size_t);
27 #endif
28 #ifndef __HAVE_ARCH_STRLCPY
29 size_t strlcpy(char *, const char *, size_t);
30 #endif
31 #ifndef __HAVE_ARCH_STRSCPY
32 ssize_t strscpy(char *, const char *, size_t);
33 #endif
34
35 /* Wraps calls to strscpy()/memset(), no arch specific code required */
36 ssize_t strscpy_pad(char *dest, const char *src, size_t count);
37
38 #ifndef __HAVE_ARCH_STRCAT
39 extern char * strcat(char *, const char *);
40 #endif
41 #ifndef __HAVE_ARCH_STRNCAT
42 extern char * strncat(char *, const char *, __kernel_size_t);
43 #endif
44 #ifndef __HAVE_ARCH_STRLCAT
45 extern size_t strlcat(char *, const char *, __kernel_size_t);
46 #endif
47 #ifndef __HAVE_ARCH_STRCMP
48 extern int strcmp(const char *,const char *);
49 #endif
50 #ifndef __HAVE_ARCH_STRNCMP
51 extern int strncmp(const char *,const char *,__kernel_size_t);
52 #endif
53 #ifndef __HAVE_ARCH_STRCASECMP
54 extern int strcasecmp(const char *s1, const char *s2);
55 #endif
56 #ifndef __HAVE_ARCH_STRNCASECMP
57 extern int strncasecmp(const char *s1, const char *s2, size_t n);
58 #endif
59 #ifndef __HAVE_ARCH_STRCHR
60 extern char * strchr(const char *,int);
61 #endif
62 #ifndef __HAVE_ARCH_STRCHRNUL
63 extern char * strchrnul(const char *,int);
64 #endif
65 #ifndef __HAVE_ARCH_STRNCHR
66 extern char * strnchr(const char *, size_t, int);
67 #endif
68 #ifndef __HAVE_ARCH_STRRCHR
69 extern char * strrchr(const char *,int);
70 #endif
71 extern char * __must_check skip_spaces(const char *);
72
73 extern char *strim(char *);
74
75 static inline __must_check char *strstrip(char *str)
76 {
77         return strim(str);
78 }
79
80 #ifndef __HAVE_ARCH_STRSTR
81 extern char * strstr(const char *, const char *);
82 #endif
83 #ifndef __HAVE_ARCH_STRNSTR
84 extern char * strnstr(const char *, const char *, size_t);
85 #endif
86 #ifndef __HAVE_ARCH_STRLEN
87 extern __kernel_size_t strlen(const char *);
88 #endif
89 #ifndef __HAVE_ARCH_STRNLEN
90 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
91 #endif
92 #ifndef __HAVE_ARCH_STRPBRK
93 extern char * strpbrk(const char *,const char *);
94 #endif
95 #ifndef __HAVE_ARCH_STRSEP
96 extern char * strsep(char **,const char *);
97 #endif
98 #ifndef __HAVE_ARCH_STRSPN
99 extern __kernel_size_t strspn(const char *,const char *);
100 #endif
101 #ifndef __HAVE_ARCH_STRCSPN
102 extern __kernel_size_t strcspn(const char *,const char *);
103 #endif
104
105 #ifndef __HAVE_ARCH_MEMSET
106 extern void * memset(void *,int,__kernel_size_t);
107 #endif
108
109 #ifndef __HAVE_ARCH_MEMSET16
110 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
111 #endif
112
113 #ifndef __HAVE_ARCH_MEMSET32
114 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
115 #endif
116
117 #ifndef __HAVE_ARCH_MEMSET64
118 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
119 #endif
120
121 static 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
130 static 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
139 extern void * memcpy(void *,const void *,__kernel_size_t);
140 #endif
141 #ifndef __HAVE_ARCH_MEMMOVE
142 extern void * memmove(void *,const void *,__kernel_size_t);
143 #endif
144 #ifndef __HAVE_ARCH_MEMSCAN
145 extern void * memscan(void *,int,__kernel_size_t);
146 #endif
147 #ifndef __HAVE_ARCH_MEMCMP
148 extern int memcmp(const void *,const void *,__kernel_size_t);
149 #endif
150 #ifndef __HAVE_ARCH_BCMP
151 extern int bcmp(const void *,const void *,__kernel_size_t);
152 #endif
153 #ifndef __HAVE_ARCH_MEMCHR
154 extern void * memchr(const void *,int,__kernel_size_t);
155 #endif
156 #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
157 static inline __must_check unsigned long memcpy_mcsafe(void *dst,
158                 const void *src, size_t cnt)
159 {
160         memcpy(dst, src, cnt);
161         return 0;
162 }
163 #endif
164 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
165 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
166 {
167         memcpy(dst, src, cnt);
168 }
169 #endif
170 void *memchr_inv(const void *s, int c, size_t n);
171 char *strreplace(char *s, char old, char new);
172
173 extern void kfree_const(const void *x);
174
175 extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
176 extern const char *kstrdup_const(const char *s, gfp_t gfp);
177 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
178 extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
179 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
180
181 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
182 extern void argv_free(char **argv);
183
184 extern bool sysfs_streq(const char *s1, const char *s2);
185 extern int kstrtobool(const char *s, bool *res);
186 static inline int strtobool(const char *s, bool *res)
187 {
188         return kstrtobool(s, res);
189 }
190
191 int match_string(const char * const *array, size_t n, const char *string);
192 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
193
194 /**
195  * sysfs_match_string - matches given string in an array
196  * @_a: array of strings
197  * @_s: string to match with
198  *
199  * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
200  */
201 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
202
203 #ifdef CONFIG_BINARY_PRINTF
204 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
205 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
206 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
207 #endif
208
209 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
210                                        const void *from, size_t available);
211
212 /**
213  * strstarts - does @str start with @prefix?
214  * @str: string to examine
215  * @prefix: prefix to look for.
216  */
217 static inline bool strstarts(const char *str, const char *prefix)
218 {
219         return strncmp(str, prefix, strlen(prefix)) == 0;
220 }
221
222 size_t memweight(const void *ptr, size_t bytes);
223 void memzero_explicit(void *s, size_t count);
224
225 /**
226  * kbasename - return the last part of a pathname.
227  *
228  * @path: path to extract the filename from.
229  */
230 static inline const char *kbasename(const char *path)
231 {
232         const char *tail = strrchr(path, '/');
233         return tail ? tail + 1 : path;
234 }
235
236 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
237 #define __RENAME(x) __asm__(#x)
238
239 void fortify_panic(const char *name) __noreturn __cold;
240 void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
241 void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
242 void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
243 void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
244
245 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
246
247 #ifdef CONFIG_KASAN
248 extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
249 extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
250 extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
251 extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
252 extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
253 extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
254 extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
255 extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
256 extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
257 extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
258 #else
259 #define __underlying_memchr     __builtin_memchr
260 #define __underlying_memcmp     __builtin_memcmp
261 #define __underlying_memcpy     __builtin_memcpy
262 #define __underlying_memmove    __builtin_memmove
263 #define __underlying_memset     __builtin_memset
264 #define __underlying_strcat     __builtin_strcat
265 #define __underlying_strcpy     __builtin_strcpy
266 #define __underlying_strlen     __builtin_strlen
267 #define __underlying_strncat    __builtin_strncat
268 #define __underlying_strncpy    __builtin_strncpy
269 #endif
270
271 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
272 {
273         size_t p_size = __builtin_object_size(p, 0);
274         if (__builtin_constant_p(size) && p_size < size)
275                 __write_overflow();
276         if (p_size < size)
277                 fortify_panic(__func__);
278         return __underlying_strncpy(p, q, size);
279 }
280
281 __FORTIFY_INLINE char *strcat(char *p, const char *q)
282 {
283         size_t p_size = __builtin_object_size(p, 0);
284         if (p_size == (size_t)-1)
285                 return __underlying_strcat(p, q);
286         if (strlcat(p, q, p_size) >= p_size)
287                 fortify_panic(__func__);
288         return p;
289 }
290
291 __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
292 {
293         __kernel_size_t ret;
294         size_t p_size = __builtin_object_size(p, 0);
295
296         /* Work around gcc excess stack consumption issue */
297         if (p_size == (size_t)-1 ||
298             (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0'))
299                 return __underlying_strlen(p);
300         ret = strnlen(p, p_size);
301         if (p_size <= ret)
302                 fortify_panic(__func__);
303         return ret;
304 }
305
306 extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
307 __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
308 {
309         size_t p_size = __builtin_object_size(p, 0);
310         __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
311         if (p_size <= ret && maxlen != ret)
312                 fortify_panic(__func__);
313         return ret;
314 }
315
316 /* defined after fortified strlen to reuse it */
317 extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
318 __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
319 {
320         size_t ret;
321         size_t p_size = __builtin_object_size(p, 0);
322         size_t q_size = __builtin_object_size(q, 0);
323         if (p_size == (size_t)-1 && q_size == (size_t)-1)
324                 return __real_strlcpy(p, q, size);
325         ret = strlen(q);
326         if (size) {
327                 size_t len = (ret >= size) ? size - 1 : ret;
328                 if (__builtin_constant_p(len) && len >= p_size)
329                         __write_overflow();
330                 if (len >= p_size)
331                         fortify_panic(__func__);
332                 __underlying_memcpy(p, q, len);
333                 p[len] = '\0';
334         }
335         return ret;
336 }
337
338 /* defined after fortified strlen and strnlen to reuse them */
339 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
340 {
341         size_t p_len, copy_len;
342         size_t p_size = __builtin_object_size(p, 0);
343         size_t q_size = __builtin_object_size(q, 0);
344         if (p_size == (size_t)-1 && q_size == (size_t)-1)
345                 return __underlying_strncat(p, q, count);
346         p_len = strlen(p);
347         copy_len = strnlen(q, count);
348         if (p_size < p_len + copy_len + 1)
349                 fortify_panic(__func__);
350         __underlying_memcpy(p + p_len, q, copy_len);
351         p[p_len + copy_len] = '\0';
352         return p;
353 }
354
355 __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
356 {
357         size_t p_size = __builtin_object_size(p, 0);
358         if (__builtin_constant_p(size) && p_size < size)
359                 __write_overflow();
360         if (p_size < size)
361                 fortify_panic(__func__);
362         return __underlying_memset(p, c, size);
363 }
364
365 __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
366 {
367         size_t p_size = __builtin_object_size(p, 0);
368         size_t q_size = __builtin_object_size(q, 0);
369         if (__builtin_constant_p(size)) {
370                 if (p_size < size)
371                         __write_overflow();
372                 if (q_size < size)
373                         __read_overflow2();
374         }
375         if (p_size < size || q_size < size)
376                 fortify_panic(__func__);
377         return __underlying_memcpy(p, q, size);
378 }
379
380 __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
381 {
382         size_t p_size = __builtin_object_size(p, 0);
383         size_t q_size = __builtin_object_size(q, 0);
384         if (__builtin_constant_p(size)) {
385                 if (p_size < size)
386                         __write_overflow();
387                 if (q_size < size)
388                         __read_overflow2();
389         }
390         if (p_size < size || q_size < size)
391                 fortify_panic(__func__);
392         return __underlying_memmove(p, q, size);
393 }
394
395 extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
396 __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
397 {
398         size_t p_size = __builtin_object_size(p, 0);
399         if (__builtin_constant_p(size) && p_size < size)
400                 __read_overflow();
401         if (p_size < size)
402                 fortify_panic(__func__);
403         return __real_memscan(p, c, size);
404 }
405
406 __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
407 {
408         size_t p_size = __builtin_object_size(p, 0);
409         size_t q_size = __builtin_object_size(q, 0);
410         if (__builtin_constant_p(size)) {
411                 if (p_size < size)
412                         __read_overflow();
413                 if (q_size < size)
414                         __read_overflow2();
415         }
416         if (p_size < size || q_size < size)
417                 fortify_panic(__func__);
418         return __underlying_memcmp(p, q, size);
419 }
420
421 __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
422 {
423         size_t p_size = __builtin_object_size(p, 0);
424         if (__builtin_constant_p(size) && p_size < size)
425                 __read_overflow();
426         if (p_size < size)
427                 fortify_panic(__func__);
428         return __underlying_memchr(p, c, size);
429 }
430
431 void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
432 __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
433 {
434         size_t p_size = __builtin_object_size(p, 0);
435         if (__builtin_constant_p(size) && p_size < size)
436                 __read_overflow();
437         if (p_size < size)
438                 fortify_panic(__func__);
439         return __real_memchr_inv(p, c, size);
440 }
441
442 extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
443 __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
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 __real_kmemdup(p, size, gfp);
451 }
452
453 /* defined after fortified strlen and memcpy to reuse them */
454 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
455 {
456         size_t p_size = __builtin_object_size(p, 0);
457         size_t q_size = __builtin_object_size(q, 0);
458         if (p_size == (size_t)-1 && q_size == (size_t)-1)
459                 return __underlying_strcpy(p, q);
460         memcpy(p, q, strlen(q) + 1);
461         return p;
462 }
463
464 /* Don't use these outside the FORITFY_SOURCE implementation */
465 #undef __underlying_memchr
466 #undef __underlying_memcmp
467 #undef __underlying_memcpy
468 #undef __underlying_memmove
469 #undef __underlying_memset
470 #undef __underlying_strcat
471 #undef __underlying_strcpy
472 #undef __underlying_strlen
473 #undef __underlying_strncat
474 #undef __underlying_strncpy
475 #endif
476
477 /**
478  * memcpy_and_pad - Copy one buffer to another with padding
479  * @dest: Where to copy to
480  * @dest_len: The destination buffer size
481  * @src: Where to copy from
482  * @count: The number of bytes to copy
483  * @pad: Character to use for padding if space is left in destination.
484  */
485 static inline void memcpy_and_pad(void *dest, size_t dest_len,
486                                   const void *src, size_t count, int pad)
487 {
488         if (dest_len > count) {
489                 memcpy(dest, src, count);
490                 memset(dest + count, pad,  dest_len - count);
491         } else
492                 memcpy(dest, src, dest_len);
493 }
494
495 #endif /* _LINUX_STRING_H_ */