Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'bpf-add-kfunc-bpf_strncasecmp'

Yuzuki Ishiyama says:

====================
bpf: Add kfunc bpf_strncasecmp()

This patchset introduces bpf_strncasecmp to allow case-insensitive and
limited-length string comparison. This is useful for parsing protocol
headers like HTTP.
---

Changes in v5:
- Fixed the test function numbering

Changes in v4:
- Updated the loop variable to maintain style consistency

Changes in v3:
- Use ternary operator to maintain style consistency
- Reverted unnecessary doc comment about XATTR_SIZE_MAX

Changes in v2:
- Compute max_sz upfront and remove len check from the loop body
- Document that @len is limited by XATTR_SIZE_MAX
====================

Link: https://patch.msgid.link/20260121033328.1850010-1-ishiyama@hpc.is.uec.ac.jp
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

+40 -5
+25 -5
kernel/bpf/helpers.c
··· 3413 3413 * __get_kernel_nofault instead of plain dereference to make them safe. 3414 3414 */ 3415 3415 3416 - static int __bpf_strcasecmp(const char *s1, const char *s2, bool ignore_case) 3416 + static int __bpf_strncasecmp(const char *s1, const char *s2, bool ignore_case, size_t len) 3417 3417 { 3418 3418 char c1, c2; 3419 3419 int i; ··· 3424 3424 } 3425 3425 3426 3426 guard(pagefault)(); 3427 - for (i = 0; i < XATTR_SIZE_MAX; i++) { 3427 + for (i = 0; i < len && i < XATTR_SIZE_MAX; i++) { 3428 3428 __get_kernel_nofault(&c1, s1, char, err_out); 3429 3429 __get_kernel_nofault(&c2, s2, char, err_out); 3430 3430 if (ignore_case) { ··· 3438 3438 s1++; 3439 3439 s2++; 3440 3440 } 3441 - return -E2BIG; 3441 + return i == XATTR_SIZE_MAX ? -E2BIG : 0; 3442 3442 err_out: 3443 3443 return -EFAULT; 3444 3444 } ··· 3458 3458 */ 3459 3459 __bpf_kfunc int bpf_strcmp(const char *s1__ign, const char *s2__ign) 3460 3460 { 3461 - return __bpf_strcasecmp(s1__ign, s2__ign, false); 3461 + return __bpf_strncasecmp(s1__ign, s2__ign, false, XATTR_SIZE_MAX); 3462 3462 } 3463 3463 3464 3464 /** ··· 3476 3476 */ 3477 3477 __bpf_kfunc int bpf_strcasecmp(const char *s1__ign, const char *s2__ign) 3478 3478 { 3479 - return __bpf_strcasecmp(s1__ign, s2__ign, true); 3479 + return __bpf_strncasecmp(s1__ign, s2__ign, true, XATTR_SIZE_MAX); 3480 + } 3481 + 3482 + /* 3483 + * bpf_strncasecmp - Compare two length-limited strings, ignoring case 3484 + * @s1__ign: One string 3485 + * @s2__ign: Another string 3486 + * @len: The maximum number of characters to compare 3487 + * 3488 + * Return: 3489 + * * %0 - Strings are equal 3490 + * * %-1 - @s1__ign is smaller 3491 + * * %1 - @s2__ign is smaller 3492 + * * %-EFAULT - Cannot read one of the strings 3493 + * * %-E2BIG - One of strings is too large 3494 + * * %-ERANGE - One of strings is outside of kernel address space 3495 + */ 3496 + __bpf_kfunc int bpf_strncasecmp(const char *s1__ign, const char *s2__ign, size_t len) 3497 + { 3498 + return __bpf_strncasecmp(s1__ign, s2__ign, true, len); 3480 3499 } 3481 3500 3482 3501 /** ··· 4545 4526 BTF_ID_FLAGS(func, __bpf_trap) 4546 4527 BTF_ID_FLAGS(func, bpf_strcmp); 4547 4528 BTF_ID_FLAGS(func, bpf_strcasecmp); 4529 + BTF_ID_FLAGS(func, bpf_strncasecmp); 4548 4530 BTF_ID_FLAGS(func, bpf_strchr); 4549 4531 BTF_ID_FLAGS(func, bpf_strchrnul); 4550 4532 BTF_ID_FLAGS(func, bpf_strnchr);
+1
tools/testing/selftests/bpf/prog_tests/string_kfuncs.c
··· 9 9 static const char * const test_cases[] = { 10 10 "strcmp", 11 11 "strcasecmp", 12 + "strncasecmp", 12 13 "strchr", 13 14 "strchrnul", 14 15 "strnchr",
+6
tools/testing/selftests/bpf/progs/string_kfuncs_failure1.c
··· 33 33 SEC("syscall") __retval(USER_PTR_ERR)int test_strcmp_null2(void *ctx) { return bpf_strcmp("hello", NULL); } 34 34 SEC("syscall") __retval(USER_PTR_ERR) int test_strcasecmp_null1(void *ctx) { return bpf_strcasecmp(NULL, "HELLO"); } 35 35 SEC("syscall") __retval(USER_PTR_ERR)int test_strcasecmp_null2(void *ctx) { return bpf_strcasecmp("HELLO", NULL); } 36 + SEC("syscall") __retval(USER_PTR_ERR)int test_strncasecmp_null1(void *ctx) { return bpf_strncasecmp(NULL, "HELLO", 5); } 37 + SEC("syscall") __retval(USER_PTR_ERR)int test_strncasecmp_null2(void *ctx) { return bpf_strncasecmp("HELLO", NULL, 5); } 36 38 SEC("syscall") __retval(USER_PTR_ERR)int test_strchr_null(void *ctx) { return bpf_strchr(NULL, 'a'); } 37 39 SEC("syscall") __retval(USER_PTR_ERR)int test_strchrnul_null(void *ctx) { return bpf_strchrnul(NULL, 'a'); } 38 40 SEC("syscall") __retval(USER_PTR_ERR)int test_strnchr_null(void *ctx) { return bpf_strnchr(NULL, 1, 'a'); } ··· 59 57 SEC("syscall") __retval(USER_PTR_ERR) int test_strcmp_user_ptr2(void *ctx) { return bpf_strcmp("hello", user_ptr); } 60 58 SEC("syscall") __retval(USER_PTR_ERR) int test_strcasecmp_user_ptr1(void *ctx) { return bpf_strcasecmp(user_ptr, "HELLO"); } 61 59 SEC("syscall") __retval(USER_PTR_ERR) int test_strcasecmp_user_ptr2(void *ctx) { return bpf_strcasecmp("HELLO", user_ptr); } 60 + SEC("syscall") __retval(USER_PTR_ERR) int test_strncasecmp_user_ptr1(void *ctx) { return bpf_strncasecmp(user_ptr, "HELLO", 5); } 61 + SEC("syscall") __retval(USER_PTR_ERR) int test_strncasecmp_user_ptr2(void *ctx) { return bpf_strncasecmp("HELLO", user_ptr, 5); } 62 62 SEC("syscall") __retval(USER_PTR_ERR) int test_strchr_user_ptr(void *ctx) { return bpf_strchr(user_ptr, 'a'); } 63 63 SEC("syscall") __retval(USER_PTR_ERR) int test_strchrnul_user_ptr(void *ctx) { return bpf_strchrnul(user_ptr, 'a'); } 64 64 SEC("syscall") __retval(USER_PTR_ERR) int test_strnchr_user_ptr(void *ctx) { return bpf_strnchr(user_ptr, 1, 'a'); } ··· 87 83 SEC("syscall") __retval(-EFAULT) int test_strcmp_pagefault2(void *ctx) { return bpf_strcmp("hello", invalid_kern_ptr); } 88 84 SEC("syscall") __retval(-EFAULT) int test_strcasecmp_pagefault1(void *ctx) { return bpf_strcasecmp(invalid_kern_ptr, "HELLO"); } 89 85 SEC("syscall") __retval(-EFAULT) int test_strcasecmp_pagefault2(void *ctx) { return bpf_strcasecmp("HELLO", invalid_kern_ptr); } 86 + SEC("syscall") __retval(-EFAULT) int test_strncasecmp_pagefault1(void *ctx) { return bpf_strncasecmp(invalid_kern_ptr, "HELLO", 5); } 87 + SEC("syscall") __retval(-EFAULT) int test_strncasecmp_pagefault2(void *ctx) { return bpf_strncasecmp("HELLO", invalid_kern_ptr, 5); } 90 88 SEC("syscall") __retval(-EFAULT) int test_strchr_pagefault(void *ctx) { return bpf_strchr(invalid_kern_ptr, 'a'); } 91 89 SEC("syscall") __retval(-EFAULT) int test_strchrnul_pagefault(void *ctx) { return bpf_strchrnul(invalid_kern_ptr, 'a'); } 92 90 SEC("syscall") __retval(-EFAULT) int test_strnchr_pagefault(void *ctx) { return bpf_strnchr(invalid_kern_ptr, 1, 'a'); }
+1
tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c
··· 8 8 9 9 SEC("syscall") int test_strcmp_too_long(void *ctx) { return bpf_strcmp(long_str, long_str); } 10 10 SEC("syscall") int test_strcasecmp_too_long(void *ctx) { return bpf_strcasecmp(long_str, long_str); } 11 + SEC("syscall") int test_strncasecmp_too_long(void *ctx) { return bpf_strncasecmp(long_str, long_str, sizeof(long_str)); } 11 12 SEC("syscall") int test_strchr_too_long(void *ctx) { return bpf_strchr(long_str, 'b'); } 12 13 SEC("syscall") int test_strchrnul_too_long(void *ctx) { return bpf_strchrnul(long_str, 'b'); } 13 14 SEC("syscall") int test_strnchr_too_long(void *ctx) { return bpf_strnchr(long_str, sizeof(long_str), 'b'); }
+7
tools/testing/selftests/bpf/progs/string_kfuncs_success.c
··· 17 17 __test(0) int test_strcasecmp_eq3(void *ctx) { return bpf_strcasecmp(str, "HELLO world"); } 18 18 __test(1) int test_strcasecmp_neq1(void *ctx) { return bpf_strcasecmp(str, "hello"); } 19 19 __test(1) int test_strcasecmp_neq2(void *ctx) { return bpf_strcasecmp(str, "HELLO"); } 20 + __test(0) int test_strncasecmp_eq1(void *ctx) { return bpf_strncasecmp(str, "hello world", 11); } 21 + __test(0) int test_strncasecmp_eq2(void *ctx) { return bpf_strncasecmp(str, "HELLO WORLD", 11); } 22 + __test(0) int test_strncasecmp_eq3(void *ctx) { return bpf_strncasecmp(str, "HELLO world", 11); } 23 + __test(0) int test_strncasecmp_eq4(void *ctx) { return bpf_strncasecmp(str, "hello", 5); } 24 + __test(0) int test_strncasecmp_eq5(void *ctx) { return bpf_strncasecmp(str, "hello world!", 11); } 25 + __test(-1) int test_strncasecmp_neq1(void *ctx) { return bpf_strncasecmp(str, "hello!", 6); } 26 + __test(1) int test_strncasecmp_neq2(void *ctx) { return bpf_strncasecmp(str, "abc", 3); } 20 27 __test(1) int test_strchr_found(void *ctx) { return bpf_strchr(str, 'e'); } 21 28 __test(11) int test_strchr_null(void *ctx) { return bpf_strchr(str, '\0'); } 22 29 __test(-ENOENT) int test_strchr_notfound(void *ctx) { return bpf_strchr(str, 'x'); }