Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_STRING_H_ |
| 3 | #define _LINUX_STRING_H_ |
| 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | #include <linux/compiler.h> /* for inline */ |
| 7 | #include <linux/types.h> /* for size_t */ |
| 8 | #include <linux/stddef.h> /* for NULL */ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 9 | #include <stdarg.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 10 | #include <uapi/linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 12 | extern char *strndup_user(const char __user *, long); |
Li Zefan | 610a77e | 2009-03-31 15:23:16 -0700 | [diff] [blame] | 13 | extern void *memdup_user(const void __user *, size_t); |
Al Viro | 50fd2f2 | 2018-01-07 13:06:15 -0500 | [diff] [blame] | 14 | extern void *vmemdup_user(const void __user *, size_t); |
Al Viro | e9d408e | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 15 | extern void *memdup_user_nul(const void __user *, size_t); |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 31 | #ifndef __HAVE_ARCH_STRSCPY |
Tejun Heo | 08a7767 | 2018-01-09 07:21:15 -0800 | [diff] [blame] | 32 | ssize_t strscpy(char *, const char *, size_t); |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 33 | #endif |
Tobin C. Harding | 458a3bf | 2019-04-05 12:58:58 +1100 | [diff] [blame^] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
David S. Miller | ded220b | 2007-03-29 01:18:42 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifndef __HAVE_ARCH_STRCHR |
| 60 | extern char * strchr(const char *,int); |
| 61 | #endif |
Grant Likely | 11d200e | 2014-03-14 17:00:14 +0000 | [diff] [blame] | 62 | #ifndef __HAVE_ARCH_STRCHRNUL |
| 63 | extern char * strchrnul(const char *,int); |
| 64 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
André Goddard Rosa | f653398 | 2009-12-14 18:01:04 -0800 | [diff] [blame] | 71 | extern char * __must_check skip_spaces(const char *); |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 72 | |
| 73 | extern char *strim(char *); |
| 74 | |
| 75 | static inline __must_check char *strstrip(char *str) |
| 76 | { |
| 77 | return strim(str); |
| 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #ifndef __HAVE_ARCH_STRSTR |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | #ifndef __HAVE_ARCH_MEMSET |
| 106 | extern void * memset(void *,int,__kernel_size_t); |
| 107 | #endif |
Matthew Wilcox | 3b3c4ba | 2017-09-08 16:13:48 -0700 | [diff] [blame] | 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 | |
Alexander Shishkin | ce76d93 | 2018-10-05 15:43:05 +0300 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Nick Desaulniers | 5f074f3 | 2019-04-05 18:38:45 -0700 | [diff] [blame] | 157 | #ifndef __HAVE_ARCH_BCMP |
| 158 | extern int bcmp(const void *,const void *,__kernel_size_t); |
| 159 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | #ifndef __HAVE_ARCH_MEMCHR |
| 161 | extern void * memchr(const void *,int,__kernel_size_t); |
| 162 | #endif |
Dan Williams | 6abccd1 | 2017-01-13 14:14:23 -0800 | [diff] [blame] | 163 | #ifndef __HAVE_ARCH_MEMCPY_MCSAFE |
Dan Williams | 60622d6 | 2018-05-03 17:06:21 -0700 | [diff] [blame] | 164 | static inline __must_check unsigned long memcpy_mcsafe(void *dst, |
| 165 | const void *src, size_t cnt) |
Dan Williams | 6abccd1 | 2017-01-13 14:14:23 -0800 | [diff] [blame] | 166 | { |
| 167 | memcpy(dst, src, cnt); |
| 168 | return 0; |
| 169 | } |
| 170 | #endif |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 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 |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 177 | void *memchr_inv(const void *s, int c, size_t n); |
Rasmus Villemoes | 94df290 | 2015-06-25 15:02:22 -0700 | [diff] [blame] | 178 | char *strreplace(char *s, char old, char new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Andrzej Hajda | a4bb1e4 | 2015-02-13 14:36:24 -0800 | [diff] [blame] | 180 | extern void kfree_const(const void *x); |
| 181 | |
Rasmus Villemoes | 48a2705 | 2016-05-19 17:10:55 -0700 | [diff] [blame] | 182 | extern char *kstrdup(const char *s, gfp_t gfp) __malloc; |
Andrzej Hajda | a4bb1e4 | 2015-02-13 14:36:24 -0800 | [diff] [blame] | 183 | extern const char *kstrdup_const(const char *s, gfp_t gfp); |
Jeremy Fitzhardinge | 1e66df3 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 184 | extern char *kstrndup(const char *s, size_t len, gfp_t gfp); |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 185 | extern void *kmemdup(const void *src, size_t len, gfp_t gfp); |
David Howells | f351574 | 2017-07-04 17:25:02 +0100 | [diff] [blame] | 186 | extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 187 | |
Jeremy Fitzhardinge | d84d1cc | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 188 | extern char **argv_split(gfp_t gfp, const char *str, int *argcp); |
| 189 | extern void argv_free(char **argv); |
| 190 | |
David Brownell | 34990cf | 2008-05-01 04:34:42 -0700 | [diff] [blame] | 191 | extern bool sysfs_streq(const char *s1, const char *s2); |
Kees Cook | ef95159 | 2016-03-17 14:22:50 -0700 | [diff] [blame] | 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 | } |
David Brownell | 34990cf | 2008-05-01 04:34:42 -0700 | [diff] [blame] | 197 | |
Andy Shevchenko | 56b0608 | 2016-03-17 14:22:14 -0700 | [diff] [blame] | 198 | int match_string(const char * const *array, size_t n, const char *string); |
Heikki Krogerus | e1fe7b6 | 2017-03-21 13:56:46 +0200 | [diff] [blame] | 199 | int __sysfs_match_string(const char * const *array, size_t n, const char *s); |
| 200 | |
| 201 | /** |
| 202 | * sysfs_match_string - matches given string in an array |
| 203 | * @_a: array of strings |
| 204 | * @_s: string to match with |
| 205 | * |
| 206 | * Helper for __sysfs_match_string(). Calculates the size of @a automatically. |
| 207 | */ |
| 208 | #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s) |
Andy Shevchenko | 56b0608 | 2016-03-17 14:22:14 -0700 | [diff] [blame] | 209 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 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 | |
Akinobu Mita | e108526e | 2008-07-23 21:26:44 -0700 | [diff] [blame] | 216 | extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, |
Daniel Borkmann | d4c5efd | 2014-08-26 23:16:35 -0400 | [diff] [blame] | 217 | const void *from, size_t available); |
Akinobu Mita | e108526e | 2008-07-23 21:26:44 -0700 | [diff] [blame] | 218 | |
Rusty Russell | 66f92cf | 2009-03-31 13:05:36 -0600 | [diff] [blame] | 219 | /** |
| 220 | * strstarts - does @str start with @prefix? |
| 221 | * @str: string to examine |
| 222 | * @prefix: prefix to look for. |
| 223 | */ |
| 224 | static inline bool strstarts(const char *str, const char *prefix) |
| 225 | { |
| 226 | return strncmp(str, prefix, strlen(prefix)) == 0; |
| 227 | } |
Akinobu Mita | 639b9e3 | 2012-07-30 14:40:55 -0700 | [diff] [blame] | 228 | |
Daniel Borkmann | d4c5efd | 2014-08-26 23:16:35 -0400 | [diff] [blame] | 229 | size_t memweight(const void *ptr, size_t bytes); |
| 230 | void memzero_explicit(void *s, size_t count); |
Akinobu Mita | 639b9e3 | 2012-07-30 14:40:55 -0700 | [diff] [blame] | 231 | |
Andy Shevchenko | b18888a | 2012-12-17 16:01:18 -0800 | [diff] [blame] | 232 | /** |
| 233 | * kbasename - return the last part of a pathname. |
| 234 | * |
| 235 | * @path: path to extract the filename from. |
| 236 | */ |
| 237 | static inline const char *kbasename(const char *path) |
| 238 | { |
| 239 | const char *tail = strrchr(path, '/'); |
| 240 | return tail ? tail + 1 : path; |
| 241 | } |
| 242 | |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 243 | #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline)) |
| 244 | #define __RENAME(x) __asm__(#x) |
| 245 | |
| 246 | void fortify_panic(const char *name) __noreturn __cold; |
| 247 | void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter"); |
| 248 | void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter"); |
Martin Wilck | 01f33c3 | 2017-08-14 22:12:38 +0200 | [diff] [blame] | 249 | void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter"); |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 250 | void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter"); |
| 251 | |
| 252 | #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE) |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 253 | __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size) |
| 254 | { |
| 255 | size_t p_size = __builtin_object_size(p, 0); |
| 256 | if (__builtin_constant_p(size) && p_size < size) |
| 257 | __write_overflow(); |
| 258 | if (p_size < size) |
| 259 | fortify_panic(__func__); |
| 260 | return __builtin_strncpy(p, q, size); |
| 261 | } |
| 262 | |
| 263 | __FORTIFY_INLINE char *strcat(char *p, const char *q) |
| 264 | { |
| 265 | size_t p_size = __builtin_object_size(p, 0); |
| 266 | if (p_size == (size_t)-1) |
| 267 | return __builtin_strcat(p, q); |
| 268 | if (strlcat(p, q, p_size) >= p_size) |
| 269 | fortify_panic(__func__); |
| 270 | return p; |
| 271 | } |
| 272 | |
| 273 | __FORTIFY_INLINE __kernel_size_t strlen(const char *p) |
| 274 | { |
| 275 | __kernel_size_t ret; |
| 276 | size_t p_size = __builtin_object_size(p, 0); |
Arnd Bergmann | 146734b | 2017-12-14 15:32:34 -0800 | [diff] [blame] | 277 | |
| 278 | /* Work around gcc excess stack consumption issue */ |
| 279 | if (p_size == (size_t)-1 || |
| 280 | (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0')) |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 281 | return __builtin_strlen(p); |
| 282 | ret = strnlen(p, p_size); |
| 283 | if (p_size <= ret) |
| 284 | fortify_panic(__func__); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen); |
| 289 | __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen) |
| 290 | { |
| 291 | size_t p_size = __builtin_object_size(p, 0); |
| 292 | __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size); |
| 293 | if (p_size <= ret && maxlen != ret) |
| 294 | fortify_panic(__func__); |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | /* defined after fortified strlen to reuse it */ |
| 299 | extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy); |
| 300 | __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size) |
| 301 | { |
| 302 | size_t ret; |
| 303 | size_t p_size = __builtin_object_size(p, 0); |
| 304 | size_t q_size = __builtin_object_size(q, 0); |
| 305 | if (p_size == (size_t)-1 && q_size == (size_t)-1) |
| 306 | return __real_strlcpy(p, q, size); |
| 307 | ret = strlen(q); |
| 308 | if (size) { |
| 309 | size_t len = (ret >= size) ? size - 1 : ret; |
| 310 | if (__builtin_constant_p(len) && len >= p_size) |
| 311 | __write_overflow(); |
| 312 | if (len >= p_size) |
| 313 | fortify_panic(__func__); |
| 314 | __builtin_memcpy(p, q, len); |
| 315 | p[len] = '\0'; |
| 316 | } |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | /* defined after fortified strlen and strnlen to reuse them */ |
| 321 | __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) |
| 322 | { |
| 323 | size_t p_len, copy_len; |
| 324 | size_t p_size = __builtin_object_size(p, 0); |
| 325 | size_t q_size = __builtin_object_size(q, 0); |
| 326 | if (p_size == (size_t)-1 && q_size == (size_t)-1) |
| 327 | return __builtin_strncat(p, q, count); |
| 328 | p_len = strlen(p); |
| 329 | copy_len = strnlen(q, count); |
| 330 | if (p_size < p_len + copy_len + 1) |
| 331 | fortify_panic(__func__); |
| 332 | __builtin_memcpy(p + p_len, q, copy_len); |
| 333 | p[p_len + copy_len] = '\0'; |
| 334 | return p; |
| 335 | } |
| 336 | |
| 337 | __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size) |
| 338 | { |
| 339 | size_t p_size = __builtin_object_size(p, 0); |
| 340 | if (__builtin_constant_p(size) && p_size < size) |
| 341 | __write_overflow(); |
| 342 | if (p_size < size) |
| 343 | fortify_panic(__func__); |
| 344 | return __builtin_memset(p, c, size); |
| 345 | } |
| 346 | |
| 347 | __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) |
| 348 | { |
| 349 | size_t p_size = __builtin_object_size(p, 0); |
| 350 | size_t q_size = __builtin_object_size(q, 0); |
| 351 | if (__builtin_constant_p(size)) { |
| 352 | if (p_size < size) |
| 353 | __write_overflow(); |
| 354 | if (q_size < size) |
| 355 | __read_overflow2(); |
| 356 | } |
| 357 | if (p_size < size || q_size < size) |
| 358 | fortify_panic(__func__); |
| 359 | return __builtin_memcpy(p, q, size); |
| 360 | } |
| 361 | |
| 362 | __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) |
| 363 | { |
| 364 | size_t p_size = __builtin_object_size(p, 0); |
| 365 | size_t q_size = __builtin_object_size(q, 0); |
| 366 | if (__builtin_constant_p(size)) { |
| 367 | if (p_size < size) |
| 368 | __write_overflow(); |
| 369 | if (q_size < size) |
| 370 | __read_overflow2(); |
| 371 | } |
| 372 | if (p_size < size || q_size < size) |
| 373 | fortify_panic(__func__); |
| 374 | return __builtin_memmove(p, q, size); |
| 375 | } |
| 376 | |
| 377 | extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan); |
| 378 | __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size) |
| 379 | { |
| 380 | size_t p_size = __builtin_object_size(p, 0); |
| 381 | if (__builtin_constant_p(size) && p_size < size) |
| 382 | __read_overflow(); |
| 383 | if (p_size < size) |
| 384 | fortify_panic(__func__); |
| 385 | return __real_memscan(p, c, size); |
| 386 | } |
| 387 | |
| 388 | __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size) |
| 389 | { |
| 390 | size_t p_size = __builtin_object_size(p, 0); |
| 391 | size_t q_size = __builtin_object_size(q, 0); |
| 392 | if (__builtin_constant_p(size)) { |
| 393 | if (p_size < size) |
| 394 | __read_overflow(); |
| 395 | if (q_size < size) |
| 396 | __read_overflow2(); |
| 397 | } |
| 398 | if (p_size < size || q_size < size) |
| 399 | fortify_panic(__func__); |
| 400 | return __builtin_memcmp(p, q, size); |
| 401 | } |
| 402 | |
| 403 | __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size) |
| 404 | { |
| 405 | size_t p_size = __builtin_object_size(p, 0); |
| 406 | if (__builtin_constant_p(size) && p_size < size) |
| 407 | __read_overflow(); |
| 408 | if (p_size < size) |
| 409 | fortify_panic(__func__); |
| 410 | return __builtin_memchr(p, c, size); |
| 411 | } |
| 412 | |
| 413 | void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv); |
| 414 | __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size) |
| 415 | { |
| 416 | size_t p_size = __builtin_object_size(p, 0); |
| 417 | if (__builtin_constant_p(size) && p_size < size) |
| 418 | __read_overflow(); |
| 419 | if (p_size < size) |
| 420 | fortify_panic(__func__); |
| 421 | return __real_memchr_inv(p, c, size); |
| 422 | } |
| 423 | |
| 424 | extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup); |
| 425 | __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp) |
| 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_kmemdup(p, size, gfp); |
| 433 | } |
Daniel Micay | 077d2ba | 2017-07-14 17:28:12 -0400 | [diff] [blame] | 434 | |
| 435 | /* defined after fortified strlen and memcpy to reuse them */ |
| 436 | __FORTIFY_INLINE char *strcpy(char *p, const char *q) |
| 437 | { |
| 438 | size_t p_size = __builtin_object_size(p, 0); |
| 439 | size_t q_size = __builtin_object_size(q, 0); |
| 440 | if (p_size == (size_t)-1 && q_size == (size_t)-1) |
| 441 | return __builtin_strcpy(p, q); |
| 442 | memcpy(p, q, strlen(q) + 1); |
| 443 | return p; |
| 444 | } |
| 445 | |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 446 | #endif |
| 447 | |
Martin Wilck | 01f33c3 | 2017-08-14 22:12:38 +0200 | [diff] [blame] | 448 | /** |
| 449 | * memcpy_and_pad - Copy one buffer to another with padding |
| 450 | * @dest: Where to copy to |
| 451 | * @dest_len: The destination buffer size |
| 452 | * @src: Where to copy from |
| 453 | * @count: The number of bytes to copy |
| 454 | * @pad: Character to use for padding if space is left in destination. |
| 455 | */ |
Martin Wilck | 1359798 | 2017-09-06 14:36:57 +0200 | [diff] [blame] | 456 | static inline void memcpy_and_pad(void *dest, size_t dest_len, |
| 457 | const void *src, size_t count, int pad) |
Martin Wilck | 01f33c3 | 2017-08-14 22:12:38 +0200 | [diff] [blame] | 458 | { |
Martin Wilck | 01f33c3 | 2017-08-14 22:12:38 +0200 | [diff] [blame] | 459 | if (dest_len > count) { |
| 460 | memcpy(dest, src, count); |
| 461 | memset(dest + count, pad, dest_len - count); |
| 462 | } else |
| 463 | memcpy(dest, src, dest_len); |
| 464 | } |
| 465 | |
Steven Rostedt (VMware) | 7292142 | 2018-12-21 18:10:14 -0500 | [diff] [blame] | 466 | /** |
| 467 | * str_has_prefix - Test if a string has a given prefix |
| 468 | * @str: The string to test |
| 469 | * @prefix: The string to see if @str starts with |
| 470 | * |
| 471 | * A common way to test a prefix of a string is to do: |
| 472 | * strncmp(str, prefix, sizeof(prefix) - 1) |
| 473 | * |
| 474 | * But this can lead to bugs due to typos, or if prefix is a pointer |
| 475 | * and not a constant. Instead use str_has_prefix(). |
| 476 | * |
| 477 | * Returns: 0 if @str does not start with @prefix |
| 478 | strlen(@prefix) if @str does start with @prefix |
| 479 | */ |
| 480 | static __always_inline size_t str_has_prefix(const char *str, const char *prefix) |
| 481 | { |
| 482 | size_t len = strlen(prefix); |
| 483 | return strncmp(str, prefix, len) == 0 ? len : 0; |
| 484 | } |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | #endif /* _LINUX_STRING_H_ */ |