bpf: Fix an issue in verifing allow_ptr_leaks
Subsystems (5)
v2 cover 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
Yafang Shao (2):
bpf: Fix issue in verifying allow_ptr_leaks
selftests/bpf: Add selftest for allow_ptr_leaks
kernel/bpf/verifier.c | 17 ++++++------ tools/testing/selftests/bpf/prog_tests/tc_bpf.c | 36 ++++++++++++++++++++++++- tools/testing/selftests/bpf/progs/test_tc_bpf.c | 13 +++++++++ 3 files changed, 57 insertions(+), 9 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/2 bpf: Fix issue in verifying allow_ptr_leaks
After we converted the capabilities of our networking-bpf program from cap_sys_admin to cap_net_admin+cap_bpf, our networking-bpf program failed to start. Because it failed the bpf verifier, and the error log is "R3 pointer comparison prohibited".
A simple reproducer as follows,
SEC("cls-ingress")
int ingress(struct __sk_buff *skb)
{
struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr);
if ((long)(iph + 1) > (long)skb->data_end)
return TC_ACT_STOLEN;
return TC_ACT_OK;
}Per discussion with Yonghong and Alexei [1], comparison of two packet pointers is not a pointer leak. This patch fixes it.
Our local kernel is 6.1.y and we expect this fix to be backported to 6.1.y, so stable is CCed.
[1]. https://lore.kernel.org/bpf/CAADnVQ+Nmspr7Si+pxWn8zkE7hX-7s93ugwC+94aXSy4uQ9vBg@mail.gmail.com/
--- kernel/bpf/verifier.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
kernel/bpf/verifier.c +9 -8
@@ -14047,6 +14047,12 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,return -EINVAL ; } +/* check src2 operand */ + err =check_reg_arg (env, insn->dst_reg,SRC_OP ); +if (err) +return err; + + dst_reg = ®s[insn->dst_reg];if (BPF_SRC (insn->code) ==BPF_X ) {if (insn->imm !=0 ) {verbose (env,"BPF_JMP/JMP32 uses reserved fields\n" );
@@ -14058,12 +14064,13 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,if (err)return err; -if (is_pointer_value (env, insn->src_reg)) { + src_reg = ®s[insn->src_reg]; +if (!(reg_is_pkt_pointer_any (dst_reg) &®_is_pkt_pointer_any (src_reg)) && +is_pointer_value (env, insn->src_reg)) {verbose (env,"R%d pointer comparison prohibited\n" , insn->src_reg);return -EACCES ; } - src_reg = ®s[insn->src_reg]; }else {if (insn->src_reg !=BPF_REG_0 ) {verbose (env,"BPF_JMP/JMP32 uses reserved fields\n" );
@@ -14071,12 +14078,6 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, } } -/* check src2 operand */ - err =check_reg_arg (env, insn->dst_reg,SRC_OP ); -if (err) -return err; - - dst_reg = ®s[insn->dst_reg]; is_jmp32 =BPF_CLASS (insn->code) ==BPF_JMP32 ;if (BPF_SRC (insn->code) ==BPF_K ) {
Signature · 1 line
1.8.3.1
PATCH v2 2/2 selftests/bpf: Add selftest for allow_ptr_leaks
In reply to Yafang Shao- Without prev commit
$ tools/testing/selftests/bpf/test_progs --name=tc_bpf #232/1 tc_bpf/tc_bpf_root:OK test_tc_bpf_non_root:PASS:set_cap_bpf_cap_net_admin 0 nsec test_tc_bpf_non_root:PASS:disable_cap_sys_admin 0 nsec 0: R1=ctx(off=0,imm=0) R10=fp0 ; if ((long)(iph + 1) > (long)skb->data_end) 0: (61) r2 = *(u32 *)(r1 +80) ; R1=ctx(off=0,imm=0) R2_w=pkt_end(off=0,imm=0) ; struct iphdr *iph = (void *)(long)skb->data + sizeof(struct ethhdr); 1: (61) r1 = *(u32 *)(r1 +76) ; R1_w=pkt(off=0,r=0,imm=0) ; if ((long)(iph + 1) > (long)skb->data_end) 2: (07) r1 += 34 ; R1_w=pkt(off=34,r=0,imm=0) 3: (b4) w0 = 1 ; R0_w=1 4: (2d) if r1 > r2 goto pc+1 R2 pointer comparison prohibited processed 5 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 test_tc_bpf_non_root:FAIL:test_tc_bpf__open_and_load unexpected error: -13 #233/2 tc_bpf_non_root:FAIL
- With prev commit
$ tools/testing/selftests/bpf/test_progs --name=tc_bpf #232/1 tc_bpf/tc_bpf_root:OK #232/2 tc_bpf/tc_bpf_non_root:OK #232 tc_bpf:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED
--- tools/testing/selftests/bpf/prog_tests/tc_bpf.c | 36 ++++++++++++++++++++++++- tools/testing/selftests/bpf/progs/test_tc_bpf.c | 13 +++++++++ 2 files changed, 48 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/tc_bpf.c +35 -1
@@ -3,6 +3,7 @@#include <test_progs.h> #include <linux/pkt_cls.h> +#include "cap_helpers.h" #include "test_tc_bpf.skel.h" #define LO_IFINDEX 1
@@ -327,7 +328,7 @@ static int test_tc_bpf_api(struct bpf_tc_hook *hook, int fd)return 0 ; } -void test_tc_bpf (void ) +void tc_bpf_root (void ) { DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX, .attach_point = BPF_TC_INGRESS);
@@ -393,3 +394,36 @@ void test_tc_bpf(void) }test_tc_bpf__destroy (skel); } + +void tc_bpf_non_root (void ) +{ +struct test_tc_bpf *skel =NULL ; +__u64 caps =0 ; +int ret; + +/* In case CAP_BPF and CAP_PERFMON is not set */ + ret =cap_enable_effective (1ULL <<CAP_BPF |1ULL <<CAP_NET_ADMIN , &caps); +if (!ASSERT_OK (ret,"set_cap_bpf_cap_net_admin" )) +return ; + ret =cap_disable_effective (1ULL <<CAP_SYS_ADMIN |1ULL <<CAP_PERFMON ,NULL ); +if (!ASSERT_OK (ret,"disable_cap_sys_admin" )) + goto restore_cap; + + skel =test_tc_bpf__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_tc_bpf__open_and_load" )) + goto restore_cap; + +test_tc_bpf__destroy (skel); + +restore_cap: +if (caps) +cap_enable_effective (caps,NULL ); +} + +void test_tc_bpf (void ) +{ +if (test__start_subtest ("tc_bpf_root" )) +tc_bpf_root (); +if (test__start_subtest ("tc_bpf_non_root" )) +tc_bpf_non_root (); +}
tools/testing/selftests/bpf/progs/test_tc_bpf.c +13 -0
@@ -2,6 +2,8 @@#include <linux/bpf.h> #include <bpf/bpf_helpers.h> +#include <linux/if_ether.h> +#include <linux/ip.h> /* Dummy prog to test TC-BPF API */
@@ -10,3 +12,14 @@ int cls(struct __sk_buff *skb) {return 0 ; } + +/* Prog to verify tc-bpf without cap_sys_admin and cap_perfmon */ +SEC("tcx/ingress") +int pkt_ptr (struct __sk_buff *skb) +{ +struct iphdr *iph = (void *)(long )skb->data +sizeof (struct ethhdr ); + +if ((long )(iph +1 ) > (long )skb->data_end) +return 1 ; +return 0 ; +}
Signature · 1 line
1.8.3.1
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Wed, 23 Aug 2023 02:07:01 +0000 you wrote:
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[...]
Here is the summary with links:
- [v2,bpf-next,1/2] bpf: Fix issue in verifying allow_ptr_leaks
https://git.kernel.org/bpf/bpf-next/c/d75e30dddf73 - [v2,bpf-next,2/2] selftests/bpf: Add selftest for allow_ptr_leaks https://git.kernel.org/bpf/bpf-next/c/0072e3624b46
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html