Skip to content

Commit ce1afb5

Browse files
bpf: Refactor bpf_tracing_func_proto() and remove bpf_get_probe_write_proto()
JIRA: https://issues.redhat.com/browse/RHEL-78201 commit 3bfb49d Author: Marco Elver <elver@google.com> Date: Fri Nov 29 09:59:34 2024 +0100 bpf: Refactor bpf_tracing_func_proto() and remove bpf_get_probe_write_proto() With bpf_get_probe_write_proto() no longer printing a message, we can avoid it being a special case with its own permission check. Refactor bpf_tracing_func_proto() similar to bpf_base_func_proto() to have a section conditional on bpf_token_capable(CAP_SYS_ADMIN), where the proto for bpf_probe_write_user() is returned. Finally, remove the unnecessary bpf_get_probe_write_proto(). This simplifies the code, and adding additional CAP_SYS_ADMIN-only helpers in future avoids duplicating the same CAP_SYS_ADMIN check. Suggested-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Marco Elver <elver@google.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20241129090040.2690691-2-elver@google.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
1 parent 152baa4 commit ce1afb5

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

kernel/trace/bpf_trace.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,6 @@ static const struct bpf_func_proto bpf_probe_write_user_proto = {
357357
.arg3_type = ARG_CONST_SIZE,
358358
};
359359

360-
static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
361-
{
362-
if (!capable(CAP_SYS_ADMIN))
363-
return NULL;
364-
365-
return &bpf_probe_write_user_proto;
366-
}
367-
368360
#define MAX_TRACE_PRINTK_VARARGS 3
369361
#define BPF_TRACE_PRINTK_SIZE 1024
370362

@@ -1442,6 +1434,8 @@ late_initcall(bpf_key_sig_kfuncs_init);
14421434
static const struct bpf_func_proto *
14431435
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14441436
{
1437+
const struct bpf_func_proto *func_proto;
1438+
14451439
switch (func_id) {
14461440
case BPF_FUNC_map_lookup_elem:
14471441
return &bpf_map_lookup_elem_proto;
@@ -1483,9 +1477,6 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14831477
return &bpf_perf_event_read_proto;
14841478
case BPF_FUNC_get_prandom_u32:
14851479
return &bpf_get_prandom_u32_proto;
1486-
case BPF_FUNC_probe_write_user:
1487-
return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1488-
NULL : bpf_get_probe_write_proto();
14891480
case BPF_FUNC_probe_read_user:
14901481
return &bpf_probe_read_user_proto;
14911482
case BPF_FUNC_probe_read_kernel:
@@ -1564,7 +1555,22 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15641555
case BPF_FUNC_trace_vprintk:
15651556
return bpf_get_trace_vprintk_proto();
15661557
default:
1567-
return bpf_base_func_proto(func_id, prog);
1558+
break;
1559+
}
1560+
1561+
func_proto = bpf_base_func_proto(func_id, prog);
1562+
if (func_proto)
1563+
return func_proto;
1564+
1565+
if (!bpf_token_capable(prog->aux->token, CAP_SYS_ADMIN))
1566+
return NULL;
1567+
1568+
switch (func_id) {
1569+
case BPF_FUNC_probe_write_user:
1570+
return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1571+
NULL : &bpf_probe_write_user_proto;
1572+
default:
1573+
return NULL;
15681574
}
15691575
}
15701576

0 commit comments

Comments
 (0)