Skip to content

Commit eed84f9

Browse files
borkmanngregkh
authored andcommitted
bpf: fix sanitation of alu op with pointer / scalar type from different paths
[ commit d3bd741 upstream ] While 979d63d ("bpf: prevent out of bounds speculation on pointer arithmetic") took care of rejecting alu op on pointer when e.g. pointer came from two different map values with different map properties such as value size, Jann reported that a case was not covered yet when a given alu op is used in both "ptr_reg += reg" and "numeric_reg += reg" from different branches where we would incorrectly try to sanitize based on the pointer's limit. Catch this corner case and reject the program instead. Fixes: 979d63d ("bpf: prevent out of bounds speculation on pointer arithmetic") Reported-by: Jann Horn <jannh@google.com> 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>
1 parent f92a819 commit eed84f9

2 files changed

Lines changed: 49 additions & 13 deletions

File tree

‎include/linux/bpf_verifier.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ struct bpf_verifier_state_list {
147147
#define BPF_ALU_SANITIZE_SRC 1U
148148
#define BPF_ALU_SANITIZE_DST 2U
149149
#define BPF_ALU_NEG_VALUE (1U << 2)
150+
#define BPF_ALU_NON_POINTER (1U << 3)
150151
#define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \
151152
BPF_ALU_SANITIZE_DST)
152153

‎kernel/bpf/verifier.c‎

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,40 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
27342734
}
27352735
}
27362736

2737+
static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
2738+
const struct bpf_insn *insn)
2739+
{
2740+
return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
2741+
}
2742+
2743+
static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
2744+
u32 alu_state, u32 alu_limit)
2745+
{
2746+
/* If we arrived here from different branches with different
2747+
* state or limits to sanitize, then this won't work.
2748+
*/
2749+
if (aux->alu_state &&
2750+
(aux->alu_state != alu_state ||
2751+
aux->alu_limit != alu_limit))
2752+
return -EACCES;
2753+
2754+
/* Corresponding fixup done in fixup_bpf_calls(). */
2755+
aux->alu_state = alu_state;
2756+
aux->alu_limit = alu_limit;
2757+
return 0;
2758+
}
2759+
2760+
static int sanitize_val_alu(struct bpf_verifier_env *env,
2761+
struct bpf_insn *insn)
2762+
{
2763+
struct bpf_insn_aux_data *aux = cur_aux(env);
2764+
2765+
if (can_skip_alu_sanitation(env, insn))
2766+
return 0;
2767+
2768+
return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
2769+
}
2770+
27372771
static int sanitize_ptr_alu(struct bpf_verifier_env *env,
27382772
struct bpf_insn *insn,
27392773
const struct bpf_reg_state *ptr_reg,
@@ -2748,7 +2782,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
27482782
struct bpf_reg_state tmp;
27492783
bool ret;
27502784

2751-
if (env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K)
2785+
if (can_skip_alu_sanitation(env, insn))
27522786
return 0;
27532787

27542788
/* We already marked aux for masking from non-speculative
@@ -2764,19 +2798,8 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
27642798

27652799
if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
27662800
return 0;
2767-
2768-
/* If we arrived here from different branches with different
2769-
* limits to sanitize, then this won't work.
2770-
*/
2771-
if (aux->alu_state &&
2772-
(aux->alu_state != alu_state ||
2773-
aux->alu_limit != alu_limit))
2801+
if (update_alu_sanitation_state(aux, alu_state, alu_limit))
27742802
return -EACCES;
2775-
2776-
/* Corresponding fixup done in fixup_bpf_calls(). */
2777-
aux->alu_state = alu_state;
2778-
aux->alu_limit = alu_limit;
2779-
27802803
do_sim:
27812804
/* Simulate and find potential out-of-bounds access under
27822805
* speculative execution from truncation as a result of
@@ -3048,6 +3071,8 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
30483071
s64 smin_val, smax_val;
30493072
u64 umin_val, umax_val;
30503073
u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
3074+
u32 dst = insn->dst_reg;
3075+
int ret;
30513076

30523077
if (insn_bitness == 32) {
30533078
/* Relevant for 32-bit RSH: Information can propagate towards
@@ -3082,6 +3107,11 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
30823107

30833108
switch (opcode) {
30843109
case BPF_ADD:
3110+
ret = sanitize_val_alu(env, insn);
3111+
if (ret < 0) {
3112+
verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
3113+
return ret;
3114+
}
30853115
if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
30863116
signed_add_overflows(dst_reg->smax_value, smax_val)) {
30873117
dst_reg->smin_value = S64_MIN;
@@ -3101,6 +3131,11 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
31013131
dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
31023132
break;
31033133
case BPF_SUB:
3134+
ret = sanitize_val_alu(env, insn);
3135+
if (ret < 0) {
3136+
verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
3137+
return ret;
3138+
}
31043139
if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
31053140
signed_sub_overflows(dst_reg->smax_value, smin_val)) {
31063141
/* Overflow possible, we know nothing */

0 commit comments

Comments
 (0)