Skip to content

Commit 3bfb49d

Browse files
melverAlexei Starovoitov
authored andcommitted
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 <[email protected]> Signed-off-by: Marco Elver <[email protected]> Acked-by: Jiri Olsa <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b28573e commit 3bfb49d

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

@@ -1441,6 +1433,8 @@ late_initcall(bpf_key_sig_kfuncs_init);
14411433
static const struct bpf_func_proto *
14421434
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14431435
{
1436+
const struct bpf_func_proto *func_proto;
1437+
14441438
switch (func_id) {
14451439
case BPF_FUNC_map_lookup_elem:
14461440
return &bpf_map_lookup_elem_proto;
@@ -1482,9 +1476,6 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14821476
return &bpf_perf_event_read_proto;
14831477
case BPF_FUNC_get_prandom_u32:
14841478
return &bpf_get_prandom_u32_proto;
1485-
case BPF_FUNC_probe_write_user:
1486-
return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1487-
NULL : bpf_get_probe_write_proto();
14881479
case BPF_FUNC_probe_read_user:
14891480
return &bpf_probe_read_user_proto;
14901481
case BPF_FUNC_probe_read_kernel:
@@ -1563,7 +1554,22 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15631554
case BPF_FUNC_trace_vprintk:
15641555
return bpf_get_trace_vprintk_proto();
15651556
default:
1566-
return bpf_base_func_proto(func_id, prog);
1557+
break;
1558+
}
1559+
1560+
func_proto = bpf_base_func_proto(func_id, prog);
1561+
if (func_proto)
1562+
return func_proto;
1563+
1564+
if (!bpf_token_capable(prog->aux->token, CAP_SYS_ADMIN))
1565+
return NULL;
1566+
1567+
switch (func_id) {
1568+
case BPF_FUNC_probe_write_user:
1569+
return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1570+
NULL : &bpf_probe_write_user_proto;
1571+
default:
1572+
return NULL;
15671573
}
15681574
}
15691575

0 commit comments

Comments
 (0)