Commit bbc1d247 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

bpf: Take into account BPF token when fetching helper protos

Instead of performing unconditional system-wide bpf_capable() and
perfmon_capable() calls inside bpf_base_func_proto() function (and other
similar ones) to determine eligibility of a given BPF helper for a given
program, use previously recorded BPF token during BPF_PROG_LOAD command
handling to inform the decision.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20240124022127.2379740-8-andrii@kernel.org
parent caf8f28e
...@@ -110,7 +110,7 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -110,7 +110,7 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_get_prandom_u32: case BPF_FUNC_get_prandom_u32:
return &bpf_get_prandom_u32_proto; return &bpf_get_prandom_u32_proto;
case BPF_FUNC_trace_printk: case BPF_FUNC_trace_printk:
if (perfmon_capable()) if (bpf_token_capable(prog->aux->token, CAP_PERFMON))
return bpf_get_trace_printk_proto(); return bpf_get_trace_printk_proto();
fallthrough; fallthrough;
default: default:
......
...@@ -2550,7 +2550,8 @@ int btf_find_next_decl_tag(const struct btf *btf, const struct btf_type *pt, ...@@ -2550,7 +2550,8 @@ int btf_find_next_decl_tag(const struct btf *btf, const struct btf_type *pt,
struct bpf_prog *bpf_prog_by_id(u32 id); struct bpf_prog *bpf_prog_by_id(u32 id);
struct bpf_link *bpf_link_by_id(u32 id); struct bpf_link *bpf_link_by_id(u32 id);
const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id); const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id,
const struct bpf_prog *prog);
void bpf_task_storage_free(struct task_struct *task); void bpf_task_storage_free(struct task_struct *task);
void bpf_cgrp_storage_free(struct cgroup *cgroup); void bpf_cgrp_storage_free(struct cgroup *cgroup);
bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog); bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
...@@ -2810,7 +2811,7 @@ static inline int btf_struct_access(struct bpf_verifier_log *log, ...@@ -2810,7 +2811,7 @@ static inline int btf_struct_access(struct bpf_verifier_log *log,
} }
static inline const struct bpf_func_proto * static inline const struct bpf_func_proto *
bpf_base_func_proto(enum bpf_func_id func_id) bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{ {
return NULL; return NULL;
} }
......
...@@ -1630,7 +1630,7 @@ cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -1630,7 +1630,7 @@ cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_perf_event_output: case BPF_FUNC_perf_event_output:
return &bpf_event_output_data_proto; return &bpf_event_output_data_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
} }
...@@ -2191,7 +2191,7 @@ sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -2191,7 +2191,7 @@ sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_perf_event_output: case BPF_FUNC_perf_event_output:
return &bpf_event_output_data_proto; return &bpf_event_output_data_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
} }
...@@ -2348,7 +2348,7 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -2348,7 +2348,7 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_perf_event_output: case BPF_FUNC_perf_event_output:
return &bpf_event_output_data_proto; return &bpf_event_output_data_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
} }
......
...@@ -1680,7 +1680,7 @@ const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak; ...@@ -1680,7 +1680,7 @@ const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
const struct bpf_func_proto bpf_task_pt_regs_proto __weak; const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
const struct bpf_func_proto * const struct bpf_func_proto *
bpf_base_func_proto(enum bpf_func_id func_id) bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{ {
switch (func_id) { switch (func_id) {
case BPF_FUNC_map_lookup_elem: case BPF_FUNC_map_lookup_elem:
...@@ -1731,7 +1731,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) ...@@ -1731,7 +1731,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)
break; break;
} }
if (!bpf_capable()) if (!bpf_token_capable(prog->aux->token, CAP_BPF))
return NULL; return NULL;
switch (func_id) { switch (func_id) {
...@@ -1789,7 +1789,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) ...@@ -1789,7 +1789,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)
break; break;
} }
if (!perfmon_capable()) if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
return NULL; return NULL;
switch (func_id) { switch (func_id) {
......
...@@ -5772,7 +5772,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = { ...@@ -5772,7 +5772,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = {
const struct bpf_func_proto * __weak const struct bpf_func_proto * __weak
tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{ {
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
BPF_CALL_1(bpf_sys_close, u32, fd) BPF_CALL_1(bpf_sys_close, u32, fd)
...@@ -5822,7 +5822,8 @@ syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -5822,7 +5822,8 @@ syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{ {
switch (func_id) { switch (func_id) {
case BPF_FUNC_sys_bpf: case BPF_FUNC_sys_bpf:
return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto; return !bpf_token_capable(prog->aux->token, CAP_PERFMON)
? NULL : &bpf_sys_bpf_proto;
case BPF_FUNC_btf_find_by_name_kind: case BPF_FUNC_btf_find_by_name_kind:
return &bpf_btf_find_by_name_kind_proto; return &bpf_btf_find_by_name_kind_proto;
case BPF_FUNC_sys_close: case BPF_FUNC_sys_close:
......
...@@ -1629,7 +1629,7 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -1629,7 +1629,7 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_trace_vprintk: case BPF_FUNC_trace_vprintk:
return bpf_get_trace_vprintk_proto(); return bpf_get_trace_vprintk_proto();
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
} }
......
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
#include "dev.h" #include "dev.h"
static const struct bpf_func_proto * static const struct bpf_func_proto *
bpf_sk_base_func_proto(enum bpf_func_id func_id); bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len) int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
{ {
...@@ -7862,7 +7862,7 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -7862,7 +7862,7 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_ktime_get_coarse_ns: case BPF_FUNC_ktime_get_coarse_ns:
return &bpf_ktime_get_coarse_ns_proto; return &bpf_ktime_get_coarse_ns_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
} }
...@@ -7955,7 +7955,7 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -7955,7 +7955,7 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return NULL; return NULL;
} }
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -7974,7 +7974,7 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -7974,7 +7974,7 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_perf_event_output: case BPF_FUNC_perf_event_output:
return &bpf_skb_event_output_proto; return &bpf_skb_event_output_proto;
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -8161,7 +8161,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -8161,7 +8161,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
#endif #endif
#endif #endif
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -8220,7 +8220,7 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -8220,7 +8220,7 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
#endif #endif
#endif #endif
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
#if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) #if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)
...@@ -8281,7 +8281,7 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -8281,7 +8281,7 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_tcp_sock_proto; return &bpf_tcp_sock_proto;
#endif /* CONFIG_INET */ #endif /* CONFIG_INET */
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -8323,7 +8323,7 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -8323,7 +8323,7 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_get_cgroup_classid_curr_proto; return &bpf_get_cgroup_classid_curr_proto;
#endif #endif
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -8367,7 +8367,7 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -8367,7 +8367,7 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_skc_lookup_tcp_proto; return &bpf_skc_lookup_tcp_proto;
#endif #endif
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -8378,7 +8378,7 @@ flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -8378,7 +8378,7 @@ flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_skb_load_bytes: case BPF_FUNC_skb_load_bytes:
return &bpf_flow_dissector_load_bytes_proto; return &bpf_flow_dissector_load_bytes_proto;
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -8405,7 +8405,7 @@ lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -8405,7 +8405,7 @@ lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_skb_under_cgroup: case BPF_FUNC_skb_under_cgroup:
return &bpf_skb_under_cgroup_proto; return &bpf_skb_under_cgroup_proto;
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -11236,7 +11236,7 @@ sk_reuseport_func_proto(enum bpf_func_id func_id, ...@@ -11236,7 +11236,7 @@ sk_reuseport_func_proto(enum bpf_func_id func_id,
case BPF_FUNC_ktime_get_coarse_ns: case BPF_FUNC_ktime_get_coarse_ns:
return &bpf_ktime_get_coarse_ns_proto; return &bpf_ktime_get_coarse_ns_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
} }
...@@ -11418,7 +11418,7 @@ sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -11418,7 +11418,7 @@ sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_sk_release: case BPF_FUNC_sk_release:
return &bpf_sk_release_proto; return &bpf_sk_release_proto;
default: default:
return bpf_sk_base_func_proto(func_id); return bpf_sk_base_func_proto(func_id, prog);
} }
} }
...@@ -11752,7 +11752,7 @@ const struct bpf_func_proto bpf_sock_from_file_proto = { ...@@ -11752,7 +11752,7 @@ const struct bpf_func_proto bpf_sock_from_file_proto = {
}; };
static const struct bpf_func_proto * static const struct bpf_func_proto *
bpf_sk_base_func_proto(enum bpf_func_id func_id) bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{ {
const struct bpf_func_proto *func; const struct bpf_func_proto *func;
...@@ -11781,10 +11781,10 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id) ...@@ -11781,10 +11781,10 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
case BPF_FUNC_ktime_get_coarse_ns: case BPF_FUNC_ktime_get_coarse_ns:
return &bpf_ktime_get_coarse_ns_proto; return &bpf_ktime_get_coarse_ns_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
if (!perfmon_capable()) if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
return NULL; return NULL;
return func; return func;
......
...@@ -197,7 +197,7 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id, ...@@ -197,7 +197,7 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
case BPF_FUNC_ktime_get_coarse_ns: case BPF_FUNC_ktime_get_coarse_ns:
return &bpf_ktime_get_coarse_ns_proto; return &bpf_ktime_get_coarse_ns_proto;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
} }
......
...@@ -314,7 +314,7 @@ static bool nf_is_valid_access(int off, int size, enum bpf_access_type type, ...@@ -314,7 +314,7 @@ static bool nf_is_valid_access(int off, int size, enum bpf_access_type type,
static const struct bpf_func_proto * static const struct bpf_func_proto *
bpf_nf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) bpf_nf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{ {
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id, prog);
} }
const struct bpf_verifier_ops netfilter_verifier_ops = { const struct bpf_verifier_ops netfilter_verifier_ops = {
......
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