Skip to content

Commit 7fedb63

Browse files
committed
bpf: Tighten speculative pointer arithmetic mask
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 <[email protected]> Reported-by: Benedict Schlueter <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Tested-by: Benedict Schlueter <[email protected]>
1 parent f528819 commit 7fedb63

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

kernel/bpf/verifier.c

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5871,7 +5871,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
58715871
bool off_is_neg = off_reg->smin_value < 0;
58725872
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
58735873
(opcode == BPF_SUB && !off_is_neg);
5874-
u32 off, max = 0, ptr_limit = 0;
5874+
u32 max = 0, ptr_limit = 0;
58755875

58765876
if (!tnum_is_const(off_reg->var_off) &&
58775877
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
@@ -5880,26 +5880,18 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
58805880
switch (ptr_reg->type) {
58815881
case PTR_TO_STACK:
58825882
/* Offset 0 is out-of-bounds, but acceptable start for the
5883-
* left direction, see BPF_REG_FP.
5883+
* left direction, see BPF_REG_FP. Also, unknown scalar
5884+
* offset where we would need to deal with min/max bounds is
5885+
* currently prohibited for unprivileged.
58845886
*/
58855887
max = MAX_BPF_STACK + mask_to_left;
5886-
/* Indirect variable offset stack access is prohibited in
5887-
* unprivileged mode so it's not handled here.
5888-
*/
5889-
off = ptr_reg->off + ptr_reg->var_off.value;
5890-
if (mask_to_left)
5891-
ptr_limit = MAX_BPF_STACK + off;
5892-
else
5893-
ptr_limit = -off - 1;
5888+
ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off);
58945889
break;
58955890
case PTR_TO_MAP_VALUE:
58965891
max = ptr_reg->map_ptr->value_size;
5897-
if (mask_to_left) {
5898-
ptr_limit = ptr_reg->umax_value + ptr_reg->off;
5899-
} else {
5900-
off = ptr_reg->smin_value + ptr_reg->off;
5901-
ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
5902-
}
5892+
ptr_limit = (mask_to_left ?
5893+
ptr_reg->smin_value :
5894+
ptr_reg->umax_value) + ptr_reg->off;
59035895
break;
59045896
default:
59055897
return REASON_TYPE;
@@ -5954,10 +5946,12 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
59545946
struct bpf_insn *insn,
59555947
const struct bpf_reg_state *ptr_reg,
59565948
const struct bpf_reg_state *off_reg,
5957-
struct bpf_reg_state *dst_reg)
5949+
struct bpf_reg_state *dst_reg,
5950+
struct bpf_insn_aux_data *tmp_aux,
5951+
const bool commit_window)
59585952
{
5953+
struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : tmp_aux;
59595954
struct bpf_verifier_state *vstate = env->cur_state;
5960-
struct bpf_insn_aux_data *aux = cur_aux(env);
59615955
bool off_is_neg = off_reg->smin_value < 0;
59625956
bool ptr_is_dst_reg = ptr_reg == dst_reg;
59635957
u8 opcode = BPF_OP(insn->code);
@@ -5976,18 +5970,33 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
59765970
if (vstate->speculative)
59775971
goto do_sim;
59785972

5979-
alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
5980-
alu_state |= ptr_is_dst_reg ?
5981-
BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
5982-
59835973
err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode);
59845974
if (err < 0)
59855975
return err;
59865976

5977+
if (commit_window) {
5978+
/* In commit phase we narrow the masking window based on
5979+
* the observed pointer move after the simulated operation.
5980+
*/
5981+
alu_state = tmp_aux->alu_state;
5982+
alu_limit = abs(tmp_aux->alu_limit - alu_limit);
5983+
} else {
5984+
alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
5985+
alu_state |= ptr_is_dst_reg ?
5986+
BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
5987+
}
5988+
59875989
err = update_alu_sanitation_state(aux, alu_state, alu_limit);
59885990
if (err < 0)
59895991
return err;
59905992
do_sim:
5993+
/* If we're in commit phase, we're done here given we already
5994+
* pushed the truncated dst_reg into the speculative verification
5995+
* stack.
5996+
*/
5997+
if (commit_window)
5998+
return 0;
5999+
59916000
/* Simulate and find potential out-of-bounds access under
59926001
* speculative execution from truncation as a result of
59936002
* masking when off was not within expected range. If off
@@ -6130,6 +6139,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
61306139
smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
61316140
u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
61326141
umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
6142+
struct bpf_insn_aux_data tmp_aux = {};
61336143
u8 opcode = BPF_OP(insn->code);
61346144
u32 dst = insn->dst_reg;
61356145
int ret;
@@ -6196,12 +6206,15 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
61966206
/* pointer types do not carry 32-bit bounds at the moment. */
61976207
__mark_reg32_unbounded(dst_reg);
61986208

6199-
switch (opcode) {
6200-
case BPF_ADD:
6201-
ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg);
6209+
if (sanitize_needed(opcode)) {
6210+
ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg,
6211+
&tmp_aux, false);
62026212
if (ret < 0)
62036213
return sanitize_err(env, insn, ret, off_reg, dst_reg);
6214+
}
62046215

6216+
switch (opcode) {
6217+
case BPF_ADD:
62056218
/* We can take a fixed offset as long as it doesn't overflow
62066219
* the s32 'off' field
62076220
*/
@@ -6252,10 +6265,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
62526265
}
62536266
break;
62546267
case BPF_SUB:
6255-
ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg);
6256-
if (ret < 0)
6257-
return sanitize_err(env, insn, ret, off_reg, dst_reg);
6258-
62596268
if (dst_reg == off_reg) {
62606269
/* scalar -= pointer. Creates an unknown scalar */
62616270
verbose(env, "R%d tried to subtract pointer from scalar\n",
@@ -6338,6 +6347,12 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
63386347

63396348
if (sanitize_check_bounds(env, insn, dst_reg) < 0)
63406349
return -EACCES;
6350+
if (sanitize_needed(opcode)) {
6351+
ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg,
6352+
&tmp_aux, true);
6353+
if (ret < 0)
6354+
return sanitize_err(env, insn, ret, off_reg, dst_reg);
6355+
}
63416356

63426357
return 0;
63436358
}

0 commit comments

Comments
 (0)