• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STRING_H_
3 #define _LINUX_STRING_H_
4 
5 #include <linux/compiler.h>	/* for inline */
6 #include <linux/types.h>	/* for size_t */
7 #include <linux/stddef.h>	/* for NULL */
8 #include <stdarg.h>
9 #include <uapi/linux/string.h>
10 
11 extern char *strndup_user(const char __user *, long);
12 extern void *memdup_user(const void __user *, size_t);
13 extern void *vmemdup_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 extern char * strnchrnul(const char *, size_t, int);
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 
strstrip(char * str)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 
memset_l(unsigned long * p,unsigned long v,__kernel_size_t n)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 
memset_p(void ** p,void * v,__kernel_size_t n)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 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));	\
143 })
144 
145 #ifndef __HAVE_ARCH_MEMCPY
146 extern void * memcpy(void *,const void *,__kernel_size_t);
147 #endif
148 #ifndef __HAVE_ARCH_MEMMOVE
149 extern void * memmove(void *,const void *,__kernel_size_t);
150 #endif
151 #ifndef __HAVE_ARCH_MEMSCAN
152 extern void * memscan(void *,int,__kernel_size_t);
153 #endif
154 #ifndef __HAVE_ARCH_MEMCMP
155 extern int memcmp(const void *,const void *,__kernel_size_t);
156 #endif
157 #ifndef __HAVE_ARCH_BCMP
158 extern int bcmp(const void *,const void *,__kernel_size_t);
159 #endif
160 #ifndef __HAVE_ARCH_MEMCHR
161 extern void * memchr(const void *,int,__kernel_size_t);
162 #endif
163 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
memcpy_flushcache(void * dst,const void * src,size_t cnt)164 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
165 {
166 	memcpy(dst, src, cnt);
167 }
168 #endif
169 
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 int match_string(const char * const *array, size_t n, const char *string);
186 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
187 
188 /**
189  * sysfs_match_string - matches given string in an array
190  * @_a: array of strings
191  * @_s: string to match with
192  *
193  * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
194  */
195 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
196 
197 #ifdef CONFIG_BINARY_PRINTF
198 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
199 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
200 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
201 #endif
202 
203 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
204 				       const void *from, size_t available);
205 
206 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
207 
208 /**
209  * strstarts - does @str start with @prefix?
210  * @str: string to examine
211  * @prefix: prefix to look for.
212  */
strstarts(const char * str,const char * prefix)213 static inline bool strstarts(const char *str, const char *prefix)
214 {
215 	return strncmp(str, prefix, strlen(prefix)) == 0;
216 }
217 
218 size_t memweight(const void *ptr, size_t bytes);
219 
220 /**
221  * memzero_explicit - Fill a region of memory (e.g. sensitive
222  *		      keying data) with 0s.
223  * @s: Pointer to the start of the area.
224  * @count: The size of the area.
225  *
226  * Note: usually using memset() is just fine (!), but in cases
227  * where clearing out _local_ data at the end of a scope is
228  * necessary, memzero_explicit() should be used instead in
229  * order to prevent the compiler from optimising away zeroing.
230  *
231  * memzero_explicit() doesn't need an arch-specific version as
232  * it just invokes the one of memset() implicitly.
233  */
memzero_explicit(void * s,size_t count)234 static inline void memzero_explicit(void *s, size_t count)
235 {
236 	memset(s, 0, count);
237 	barrier_data(s);
238 }
239 
240 /**
241  * kbasename - return the last part of a pathname.
242  *
243  * @path: path to extract the filename from.
244  */
kbasename(const char * path)245 static inline const char *kbasename(const char *path)
246 {
247 	const char *tail = strrchr(path, '/');
248 	return tail ? tail + 1 : path;
249 }
250 
251 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
252 #define __RENAME(x) __asm__(#x)
253 
254 void fortify_panic(const char *name) __noreturn __cold;
255 void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
256 void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
257 void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
258 void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
259 
260 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
261 
262 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
263 extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
264 extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
265 extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
266 extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
267 extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
268 extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
269 extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
270 extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
271 extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
272 extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
273 #else
274 #define __underlying_memchr	__builtin_memchr
275 #define __underlying_memcmp	__builtin_memcmp
276 #define __underlying_memcpy	__builtin_memcpy
277 #define __underlying_memmove	__builtin_memmove
278 #define __underlying_memset	__builtin_memset
279 #define __underlying_strcat	__builtin_strcat
280 #define __underlying_strcpy	__builtin_strcpy
281 #define __underlying_strlen	__builtin_strlen
282 #define __underlying_strncat	__builtin_strncat
283 #define __underlying_strncpy	__builtin_strncpy
284 #endif
285 
strncpy(char * p,const char * q,__kernel_size_t size)286 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
287 {
288 	size_t p_size = __builtin_object_size(p, 0);
289 	if (__builtin_constant_p(size) && p_size < size)
290 		__write_overflow();
291 	if (p_size < size)
292 		fortify_panic(__func__);
293 	return __underlying_strncpy(p, q, size);
294 }
295 
strcat(char * p,const char * q)296 __FORTIFY_INLINE char *strcat(char *p, const char *q)
297 {
298 	size_t p_size = __builtin_object_size(p, 0);
299 	if (p_size == (size_t)-1)
300 		return __underlying_strcat(p, q);
301 	if (strlcat(p, q, p_size) >= p_size)
302 		fortify_panic(__func__);
303 	return p;
304 }
305 
strlen(const char * p)306 __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
307 {
308 	__kernel_size_t ret;
309 	size_t p_size = __builtin_object_size(p, 0);
310 
311 	/* Work around gcc excess stack consumption issue */
312 	if (p_size == (size_t)-1 ||
313 	    (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0'))
314 		return __underlying_strlen(p);
315 	ret = strnlen(p, p_size);
316 	if (p_size <= ret)
317 		fortify_panic(__func__);
318 	return ret;
319 }
320 
321 extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
strnlen(const char * p,__kernel_size_t maxlen)322 __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
323 {
324 	size_t p_size = __builtin_object_size(p, 0);
325 	__kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
326 	if (p_size <= ret && maxlen != ret)
327 		fortify_panic(__func__);
328 	return ret;
329 }
330 
331 /* defined after fortified strlen to reuse it */
332 extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
strlcpy(char * p,const char * q,size_t size)333 __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
334 {
335 	size_t ret;
336 	size_t p_size = __builtin_object_size(p, 0);
337 	size_t q_size = __builtin_object_size(q, 0);
338 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
339 		return __real_strlcpy(p, q, size);
340 	ret = strlen(q);
341 	if (size) {
342 		size_t len = (ret >= size) ? size - 1 : ret;
343 		if (__builtin_constant_p(len) && len >= p_size)
344 			__write_overflow();
345 		if (len >= p_size)
346 			fortify_panic(__func__);
347 		__underlying_memcpy(p, q, len);
348 		p[len] = '\0';
349 	}
350 	return ret;
351 }
352 
353 /* defined after fortified strlen and strnlen to reuse them */
strncat(char * p,const char * q,__kernel_size_t count)354 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
355 {
356 	size_t p_len, copy_len;
357 	size_t p_size = __builtin_object_size(p, 0);
358 	size_t q_size = __builtin_object_size(q, 0);
359 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
360 		return __underlying_strncat(p, q, count);
361 	p_len = strlen(p);
362 	copy_len = strnlen(q, count);
363 	if (p_size < p_len + copy_len + 1)
364 		fortify_panic(__func__);
365 	__underlying_memcpy(p + p_len, q, copy_len);
366 	p[p_len + copy_len] = '\0';
367 	return p;
368 }
369 
memset(void * p,int c,__kernel_size_t size)370 __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
371 {
372 	size_t p_size = __builtin_object_size(p, 0);
373 	if (__builtin_constant_p(size) && p_size < size)
374 		__write_overflow();
375 	if (p_size < size)
376 		fortify_panic(__func__);
377 	return __underlying_memset(p, c, size);
378 }
379 
memcpy(void * p,const void * q,__kernel_size_t size)380 __FORTIFY_INLINE void *memcpy(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_memcpy(p, q, size);
393 }
394 
memmove(void * p,const void * q,__kernel_size_t size)395 __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
396 {
397 	size_t p_size = __builtin_object_size(p, 0);
398 	size_t q_size = __builtin_object_size(q, 0);
399 	if (__builtin_constant_p(size)) {
400 		if (p_size < size)
401 			__write_overflow();
402 		if (q_size < size)
403 			__read_overflow2();
404 	}
405 	if (p_size < size || q_size < size)
406 		fortify_panic(__func__);
407 	return __underlying_memmove(p, q, size);
408 }
409 
410 extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
memscan(void * p,int c,__kernel_size_t size)411 __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
412 {
413 	size_t p_size = __builtin_object_size(p, 0);
414 	if (__builtin_constant_p(size) && p_size < size)
415 		__read_overflow();
416 	if (p_size < size)
417 		fortify_panic(__func__);
418 	return __real_memscan(p, c, size);
419 }
420 
memcmp(const void * p,const void * q,__kernel_size_t size)421 __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
422 {
423 	size_t p_size = __builtin_object_size(p, 0);
424 	size_t q_size = __builtin_object_size(q, 0);
425 	if (__builtin_constant_p(size)) {
426 		if (p_size < size)
427 			__read_overflow();
428 		if (q_size < size)
429 			__read_overflow2();
430 	}
431 	if (p_size < size || q_size < size)
432 		fortify_panic(__func__);
433 	return __underlying_memcmp(p, q, size);
434 }
435 
memchr(const void * p,int c,__kernel_size_t size)436 __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
437 {
438 	size_t p_size = __builtin_object_size(p, 0);
439 	if (__builtin_constant_p(size) && p_size < size)
440 		__read_overflow();
441 	if (p_size < size)
442 		fortify_panic(__func__);
443 	return __underlying_memchr(p, c, size);
444 }
445 
446 void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
memchr_inv(const void * p,int c,size_t size)447 __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
448 {
449 	size_t p_size = __builtin_object_size(p, 0);
450 	if (__builtin_constant_p(size) && p_size < size)
451 		__read_overflow();
452 	if (p_size < size)
453 		fortify_panic(__func__);
454 	return __real_memchr_inv(p, c, size);
455 }
456 
457 extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
kmemdup(const void * p,size_t size,gfp_t gfp)458 __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
459 {
460 	size_t p_size = __builtin_object_size(p, 0);
461 	if (__builtin_constant_p(size) && p_size < size)
462 		__read_overflow();
463 	if (p_size < size)
464 		fortify_panic(__func__);
465 	return __real_kmemdup(p, size, gfp);
466 }
467 
468 /* defined after fortified strlen and memcpy to reuse them */
strcpy(char * p,const char * q)469 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
470 {
471 	size_t p_size = __builtin_object_size(p, 0);
472 	size_t q_size = __builtin_object_size(q, 0);
473 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
474 		return __underlying_strcpy(p, q);
475 	memcpy(p, q, strlen(q) + 1);
476 	return p;
477 }
478 
479 /* Don't use these outside the FORITFY_SOURCE implementation */
480 #undef __underlying_memchr
481 #undef __underlying_memcmp
482 #undef __underlying_memcpy
483 #undef __underlying_memmove
484 #undef __underlying_memset
485 #undef __underlying_strcat
486 #undef __underlying_strcpy
487 #undef __underlying_strlen
488 #undef __underlying_strncat
489 #undef __underlying_strncpy
490 #endif
491 
492 /**
493  * memcpy_and_pad - Copy one buffer to another with padding
494  * @dest: Where to copy to
495  * @dest_len: The destination buffer size
496  * @src: Where to copy from
497  * @count: The number of bytes to copy
498  * @pad: Character to use for padding if space is left in destination.
499  */
memcpy_and_pad(void * dest,size_t dest_len,const void * src,size_t count,int pad)500 static inline void memcpy_and_pad(void *dest, size_t dest_len,
501 				  const void *src, size_t count, int pad)
502 {
503 	if (dest_len > count) {
504 		memcpy(dest, src, count);
505 		memset(dest + count, pad,  dest_len - count);
506 	} else
507 		memcpy(dest, src, dest_len);
508 }
509 
510 /**
511  * str_has_prefix - Test if a string has a given prefix
512  * @str: The string to test
513  * @prefix: The string to see if @str starts with
514  *
515  * A common way to test a prefix of a string is to do:
516  *  strncmp(str, prefix, sizeof(prefix) - 1)
517  *
518  * But this can lead to bugs due to typos, or if prefix is a pointer
519  * and not a constant. Instead use str_has_prefix().
520  *
521  * Returns:
522  * * strlen(@prefix) if @str starts with @prefix
523  * * 0 if @str does not start with @prefix
524  */
str_has_prefix(const char * str,const char * prefix)525 static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
526 {
527 	size_t len = strlen(prefix);
528 	return strncmp(str, prefix, len) == 0 ? len : 0;
529 }
530 
531 #endif /* _LINUX_STRING_H_ */
532