BPF: New helper to obtain namespace data from current task
Subsystems (10)
v12 cover BPF: New helper to obtain namespace data from current task
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.
In the future different pid_ns files may belong to different devices, according to the discussion between Eric Biederman and Yonghong in 2017 Linux plumbers conference. To address that situation the helper requires inum and dev_t from /proc/self/ns/pid. 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.
Changes from V11:
- helper: Input changed dev from u32 to u64.
- Moved self-test to test_progs.
- Remove unneeded maps in self-test.
Carlos Neira (4):
fs/nsfs.c: added ns_match
bpf: added new helper bpf_get_ns_current_pid_tgid
tools: Added bpf_get_ns_current_pid_tgid helper tools/testing/selftests/bpf: Add self-tests for new helper.
fs/nsfs.c | 8 ++ include/linux/bpf.h | 1 + include/linux/proc_ns.h | 2 + include/uapi/linux/bpf.h | 18 +++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 36 ++++++++ kernel/trace/bpf_trace.c | 2 + tools/include/uapi/linux/bpf.h | 18 +++- tools/testing/selftests/bpf/bpf_helpers.h | 3 + .../bpf/prog_tests/get_ns_current_pid_tgid.c | 85 +++++++++++++++++++ .../bpf/progs/get_ns_current_pid_tgid_kern.c | 47 ++++++++++ 11 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_ns_current_pid_tgid.c create mode 100644 tools/testing/selftests/bpf/progs/get_ns_current_pid_tgid_kern.c
Signature · 1 line
2.20.1
PATCH v12 1/4 fs/nsfs.c: added ns_match
ns_match returns true if the namespace inode and dev_t matches the ones provided by the caller.
--- fs/nsfs.c | 8 ++++++++ include/linux/proc_ns.h | 2 ++ 2 files changed, 10 insertions(+)
fs/nsfs.c +8 -0
@@ -245,6 +245,14 @@ struct file *proc_ns_fget(int fd)return ERR_PTR (-EINVAL ); } +/* Returns true if current namespace matches dev/ino. +*/ +bool ns_match (const struct ns_common *ns,dev_t dev,ino_t ino) +{ +return ((ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev)); +} + +static int nsfs_show_path (struct seq_file *seq,struct dentry *dentry) {struct inode *inode =d_inode (dentry);
include/linux/proc_ns.h +2 -0
@@ -82,6 +82,8 @@ typedef struct ns_common *ns_get_path_helper_t(void *);extern void *ns_get_path_cb (struct path *path,ns_get_path_helper_t ns_get_cb,void *private_data); +extern bool ns_match (const struct ns_common *ns,dev_t dev,ino_t ino); +extern int ns_get_name (char *buf,size_t size,struct task_struct *task,const struct proc_ns_operations *ns_ops);extern void nsfs_init (void );
Signature · 1 line
2.20.1
PATCH v12 2/4 bpf: added new helper bpf_get_ns_current_pid_tgid
In reply to Carlos NeiraNew bpf helper bpf_get_ns_current_pid_tgid,
This helper will return pid and tgid from current task which namespace matches dev_t and inode number provided, this will allows us to instrument a process inside a container.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 36 ++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 57 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -1055,6 +1055,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto;extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );
include/uapi/linux/bpf.h +17 -1
@@ -2750,6 +2750,21 @@ union bpf_attr {* **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 +* +* u64 bpf_get_ns_current_pid_tgid(u64 dev, u64 inum) +* Return +* A 64-bit integer containing the current tgid and pid from current task +* which namespace inode and dev_t matches , and is create as such: +* *current_task*\ **->tgid << 32 \|** +* *current_task*\ **->pid**. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if dev and inum supplied don't match dev_t and inode number +* with nsfs of current task, or if dev conversion to dev_t lost high bits. +* +* **-ENOENT** if /proc/self/ns does not exists. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2862,7 +2877,8 @@ union bpf_attr {FN (sk_storage_get), \FN (sk_storage_delete), \FN (send_signal), \ -FN (tcp_gen_syncookie), +FN (tcp_gen_syncookie), \ +FN (get_ns_current_pid_tgid),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/bpf/core.c +1 -0
@@ -2042,6 +2042,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_ns_current_pid_tgid_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {
kernel/bpf/helpers.c +36 -0
@@ -11,6 +11,8 @@#include <linux/uidgid.h> #include <linux/filter.h> #include <linux/ctype.h> +#include <linux/pid_namespace.h> +#include <linux/proc_ns.h> #include "../../lib/kstrtox.h"
@@ -487,3 +489,37 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, };#endif + +BPF_CALL_2(bpf_get_ns_current_pid_tgid, u64, dev, u64, inum) +{ +struct task_struct *task = current; +struct pid_namespace *pidns; +pid_t pid, tgid; + +if ((u64 )(dev_t )dev != dev) +return -EINVAL ; + +if (unlikely (!task)) +return -EINVAL ; + + pidns =task_active_pid_ns (task); +if (unlikely (!pidns)) +return -ENOENT ; + + +if (!ns_match (&pidns->ns, (dev_t )dev, inum)) +return -EINVAL ; + + pid =task_pid_nr_ns (task, pidns); + tgid =task_tgid_nr_ns (task, pidns); + +return (u64 ) tgid <<32 | pid; +} + +const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { + .func = bpf_get_ns_current_pid_tgid, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_ANYTHING , + .arg2_type =ARG_ANYTHING , +};
kernel/trace/bpf_trace.c +2 -0
@@ -735,6 +735,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif case BPF_FUNC_send_signal:return &bpf_send_signal_proto; +case BPF_FUNC_get_ns_current_pid_tgid: +return &bpf_get_ns_current_pid_tgid_proto;default :return NULL ; }
Signature · 1 line
2.20.1
Daniel Borkmann
maintainerRe: [PATCH V12 2/4] bpf: added new helper bpf_get_ns_current_pid_tgid
Quoted 106 lines · Carlos Neira
New bpf helper
bpf_get_ns_current_pid_tgid,
This helper will return pid and tgid from current task which namespace matchesdev_tand inode number provided, this will allows us to instrument a process inside a container.Signed-off-by Carlos Neira <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 36 ++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 57 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -1055,6 +1055,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto;extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +17
-1@@ -2750,6 +2750,21 @@ union bpf_attr {* **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 +* +* u64 bpf_get_ns_current_pid_tgid(u64 dev, u64 inum) +* Return +* A 64-bit integer containing the current tgid and pid from current task +* which namespace inode and dev_t matches , and is create as such: +* *current_task*\ **->tgid << 32 \|** +* *current_task*\ **->pid**. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if dev and inum supplied don't match dev_t and inode number +* with nsfs of current task, or if dev conversion to dev_t lost high bits. +* +* **-ENOENT** if /proc/self/ns does not exists. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2862,7 +2877,8 @@ union bpf_attr {FN (sk_storage_get), \FN (sk_storage_delete), \FN (send_signal), \ -FN (tcp_gen_syncookie), +FN (tcp_gen_syncookie), \ +FN (get_ns_current_pid_tgid),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -2042,6 +2042,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_ns_current_pid_tgid_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +17
-0@@ -11,6 +11,8 @@#include <linux/uidgid.h> #include <linux/filter.h> #include <linux/ctype.h> +#include <linux/pid_namespace.h> +#include <linux/proc_ns.h> #include "../../lib/kstrtox.h" @@ -487,3 +489,37 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, };#endif + +BPF_CALL_2(bpf_get_ns_current_pid_tgid, u64, dev, u64, inum) +{ +struct task_struct *task = current; +struct pid_namespace *pidns; +pid_t pid, tgid; + +if ((u64 )(dev_t )dev != dev) +return -EINVAL ; + +if (unlikely (!task)) +return -EINVAL ; + + pidns =task_active_pid_ns (task); +if (unlikely (!pidns))
kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtoul_proto = { · line 516
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+return -ENOENT ; + + +if (!ns_match (&pidns->ns, (dev_t )dev, inum)) +return -EINVAL ; + + pid =task_pid_nr_ns (task, pidns); + tgid =task_tgid_nr_ns (task, pidns); + +return (u64 ) tgid <<32 | pid;
Basically here you are overlapping the 64-bit return value for the valid outcome with the error codes above for the invalid case. If you look at bpf_perf_event_read() we already had such broken occasion that bit us in the past, and needed to introduce bpf_perf_event_read_value() instead. Lets not go there again and design it similarly to the latter.
Quoted 23 lines
kernel/bpf/helpers.c +9
-0+} + +const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { + .func = bpf_get_ns_current_pid_tgid, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_ANYTHING , + .arg2_type =ARG_ANYTHING , +};kernel/trace/bpf_trace.c +2
-0@@ -735,6 +735,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif case BPF_FUNC_send_signal:return &bpf_send_signal_proto; +case BPF_FUNC_get_ns_current_pid_tgid: +return &bpf_get_ns_current_pid_tgid_proto;default :return NULL ; }
Re: [PATCH V12 2/4] bpf: added new helper bpf_get_ns_current_pid_tgid
Quoted 134 lines · Daniel Borkmann
Quoted 101 lines · Carlos Neira
New bpf helper
bpf_get_ns_current_pid_tgid,
This helper will return pid and tgid from current task which namespace matchesdev_tand inode number provided, this will allows us to instrument a process inside a container.Signed-off-by Carlos Neira <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 36 ++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 57 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -1055,6 +1055,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto;extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +17
-1@@ -2750,6 +2750,21 @@ union bpf_attr {* **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 +* +* u64 bpf_get_ns_current_pid_tgid(u64 dev, u64 inum) +* Return +* A 64-bit integer containing the current tgid and pid from current task +* which namespace inode and dev_t matches , and is create as such: +* *current_task*\ **->tgid << 32 \|** +* *current_task*\ **->pid**. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if dev and inum supplied don't match dev_t and inode number +* with nsfs of current task, or if dev conversion to dev_t lost high bits. +* +* **-ENOENT** if /proc/self/ns does not exists. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2862,7 +2877,8 @@ union bpf_attr {FN (sk_storage_get), \FN (sk_storage_delete), \FN (send_signal), \ -FN (tcp_gen_syncookie), +FN (tcp_gen_syncookie), \ +FN (get_ns_current_pid_tgid),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -2042,6 +2042,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_ns_current_pid_tgid_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +17
-0@@ -11,6 +11,8 @@#include <linux/uidgid.h> #include <linux/filter.h> #include <linux/ctype.h> +#include <linux/pid_namespace.h> +#include <linux/proc_ns.h> #include "../../lib/kstrtox.h" @@ -487,3 +489,37 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, };#endif + +BPF_CALL_2(bpf_get_ns_current_pid_tgid, u64, dev, u64, inum) +{ +struct task_struct *task = current; +struct pid_namespace *pidns; +pid_t pid, tgid; + +if ((u64 )(dev_t )dev != dev) +return -EINVAL ; + +if (unlikely (!task)) +return -EINVAL ; + + pidns =task_active_pid_ns (task); +if (unlikely (!pidns))kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtoul_proto = { · line 516
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+return -ENOENT ; + + +if (!ns_match (&pidns->ns, (dev_t )dev, inum)) +return -EINVAL ; + + pid =task_pid_nr_ns (task, pidns); + tgid =task_tgid_nr_ns (task, pidns); + +return (u64 ) tgid <<32 | pid;Basically here you are overlapping the 64-bit return value for the valid outcome with the error codes above for the invalid case. If you look at
bpf_perf_event_read()we already had such broken occasion that bit us in the past, and needed to introducebpf_perf_event_read_value()instead. Lets not go there again and design it similarly to the latter.Quoted 15 lines
kernel/bpf/helpers.c +9
-0+} + +const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { + .func = bpf_get_ns_current_pid_tgid, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_ANYTHING , + .arg2_type =ARG_ANYTHING , +};kernel/trace/bpf_trace.c +0
-0@@ -735,6 +735,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif
Quoted 9 lines
kernel/trace/bpf_trace.c tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 742
Quoted 8 lines
kernel/trace/bpf_trace.c +2
-0case BPF_FUNC_send_signal:return &bpf_send_signal_proto; +case BPF_FUNC_get_ns_current_pid_tgid: +return &bpf_get_ns_current_pid_tgid_proto;default :return NULL ; }
Daniel,
If I understand correctly, to avoid problems I need to change the helper's function signature to something like the following:
struct bpf_ns_current_pid_tgid_storage {
__u64 dev;
__u64 inum;
__u64 pidtgid;
};BPF_CALL_2(bpf_get_ns_current_pid_tgid,
struct bpf_ns_current_pid_tgid_storage *, buf, u32, size);
then use dev and inum provided by the user and return the requested value into pidtgid of the struct. Would that work?
Thanks for your help.
Re: [PATCH V12 2/4] bpf: added new helper bpf_get_ns_current_pid_tgid
Quoted 145 lines · Carlos Antonio Neira Bustos
Quoted 134 lines · Daniel Borkmann
Quoted 101 lines · Carlos Neira
New bpf helper
bpf_get_ns_current_pid_tgid,
This helper will return pid and tgid from current task which namespace matchesdev_tand inode number provided, this will allows us to instrument a process inside a container.Signed-off-by Carlos Neira <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 36 ++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 57 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -1055,6 +1055,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto;extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +17
-1@@ -2750,6 +2750,21 @@ union bpf_attr {* **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 +* +* u64 bpf_get_ns_current_pid_tgid(u64 dev, u64 inum) +* Return +* A 64-bit integer containing the current tgid and pid from current task +* which namespace inode and dev_t matches , and is create as such: +* *current_task*\ **->tgid << 32 \|** +* *current_task*\ **->pid**. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if dev and inum supplied don't match dev_t and inode number +* with nsfs of current task, or if dev conversion to dev_t lost high bits. +* +* **-ENOENT** if /proc/self/ns does not exists. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2862,7 +2877,8 @@ union bpf_attr {FN (sk_storage_get), \FN (sk_storage_delete), \FN (send_signal), \ -FN (tcp_gen_syncookie), +FN (tcp_gen_syncookie), \ +FN (get_ns_current_pid_tgid),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -2042,6 +2042,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_ns_current_pid_tgid_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +17
-0@@ -11,6 +11,8 @@#include <linux/uidgid.h> #include <linux/filter.h> #include <linux/ctype.h> +#include <linux/pid_namespace.h> +#include <linux/proc_ns.h> #include "../../lib/kstrtox.h" @@ -487,3 +489,37 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, };#endif + +BPF_CALL_2(bpf_get_ns_current_pid_tgid, u64, dev, u64, inum) +{ +struct task_struct *task = current; +struct pid_namespace *pidns; +pid_t pid, tgid; + +if ((u64 )(dev_t )dev != dev) +return -EINVAL ; + +if (unlikely (!task)) +return -EINVAL ; + + pidns =task_active_pid_ns (task); +if (unlikely (!pidns))kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtoul_proto = { · line 516
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+return -ENOENT ; + + +if (!ns_match (&pidns->ns, (dev_t )dev, inum)) +return -EINVAL ; + + pid =task_pid_nr_ns (task, pidns); + tgid =task_tgid_nr_ns (task, pidns); + +return (u64 ) tgid <<32 | pid;Basically here you are overlapping the 64-bit return value for the valid outcome with the error codes above for the invalid case. If you look at
bpf_perf_event_read()we already had such broken occasion that bit us in the past, and needed to introducebpf_perf_event_read_value()instead. Lets not go there again and design it similarly to the latter.Quoted 15 lines
kernel/bpf/helpers.c +9
-0+} + +const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { + .func = bpf_get_ns_current_pid_tgid, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_ANYTHING , + .arg2_type =ARG_ANYTHING , +};kernel/trace/bpf_trace.c +0
-0@@ -735,6 +735,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif Quoted 9 lines
kernel/trace/bpf_trace.c tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 742
Quoted 8 lines
kernel/trace/bpf_trace.c +2
-0case BPF_FUNC_send_signal:return &bpf_send_signal_proto; +case BPF_FUNC_get_ns_current_pid_tgid: +return &bpf_get_ns_current_pid_tgid_proto;default :return NULL ; }
Quoted 7 lines
Daniel,
If I understand correctly, to avoid problems I need to change the helper's function signature to something like the following:struct bpf_ns_current_pid_tgid_storage { __u64 dev; __u64 inum; __u64 pidtgid;
if you do it this way, please do
__u32 pid;__u34 tgid;
I have to look up where pid and tgid is in that 64-bit number every single time, let's not do it here for no good reason.
Quoted 13 lines
};
BPF_CALL_2(bpf_get_ns_current_pid_tgid,
struct bpf_ns_current_pid_tgid_storage *, buf, u32, size);then use dev and inum provided by the user and return the requested value into pidtgid of the struct. Would that work?
Thanks for your help.
Re: [PATCH V12 2/4] bpf: added new helper bpf_get_ns_current_pid_tgid
Quoted 155 lines · Andrii Nakryiko
Quoted 145 lines · Carlos Antonio Neira Bustos
Quoted 134 lines · Daniel Borkmann
Quoted 101 lines · Carlos Neira
New bpf helper
bpf_get_ns_current_pid_tgid,
This helper will return pid and tgid from current task which namespace matchesdev_tand inode number provided, this will allows us to instrument a process inside a container.Signed-off-by Carlos Neira <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 36 ++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 57 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -1055,6 +1055,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;extern const struct bpf_func_proto bpf_strtol_proto;extern const struct bpf_func_proto bpf_strtoul_proto;extern const struct bpf_func_proto bpf_tcp_sock_proto; +extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +17
-1@@ -2750,6 +2750,21 @@ union bpf_attr {* **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 +* +* u64 bpf_get_ns_current_pid_tgid(u64 dev, u64 inum) +* Return +* A 64-bit integer containing the current tgid and pid from current task +* which namespace inode and dev_t matches , and is create as such: +* *current_task*\ **->tgid << 32 \|** +* *current_task*\ **->pid**. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if dev and inum supplied don't match dev_t and inode number +* with nsfs of current task, or if dev conversion to dev_t lost high bits. +* +* **-ENOENT** if /proc/self/ns does not exists. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2862,7 +2877,8 @@ union bpf_attr {FN (sk_storage_get), \FN (sk_storage_delete), \FN (send_signal), \ -FN (tcp_gen_syncookie), +FN (tcp_gen_syncookie), \ +FN (get_ns_current_pid_tgid),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -2042,6 +2042,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_ns_current_pid_tgid_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +17
-0@@ -11,6 +11,8 @@#include <linux/uidgid.h> #include <linux/filter.h> #include <linux/ctype.h> +#include <linux/pid_namespace.h> +#include <linux/proc_ns.h> #include "../../lib/kstrtox.h" @@ -487,3 +489,37 @@ const struct bpf_func_proto bpf_strtoul_proto = { .arg4_type = ARG_PTR_TO_LONG, };#endif + +BPF_CALL_2(bpf_get_ns_current_pid_tgid, u64, dev, u64, inum) +{ +struct task_struct *task = current; +struct pid_namespace *pidns; +pid_t pid, tgid; + +if ((u64 )(dev_t )dev != dev) +return -EINVAL ; + +if (unlikely (!task)) +return -EINVAL ; + + pidns =task_active_pid_ns (task); +if (unlikely (!pidns))kernel/bpf/helpers.c const struct bpf_func_proto bpf_strtoul_proto = { · line 516
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+return -ENOENT ; + + +if (!ns_match (&pidns->ns, (dev_t )dev, inum)) +return -EINVAL ; + + pid =task_pid_nr_ns (task, pidns); + tgid =task_tgid_nr_ns (task, pidns); + +return (u64 ) tgid <<32 | pid;Basically here you are overlapping the 64-bit return value for the valid outcome with the error codes above for the invalid case. If you look at
bpf_perf_event_read()we already had such broken occasion that bit us in the past, and needed to introducebpf_perf_event_read_value()instead. Lets not go there again and design it similarly to the latter.Quoted 15 lines
kernel/bpf/helpers.c +9
-0+} + +const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { + .func = bpf_get_ns_current_pid_tgid, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_ANYTHING , + .arg2_type =ARG_ANYTHING , +};kernel/trace/bpf_trace.c +0
-0@@ -735,6 +735,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)#endif Quoted 9 lines
kernel/trace/bpf_trace.c tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 742
Quoted 8 lines
kernel/trace/bpf_trace.c +2
-0case BPF_FUNC_send_signal:return &bpf_send_signal_proto; +case BPF_FUNC_get_ns_current_pid_tgid: +return &bpf_get_ns_current_pid_tgid_proto;default :return NULL ; }Quoted 7 lines
Daniel,
If I understand correctly, to avoid problems I need to change the helper's function signature to something like the following:struct bpf_ns_current_pid_tgid_storage { __u64 dev; __u64 inum; __u64 pidtgid;
Quoted 21 lines
if you do it this way, please do
__u32pid;__u34tgid;I have to look up where pid and tgid is in that 64-bit number every single time, let's not do it here for no good reason.
Quoted 13 lines
};
BPF_CALL_2(bpf_get_ns_current_pid_tgid,
struct bpf_ns_current_pid_tgid_storage *, buf, u32, size);then use dev and inum provided by the user and return the requested value into pidtgid of the struct. Would that work?
Thanks for your help.
Andrii,
You are right, I'll do so.
Bests.
PATCH v12 3/4 tools: Added bpf_get_ns_current_pid_tgid helper
In reply to Carlos Neirasync tools/include/uapi/linux/bpf.h to include new helper.
--- tools/include/uapi/linux/bpf.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +17 -1
@@ -2750,6 +2750,21 @@ union bpf_attr {* **-EOPNOTSUPP** kernel configuration does not enable SYN cookies * * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 +* +* u64 bpf_get_ns_current_pid_tgid(u64 dev, u64 inum) +* Return +* A 64-bit integer containing the current tgid and pid from current task +* which namespace inode and dev_t matches , and is create as such: +* *current_task*\ **->tgid << 32 \|** +* *current_task*\ **->pid**. +* +* On failure, the returned value is one of the following: +* +* **-EINVAL** if dev and inum supplied don't match dev_t and inode number +* with nsfs of current task, or if dev conversion to dev_t lost high bits. +* +* **-ENOENT** if /proc/self/ns does not exists. +* */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2862,7 +2877,8 @@ union bpf_attr {FN (sk_storage_get), \FN (sk_storage_delete), \FN (send_signal), \ -FN (tcp_gen_syncookie), +FN (tcp_gen_syncookie), \ +FN (get_ns_current_pid_tgid),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
Signature · 1 line
2.20.1
PATCH v12 4/4 tools/testing/selftests/bpf: Add self-tests for new helper.
In reply to Carlos NeiraSelf tests added for new helper
--- tools/testing/selftests/bpf/bpf_helpers.h | 3 + .../bpf/prog_tests/get_ns_current_pid_tgid.c | 85 +++++++++++++++++++ .../bpf/progs/get_ns_current_pid_tgid_kern.c | 47 ++++++++++ 3 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_ns_current_pid_tgid.c create mode 100644 tools/testing/selftests/bpf/progs/get_ns_current_pid_tgid_kern.c
tools/testing/selftests/bpf/bpf_helpers.h +3 -0
@@ -233,6 +233,9 @@ static int (*bpf_send_signal)(unsigned sig) = (void *)BPF_FUNC_send_signal;static long long (*bpf_tcp_gen_syncookie)(struct bpf_sock *sk,void *ip,int ip_len,void *tcp,int tcp_len) = (void *) BPF_FUNC_tcp_gen_syncookie; +static unsigned long long (*bpf_get_ns_current_pid_tgid)(__u64 dev,__u64 inum) = + (void *) BPF_FUNC_get_ns_current_pid_tgid; +/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
tools/testing/selftests/bpf/prog_tests/get_ns_current_pid_tgid.c new file +85 -0
85 lines not on this page · raw message
tools/testing/selftests/bpf/progs/get_ns_current_pid_tgid_kern.c new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.20.1