* [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
@ 2015-05-08 5:39 ` Xi Wang
0 siblings, 0 replies; 10+ messages in thread
From: Xi Wang @ 2015-05-08 5:39 UTC (permalink / raw)
To: linux-arm-kernel
Cc: linux-kernel, Xi Wang, Zi Shen Lim, Alexei Starovoitov,
Catalin Marinas, Will Deacon
Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT. Since imm is
signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
The fix is to convert imm to u32 first and zero-extend it to u64.
Also extend test_bpf to catch this JIT bug; the interpreter is correct.
Before:
test_bpf: #58 load 64-bit immediate ret -1 != 1 FAIL (1 times)
After:
test_bpf: #58 load 64-bit immediate 74 PASS
Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
Cc: Zi Shen Lim <zlim.lnx@gmail.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
arch/arm64/net/bpf_jit_comp.c | 2 +-
lib/test_bpf.c | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index edba042b2325..14cdc099fda0 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -487,7 +487,7 @@ emit_cond_jmp:
return -EINVAL;
}
- imm64 = (u64)insn1.imm << 32 | imm;
+ imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
emit_a64_mov_i64(dst, imm64, ctx);
return 1;
diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index 80d78c51f65f..9f6849891b5f 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -1755,7 +1755,8 @@ static struct bpf_test tests[] = {
BPF_EXIT_INSN(),
BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
BPF_EXIT_INSN(),
- BPF_ALU64_IMM(BPF_MOV, R0, 1),
+ BPF_LD_IMM64(R0, 0x1ffffffffLL),
+ BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
BPF_EXIT_INSN(),
},
INTERNAL,
--
1.9.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
@ 2015-05-08 5:39 ` Xi Wang
0 siblings, 0 replies; 10+ messages in thread
From: Xi Wang @ 2015-05-08 5:39 UTC (permalink / raw)
To: linux-arm-kernel
Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT. Since imm is
signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
The fix is to convert imm to u32 first and zero-extend it to u64.
Also extend test_bpf to catch this JIT bug; the interpreter is correct.
Before:
test_bpf: #58 load 64-bit immediate ret -1 != 1 FAIL (1 times)
After:
test_bpf: #58 load 64-bit immediate 74 PASS
Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
Cc: Zi Shen Lim <zlim.lnx@gmail.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
arch/arm64/net/bpf_jit_comp.c | 2 +-
lib/test_bpf.c | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index edba042b2325..14cdc099fda0 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -487,7 +487,7 @@ emit_cond_jmp:
return -EINVAL;
}
- imm64 = (u64)insn1.imm << 32 | imm;
+ imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
emit_a64_mov_i64(dst, imm64, ctx);
return 1;
diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index 80d78c51f65f..9f6849891b5f 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -1755,7 +1755,8 @@ static struct bpf_test tests[] = {
BPF_EXIT_INSN(),
BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
BPF_EXIT_INSN(),
- BPF_ALU64_IMM(BPF_MOV, R0, 1),
+ BPF_LD_IMM64(R0, 0x1ffffffffLL),
+ BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
BPF_EXIT_INSN(),
},
INTERNAL,
--
1.9.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
2015-05-08 5:39 ` Xi Wang
@ 2015-05-08 8:38 ` Will Deacon
-1 siblings, 0 replies; 10+ messages in thread
From: Will Deacon @ 2015-05-08 8:38 UTC (permalink / raw)
To: Xi Wang
Cc: linux-arm-kernel, linux-kernel, Zi Shen Lim, Alexei Starovoitov,
Catalin Marinas
On Fri, May 08, 2015 at 06:39:51AM +0100, Xi Wang wrote:
> Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT. Since imm is
> signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
> The fix is to convert imm to u32 first and zero-extend it to u64.
>
> Also extend test_bpf to catch this JIT bug; the interpreter is correct.
>
> Before:
> test_bpf: #58 load 64-bit immediate ret -1 != 1 FAIL (1 times)
>
> After:
> test_bpf: #58 load 64-bit immediate 74 PASS
>
> Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
> Cc: Zi Shen Lim <zlim.lnx@gmail.com>
> Cc: Alexei Starovoitov <ast@plumgrid.com>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will.deacon@arm.com>
> Signed-off-by: Xi Wang <xi.wang@gmail.com>
> ---
> arch/arm64/net/bpf_jit_comp.c | 2 +-
> lib/test_bpf.c | 3 ++-
> 2 files changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index edba042b2325..14cdc099fda0 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
> @@ -487,7 +487,7 @@ emit_cond_jmp:
> return -EINVAL;
> }
>
> - imm64 = (u64)insn1.imm << 32 | imm;
> + imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
This seems a bit convoluted to me. Don't you just need to add a (u32)
cast to imm and that's it? The (u64)(u32) looks redundant.
> emit_a64_mov_i64(dst, imm64, ctx);
>
> return 1;
> diff --git a/lib/test_bpf.c b/lib/test_bpf.c
> index 80d78c51f65f..9f6849891b5f 100644
> --- a/lib/test_bpf.c
> +++ b/lib/test_bpf.c
> @@ -1755,7 +1755,8 @@ static struct bpf_test tests[] = {
> BPF_EXIT_INSN(),
> BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
> BPF_EXIT_INSN(),
> - BPF_ALU64_IMM(BPF_MOV, R0, 1),
> + BPF_LD_IMM64(R0, 0x1ffffffffLL),
> + BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
> BPF_EXIT_INSN(),
This hunk should probably be a separate patch, unless you get Alexei's ack
for me to take it via the arm64 tree too.
Will
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
@ 2015-05-08 8:38 ` Will Deacon
0 siblings, 0 replies; 10+ messages in thread
From: Will Deacon @ 2015-05-08 8:38 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, May 08, 2015 at 06:39:51AM +0100, Xi Wang wrote:
> Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT. Since imm is
> signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
> The fix is to convert imm to u32 first and zero-extend it to u64.
>
> Also extend test_bpf to catch this JIT bug; the interpreter is correct.
>
> Before:
> test_bpf: #58 load 64-bit immediate ret -1 != 1 FAIL (1 times)
>
> After:
> test_bpf: #58 load 64-bit immediate 74 PASS
>
> Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
> Cc: Zi Shen Lim <zlim.lnx@gmail.com>
> Cc: Alexei Starovoitov <ast@plumgrid.com>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will.deacon@arm.com>
> Signed-off-by: Xi Wang <xi.wang@gmail.com>
> ---
> arch/arm64/net/bpf_jit_comp.c | 2 +-
> lib/test_bpf.c | 3 ++-
> 2 files changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index edba042b2325..14cdc099fda0 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
> @@ -487,7 +487,7 @@ emit_cond_jmp:
> return -EINVAL;
> }
>
> - imm64 = (u64)insn1.imm << 32 | imm;
> + imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
This seems a bit convoluted to me. Don't you just need to add a (u32)
cast to imm and that's it? The (u64)(u32) looks redundant.
> emit_a64_mov_i64(dst, imm64, ctx);
>
> return 1;
> diff --git a/lib/test_bpf.c b/lib/test_bpf.c
> index 80d78c51f65f..9f6849891b5f 100644
> --- a/lib/test_bpf.c
> +++ b/lib/test_bpf.c
> @@ -1755,7 +1755,8 @@ static struct bpf_test tests[] = {
> BPF_EXIT_INSN(),
> BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
> BPF_EXIT_INSN(),
> - BPF_ALU64_IMM(BPF_MOV, R0, 1),
> + BPF_LD_IMM64(R0, 0x1ffffffffLL),
> + BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
> BPF_EXIT_INSN(),
This hunk should probably be a separate patch, unless you get Alexei's ack
for me to take it via the arm64 tree too.
Will
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
2015-05-08 8:38 ` Will Deacon
@ 2015-05-08 8:45 ` Xi Wang
-1 siblings, 0 replies; 10+ messages in thread
From: Xi Wang @ 2015-05-08 8:45 UTC (permalink / raw)
To: Will Deacon
Cc: linux-arm-kernel, linux-kernel, Zi Shen Lim, Alexei Starovoitov,
Catalin Marinas
On Fri, May 8, 2015 at 1:38 AM, Will Deacon <will.deacon@arm.com> wrote:
>> - imm64 = (u64)insn1.imm << 32 | imm;
>> + imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
>
> This seems a bit convoluted to me. Don't you just need to add a (u32)
> cast to imm and that's it? The (u64)(u32) looks redundant.
You're right - the second (u64) is redundant; the hope was to make
the code easier to understand. It's from the interpreter code in
kernel/core/bpf.c, which uses (u64)(u32) as well.
>> - BPF_ALU64_IMM(BPF_MOV, R0, 1),
>> + BPF_LD_IMM64(R0, 0x1ffffffffLL),
>> + BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
>> BPF_EXIT_INSN(),
>
> This hunk should probably be a separate patch, unless you get Alexei's ack
> for me to take it via the arm64 tree too.
I would be happy to split this into a separate patch if that works
better, or simply drop this part.
- xi
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
@ 2015-05-08 8:45 ` Xi Wang
0 siblings, 0 replies; 10+ messages in thread
From: Xi Wang @ 2015-05-08 8:45 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, May 8, 2015 at 1:38 AM, Will Deacon <will.deacon@arm.com> wrote:
>> - imm64 = (u64)insn1.imm << 32 | imm;
>> + imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
>
> This seems a bit convoluted to me. Don't you just need to add a (u32)
> cast to imm and that's it? The (u64)(u32) looks redundant.
You're right - the second (u64) is redundant; the hope was to make
the code easier to understand. It's from the interpreter code in
kernel/core/bpf.c, which uses (u64)(u32) as well.
>> - BPF_ALU64_IMM(BPF_MOV, R0, 1),
>> + BPF_LD_IMM64(R0, 0x1ffffffffLL),
>> + BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
>> BPF_EXIT_INSN(),
>
> This hunk should probably be a separate patch, unless you get Alexei's ack
> for me to take it via the arm64 tree too.
I would be happy to split this into a separate patch if that works
better, or simply drop this part.
- xi
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
2015-05-08 8:45 ` Xi Wang
@ 2015-05-08 15:17 ` Will Deacon
-1 siblings, 0 replies; 10+ messages in thread
From: Will Deacon @ 2015-05-08 15:17 UTC (permalink / raw)
To: Xi Wang
Cc: linux-arm-kernel, linux-kernel, Zi Shen Lim, Alexei Starovoitov,
Catalin Marinas
On Fri, May 08, 2015 at 09:45:59AM +0100, Xi Wang wrote:
> On Fri, May 8, 2015 at 1:38 AM, Will Deacon <will.deacon@arm.com> wrote:
> >> - imm64 = (u64)insn1.imm << 32 | imm;
> >> + imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
> >
> > This seems a bit convoluted to me. Don't you just need to add a (u32)
> > cast to imm and that's it? The (u64)(u32) looks redundant.
>
> You're right - the second (u64) is redundant; the hope was to make
> the code easier to understand. It's from the interpreter code in
> kernel/core/bpf.c, which uses (u64)(u32) as well.
>
> >> - BPF_ALU64_IMM(BPF_MOV, R0, 1),
> >> + BPF_LD_IMM64(R0, 0x1ffffffffLL),
> >> + BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
> >> BPF_EXIT_INSN(),
> >
> > This hunk should probably be a separate patch, unless you get Alexei's ack
> > for me to take it via the arm64 tree too.
>
> I would be happy to split this into a separate patch if that works
> better, or simply drop this part.
Ok, I plan to apply the patch below for 4.1.
Will
--->8
>From 1e4df6b7208140f3c49f316d33a409d3a161f350 Mon Sep 17 00:00:00 2001
From: Xi Wang <xi.wang@gmail.com>
Date: Fri, 8 May 2015 06:39:51 +0100
Subject: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT. Since imm is
signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
The fix is to convert imm to u32 first, which will be zero-extended to
u64 implicitly.
Cc: Zi Shen Lim <zlim.lnx@gmail.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: <stable@vger.kernel.org>
Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
Signed-off-by: Xi Wang <xi.wang@gmail.com>
[will: removed non-arm64 bits and redundant casting]
Signed-off-by: Will Deacon <will.deacon@arm.com>
---
arch/arm64/net/bpf_jit_comp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index edba042b2325..dc6a4842683a 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -487,7 +487,7 @@ emit_cond_jmp:
return -EINVAL;
}
- imm64 = (u64)insn1.imm << 32 | imm;
+ imm64 = (u64)insn1.imm << 32 | (u32)imm;
emit_a64_mov_i64(dst, imm64, ctx);
return 1;
--
2.1.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
@ 2015-05-08 15:17 ` Will Deacon
0 siblings, 0 replies; 10+ messages in thread
From: Will Deacon @ 2015-05-08 15:17 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, May 08, 2015 at 09:45:59AM +0100, Xi Wang wrote:
> On Fri, May 8, 2015 at 1:38 AM, Will Deacon <will.deacon@arm.com> wrote:
> >> - imm64 = (u64)insn1.imm << 32 | imm;
> >> + imm64 = ((u64)(u32)insn1.imm) << 32 | (u64)(u32)imm;
> >
> > This seems a bit convoluted to me. Don't you just need to add a (u32)
> > cast to imm and that's it? The (u64)(u32) looks redundant.
>
> You're right - the second (u64) is redundant; the hope was to make
> the code easier to understand. It's from the interpreter code in
> kernel/core/bpf.c, which uses (u64)(u32) as well.
>
> >> - BPF_ALU64_IMM(BPF_MOV, R0, 1),
> >> + BPF_LD_IMM64(R0, 0x1ffffffffLL),
> >> + BPF_ALU64_IMM(BPF_RSH, R0, 32), /* R0 = 1 */
> >> BPF_EXIT_INSN(),
> >
> > This hunk should probably be a separate patch, unless you get Alexei's ack
> > for me to take it via the arm64 tree too.
>
> I would be happy to split this into a separate patch if that works
> better, or simply drop this part.
Ok, I plan to apply the patch below for 4.1.
Will
--->8
>From 1e4df6b7208140f3c49f316d33a409d3a161f350 Mon Sep 17 00:00:00 2001
From: Xi Wang <xi.wang@gmail.com>
Date: Fri, 8 May 2015 06:39:51 +0100
Subject: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
Consider "(u64)insn1.imm << 32 | imm" in the arm64 JIT. Since imm is
signed 32-bit, it is sign-extended to 64-bit, losing the high 32 bits.
The fix is to convert imm to u32 first, which will be zero-extended to
u64 implicitly.
Cc: Zi Shen Lim <zlim.lnx@gmail.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: <stable@vger.kernel.org>
Fixes: 30d3d94cc3d5 ("arm64: bpf: add 'load 64-bit immediate' instruction")
Signed-off-by: Xi Wang <xi.wang@gmail.com>
[will: removed non-arm64 bits and redundant casting]
Signed-off-by: Will Deacon <will.deacon@arm.com>
---
arch/arm64/net/bpf_jit_comp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index edba042b2325..dc6a4842683a 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -487,7 +487,7 @@ emit_cond_jmp:
return -EINVAL;
}
- imm64 = (u64)insn1.imm << 32 | imm;
+ imm64 = (u64)insn1.imm << 32 | (u32)imm;
emit_a64_mov_i64(dst, imm64, ctx);
return 1;
--
2.1.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
2015-05-08 15:17 ` Will Deacon
@ 2015-05-08 15:30 ` Alexei Starovoitov
-1 siblings, 0 replies; 10+ messages in thread
From: Alexei Starovoitov @ 2015-05-08 15:30 UTC (permalink / raw)
To: Will Deacon, Xi Wang
Cc: linux-arm-kernel, linux-kernel, Zi Shen Lim, Catalin Marinas
On 5/8/15 8:17 AM, Will Deacon wrote:
>
> Ok, I plan to apply the patch below for 4.1.
great catch. Looks good to me.
Xi, could you send a separate patch for test_bpf update to net-next?
Thanks!
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate
@ 2015-05-08 15:30 ` Alexei Starovoitov
0 siblings, 0 replies; 10+ messages in thread
From: Alexei Starovoitov @ 2015-05-08 15:30 UTC (permalink / raw)
To: linux-arm-kernel
On 5/8/15 8:17 AM, Will Deacon wrote:
>
> Ok, I plan to apply the patch below for 4.1.
great catch. Looks good to me.
Xi, could you send a separate patch for test_bpf update to net-next?
Thanks!
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2015-05-08 15:30 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-08 5:39 [PATCH] arm64: bpf: fix signedness bug in loading 64-bit immediate Xi Wang
2015-05-08 5:39 ` Xi Wang
2015-05-08 8:38 ` Will Deacon
2015-05-08 8:38 ` Will Deacon
2015-05-08 8:45 ` Xi Wang
2015-05-08 8:45 ` Xi Wang
2015-05-08 15:17 ` Will Deacon
2015-05-08 15:17 ` Will Deacon
2015-05-08 15:30 ` Alexei Starovoitov
2015-05-08 15:30 ` Alexei Starovoitov
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.