]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.19.51/bpf-fix-undefined-behavior-in-narrow-load-handling.patch
Linux 4.19.51
[thirdparty/kernel/stable-queue.git] / releases / 4.19.51 / bpf-fix-undefined-behavior-in-narrow-load-handling.patch
CommitLineData
37554d48
SL
1From 0f2c873122abd31fc364dd9aef452ae94c80d50b Mon Sep 17 00:00:00 2001
2From: Krzesimir Nowak <krzesimir@kinvolk.io>
3Date: Wed, 8 May 2019 18:08:58 +0200
4Subject: bpf: fix undefined behavior in narrow load handling
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9[ Upstream commit e2f7fc0ac6957cabff4cecf6c721979b571af208 ]
10
11Commit 31fd85816dbe ("bpf: permits narrower load from bpf program
12context fields") made the verifier add AND instructions to clear the
13unwanted bits with a mask when doing a narrow load. The mask is
14computed with
15
16 (1 << size * 8) - 1
17
18where "size" is the size of the narrow load. When doing a 4 byte load
19of a an 8 byte field the verifier shifts the literal 1 by 32 places to
20the left. This results in an overflow of a signed integer, which is an
21undefined behavior. Typically, the computed mask was zero, so the
22result of the narrow load ended up being zero too.
23
24Cast the literal to long long to avoid overflows. Note that narrow
25load of the 4 byte fields does not have the undefined behavior,
26because the load size can only be either 1 or 2 bytes, so shifting 1
27by 8 or 16 places will not overflow it. And reading 4 bytes would not
28be a narrow load of a 4 bytes field.
29
30Fixes: 31fd85816dbe ("bpf: permits narrower load from bpf program context fields")
31Reviewed-by: Alban Crequy <alban@kinvolk.io>
32Reviewed-by: Iago López Galeiras <iago@kinvolk.io>
33Signed-off-by: Krzesimir Nowak <krzesimir@kinvolk.io>
34Cc: Yonghong Song <yhs@fb.com>
35Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
36Signed-off-by: Sasha Levin <sashal@kernel.org>
37---
38 kernel/bpf/verifier.c | 2 +-
39 1 file changed, 1 insertion(+), 1 deletion(-)
40
41diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
42index acc2305ad895..d3580a68dbef 100644
43--- a/kernel/bpf/verifier.c
44+++ b/kernel/bpf/verifier.c
45@@ -5743,7 +5743,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
46 insn->dst_reg,
47 shift);
48 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
49- (1 << size * 8) - 1);
50+ (1ULL << size * 8) - 1);
51 }
52 }
53
54--
552.20.1
56