This source file includes following definitions.
- strstrip
- memset_l
- memset_p
- memcpy_mcsafe
- memcpy_flushcache
- kstrdup
- bprintf
- memzero_explicit
- kbasename
- fortify_panic
- strcat
- strlen
- __real_strnlen
- __real_strlcpy
- strncat
- memset
- memcpy
- memmove
- __real_memscan
- memcmp
- memchr
- __real_memchr_inv
- __real_kmemdup
- strcpy
- memcpy_and_pad
- str_has_prefix
1
2 #ifndef _LINUX_STRING_H_
3 #define _LINUX_STRING_H_
4
5
6 #include <linux/compiler.h>
7 #include <linux/types.h>
8 #include <linux/stddef.h>
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
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
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 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_MCSAFE
164 static inline __must_check unsigned long memcpy_mcsafe(void *dst,
165 const void *src, size_t cnt)
166 {
167 memcpy(dst, src, cnt);
168 return 0;
169 }
170 #endif
171 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
172 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
173 {
174 memcpy(dst, src, cnt);
175 }
176 #endif
177 void *memchr_inv(const void *s, int c, size_t n);
178 char *strreplace(char *s, char old, char new);
179
180 extern void kfree_const(const void *x);
181
182 extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
183 extern const char *kstrdup_const(const char *s, gfp_t gfp);
184 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
185 extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
186 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
187
188 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
189 extern void argv_free(char **argv);
190
191 extern bool sysfs_streq(const char *s1, const char *s2);
192 extern int kstrtobool(const char *s, bool *res);
193 static inline int strtobool(const char *s, bool *res)
194 {
195 return kstrtobool(s, res);
196 }
197
198 int match_string(const char * const *array, size_t n, const char *string);
199 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
200
201
202
203
204
205
206
207
208 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
209
210 #ifdef CONFIG_BINARY_PRINTF
211 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
212 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
213 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
214 #endif
215
216 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
217 const void *from, size_t available);
218
219
220
221
222
223
224 static inline bool strstarts(const char *str, const char *prefix)
225 {
226 return strncmp(str, prefix, strlen(prefix)) == 0;
227 }
228
229 size_t memweight(const void *ptr, size_t bytes);
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 static inline void memzero_explicit(void *s, size_t count)
246 {
247 memset(s, 0, count);
248 barrier_data(s);
249 }
250
251
252
253
254
255
256 static inline const char *kbasename(const char *path)
257 {
258 const char *tail = strrchr(path, '/');
259 return tail ? tail + 1 : path;
260 }
261
262 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
263 #define __RENAME(x) __asm__(#x)
264
265 void fortify_panic(const char *name) __noreturn __cold;
266 void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
267 void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
268 void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
269 void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
270
271 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
272 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
273 {
274 size_t p_size = __builtin_object_size(p, 0);
275 if (__builtin_constant_p(size) && p_size < size)
276 __write_overflow();
277 if (p_size < size)
278 fortify_panic(__func__);
279 return __builtin_strncpy(p, q, size);
280 }
281
282 __FORTIFY_INLINE char *strcat(char *p, const char *q)
283 {
284 size_t p_size = __builtin_object_size(p, 0);
285 if (p_size == (size_t)-1)
286 return __builtin_strcat(p, q);
287 if (strlcat(p, q, p_size) >= p_size)
288 fortify_panic(__func__);
289 return p;
290 }
291
292 __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
293 {
294 __kernel_size_t ret;
295 size_t p_size = __builtin_object_size(p, 0);
296
297
298 if (p_size == (size_t)-1 ||
299 (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0'))
300 return __builtin_strlen(p);
301 ret = strnlen(p, p_size);
302 if (p_size <= ret)
303 fortify_panic(__func__);
304 return ret;
305 }
306
307 extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
308 __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
309 {
310 size_t p_size = __builtin_object_size(p, 0);
311 __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
312 if (p_size <= ret && maxlen != ret)
313 fortify_panic(__func__);
314 return ret;
315 }
316
317
318 extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
319 __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
320 {
321 size_t ret;
322 size_t p_size = __builtin_object_size(p, 0);
323 size_t q_size = __builtin_object_size(q, 0);
324 if (p_size == (size_t)-1 && q_size == (size_t)-1)
325 return __real_strlcpy(p, q, size);
326 ret = strlen(q);
327 if (size) {
328 size_t len = (ret >= size) ? size - 1 : ret;
329 if (__builtin_constant_p(len) && len >= p_size)
330 __write_overflow();
331 if (len >= p_size)
332 fortify_panic(__func__);
333 __builtin_memcpy(p, q, len);
334 p[len] = '\0';
335 }
336 return ret;
337 }
338
339
340 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
341 {
342 size_t p_len, copy_len;
343 size_t p_size = __builtin_object_size(p, 0);
344 size_t q_size = __builtin_object_size(q, 0);
345 if (p_size == (size_t)-1 && q_size == (size_t)-1)
346 return __builtin_strncat(p, q, count);
347 p_len = strlen(p);
348 copy_len = strnlen(q, count);
349 if (p_size < p_len + copy_len + 1)
350 fortify_panic(__func__);
351 __builtin_memcpy(p + p_len, q, copy_len);
352 p[p_len + copy_len] = '\0';
353 return p;
354 }
355
356 __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
357 {
358 size_t p_size = __builtin_object_size(p, 0);
359 if (__builtin_constant_p(size) && p_size < size)
360 __write_overflow();
361 if (p_size < size)
362 fortify_panic(__func__);
363 return __builtin_memset(p, c, size);
364 }
365
366 __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
367 {
368 size_t p_size = __builtin_object_size(p, 0);
369 size_t q_size = __builtin_object_size(q, 0);
370 if (__builtin_constant_p(size)) {
371 if (p_size < size)
372 __write_overflow();
373 if (q_size < size)
374 __read_overflow2();
375 }
376 if (p_size < size || q_size < size)
377 fortify_panic(__func__);
378 return __builtin_memcpy(p, q, size);
379 }
380
381 __FORTIFY_INLINE void *memmove(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 __write_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_memmove(p, q, size);
394 }
395
396 extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
397 __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
398 {
399 size_t p_size = __builtin_object_size(p, 0);
400 if (__builtin_constant_p(size) && p_size < size)
401 __read_overflow();
402 if (p_size < size)
403 fortify_panic(__func__);
404 return __real_memscan(p, c, size);
405 }
406
407 __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
408 {
409 size_t p_size = __builtin_object_size(p, 0);
410 size_t q_size = __builtin_object_size(q, 0);
411 if (__builtin_constant_p(size)) {
412 if (p_size < size)
413 __read_overflow();
414 if (q_size < size)
415 __read_overflow2();
416 }
417 if (p_size < size || q_size < size)
418 fortify_panic(__func__);
419 return __builtin_memcmp(p, q, size);
420 }
421
422 __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
423 {
424 size_t p_size = __builtin_object_size(p, 0);
425 if (__builtin_constant_p(size) && p_size < size)
426 __read_overflow();
427 if (p_size < size)
428 fortify_panic(__func__);
429 return __builtin_memchr(p, c, size);
430 }
431
432 void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
433 __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
434 {
435 size_t p_size = __builtin_object_size(p, 0);
436 if (__builtin_constant_p(size) && p_size < size)
437 __read_overflow();
438 if (p_size < size)
439 fortify_panic(__func__);
440 return __real_memchr_inv(p, c, size);
441 }
442
443 extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
444 __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
445 {
446 size_t p_size = __builtin_object_size(p, 0);
447 if (__builtin_constant_p(size) && p_size < size)
448 __read_overflow();
449 if (p_size < size)
450 fortify_panic(__func__);
451 return __real_kmemdup(p, size, gfp);
452 }
453
454
455 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
456 {
457 size_t p_size = __builtin_object_size(p, 0);
458 size_t q_size = __builtin_object_size(q, 0);
459 if (p_size == (size_t)-1 && q_size == (size_t)-1)
460 return __builtin_strcpy(p, q);
461 memcpy(p, q, strlen(q) + 1);
462 return p;
463 }
464
465 #endif
466
467
468
469
470
471
472
473
474
475 static inline void memcpy_and_pad(void *dest, size_t dest_len,
476 const void *src, size_t count, int pad)
477 {
478 if (dest_len > count) {
479 memcpy(dest, src, count);
480 memset(dest + count, pad, dest_len - count);
481 } else
482 memcpy(dest, src, dest_len);
483 }
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500 static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
501 {
502 size_t len = strlen(prefix);
503 return strncmp(str, prefix, len) == 0 ? len : 0;
504 }
505
506 #endif