Skip to content

Commit 3da4f52

Browse files
Yonghong SongKernel Patches Daemon
authored andcommitted
bpf: Allow helper bpf_get_[ns_]current_pid_tgid() for all prog types
Currently bpf_get_current_pid_tgid() is allowed in tracing, cgroup and sk_msg progs while bpf_get_ns_current_pid_tgid() is only allowed in tracing progs. We have an internal use case where for an application running in a container (with pid namespace), user wants to get the pid associated with the pid namespace in a cgroup bpf program. Currently, cgroup bpf progs already allow bpf_get_current_pid_tgid(). Let us allow bpf_get_ns_current_pid_tgid() as well. With auditing the code, bpf_get_current_pid_tgid() is also used by sk_msg prog. But there are no side effect to expose these two helpers to all prog types since they do not reveal any kernel specific data. The detailed discussion is in [1]. So with this patch, both bpf_get_current_pid_tgid() and bpf_get_ns_current_pid_tgid() are put in bpf_base_func_proto(), making them available to all program types. [1] https://lore.kernel.org/bpf/20240307232659.1115872-1-yonghong.song@linux.dev/ Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Acked-by: Jiri Olsa <jolsa@kernel.org>
1 parent a4c6f79 commit 3da4f52

File tree

4 files changed

+4
-8
lines changed

4 files changed

+4
-8
lines changed

kernel/bpf/cgroup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,8 +2575,6 @@ cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
25752575
switch (func_id) {
25762576
case BPF_FUNC_get_current_uid_gid:
25772577
return &bpf_get_current_uid_gid_proto;
2578-
case BPF_FUNC_get_current_pid_tgid:
2579-
return &bpf_get_current_pid_tgid_proto;
25802578
case BPF_FUNC_get_current_comm:
25812579
return &bpf_get_current_comm_proto;
25822580
#ifdef CONFIG_CGROUP_NET_CLASSID

kernel/bpf/helpers.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,10 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
17301730
return &bpf_strtol_proto;
17311731
case BPF_FUNC_strtoul:
17321732
return &bpf_strtoul_proto;
1733+
case BPF_FUNC_get_current_pid_tgid:
1734+
return &bpf_get_current_pid_tgid_proto;
1735+
case BPF_FUNC_get_ns_current_pid_tgid:
1736+
return &bpf_get_ns_current_pid_tgid_proto;
17331737
default:
17341738
break;
17351739
}

kernel/trace/bpf_trace.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,6 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15251525
return &bpf_ktime_get_boot_ns_proto;
15261526
case BPF_FUNC_tail_call:
15271527
return &bpf_tail_call_proto;
1528-
case BPF_FUNC_get_current_pid_tgid:
1529-
return &bpf_get_current_pid_tgid_proto;
15301528
case BPF_FUNC_get_current_task:
15311529
return &bpf_get_current_task_proto;
15321530
case BPF_FUNC_get_current_task_btf:
@@ -1582,8 +1580,6 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15821580
return &bpf_send_signal_thread_proto;
15831581
case BPF_FUNC_perf_event_read_value:
15841582
return &bpf_perf_event_read_value_proto;
1585-
case BPF_FUNC_get_ns_current_pid_tgid:
1586-
return &bpf_get_ns_current_pid_tgid_proto;
15871583
case BPF_FUNC_ringbuf_output:
15881584
return &bpf_ringbuf_output_proto;
15891585
case BPF_FUNC_ringbuf_reserve:

net/core/filter.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8342,8 +8342,6 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
83428342
return &bpf_event_output_data_proto;
83438343
case BPF_FUNC_get_current_uid_gid:
83448344
return &bpf_get_current_uid_gid_proto;
8345-
case BPF_FUNC_get_current_pid_tgid:
8346-
return &bpf_get_current_pid_tgid_proto;
83478345
case BPF_FUNC_sk_storage_get:
83488346
return &bpf_sk_storage_get_proto;
83498347
case BPF_FUNC_sk_storage_delete:

0 commit comments

Comments
 (0)