Commit 1411707a authored by Thomas Graf's avatar Thomas Graf Committed by Greg Kroah-Hartman

bpf: Detect identical PTR_TO_MAP_VALUE_OR_NULL registers

[ Upstream commit 57a09bf0 ]

A BPF program is required to check the return register of a
map_elem_lookup() call before accessing memory. The verifier keeps
track of this by converting the type of the result register from
PTR_TO_MAP_VALUE_OR_NULL to PTR_TO_MAP_VALUE after a conditional
jump ensures safety. This check is currently exclusively performed
for the result register 0.

In the event the compiler reorders instructions, BPF_MOV64_REG
instructions may be moved before the conditional jump which causes
them to keep their type PTR_TO_MAP_VALUE_OR_NULL to which the
verifier objects when the register is accessed:

0: (b7) r1 = 10
1: (7b) *(u64 *)(r10 -8) = r1
2: (bf) r2 = r10
3: (07) r2 += -8
4: (18) r1 = 0x59c00000
6: (85) call 1
7: (bf) r4 = r0
8: (15) if r0 == 0x0 goto pc+1
 R0=map_value(ks=8,vs=8) R4=map_value_or_null(ks=8,vs=8) R10=fp
9: (7a) *(u64 *)(r4 +0) = 0
R4 invalid mem access 'map_value_or_null'

This commit extends the verifier to keep track of all identical
PTR_TO_MAP_VALUE_OR_NULL registers after a map_elem_lookup() by
assigning them an ID and then marking them all when the conditional
jump is observed.
Signed-off-by: default avatarThomas Graf <tgraf@suug.ch>
Reviewed-by: default avatarJosef Bacik <jbacik@fb.com>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 9e38375a
...@@ -24,13 +24,13 @@ struct bpf_reg_state { ...@@ -24,13 +24,13 @@ struct bpf_reg_state {
*/ */
s64 min_value; s64 min_value;
u64 max_value; u64 max_value;
u32 id;
union { union {
/* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */ /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */
s64 imm; s64 imm;
/* valid when type == PTR_TO_PACKET* */ /* valid when type == PTR_TO_PACKET* */
struct { struct {
u32 id;
u16 off; u16 off;
u16 range; u16 range;
}; };
......
...@@ -212,9 +212,10 @@ static void print_verifier_state(struct bpf_verifier_state *state) ...@@ -212,9 +212,10 @@ static void print_verifier_state(struct bpf_verifier_state *state)
else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
t == PTR_TO_MAP_VALUE_OR_NULL || t == PTR_TO_MAP_VALUE_OR_NULL ||
t == PTR_TO_MAP_VALUE_ADJ) t == PTR_TO_MAP_VALUE_ADJ)
verbose("(ks=%d,vs=%d)", verbose("(ks=%d,vs=%d,id=%u)",
reg->map_ptr->key_size, reg->map_ptr->key_size,
reg->map_ptr->value_size); reg->map_ptr->value_size,
reg->id);
if (reg->min_value != BPF_REGISTER_MIN_RANGE) if (reg->min_value != BPF_REGISTER_MIN_RANGE)
verbose(",min_value=%lld", verbose(",min_value=%lld",
(long long)reg->min_value); (long long)reg->min_value);
...@@ -447,6 +448,7 @@ static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) ...@@ -447,6 +448,7 @@ static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno)
{ {
BUG_ON(regno >= MAX_BPF_REG); BUG_ON(regno >= MAX_BPF_REG);
regs[regno].type = UNKNOWN_VALUE; regs[regno].type = UNKNOWN_VALUE;
regs[regno].id = 0;
regs[regno].imm = 0; regs[regno].imm = 0;
} }
...@@ -1252,6 +1254,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id) ...@@ -1252,6 +1254,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id)
return -EINVAL; return -EINVAL;
} }
regs[BPF_REG_0].map_ptr = meta.map_ptr; regs[BPF_REG_0].map_ptr = meta.map_ptr;
regs[BPF_REG_0].id = ++env->id_gen;
} else { } else {
verbose("unknown return type %d of func %d\n", verbose("unknown return type %d of func %d\n",
fn->ret_type, func_id); fn->ret_type, func_id);
...@@ -1668,8 +1671,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) ...@@ -1668,8 +1671,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
insn->src_reg); insn->src_reg);
return -EACCES; return -EACCES;
} }
regs[insn->dst_reg].type = UNKNOWN_VALUE; mark_reg_unknown_value(regs, insn->dst_reg);
regs[insn->dst_reg].map_ptr = NULL;
} }
} else { } else {
/* case: R = imm /* case: R = imm
...@@ -1931,6 +1933,38 @@ static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, ...@@ -1931,6 +1933,38 @@ static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
check_reg_overflow(true_reg); check_reg_overflow(true_reg);
} }
static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id,
enum bpf_reg_type type)
{
struct bpf_reg_state *reg = &regs[regno];
if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) {
reg->type = type;
if (type == UNKNOWN_VALUE)
mark_reg_unknown_value(regs, regno);
}
}
/* The logic is similar to find_good_pkt_pointers(), both could eventually
* be folded together at some point.
*/
static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
enum bpf_reg_type type)
{
struct bpf_reg_state *regs = state->regs;
int i;
for (i = 0; i < MAX_BPF_REG; i++)
mark_map_reg(regs, i, regs[regno].id, type);
for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
if (state->stack_slot_type[i] != STACK_SPILL)
continue;
mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE,
regs[regno].id, type);
}
}
static int check_cond_jmp_op(struct bpf_verifier_env *env, static int check_cond_jmp_op(struct bpf_verifier_env *env,
struct bpf_insn *insn, int *insn_idx) struct bpf_insn *insn, int *insn_idx)
{ {
...@@ -2018,18 +2052,13 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, ...@@ -2018,18 +2052,13 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
if (BPF_SRC(insn->code) == BPF_K && if (BPF_SRC(insn->code) == BPF_K &&
insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
if (opcode == BPF_JEQ) { /* Mark all identical map registers in each branch as either
/* next fallthrough insn can access memory via * safe or unknown depending R == 0 or R != 0 conditional.
* this register */
*/ mark_map_regs(this_branch, insn->dst_reg,
regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE);
/* branch targer cannot access it, since reg == 0 */ mark_map_regs(other_branch, insn->dst_reg,
mark_reg_unknown_value(other_branch->regs, opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE);
insn->dst_reg);
} else {
other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
mark_reg_unknown_value(regs, insn->dst_reg);
}
} else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
dst_reg->type == PTR_TO_PACKET && dst_reg->type == PTR_TO_PACKET &&
regs[insn->src_reg].type == PTR_TO_PACKET_END) { regs[insn->src_reg].type == PTR_TO_PACKET_END) {
......
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