]> git.baikalelectronics.ru Git - kernel.git/commit
bpf: fix undefined behavior in narrow load handling
authorKrzesimir Nowak <krzesimir@kinvolk.io>
Wed, 8 May 2019 16:08:58 +0000 (18:08 +0200)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 13 May 2019 00:05:50 +0000 (02:05 +0200)
commit4ebe95d53e45fe8ec07ac33aeabc824c1cb0e065
treecaaf45b1359a0a04f475b0b434c8a34a1d1d81e7
parent3d18bd4c45b5e90bb740a4bafeb3dd1edf97e252
bpf: fix undefined behavior in narrow load handling

Commit ea168ec57242 ("bpf: permits narrower load from bpf program
context fields") made the verifier add AND instructions to clear the
unwanted bits with a mask when doing a narrow load. The mask is
computed with

  (1 << size * 8) - 1

where "size" is the size of the narrow load. When doing a 4 byte load
of a an 8 byte field the verifier shifts the literal 1 by 32 places to
the left. This results in an overflow of a signed integer, which is an
undefined behavior. Typically, the computed mask was zero, so the
result of the narrow load ended up being zero too.

Cast the literal to long long to avoid overflows. Note that narrow
load of the 4 byte fields does not have the undefined behavior,
because the load size can only be either 1 or 2 bytes, so shifting 1
by 8 or 16 places will not overflow it. And reading 4 bytes would not
be a narrow load of a 4 bytes field.

Fixes: ea168ec57242 ("bpf: permits narrower load from bpf program context fields")
Reviewed-by: Alban Crequy <alban@kinvolk.io>
Reviewed-by: Iago López Galeiras <iago@kinvolk.io>
Signed-off-by: Krzesimir Nowak <krzesimir@kinvolk.io>
Cc: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
kernel/bpf/verifier.c