Re: [PATCH bpf-next v2 1/2] bpf: add bound tracking for BPF_MOD

From: Xu Kuohai
Date: Fri Mar 24 2023 - 21:57:41 EST


On 3/24/2023 1:16 AM, Alexei Starovoitov wrote:
On Thu, Mar 23, 2023 at 8:59 AM Xu Kuohai <xukuohai@xxxxxxxxxxxxxxx> wrote:

From: Xu Kuohai <xukuohai@xxxxxxxxxx>

dst_reg is marked as unknown when BPF_MOD instruction is verified, causing
the following bpf prog to be incorrectly rejected.

0: r0 = 0
1: r0 %= 1 // r0 is marked as unknown
2: r1 = 0
3: r1 += 1
4: if r1 < r0 goto pc-2 // verifier treats the loop as unbounded
5: exit

To teach verifier to accept the above prog, this patch adds bound tracking
for BPF_MOD.

The approach is based on the following rules:

1. BPF_MOD is unsigned;

2. For an unsigned constant divisor x:

a. when x != 0, the resulted dst_reg bits are in the range [0, x - 1],
and if no wrapping occurs, the result can be further narrowed down
to [umin mod x, umax mod x];

b. when x == 0, dst_reg is truncated to 32 bits by mod32 or remains
unchanged by mod64.

Signed-off-by: Xu Kuohai <xukuohai@xxxxxxxxxx>

Same Nack as before.

Sorry, I did not receive Nack for this patch before.

You haven't answered _why_ anyone needs it.

No idea, I simply believe it's good to have a closer estimate of the result.