public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Hao Sun <sunhao.th@gmail.com>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>, bpf <bpf@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [Bug Report] bpf: incorrectly pruning runtime execution path
Date: Thu, 14 Dec 2023 01:35:26 +0200	[thread overview]
Message-ID: <480a5cfefc23446f7c82c5b87eef6306364132b9.camel@gmail.com> (raw)
In-Reply-To: <CACkBjsaEQxCaZ0ERRnBXduBqdw3MXB5r7naJx_anqxi0Wa-M_Q@mail.gmail.com>

On Wed, 2023-12-13 at 11:25 +0100, Hao Sun wrote:
[...]
 
> I tried to convert the repro to a valid test case in inline asm, but seems
> JSET (if r0 & 0xfffffffe goto pc+3) is currently not supported in clang-17.
> Will try after clang-18 is released.
> 
> #30 is expected to be executed, see below where everything after ";" is
> the runtime value:
>    ...
>    6: (36) if w8 >= 0x69 goto pc+1    ; w8 = 0xbe, always taken
>    ...
>   11: (45) if r0 & 0xfffffffe goto pc+3  ; r0 = 0x616, taken
>   ...
>   18: (56) if w8 != 0xf goto pc+3     ; w8 not touched, taken
>   ...
>   23: (bf) r5 = r8     ; w5 = 0xbe
>   24: (18) r2 = 0x4
>   26: (7e) if w8 s>= w0 goto pc+5    ; non-taken
>   27: (4f) r8 |= r8
>   28: (0f) r8 += r8
>   29: (d6) if w5 s<= 0x1d goto pc+2  ; non-taken
>   30: (18) r0 = 0x4      ; executed
> 
> Since the verifier prunes at #26, #30 is dead and eliminated. So, #30
> is executed after manually commenting out the dead code rewrite pass.
> 
> From my understanding, I think r0 should be marked as precise when
> first backtrack from #29, because r5 range at this point depends on w0
> as r8 and r5 share the same id at #26.

Hi Hao, Andrii,

I converted program in question to a runnable test, here is a link to
the patch adding it and disabling dead code removal:
https://gist.github.com/eddyz87/e888ad70c947f28f94146a47e33cd378

Run the test as follows:
  ./test_progs -vvv -a verifier_and/pruning_test

And inspect the retval:
  do_prog_test_run:PASS:bpf_prog_test_run 0 nsec
  run_subtest:FAIL:647 Unexpected retval: 1353935089 != 4

Note that I tried this test with two functions:
- bpf_get_current_cgroup_id, with this function I get retval 2, not 4 :)
- bpf_get_prandom_u32, with this function I get a random retval each time.

What is the expectation when 'bpf_get_current_cgroup_id' is used?
That it is some known (to us) number, but verifier treats it as unknown scalar?

Also, I find this portion of the verification log strange:

    ...
    13: (0f) r0 += r0                     ; R0_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=2,
                                                        var_off=(0x0; 0x3))
    14: (2f) r4 *= r4                     ; R4_w=scalar()
    15: (18) r3 = 0x1f00000034            ; R3_w=0x1f00000034
    17: (c4) w4 s>>= 29                   ; R4_w=scalar(smin=0,smax=umax=0xffffffff,smin32=-4,smax32=3,
                                                        var_off=(0x0; 0xffffffff))
    18: (56) if w8 != 0xf goto pc+3       ; R8_w=scalar(smin=0x800000000000000f,smax=0x7fffffff0000000f,
                                                        umin=smin32=umin32=15,umax=0xffffffff0000000f,
                                                        smax32=umax32=15,var_off=(0xf; 0xffffffff00000000))
    19: (d7) r3 = bswap32 r3              ; R3_w=scalar()
    20: (18) r2 = 0x1c                    ; R2=28
    22: (67) r4 <<= 2                     ; R4_w=scalar(smin=0,smax=umax=0x3fffffffc,
                                                        smax32=0x7ffffffc,umax32=0xfffffffc,
                                                        var_off=(0x0; 0x3fffffffc))
    23: (bf) r5 = r8                      ; R5_w=scalar(id=1,smin=0x800000000000000f,
                                                        smax=0x7fffffff0000000f,
                                                        umin=smin32=umin32=15,
                                                        umax=0xffffffff0000000f,
                                                        smax32=umax32=15,
                                                        var_off=(0xf; 0xffffffff00000000))
                                            R8=scalar(id=1,smin=0x800000000000000f,
                                                      smax=0x7fffffff0000000f,
                                                      umin=smin32=umin32=15,
                                                      umax=0xffffffff0000000f,
                                                      smax32=umax32=15,
                                                      var_off=(0xf; 0xffffffff00000000))
    24: (18) r2 = 0x4                     ; R2_w=4
    26: (7e) if w8 s>= w0 goto pc+5
    mark_precise: frame0: last_idx 26 first_idx 22 subseq_idx -1 
    mark_precise: frame0: regs=r5,r8 stack= before 24: (18) r2 = 0x4
    ...                   ^^^^^^^^^^
                          ^^^^^^^^^^
Here w8 == 15, w0 in range [0, 2], so the jump is being predicted,
but for some reason R0 is not among the registers that would be marked precise.

  parent reply	other threads:[~2023-12-13 23:35 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-11 15:31 [Bug Report] bpf: incorrectly pruning runtime execution path Hao Sun
2023-12-13  0:51 ` Andrii Nakryiko
2023-12-13 10:25   ` Hao Sun
2023-12-13 23:30     ` Andrii Nakryiko
2023-12-14  0:08       ` Eduard Zingerman
2023-12-14  0:36         ` Andrii Nakryiko
2023-12-13 23:35     ` Eduard Zingerman [this message]
2023-12-13 23:40       ` Andrii Nakryiko
2023-12-13 23:47         ` Eduard Zingerman
2023-12-13 23:50           ` Andrii Nakryiko
2023-12-14  9:38       ` Hao Sun
2023-12-14 15:10         ` Eduard Zingerman
2023-12-14 16:26           ` Eduard Zingerman
2023-12-15  0:06             ` Andrii Nakryiko
2023-12-15  0:16               ` Eduard Zingerman
2023-12-15  0:49               ` Eduard Zingerman
2023-12-15  1:24                 ` Eduard Zingerman
2023-12-15  1:43                   ` Eduard Zingerman
2023-12-15  2:16                   ` Alexei Starovoitov
2023-12-15  2:28                     ` Eduard Zingerman
2023-12-15  5:20                       ` Andrii Nakryiko
2023-12-15 16:22                         ` Eduard Zingerman
2023-12-15 17:01                           ` Andrii Nakryiko
2023-12-15 20:55                     ` Eduard Zingerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=480a5cfefc23446f7c82c5b87eef6306364132b9.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sunhao.th@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox