Skip to content

Commit f586a77

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-fix-an-issue-in-verifing-allow_ptr_leaks'
Yafang Shao says: ==================== bpf: Fix an issue in verifing allow_ptr_leaks Patch #1: An issue found in our local 6.1 kernel. This issue also exists in bpf-next. Patch #2: Selftess for #1 v1->v2: - Add acked-by from Eduard - Fix build error reported by Alexei ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 29d67fd + 0072e36 commit f586a77

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

kernel/bpf/verifier.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14041,6 +14041,12 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1404114041
return -EINVAL;
1404214042
}
1404314043

14044+
/* check src2 operand */
14045+
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
14046+
if (err)
14047+
return err;
14048+
14049+
dst_reg = &regs[insn->dst_reg];
1404414050
if (BPF_SRC(insn->code) == BPF_X) {
1404514051
if (insn->imm != 0) {
1404614052
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
@@ -14052,25 +14058,20 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1405214058
if (err)
1405314059
return err;
1405414060

14055-
if (is_pointer_value(env, insn->src_reg)) {
14061+
src_reg = &regs[insn->src_reg];
14062+
if (!(reg_is_pkt_pointer_any(dst_reg) && reg_is_pkt_pointer_any(src_reg)) &&
14063+
is_pointer_value(env, insn->src_reg)) {
1405614064
verbose(env, "R%d pointer comparison prohibited\n",
1405714065
insn->src_reg);
1405814066
return -EACCES;
1405914067
}
14060-
src_reg = &regs[insn->src_reg];
1406114068
} else {
1406214069
if (insn->src_reg != BPF_REG_0) {
1406314070
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
1406414071
return -EINVAL;
1406514072
}
1406614073
}
1406714074

14068-
/* check src2 operand */
14069-
err = check_reg_arg(env, insn->dst_reg, SRC_OP);
14070-
if (err)
14071-
return err;
14072-
14073-
dst_reg = &regs[insn->dst_reg];
1407414075
is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
1407514076

1407614077
if (BPF_SRC(insn->code) == BPF_K) {

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <test_progs.h>
44
#include <linux/pkt_cls.h>
55

6+
#include "cap_helpers.h"
67
#include "test_tc_bpf.skel.h"
78

89
#define LO_IFINDEX 1
@@ -327,7 +328,7 @@ static int test_tc_bpf_api(struct bpf_tc_hook *hook, int fd)
327328
return 0;
328329
}
329330

330-
void test_tc_bpf(void)
331+
void tc_bpf_root(void)
331332
{
332333
DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
333334
.attach_point = BPF_TC_INGRESS);
@@ -393,3 +394,36 @@ void test_tc_bpf(void)
393394
}
394395
test_tc_bpf__destroy(skel);
395396
}
397+
398+
void tc_bpf_non_root(void)
399+
{
400+
struct test_tc_bpf *skel = NULL;
401+
__u64 caps = 0;
402+
int ret;
403+
404+
/* In case CAP_BPF and CAP_PERFMON is not set */
405+
ret = cap_enable_effective(1ULL << CAP_BPF | 1ULL << CAP_NET_ADMIN, &caps);
406+
if (!ASSERT_OK(ret, "set_cap_bpf_cap_net_admin"))
407+
return;
408+
ret = cap_disable_effective(1ULL << CAP_SYS_ADMIN | 1ULL << CAP_PERFMON, NULL);
409+
if (!ASSERT_OK(ret, "disable_cap_sys_admin"))
410+
goto restore_cap;
411+
412+
skel = test_tc_bpf__open_and_load();
413+
if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load"))
414+
goto restore_cap;
415+
416+
test_tc_bpf__destroy(skel);
417+
418+
restore_cap:
419+
if (caps)
420+
cap_enable_effective(caps, NULL);
421+
}
422+
423+
void test_tc_bpf(void)
424+
{
425+
if (test__start_subtest("tc_bpf_root"))
426+
tc_bpf_root();
427+
if (test__start_subtest("tc_bpf_non_root"))
428+
tc_bpf_non_root();
429+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
5+
#include <linux/if_ether.h>
6+
#include <linux/ip.h>
57

68
/* Dummy prog to test TC-BPF API */
79

@@ -10,3 +12,14 @@ int cls(struct __sk_buff *skb)
1012
{
1113
return 0;
1214
}
15+
16+
/* Prog to verify tc-bpf without cap_sys_admin and cap_perfmon */
17+
SEC("tcx/ingress")
18+
int pkt_ptr(struct __sk_buff *skb)
19+
{
20+
struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr);
21+
22+
if ((long)(iph + 1) > (long)skb->data_end)
23+
return 1;
24+
return 0;
25+
}

0 commit comments

Comments
 (0)