Skip to content

Commit c5fb199

Browse files
Hou TaoAlexei Starovoitov
Hou Tao
authored and
Alexei Starovoitov
committed
bpf: Add bpf_strncmp helper
The helper compares two strings: one string is a null-terminated read-only string, and another string has const max storage size but doesn't need to be null-terminated. It can be used to compare file name in tracing or LSM program. Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 259172b commit c5fb199

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,7 @@ extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
21632163
extern const struct bpf_func_proto bpf_kallsyms_lookup_name_proto;
21642164
extern const struct bpf_func_proto bpf_find_vma_proto;
21652165
extern const struct bpf_func_proto bpf_loop_proto;
2166+
extern const struct bpf_func_proto bpf_strncmp_proto;
21662167

21672168
const struct bpf_func_proto *tracing_prog_func_proto(
21682169
enum bpf_func_id func_id, const struct bpf_prog *prog);

include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -4983,6 +4983,16 @@ union bpf_attr {
49834983
* Return
49844984
* The number of loops performed, **-EINVAL** for invalid **flags**,
49854985
* **-E2BIG** if **nr_loops** exceeds the maximum number of loops.
4986+
*
4987+
* long bpf_strncmp(const char *s1, u32 s1_sz, const char *s2)
4988+
* Description
4989+
* Do strncmp() between **s1** and **s2**. **s1** doesn't need
4990+
* to be null-terminated and **s1_sz** is the maximum storage
4991+
* size of **s1**. **s2** must be a read-only string.
4992+
* Return
4993+
* An integer less than, equal to, or greater than zero
4994+
* if the first **s1_sz** bytes of **s1** is found to be
4995+
* less than, to match, or be greater than **s2**.
49864996
*/
49874997
#define __BPF_FUNC_MAPPER(FN) \
49884998
FN(unspec), \
@@ -5167,6 +5177,7 @@ union bpf_attr {
51675177
FN(kallsyms_lookup_name), \
51685178
FN(find_vma), \
51695179
FN(loop), \
5180+
FN(strncmp), \
51705181
/* */
51715182

51725183
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/bpf/helpers.c

+16
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,20 @@ const struct bpf_func_proto bpf_strtoul_proto = {
565565
};
566566
#endif
567567

568+
BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2)
569+
{
570+
return strncmp(s1, s2, s1_sz);
571+
}
572+
573+
const struct bpf_func_proto bpf_strncmp_proto = {
574+
.func = bpf_strncmp,
575+
.gpl_only = false,
576+
.ret_type = RET_INTEGER,
577+
.arg1_type = ARG_PTR_TO_MEM,
578+
.arg2_type = ARG_CONST_SIZE,
579+
.arg3_type = ARG_PTR_TO_CONST_STR,
580+
};
581+
568582
BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino,
569583
struct bpf_pidns_info *, nsdata, u32, size)
570584
{
@@ -1378,6 +1392,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
13781392
return &bpf_for_each_map_elem_proto;
13791393
case BPF_FUNC_loop:
13801394
return &bpf_loop_proto;
1395+
case BPF_FUNC_strncmp:
1396+
return &bpf_strncmp_proto;
13811397
default:
13821398
break;
13831399
}

tools/include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -4983,6 +4983,16 @@ union bpf_attr {
49834983
* Return
49844984
* The number of loops performed, **-EINVAL** for invalid **flags**,
49854985
* **-E2BIG** if **nr_loops** exceeds the maximum number of loops.
4986+
*
4987+
* long bpf_strncmp(const char *s1, u32 s1_sz, const char *s2)
4988+
* Description
4989+
* Do strncmp() between **s1** and **s2**. **s1** doesn't need
4990+
* to be null-terminated and **s1_sz** is the maximum storage
4991+
* size of **s1**. **s2** must be a read-only string.
4992+
* Return
4993+
* An integer less than, equal to, or greater than zero
4994+
* if the first **s1_sz** bytes of **s1** is found to be
4995+
* less than, to match, or be greater than **s2**.
49864996
*/
49874997
#define __BPF_FUNC_MAPPER(FN) \
49884998
FN(unspec), \
@@ -5167,6 +5177,7 @@ union bpf_attr {
51675177
FN(kallsyms_lookup_name), \
51685178
FN(find_vma), \
51695179
FN(loop), \
5180+
FN(strncmp), \
51705181
/* */
51715182

51725183
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)