Skip to content

Commit 19559e8

Browse files
RtoaxAlexei Starovoitov
authored andcommitted
bpf: add bpf_strcasecmp kfunc
bpf_strcasecmp() function performs same like bpf_strcmp() except ignoring the case of the characters. Signed-off-by: Rong Tao <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Viktor Malik <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 525ac69 commit 19559e8

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

kernel/bpf/helpers.c

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,45 +3349,72 @@ __bpf_kfunc void __bpf_trap(void)
33493349
* __get_kernel_nofault instead of plain dereference to make them safe.
33503350
*/
33513351

3352-
/**
3353-
* bpf_strcmp - Compare two strings
3354-
* @s1__ign: One string
3355-
* @s2__ign: Another string
3356-
*
3357-
* Return:
3358-
* * %0 - Strings are equal
3359-
* * %-1 - @s1__ign is smaller
3360-
* * %1 - @s2__ign is smaller
3361-
* * %-EFAULT - Cannot read one of the strings
3362-
* * %-E2BIG - One of strings is too large
3363-
* * %-ERANGE - One of strings is outside of kernel address space
3364-
*/
3365-
__bpf_kfunc int bpf_strcmp(const char *s1__ign, const char *s2__ign)
3352+
static int __bpf_strcasecmp(const char *s1, const char *s2, bool ignore_case)
33663353
{
33673354
char c1, c2;
33683355
int i;
33693356

3370-
if (!copy_from_kernel_nofault_allowed(s1__ign, 1) ||
3371-
!copy_from_kernel_nofault_allowed(s2__ign, 1)) {
3357+
if (!copy_from_kernel_nofault_allowed(s1, 1) ||
3358+
!copy_from_kernel_nofault_allowed(s2, 1)) {
33723359
return -ERANGE;
33733360
}
33743361

33753362
guard(pagefault)();
33763363
for (i = 0; i < XATTR_SIZE_MAX; i++) {
3377-
__get_kernel_nofault(&c1, s1__ign, char, err_out);
3378-
__get_kernel_nofault(&c2, s2__ign, char, err_out);
3364+
__get_kernel_nofault(&c1, s1, char, err_out);
3365+
__get_kernel_nofault(&c2, s2, char, err_out);
3366+
if (ignore_case) {
3367+
c1 = tolower(c1);
3368+
c2 = tolower(c2);
3369+
}
33793370
if (c1 != c2)
33803371
return c1 < c2 ? -1 : 1;
33813372
if (c1 == '\0')
33823373
return 0;
3383-
s1__ign++;
3384-
s2__ign++;
3374+
s1++;
3375+
s2++;
33853376
}
33863377
return -E2BIG;
33873378
err_out:
33883379
return -EFAULT;
33893380
}
33903381

3382+
/**
3383+
* bpf_strcmp - Compare two strings
3384+
* @s1__ign: One string
3385+
* @s2__ign: Another string
3386+
*
3387+
* Return:
3388+
* * %0 - Strings are equal
3389+
* * %-1 - @s1__ign is smaller
3390+
* * %1 - @s2__ign is smaller
3391+
* * %-EFAULT - Cannot read one of the strings
3392+
* * %-E2BIG - One of strings is too large
3393+
* * %-ERANGE - One of strings is outside of kernel address space
3394+
*/
3395+
__bpf_kfunc int bpf_strcmp(const char *s1__ign, const char *s2__ign)
3396+
{
3397+
return __bpf_strcasecmp(s1__ign, s2__ign, false);
3398+
}
3399+
3400+
/**
3401+
* bpf_strcasecmp - Compare two strings, ignoring the case of the characters
3402+
* @s1__ign: One string
3403+
* @s2__ign: Another string
3404+
*
3405+
* Return:
3406+
* * %0 - Strings are equal
3407+
* * %-1 - @s1__ign is smaller
3408+
* * %1 - @s2__ign is smaller
3409+
* * %-EFAULT - Cannot read one of the strings
3410+
* * %-E2BIG - One of strings is too large
3411+
* * %-ERANGE - One of strings is outside of kernel address space
3412+
*/
3413+
__bpf_kfunc int bpf_strcasecmp(const char *s1__ign, const char *s2__ign)
3414+
{
3415+
return __bpf_strcasecmp(s1__ign, s2__ign, true);
3416+
}
3417+
33913418
/**
33923419
* bpf_strnchr - Find a character in a length limited string
33933420
* @s__ign: The string to be searched
@@ -3832,6 +3859,7 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
38323859
#endif
38333860
BTF_ID_FLAGS(func, __bpf_trap)
38343861
BTF_ID_FLAGS(func, bpf_strcmp);
3862+
BTF_ID_FLAGS(func, bpf_strcasecmp);
38353863
BTF_ID_FLAGS(func, bpf_strchr);
38363864
BTF_ID_FLAGS(func, bpf_strchrnul);
38373865
BTF_ID_FLAGS(func, bpf_strnchr);

0 commit comments

Comments
 (0)