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