Commit 1b1597e6 authored by Piotr Krysiuk's avatar Piotr Krysiuk Committed by Daniel Borkmann

bpf: Add sanity check for upper ptr_limit

Given we know the max possible value of ptr_limit at the time of retrieving
the latter, add basic assertions, so that the verifier can bail out if
anything looks odd and reject the program. Nothing triggered this so far,
but it also does not hurt to have these.
Signed-off-by: default avatarPiotr Krysiuk <piotras@gmail.com>
Co-developed-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent b5871dca
...@@ -5861,10 +5861,14 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, ...@@ -5861,10 +5861,14 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
{ {
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
(opcode == BPF_SUB && !off_is_neg); (opcode == BPF_SUB && !off_is_neg);
u32 off; u32 off, max;
switch (ptr_reg->type) { switch (ptr_reg->type) {
case PTR_TO_STACK: case PTR_TO_STACK:
/* Offset 0 is out-of-bounds, but acceptable start for the
* left direction, see BPF_REG_FP.
*/
max = MAX_BPF_STACK + mask_to_left;
/* Indirect variable offset stack access is prohibited in /* Indirect variable offset stack access is prohibited in
* unprivileged mode so it's not handled here. * unprivileged mode so it's not handled here.
*/ */
...@@ -5873,15 +5877,16 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, ...@@ -5873,15 +5877,16 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
*ptr_limit = MAX_BPF_STACK + off; *ptr_limit = MAX_BPF_STACK + off;
else else
*ptr_limit = -off - 1; *ptr_limit = -off - 1;
return 0; return *ptr_limit >= max ? -ERANGE : 0;
case PTR_TO_MAP_VALUE: case PTR_TO_MAP_VALUE:
max = ptr_reg->map_ptr->value_size;
if (mask_to_left) { if (mask_to_left) {
*ptr_limit = ptr_reg->umax_value + ptr_reg->off; *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
} else { } else {
off = ptr_reg->smin_value + ptr_reg->off; off = ptr_reg->smin_value + ptr_reg->off;
*ptr_limit = ptr_reg->map_ptr->value_size - off - 1; *ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
} }
return 0; return *ptr_limit >= max ? -ERANGE : 0;
default: default:
return -EINVAL; return -EINVAL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment