string.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STRING_H_
  3. #define _LINUX_STRING_H_
  4. #include <linux/compiler.h> /* for inline */
  5. #include <linux/types.h> /* for size_t */
  6. #include <linux/stddef.h> /* for NULL */
  7. #include <stdarg.h>
  8. #include <uapi/linux/string.h>
  9. extern char *strndup_user(const char __user *, long);
  10. extern void *memdup_user(const void __user *, size_t);
  11. extern void *vmemdup_user(const void __user *, size_t);
  12. extern void *memdup_user_nul(const void __user *, size_t);
  13. /*
  14. * Include machine specific inline routines
  15. */
  16. #include <asm/string.h>
  17. #ifndef __HAVE_ARCH_STRCPY
  18. extern char * strcpy(char *,const char *);
  19. #endif
  20. #ifndef __HAVE_ARCH_STRNCPY
  21. extern char * strncpy(char *,const char *, __kernel_size_t);
  22. #endif
  23. #ifndef __HAVE_ARCH_STRLCPY
  24. size_t strlcpy(char *, const char *, size_t);
  25. #endif
  26. #ifndef __HAVE_ARCH_STRSCPY
  27. ssize_t strscpy(char *, const char *, size_t);
  28. #endif
  29. /* Wraps calls to strscpy()/memset(), no arch specific code required */
  30. ssize_t strscpy_pad(char *dest, const char *src, size_t count);
  31. #ifndef __HAVE_ARCH_STRCAT
  32. extern char * strcat(char *, const char *);
  33. #endif
  34. #ifndef __HAVE_ARCH_STRNCAT
  35. extern char * strncat(char *, const char *, __kernel_size_t);
  36. #endif
  37. #ifndef __HAVE_ARCH_STRLCAT
  38. extern size_t strlcat(char *, const char *, __kernel_size_t);
  39. #endif
  40. #ifndef __HAVE_ARCH_STRCMP
  41. extern int strcmp(const char *,const char *);
  42. #endif
  43. #ifndef __HAVE_ARCH_STRNCMP
  44. extern int strncmp(const char *,const char *,__kernel_size_t);
  45. #endif
  46. #ifndef __HAVE_ARCH_STRCASECMP
  47. extern int strcasecmp(const char *s1, const char *s2);
  48. #endif
  49. #ifndef __HAVE_ARCH_STRNCASECMP
  50. extern int strncasecmp(const char *s1, const char *s2, size_t n);
  51. #endif
  52. #ifndef __HAVE_ARCH_STRCHR
  53. extern char * strchr(const char *,int);
  54. #endif
  55. #ifndef __HAVE_ARCH_STRCHRNUL
  56. extern char * strchrnul(const char *,int);
  57. #endif
  58. extern char * strnchrnul(const char *, size_t, int);
  59. #ifndef __HAVE_ARCH_STRNCHR
  60. extern char * strnchr(const char *, size_t, int);
  61. #endif
  62. #ifndef __HAVE_ARCH_STRRCHR
  63. extern char * strrchr(const char *,int);
  64. #endif
  65. extern char * __must_check skip_spaces(const char *);
  66. extern char *strim(char *);
  67. static inline __must_check char *strstrip(char *str)
  68. {
  69. return strim(str);
  70. }
  71. #ifndef __HAVE_ARCH_STRSTR
  72. extern char * strstr(const char *, const char *);
  73. #endif
  74. #ifndef __HAVE_ARCH_STRNSTR
  75. extern char * strnstr(const char *, const char *, size_t);
  76. #endif
  77. #ifndef __HAVE_ARCH_STRLEN
  78. extern __kernel_size_t strlen(const char *);
  79. #endif
  80. #ifndef __HAVE_ARCH_STRNLEN
  81. extern __kernel_size_t strnlen(const char *,__kernel_size_t);
  82. #endif
  83. #ifndef __HAVE_ARCH_STRPBRK
  84. extern char * strpbrk(const char *,const char *);
  85. #endif
  86. #ifndef __HAVE_ARCH_STRSEP
  87. extern char * strsep(char **,const char *);
  88. #endif
  89. #ifndef __HAVE_ARCH_STRSPN
  90. extern __kernel_size_t strspn(const char *,const char *);
  91. #endif
  92. #ifndef __HAVE_ARCH_STRCSPN
  93. extern __kernel_size_t strcspn(const char *,const char *);
  94. #endif
  95. #ifndef __HAVE_ARCH_MEMSET
  96. extern void * memset(void *,int,__kernel_size_t);
  97. #endif
  98. #ifndef __HAVE_ARCH_MEMSET16
  99. extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
  100. #endif
  101. #ifndef __HAVE_ARCH_MEMSET32
  102. extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
  103. #endif
  104. #ifndef __HAVE_ARCH_MEMSET64
  105. extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
  106. #endif
  107. static inline void *memset_l(unsigned long *p, unsigned long v,
  108. __kernel_size_t n)
  109. {
  110. if (BITS_PER_LONG == 32)
  111. return memset32((uint32_t *)p, v, n);
  112. else
  113. return memset64((uint64_t *)p, v, n);
  114. }
  115. static inline void *memset_p(void **p, void *v, __kernel_size_t n)
  116. {
  117. if (BITS_PER_LONG == 32)
  118. return memset32((uint32_t *)p, (uintptr_t)v, n);
  119. else
  120. return memset64((uint64_t *)p, (uintptr_t)v, n);
  121. }
  122. extern void **__memcat_p(void **a, void **b);
  123. #define memcat_p(a, b) ({ \
  124. BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
  125. "type mismatch in memcat_p()"); \
  126. (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
  127. })
  128. #ifndef __HAVE_ARCH_MEMCPY
  129. extern void * memcpy(void *,const void *,__kernel_size_t);
  130. #endif
  131. #ifndef __HAVE_ARCH_MEMMOVE
  132. extern void * memmove(void *,const void *,__kernel_size_t);
  133. #endif
  134. #ifndef __HAVE_ARCH_MEMSCAN
  135. extern void * memscan(void *,int,__kernel_size_t);
  136. #endif
  137. #ifndef __HAVE_ARCH_MEMCMP
  138. extern int memcmp(const void *,const void *,__kernel_size_t);
  139. #endif
  140. #ifndef __HAVE_ARCH_BCMP
  141. extern int bcmp(const void *,const void *,__kernel_size_t);
  142. #endif
  143. #ifndef __HAVE_ARCH_MEMCHR
  144. extern void * memchr(const void *,int,__kernel_size_t);
  145. #endif
  146. #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
  147. static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
  148. {
  149. memcpy(dst, src, cnt);
  150. }
  151. #endif
  152. void *memchr_inv(const void *s, int c, size_t n);
  153. char *strreplace(char *s, char old, char new);
  154. extern void kfree_const(const void *x);
  155. extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
  156. extern const char *kstrdup_const(const char *s, gfp_t gfp);
  157. extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
  158. extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
  159. extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
  160. extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
  161. extern void argv_free(char **argv);
  162. extern bool sysfs_streq(const char *s1, const char *s2);
  163. extern int kstrtobool(const char *s, bool *res);
  164. static inline int strtobool(const char *s, bool *res)
  165. {
  166. return kstrtobool(s, res);
  167. }
  168. int match_string(const char * const *array, size_t n, const char *string);
  169. int __sysfs_match_string(const char * const *array, size_t n, const char *s);
  170. /**
  171. * sysfs_match_string - matches given string in an array
  172. * @_a: array of strings
  173. * @_s: string to match with
  174. *
  175. * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
  176. */
  177. #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
  178. #ifdef CONFIG_BINARY_PRINTF
  179. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
  180. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
  181. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
  182. #endif
  183. extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  184. const void *from, size_t available);
  185. int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
  186. /**
  187. * strstarts - does @str start with @prefix?
  188. * @str: string to examine
  189. * @prefix: prefix to look for.
  190. */
  191. static inline bool strstarts(const char *str, const char *prefix)
  192. {
  193. return strncmp(str, prefix, strlen(prefix)) == 0;
  194. }
  195. size_t memweight(const void *ptr, size_t bytes);
  196. /**
  197. * memzero_explicit - Fill a region of memory (e.g. sensitive
  198. * keying data) with 0s.
  199. * @s: Pointer to the start of the area.
  200. * @count: The size of the area.
  201. *
  202. * Note: usually using memset() is just fine (!), but in cases
  203. * where clearing out _local_ data at the end of a scope is
  204. * necessary, memzero_explicit() should be used instead in
  205. * order to prevent the compiler from optimising away zeroing.
  206. *
  207. * memzero_explicit() doesn't need an arch-specific version as
  208. * it just invokes the one of memset() implicitly.
  209. */
  210. static inline void memzero_explicit(void *s, size_t count)
  211. {
  212. memset(s, 0, count);
  213. barrier_data(s);
  214. }
  215. /**
  216. * kbasename - return the last part of a pathname.
  217. *
  218. * @path: path to extract the filename from.
  219. */
  220. static inline const char *kbasename(const char *path)
  221. {
  222. const char *tail = strrchr(path, '/');
  223. return tail ? tail + 1 : path;
  224. }
  225. #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
  226. #define __RENAME(x) __asm__(#x)
  227. void fortify_panic(const char *name) __noreturn __cold;
  228. void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
  229. void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
  230. void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
  231. void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
  232. #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
  233. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  234. extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
  235. extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
  236. extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
  237. extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
  238. extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
  239. extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
  240. extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
  241. extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
  242. extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
  243. extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
  244. #else
  245. #define __underlying_memchr __builtin_memchr
  246. #define __underlying_memcmp __builtin_memcmp
  247. #define __underlying_memcpy __builtin_memcpy
  248. #define __underlying_memmove __builtin_memmove
  249. #define __underlying_memset __builtin_memset
  250. #define __underlying_strcat __builtin_strcat
  251. #define __underlying_strcpy __builtin_strcpy
  252. #define __underlying_strlen __builtin_strlen
  253. #define __underlying_strncat __builtin_strncat
  254. #define __underlying_strncpy __builtin_strncpy
  255. #endif
  256. __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
  257. {
  258. size_t p_size = __builtin_object_size(p, 0);
  259. if (__builtin_constant_p(size) && p_size < size)
  260. __write_overflow();
  261. if (p_size < size)
  262. fortify_panic(__func__);
  263. return __underlying_strncpy(p, q, size);
  264. }
  265. __FORTIFY_INLINE char *strcat(char *p, const char *q)
  266. {
  267. size_t p_size = __builtin_object_size(p, 0);
  268. if (p_size == (size_t)-1)
  269. return __underlying_strcat(p, q);
  270. if (strlcat(p, q, p_size) >= p_size)
  271. fortify_panic(__func__);
  272. return p;
  273. }
  274. __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
  275. {
  276. __kernel_size_t ret;
  277. size_t p_size = __builtin_object_size(p, 0);
  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'))
  281. return __underlying_strlen(p);
  282. ret = strnlen(p, p_size);
  283. if (p_size <= ret)
  284. fortify_panic(__func__);
  285. return ret;
  286. }
  287. extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
  288. __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
  289. {
  290. size_t p_size = __builtin_object_size(p, 0);
  291. __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
  292. if (p_size <= ret && maxlen != ret)
  293. fortify_panic(__func__);
  294. return ret;
  295. }
  296. /* defined after fortified strlen to reuse it */
  297. extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
  298. __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
  299. {
  300. size_t ret;
  301. size_t p_size = __builtin_object_size(p, 0);
  302. size_t q_size = __builtin_object_size(q, 0);
  303. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  304. return __real_strlcpy(p, q, size);
  305. ret = strlen(q);
  306. if (size) {
  307. size_t len = (ret >= size) ? size - 1 : ret;
  308. if (__builtin_constant_p(len) && len >= p_size)
  309. __write_overflow();
  310. if (len >= p_size)
  311. fortify_panic(__func__);
  312. __underlying_memcpy(p, q, len);
  313. p[len] = '\0';
  314. }
  315. return ret;
  316. }
  317. /* defined after fortified strlen and strnlen to reuse them */
  318. __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
  319. {
  320. size_t p_len, copy_len;
  321. size_t p_size = __builtin_object_size(p, 0);
  322. size_t q_size = __builtin_object_size(q, 0);
  323. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  324. return __underlying_strncat(p, q, count);
  325. p_len = strlen(p);
  326. copy_len = strnlen(q, count);
  327. if (p_size < p_len + copy_len + 1)
  328. fortify_panic(__func__);
  329. __underlying_memcpy(p + p_len, q, copy_len);
  330. p[p_len + copy_len] = '\0';
  331. return p;
  332. }
  333. __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
  334. {
  335. size_t p_size = __builtin_object_size(p, 0);
  336. if (__builtin_constant_p(size) && p_size < size)
  337. __write_overflow();
  338. if (p_size < size)
  339. fortify_panic(__func__);
  340. return __underlying_memset(p, c, size);
  341. }
  342. __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
  343. {
  344. size_t p_size = __builtin_object_size(p, 0);
  345. size_t q_size = __builtin_object_size(q, 0);
  346. if (__builtin_constant_p(size)) {
  347. if (p_size < size)
  348. __write_overflow();
  349. if (q_size < size)
  350. __read_overflow2();
  351. }
  352. if (p_size < size || q_size < size)
  353. fortify_panic(__func__);
  354. return __underlying_memcpy(p, q, size);
  355. }
  356. __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
  357. {
  358. size_t p_size = __builtin_object_size(p, 0);
  359. size_t q_size = __builtin_object_size(q, 0);
  360. if (__builtin_constant_p(size)) {
  361. if (p_size < size)
  362. __write_overflow();
  363. if (q_size < size)
  364. __read_overflow2();
  365. }
  366. if (p_size < size || q_size < size)
  367. fortify_panic(__func__);
  368. return __underlying_memmove(p, q, size);
  369. }
  370. extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
  371. __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
  372. {
  373. size_t p_size = __builtin_object_size(p, 0);
  374. if (__builtin_constant_p(size) && p_size < size)
  375. __read_overflow();
  376. if (p_size < size)
  377. fortify_panic(__func__);
  378. return __real_memscan(p, c, size);
  379. }
  380. __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
  381. {
  382. size_t p_size = __builtin_object_size(p, 0);
  383. size_t q_size = __builtin_object_size(q, 0);
  384. if (__builtin_constant_p(size)) {
  385. if (p_size < size)
  386. __read_overflow();
  387. if (q_size < size)
  388. __read_overflow2();
  389. }
  390. if (p_size < size || q_size < size)
  391. fortify_panic(__func__);
  392. return __underlying_memcmp(p, q, size);
  393. }
  394. __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
  395. {
  396. size_t p_size = __builtin_object_size(p, 0);
  397. if (__builtin_constant_p(size) && p_size < size)
  398. __read_overflow();
  399. if (p_size < size)
  400. fortify_panic(__func__);
  401. return __underlying_memchr(p, c, size);
  402. }
  403. void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
  404. __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
  405. {
  406. size_t p_size = __builtin_object_size(p, 0);
  407. if (__builtin_constant_p(size) && p_size < size)
  408. __read_overflow();
  409. if (p_size < size)
  410. fortify_panic(__func__);
  411. return __real_memchr_inv(p, c, size);
  412. }
  413. extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
  414. __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
  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_kmemdup(p, size, gfp);
  422. }
  423. /* defined after fortified strlen and memcpy to reuse them */
  424. __FORTIFY_INLINE char *strcpy(char *p, const char *q)
  425. {
  426. size_t p_size = __builtin_object_size(p, 0);
  427. size_t q_size = __builtin_object_size(q, 0);
  428. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  429. return __underlying_strcpy(p, q);
  430. memcpy(p, q, strlen(q) + 1);
  431. return p;
  432. }
  433. /* Don't use these outside the FORITFY_SOURCE implementation */
  434. #undef __underlying_memchr
  435. #undef __underlying_memcmp
  436. #undef __underlying_memcpy
  437. #undef __underlying_memmove
  438. #undef __underlying_memset
  439. #undef __underlying_strcat
  440. #undef __underlying_strcpy
  441. #undef __underlying_strlen
  442. #undef __underlying_strncat
  443. #undef __underlying_strncpy
  444. #endif
  445. /**
  446. * memcpy_and_pad - Copy one buffer to another with padding
  447. * @dest: Where to copy to
  448. * @dest_len: The destination buffer size
  449. * @src: Where to copy from
  450. * @count: The number of bytes to copy
  451. * @pad: Character to use for padding if space is left in destination.
  452. */
  453. static inline void memcpy_and_pad(void *dest, size_t dest_len,
  454. const void *src, size_t count, int pad)
  455. {
  456. if (dest_len > count) {
  457. memcpy(dest, src, count);
  458. memset(dest + count, pad, dest_len - count);
  459. } else
  460. memcpy(dest, src, dest_len);
  461. }
  462. /**
  463. * str_has_prefix - Test if a string has a given prefix
  464. * @str: The string to test
  465. * @prefix: The string to see if @str starts with
  466. *
  467. * A common way to test a prefix of a string is to do:
  468. * strncmp(str, prefix, sizeof(prefix) - 1)
  469. *
  470. * But this can lead to bugs due to typos, or if prefix is a pointer
  471. * and not a constant. Instead use str_has_prefix().
  472. *
  473. * Returns:
  474. * * strlen(@prefix) if @str starts with @prefix
  475. * * 0 if @str does not start with @prefix
  476. */
  477. static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
  478. {
  479. size_t len = strlen(prefix);
  480. return strncmp(str, prefix, len) == 0 ? len : 0;
  481. }
  482. #endif /* _LINUX_STRING_H_ */