Skip to content

Commit ef4e68f

Browse files
borkmanngregkh
authored andcommitted
bpf: Tighten speculative pointer arithmetic mask
commit 7fedb63 upstream. This work tightens the offset mask we use for unprivileged pointer arithmetic in order to mitigate a corner case reported by Piotr and Benedict where in the speculative domain it is possible to advance, for example, the map value pointer by up to value_size-1 out-of-bounds in order to leak kernel memory via side-channel to user space. Before this change, the computed ptr_limit for retrieve_ptr_limit() helper represents largest valid distance when moving pointer to the right or left which is then fed as aux->alu_limit to generate masking instructions against the offset register. After the change, the derived aux->alu_limit represents the largest potential value of the offset register which we mask against which is just a narrower subset of the former limit. For minimal complexity, we call sanitize_ptr_alu() from 2 observation points in adjust_ptr_min_max_vals(), that is, before and after the simulated alu operation. In the first step, we retieve the alu_state and alu_limit before the operation as well as we branch-off a verifier path and push it to the verification stack as we did before which checks the dst_reg under truncation, in other words, when the speculative domain would attempt to move the pointer out-of-bounds. In the second step, we retrieve the new alu_limit and calculate the absolute distance between both. Moreover, we commit the alu_state and final alu_limit via update_alu_sanitation_state() to the env's instruction aux data, and bail out from there if there is a mismatch due to coming from different verification paths with different states. Reported-by: Piotr Krysiuk <piotras@gmail.com> Reported-by: Benedict Schlueter <benedict.schlueter@rub.de> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Tested-by: Benedict Schlueter <benedict.schlueter@rub.de> [fllinden@amazon.com: backported to 5.4] Signed-off-by: Frank van der Linden <fllinden@amazon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4dc6e55 commit ef4e68f

1 file changed

Lines changed: 44 additions & 29 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,7 +4278,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
42784278
bool off_is_neg = off_reg->smin_value < 0;
42794279
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
42804280
(opcode == BPF_SUB && !off_is_neg);
4281-
u32 off, max = 0, ptr_limit = 0;
4281+
u32 max = 0, ptr_limit = 0;
42824282

42834283
if (!tnum_is_const(off_reg->var_off) &&
42844284
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
@@ -4287,26 +4287,18 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
42874287
switch (ptr_reg->type) {
42884288
case PTR_TO_STACK:
42894289
/* Offset 0 is out-of-bounds, but acceptable start for the
4290-
* left direction, see BPF_REG_FP.
4290+
* left direction, see BPF_REG_FP. Also, unknown scalar
4291+
* offset where we would need to deal with min/max bounds is
4292+
* currently prohibited for unprivileged.
42914293
*/
42924294
max = MAX_BPF_STACK + mask_to_left;
4293-
/* Indirect variable offset stack access is prohibited in
4294-
* unprivileged mode so it's not handled here.
4295-
*/
4296-
off = ptr_reg->off + ptr_reg->var_off.value;
4297-
if (mask_to_left)
4298-
ptr_limit = MAX_BPF_STACK + off;
4299-
else
4300-
ptr_limit = -off - 1;
4295+
ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off);
43014296
break;
43024297
case PTR_TO_MAP_VALUE:
43034298
max = ptr_reg->map_ptr->value_size;
4304-
if (mask_to_left) {
4305-
ptr_limit = ptr_reg->umax_value + ptr_reg->off;
4306-
} else {
4307-
off = ptr_reg->smin_value + ptr_reg->off;
4308-
ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
4309-
}
4299+
ptr_limit = (mask_to_left ?
4300+
ptr_reg->smin_value :
4301+
ptr_reg->umax_value) + ptr_reg->off;
43104302
break;
43114303
default:
43124304
return REASON_TYPE;
@@ -4361,10 +4353,12 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
43614353
struct bpf_insn *insn,
43624354
const struct bpf_reg_state *ptr_reg,
43634355
const struct bpf_reg_state *off_reg,
4364-
struct bpf_reg_state *dst_reg)
4356+
struct bpf_reg_state *dst_reg,
4357+
struct bpf_insn_aux_data *tmp_aux,
4358+
const bool commit_window)
43654359
{
4360+
struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : tmp_aux;
43664361
struct bpf_verifier_state *vstate = env->cur_state;
4367-
struct bpf_insn_aux_data *aux = cur_aux(env);
43684362
bool off_is_neg = off_reg->smin_value < 0;
43694363
bool ptr_is_dst_reg = ptr_reg == dst_reg;
43704364
u8 opcode = BPF_OP(insn->code);
@@ -4383,18 +4377,33 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
43834377
if (vstate->speculative)
43844378
goto do_sim;
43854379

4386-
alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
4387-
alu_state |= ptr_is_dst_reg ?
4388-
BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
4389-
43904380
err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode);
43914381
if (err < 0)
43924382
return err;
43934383

4384+
if (commit_window) {
4385+
/* In commit phase we narrow the masking window based on
4386+
* the observed pointer move after the simulated operation.
4387+
*/
4388+
alu_state = tmp_aux->alu_state;
4389+
alu_limit = abs(tmp_aux->alu_limit - alu_limit);
4390+
} else {
4391+
alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
4392+
alu_state |= ptr_is_dst_reg ?
4393+
BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
4394+
}
4395+
43944396
err = update_alu_sanitation_state(aux, alu_state, alu_limit);
43954397
if (err < 0)
43964398
return err;
43974399
do_sim:
4400+
/* If we're in commit phase, we're done here given we already
4401+
* pushed the truncated dst_reg into the speculative verification
4402+
* stack.
4403+
*/
4404+
if (commit_window)
4405+
return 0;
4406+
43984407
/* Simulate and find potential out-of-bounds access under
43994408
* speculative execution from truncation as a result of
44004409
* masking when off was not within expected range. If off
@@ -4506,6 +4515,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
45064515
smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
45074516
u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
45084517
umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
4518+
struct bpf_insn_aux_data tmp_aux = {};
45094519
u8 opcode = BPF_OP(insn->code);
45104520
u32 dst = insn->dst_reg;
45114521
int ret;
@@ -4564,12 +4574,15 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
45644574
!check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
45654575
return -EINVAL;
45664576

4567-
switch (opcode) {
4568-
case BPF_ADD:
4569-
ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg);
4577+
if (sanitize_needed(opcode)) {
4578+
ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg,
4579+
&tmp_aux, false);
45704580
if (ret < 0)
45714581
return sanitize_err(env, insn, ret, off_reg, dst_reg);
4582+
}
45724583

4584+
switch (opcode) {
4585+
case BPF_ADD:
45734586
/* We can take a fixed offset as long as it doesn't overflow
45744587
* the s32 'off' field
45754588
*/
@@ -4620,10 +4633,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
46204633
}
46214634
break;
46224635
case BPF_SUB:
4623-
ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg);
4624-
if (ret < 0)
4625-
return sanitize_err(env, insn, ret, off_reg, dst_reg);
4626-
46274636
if (dst_reg == off_reg) {
46284637
/* scalar -= pointer. Creates an unknown scalar */
46294638
verbose(env, "R%d tried to subtract pointer from scalar\n",
@@ -4706,6 +4715,12 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
47064715

47074716
if (sanitize_check_bounds(env, insn, dst_reg) < 0)
47084717
return -EACCES;
4718+
if (sanitize_needed(opcode)) {
4719+
ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg,
4720+
&tmp_aux, true);
4721+
if (ret < 0)
4722+
return sanitize_err(env, insn, ret, off_reg, dst_reg);
4723+
}
47094724

47104725
return 0;
47114726
}

0 commit comments

Comments
 (0)