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 | e9d408e | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 14 | extern void *memdup_user_nul(const void __user *, size_t); |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Chris Metcalf | 30035e4 | 2015-04-29 12:52:04 -0400 | [diff] [blame] | 30 | #ifndef __HAVE_ARCH_STRSCPY |
| 31 | ssize_t __must_check strscpy(char *, const char *, size_t); |
| 32 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #ifndef __HAVE_ARCH_STRCAT |
| 34 | extern char * strcat(char *, const char *); |
| 35 | #endif |
| 36 | #ifndef __HAVE_ARCH_STRNCAT |
| 37 | extern char * strncat(char *, const char *, __kernel_size_t); |
| 38 | #endif |
| 39 | #ifndef __HAVE_ARCH_STRLCAT |
| 40 | extern size_t strlcat(char *, const char *, __kernel_size_t); |
| 41 | #endif |
| 42 | #ifndef __HAVE_ARCH_STRCMP |
| 43 | extern int strcmp(const char *,const char *); |
| 44 | #endif |
| 45 | #ifndef __HAVE_ARCH_STRNCMP |
| 46 | extern int strncmp(const char *,const char *,__kernel_size_t); |
| 47 | #endif |
David S. Miller | ded220b | 2007-03-29 01:18:42 -0700 | [diff] [blame] | 48 | #ifndef __HAVE_ARCH_STRCASECMP |
| 49 | extern int strcasecmp(const char *s1, const char *s2); |
| 50 | #endif |
| 51 | #ifndef __HAVE_ARCH_STRNCASECMP |
| 52 | extern int strncasecmp(const char *s1, const char *s2, size_t n); |
| 53 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #ifndef __HAVE_ARCH_STRCHR |
| 55 | extern char * strchr(const char *,int); |
| 56 | #endif |
Grant Likely | 11d200e | 2014-03-14 17:00:14 +0000 | [diff] [blame] | 57 | #ifndef __HAVE_ARCH_STRCHRNUL |
| 58 | extern char * strchrnul(const char *,int); |
| 59 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #ifndef __HAVE_ARCH_STRNCHR |
| 61 | extern char * strnchr(const char *, size_t, int); |
| 62 | #endif |
| 63 | #ifndef __HAVE_ARCH_STRRCHR |
| 64 | extern char * strrchr(const char *,int); |
| 65 | #endif |
André Goddard Rosa | f653398 | 2009-12-14 18:01:04 -0800 | [diff] [blame] | 66 | extern char * __must_check skip_spaces(const char *); |
KOSAKI Motohiro | ca54cb8 | 2009-12-14 18:01:15 -0800 | [diff] [blame] | 67 | |
| 68 | extern char *strim(char *); |
| 69 | |
| 70 | static inline __must_check char *strstrip(char *str) |
| 71 | { |
| 72 | return strim(str); |
| 73 | } |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #ifndef __HAVE_ARCH_STRSTR |
Li Zefan | d5f1fb5 | 2010-01-14 10:53:55 +0800 | [diff] [blame] | 76 | extern char * strstr(const char *, const char *); |
| 77 | #endif |
| 78 | #ifndef __HAVE_ARCH_STRNSTR |
| 79 | extern char * strnstr(const char *, const char *, size_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #endif |
| 81 | #ifndef __HAVE_ARCH_STRLEN |
| 82 | extern __kernel_size_t strlen(const char *); |
| 83 | #endif |
| 84 | #ifndef __HAVE_ARCH_STRNLEN |
| 85 | extern __kernel_size_t strnlen(const char *,__kernel_size_t); |
| 86 | #endif |
Kyle McMartin | 8833d32 | 2006-04-10 22:53:57 -0700 | [diff] [blame] | 87 | #ifndef __HAVE_ARCH_STRPBRK |
| 88 | extern char * strpbrk(const char *,const char *); |
| 89 | #endif |
| 90 | #ifndef __HAVE_ARCH_STRSEP |
| 91 | extern char * strsep(char **,const char *); |
| 92 | #endif |
| 93 | #ifndef __HAVE_ARCH_STRSPN |
| 94 | extern __kernel_size_t strspn(const char *,const char *); |
| 95 | #endif |
| 96 | #ifndef __HAVE_ARCH_STRCSPN |
| 97 | extern __kernel_size_t strcspn(const char *,const char *); |
| 98 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | #ifndef __HAVE_ARCH_MEMSET |
| 101 | extern void * memset(void *,int,__kernel_size_t); |
| 102 | #endif |
Matthew Wilcox | 3b3c4ba | 2017-09-08 16:13:48 -0700 | [diff] [blame] | 103 | |
| 104 | #ifndef __HAVE_ARCH_MEMSET16 |
| 105 | extern void *memset16(uint16_t *, uint16_t, __kernel_size_t); |
| 106 | #endif |
| 107 | |
| 108 | #ifndef __HAVE_ARCH_MEMSET32 |
| 109 | extern void *memset32(uint32_t *, uint32_t, __kernel_size_t); |
| 110 | #endif |
| 111 | |
| 112 | #ifndef __HAVE_ARCH_MEMSET64 |
| 113 | extern void *memset64(uint64_t *, uint64_t, __kernel_size_t); |
| 114 | #endif |
| 115 | |
| 116 | static inline void *memset_l(unsigned long *p, unsigned long v, |
| 117 | __kernel_size_t n) |
| 118 | { |
| 119 | if (BITS_PER_LONG == 32) |
| 120 | return memset32((uint32_t *)p, v, n); |
| 121 | else |
| 122 | return memset64((uint64_t *)p, v, n); |
| 123 | } |
| 124 | |
| 125 | static inline void *memset_p(void **p, void *v, __kernel_size_t n) |
| 126 | { |
| 127 | if (BITS_PER_LONG == 32) |
| 128 | return memset32((uint32_t *)p, (uintptr_t)v, n); |
| 129 | else |
| 130 | return memset64((uint64_t *)p, (uintptr_t)v, n); |
| 131 | } |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | #ifndef __HAVE_ARCH_MEMCPY |
| 134 | extern void * memcpy(void *,const void *,__kernel_size_t); |
| 135 | #endif |
| 136 | #ifndef __HAVE_ARCH_MEMMOVE |
| 137 | extern void * memmove(void *,const void *,__kernel_size_t); |
| 138 | #endif |
| 139 | #ifndef __HAVE_ARCH_MEMSCAN |
| 140 | extern void * memscan(void *,int,__kernel_size_t); |
| 141 | #endif |
| 142 | #ifndef __HAVE_ARCH_MEMCMP |
| 143 | extern int memcmp(const void *,const void *,__kernel_size_t); |
| 144 | #endif |
| 145 | #ifndef __HAVE_ARCH_MEMCHR |
| 146 | extern void * memchr(const void *,int,__kernel_size_t); |
| 147 | #endif |
Dan Williams | 6abccd1 | 2017-01-13 14:14:23 -0800 | [diff] [blame] | 148 | #ifndef __HAVE_ARCH_MEMCPY_MCSAFE |
| 149 | static inline __must_check int memcpy_mcsafe(void *dst, const void *src, |
| 150 | size_t cnt) |
| 151 | { |
| 152 | memcpy(dst, src, cnt); |
| 153 | return 0; |
| 154 | } |
| 155 | #endif |
Dan Williams | 0aed55a | 2017-05-29 12:22:50 -0700 | [diff] [blame] | 156 | #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE |
| 157 | static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) |
| 158 | { |
| 159 | memcpy(dst, src, cnt); |
| 160 | } |
| 161 | #endif |
Akinobu Mita | 79824820 | 2011-10-31 17:08:07 -0700 | [diff] [blame] | 162 | void *memchr_inv(const void *s, int c, size_t n); |
Rasmus Villemoes | 94df290 | 2015-06-25 15:02:22 -0700 | [diff] [blame] | 163 | char *strreplace(char *s, char old, char new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Andrzej Hajda | a4bb1e4 | 2015-02-13 14:36:24 -0800 | [diff] [blame] | 165 | extern void kfree_const(const void *x); |
| 166 | |
Rasmus Villemoes | 48a2705 | 2016-05-19 17:10:55 -0700 | [diff] [blame] | 167 | extern char *kstrdup(const char *s, gfp_t gfp) __malloc; |
Andrzej Hajda | a4bb1e4 | 2015-02-13 14:36:24 -0800 | [diff] [blame] | 168 | extern const char *kstrdup_const(const char *s, gfp_t gfp); |
Jeremy Fitzhardinge | 1e66df3 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 169 | extern char *kstrndup(const char *s, size_t len, gfp_t gfp); |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 170 | extern void *kmemdup(const void *src, size_t len, gfp_t gfp); |
David Howells | f351574 | 2017-07-04 17:25:02 +0100 | [diff] [blame] | 171 | 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] | 172 | |
Jeremy Fitzhardinge | d84d1cc | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 173 | extern char **argv_split(gfp_t gfp, const char *str, int *argcp); |
| 174 | extern void argv_free(char **argv); |
| 175 | |
David Brownell | 34990cf | 2008-05-01 04:34:42 -0700 | [diff] [blame] | 176 | extern bool sysfs_streq(const char *s1, const char *s2); |
Kees Cook | ef95159 | 2016-03-17 14:22:50 -0700 | [diff] [blame] | 177 | extern int kstrtobool(const char *s, bool *res); |
| 178 | static inline int strtobool(const char *s, bool *res) |
| 179 | { |
| 180 | return kstrtobool(s, res); |
| 181 | } |
David Brownell | 34990cf | 2008-05-01 04:34:42 -0700 | [diff] [blame] | 182 | |
Andy Shevchenko | 56b0608 | 2016-03-17 14:22:14 -0700 | [diff] [blame] | 183 | 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] | 184 | int __sysfs_match_string(const char * const *array, size_t n, const char *s); |
| 185 | |
| 186 | /** |
| 187 | * sysfs_match_string - matches given string in an array |
| 188 | * @_a: array of strings |
| 189 | * @_s: string to match with |
| 190 | * |
| 191 | * Helper for __sysfs_match_string(). Calculates the size of @a automatically. |
| 192 | */ |
| 193 | #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] | 194 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 195 | #ifdef CONFIG_BINARY_PRINTF |
| 196 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args); |
| 197 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf); |
| 198 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4); |
| 199 | #endif |
| 200 | |
Akinobu Mita | e108526e | 2008-07-23 21:26:44 -0700 | [diff] [blame] | 201 | 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] | 202 | const void *from, size_t available); |
Akinobu Mita | e108526e | 2008-07-23 21:26:44 -0700 | [diff] [blame] | 203 | |
Rusty Russell | 66f92cf | 2009-03-31 13:05:36 -0600 | [diff] [blame] | 204 | /** |
| 205 | * strstarts - does @str start with @prefix? |
| 206 | * @str: string to examine |
| 207 | * @prefix: prefix to look for. |
| 208 | */ |
| 209 | static inline bool strstarts(const char *str, const char *prefix) |
| 210 | { |
| 211 | return strncmp(str, prefix, strlen(prefix)) == 0; |
| 212 | } |
Akinobu Mita | 639b9e3 | 2012-07-30 14:40:55 -0700 | [diff] [blame] | 213 | |
Daniel Borkmann | d4c5efd | 2014-08-26 23:16:35 -0400 | [diff] [blame] | 214 | size_t memweight(const void *ptr, size_t bytes); |
| 215 | void memzero_explicit(void *s, size_t count); |
Akinobu Mita | 639b9e3 | 2012-07-30 14:40:55 -0700 | [diff] [blame] | 216 | |
Andy Shevchenko | b18888a | 2012-12-17 16:01:18 -0800 | [diff] [blame] | 217 | /** |
| 218 | * kbasename - return the last part of a pathname. |
| 219 | * |
| 220 | * @path: path to extract the filename from. |
| 221 | */ |
| 222 | static inline const char *kbasename(const char *path) |
| 223 | { |
| 224 | const char *tail = strrchr(path, '/'); |
| 225 | return tail ? tail + 1 : path; |
| 226 | } |
| 227 | |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 228 | #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline)) |
| 229 | #define __RENAME(x) __asm__(#x) |
| 230 | |
| 231 | void fortify_panic(const char *name) __noreturn __cold; |
| 232 | void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter"); |
| 233 | 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] | 234 | 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] | 235 | void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter"); |
| 236 | |
| 237 | #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE) |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 238 | __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size) |
| 239 | { |
| 240 | size_t p_size = __builtin_object_size(p, 0); |
| 241 | if (__builtin_constant_p(size) && p_size < size) |
| 242 | __write_overflow(); |
| 243 | if (p_size < size) |
| 244 | fortify_panic(__func__); |
| 245 | return __builtin_strncpy(p, q, size); |
| 246 | } |
| 247 | |
| 248 | __FORTIFY_INLINE char *strcat(char *p, const char *q) |
| 249 | { |
| 250 | size_t p_size = __builtin_object_size(p, 0); |
| 251 | if (p_size == (size_t)-1) |
| 252 | return __builtin_strcat(p, q); |
| 253 | if (strlcat(p, q, p_size) >= p_size) |
| 254 | fortify_panic(__func__); |
| 255 | return p; |
| 256 | } |
| 257 | |
| 258 | __FORTIFY_INLINE __kernel_size_t strlen(const char *p) |
| 259 | { |
| 260 | __kernel_size_t ret; |
| 261 | size_t p_size = __builtin_object_size(p, 0); |
Arnd Bergmann | 146734b | 2017-12-14 15:32:34 -0800 | [diff] [blame] | 262 | |
| 263 | /* Work around gcc excess stack consumption issue */ |
| 264 | if (p_size == (size_t)-1 || |
| 265 | (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0')) |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 266 | return __builtin_strlen(p); |
| 267 | ret = strnlen(p, p_size); |
| 268 | if (p_size <= ret) |
| 269 | fortify_panic(__func__); |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen); |
| 274 | __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen) |
| 275 | { |
| 276 | size_t p_size = __builtin_object_size(p, 0); |
| 277 | __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size); |
| 278 | if (p_size <= ret && maxlen != ret) |
| 279 | fortify_panic(__func__); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* defined after fortified strlen to reuse it */ |
| 284 | extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy); |
| 285 | __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size) |
| 286 | { |
| 287 | size_t ret; |
| 288 | size_t p_size = __builtin_object_size(p, 0); |
| 289 | size_t q_size = __builtin_object_size(q, 0); |
| 290 | if (p_size == (size_t)-1 && q_size == (size_t)-1) |
| 291 | return __real_strlcpy(p, q, size); |
| 292 | ret = strlen(q); |
| 293 | if (size) { |
| 294 | size_t len = (ret >= size) ? size - 1 : ret; |
| 295 | if (__builtin_constant_p(len) && len >= p_size) |
| 296 | __write_overflow(); |
| 297 | if (len >= p_size) |
| 298 | fortify_panic(__func__); |
| 299 | __builtin_memcpy(p, q, len); |
| 300 | p[len] = '\0'; |
| 301 | } |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | /* defined after fortified strlen and strnlen to reuse them */ |
| 306 | __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) |
| 307 | { |
| 308 | size_t p_len, copy_len; |
| 309 | size_t p_size = __builtin_object_size(p, 0); |
| 310 | size_t q_size = __builtin_object_size(q, 0); |
| 311 | if (p_size == (size_t)-1 && q_size == (size_t)-1) |
| 312 | return __builtin_strncat(p, q, count); |
| 313 | p_len = strlen(p); |
| 314 | copy_len = strnlen(q, count); |
| 315 | if (p_size < p_len + copy_len + 1) |
| 316 | fortify_panic(__func__); |
| 317 | __builtin_memcpy(p + p_len, q, copy_len); |
| 318 | p[p_len + copy_len] = '\0'; |
| 319 | return p; |
| 320 | } |
| 321 | |
| 322 | __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size) |
| 323 | { |
| 324 | size_t p_size = __builtin_object_size(p, 0); |
| 325 | if (__builtin_constant_p(size) && p_size < size) |
| 326 | __write_overflow(); |
| 327 | if (p_size < size) |
| 328 | fortify_panic(__func__); |
| 329 | return __builtin_memset(p, c, size); |
| 330 | } |
| 331 | |
| 332 | __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) |
| 333 | { |
| 334 | size_t p_size = __builtin_object_size(p, 0); |
| 335 | size_t q_size = __builtin_object_size(q, 0); |
| 336 | if (__builtin_constant_p(size)) { |
| 337 | if (p_size < size) |
| 338 | __write_overflow(); |
| 339 | if (q_size < size) |
| 340 | __read_overflow2(); |
| 341 | } |
| 342 | if (p_size < size || q_size < size) |
| 343 | fortify_panic(__func__); |
| 344 | return __builtin_memcpy(p, q, size); |
| 345 | } |
| 346 | |
| 347 | __FORTIFY_INLINE void *memmove(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_memmove(p, q, size); |
| 360 | } |
| 361 | |
| 362 | extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan); |
| 363 | __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size) |
| 364 | { |
| 365 | size_t p_size = __builtin_object_size(p, 0); |
| 366 | if (__builtin_constant_p(size) && p_size < size) |
| 367 | __read_overflow(); |
| 368 | if (p_size < size) |
| 369 | fortify_panic(__func__); |
| 370 | return __real_memscan(p, c, size); |
| 371 | } |
| 372 | |
| 373 | __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size) |
| 374 | { |
| 375 | size_t p_size = __builtin_object_size(p, 0); |
| 376 | size_t q_size = __builtin_object_size(q, 0); |
| 377 | if (__builtin_constant_p(size)) { |
| 378 | if (p_size < size) |
| 379 | __read_overflow(); |
| 380 | if (q_size < size) |
| 381 | __read_overflow2(); |
| 382 | } |
| 383 | if (p_size < size || q_size < size) |
| 384 | fortify_panic(__func__); |
| 385 | return __builtin_memcmp(p, q, size); |
| 386 | } |
| 387 | |
| 388 | __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size) |
| 389 | { |
| 390 | size_t p_size = __builtin_object_size(p, 0); |
| 391 | if (__builtin_constant_p(size) && p_size < size) |
| 392 | __read_overflow(); |
| 393 | if (p_size < size) |
| 394 | fortify_panic(__func__); |
| 395 | return __builtin_memchr(p, c, size); |
| 396 | } |
| 397 | |
| 398 | void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv); |
| 399 | __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size) |
| 400 | { |
| 401 | size_t p_size = __builtin_object_size(p, 0); |
| 402 | if (__builtin_constant_p(size) && p_size < size) |
| 403 | __read_overflow(); |
| 404 | if (p_size < size) |
| 405 | fortify_panic(__func__); |
| 406 | return __real_memchr_inv(p, c, size); |
| 407 | } |
| 408 | |
| 409 | extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup); |
| 410 | __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp) |
| 411 | { |
| 412 | size_t p_size = __builtin_object_size(p, 0); |
| 413 | if (__builtin_constant_p(size) && p_size < size) |
| 414 | __read_overflow(); |
| 415 | if (p_size < size) |
| 416 | fortify_panic(__func__); |
| 417 | return __real_kmemdup(p, size, gfp); |
| 418 | } |
Daniel Micay | 077d2ba | 2017-07-14 17:28:12 -0400 | [diff] [blame] | 419 | |
| 420 | /* defined after fortified strlen and memcpy to reuse them */ |
| 421 | __FORTIFY_INLINE char *strcpy(char *p, const char *q) |
| 422 | { |
| 423 | size_t p_size = __builtin_object_size(p, 0); |
| 424 | size_t q_size = __builtin_object_size(q, 0); |
| 425 | if (p_size == (size_t)-1 && q_size == (size_t)-1) |
| 426 | return __builtin_strcpy(p, q); |
| 427 | memcpy(p, q, strlen(q) + 1); |
| 428 | return p; |
| 429 | } |
| 430 | |
Daniel Micay | 6974f0c | 2017-07-12 14:36:10 -0700 | [diff] [blame] | 431 | #endif |
| 432 | |
Martin Wilck | 01f33c3 | 2017-08-14 22:12:38 +0200 | [diff] [blame] | 433 | /** |
| 434 | * memcpy_and_pad - Copy one buffer to another with padding |
| 435 | * @dest: Where to copy to |
| 436 | * @dest_len: The destination buffer size |
| 437 | * @src: Where to copy from |
| 438 | * @count: The number of bytes to copy |
| 439 | * @pad: Character to use for padding if space is left in destination. |
| 440 | */ |
Martin Wilck | 1359798 | 2017-09-06 14:36:57 +0200 | [diff] [blame] | 441 | static inline void memcpy_and_pad(void *dest, size_t dest_len, |
| 442 | const void *src, size_t count, int pad) |
Martin Wilck | 01f33c3 | 2017-08-14 22:12:38 +0200 | [diff] [blame] | 443 | { |
Martin Wilck | 01f33c3 | 2017-08-14 22:12:38 +0200 | [diff] [blame] | 444 | if (dest_len > count) { |
| 445 | memcpy(dest, src, count); |
| 446 | memset(dest + count, pad, dest_len - count); |
| 447 | } else |
| 448 | memcpy(dest, src, dest_len); |
| 449 | } |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | #endif /* _LINUX_STRING_H_ */ |