hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/include/linux/string.h
....@@ -62,6 +62,7 @@
6262 #ifndef __HAVE_ARCH_STRCHRNUL
6363 extern char * strchrnul(const char *,int);
6464 #endif
65
+extern char * strnchrnul(const char *, size_t, int);
6566 #ifndef __HAVE_ARCH_STRNCHR
6667 extern char * strnchr(const char *, size_t, int);
6768 #endif
....@@ -135,6 +136,13 @@
135136 return memset64((uint64_t *)p, (uintptr_t)v, n);
136137 }
137138
139
+extern void **__memcat_p(void **a, void **b);
140
+#define memcat_p(a, b) ({ \
141
+ BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
142
+ "type mismatch in memcat_p()"); \
143
+ (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
144
+})
145
+
138146 #ifndef __HAVE_ARCH_MEMCPY
139147 extern void * memcpy(void *,const void *,__kernel_size_t);
140148 #endif
....@@ -153,20 +161,13 @@
153161 #ifndef __HAVE_ARCH_MEMCHR
154162 extern void * memchr(const void *,int,__kernel_size_t);
155163 #endif
156
-#ifndef __HAVE_ARCH_MEMCPY_MCSAFE
157
-static 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
164164 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
165165 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
166166 {
167167 memcpy(dst, src, cnt);
168168 }
169169 #endif
170
+
170171 void *memchr_inv(const void *s, int c, size_t n);
171172 char *strreplace(char *s, char old, char new);
172173
....@@ -222,7 +223,26 @@
222223 }
223224
224225 size_t memweight(const void *ptr, size_t bytes);
225
-void memzero_explicit(void *s, size_t count);
226
+
227
+/**
228
+ * memzero_explicit - Fill a region of memory (e.g. sensitive
229
+ * keying data) with 0s.
230
+ * @s: Pointer to the start of the area.
231
+ * @count: The size of the area.
232
+ *
233
+ * Note: usually using memset() is just fine (!), but in cases
234
+ * where clearing out _local_ data at the end of a scope is
235
+ * necessary, memzero_explicit() should be used instead in
236
+ * order to prevent the compiler from optimising away zeroing.
237
+ *
238
+ * memzero_explicit() doesn't need an arch-specific version as
239
+ * it just invokes the one of memset() implicitly.
240
+ */
241
+static inline void memzero_explicit(void *s, size_t count)
242
+{
243
+ memset(s, 0, count);
244
+ barrier_data(s);
245
+}
226246
227247 /**
228248 * kbasename - return the last part of a pathname.
....@@ -246,7 +266,7 @@
246266
247267 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
248268
249
-#ifdef CONFIG_KASAN
269
+#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
250270 extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
251271 extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
252272 extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
....@@ -494,4 +514,25 @@
494514 memcpy(dest, src, dest_len);
495515 }
496516
517
+/**
518
+ * str_has_prefix - Test if a string has a given prefix
519
+ * @str: The string to test
520
+ * @prefix: The string to see if @str starts with
521
+ *
522
+ * A common way to test a prefix of a string is to do:
523
+ * strncmp(str, prefix, sizeof(prefix) - 1)
524
+ *
525
+ * But this can lead to bugs due to typos, or if prefix is a pointer
526
+ * and not a constant. Instead use str_has_prefix().
527
+ *
528
+ * Returns:
529
+ * * strlen(@prefix) if @str starts with @prefix
530
+ * * 0 if @str does not start with @prefix
531
+ */
532
+static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
533
+{
534
+ size_t len = strlen(prefix);
535
+ return strncmp(str, prefix, len) == 0 ? len : 0;
536
+}
537
+
497538 #endif /* _LINUX_STRING_H_ */