From: Alexei Starovoitov 'rX = addr_space_cast(rY, 0, 1)' makes a pointer to arena from an address in arena. The verifier gives rX the type PTR_TO_ARENA and replaces the insn with 'wX = wY', which keeps the low 32 bits. The JIT adds the base of the arena in each access. Thus an access through PTR_TO_ARENA cannot leave the arena, and the verifier does not check it. In C the __arena attribute marks the pointers to arena, and clang casts only these pointers. Rust has no address spaces. The compiler follows each pointer back to its origin, and it does not cast a pointer that comes from the kernel. But it cannot always find the origin. For example, two progs of an object call the same function. One prog passes a ring buffer record, and the other prog passes an object in arena: fn fill(e: &mut Event) r6 = addr_space_cast(r1, 0, 1) ; a ring buffer record, or a number *(u64 *)(r6 + 0) = r2 When the compiler does not know the origin, it casts the pointer. The verifier does not examine the type of r1. r6 becomes PTR_TO_ARENA in the two progs. The first prog loads, and it writes to arena instead of the record. The verifier knows the type of rY. Let the type select what the cast does: rY the cast becomes rX ----------------------------- -------------------- ------------ number (scalar), PTR_TO_ARENA wX = wY, as before PTR_TO_ARENA all other pointers rX = rY, or nothing a copy of rY when X == Y All other pointers are kernel pointers for the verifier, trusted or not. A kernel pointer is in address space 0 already. Thus a move is the correct cast. rX is a copy of rY. The verifier applies the same checks to the two registers: bounds, NULL, references. The verifier replaces the cast one time, for all paths. Thus it rejects a prog when it finds a number at a cast on one path and a kernel pointer at the same cast on a different path: same addr_space_cast insn cannot be used with a number and with a pointer NULL is a number too. The analysis of live stack slots follows a pointer to the stack through a move. Make arg_track_xfer() follow it through the cast too. Otherwise the analysis does not see a read of the stack through rX, and the verifier forgets a slot that the prog reads. This analysis runs before the verifier knows the type of rY, and it can take a number for a pointer to the stack. Then rX points to arena. Thus rX is "the slot of rY, or not a pointer" for the analysis: an access through rX can read or write that slot, and it is never a certain write. The cast continues to be one insn at most. A prog in C gets different code only where the verifier finds a kernel pointer at a cast. The kernel marks the pointers to arena in its structs with btf_type_tag("arena"). gcc does not emit the tag. Then such a member is a kernel pointer for the verifier, and the cast is a move now. The members 'u64 __arena *' and 'void __arena *' of an object from bpf_obj_new() are rdonly_untrusted_mem for the verifier, and the cast is a move too. Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 3 ++ kernel/bpf/fixups.c | 20 +++++++++++++ kernel/bpf/liveness.c | 15 ++++++++-- kernel/bpf/verifier.c | 54 ++++++++++++++++++++++++++++++++++-- 4 files changed, 86 insertions(+), 6 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 4a514f27f239..9ae6e109d211 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -795,6 +795,8 @@ struct bpf_insn_aux_data { u64 non_sleepable:1; /* helper/kfunc may be called from non-sleepable context */ u64 is_iter_next:1; /* bpf_iter__next() kfunc call */ u64 call_with_percpu_alloc_ptr:1; /* {this,per}_cpu_ptr() with prog percpu alloc */ + u64 cast_arena:1; /* addr_space_cast of an address in arena */ + u64 cast_mov:1; /* addr_space_cast of a pointer that is not in arena, it's a move */ u64 bb_end:1; /* last instruction of a basic block */ u64 need_scev:1; u64 alu_state:5; /* used in combination with alu_limit */ @@ -2089,6 +2091,7 @@ int bpf_optimize_bpf_loop(struct bpf_verifier_env *env); void bpf_opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env); int bpf_opt_remove_dead_code(struct bpf_verifier_env *env); int bpf_opt_remove_nops(struct bpf_verifier_env *env); +void bpf_lower_cast_mov(struct bpf_verifier_env *env); int bpf_opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, const union bpf_attr *attr); int bpf_convert_ctx_accesses(struct bpf_verifier_env *env); int bpf_jit_subprogs(struct bpf_verifier_env *env); diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 6b719a28be68..eb5756cbc7e5 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -709,6 +709,26 @@ static bool insn_is_nop(struct bpf_verifier_env *env, int i, int next) !memcmp(&insn, &MAY_GOTO_0, sizeof(MAY_GOTO_0)); } +/* addr_space_cast of a pointer that is not in arena is a move, see is_cast_mov() */ +void bpf_lower_cast_mov(struct bpf_verifier_env *env) +{ + struct bpf_insn_aux_data *aux = env->insn_aux_data; + struct bpf_insn *insn = env->prog->insnsi; + int i; + + for (i = 0; i < env->prog->len; i++, insn++) { + if (!aux[i].cast_mov) + continue; + /* an address in arena has 32 bits, this pointer has 64 */ + aux[i].zext_dst = false; + if (insn->dst_reg == insn->src_reg) + /* bpf_opt_remove_nops() removes it */ + *insn = NOP; + else + *insn = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); + } +} + int bpf_opt_remove_nops(struct bpf_verifier_env *env) { int insn_cnt = env->prog->len; diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c index f51445ead384..1a4cfb575077 100644 --- a/kernel/bpf/liveness.c +++ b/kernel/bpf/liveness.c @@ -1456,8 +1456,16 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn, if (code == BPF_MOV) { if (insn->off == 0) { *dst = *src; + } else if (insn->off == BPF_ADDR_SPACE_CAST && insn->imm == 1) { + /* + * The cast of a pointer to stack to as(0) is a move. But src + * can be a stale spill, see below. Then it's a number for the + * verifier, and dst points to arena. A store through dst must + * not count as a certain store to a slot of the stack. + */ + *dst = __arg_track_join(*src, none); } else { - /* addr_space_cast destroys a pointer */ + /* the cast to as(1) and sign extension destroy a pointer */ *dst = none; } } else { @@ -1474,8 +1482,9 @@ static void arg_track_xfer(struct bpf_verifier_env *env, struct bpf_insn *insn, * Helpers, kfuncs and global subprogs can scratch stack slots. The verifier reads * a scalar from a scratched slot, but at_stack_out[slot] is not cleared, and * fill_from_stack() returns the stale spill: an FP-derived arg_track for a scalar. - * A scalar can't be dereferenced, so the stale value can only set liveness bits - * for stack accesses that don't happen. That is conservative. + * A scalar can't be dereferenced before addr_space_cast, see above. So the stale + * value can only set liveness bits for stack accesses that don't happen. That is + * conservative. * * Static subprogs and callbacks can't store an FP-derived pointer into the frame of * the caller, but they can store any other pointer there. The caller can fill it diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 62f26149c3ae..488ee53561be 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17894,6 +17894,51 @@ int bpf_reg_union(struct bpf_verifier_env *env, struct bpf_reg_state *acc, return reg_bounds_sanity_check(env, acc, "bpf_reg_union"); } +/* + * There are no address spaces in Rust. The compiler doesn't cast a pointer that + * comes from the kernel. But it can't always tell, and then it casts the pointer + * to as(0) before the access, like an address in arena. + * A pointer that the verifier knows is in as(0) already, so the cast is a move. + * arg_track_xfer() does the same for pointers to stack. + */ +static bool is_cast_mov(const struct bpf_insn *insn, const struct bpf_reg_state *src_reg) +{ + /* cast from as(1) to as(0) */ + if (insn->imm != 1) + return false; + + switch (base_type(src_reg->type)) { + case SCALAR_VALUE: + case PTR_TO_ARENA: + return false; + default: + return true; + } +} + +static int check_addr_space_cast(struct bpf_verifier_env *env, struct bpf_insn *insn, + struct bpf_reg_state *dst_reg, struct bpf_reg_state *src_reg) +{ + struct bpf_insn_aux_data *aux = cur_aux(env); + + if (is_cast_mov(insn, src_reg)) { + aux->cast_mov = true; + *dst_reg = *src_reg; + } else { + aux->cast_arena = true; + mark_reg_unknown(env, cur_regs(env), insn->dst_reg); + if (insn->imm == 1) /* cast from as(1) to as(0) */ + dst_reg->type = PTR_TO_ARENA; + } + + /* it's either wX = wY or rX = rY, see bpf_lower_cast_mov() */ + if (aux->cast_mov && aux->cast_arena) { + verbose(env, "same addr_space_cast insn cannot be used with a number and with a pointer\n"); + return -EINVAL; + } + return 0; +} + /* check validity of 32-bit and 64-bit arithmetic operations */ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) { @@ -17955,9 +18000,9 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) if (BPF_CLASS(insn->code) == BPF_ALU64) { if (insn->imm) { /* off == BPF_ADDR_SPACE_CAST */ - mark_reg_unknown(env, regs, insn->dst_reg); - if (insn->imm == 1) /* cast from as(1) to as(0) */ - dst_reg->type = PTR_TO_ARENA; + err = check_addr_space_cast(env, insn, dst_reg, src_reg); + if (err) + return err; } else if (insn->off == 0) { /* case: R1 = R2 * copy register state to dest reg @@ -24231,6 +24276,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, if (ret == 0) ret = bpf_optimize_bpf_loop(env); + if (ret == 0) + bpf_lower_cast_mov(env); + if (is_priv) { if (ret == 0) bpf_opt_hard_wire_dead_code_branches(env); -- 2.55.0