diff mbox series

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

Message ID 20230502165754.16728-1-will@kernel.org (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Fix mask generation for 32-bit narrow loads of 64-bit fields | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-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: 20 this patch: 20
netdev/cc_maintainers fail 1 blamed authors not CCed: davem@davemloft.net; 8 maintainers not CCed: kpsingh@kernel.org martin.lau@linux.dev sdf@google.com song@kernel.org andrii@kernel.org davem@davemloft.net jolsa@kernel.org haoluo@google.com
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 20 this patch: 20
netdev/checkpatch warning WARNING: line length of 87 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-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_verifier on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-7 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16

Commit Message

Will Deacon May 2, 2023, 4:57 p.m. UTC
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 <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Krzesimir Nowak <krzesimir@kinvolk.io>
Cc: Yonghong Song <yhs@fb.com>
Cc: Andrey Ignatov <rdna@fb.com>
Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")
Signed-off-by: Will Deacon <will@kernel.org>
---

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(-)

Comments

Yonghong Song May 4, 2023, 8:18 p.m. UTC | #1
On 5/2/23 9:57 AM, Will Deacon wrote:
> 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 <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: John Fastabend <john.fastabend@gmail.com>
> Cc: Krzesimir Nowak <krzesimir@kinvolk.io>
> Cc: Yonghong Song <yhs@fb.com>
> Cc: Andrey Ignatov <rdna@fb.com>
> Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")
> Signed-off-by: Will Deacon <will@kernel.org>


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.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
> 
> 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(-)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index fbcf5a4e2fcd..5871aa78d01a 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -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);
>   			}
>   		}
Alexei Starovoitov May 5, 2023, 3:30 p.m. UTC | #2
On Thu, May 4, 2023 at 1:18 PM Yonghong Song <yhs@meta.com> wrote:
>
>
>
> On 5/2/23 9:57 AM, Will Deacon wrote:
> > 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 <ast@kernel.org>
> > Cc: Daniel Borkmann <daniel@iogearbox.net>
> > Cc: John Fastabend <john.fastabend@gmail.com>
> > Cc: Krzesimir Nowak <krzesimir@kinvolk.io>
> > Cc: Yonghong Song <yhs@fb.com>
> > Cc: Andrey Ignatov <rdna@fb.com>
> > Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")
> > Signed-off-by: Will Deacon <will@kernel.org>
>
>
> 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.
>
> Acked-by: Yonghong Song <yhs@fb.com>

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.
Yonghong Song May 5, 2023, 4:34 p.m. UTC | #3
On 5/5/23 8:30 AM, Alexei Starovoitov wrote:
> On Thu, May 4, 2023 at 1:18 PM Yonghong Song <yhs@meta.com> wrote:
>>
>>
>>
>> On 5/2/23 9:57 AM, Will Deacon wrote:
>>> 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 <ast@kernel.org>
>>> Cc: Daniel Borkmann <daniel@iogearbox.net>
>>> Cc: John Fastabend <john.fastabend@gmail.com>
>>> Cc: Krzesimir Nowak <krzesimir@kinvolk.io>
>>> Cc: Yonghong Song <yhs@fb.com>
>>> Cc: Andrey Ignatov <rdna@fb.com>
>>> Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")
>>> Signed-off-by: Will Deacon <will@kernel.org>
>>
>>
>> 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.
>>
>> Acked-by: Yonghong Song <yhs@fb.com>
> 
> 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.

> Would be good to have a selftest for this.
diff mbox series

Patch

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index fbcf5a4e2fcd..5871aa78d01a 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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);
 			}
 		}