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
12extern char *strndup_user(const char __user *, long);
13extern void *memdup_user(const void __user *, size_t);
14extern void *vmemdup_user(const void __user *, size_t);
15extern 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
23extern char * strcpy(char *,const char *);
24#endif
25#ifndef __HAVE_ARCH_STRNCPY
26extern char * strncpy(char *,const char *, __kernel_size_t);
27#endif
28#ifndef __HAVE_ARCH_STRLCPY
29size_t strlcpy(char *, const char *, size_t);
30#endif
31#ifndef __HAVE_ARCH_STRSCPY
32ssize_t strscpy(char *, const char *, size_t);
33#endif
34#ifndef __HAVE_ARCH_STRCAT
35extern char * strcat(char *, const char *);
36#endif
37#ifndef __HAVE_ARCH_STRNCAT
38extern char * strncat(char *, const char *, __kernel_size_t);
39#endif
40#ifndef __HAVE_ARCH_STRLCAT
41extern size_t strlcat(char *, const char *, __kernel_size_t);
42#endif
43#ifndef __HAVE_ARCH_STRCMP
44extern int strcmp(const char *,const char *);
45#endif
46#ifndef __HAVE_ARCH_STRNCMP
47extern int strncmp(const char *,const char *,__kernel_size_t);
48#endif
49#ifndef __HAVE_ARCH_STRCASECMP
50extern int strcasecmp(const char *s1, const char *s2);
51#endif
52#ifndef __HAVE_ARCH_STRNCASECMP
53extern int strncasecmp(const char *s1, const char *s2, size_t n);
54#endif
55#ifndef __HAVE_ARCH_STRCHR
56extern char * strchr(const char *,int);
57#endif
58#ifndef __HAVE_ARCH_STRCHRNUL
59extern char * strchrnul(const char *,int);
60#endif
61#ifndef __HAVE_ARCH_STRNCHR
62extern char * strnchr(const char *, size_t, int);
63#endif
64#ifndef __HAVE_ARCH_STRRCHR
65extern char * strrchr(const char *,int);
66#endif
67extern char * __must_check skip_spaces(const char *);
68
69extern char *strim(char *);
70
71static inline __must_check char *strstrip(char *str)
72{
73 return strim(str);
74}
75
76#ifndef __HAVE_ARCH_STRSTR
77extern char * strstr(const char *, const char *);
78#endif
79#ifndef __HAVE_ARCH_STRNSTR
80extern char * strnstr(const char *, const char *, size_t);
81#endif
82#ifndef __HAVE_ARCH_STRLEN
83extern __kernel_size_t strlen(const char *);
84#endif
85#ifndef __HAVE_ARCH_STRNLEN
86extern __kernel_size_t strnlen(const char *,__kernel_size_t);
87#endif
88#ifndef __HAVE_ARCH_STRPBRK
89extern char * strpbrk(const char *,const char *);
90#endif
91#ifndef __HAVE_ARCH_STRSEP
92extern char * strsep(char **,const char *);
93#endif
94#ifndef __HAVE_ARCH_STRSPN
95extern __kernel_size_t strspn(const char *,const char *);
96#endif
97#ifndef __HAVE_ARCH_STRCSPN
98extern __kernel_size_t strcspn(const char *,const char *);
99#endif
100
101#ifndef __HAVE_ARCH_MEMSET
102extern void * memset(void *,int,__kernel_size_t);
103#endif
104
105#ifndef __HAVE_ARCH_MEMSET16
106extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
107#endif
108
109#ifndef __HAVE_ARCH_MEMSET32
110extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
111#endif
112
113#ifndef __HAVE_ARCH_MEMSET64
114extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
115#endif
116
117static inline void *memset_l(unsigned long *p, unsigned long v,
118 __kernel_size_t n)
119{
120 if (BITS_PER_LONG == 32)
121 return memset32((uint32_t *)p, v, n);
122 else
123 return memset64((uint64_t *)p, v, n);
124}
125
126static inline void *memset_p(void **p, void *v, __kernel_size_t n)
127{
128 if (BITS_PER_LONG == 32)
129 return memset32((uint32_t *)p, (uintptr_t)v, n);
130 else
131 return memset64((uint64_t *)p, (uintptr_t)v, n);
132}
133
134extern void **__memcat_p(void **a, void **b);
135#define memcat_p(a, b) ({ \
136 BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
137 "type mismatch in memcat_p()"); \
138 (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
139})
140
141#ifndef __HAVE_ARCH_MEMCPY
142extern void * memcpy(void *,const void *,__kernel_size_t);
143#endif
144#ifndef __HAVE_ARCH_MEMMOVE
145extern void * memmove(void *,const void *,__kernel_size_t);
146#endif
147#ifndef __HAVE_ARCH_MEMSCAN
148extern void * memscan(void *,int,__kernel_size_t);
149#endif
150#ifndef __HAVE_ARCH_MEMCMP
151extern int memcmp(const void *,const void *,__kernel_size_t);
152#endif
153#ifndef __HAVE_ARCH_MEMCHR
154extern void * memchr(const void *,int,__kernel_size_t);
155#endif
156#ifndef __HAVE_ARCH_MEMCPY_MCSAFE
157static 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
165static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
166{
167 memcpy(dst, src, cnt);
168}
169#endif
170void *memchr_inv(const void *s, int c, size_t n);
171char *strreplace(char *s, char old, char new);
172
173extern void kfree_const(const void *x);
174
175extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
176extern const char *kstrdup_const(const char *s, gfp_t gfp);
177extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
178extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
179extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
180
181extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
182extern void argv_free(char **argv);
183
184extern bool sysfs_streq(const char *s1, const char *s2);
185extern int kstrtobool(const char *s, bool *res);
186static inline int strtobool(const char *s, bool *res)
187{
188 return kstrtobool(s, res);
189}
190
191int match_string(const char * const *array, size_t n, const char *string);
192int __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
204int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
205int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
206int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
207#endif
208
209extern 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 */
217static inline bool strstarts(const char *str, const char *prefix)
218{
219 return strncmp(str, prefix, strlen(prefix)) == 0;
220}
221
222size_t memweight(const void *ptr, size_t bytes);
223void 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 */
230static 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
239void fortify_panic(const char *name) __noreturn __cold;
240void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
241void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
242void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
243void __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__FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
247{
248 size_t p_size = __builtin_object_size(p, 0);
249 if (__builtin_constant_p(size) && p_size < size)
250 __write_overflow();
251 if (p_size < size)
252 fortify_panic(__func__);
253 return __builtin_strncpy(p, q, size);
254}
255
256__FORTIFY_INLINE char *strcat(char *p, const char *q)
257{
258 size_t p_size = __builtin_object_size(p, 0);
259 if (p_size == (size_t)-1)
260 return __builtin_strcat(p, q);
261 if (strlcat(p, q, p_size) >= p_size)
262 fortify_panic(__func__);
263 return p;
264}
265
266__FORTIFY_INLINE __kernel_size_t strlen(const char *p)
267{
268 __kernel_size_t ret;
269 size_t p_size = __builtin_object_size(p, 0);
270
271 /* Work around gcc excess stack consumption issue */
272 if (p_size == (size_t)-1 ||
273 (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0'))
274 return __builtin_strlen(p);
275 ret = strnlen(p, p_size);
276 if (p_size <= ret)
277 fortify_panic(__func__);
278 return ret;
279}
280
281extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
282__FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
283{
284 size_t p_size = __builtin_object_size(p, 0);
285 __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
286 if (p_size <= ret && maxlen != ret)
287 fortify_panic(__func__);
288 return ret;
289}
290
291/* defined after fortified strlen to reuse it */
292extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
293__FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
294{
295 size_t ret;
296 size_t p_size = __builtin_object_size(p, 0);
297 size_t q_size = __builtin_object_size(q, 0);
298 if (p_size == (size_t)-1 && q_size == (size_t)-1)
299 return __real_strlcpy(p, q, size);
300 ret = strlen(q);
301 if (size) {
302 size_t len = (ret >= size) ? size - 1 : ret;
303 if (__builtin_constant_p(len) && len >= p_size)
304 __write_overflow();
305 if (len >= p_size)
306 fortify_panic(__func__);
307 __builtin_memcpy(p, q, len);
308 p[len] = '\0';
309 }
310 return ret;
311}
312
313/* defined after fortified strlen and strnlen to reuse them */
314__FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
315{
316 size_t p_len, copy_len;
317 size_t p_size = __builtin_object_size(p, 0);
318 size_t q_size = __builtin_object_size(q, 0);
319 if (p_size == (size_t)-1 && q_size == (size_t)-1)
320 return __builtin_strncat(p, q, count);
321 p_len = strlen(p);
322 copy_len = strnlen(q, count);
323 if (p_size < p_len + copy_len + 1)
324 fortify_panic(__func__);
325 __builtin_memcpy(p + p_len, q, copy_len);
326 p[p_len + copy_len] = '\0';
327 return p;
328}
329
330__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
331{
332 size_t p_size = __builtin_object_size(p, 0);
333 if (__builtin_constant_p(size) && p_size < size)
334 __write_overflow();
335 if (p_size < size)
336 fortify_panic(__func__);
337 return __builtin_memset(p, c, size);
338}
339
340__FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
341{
342 size_t p_size = __builtin_object_size(p, 0);
343 size_t q_size = __builtin_object_size(q, 0);
344 if (__builtin_constant_p(size)) {
345 if (p_size < size)
346 __write_overflow();
347 if (q_size < size)
348 __read_overflow2();
349 }
350 if (p_size < size || q_size < size)
351 fortify_panic(__func__);
352 return __builtin_memcpy(p, q, size);
353}
354
355__FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
356{
357 size_t p_size = __builtin_object_size(p, 0);
358 size_t q_size = __builtin_object_size(q, 0);
359 if (__builtin_constant_p(size)) {
360 if (p_size < size)
361 __write_overflow();
362 if (q_size < size)
363 __read_overflow2();
364 }
365 if (p_size < size || q_size < size)
366 fortify_panic(__func__);
367 return __builtin_memmove(p, q, size);
368}
369
370extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
371__FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
372{
373 size_t p_size = __builtin_object_size(p, 0);
374 if (__builtin_constant_p(size) && p_size < size)
375 __read_overflow();
376 if (p_size < size)
377 fortify_panic(__func__);
378 return __real_memscan(p, c, size);
379}
380
381__FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
382{
383 size_t p_size = __builtin_object_size(p, 0);
384 size_t q_size = __builtin_object_size(q, 0);
385 if (__builtin_constant_p(size)) {
386 if (p_size < size)
387 __read_overflow();
388 if (q_size < size)
389 __read_overflow2();
390 }
391 if (p_size < size || q_size < size)
392 fortify_panic(__func__);
393 return __builtin_memcmp(p, q, size);
394}
395
396__FORTIFY_INLINE void *memchr(const 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 __builtin_memchr(p, c, size);
404}
405
406void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
407__FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
408{
409 size_t p_size = __builtin_object_size(p, 0);
410 if (__builtin_constant_p(size) && p_size < size)
411 __read_overflow();
412 if (p_size < size)
413 fortify_panic(__func__);
414 return __real_memchr_inv(p, c, size);
415}
416
417extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
418__FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
419{
420 size_t p_size = __builtin_object_size(p, 0);
421 if (__builtin_constant_p(size) && p_size < size)
422 __read_overflow();
423 if (p_size < size)
424 fortify_panic(__func__);
425 return __real_kmemdup(p, size, gfp);
426}
427
428/* defined after fortified strlen and memcpy to reuse them */
429__FORTIFY_INLINE char *strcpy(char *p, const char *q)
430{
431 size_t p_size = __builtin_object_size(p, 0);
432 size_t q_size = __builtin_object_size(q, 0);
433 if (p_size == (size_t)-1 && q_size == (size_t)-1)
434 return __builtin_strcpy(p, q);
435 memcpy(p, q, strlen(q) + 1);
436 return p;
437}
438
439#endif
440
441/**
442 * memcpy_and_pad - Copy one buffer to another with padding
443 * @dest: Where to copy to
444 * @dest_len: The destination buffer size
445 * @src: Where to copy from
446 * @count: The number of bytes to copy
447 * @pad: Character to use for padding if space is left in destination.
448 */
449static inline void memcpy_and_pad(void *dest, size_t dest_len,
450 const void *src, size_t count, int pad)
451{
452 if (dest_len > count) {
453 memcpy(dest, src, count);
454 memset(dest + count, pad, dest_len - count);
455 } else
456 memcpy(dest, src, dest_len);
457}
458
459/**
460 * str_has_prefix - Test if a string has a given prefix
461 * @str: The string to test
462 * @prefix: The string to see if @str starts with
463 *
464 * A common way to test a prefix of a string is to do:
465 * strncmp(str, prefix, sizeof(prefix) - 1)
466 *
467 * But this can lead to bugs due to typos, or if prefix is a pointer
468 * and not a constant. Instead use str_has_prefix().
469 *
470 * Returns: 0 if @str does not start with @prefix
471 strlen(@prefix) if @str does start with @prefix
472 */
473static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
474{
475 size_t len = strlen(prefix);
476 return strncmp(str, prefix, len) == 0 ? len : 0;
477}
478
479#endif /* _LINUX_STRING_H_ */
480