Skip to content

Commit f470378

Browse files
jrfastabAlexei Starovoitov
authored andcommitted
bpf: Extend bpf_base_func_proto helpers with probe_* and *current_task*
Often it is useful when applying policy to know something about the task. If the administrator has CAP_SYS_ADMIN rights then they can use kprobe + networking hook and link the two programs together to accomplish this. However, this is a bit clunky and also means we have to call both the network program and kprobe program when we could just use a single program and avoid passing metadata through sk_msg/skb->cb, socket, maps, etc. To accomplish this add probe_* helpers to bpf_base_func_proto programs guarded by a perfmon_capable() check. New supported helpers are the following, BPF_FUNC_get_current_task BPF_FUNC_probe_read_user BPF_FUNC_probe_read_kernel BPF_FUNC_probe_read_user_str BPF_FUNC_probe_read_kernel_str Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/159033905529.12355.4368381069655254932.stgit@john-Precision-5820-Tower Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent abe3cac commit f470378

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

kernel/bpf/helpers.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ const struct bpf_func_proto bpf_event_output_data_proto = {
601601
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
602602
};
603603

604+
const struct bpf_func_proto bpf_get_current_task_proto __weak;
605+
const struct bpf_func_proto bpf_probe_read_user_proto __weak;
606+
const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
607+
const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
608+
const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
609+
604610
const struct bpf_func_proto *
605611
bpf_base_func_proto(enum bpf_func_id func_id)
606612
{
@@ -647,6 +653,24 @@ bpf_base_func_proto(enum bpf_func_id func_id)
647653
return bpf_get_trace_printk_proto();
648654
case BPF_FUNC_jiffies64:
649655
return &bpf_jiffies64_proto;
656+
default:
657+
break;
658+
}
659+
660+
if (!perfmon_capable())
661+
return NULL;
662+
663+
switch (func_id) {
664+
case BPF_FUNC_get_current_task:
665+
return &bpf_get_current_task_proto;
666+
case BPF_FUNC_probe_read_user:
667+
return &bpf_probe_read_user_proto;
668+
case BPF_FUNC_probe_read_kernel:
669+
return &bpf_probe_read_kernel_proto;
670+
case BPF_FUNC_probe_read_user_str:
671+
return &bpf_probe_read_user_str_proto;
672+
case BPF_FUNC_probe_read_kernel_str:
673+
return &bpf_probe_read_kernel_str_proto;
650674
default:
651675
return NULL;
652676
}

kernel/trace/bpf_trace.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
147147
return ret;
148148
}
149149

150-
static const struct bpf_func_proto bpf_probe_read_user_proto = {
150+
const struct bpf_func_proto bpf_probe_read_user_proto = {
151151
.func = bpf_probe_read_user,
152152
.gpl_only = true,
153153
.ret_type = RET_INTEGER,
@@ -167,7 +167,7 @@ BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
167167
return ret;
168168
}
169169

170-
static const struct bpf_func_proto bpf_probe_read_user_str_proto = {
170+
const struct bpf_func_proto bpf_probe_read_user_str_proto = {
171171
.func = bpf_probe_read_user_str,
172172
.gpl_only = true,
173173
.ret_type = RET_INTEGER,
@@ -198,7 +198,7 @@ BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
198198
return bpf_probe_read_kernel_common(dst, size, unsafe_ptr, false);
199199
}
200200

201-
static const struct bpf_func_proto bpf_probe_read_kernel_proto = {
201+
const struct bpf_func_proto bpf_probe_read_kernel_proto = {
202202
.func = bpf_probe_read_kernel,
203203
.gpl_only = true,
204204
.ret_type = RET_INTEGER,
@@ -253,7 +253,7 @@ BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
253253
return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr, false);
254254
}
255255

256-
static const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
256+
const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
257257
.func = bpf_probe_read_kernel_str,
258258
.gpl_only = true,
259259
.ret_type = RET_INTEGER,
@@ -907,7 +907,7 @@ BPF_CALL_0(bpf_get_current_task)
907907
return (long) current;
908908
}
909909

910-
static const struct bpf_func_proto bpf_get_current_task_proto = {
910+
const struct bpf_func_proto bpf_get_current_task_proto = {
911911
.func = bpf_get_current_task,
912912
.gpl_only = true,
913913
.ret_type = RET_INTEGER,

0 commit comments

Comments
 (0)