hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/include/linux/string.h
....@@ -2,7 +2,6 @@
22 #ifndef _LINUX_STRING_H_
33 #define _LINUX_STRING_H_
44
5
-
65 #include <linux/compiler.h> /* for inline */
76 #include <linux/types.h> /* for size_t */
87 #include <linux/stddef.h> /* for NULL */
....@@ -62,6 +61,7 @@
6261 #ifndef __HAVE_ARCH_STRCHRNUL
6362 extern char * strchrnul(const char *,int);
6463 #endif
64
+extern char * strnchrnul(const char *, size_t, int);
6565 #ifndef __HAVE_ARCH_STRNCHR
6666 extern char * strnchr(const char *, size_t, int);
6767 #endif
....@@ -135,6 +135,13 @@
135135 return memset64((uint64_t *)p, (uintptr_t)v, n);
136136 }
137137
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
+
138145 #ifndef __HAVE_ARCH_MEMCPY
139146 extern void * memcpy(void *,const void *,__kernel_size_t);
140147 #endif
....@@ -153,20 +160,13 @@
153160 #ifndef __HAVE_ARCH_MEMCHR
154161 extern void * memchr(const void *,int,__kernel_size_t);
155162 #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
164163 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
165164 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
166165 {
167166 memcpy(dst, src, cnt);
168167 }
169168 #endif
169
+
170170 void *memchr_inv(const void *s, int c, size_t n);
171171 char *strreplace(char *s, char old, char new);
172172
....@@ -182,12 +182,6 @@
182182 extern void argv_free(char **argv);
183183
184184 extern bool sysfs_streq(const char *s1, const char *s2);
185
-extern int kstrtobool(const char *s, bool *res);
186
-static inline int strtobool(const char *s, bool *res)
187
-{
188
- return kstrtobool(s, res);
189
-}
190
-
191185 int match_string(const char * const *array, size_t n, const char *string);
192186 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
193187
....@@ -222,7 +216,26 @@
222216 }
223217
224218 size_t memweight(const void *ptr, size_t bytes);
225
-void memzero_explicit(void *s, size_t count);
219
+
220
+/**
221
+ * memzero_explicit - Fill a region of memory (e.g. sensitive
222
+ * keying data) with 0s.
223
+ * @s: Pointer to the start of the area.
224
+ * @count: The size of the area.
225
+ *
226
+ * Note: usually using memset() is just fine (!), but in cases
227
+ * where clearing out _local_ data at the end of a scope is
228
+ * necessary, memzero_explicit() should be used instead in
229
+ * order to prevent the compiler from optimising away zeroing.
230
+ *
231
+ * memzero_explicit() doesn't need an arch-specific version as
232
+ * it just invokes the one of memset() implicitly.
233
+ */
234
+static inline void memzero_explicit(void *s, size_t count)
235
+{
236
+ memset(s, 0, count);
237
+ barrier_data(s);
238
+}
226239
227240 /**
228241 * kbasename - return the last part of a pathname.
....@@ -246,7 +259,7 @@
246259
247260 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
248261
249
-#ifdef CONFIG_KASAN
262
+#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
250263 extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
251264 extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
252265 extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
....@@ -494,4 +507,25 @@
494507 memcpy(dest, src, dest_len);
495508 }
496509
510
+/**
511
+ * str_has_prefix - Test if a string has a given prefix
512
+ * @str: The string to test
513
+ * @prefix: The string to see if @str starts with
514
+ *
515
+ * A common way to test a prefix of a string is to do:
516
+ * strncmp(str, prefix, sizeof(prefix) - 1)
517
+ *
518
+ * But this can lead to bugs due to typos, or if prefix is a pointer
519
+ * and not a constant. Instead use str_has_prefix().
520
+ *
521
+ * Returns:
522
+ * * strlen(@prefix) if @str starts with @prefix
523
+ * * 0 if @str does not start with @prefix
524
+ */
525
+static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
526
+{
527
+ size_t len = strlen(prefix);
528
+ return strncmp(str, prefix, len) == 0 ? len : 0;
529
+}
530
+
497531 #endif /* _LINUX_STRING_H_ */