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)
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

index 82243cb6c54db91cff883b74b66bc6281074ae35..8ba73042a23952354311ffd82fd57f9488b72c91 100644 (file)
@@ -2575,8 +2575,6 @@ cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
        switch (func_id) {
        case BPF_FUNC_get_current_uid_gid:
                return &bpf_get_current_uid_gid_proto;
-       case BPF_FUNC_get_current_pid_tgid:
-               return &bpf_get_current_pid_tgid_proto;
        case BPF_FUNC_get_current_comm:
                return &bpf_get_current_comm_proto;
 #ifdef CONFIG_CGROUP_NET_CLASSID
index a895878595710b41c4d80e5a1d74ba9bb4ee0b73..9234174ccb21b9e5f682e4c7f572ea2c6033bf2f 100644 (file)
@@ -1730,6 +1730,10 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_strtol_proto;
        case BPF_FUNC_strtoul:
                return &bpf_strtoul_proto;
+       case BPF_FUNC_get_current_pid_tgid:
+               return &bpf_get_current_pid_tgid_proto;
+       case BPF_FUNC_get_ns_current_pid_tgid:
+               return &bpf_get_ns_current_pid_tgid_proto;
        default:
                break;
        }
index 0a5c4efc73c3674fa225757c6f4ccc921f758b57..1b041911b1d8c99dfbf9c3c901e34af8489de0cf 100644 (file)
@@ -1525,8 +1525,6 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_ktime_get_boot_ns_proto;
        case BPF_FUNC_tail_call:
                return &bpf_tail_call_proto;
-       case BPF_FUNC_get_current_pid_tgid:
-               return &bpf_get_current_pid_tgid_proto;
        case BPF_FUNC_get_current_task:
                return &bpf_get_current_task_proto;
        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)
                return &bpf_send_signal_thread_proto;
        case BPF_FUNC_perf_event_read_value:
                return &bpf_perf_event_read_value_proto;
-       case BPF_FUNC_get_ns_current_pid_tgid:
-               return &bpf_get_ns_current_pid_tgid_proto;
        case BPF_FUNC_ringbuf_output:
                return &bpf_ringbuf_output_proto;
        case BPF_FUNC_ringbuf_reserve:
index 8adf95765cdd967a15b2661dfb454db0ccf350b0..0c66e4a3fc5bbeca77ca28db00d64cc1d4923e2d 100644 (file)
@@ -8342,8 +8342,6 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_event_output_data_proto;
        case BPF_FUNC_get_current_uid_gid:
                return &bpf_get_current_uid_gid_proto;
-       case BPF_FUNC_get_current_pid_tgid:
-               return &bpf_get_current_pid_tgid_proto;
        case BPF_FUNC_sk_storage_get:
                return &bpf_sk_storage_get_proto;
        case BPF_FUNC_sk_storage_delete: