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