diff mbox series

[bpf-next] Detect jumping to reserved code during check_cfg()

Message ID 20231009-jmp-into-reserved-fields-v1-1-d8006e2ac1f6@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next] Detect jumping to reserved code during check_cfg() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1352 this patch: 1352
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 1364 this patch: 1364
netdev/verify_signedoff fail author Signed-off-by missing
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1375 this patch: 1375
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 fail Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 fail Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 fail Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-26 fail Logs for test_verifier on x86_64 with gcc

Commit Message

Hao Sun Oct. 9, 2023, 11:12 a.m. UTC
Currently, we don't check if the branch-taken of a jump is reserved code of
ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
gives the following log in such case:

func#0 @0
0: R1=ctx(off=0,imm=0) R10=fp0
0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
2: (18) r1 = 0x1d                     ; R1_w=29
4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
5: (1c) w1 -= w1                      ; R1_w=0
6: (18) r5 = 0x32                     ; R5_w=50
8: (56) if w5 != 0xfffffff4 goto pc-2
mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
7: R5_w=50
7: BUG_ld_00
invalid BPF_LD_IMM insn

Here the verifier rejects the program because it thinks insn at 7 is an
invalid BPF_LD_IMM, but such a error log is not accurate since the issue
is jumping to reserved code not because the program contains invalid insn.
Therefore, make the verifier check the jump target during check_cfg(). For
the same program, the verifier reports the following log:

func#0 @0
jump to reserved code from insn 8 to 7

---


Signed-off-by: Hao Sun <sunhao.th@gmail.com>
---
 kernel/bpf/verifier.c | 7 +++++++
 1 file changed, 7 insertions(+)


---
base-commit: 3157b7ce14bbf468b0ca8613322a05c37b5ae25d
change-id: 20231009-jmp-into-reserved-fields-fc1a98a8e7dc

Best regards,

Comments

John Fastabend Oct. 10, 2023, 7:02 a.m. UTC | #1
Hao Sun wrote:
> Currently, we don't check if the branch-taken of a jump is reserved code of
> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
> gives the following log in such case:
> 
> func#0 @0
> 0: R1=ctx(off=0,imm=0) R10=fp0
> 0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> 2: (18) r1 = 0x1d                     ; R1_w=29
> 4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> 5: (1c) w1 -= w1                      ; R1_w=0
> 6: (18) r5 = 0x32                     ; R5_w=50
> 8: (56) if w5 != 0xfffffff4 goto pc-2
> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
> 7: R5_w=50
> 7: BUG_ld_00
> invalid BPF_LD_IMM insn
> 
> Here the verifier rejects the program because it thinks insn at 7 is an
> invalid BPF_LD_IMM, but such a error log is not accurate since the issue
> is jumping to reserved code not because the program contains invalid insn.
> Therefore, make the verifier check the jump target during check_cfg(). For
> the same program, the verifier reports the following log:

I think we at least would want a test case for this. Also how did you create
this case? Is it just something you did manually and noticed a strange error?

> 
> func#0 @0
> jump to reserved code from insn 8 to 7
> 
> ---
> 
> 
> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> ---
>  kernel/bpf/verifier.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index eed7350e15f4..725ac0b464cf 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
>  {
>  	int *insn_stack = env->cfg.insn_stack;
>  	int *insn_state = env->cfg.insn_state;
> +	struct bpf_insn *insns = env->prog->insnsi;
>  
>  	if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
>  		return DONE_EXPLORING;
> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
>  		return -EINVAL;
>  	}
>  
> +	if (e == BRANCH && insns[w].code == 0) {
> +		verbose_linfo(env, t, "%d", t);
> +		verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
> +		return -EINVAL;
> +	}
> +
>  	if (e == BRANCH) {
>  		/* mark branch target for state pruning */
>  		mark_prune_point(env, w);
> 
> ---
> base-commit: 3157b7ce14bbf468b0ca8613322a05c37b5ae25d
> change-id: 20231009-jmp-into-reserved-fields-fc1a98a8e7dc
> 
> Best regards,
> -- 
> Hao Sun <sunhao.th@gmail.com>
>
Daniel Borkmann Oct. 10, 2023, 8:33 a.m. UTC | #2
On 10/10/23 9:02 AM, John Fastabend wrote:
> Hao Sun wrote:
>> Currently, we don't check if the branch-taken of a jump is reserved code of
>> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
>> gives the following log in such case:
>>
>> func#0 @0
>> 0: R1=ctx(off=0,imm=0) R10=fp0
>> 0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
>> 2: (18) r1 = 0x1d                     ; R1_w=29
>> 4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
>> 5: (1c) w1 -= w1                      ; R1_w=0
>> 6: (18) r5 = 0x32                     ; R5_w=50
>> 8: (56) if w5 != 0xfffffff4 goto pc-2
>> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
>> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
>> 7: R5_w=50
>> 7: BUG_ld_00
>> invalid BPF_LD_IMM insn
>>
>> Here the verifier rejects the program because it thinks insn at 7 is an
>> invalid BPF_LD_IMM, but such a error log is not accurate since the issue
>> is jumping to reserved code not because the program contains invalid insn.
>> Therefore, make the verifier check the jump target during check_cfg(). For
>> the same program, the verifier reports the following log:
> 
> I think we at least would want a test case for this. Also how did you create
> this case? Is it just something you did manually and noticed a strange error?

Curious as well.

We do have test cases which try to jump into the middle of a double insn as can
be seen that this patch breaks BPF CI with regards to log mismatch below (which
still needs to be adapted, too). Either way, it probably doesn't hurt to also add
the above snippet as a test.

Hao, as I understand, the patch here is an usability improvement (not a fix per se)
where we reject such cases earlier during cfg check rather than at a later point
where we validate ld_imm instruction. Or are there cases you found which were not
yet captured via current check_ld_imm()?

test_verifier failure log :

   #458/u test1 ld_imm64 FAIL
   Unexpected verifier log!
   EXP: R1 pointer comparison
   RES:
   FAIL
   Unexpected error message!
   	EXP: R1 pointer comparison
   	RES: jump to reserved code from insn 0 to 2
   verification time 22 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0

   jump to reserved code from insn 0 to 2
   verification time 22 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
   #458/p test1 ld_imm64 FAIL
   Unexpected verifier log!
   EXP: invalid BPF_LD_IMM insn
   RES:
   FAIL
   Unexpected error message!
   	EXP: invalid BPF_LD_IMM insn
   	RES: jump to reserved code from insn 0 to 2
   verification time 9 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0

   jump to reserved code from insn 0 to 2
   verification time 9 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
   #459/u test2 ld_imm64 FAIL
   Unexpected verifier log!
   EXP: R1 pointer comparison
   RES:
   FAIL
   Unexpected error message!
   	EXP: R1 pointer comparison
   	RES: jump to reserved code from insn 0 to 2
   verification time 11 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0

   jump to reserved code from insn 0 to 2
   verification time 11 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
   #459/p test2 ld_imm64 FAIL
   Unexpected verifier log!
   EXP: invalid BPF_LD_IMM insn
   RES:
   FAIL
   Unexpected error message!
   	EXP: invalid BPF_LD_IMM insn
   	RES: jump to reserved code from insn 0 to 2
   verification time 8 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0

   jump to reserved code from insn 0 to 2
   verification time 8 usec
   stack depth 0
   processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
   #460/u test3 ld_imm64 OK

>> func#0 @0
>> jump to reserved code from insn 8 to 7
>>
>> ---
>>
>>
>> Signed-off-by: Hao Sun <sunhao.th@gmail.com>

nit: This needs to be before the "---" line.

>> ---
>>   kernel/bpf/verifier.c | 7 +++++++
>>   1 file changed, 7 insertions(+)
>>
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index eed7350e15f4..725ac0b464cf 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
>> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
>>   {
>>   	int *insn_stack = env->cfg.insn_stack;
>>   	int *insn_state = env->cfg.insn_state;
>> +	struct bpf_insn *insns = env->prog->insnsi;
>>   
>>   	if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
>>   		return DONE_EXPLORING;
>> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
>>   		return -EINVAL;
>>   	}
>>   
>> +	if (e == BRANCH && insns[w].code == 0) {
>> +		verbose_linfo(env, t, "%d", t);
>> +		verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
>> +		return -EINVAL;
>> +	}

Other than that, lgtm.

>>   	if (e == BRANCH) {
>>   		/* mark branch target for state pruning */
>>   		mark_prune_point(env, w);
>>
Hao Sun Oct. 10, 2023, 9:17 a.m. UTC | #3
On Tue, Oct 10, 2023 at 10:33 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> On 10/10/23 9:02 AM, John Fastabend wrote:
> > Hao Sun wrote:
> >> Currently, we don't check if the branch-taken of a jump is reserved code of
> >> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
> >> gives the following log in such case:
> >>
> >> func#0 @0
> >> 0: R1=ctx(off=0,imm=0) R10=fp0
> >> 0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> >> 2: (18) r1 = 0x1d                     ; R1_w=29
> >> 4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> >> 5: (1c) w1 -= w1                      ; R1_w=0
> >> 6: (18) r5 = 0x32                     ; R5_w=50
> >> 8: (56) if w5 != 0xfffffff4 goto pc-2
> >> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
> >> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
> >> 7: R5_w=50
> >> 7: BUG_ld_00
> >> invalid BPF_LD_IMM insn
> >>
> >> Here the verifier rejects the program because it thinks insn at 7 is an
> >> invalid BPF_LD_IMM, but such a error log is not accurate since the issue
> >> is jumping to reserved code not because the program contains invalid insn.
> >> Therefore, make the verifier check the jump target during check_cfg(). For
> >> the same program, the verifier reports the following log:
> >
> > I think we at least would want a test case for this. Also how did you create
> > this case? Is it just something you did manually and noticed a strange error?
>
> Curious as well.

I just wrote a testing tool for the verifier, which uses a test oracle
to capture
incorrect verifier's states, capturing incorrect verifier logs is a
bonus from this.
The bug is captured during testing the testing tool :). I will publish
the work when
I think it's useful enough and ready.

>
> We do have test cases which try to jump into the middle of a double insn as can
> be seen that this patch breaks BPF CI with regards to log mismatch below (which
> still needs to be adapted, too). Either way, it probably doesn't hurt to also add
> the above snippet as a test.
>

Will add a test case for this, and try to fix these broken tests, in patch v2.

> Hao, as I understand, the patch here is an usability improvement (not a fix per se)
> where we reject such cases earlier during cfg check rather than at a later point
> where we validate ld_imm instruction. Or are there cases you found which were not
> yet captured via current check_ld_imm()?
>

I regard this as a fix, because the verifier log is not correct, since
the program does
not contain any invalid ld_imm64 instructions in this case.

I haven't met other cases not captured via check_ld_imm(), but somehow, I think
we probably want to convert the check there as an internal bug,
because we already
have bpf_opcode_in_insntable() check in resolve_pseudo_ldimm64(). Once we meet
invalid insn code here, then somewhere else in the verifier is
probably wrong. But
I'm not sure, maybe something like this:

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index eed7350e15f4..bed97de568a5 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -14532,8 +14532,8 @@ static int check_ld_imm(struct
bpf_verifier_env *env, struct bpf_insn *insn)
        int err;

        if (BPF_SIZE(insn->code) != BPF_DW) {
-               verbose(env, "invalid BPF_LD_IMM insn\n");
-               return -EINVAL;
+               verbose(env, "verifier internal bug, invalid BPF_LD_IMM\n");
+               return -EFAULT;
        }
        if (insn->off != 0) {
                verbose(env, "BPF_LD_IMM64 uses reserved fields\n");

> test_verifier failure log :
>
>    #458/u test1 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: R1 pointer comparison
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: R1 pointer comparison
>         RES: jump to reserved code from insn 0 to 2
>    verification time 22 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 22 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #458/p test1 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: invalid BPF_LD_IMM insn
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: invalid BPF_LD_IMM insn
>         RES: jump to reserved code from insn 0 to 2
>    verification time 9 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 9 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #459/u test2 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: R1 pointer comparison
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: R1 pointer comparison
>         RES: jump to reserved code from insn 0 to 2
>    verification time 11 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 11 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #459/p test2 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: invalid BPF_LD_IMM insn
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: invalid BPF_LD_IMM insn
>         RES: jump to reserved code from insn 0 to 2
>    verification time 8 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 8 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #460/u test3 ld_imm64 OK
>
> >> func#0 @0
> >> jump to reserved code from insn 8 to 7
> >>
> >> ---
> >>
> >>
> >> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
>
> nit: This needs to be before the "---" line.
>

Noted.

> >> ---
> >>   kernel/bpf/verifier.c | 7 +++++++
> >>   1 file changed, 7 insertions(+)
> >>
> >> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> >> index eed7350e15f4..725ac0b464cf 100644
> >> --- a/kernel/bpf/verifier.c
> >> +++ b/kernel/bpf/verifier.c
> >> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> >>   {
> >>      int *insn_stack = env->cfg.insn_stack;
> >>      int *insn_state = env->cfg.insn_state;
> >> +    struct bpf_insn *insns = env->prog->insnsi;
> >>
> >>      if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
> >>              return DONE_EXPLORING;
> >> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> >>              return -EINVAL;
> >>      }
> >>
> >> +    if (e == BRANCH && insns[w].code == 0) {
> >> +            verbose_linfo(env, t, "%d", t);
> >> +            verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
> >> +            return -EINVAL;
> >> +    }
>
> Other than that, lgtm.
>
> >>      if (e == BRANCH) {
> >>              /* mark branch target for state pruning */
> >>              mark_prune_point(env, w);
> >>
Daniel Borkmann Oct. 10, 2023, 3:35 p.m. UTC | #4
On 10/10/23 11:17 AM, Hao Sun wrote:
[...]
> I regard this as a fix, because the verifier log is not correct, since
> the program does
> not contain any invalid ld_imm64 instructions in this case.
> 
> I haven't met other cases not captured via check_ld_imm(), but somehow, I think
> we probably want to convert the check there as an internal bug,
> because we already
> have bpf_opcode_in_insntable() check in resolve_pseudo_ldimm64(). Once we meet
> invalid insn code here, then somewhere else in the verifier is
> probably wrong. But
> I'm not sure, maybe something like this:

Makes sense, you could probably add this into your series as a separate commit.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index eed7350e15f4..bed97de568a5 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -14532,8 +14532,8 @@ static int check_ld_imm(struct
> bpf_verifier_env *env, struct bpf_insn *insn)
>          int err;
> 
>          if (BPF_SIZE(insn->code) != BPF_DW) {
> -               verbose(env, "invalid BPF_LD_IMM insn\n");
> -               return -EINVAL;
> +               verbose(env, "verifier internal bug, invalid BPF_LD_IMM\n");

If so please stick to the common style as we have in other locations:

verbose(env, "verifier internal error: <xyz>\n");

> +               return -EFAULT;
>          }
>          if (insn->off != 0) {
>                  verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
>
Andrii Nakryiko Oct. 11, 2023, 2:42 a.m. UTC | #5
On Tue, Oct 10, 2023 at 1:33 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> On 10/10/23 9:02 AM, John Fastabend wrote:
> > Hao Sun wrote:
> >> Currently, we don't check if the branch-taken of a jump is reserved code of
> >> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
> >> gives the following log in such case:
> >>
> >> func#0 @0
> >> 0: R1=ctx(off=0,imm=0) R10=fp0
> >> 0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> >> 2: (18) r1 = 0x1d                     ; R1_w=29
> >> 4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> >> 5: (1c) w1 -= w1                      ; R1_w=0
> >> 6: (18) r5 = 0x32                     ; R5_w=50
> >> 8: (56) if w5 != 0xfffffff4 goto pc-2
> >> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
> >> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
> >> 7: R5_w=50
> >> 7: BUG_ld_00
> >> invalid BPF_LD_IMM insn
> >>
> >> Here the verifier rejects the program because it thinks insn at 7 is an
> >> invalid BPF_LD_IMM, but such a error log is not accurate since the issue
> >> is jumping to reserved code not because the program contains invalid insn.
> >> Therefore, make the verifier check the jump target during check_cfg(). For
> >> the same program, the verifier reports the following log:
> >
> > I think we at least would want a test case for this. Also how did you create
> > this case? Is it just something you did manually and noticed a strange error?
>
> Curious as well.
>
> We do have test cases which try to jump into the middle of a double insn as can
> be seen that this patch breaks BPF CI with regards to log mismatch below (which
> still needs to be adapted, too). Either way, it probably doesn't hurt to also add
> the above snippet as a test.
>
> Hao, as I understand, the patch here is an usability improvement (not a fix per se)
> where we reject such cases earlier during cfg check rather than at a later point
> where we validate ld_imm instruction. Or are there cases you found which were not
> yet captured via current check_ld_imm()?
>
> test_verifier failure log :
>
>    #458/u test1 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: R1 pointer comparison
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: R1 pointer comparison
>         RES: jump to reserved code from insn 0 to 2
>    verification time 22 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 22 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #458/p test1 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: invalid BPF_LD_IMM insn
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: invalid BPF_LD_IMM insn
>         RES: jump to reserved code from insn 0 to 2
>    verification time 9 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 9 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #459/u test2 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: R1 pointer comparison
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: R1 pointer comparison
>         RES: jump to reserved code from insn 0 to 2
>    verification time 11 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 11 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #459/p test2 ld_imm64 FAIL
>    Unexpected verifier log!
>    EXP: invalid BPF_LD_IMM insn
>    RES:
>    FAIL
>    Unexpected error message!
>         EXP: invalid BPF_LD_IMM insn
>         RES: jump to reserved code from insn 0 to 2
>    verification time 8 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>
>    jump to reserved code from insn 0 to 2
>    verification time 8 usec
>    stack depth 0
>    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>    #460/u test3 ld_imm64 OK
>
> >> func#0 @0
> >> jump to reserved code from insn 8 to 7
> >>
> >> ---
> >>
> >>
> >> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
>
> nit: This needs to be before the "---" line.
>
> >> ---
> >>   kernel/bpf/verifier.c | 7 +++++++
> >>   1 file changed, 7 insertions(+)
> >>
> >> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> >> index eed7350e15f4..725ac0b464cf 100644
> >> --- a/kernel/bpf/verifier.c
> >> +++ b/kernel/bpf/verifier.c
> >> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> >>   {
> >>      int *insn_stack = env->cfg.insn_stack;
> >>      int *insn_state = env->cfg.insn_state;
> >> +    struct bpf_insn *insns = env->prog->insnsi;
> >>
> >>      if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
> >>              return DONE_EXPLORING;
> >> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> >>              return -EINVAL;
> >>      }
> >>
> >> +    if (e == BRANCH && insns[w].code == 0) {
> >> +            verbose_linfo(env, t, "%d", t);
> >> +            verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
> >> +            return -EINVAL;
> >> +    }
>
> Other than that, lgtm.

We do rely quite a lot on verifier not complaining eagerly about some
potentially invalid instructions if it's provable that some portion of
the code won't ever be reached (think using .rodata variables for
feature gating, poisoning intructions due to failed CO-RE relocation,
which libbpf does actively, except it's using a call to non-existing
helper). As such, check_cfg() is a wrong place to do such validity
checks because some of the branches might never be run and validated
in practice.

This seems like a pretty obscure case of fuzzer generated test with
random jumps into the middle of ldimm64 instruction. I think the tool
should be able to avoid this or handle verifier log just fine in such
situations. On the other hand, valid code generated by compilers will
never have such jumps.

So perhaps we can improve existing "invalid BPF_LD_IMM insn" message,
but let's not teach check_cfg() more checks than necessary?

>
> >>      if (e == BRANCH) {
> >>              /* mark branch target for state pruning */
> >>              mark_prune_point(env, w);
> >>
>
Hao Sun Oct. 11, 2023, 6:46 a.m. UTC | #6
On Wed, Oct 11, 2023 at 4:42 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Tue, Oct 10, 2023 at 1:33 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
> >
> > On 10/10/23 9:02 AM, John Fastabend wrote:
> > > Hao Sun wrote:
> > >> Currently, we don't check if the branch-taken of a jump is reserved code of
> > >> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
> > >> gives the following log in such case:
> > >>
> > >> func#0 @0
> > >> 0: R1=ctx(off=0,imm=0) R10=fp0
> > >> 0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> > >> 2: (18) r1 = 0x1d                     ; R1_w=29
> > >> 4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> > >> 5: (1c) w1 -= w1                      ; R1_w=0
> > >> 6: (18) r5 = 0x32                     ; R5_w=50
> > >> 8: (56) if w5 != 0xfffffff4 goto pc-2
> > >> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
> > >> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
> > >> 7: R5_w=50
> > >> 7: BUG_ld_00
> > >> invalid BPF_LD_IMM insn
> > >>
> > >> Here the verifier rejects the program because it thinks insn at 7 is an
> > >> invalid BPF_LD_IMM, but such a error log is not accurate since the issue
> > >> is jumping to reserved code not because the program contains invalid insn.
> > >> Therefore, make the verifier check the jump target during check_cfg(). For
> > >> the same program, the verifier reports the following log:
> > >
> > > I think we at least would want a test case for this. Also how did you create
> > > this case? Is it just something you did manually and noticed a strange error?
> >
> > Curious as well.
> >
> > We do have test cases which try to jump into the middle of a double insn as can
> > be seen that this patch breaks BPF CI with regards to log mismatch below (which
> > still needs to be adapted, too). Either way, it probably doesn't hurt to also add
> > the above snippet as a test.
> >
> > Hao, as I understand, the patch here is an usability improvement (not a fix per se)
> > where we reject such cases earlier during cfg check rather than at a later point
> > where we validate ld_imm instruction. Or are there cases you found which were not
> > yet captured via current check_ld_imm()?
> >
> > test_verifier failure log :
> >
> >    #458/u test1 ld_imm64 FAIL
> >    Unexpected verifier log!
> >    EXP: R1 pointer comparison
> >    RES:
> >    FAIL
> >    Unexpected error message!
> >         EXP: R1 pointer comparison
> >         RES: jump to reserved code from insn 0 to 2
> >    verification time 22 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >
> >    jump to reserved code from insn 0 to 2
> >    verification time 22 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >    #458/p test1 ld_imm64 FAIL
> >    Unexpected verifier log!
> >    EXP: invalid BPF_LD_IMM insn
> >    RES:
> >    FAIL
> >    Unexpected error message!
> >         EXP: invalid BPF_LD_IMM insn
> >         RES: jump to reserved code from insn 0 to 2
> >    verification time 9 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >
> >    jump to reserved code from insn 0 to 2
> >    verification time 9 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >    #459/u test2 ld_imm64 FAIL
> >    Unexpected verifier log!
> >    EXP: R1 pointer comparison
> >    RES:
> >    FAIL
> >    Unexpected error message!
> >         EXP: R1 pointer comparison
> >         RES: jump to reserved code from insn 0 to 2
> >    verification time 11 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >
> >    jump to reserved code from insn 0 to 2
> >    verification time 11 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >    #459/p test2 ld_imm64 FAIL
> >    Unexpected verifier log!
> >    EXP: invalid BPF_LD_IMM insn
> >    RES:
> >    FAIL
> >    Unexpected error message!
> >         EXP: invalid BPF_LD_IMM insn
> >         RES: jump to reserved code from insn 0 to 2
> >    verification time 8 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >
> >    jump to reserved code from insn 0 to 2
> >    verification time 8 usec
> >    stack depth 0
> >    processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >    #460/u test3 ld_imm64 OK
> >
> > >> func#0 @0
> > >> jump to reserved code from insn 8 to 7
> > >>
> > >> ---
> > >>
> > >>
> > >> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> >
> > nit: This needs to be before the "---" line.
> >
> > >> ---
> > >>   kernel/bpf/verifier.c | 7 +++++++
> > >>   1 file changed, 7 insertions(+)
> > >>
> > >> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> > >> index eed7350e15f4..725ac0b464cf 100644
> > >> --- a/kernel/bpf/verifier.c
> > >> +++ b/kernel/bpf/verifier.c
> > >> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> > >>   {
> > >>      int *insn_stack = env->cfg.insn_stack;
> > >>      int *insn_state = env->cfg.insn_state;
> > >> +    struct bpf_insn *insns = env->prog->insnsi;
> > >>
> > >>      if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
> > >>              return DONE_EXPLORING;
> > >> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> > >>              return -EINVAL;
> > >>      }
> > >>
> > >> +    if (e == BRANCH && insns[w].code == 0) {
> > >> +            verbose_linfo(env, t, "%d", t);
> > >> +            verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
> > >> +            return -EINVAL;
> > >> +    }
> >
> > Other than that, lgtm.
>
> We do rely quite a lot on verifier not complaining eagerly about some
> potentially invalid instructions if it's provable that some portion of
> the code won't ever be reached (think using .rodata variables for
> feature gating, poisoning intructions due to failed CO-RE relocation,
> which libbpf does actively, except it's using a call to non-existing
> helper). As such, check_cfg() is a wrong place to do such validity
> checks because some of the branches might never be run and validated
> in practice.
>

Don't really agree. Jump to the middle of ld_imm64 is just like jumping
out of bounds, both break the CFG integrity immediately. For those
apparently incorrect  jumps, rejecting early makes everything simple;
otherwise, we probably need some rewrite in the end.

Also, as you mentioned, libbpf relies on non-existing helpers, not jump
to the middle of ld_imm64. It seems better and easier to not leave this
hole.

> This seems like a pretty obscure case of fuzzer generated test with
> random jumps into the middle of ldimm64 instruction. I think the tool
> should be able to avoid this or handle verifier log just fine in such
> situations. On the other hand, valid code generated by compilers will
> never have such jumps.
>
> So perhaps we can improve existing "invalid BPF_LD_IMM insn" message,
> but let's not teach check_cfg() more checks than necessary?
>

Improving that `invalid BPF_LD_IMM` log does not solve the problem, the
issue here is an invalid jump. Also, there could be various causes that make
the verifier see an invalid BPF_LD_IMM in check_ld_imm().

> >
> > >>      if (e == BRANCH) {
> > >>              /* mark branch target for state pruning */
> > >>              mark_prune_point(env, w);
> > >>
> >
Daniel Borkmann Oct. 11, 2023, 2:50 p.m. UTC | #7
On 10/11/23 8:46 AM, Hao Sun wrote:
> On Wed, Oct 11, 2023 at 4:42 AM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
>> On Tue, Oct 10, 2023 at 1:33 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
>>> On 10/10/23 9:02 AM, John Fastabend wrote:
>>>> Hao Sun wrote:
>>>>> Currently, we don't check if the branch-taken of a jump is reserved code of
>>>>> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
>>>>> gives the following log in such case:
>>>>>
>>>>> func#0 @0
>>>>> 0: R1=ctx(off=0,imm=0) R10=fp0
>>>>> 0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
>>>>> 2: (18) r1 = 0x1d                     ; R1_w=29
>>>>> 4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
>>>>> 5: (1c) w1 -= w1                      ; R1_w=0
>>>>> 6: (18) r5 = 0x32                     ; R5_w=50
>>>>> 8: (56) if w5 != 0xfffffff4 goto pc-2
>>>>> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
>>>>> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
>>>>> 7: R5_w=50
>>>>> 7: BUG_ld_00
>>>>> invalid BPF_LD_IMM insn
>>>>>
>>>>> Here the verifier rejects the program because it thinks insn at 7 is an
>>>>> invalid BPF_LD_IMM, but such a error log is not accurate since the issue
>>>>> is jumping to reserved code not because the program contains invalid insn.
>>>>> Therefore, make the verifier check the jump target during check_cfg(). For
>>>>> the same program, the verifier reports the following log:
>>>>
>>>> I think we at least would want a test case for this. Also how did you create
>>>> this case? Is it just something you did manually and noticed a strange error?
>>>
>>> Curious as well.
>>>
>>> We do have test cases which try to jump into the middle of a double insn as can
>>> be seen that this patch breaks BPF CI with regards to log mismatch below (which
>>> still needs to be adapted, too). Either way, it probably doesn't hurt to also add
>>> the above snippet as a test.
>>>
>>> Hao, as I understand, the patch here is an usability improvement (not a fix per se)
>>> where we reject such cases earlier during cfg check rather than at a later point
>>> where we validate ld_imm instruction. Or are there cases you found which were not
>>> yet captured via current check_ld_imm()?
>>>
>>> test_verifier failure log :
>>>
>>>     #458/u test1 ld_imm64 FAIL
>>>     Unexpected verifier log!
>>>     EXP: R1 pointer comparison
>>>     RES:
>>>     FAIL
>>>     Unexpected error message!
>>>          EXP: R1 pointer comparison
>>>          RES: jump to reserved code from insn 0 to 2
>>>     verification time 22 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>
>>>     jump to reserved code from insn 0 to 2
>>>     verification time 22 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>     #458/p test1 ld_imm64 FAIL
>>>     Unexpected verifier log!
>>>     EXP: invalid BPF_LD_IMM insn
>>>     RES:
>>>     FAIL
>>>     Unexpected error message!
>>>          EXP: invalid BPF_LD_IMM insn
>>>          RES: jump to reserved code from insn 0 to 2
>>>     verification time 9 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>
>>>     jump to reserved code from insn 0 to 2
>>>     verification time 9 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>     #459/u test2 ld_imm64 FAIL
>>>     Unexpected verifier log!
>>>     EXP: R1 pointer comparison
>>>     RES:
>>>     FAIL
>>>     Unexpected error message!
>>>          EXP: R1 pointer comparison
>>>          RES: jump to reserved code from insn 0 to 2
>>>     verification time 11 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>
>>>     jump to reserved code from insn 0 to 2
>>>     verification time 11 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>     #459/p test2 ld_imm64 FAIL
>>>     Unexpected verifier log!
>>>     EXP: invalid BPF_LD_IMM insn
>>>     RES:
>>>     FAIL
>>>     Unexpected error message!
>>>          EXP: invalid BPF_LD_IMM insn
>>>          RES: jump to reserved code from insn 0 to 2
>>>     verification time 8 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>
>>>     jump to reserved code from insn 0 to 2
>>>     verification time 8 usec
>>>     stack depth 0
>>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
>>>     #460/u test3 ld_imm64 OK
>>>
>>>>> func#0 @0
>>>>> jump to reserved code from insn 8 to 7
>>>>>
>>>>> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
>>>
>>> nit: This needs to be before the "---" line.
>>>
>>>>> ---
>>>>>    kernel/bpf/verifier.c | 7 +++++++
>>>>>    1 file changed, 7 insertions(+)
>>>>>
>>>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>>>>> index eed7350e15f4..725ac0b464cf 100644
>>>>> --- a/kernel/bpf/verifier.c
>>>>> +++ b/kernel/bpf/verifier.c
>>>>> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
>>>>>    {
>>>>>       int *insn_stack = env->cfg.insn_stack;
>>>>>       int *insn_state = env->cfg.insn_state;
>>>>> +    struct bpf_insn *insns = env->prog->insnsi;
>>>>>
>>>>>       if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
>>>>>               return DONE_EXPLORING;
>>>>> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
>>>>>               return -EINVAL;
>>>>>       }
>>>>>
>>>>> +    if (e == BRANCH && insns[w].code == 0) {
>>>>> +            verbose_linfo(env, t, "%d", t);
>>>>> +            verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
>>>>> +            return -EINVAL;
>>>>> +    }
>>>
>>> Other than that, lgtm.
>>
>> We do rely quite a lot on verifier not complaining eagerly about some
>> potentially invalid instructions if it's provable that some portion of
>> the code won't ever be reached (think using .rodata variables for
>> feature gating, poisoning intructions due to failed CO-RE relocation,
>> which libbpf does actively, except it's using a call to non-existing
>> helper). As such, check_cfg() is a wrong place to do such validity
>> checks because some of the branches might never be run and validated
>> in practice.
> 
> Don't really agree. Jump to the middle of ld_imm64 is just like jumping
> out of bounds, both break the CFG integrity immediately. For those
> apparently incorrect  jumps, rejecting early makes everything simple;
> otherwise, we probably need some rewrite in the end.

Could you elaborate on the 'breaking CFG integrity immediately'? This was
what I was trying to gather earlier with log improvement vs actual fix.

Do you mean /potentially/ breaking CFG integrity, if, say, we had a double
insn jump in future and there is a back-jump to the 2nd part of the insn?

> Also, as you mentioned, libbpf relies on non-existing helpers, not jump
> to the middle of ld_imm64. It seems better and easier to not leave this
> hole.

Thanks,
Daniel
Hao Sun Oct. 12, 2023, 6:23 a.m. UTC | #8
On Wed, Oct 11, 2023 at 4:50 PM Daniel Borkmann <daniel@iogearbox.net> wrote:
>
> On 10/11/23 8:46 AM, Hao Sun wrote:
> > On Wed, Oct 11, 2023 at 4:42 AM Andrii Nakryiko
> > <andrii.nakryiko@gmail.com> wrote:
> >> On Tue, Oct 10, 2023 at 1:33 AM Daniel Borkmann <daniel@iogearbox.net> wrote:
> >>> On 10/10/23 9:02 AM, John Fastabend wrote:
> >>>> Hao Sun wrote:
> >>>>> Currently, we don't check if the branch-taken of a jump is reserved code of
> >>>>> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The verifier
> >>>>> gives the following log in such case:
> >>>>>
> >>>>> func#0 @0
> >>>>> 0: R1=ctx(off=0,imm=0) R10=fp0
> >>>>> 0: (18) r4 = 0xffff888103436000       ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> >>>>> 2: (18) r1 = 0x1d                     ; R1_w=29
> >>>>> 4: (55) if r4 != 0x0 goto pc+4        ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0)
> >>>>> 5: (1c) w1 -= w1                      ; R1_w=0
> >>>>> 6: (18) r5 = 0x32                     ; R5_w=50
> >>>>> 8: (56) if w5 != 0xfffffff4 goto pc-2
> >>>>> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1
> >>>>> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32
> >>>>> 7: R5_w=50
> >>>>> 7: BUG_ld_00
> >>>>> invalid BPF_LD_IMM insn
> >>>>>
> >>>>> Here the verifier rejects the program because it thinks insn at 7 is an
> >>>>> invalid BPF_LD_IMM, but such a error log is not accurate since the issue
> >>>>> is jumping to reserved code not because the program contains invalid insn.
> >>>>> Therefore, make the verifier check the jump target during check_cfg(). For
> >>>>> the same program, the verifier reports the following log:
> >>>>
> >>>> I think we at least would want a test case for this. Also how did you create
> >>>> this case? Is it just something you did manually and noticed a strange error?
> >>>
> >>> Curious as well.
> >>>
> >>> We do have test cases which try to jump into the middle of a double insn as can
> >>> be seen that this patch breaks BPF CI with regards to log mismatch below (which
> >>> still needs to be adapted, too). Either way, it probably doesn't hurt to also add
> >>> the above snippet as a test.
> >>>
> >>> Hao, as I understand, the patch here is an usability improvement (not a fix per se)
> >>> where we reject such cases earlier during cfg check rather than at a later point
> >>> where we validate ld_imm instruction. Or are there cases you found which were not
> >>> yet captured via current check_ld_imm()?
> >>>
> >>> test_verifier failure log :
> >>>
> >>>     #458/u test1 ld_imm64 FAIL
> >>>     Unexpected verifier log!
> >>>     EXP: R1 pointer comparison
> >>>     RES:
> >>>     FAIL
> >>>     Unexpected error message!
> >>>          EXP: R1 pointer comparison
> >>>          RES: jump to reserved code from insn 0 to 2
> >>>     verification time 22 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>
> >>>     jump to reserved code from insn 0 to 2
> >>>     verification time 22 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>     #458/p test1 ld_imm64 FAIL
> >>>     Unexpected verifier log!
> >>>     EXP: invalid BPF_LD_IMM insn
> >>>     RES:
> >>>     FAIL
> >>>     Unexpected error message!
> >>>          EXP: invalid BPF_LD_IMM insn
> >>>          RES: jump to reserved code from insn 0 to 2
> >>>     verification time 9 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>
> >>>     jump to reserved code from insn 0 to 2
> >>>     verification time 9 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>     #459/u test2 ld_imm64 FAIL
> >>>     Unexpected verifier log!
> >>>     EXP: R1 pointer comparison
> >>>     RES:
> >>>     FAIL
> >>>     Unexpected error message!
> >>>          EXP: R1 pointer comparison
> >>>          RES: jump to reserved code from insn 0 to 2
> >>>     verification time 11 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>
> >>>     jump to reserved code from insn 0 to 2
> >>>     verification time 11 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>     #459/p test2 ld_imm64 FAIL
> >>>     Unexpected verifier log!
> >>>     EXP: invalid BPF_LD_IMM insn
> >>>     RES:
> >>>     FAIL
> >>>     Unexpected error message!
> >>>          EXP: invalid BPF_LD_IMM insn
> >>>          RES: jump to reserved code from insn 0 to 2
> >>>     verification time 8 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>
> >>>     jump to reserved code from insn 0 to 2
> >>>     verification time 8 usec
> >>>     stack depth 0
> >>>     processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
> >>>     #460/u test3 ld_imm64 OK
> >>>
> >>>>> func#0 @0
> >>>>> jump to reserved code from insn 8 to 7
> >>>>>
> >>>>> Signed-off-by: Hao Sun <sunhao.th@gmail.com>
> >>>
> >>> nit: This needs to be before the "---" line.
> >>>
> >>>>> ---
> >>>>>    kernel/bpf/verifier.c | 7 +++++++
> >>>>>    1 file changed, 7 insertions(+)
> >>>>>
> >>>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> >>>>> index eed7350e15f4..725ac0b464cf 100644
> >>>>> --- a/kernel/bpf/verifier.c
> >>>>> +++ b/kernel/bpf/verifier.c
> >>>>> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> >>>>>    {
> >>>>>       int *insn_stack = env->cfg.insn_stack;
> >>>>>       int *insn_state = env->cfg.insn_state;
> >>>>> +    struct bpf_insn *insns = env->prog->insnsi;
> >>>>>
> >>>>>       if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
> >>>>>               return DONE_EXPLORING;
> >>>>> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
> >>>>>               return -EINVAL;
> >>>>>       }
> >>>>>
> >>>>> +    if (e == BRANCH && insns[w].code == 0) {
> >>>>> +            verbose_linfo(env, t, "%d", t);
> >>>>> +            verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
> >>>>> +            return -EINVAL;
> >>>>> +    }
> >>>
> >>> Other than that, lgtm.
> >>
> >> We do rely quite a lot on verifier not complaining eagerly about some
> >> potentially invalid instructions if it's provable that some portion of
> >> the code won't ever be reached (think using .rodata variables for
> >> feature gating, poisoning intructions due to failed CO-RE relocation,
> >> which libbpf does actively, except it's using a call to non-existing
> >> helper). As such, check_cfg() is a wrong place to do such validity
> >> checks because some of the branches might never be run and validated
> >> in practice.
> >
> > Don't really agree. Jump to the middle of ld_imm64 is just like jumping
> > out of bounds, both break the CFG integrity immediately. For those
> > apparently incorrect  jumps, rejecting early makes everything simple;
> > otherwise, we probably need some rewrite in the end.
>
> Could you elaborate on the 'breaking CFG integrity immediately'? This was
> what I was trying to gather earlier with log improvement vs actual fix.
>
> Do you mean /potentially/ breaking CFG integrity, if, say, we had a double
> insn jump in future and there is a back-jump to the 2nd part of the insn?
>

I mean jumping to the middle of ld_imm64 is similar to jumping out-of-bound,
both are CFG-related issues and can be handled early in one place.

For the case you mentioned, the current code would handle such an issue in
check_ld_imm64(), and again gives "BAD_LD_IMM" log, which is strange.

> > Also, as you mentioned, libbpf relies on non-existing helpers, not jump
> > to the middle of ld_imm64. It seems better and easier to not leave this
> > hole.
>
> Thanks,
> Daniel
diff mbox series

Patch

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index eed7350e15f4..725ac0b464cf 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -14980,6 +14980,7 @@  static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
 {
 	int *insn_stack = env->cfg.insn_stack;
 	int *insn_state = env->cfg.insn_state;
+	struct bpf_insn *insns = env->prog->insnsi;
 
 	if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
 		return DONE_EXPLORING;
@@ -14993,6 +14994,12 @@  static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
 		return -EINVAL;
 	}
 
+	if (e == BRANCH && insns[w].code == 0) {
+		verbose_linfo(env, t, "%d", t);
+		verbose(env, "jump to reserved code from insn %d to %d\n", t, w);
+		return -EINVAL;
+	}
+
 	if (e == BRANCH) {
 		/* mark branch target for state pruning */
 		mark_prune_point(env, w);