Skip to contentSkip to conversation

bpf: Fix mask generation for 32-bit narrow loads of 64-bit fields

PATCH Superseded Will Deacon 3 participants 4 messages May 2, 2023 – May 5, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Fix mask generation for 32-bit narrow loads of 64-bit fields

A narrow load from a 64-bit context field results in a 64-bit load followed potentially by a 64-bit right-shift and then a bitwise AND operation to extract the relevant data.

In the case of a 32-bit access, an immediate mask of 0xffffffff is used to construct a 64-bit BPP_AND operation which then sign-extends the mask value and effectively acts as a glorified no-op.

Fix the mask generation so that narrow loads always perform a 32-bit AND operation.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Krzesimir Nowak <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Andrey Ignatov <[email protected]>
Fixes 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")

---

I spotted this while playing around with the JIT on arm64. I can't figure out why 31fd85816dbe special-cases 8-byte ctx fields in the first place, so I fear I may be missing something...

 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index fbcf5a4e2fcd..5871aa78d01a 100644
@@ -17033,7 +17033,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
 					insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
 									insn->dst_reg,
 									shift);
-				insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
+				insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
 								(1ULL << size * 8) - 1);
 			}
 		}
Signature · 1 line
2.40.1.495.gc816e09b53d-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Will Deacon

A narrow load from a 64-bit context field results in a 64-bit load followed potentially by a 64-bit right-shift and then a bitwise AND operation to extract the relevant data.

In the case of a 32-bit access, an immediate mask of 0xffffffff is used to construct a 64-bit BPP_AND operation which then sign-extends the mask value and effectively acts as a glorified no-op.

Fix the mask generation so that narrow loads always perform a 32-bit AND operation.

Quoted 8 lines
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Krzesimir Nowak <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Andrey Ignatov <[email protected]>
Fixes 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")

Thanks for the fix! You didn't miss anything. It is a bug and we did not find it probably because user always use 'u64 val = ctx->u64_field' in their bpf code...

But I think the commit message can be improved. An example to show the difference without and with this patch can explain the issue much better.

Quoted 22 lines

---

I spotted this while playing around with the JIT on arm64. I can't figure out why 31fd85816dbe special-cases 8-byte ctx fields in the first place, so I fear I may be missing something...

  kernel/bpf/verifier.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index fbcf5a4e2fcd..5871aa78d01a 100644
@@ -17033,7 +17033,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
  					insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
  									insn->dst_reg,
  									shift);
-				insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
+				insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
  								(1ULL << size * 8) - 1);
  			}
  		}
#2 lore raw
Quoted 25 lines · Yonghong Song
Quoted 11 lines · Will Deacon

A narrow load from a 64-bit context field results in a 64-bit load followed potentially by a 64-bit right-shift and then a bitwise AND operation to extract the relevant data.

In the case of a 32-bit access, an immediate mask of 0xffffffff is used to construct a 64-bit BPP_AND operation which then sign-extends the mask value and effectively acts as a glorified no-op.

Fix the mask generation so that narrow loads always perform a 32-bit AND operation.

Quoted 8 lines
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Krzesimir Nowak <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Andrey Ignatov <[email protected]>
Fixes 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")
Quoted 8 lines

Thanks for the fix! You didn't miss anything. It is a bug and we did not find it probably because user always use 'u64 val = ctx->u64_field' in their bpf code...

But I think the commit message can be improved. An example to show the difference without and with this patch can explain the issue much better.

If I'm reading it correctly it's indeed a bug. alu64(and, 0xffffFFFF) is a nop
but it should have been
alu32(and, 0xffffFFFF) which will clear upper 32-bit, right? Would be good to have a selftest for this.

#3 lore raw
Quoted 33 lines · Alexei Starovoitov
Quoted 32 lines · Yonghong Song
Quoted 11 lines · Will Deacon

A narrow load from a 64-bit context field results in a 64-bit load followed potentially by a 64-bit right-shift and then a bitwise AND operation to extract the relevant data.

In the case of a 32-bit access, an immediate mask of 0xffffffff is used to construct a 64-bit BPP_AND operation which then sign-extends the mask value and effectively acts as a glorified no-op.

Fix the mask generation so that narrow loads always perform a 32-bit AND operation.

Quoted 8 lines
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Krzesimir Nowak <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Andrey Ignatov <[email protected]>
Fixes 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")

Thanks for the fix! You didn't miss anything. It is a bug and we did not find it probably because user always use 'u64 val = ctx->u64_field' in their bpf code...

But I think the commit message can be improved. An example to show the difference without and with this patch can explain the issue much better.

Quoted 6 lines
Quoted 1 line

If I'm reading it correctly it's indeed a bug. alu64(and, 0xffffFFFF) is a nop
but it should have been
alu32(and, 0xffffFFFF) which will clear upper 32-bit, right?

Right. This is my understanding as well.

Quoted 1 line

Would be good to have a selftest for this.