bpf: Allow helper bpf_get_[ns_]current_pid_tgid() for all prog types
authorYonghong Song <yonghong.song@linux.dev>
Fri, 15 Mar 2024 18:48:54 +0000 (11:48 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 19 Mar 2024 21:24:07 +0000 (14:24 -0700)
commiteb166e522c77699fc19bfa705652327a1e51a117
tree868016438479325a893aa55572834edc674f267d
parent1a4a0cb7985f921548f1a7ac17686afbefe67f87
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>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20240315184854.2975190-1-yonghong.song@linux.dev
kernel/bpf/cgroup.c
kernel/bpf/helpers.c
kernel/trace/bpf_trace.c
net/core/filter.c