Skip to content

Commit 199cdd0

Browse files
haoluo1022gregkh
authored andcommitted
bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM.
commit 34d3a78 upstream. Tag the return type of {per, this}_cpu_ptr with RDONLY_MEM. The returned value of this pair of helpers is kernel object, which can not be updated by bpf programs. Previously these two helpers return PTR_OT_MEM for kernel objects of scalar type, which allows one to directly modify the memory. Now with RDONLY_MEM tagging, the verifier will reject programs that write into RDONLY_MEM. Fixes: 63d9b80 ("bpf: Introducte bpf_this_cpu_ptr()") Fixes: eaa6bcb ("bpf: Introduce bpf_per_cpu_ptr()") Fixes: 4976b71 ("bpf: Introduce pseudo_btf_id") Signed-off-by: Hao Luo <haoluo@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20211217003152.48334-8-haoluo@google.com Cc: stable@vger.kernel.org # 5.16.x Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6f6edc4 commit 199cdd0

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

‎kernel/bpf/helpers.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
667667
const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
668668
.func = bpf_per_cpu_ptr,
669669
.gpl_only = false,
670-
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL,
670+
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY,
671671
.arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
672672
.arg2_type = ARG_ANYTHING,
673673
};
@@ -680,7 +680,7 @@ BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr)
680680
const struct bpf_func_proto bpf_this_cpu_ptr_proto = {
681681
.func = bpf_this_cpu_ptr,
682682
.gpl_only = false,
683-
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID,
683+
.ret_type = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY,
684684
.arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
685685
};
686686

‎kernel/bpf/verifier.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4333,15 +4333,30 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
43334333
mark_reg_unknown(env, regs, value_regno);
43344334
}
43354335
}
4336-
} else if (reg->type == PTR_TO_MEM) {
4336+
} else if (base_type(reg->type) == PTR_TO_MEM) {
4337+
bool rdonly_mem = type_is_rdonly_mem(reg->type);
4338+
4339+
if (type_may_be_null(reg->type)) {
4340+
verbose(env, "R%d invalid mem access '%s'\n", regno,
4341+
reg_type_str(env, reg->type));
4342+
return -EACCES;
4343+
}
4344+
4345+
if (t == BPF_WRITE && rdonly_mem) {
4346+
verbose(env, "R%d cannot write into %s\n",
4347+
regno, reg_type_str(env, reg->type));
4348+
return -EACCES;
4349+
}
4350+
43374351
if (t == BPF_WRITE && value_regno >= 0 &&
43384352
is_pointer_value(env, value_regno)) {
43394353
verbose(env, "R%d leaks addr into mem\n", value_regno);
43404354
return -EACCES;
43414355
}
4356+
43424357
err = check_mem_region_access(env, regno, off, size,
43434358
reg->mem_size, false);
4344-
if (!err && t == BPF_READ && value_regno >= 0)
4359+
if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem))
43454360
mark_reg_unknown(env, regs, value_regno);
43464361
} else if (reg->type == PTR_TO_CTX) {
43474362
enum bpf_reg_type reg_type = SCALAR_VALUE;
@@ -6550,6 +6565,13 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
65506565
regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
65516566
regs[BPF_REG_0].mem_size = tsize;
65526567
} else {
6568+
/* MEM_RDONLY may be carried from ret_flag, but it
6569+
* doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise
6570+
* it will confuse the check of PTR_TO_BTF_ID in
6571+
* check_mem_access().
6572+
*/
6573+
ret_flag &= ~MEM_RDONLY;
6574+
65536575
regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag;
65546576
regs[BPF_REG_0].btf = meta.ret_btf;
65556577
regs[BPF_REG_0].btf_id = meta.ret_btf_id;
@@ -9362,7 +9384,7 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
93629384

93639385
if (insn->src_reg == BPF_PSEUDO_BTF_ID) {
93649386
dst_reg->type = aux->btf_var.reg_type;
9365-
switch (dst_reg->type) {
9387+
switch (base_type(dst_reg->type)) {
93669388
case PTR_TO_MEM:
93679389
dst_reg->mem_size = aux->btf_var.mem_size;
93689390
break;
@@ -11505,7 +11527,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env,
1150511527
err = -EINVAL;
1150611528
goto err_put;
1150711529
}
11508-
aux->btf_var.reg_type = PTR_TO_MEM;
11530+
aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY;
1150911531
aux->btf_var.mem_size = tsize;
1151011532
} else {
1151111533
aux->btf_var.reg_type = PTR_TO_BTF_ID;

0 commit comments

Comments
 (0)