Skip to content

Commit fccb2db

Browse files
RtoaxKernel Patches Daemon
authored andcommitted
bpf: add bpf_strcasestr,bpf_strncasestr kfuncs
bpf_strcasestr() and bpf_strncasestr() functions perform same like bpf_strstr() and bpf_strnstr() except ignoring the case of the characters. Signed-off-by: Rong Tao <[email protected]>
1 parent 879ec96 commit fccb2db

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed

kernel/bpf/helpers.c

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,34 +3675,20 @@ __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+
int __bpf_strnstr(const char *s1, const char *s2, size_t len, bool ignore_case)
36933679
{
36943680
char c1, c2;
36953681
int i, j;
36963682

3697-
if (!copy_from_kernel_nofault_allowed(s1__ign, 1) ||
3698-
!copy_from_kernel_nofault_allowed(s2__ign, 1)) {
3683+
if (!copy_from_kernel_nofault_allowed(s1, 1) ||
3684+
!copy_from_kernel_nofault_allowed(s2, 1)) {
36993685
return -ERANGE;
37003686
}
37013687

37023688
guard(pagefault)();
37033689
for (i = 0; i < XATTR_SIZE_MAX; i++) {
37043690
for (j = 0; i + j <= len && j < XATTR_SIZE_MAX; j++) {
3705-
__get_kernel_nofault(&c2, s2__ign + j, char, err_out);
3691+
__get_kernel_nofault(&c2, s2 + j, char, err_out);
37063692
if (c2 == '\0')
37073693
return i;
37083694
/*
@@ -3712,7 +3698,13 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
37123698
*/
37133699
if (i + j == len)
37143700
break;
3715-
__get_kernel_nofault(&c1, s1__ign + j, char, err_out);
3701+
__get_kernel_nofault(&c1, s1 + j, char, err_out);
3702+
3703+
if (ignore_case) {
3704+
c1 = tolower(c1);
3705+
c2 = tolower(c2);
3706+
}
3707+
37163708
if (c1 == '\0')
37173709
return -ENOENT;
37183710
if (c1 != c2)
@@ -3722,7 +3714,7 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
37223714
return -E2BIG;
37233715
if (i + j == len)
37243716
return -ENOENT;
3725-
s1__ign++;
3717+
s1++;
37263718
}
37273719
return -E2BIG;
37283720
err_out:
@@ -3744,8 +3736,68 @@ __bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len
37443736
*/
37453737
__bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign)
37463738
{
3747-
return bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX);
3739+
return __bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX, false);
3740+
}
3741+
3742+
/**
3743+
* bpf_strcasestr - Find the first substring in a string, ignoring the case of
3744+
* the characters
3745+
* @s1__ign: The string to be searched
3746+
* @s2__ign: The string to search for
3747+
*
3748+
* Return:
3749+
* * >=0 - Index of the first character of the first occurrence of @s2__ign
3750+
* within @s1__ign
3751+
* * %-ENOENT - @s2__ign is not a substring of @s1__ign
3752+
* * %-EFAULT - Cannot read one of the strings
3753+
* * %-E2BIG - One of the strings is too large
3754+
* * %-ERANGE - One of the strings is outside of kernel address space
3755+
*/
3756+
__bpf_kfunc int bpf_strcasestr(const char *s1__ign, const char *s2__ign)
3757+
{
3758+
return __bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX, true);
37483759
}
3760+
3761+
/**
3762+
* bpf_strnstr - Find the first substring in a length-limited string
3763+
* @s1__ign: The string to be searched
3764+
* @s2__ign: The string to search for
3765+
* @len: the maximum number of characters to search
3766+
*
3767+
* Return:
3768+
* * >=0 - Index of the first character of the first occurrence of @s2__ign
3769+
* within the first @len characters of @s1__ign
3770+
* * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign
3771+
* * %-EFAULT - Cannot read one of the strings
3772+
* * %-E2BIG - One of the strings is too large
3773+
* * %-ERANGE - One of the strings is outside of kernel address space
3774+
*/
3775+
__bpf_kfunc int bpf_strnstr(const char *s1__ign, const char *s2__ign, size_t len)
3776+
{
3777+
return __bpf_strnstr(s1__ign, s2__ign, len, false);
3778+
}
3779+
3780+
/**
3781+
* bpf_strnstr - Find the first substring in a length-limited string, ignoring
3782+
* the case of the characters
3783+
* @s1__ign: The string to be searched
3784+
* @s2__ign: The string to search for
3785+
* @len: the maximum number of characters to search
3786+
*
3787+
* Return:
3788+
* * >=0 - Index of the first character of the first occurrence of @s2__ign
3789+
* within the first @len characters of @s1__ign
3790+
* * %-ENOENT - @s2__ign not found in the first @len characters of @s1__ign
3791+
* * %-EFAULT - Cannot read one of the strings
3792+
* * %-E2BIG - One of the strings is too large
3793+
* * %-ERANGE - One of the strings is outside of kernel address space
3794+
*/
3795+
__bpf_kfunc int bpf_strncasestr(const char *s1__ign, const char *s2__ign,
3796+
size_t len)
3797+
{
3798+
return __bpf_strnstr(s1__ign, s2__ign, len, true);
3799+
}
3800+
37493801
#ifdef CONFIG_KEYS
37503802
/**
37513803
* bpf_lookup_user_key - lookup a key by its serial
@@ -4367,7 +4419,9 @@ BTF_ID_FLAGS(func, bpf_strnlen);
43674419
BTF_ID_FLAGS(func, bpf_strspn);
43684420
BTF_ID_FLAGS(func, bpf_strcspn);
43694421
BTF_ID_FLAGS(func, bpf_strstr);
4422+
BTF_ID_FLAGS(func, bpf_strcasestr);
43704423
BTF_ID_FLAGS(func, bpf_strnstr);
4424+
BTF_ID_FLAGS(func, bpf_strncasestr);
43714425
#if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS)
43724426
BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
43734427
#endif

0 commit comments

Comments
 (0)