Skip to content

Commit 2e36338

Browse files
author
Alexei Starovoitov
committed
Merge branch 'add-kfuncs-bpf_strcasestr-and-bpf_strncasestr'
Rong Tao says: ==================== Add kfuncs bpf_strcasestr and bpf_strncasestr From: Rong Tao <[email protected]> Add kfuncs bpf_strcasestr and bpf_strncasestr, which are extensions of bpf_strstr and bpf_strnstr, suitable for more scenarios. v4: Fix wrong comment. v3: keep __bpf_strnstr() static and compress some tests. https://lore.kernel.org/lkml/[email protected]/ v2: remove extra __bpf_kfunc and fix comment of bpf_strncasestr(). https://lore.kernel.org/all/[email protected]/ v1: https://lore.kernel.org/all/[email protected]/ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 17566cf + eca0b64 commit 2e36338

File tree

5 files changed

+103
-21
lines changed

5 files changed

+103
-21
lines changed

kernel/bpf/helpers.c

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,34 +3675,21 @@ __bpf_kfunc int bpf_strcspn(const char *s__ign, const char *reject__ign)
36753675
return -EFAULT;
36763676
}
36773677

3678-
/**
3679-
* bpf_strnstr - Find the first substring in a length-limited string
3680-
* @s1__ign: The string to be searched
3681-
* @s2__ign: The string to search for
3682-
* @len: the maximum number of characters to search
3683-
*
3684-
* Return:
3685-
* * >=0 - Index of the first character of the first occurrence of @s2__ign
3686-
* within the first @len characters of @s1__ign
3687-
* * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign
3688-
* * %-EFAULT - Cannot read one of the strings
3689-
* * %-E2BIG - One of the strings is too large
3690-
* * %-ERANGE - One of the strings is outside of kernel address space
3691-
*/
3692-
__bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len)
3678+
static int __bpf_strnstr(const char *s1, const char *s2, size_t len,
3679+
bool ignore_case)
36933680
{
36943681
char c1, c2;
36953682
int i, j;
36963683

3697-
if (!copy_from_kernel_nofault_allowed(s1__ign, 1) ||
3698-
!copy_from_kernel_nofault_allowed(s2__ign, 1)) {
3684+
if (!copy_from_kernel_nofault_allowed(s1, 1) ||
3685+
!copy_from_kernel_nofault_allowed(s2, 1)) {
36993686
return -ERANGE;
37003687
}
37013688

37023689
guard(pagefault)();
37033690
for (i = 0; i < XATTR_SIZE_MAX; i++) {
37043691
for (j = 0; i + j <= len && j < XATTR_SIZE_MAX; j++) {
3705-
__get_kernel_nofault(&c2, s2__ign + j, char, err_out);
3692+
__get_kernel_nofault(&c2, s2 + j, char, err_out);
37063693
if (c2 == '\0')
37073694
return i;
37083695
/*
@@ -3712,7 +3699,13 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
37123699
*/
37133700
if (i + j == len)
37143701
break;
3715-
__get_kernel_nofault(&c1, s1__ign + j, char, err_out);
3702+
__get_kernel_nofault(&c1, s1 + j, char, err_out);
3703+
3704+
if (ignore_case) {
3705+
c1 = tolower(c1);
3706+
c2 = tolower(c2);
3707+
}
3708+
37163709
if (c1 == '\0')
37173710
return -ENOENT;
37183711
if (c1 != c2)
@@ -3722,7 +3715,7 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
37223715
return -E2BIG;
37233716
if (i + j == len)
37243717
return -ENOENT;
3725-
s1__ign++;
3718+
s1++;
37263719
}
37273720
return -E2BIG;
37283721
err_out:
@@ -3744,8 +3737,69 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
37443737
*/
37453738
__bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign)
37463739
{
3747-
return bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX);
3740+
return __bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX, false);
3741+
}
3742+
3743+
/**
3744+
* bpf_strcasestr - Find the first substring in a string, ignoring the case of
3745+
* the characters
3746+
* @s1__ign: The string to be searched
3747+
* @s2__ign: The string to search for
3748+
*
3749+
* Return:
3750+
* * >=0 - Index of the first character of the first occurrence of @s2__ign
3751+
* within @s1__ign
3752+
* * %-ENOENT - @s2__ign is not a substring of @s1__ign
3753+
* * %-EFAULT - Cannot read one of the strings
3754+
* * %-E2BIG - One of the strings is too large
3755+
* * %-ERANGE - One of the strings is outside of kernel address space
3756+
*/
3757+
__bpf_kfunc int bpf_strcasestr(const char *s1__ign, const char *s2__ign)
3758+
{
3759+
return __bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX, true);
37483760
}
3761+
3762+
/**
3763+
* bpf_strnstr - Find the first substring in a length-limited string
3764+
* @s1__ign: The string to be searched
3765+
* @s2__ign: The string to search for
3766+
* @len: the maximum number of characters to search
3767+
*
3768+
* Return:
3769+
* * >=0 - Index of the first character of the first occurrence of @s2__ign
3770+
* within the first @len characters of @s1__ign
3771+
* * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign
3772+
* * %-EFAULT - Cannot read one of the strings
3773+
* * %-E2BIG - One of the strings is too large
3774+
* * %-ERANGE - One of the strings is outside of kernel address space
3775+
*/
3776+
__bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign,
3777+
size_t len)
3778+
{
3779+
return __bpf_strnstr(s1__ign, s2__ign, len, false);
3780+
}
3781+
3782+
/**
3783+
* bpf_strncasestr - Find the first substring in a length-limited string,
3784+
* ignoring the case of the characters
3785+
* @s1__ign: The string to be searched
3786+
* @s2__ign: The string to search for
3787+
* @len: the maximum number of characters to search
3788+
*
3789+
* Return:
3790+
* * >=0 - Index of the first character of the first occurrence of @s2__ign
3791+
* within the first @len characters of @s1__ign
3792+
* * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign
3793+
* * %-EFAULT - Cannot read one of the strings
3794+
* * %-E2BIG - One of the strings is too large
3795+
* * %-ERANGE - One of the strings is outside of kernel address space
3796+
*/
3797+
__bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign,
3798+
size_t len)
3799+
{
3800+
return __bpf_strnstr(s1__ign, s2__ign, len, true);
3801+
}
3802+
37493803
#ifdef CONFIG_KEYS
37503804
/**
37513805
* bpf_lookup_user_key - lookup a key by its serial
@@ -4367,7 +4421,9 @@ BTF_ID_FLAGS(func, bpf_strnlen);
43674421
BTF_ID_FLAGS(func, bpf_strspn);
43684422
BTF_ID_FLAGS(func, bpf_strcspn);
43694423
BTF_ID_FLAGS(func, bpf_strstr);
4424+
BTF_ID_FLAGS(func, bpf_strcasestr);
43704425
BTF_ID_FLAGS(func, bpf_strnstr);
4426+
BTF_ID_FLAGS(func, bpf_strncasestr);
43714427
#if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS)
43724428
BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
43734429
#endif

tools/testing/selftests/bpf/prog_tests/string_kfuncs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ static const char * const test_cases[] = {
2020
"strcspn_str",
2121
"strcspn_reject",
2222
"strstr",
23+
"strcasestr",
2324
"strnstr",
25+
"strncasestr",
2426
};
2527

2628
void run_too_long_tests(void)

tools/testing/selftests/bpf/progs/string_kfuncs_failure1.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ SEC("syscall") __retval(USER_PTR_ERR)int test_strcspn_null1(void *ctx) { return
4545
SEC("syscall") __retval(USER_PTR_ERR)int test_strcspn_null2(void *ctx) { return bpf_strcspn("hello", NULL); }
4646
SEC("syscall") __retval(USER_PTR_ERR)int test_strstr_null1(void *ctx) { return bpf_strstr(NULL, "hello"); }
4747
SEC("syscall") __retval(USER_PTR_ERR)int test_strstr_null2(void *ctx) { return bpf_strstr("hello", NULL); }
48+
SEC("syscall") __retval(USER_PTR_ERR)int test_strcasestr_null1(void *ctx) { return bpf_strcasestr(NULL, "hello"); }
49+
SEC("syscall") __retval(USER_PTR_ERR)int test_strcasestr_null2(void *ctx) { return bpf_strcasestr("hello", NULL); }
4850
SEC("syscall") __retval(USER_PTR_ERR)int test_strnstr_null1(void *ctx) { return bpf_strnstr(NULL, "hello", 1); }
4951
SEC("syscall") __retval(USER_PTR_ERR)int test_strnstr_null2(void *ctx) { return bpf_strnstr("hello", NULL, 1); }
52+
SEC("syscall") __retval(USER_PTR_ERR)int test_strncasestr_null1(void *ctx) { return bpf_strncasestr(NULL, "hello", 1); }
53+
SEC("syscall") __retval(USER_PTR_ERR)int test_strncasestr_null2(void *ctx) { return bpf_strncasestr("hello", NULL, 1); }
5054

5155
/* Passing userspace ptr to string kfuncs */
5256
SEC("syscall") __retval(USER_PTR_ERR) int test_strcmp_user_ptr1(void *ctx) { return bpf_strcmp(user_ptr, "hello"); }
@@ -65,8 +69,12 @@ SEC("syscall") __retval(USER_PTR_ERR) int test_strcspn_user_ptr1(void *ctx) { re
6569
SEC("syscall") __retval(USER_PTR_ERR) int test_strcspn_user_ptr2(void *ctx) { return bpf_strcspn("hello", user_ptr); }
6670
SEC("syscall") __retval(USER_PTR_ERR) int test_strstr_user_ptr1(void *ctx) { return bpf_strstr(user_ptr, "hello"); }
6771
SEC("syscall") __retval(USER_PTR_ERR) int test_strstr_user_ptr2(void *ctx) { return bpf_strstr("hello", user_ptr); }
72+
SEC("syscall") __retval(USER_PTR_ERR) int test_strcasestr_user_ptr1(void *ctx) { return bpf_strcasestr(user_ptr, "hello"); }
73+
SEC("syscall") __retval(USER_PTR_ERR) int test_strcasestr_user_ptr2(void *ctx) { return bpf_strcasestr("hello", user_ptr); }
6874
SEC("syscall") __retval(USER_PTR_ERR) int test_strnstr_user_ptr1(void *ctx) { return bpf_strnstr(user_ptr, "hello", 1); }
6975
SEC("syscall") __retval(USER_PTR_ERR) int test_strnstr_user_ptr2(void *ctx) { return bpf_strnstr("hello", user_ptr, 1); }
76+
SEC("syscall") __retval(USER_PTR_ERR) int test_strncasestr_user_ptr1(void *ctx) { return bpf_strncasestr(user_ptr, "hello", 1); }
77+
SEC("syscall") __retval(USER_PTR_ERR) int test_strncasestr_user_ptr2(void *ctx) { return bpf_strncasestr("hello", user_ptr, 1); }
7078

7179
#endif /* __TARGET_ARCH_s390 */
7280

@@ -87,7 +95,11 @@ SEC("syscall") __retval(-EFAULT) int test_strcspn_pagefault1(void *ctx) { return
8795
SEC("syscall") __retval(-EFAULT) int test_strcspn_pagefault2(void *ctx) { return bpf_strcspn("hello", invalid_kern_ptr); }
8896
SEC("syscall") __retval(-EFAULT) int test_strstr_pagefault1(void *ctx) { return bpf_strstr(invalid_kern_ptr, "hello"); }
8997
SEC("syscall") __retval(-EFAULT) int test_strstr_pagefault2(void *ctx) { return bpf_strstr("hello", invalid_kern_ptr); }
98+
SEC("syscall") __retval(-EFAULT) int test_strcasestr_pagefault1(void *ctx) { return bpf_strcasestr(invalid_kern_ptr, "hello"); }
99+
SEC("syscall") __retval(-EFAULT) int test_strcasestr_pagefault2(void *ctx) { return bpf_strcasestr("hello", invalid_kern_ptr); }
90100
SEC("syscall") __retval(-EFAULT) int test_strnstr_pagefault1(void *ctx) { return bpf_strnstr(invalid_kern_ptr, "hello", 1); }
91101
SEC("syscall") __retval(-EFAULT) int test_strnstr_pagefault2(void *ctx) { return bpf_strnstr("hello", invalid_kern_ptr, 1); }
102+
SEC("syscall") __retval(-EFAULT) int test_strncasestr_pagefault1(void *ctx) { return bpf_strncasestr(invalid_kern_ptr, "hello", 1); }
103+
SEC("syscall") __retval(-EFAULT) int test_strncasestr_pagefault2(void *ctx) { return bpf_strncasestr("hello", invalid_kern_ptr, 1); }
92104

93105
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/progs/string_kfuncs_failure2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ SEC("syscall") int test_strspn_accept_too_long(void *ctx) { return bpf_strspn("b
1919
SEC("syscall") int test_strcspn_str_too_long(void *ctx) { return bpf_strcspn(long_str, "b"); }
2020
SEC("syscall") int test_strcspn_reject_too_long(void *ctx) { return bpf_strcspn("b", long_str); }
2121
SEC("syscall") int test_strstr_too_long(void *ctx) { return bpf_strstr(long_str, "hello"); }
22+
SEC("syscall") int test_strcasestr_too_long(void *ctx) { return bpf_strcasestr(long_str, "hello"); }
2223
SEC("syscall") int test_strnstr_too_long(void *ctx) { return bpf_strnstr(long_str, "hello", sizeof(long_str)); }
24+
SEC("syscall") int test_strncasestr_too_long(void *ctx) { return bpf_strncasestr(long_str, "hello", sizeof(long_str)); }
2325

2426
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/progs/string_kfuncs_success.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,24 @@ __test(11) int test_strnlen(void *ctx) { return bpf_strnlen(str, 12); }
3333
__test(5) int test_strspn(void *ctx) { return bpf_strspn(str, "ehlo"); }
3434
__test(2) int test_strcspn(void *ctx) { return bpf_strcspn(str, "lo"); }
3535
__test(6) int test_strstr_found(void *ctx) { return bpf_strstr(str, "world"); }
36+
__test(6) int test_strcasestr_found(void *ctx) { return bpf_strcasestr(str, "woRLD"); }
3637
__test(-ENOENT) int test_strstr_notfound(void *ctx) { return bpf_strstr(str, "hi"); }
38+
__test(-ENOENT) int test_strcasestr_notfound(void *ctx) { return bpf_strcasestr(str, "hi"); }
3739
__test(0) int test_strstr_empty(void *ctx) { return bpf_strstr(str, ""); }
40+
__test(0) int test_strcasestr_empty(void *ctx) { return bpf_strcasestr(str, ""); }
3841
__test(0) int test_strnstr_found1(void *ctx) { return bpf_strnstr("", "", 0); }
3942
__test(0) int test_strnstr_found2(void *ctx) { return bpf_strnstr(str, "hello", 5); }
4043
__test(0) int test_strnstr_found3(void *ctx) { return bpf_strnstr(str, "hello", 6); }
4144
__test(-ENOENT) int test_strnstr_notfound1(void *ctx) { return bpf_strnstr(str, "hi", 10); }
4245
__test(-ENOENT) int test_strnstr_notfound2(void *ctx) { return bpf_strnstr(str, "hello", 4); }
4346
__test(-ENOENT) int test_strnstr_notfound3(void *ctx) { return bpf_strnstr("", "a", 0); }
4447
__test(0) int test_strnstr_empty(void *ctx) { return bpf_strnstr(str, "", 1); }
48+
__test(0) int test_strncasestr_found1(void *ctx) { return bpf_strncasestr("", "", 0); }
49+
__test(0) int test_strncasestr_found2(void *ctx) { return bpf_strncasestr(str, "heLLO", 5); }
50+
__test(0) int test_strncasestr_found3(void *ctx) { return bpf_strncasestr(str, "heLLO", 6); }
51+
__test(-ENOENT) int test_strncasestr_notfound1(void *ctx) { return bpf_strncasestr(str, "hi", 10); }
52+
__test(-ENOENT) int test_strncasestr_notfound2(void *ctx) { return bpf_strncasestr(str, "hello", 4); }
53+
__test(-ENOENT) int test_strncasestr_notfound3(void *ctx) { return bpf_strncasestr("", "a", 0); }
54+
__test(0) int test_strncasestr_empty(void *ctx) { return bpf_strncasestr(str, "", 1); }
4555

4656
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)