BPF: helpers: New helper to obtain namespace data from current task
Subsystems (9)
PATCH v3 1/3 BPF: helpers: New helper to obtain namespace data from current task
This is a series of patches to introduce a new helper called bpf_get_current_pidns_info, this change has been splitted into the following patches:
1- Feature introduction
2- Update tools/.../bpf.h
3- Self tests and samples
From 852a65906122b05b4d1a23af868b2c245d240402 Mon Sep 17 00:00:00 2001
From: Carlos <[email protected]>
Date: Tue, 19 Mar 2019 19:38:48 -0300
Subject: [PATCH] [PATCH bpf-next 1/3] BPF: New helper to obtain namespace data
from current task
This helper obtains the active namespace from current and returns pid, tgid, device and namespace id as seen from that namespace, allowing to instrument a process inside a container.
Device is read from /proc/self/ns/pid, as in the future it's possible that different pid_ns files may belong to different devices, according to the discussion between Eric Biederman and Yonghong in 2017 linux plumbers conference.
Currently bpf_get_current_pid_tgid(), is used to do pid filtering in bcc's scripts but this helper returns the pid as seen by the root namespace which is fine when a bcc script is not executed inside a container.
When the process of interest is inside a container, pid filtering will not work if bpf_get_current_pid_tgid() is used. This helper addresses this limitation returning the pid as it's seen by the current namespace where the script is executing.
This helper has the same use cases as bpf_get_current_pid_tgid() as it can be used to do pid filtering even inside a container.
For example a bcc script using bpf_get_current_pid_tgid() (tools/funccount.py):
u32 pid = bpf_get_current_pid_tgid() >> 32;
if (pid != <pid_arg_passed_in>)
return 0;Could be modified to use bpf_get_current_pidns_info() as follows:
struct bpf_pidns pidns;
bpf_get_current_pidns_info(&pidns, sizeof(struct bpf_pidns));
u32 pid = pidns.tgid;
u32 nsid = pidns.nsid;
if ((pid != <pid_arg_passed_in>) && (nsid != <nsid_arg_passed_in>))
return 0;To find out the name PID namespace id of a process, you could use this command:
$ ps -h -o pidns -p <pid_of_interest>
Or this other command:
$ ls -Li /proc/<pid_of_interest>/ns/pid
-
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 26 ++++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 96 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -930,6 +930,7 @@ extern const struct bpf_func_proto bpf_sk_redirect_map_proto;extern const struct bpf_func_proto bpf_spin_lock_proto;extern const struct bpf_func_proto bpf_spin_unlock_proto;extern const struct bpf_func_proto bpf_get_local_storage_proto; +extern const struct bpf_func_proto bpf_get_current_pidns_info_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );
include/uapi/linux/bpf.h +25 -1
@@ -2366,6 +2366,18 @@ union bpf_attr {* current value is ect (ECN capable). Works with IPv6 and IPv4. * Return * 1 if set, 0 if not set. +* +* int bpf_get_current_pidns_info(struct bpf_pidns_info *pidns, u32 size_of_pidns) +* Description +* Copies into *pidns* pid, namespace id and tgid as seen by the +* current namespace and also device from /proc/self/ns/pid. +* *size_of_pidns* must be the size of *pidns* +* +* This helper is used when pid filtering is needed inside a +* container as bpf_get_current_tgid() helper returns always the +* pid id as seen by the root namespace. +* Return +* 0 on success -EINVAL on error. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2465,7 +2477,8 @@ union bpf_attr {FN (spin_unlock), \FN (sk_fullsock), \FN (tcp_sock), \ -FN (skb_ecn_set_ce), +FN (skb_ecn_set_ce), \ +FN (get_current_pidns_info),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -3152,4 +3165,15 @@ struct bpf_line_info {struct bpf_spin_lock {__u32 val; }; + +/* helper bpf_get_current_pidns_info will store the following +* data, dev will contain major/minor from /proc/self/pid. +*/ +struct bpf_pidns_info { +__u32 dev; +__u32 nsid; +__u32 tgid; +__u32 pid; +}; +#endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/core.c +1 -0
@@ -2044,6 +2044,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto bpf_get_local_storage_proto__weak ; +const struct bpf_func_proto bpf_get_current_pidns_info__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {
kernel/bpf/helpers.c +67 -0
76 lines not on this page · raw message
kernel/trace/bpf_trace.c +2 -0
@@ -604,6 +604,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_current_cgroup_id:return &bpf_get_current_cgroup_id_proto;#endif +case BPF_FUNC_get_current_pidns_info: +return &bpf_get_current_pidns_info_proto;default :return NULL ; }
Signature · 1 line
2.11.0
From b7f5af5c1513b6e89fc5b68ddb1ad65e4fbede02 Mon Sep 17 00:00:00 2001
From: Carlos <[email protected]>
Date: Mon, 18 Mar 2019 13:09:35 -0300
Subject: [PATCH] [PATCH bpf-next 2/3] BPF: New helper to obtain namespace data
from current task
This helper obtains the active namespace from current and returns pid, tgid, device and namespace id as seen from that namespace, allowing to instrument a process inside a container.
Device is read from /proc/self/ns/pid, as in the future it's possible that different pid_ns files may belong to different devices, according to the discussion between Eric Biederman and Yonghong in 2017 linux plumbers conference.
Currently bpf_get_current_pid_tgid(), is used to do pid filtering in bcc's scripts but this helper returns the pid as seen by the root namespace which is fine when a bcc script is not executed inside a container.
When the process of interest is inside a container, pid filtering will not work if bpf_get_current_pid_tgid() is used. This helper addresses this limitation returning the pid as it's seen by the current namespace where the script is executing.
This helper has the same use cases as bpf_get_current_pid_tgid() as it can be used to do pid filtering even inside a container.
For example a bcc script using bpf_get_current_pid_tgid() (tools/funccount.py):
u32 pid = bpf_get_current_pid_tgid() >> 32;
if (pid != <pid_arg_passed_in>)
return 0;Could be modified to use bpf_get_current_pidns_info() as follows:
struct bpf_pidns pidns;
bpf_get_current_pidns_info(&pidns, sizeof(struct bpf_pidns));
u32 pid = pidns.tgid;
u32 nsid = pidns.nsid;
if ((pid != <pid_arg_passed_in>) && (nsid != <nsid_arg_passed_in>))
return 0;To find out the name PID namespace id of a process, you could use this command:
$ ps -h -o pidns -p <pid_of_interest>
Or this other command:
$ ls -Li /proc/<pid_of_interest>/ns/pid
--- tools/include/uapi/linux/bpf.h | 25 ++++++++++++++++++++++++- tools/testing/selftests/bpf/bpf_helpers.h | 3 +++ 2 files changed, 27 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +24 -1
@@ -2366,6 +2366,18 @@ union bpf_attr {* current value is ect (ECN capable). Works with IPv6 and IPv4. * Return * 1 if set, 0 if not set. +* +* int bpf_get_current_pidns(struct bpf_pidns_info *pidns, u32 size_of_pidns) +* Description +* Copies into *pidns* pid, namespace id and tgid as seen by the +* current namespace and also device from /proc/self/ns/pid. +* *size_of_pidns* must be the size of *pidns* +* +* This helper is used when pid filtering is needed inside a +* container as bpf_get_current_tgid() helper returns always the +* pid id as seen by the root namespace. +* Return +* 0 on success -EINVAL on error. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2465,7 +2477,8 @@ union bpf_attr {FN (spin_unlock), \FN (sk_fullsock), \FN (tcp_sock), \ -FN (skb_ecn_set_ce), +FN (skb_ecn_set_ce), \ +FN (get_current_pidns_info),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -3152,4 +3165,14 @@ struct bpf_line_info {struct bpf_spin_lock {__u32 val; }; + +/* helper bpf_get_current_pidns_info will store the following +* data, dev will contain major/minor from /proc/self/pid. +*/ +struct bpf_pidns_info { +__u32 dev; +__u32 nsid; +__u32 tgid; +__u32 pid; +};#endif /* _UAPI__LINUX_BPF_H__ */
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
@@ -182,6 +182,9 @@ static struct bpf_tcp_sock *(*bpf_tcp_sock)(struct bpf_sock *sk) = (void *) BPF_FUNC_tcp_sock;static int (*bpf_skb_ecn_set_ce)(void *ctx) = (void *) BPF_FUNC_skb_ecn_set_ce; +static int (*bpf_get_current_pidns_info)(struct bpf_pidns_info *buf, +unsigned int buf_size) = + (void *) BPF_FUNC_get_current_pidns_info;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
Signature · 1 line
2.11.0
From 7b7759029fb2bbf6d65a6ac26bb78121e91a30f1 Mon Sep 17 00:00:00 2001
From: Carlos <[email protected]>
Date: Tue, 19 Mar 2019 18:00:56 -0300
Subject: [PATCH] [PATCH bpf-next 3/3] BPF: New helper to obtain namespace data
from current task
This helper obtains the active namespace from current and returns pid, tgid, device and namespace id as seen from that namespace, allowing to instrument a process inside a container.
Device is read from /proc/self/ns/pid, as in the future it's possible that different pid_ns files may belong to different devices, according to the discussion between Eric Biederman and Yonghong in 2017 linux plumbers conference.
Currently bpf_get_current_pid_tgid(), is used to do pid filtering in bcc's scripts but this helper returns the pid as seen by the root namespace which is fine when a bcc script is not executed inside a container.
When the process of interest is inside a container, pid filtering will not work if bpf_get_current_pid_tgid() is used. This helper addresses this limitation returning the pid as it's seen by the current namespace where the script is executing.
This helper has the same use cases as bpf_get_current_pid_tgid() as it can be used to do pid filtering even inside a container.
For example a bcc script using bpf_get_current_pid_tgid() (tools/funccount.py):
u32 pid = bpf_get_current_pid_tgid() >> 32;
if (pid != <pid_arg_passed_in>)
return 0;Could be modified to use bpf_get_current_pidns_info() as follows:
struct bpf_pidns pidns;
bpf_get_current_pidns_info(&pidns, sizeof(struct bpf_pidns));
u32 pid = pidns.tgid;
u32 nsid = pidns.nsid;
if ((pid != <pid_arg_passed_in>) && (nsid != <nsid_arg_passed_in>))
return 0;To find out the name PID namespace id of a process, you could use this command:
$ ps -h -o pidns -p <pid_of_interest>
Or this other command:
$ ls -Li /proc/<pid_of_interest>/ns/pid
---
--- samples/bpf/Makefile | 3 + samples/bpf/trace_ns_info_user.c | 35 ++++++ samples/bpf/trace_ns_info_user_kern.c | 44 +++++++ tools/testing/selftests/bpf/Makefile | 2 +- .../testing/selftests/bpf/progs/test_pidns_kern.c | 48 +++++++ tools/testing/selftests/bpf/test_pidns.c | 138 +++++++++++++++++++++ 6 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 samples/bpf/trace_ns_info_user.c create mode 100644 samples/bpf/trace_ns_info_user_kern.c create mode 100644 tools/testing/selftests/bpf/progs/test_pidns_kern.c create mode 100644 tools/testing/selftests/bpf/test_pidns.c
samples/bpf/Makefile +3 -0
@@ -53,6 +53,7 @@ hostprogs-y += xdp_fwd hostprogs-y += task_fd_query hostprogs-y += xdp_sample_pkts hostprogs-y += hbm +hostprogs-y += trace_ns_info # Libbpf dependencies LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
@@ -109,6 +110,7 @@ xdp_fwd-objs := xdp_fwd_user.o task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) hbm-objs := bpf_load.o hbm.o $(CGROUP_HELPERS) +trace_ns_info-objs := bpf_load.o trace_ns_info_user.o # Tell kbuild to always build the programs always := $(hostprogs-y)
@@ -167,6 +169,7 @@ always += xdp_fwd_kern.o always += task_fd_query_kern.o always += xdp_sample_pkts_kern.o always += hbm_out_kern.o +always += trace_ns_info_user_kern.o KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib/
samples/bpf/trace_ns_info_user.c new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2018 Carlos Neira [email protected] +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of version 2 of the GNU General Public +* License as published by the Free Software Foundation. +*/ + +#include <stdio.h> +#include <linux/bpf.h> +#include <unistd.h> +#include "bpf/libbpf.h" +#include "bpf_load.h" + +/* This code was taken verbatim from tracex1_user.c, it's used +* to exercize bpf_get_current_pidns_info() helper call. +*/ +int main (int ac,char **argv) +{ +FILE *f; +char filename[256 ]; + +snprintf (filename,sizeof (filename),"%s_user_kern.o" , argv[0 ]); +printf ("loading %s\n" , filename); + +if (load_bpf_file (filename)) { +printf ("%s" , bpf_log_buf); +return 1 ; + } + + f =popen ("taskset 1 ping localhost" ,"r" ); + (void ) f; +read_trace_pipe (); +return 0 ; +}
samples/bpf/trace_ns_info_user_kern.c new file +44 -0
44 lines not on this page · raw message
tools/testing/selftests/bpf/Makefile +1 -1
@@ -23,7 +23,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user \ test_socket_cookie test_cgroup_storage test_select_reuseport test_section_names \ - test_netcnt test_tcpnotify_user test_sock_fields + test_netcnt test_tcpnotify_user test_sock_fields test_pidns BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c))) TEST_GEN_FILES = $(BPF_OBJ_FILES)
tools/testing/selftests/bpf/progs/test_pidns_kern.c new file +48 -0
48 lines not on this page · raw message
tools/testing/selftests/bpf/test_pidns.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
2.11.0
Quoted 189 lines · Carlos Antonio Neira Bustos
This is a series of patches to introduce a new helper called
bpf_get_current_pidns_info, this change has been splitted into the following patches:1- Feature introduction
2- Updatetools/.../bpf.h
3- Self tests and samplesFrom
852a65906122b05b4d1a23af868b2c245d240402Mon Sep 17 00:00:00 2001
From: Carlos <[email protected]>
Date: Tue, 19 Mar 2019 19:38:48 -0300
Subject: [PATCH] [PATCH bpf-next 1/3] BPF: New helper to obtain namespace data
from current taskThis helper obtains the active namespace from current and returns pid, tgid, device and namespace id as seen from that namespace, allowing to instrument a process inside a container.
Device is read from/proc/self/ns/pid, as in the future it's possible that differentpid_nsfiles may belong to different devices, according to the discussion between Eric Biederman and Yonghong in 2017 linux plumbers conference.
Currentlybpf_get_current_pid_tgid(), is used to do pid filtering in bcc's scripts but this helper returns the pid as seen by the root namespace which is fine when a bcc script is not executed inside a container.
When the process of interest is inside a container, pid filtering will not work ifbpf_get_current_pid_tgid()is used. This helper addresses this limitation returning the pid as it's seen by the current namespace where the script is executing.This helper has the same use cases as
bpf_get_current_pid_tgid()as it can be used to do pid filtering even inside a container.For example a bcc script using
bpf_get_current_pid_tgid()(tools/funccount.py):u32 pid = bpf_get_current_pid_tgid() >> 32; if (pid != <pid_arg_passed_in>) return 0;Could be modified to use
bpf_get_current_pidns_info()as follows:struct bpf_pidns pidns; bpf_get_current_pidns_info(&pidns, sizeof(struct bpf_pidns)); u32 pid = pidns.tgid; u32 nsid = pidns.nsid; if ((pid != <pid_arg_passed_in>) && (nsid != <nsid_arg_passed_in>)) return 0;To find out the name PID namespace id of a process, you could use this command:
$ ps -h -o pidns -p <pid_of_interest>Or this other command:
$ ls -Li /proc/<pid_of_interest>/ns/pidSigned-off-by Carlos Antonio Neira Bustos <[email protected]>(quoted diff) +0
-2- --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 26 ++++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 96 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -930,6 +930,7 @@ extern const struct bpf_func_proto bpf_sk_redirect_map_proto;extern const struct bpf_func_proto bpf_spin_lock_proto;extern const struct bpf_func_proto bpf_spin_unlock_proto;extern const struct bpf_func_proto bpf_get_local_storage_proto; +extern const struct bpf_func_proto bpf_get_current_pidns_info_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +25
-1@@ -2366,6 +2366,18 @@ union bpf_attr {* current value is ect (ECN capable). Works with IPv6 and IPv4. * Return * 1 if set, 0 if not set. +* +* int bpf_get_current_pidns_info(struct bpf_pidns_info *pidns, u32 size_of_pidns) +* Description +* Copies into *pidns* pid, namespace id and tgid as seen by the +* current namespace and also device from /proc/self/ns/pid. +* *size_of_pidns* must be the size of *pidns* +* +* This helper is used when pid filtering is needed inside a +* container as bpf_get_current_tgid() helper returns always the +* pid id as seen by the root namespace. +* Return +* 0 on success -EINVAL on error. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2465,7 +2477,8 @@ union bpf_attr {FN (spin_unlock), \FN (sk_fullsock), \FN (tcp_sock), \ -FN (skb_ecn_set_ce), +FN (skb_ecn_set_ce), \ +FN (get_current_pidns_info),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3152,4 +3165,15 @@ struct bpf_line_info {struct bpf_spin_lock {__u32 val; }; + +/* helper bpf_get_current_pidns_info will store the following +* data, dev will contain major/minor from /proc/self/pid. +*/ +struct bpf_pidns_info { +__u32 dev; +__u32 nsid; +__u32 tgid; +__u32 pid; +}; +#endif /* _UAPI__LINUX_BPF_H__ */ kernel/bpf/core.c +1
-0@@ -2044,6 +2044,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto bpf_get_local_storage_proto__weak ; +const struct bpf_func_proto bpf_get_current_pidns_info__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +36
-0@@ -18,6 +18,11 @@#include <linux/sched.h> #include <linux/uidgid.h> #include <linux/filter.h> +#include <linux/pid_namespace.h> +#include <linux/major.h> +#include <linux/stat.h> +#include <linux/namei.h> +#include <linux/version.h> /* If kernel subsystem is allowing eBPF programs to call this function, * inside its own verifier_ops->get_func_proto() callback it should return @@ -364,3 +369,65 @@ const struct bpf_func_proto bpf_get_local_storage_proto = { };#endif #endif + +BPF_CALL_2(bpf_get_current_pidns_info, struct bpf_pidns_info *, pidns_info, u32, + size) +{ +const char *nspid ="/proc/self/ns/pid" ; +struct pid_namespace *pidns =NULL ; +struct kstat ks; +struct path kp; +pid_t tgid =0 ; +pid_t pid =0 ; +int res =0 ; + +if (unlikely (size !=sizeof (struct bpf_pidns_info ))) + goto clear; + + pidns =task_active_pid_ns (current); + +if (unlikely (!pidns)) + goto clear; + + pidns_info->nsid = pidns->ns.inum; + pid =task_pid_nr_ns (current, pidns); + +if (unlikely (!pid)) + goto clear; + + tgid =task_tgid_nr_ns (current, pidns); + +if (unlikely (!tgid)) + goto clear; +
kernel/bpf/helpers.c const struct bpf_func_proto bpf_get_local_storage_proto = { · line 412
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+ pidns_info->tgid = (u32 ) tgid; + pidns_info->pid = (u32 ) pid; + +kern_path (nspid,0 , &kp); + +#if LINUX_VERSION_CODE >=KERNEL_VERSION (4 ,11 ,0 ) + res =vfs_getattr (&kp, &ks,STATX_ALL ,0 ); +#else + res =vfs_getattr (&kp, &ks); +#endif
Please access namespaces directly.
I suspect bpf helpers cannot do vfs_getattr. Something inside might sleep while bpf progs are non preemptible.
Also please run ./scripts/checkpatch.pl on your patches and read Documentation/bpf/bpf_devel_QA.rst.
Quoted 205 lines · Alexei Starovoitov
Quoted 189 lines · Carlos Antonio Neira Bustos
This is a series of patches to introduce a new helper called
bpf_get_current_pidns_info, this change has been splitted into the following patches:1- Feature introduction
2- Updatetools/.../bpf.h
3- Self tests and samplesFrom
852a65906122b05b4d1a23af868b2c245d240402Mon Sep 17 00:00:00 2001
From: Carlos <[email protected]>
Date: Tue, 19 Mar 2019 19:38:48 -0300
Subject: [PATCH] [PATCH bpf-next 1/3] BPF: New helper to obtain namespace data
from current taskThis helper obtains the active namespace from current and returns pid, tgid, device and namespace id as seen from that namespace, allowing to instrument a process inside a container.
Device is read from/proc/self/ns/pid, as in the future it's possible that differentpid_nsfiles may belong to different devices, according to the discussion between Eric Biederman and Yonghong in 2017 linux plumbers conference.
Currentlybpf_get_current_pid_tgid(), is used to do pid filtering in bcc's scripts but this helper returns the pid as seen by the root namespace which is fine when a bcc script is not executed inside a container.
When the process of interest is inside a container, pid filtering will not work ifbpf_get_current_pid_tgid()is used. This helper addresses this limitation returning the pid as it's seen by the current namespace where the script is executing.This helper has the same use cases as
bpf_get_current_pid_tgid()as it can be used to do pid filtering even inside a container.For example a bcc script using
bpf_get_current_pid_tgid()(tools/funccount.py):u32 pid = bpf_get_current_pid_tgid() >> 32; if (pid != <pid_arg_passed_in>) return 0;Could be modified to use
bpf_get_current_pidns_info()as follows:struct bpf_pidns pidns; bpf_get_current_pidns_info(&pidns, sizeof(struct bpf_pidns)); u32 pid = pidns.tgid; u32 nsid = pidns.nsid; if ((pid != <pid_arg_passed_in>) && (nsid != <nsid_arg_passed_in>)) return 0;To find out the name PID namespace id of a process, you could use this command:
$ ps -h -o pidns -p <pid_of_interest>Or this other command:
$ ls -Li /proc/<pid_of_interest>/ns/pidSigned-off-by Carlos Antonio Neira Bustos <[email protected]>(quoted diff) +0
-2- --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 26 ++++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 96 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -930,6 +930,7 @@ extern const struct bpf_func_proto bpf_sk_redirect_map_proto;extern const struct bpf_func_proto bpf_spin_lock_proto;extern const struct bpf_func_proto bpf_spin_unlock_proto;extern const struct bpf_func_proto bpf_get_local_storage_proto; +extern const struct bpf_func_proto bpf_get_current_pidns_info_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +25
-1@@ -2366,6 +2366,18 @@ union bpf_attr {* current value is ect (ECN capable). Works with IPv6 and IPv4. * Return * 1 if set, 0 if not set. +* +* int bpf_get_current_pidns_info(struct bpf_pidns_info *pidns, u32 size_of_pidns) +* Description +* Copies into *pidns* pid, namespace id and tgid as seen by the +* current namespace and also device from /proc/self/ns/pid. +* *size_of_pidns* must be the size of *pidns* +* +* This helper is used when pid filtering is needed inside a +* container as bpf_get_current_tgid() helper returns always the +* pid id as seen by the root namespace. +* Return +* 0 on success -EINVAL on error. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2465,7 +2477,8 @@ union bpf_attr {FN (spin_unlock), \FN (sk_fullsock), \FN (tcp_sock), \ -FN (skb_ecn_set_ce), +FN (skb_ecn_set_ce), \ +FN (get_current_pidns_info),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3152,4 +3165,15 @@ struct bpf_line_info {struct bpf_spin_lock {__u32 val; }; + +/* helper bpf_get_current_pidns_info will store the following +* data, dev will contain major/minor from /proc/self/pid. +*/ +struct bpf_pidns_info { +__u32 dev; +__u32 nsid; +__u32 tgid; +__u32 pid; +}; +#endif /* _UAPI__LINUX_BPF_H__ */ kernel/bpf/core.c +1
-0@@ -2044,6 +2044,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto bpf_get_local_storage_proto__weak ; +const struct bpf_func_proto bpf_get_current_pidns_info__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +36
-0@@ -18,6 +18,11 @@#include <linux/sched.h> #include <linux/uidgid.h> #include <linux/filter.h> +#include <linux/pid_namespace.h> +#include <linux/major.h> +#include <linux/stat.h> +#include <linux/namei.h> +#include <linux/version.h> /* If kernel subsystem is allowing eBPF programs to call this function, * inside its own verifier_ops->get_func_proto() callback it should return @@ -364,3 +369,65 @@ const struct bpf_func_proto bpf_get_local_storage_proto = { };#endif #endif + +BPF_CALL_2(bpf_get_current_pidns_info, struct bpf_pidns_info *, pidns_info, u32, + size) +{ +const char *nspid ="/proc/self/ns/pid" ; +struct pid_namespace *pidns =NULL ; +struct kstat ks; +struct path kp; +pid_t tgid =0 ; +pid_t pid =0 ; +int res =0 ; + +if (unlikely (size !=sizeof (struct bpf_pidns_info ))) + goto clear; + + pidns =task_active_pid_ns (current); + +if (unlikely (!pidns)) + goto clear; + + pidns_info->nsid = pidns->ns.inum; + pid =task_pid_nr_ns (current, pidns); + +if (unlikely (!pid)) + goto clear; + + tgid =task_tgid_nr_ns (current, pidns); + +if (unlikely (!tgid)) + goto clear; +kernel/bpf/helpers.c const struct bpf_func_proto bpf_get_local_storage_proto = { · line 412
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+ pidns_info->tgid = (u32 ) tgid; + pidns_info->pid = (u32 ) pid; + +kern_path (nspid,0 , &kp); + +#if LINUX_VERSION_CODE >=KERNEL_VERSION (4 ,11 ,0 ) + res =vfs_getattr (&kp, &ks,STATX_ALL ,0 ); +#else + res =vfs_getattr (&kp, &ks); +#endif Please access namespaces directly.
I suspect bpf helpers cannot dovfs_getattr. Something inside might sleep while bpf progs are non preemptible.
Quoted 3 lines
Also please run
./scripts/checkpatch.plon your patches and readDocumentation/bpf/bpf_devel_QA.rst.
Hello Alexei,
Thanks for checking this out and also thanks for the documentation link. At this point I have a couple of questions:
* What is the reason to not use the current namespace api instead of directly accessing namespaces?.
* Regarding bpf programs not being preemptible. If we add spin_locks to the vfs_getattr call, would that be an acceptable solution?
spin_lock(&bpf_lock); res = vfs_getattr(&kp, &ks); spin_unlock(&bpf_lock);
Is there another way to interact with the vfs layer within a bpf helper?.
Bests