bpf: fix check_map_access smin_value test when pointer contains offset
authorDaniel Borkmann <daniel@iogearbox.net>
Mon, 28 Jan 2019 20:28:27 +0000 (21:28 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 31 Jan 2019 07:14:41 +0000 (08:14 +0100)
commit4f7f708d0e6c7ba7dc3387b3561d950a3a2ff931
tree0738f6f781b38240e5db711904df7e90debcafab
parent44f8fc6499807c03d30921486e059b951dcea63d
bpf: fix check_map_access smin_value test when pointer contains offset

[ commit b7137c4eab85c1cf3d46acdde90ce1163b28c873 upstream ]

In check_map_access() we probe actual bounds through __check_map_access()
with offset of reg->smin_value + off for lower bound and offset of
reg->umax_value + off for the upper bound. However, even though the
reg->smin_value could have a negative value, the final result of the
sum with off could be positive when pointer arithmetic with known and
unknown scalars is combined. In this case we reject the program with
an error such as "R<x> min value is negative, either use unsigned index
or do a if (index >=0) check." even though the access itself would be
fine. Therefore extend the check to probe whether the actual resulting
reg->smin_value + off is less than zero.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
kernel/bpf/verifier.c