Skip to contentSkip to conversation

Digest of [email protected] issue 63 (205)

DISCUSSION Pablo Collado Soto 1 participant 1 message Dec 4 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

Hi Allfox Wy,

First off, all the following has been done with clang(1) (and LLVM) 22.1.8 on a 5.14.0 kernel (i.e. AlmaLinux 9.9) so YMMV. I'm attaching the Makefile and source files I used to check all this in case you want to reproduce it on your machine. Loading was done with bpftoool(8) 7.5.0 which uses libbpf 1.5.0 under the hood.

Anyway, compiling and loading original.bpf.c with

	$ make original.bpf.o
	$ bpftool prog load original.bpf.o /sys/fs/bpf/xdp/original

does indeed cause the verification error you mention

	19: (71) r3 = *(u8 *)(r1 +0)
	invalid access to packet, off=13 size=1, R1(id=1,off=13,r=0)
	R1 offset is outside of the packet

If we look at the actual eBPF instructions [0] with llvm-objdump(1)

	$ llvm-objdump -S original.bpf.o

the interesting bit is

	;     if (tot_len < sizeof(*iph) || tot_len > 1500)
	      11:       bc 34 00 00 00 00 00 00 w4 = w3
	      12:       04 04 00 00 23 fa ff ff w4 += -0x5dd
	      13:       54 04 00 00 ff ff 00 00 w4 &= 0xffff
	      14:       a6 04 0a 00 37 fa 00 00 if w4 < 0xfa37 goto +0xa <xdp_print_last_ip_byte+0xc8>
	      15:       bc 33 00 00 00 00 00 00 w3 = w3

	;     __u8 *last_byte = (__u8 *)iph + tot_len - 1;
	      16:       0f 31 00 00 00 00 00 00 r1 += r3
	      17:       07 01 00 00 ff ff ff ff r1 += -0x1

	;     if ((void *)last_byte >= data_end)
	      18:       3d 21 06 00 00 00 00 00 if r1 >= r2 goto +0x6 <xdp_print_last_ip_byte+0xc8>

	;     bpf_printk("last IP byte: 0x%02x (%u)\n", *last_byte, *last_byte);
	      19:       71 13 00 00 00 00 00 00 w3 = *(u8 *)(r1 + 0x0)

As we're compiling with optimisations enabled (ie.e. with the clang(1) -O2 flag) the check

	if (tot_len < sizeof(*iph) || tot_len > 1500)
		return XDP_PASS;

is being 'condensed' into a check against 0xfa37. On the verifier output we find

	; if (tot_len < sizeof(*iph) || tot_len > 1500) @ original.bpf.c:25
	11: (bc) w4 = w3                      ; R3_w=scalar() R4_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))
	12: (04) w4 += -1501                  ; R4_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))
	13: (54) w4 &= 65535                  ; R4_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=0xffff,var_off=(0x0; 0xffff))
	14: (a6) if w4 < 0xfa37 goto pc+10    ; R4_w=scalar(smin=umin=smin32=umin32=0xfa37,smax=umax=smax32=umax32=0xffff,var_off=(0xf800; 0x7ff))
	15: (bc) w3 = w3                      ; R3_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))

After reading [1] to get a feel for what we're seeing we can conclude the verifier imposes no limits on the maximum value of R3 so it assumes R3 is any 32-bit number. This implies that adding this number could cause an overflow causing the pointer stored in R1 to point at something outside the [ctx->data, ctx->data_end] range. This explains why later on we see

	; if ((void *)last_byte >= data_end) @ original.bpf.c:29
	18: (3d) if r1 >= r2 goto pc+6        ; R1=pkt(id=1,off=13,r=0,smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff)) R2=pkt_end()

The verifier doesn't consider **any** range valid (i.e. r=0). It seems the problem has nothing to do with the endianness swap operation which in the verifier we see as

	10: (dc) r3 = be16 r3                 ; R3_w=scalar()

The fact that the check on R4 doesn't apply to R3's bounds is what's throwing the verifier off. Applying your fix (i.e. the 0x0fff mask) doesn't solve the issue for my setup either. With that the verifier says

	; __u32 tot_len = bpf_ntohs(iph->tot_len); @ mask-fix.bpf.c:24
	9: (69) r3 = *(u16 *)(r1 +2)          ; R1=pkt(off=14,r=34) R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=0xffff,var_off=(0x0; 0xffff))

	; tot_len &= 0x0fff; @ mask-fix.bpf.c:25
	10: (54) w3 &= 65295                  ; R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=0xff0f,var_off=(0x0; 0xff0f))
	11: (dc) r3 = be16 r3                 ; R3_w=scalar()

	; if (tot_len < sizeof(*iph) || tot_len > 1500) @ mask-fix.bpf.c:26
	12: (bc) w4 = w3                      ; R3_w=scalar() R4_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))
	13: (04) w4 += -1501                  ; R4_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))
	14: (a6) if w4 < 0xfffffa37 goto pc+10        ; R4_w=scalar(smin=umin=umin32=0xfffffa37,smax=umax=0xffffffff,smin32=-1481,smax32=-1,var_off=(0xfffff800; 0x7ff))
	15: (bc) w3 = w3                      ; R3_w=scalar(smin=0,smax=umax=0xffffffff,var_off=(0x0; 0xffffffff))

but the problem remains at instruction #15 where R3 is unbounded. You could try to compile the eBPF program without any optimisations, thus avoiding the combination of the checks

	$ clang -g -target bpf -c original.bpf.c -o original-no-opt.bpf.o

loading such a program quickly fails however: several rules the verifier expects to be followed aren't adhered to which are explained on [1]. The first error I see is

	2: (b7) r2 = 4                        ; R2_w=4
	3: (79) r2 = *(u64 *)(r2 +0)
	R2 invalid mem access 'scalar'

The problem is we're trying to dereference an address based off-of an scalar. The proposed mask is, at least on my machine, being ignored by optimisations. As LLVM knows the maximum value on R3 will be 1500 as per our check and given 1500 can be represented with ceil(log_2(1500)) = 11 bits any mask larger than 0x03FF is simply discarded by the compiler as it adds nothing. It's this optimisation that LLVM can make what's causing the verifier failures. Given the length field in an IPv4 header is 16 bits masking with anything other than 0xFFFF risks loosing information. You do impose that the length is less than 1500, so you could mask with 0x07FF, but that's simply going to be ignored by LLVM and it won't show up in the final binary.

Anyway, we have established the problem is we cannot place any bounds on R3's value and so the verifier doesn't validate any range of addresses for us to access, triggering the issue on the first access. The convoluted check only happens because you're checking both tot_len < sizeof(*iph) || tot_len > 1500, if we only check the latter (as seen on fixed.bpf.c) we instead get the following instructions

	;     if (tot_len > 1500)
	      11:       26 03 0a 00 dc 05 00 00 if w3 > 0x5dc goto +0xa <xdp_print_last_ip_byte+0xb0>

which the verifier now picks up correctly. You can get the trace with

	$ bpftool prog load -d fixed.bpf.o /sys/fs/bpf/xdp/fixed

which shows

	; __u32 tot_len = bpf_ntohs(iph->tot_len); @ idea.bpf.c:24
	9: (69) r3 = *(u16 *)(r1 +2)          ; R1=pkt(off=14,r=34) R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=0xffff,var_off=(0x0; 0xffff))
	10: (dc) r3 = be16 r3                 ; R3_w=scalar()

	; if (tot_len > 1500) @ idea.bpf.c:25
	11: (26) if w3 > 0x5dc goto pc+10     ; R3_w=scalar(smax=0x7fffffff000005dc,umax=0xffffffff000005dc,smin32=0,smax32=umax32=1500,var_off=(0x0; 0xffffffff000007ff))

		Note how the value of R3 is bounded here! It'll be 1500 at most.

	; __u8 *last_byte = (__u8 *)iph + tot_len - 1; @ idea.bpf.c:28
	12: (bc) w3 = w3                      ; R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))
	13: (0f) r1 += r3                     ; R1_w=pkt(id=1,off=14,r=0,smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))
						R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))

		As R3 is bounded here, so is R1 at 1500.

	14: (07) r1 += -1                     ; R1_w=pkt(id=1,off=13,r=0,smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))

	; if ((void *)last_byte >= data_end) @ idea.bpf.c:29
	15: (3d) if r1 >= r2 goto pc+6        ; R1_w=pkt(id=1,off=13,r=14,smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff)) R2=pkt_end()

		And the last key of the puzzle is here, where the allowed access range goes from r=0 to
		r=14; we can now access the [R0, R0 + 14] range!

	; bpf_printk("last IP byte: 0x%02x (%u)\n", *last_byte, *last_byte); @ idea.bpf.c:32
	16: (71) r3 = *(u8 *)(r1 +0)          ; R1_w=pkt(id=1,off=13,r=14,smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))
						R3_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff))

		The call to bpf_printk now succeeds!

The verifier output for my 5.14 kernel is a bit cryptic when it comes to the off and r fields for direct packet accesses. Trying to load the same program on a 7.1.12 kernel with bpftool 7.6.0 (libbpf 1.6) shows

	; if (tot_len > 1500) @ idea.bpf.c:25
	11: (26) if w3 > 0x5dc goto pc+10     ; R3=scalar(smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))

		R3 becomes bound in pretty much the same way.

	; __u8 *last_byte = (__u8 *)iph + tot_len - 1; @ idea.bpf.c:28
	12: (bc) w3 = w3                      ; R3=scalar(id=1,smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))
	13: (0f) r1 += r3                     ; R1=pkt(id=2,r=0,smin=umin=smin32=umin32=14,smax=umax=smax32=umax32=1514,var_off=(0x0; 0x7ff))
						R3=scalar(id=1,smin=smin32=0,smax=umax=smax32=umax32=1500,var_off=(0x0; 0x7ff))

		But... this trace is much easier to read! R1 is bounded but not at 1500: it was 14 already! So... it's
		real actual maximum value was 1514 as is correctly shown here. I do not know if this in an issue in the verifier
		itself or in the way it prints information. I suppose it's the latter as the `off` field is gone. I'm sure someone
		else will know :P

	14: (07) r1 += -1                     ; R1=pkt(id=2,r=0,smin=umin=smin32=umin32=13,smax=umax=smax32=umax32=1513,var_off=(0x0; 0x7ff))

		The maximum theoretical value is decremented too, but no range has been validated yet (i.e. r=0 still).

	; if ((void *)last_byte >= data_end) @ idea.bpf.c:29
	15: (3d) if r1 >= r2 goto pc+6        ; R1=pkt(id=2,r=1514,smin=umin=smin32=umin32=13,smax=umax=smax32=umax32=1513,var_off=(0x0; 0x7ff)) R2=pkt_end()

		This check validates the r=1514 range! So we can now access the [R0, R0 + 1514] range with no problem at all.
		This makes sense, 1500 is the maximum L4 payload length we enforced and 14 is the length of the L2 Ethernet
		header we've already 'consumed'.

	; bpf_printk("last IP byte: 0x%02x (%u)\n", *last_byte, *last_byte); @ idea.bpf.c:32
	16: (71) r3 = *(u8 *)(r1 +0)          ; R1=pkt(id=2,r=1514,smin=umin=smin32=umin32=13,smax=umax=smax32=umax32=1513,var_off=(0x0; 0x7ff))
						R3=scalar(smin=smin32=0,smax=umax=smax32=umax32=255,var_off=(0x0; 0xff))

		And the access can once again succeed!

The verifier output for trying to load original.bpf.c on the 7.1.12 kernel is also a bit more telling:

	; __u8 *last_byte = (__u8 *)iph + tot_len - 1; @ original.bpf.c:28
	16: (0f) r1 += r3                     ; R1=pkt(id=2,r=0,smin=umin=smin32=umin32=14,smax=umax=smax32=umax32=0x1000d,var_off=(0x0; 0x1ffff))
						R3=scalar(id=1,smin=smin32=0,smax=umax=smax32=umax32=0xffff,var_off=(0x0; 0xffff))

Notice how the maximum value (0x1000d) is larger than 0xFFFF which I guess causes all sorts of issues with packet access.

So... putting it all together the problem is the check

	if (tot_len < sizeof(*iph) || tot_len > 1500)

that's throwing it all into disarray... One thing you can do is drop the tot_len < sizeof(*iph) check which might just be okay depending on your use case. If not, you can try to add the assembly check for that condition by hand to circumvent the LLVM optimiser or maybe someone else has a better idea for dealing with all this.

At any rate, I hope this explanation is both correct (which I don't guarantee!) and informative :P

All the best,

References:
0: https://www.rfc-editor.org/rfc/rfc9669.html
1: https://docs.kernel.org/bpf/verifier.html#direct-packet-access

Signature · 5 lines
Pablo Collado Soto
+ ----------------------------------------------- +
| I have a very small mind and must live with it. |
|     -- E. Dijkstra                              |
+ ----------------------------------------------- +