From: YiFei Zhu <yifeifz2@xxxxxxxxxxxx> Add a verifier hook that is able to reject direct map access that does not make use of eBPF helpers. These accesses mostly correspond to eBPF data section accesses. This allows a program type to disable maps altogether by resturing direct map accesses and not whitelisting helpers that perform map accesses. Signed-off-by: YiFei Zhu <yifeifz2@xxxxxxxxxxxx> --- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 02b02cb29ce2..86f3e8784e43 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -484,6 +484,7 @@ struct bpf_verifier_ops { enum bpf_access_type atype, u32 *next_btf_id); bool (*check_kfunc_call)(u32 kfunc_btf_id); + bool (*map_access)(enum bpf_access_type type); }; struct bpf_prog_offload_ops { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 8fd552c16763..8eec1796caaa 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3100,6 +3100,9 @@ static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, struct bpf_map *map = regs[regno].map_ptr; u32 cap = bpf_map_flags_to_cap(map); + if (env->ops->map_access && !env->ops->map_access(type)) + cap = 0; + if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", map->value_size, off, size); -- 2.31.1