Skip to contentSkip to conversation

BPF: New helper to obtain namespace data from current task

PATCH In review Carlos Neira 4 participants 10 messages Mar 4, 2020 – Apr 28, 2020 1 message not in the archive

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

v17 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.

Carlos Neira (3):
fs/nsfs.c: added ns_match
bpf: added new helper bpf_get_ns_current_pid_tgid tools/testing/selftests/bpf: Add self-tests for new helper
bpf_get_ns_current_pid_tgid.

 fs/nsfs.c                                     |  14 ++
 include/linux/bpf.h                           |   1 +
 include/linux/proc_ns.h                       |   2 +
 include/uapi/linux/bpf.h                      |  20 ++-
 kernel/bpf/core.c                             |   1 +
 kernel/bpf/helpers.c                          |  45 +++++
 kernel/trace/bpf_trace.c                      |   2 +
 scripts/bpf_helpers_doc.py                    |   1 +
 tools/include/uapi/linux/bpf.h                |  20 ++-
 tools/testing/selftests/bpf/Makefile          |   3 +-
 .../bpf/prog_tests/ns_current_pid_tgid.c      |  88 ++++++++++
 .../bpf/progs/test_ns_current_pid_tgid.c      |  37 ++++
 .../bpf/test_current_pid_tgid_new_ns.c        | 159 ++++++++++++++++++
 13 files changed, 390 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
 create mode 100644 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v17 1/3 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               | 14 ++++++++++++++
 include/linux/proc_ns.h |  2 ++
 2 files changed, 16 insertions(+)
fs/nsfs.c +14 -0
index b13bfd406820..4f1205725cfe 100644
@@ -247,6 +247,20 @@ struct file *proc_ns_fget(int fd)
 	return ERR_PTR(-EINVAL);
 }
 
+/**
+ * ns_match() - Returns true if current namespace matches dev/ino provided.
+ * @ns_common: current ns
+ * @dev: dev_t from nsfs that will be matched against current nsfs
+ * @ino: ino_t from nsfs that will be matched against current nsfs
+ *
+ * Return: true if dev and ino matches the current nsfs.
+ */
+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
index 4626b1ac3b6c..adff08bfecf9 100644
@@ -85,6 +85,8 @@ typedef struct ns_common *ns_get_path_helper_t(void *);
 extern int 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
#2 lore raw

PATCH v17 2/3 bpf: added new helper bpf_get_ns_current_pid_tgid

In reply to Carlos Neira

New 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       | 20 ++++++++++++++-
 kernel/bpf/core.c              |  1 +
 kernel/bpf/helpers.c           | 45 ++++++++++++++++++++++++++++++++++
 kernel/trace/bpf_trace.c       |  2 ++
 scripts/bpf_helpers_doc.py     |  1 +
 tools/include/uapi/linux/bpf.h | 20 ++++++++++++++-
 7 files changed, 88 insertions(+), 2 deletions(-)
include/linux/bpf.h +1 -0
index f13c78c6f29d..8d6380394388 100644
@@ -1472,6 +1472,7 @@ 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_jiffies64_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 +19 -1
index d6b33ea27bcc..f3ad826fa3bd 100644
@@ -2913,6 +2913,19 @@ union bpf_attr {
  *		of sizeof(struct perf_branch_entry).
  *
  *		**-ENOENT** if architecture does not support branch records.
+ *
+ * int bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size)
+ *	Description
+ *		Returns 0 on success, values for *pid* and *tgid* as seen from the current
+ *		*namespace* will be returned in *nsdata*.
+ *
+ *		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 pidns does not exists for the current task.
+ *
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3034,7 +3047,8 @@ union bpf_attr {
 	FN(tcp_send_ack),		\
 	FN(send_signal_thread),		\
 	FN(jiffies64),			\
-	FN(read_branch_records),
+	FN(read_branch_records),	\
+	FN(get_ns_current_pid_tgid),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
@@ -3828,4 +3842,8 @@ struct bpf_sockopt {
 	__s32	retval;
 };
 
+struct bpf_pidns_info {
+	__u32 pid;
+	__u32 tgid;
+};
 #endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/core.c +1 -0
index 973a20d49749..0f9ca46e1978 100644
@@ -2149,6 +2149,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 +45 -0

54 lines not on this page · raw message

kernel/trace/bpf_trace.c +2 -0
index 07764c761073..d7985629a3b1 100644
@@ -843,6 +843,8 @@ 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;
 	default:
 		return NULL;
 	}
scripts/bpf_helpers_doc.py +1 -0
index cebed6fb5bbb..c1e2b5410faa 100755
@@ -435,6 +435,7 @@ class PrinterHelpers(Printer):
             'struct bpf_fib_lookup',
             'struct bpf_perf_event_data',
             'struct bpf_perf_event_value',
+            'struct bpf_pidns_info',
             'struct bpf_sock',
             'struct bpf_sock_addr',
             'struct bpf_sock_ops',
tools/include/uapi/linux/bpf.h +19 -1
index d6b33ea27bcc..f3ad826fa3bd 100644
@@ -2913,6 +2913,19 @@ union bpf_attr {
  *		of sizeof(struct perf_branch_entry).
  *
  *		**-ENOENT** if architecture does not support branch records.
+ *
+ * int bpf_get_ns_current_pid_tgid(u64 dev, u64 ino, struct bpf_pidns_info *nsdata, u32 size)
+ *	Description
+ *		Returns 0 on success, values for *pid* and *tgid* as seen from the current
+ *		*namespace* will be returned in *nsdata*.
+ *
+ *		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 pidns does not exists for the current task.
+ *
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3034,7 +3047,8 @@ union bpf_attr {
 	FN(tcp_send_ack),		\
 	FN(send_signal_thread),		\
 	FN(jiffies64),			\
-	FN(read_branch_records),
+	FN(read_branch_records),	\
+	FN(get_ns_current_pid_tgid),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
@@ -3828,4 +3842,8 @@ struct bpf_sockopt {
 	__s32	retval;
 };
 
+struct bpf_pidns_info {
+	__u32 pid;
+	__u32 tgid;
+};
 #endif /* _UAPI__LINUX_BPF_H__ */
Signature · 1 line
2.20.1
#3 lore raw

PATCH v17 3/3 tools/testing/selftests/bpf: Add self-tests for new helper bpf_get_ns_current_pid_tgid.

In reply to Carlos Neira

Self tests added for new helper bpf_get_ns_current_pid_tgid

---
 tools/testing/selftests/bpf/Makefile          |   3 +-
 .../bpf/prog_tests/ns_current_pid_tgid.c      |  88 ++++++++++
 .../bpf/progs/test_ns_current_pid_tgid.c      |  37 ++++
 .../bpf/test_current_pid_tgid_new_ns.c        | 159 ++++++++++++++++++
 4 files changed, 286 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
 create mode 100644 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c
tools/testing/selftests/bpf/Makefile +2 -1
index 2d7f5df33f04..ea77239095a1 100644
@@ -32,7 +32,8 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
 	test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
 	test_cgroup_storage \
 	test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
-	test_progs-no_alu32
+	test_progs-no_alu32 \
+	test_current_pid_tgid_new_ns
 
 # Also test bpf-gcc, if present
 ifneq ($(BPF_GCC),)
tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c new file +88 -0

88 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c new file +37 -0
new file mode 100644
index 000000000000..1dca70a6de2f
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2019 Carlos Neira [email protected] */
+
+#include <linux/bpf.h>
+#include <stdint.h>
+#include <bpf/bpf_helpers.h>
+
+static volatile struct {
+	__u64 dev;
+	__u64 ino;
+	__u64 pid_tgid;
+	__u64 user_pid_tgid;
+} res;
+
+SEC("raw_tracepoint/sys_enter")
+int trace(void *ctx)
+{
+	__u64  ns_pid_tgid, expected_pid;
+	struct bpf_pidns_info nsdata;
+	__u32 key = 0;
+
+	if (bpf_get_ns_current_pid_tgid(res.dev, res.ino, &nsdata,
+		   sizeof(struct bpf_pidns_info)))
+		return 0;
+
+	ns_pid_tgid = (__u64)nsdata.tgid << 32 | nsdata.pid;
+	expected_pid = res.user_pid_tgid;
+
+	if (expected_pid != ns_pid_tgid)
+		return 0;
+
+	res.pid_tgid = ns_pid_tgid;
+
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c new file +159 -0

159 lines not on this page · raw message

Signature · 1 line
2.20.1
#4 lore raw
In reply to Carlos Neira
Quoted 9 lines · Carlos Neira

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.

Quoted 5 lines

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.

Applied. Thanks.
There was one spurious trailing whitespace that I fixed in patch 3 and missing .gitignore update for test_current_pid_tgid_new_ns. Could you please follow up with another patch to fold test_current_pid_tgid_new_ns into test_progs.
I'd really like to consolidate all tests into single binary.

#5 lore raw

2020-03-12 17:45 UTC-0700 ~ Alexei Starovoitov <[email protected]>

Quoted 16 lines · Carlos Neira
Quoted 9 lines

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.

Quoted 5 lines

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.

Quoted 7 lines

Applied. Thanks.
There was one spurious trailing whitespace that I fixed in patch 3 and missing .gitignore update for test_current_pid_tgid_new_ns. Could you please follow up with another patch to fold test_current_pid_tgid_new_ns into test_progs.
I'd really like to consolidate all tests into single binary.

Compiling bpftool (with libbpf now relying on bpf_helper_defs.h generated from helpers documentation), I observe the following warning:

.output/bpf_helper_defs.h:2834:72: warning: declaration of 'struct bpf_pidns_info' will not be visible outside of this function [-Wvisibility] static int (*bpf_get_ns_current_pid_tgid)(__u64 dev, __u64 ino, struct bpf_pidns_info *nsdata, __u32 size) = (void *) 120;

Would it be possible to address this as part of the follow-up too, please? I think the fix would be to add "struct bpf_pidns_info" to type_fds (I see it was added to known_types already) in scripts/bpf_helpers_doc.py.

Thanks,
Quentin

#6 lore raw
Quoted 32 lines · Quentin Monnet

2020-03-12 17:45 UTC-0700 ~ Alexei Starovoitov <[email protected]>

Quoted 16 lines · Carlos Neira
Quoted 9 lines

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.

Quoted 5 lines

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.

Quoted 7 lines

Applied. Thanks.
There was one spurious trailing whitespace that I fixed in patch 3 and missing .gitignore update for test_current_pid_tgid_new_ns. Could you please follow up with another patch to fold test_current_pid_tgid_new_ns into test_progs.
I'd really like to consolidate all tests into single binary.

Compiling bpftool (with libbpf now relying on bpf_helper_defs.h generated from helpers documentation), I observe the following warning:

.output/bpf_helper_defs.h:2834:72: warning: declaration of 'struct bpf_pidns_info' will not be visible outside of this function [-Wvisibility] static int (*bpf_get_ns_current_pid_tgid)(__u64 dev, __u64 ino, struct bpf_pidns_info *nsdata, __u32 size) = (void *) 120;

Quoted 7 lines

Would it be possible to address this as part of the follow-up too, please? I think the fix would be to add "struct bpf_pidns_info" to type_fds (I see it was added to known_types already) in scripts/bpf_helpers_doc.py.

Thanks,
Quentin

Thanks for checking this out Quentin,
I'm sorry I'll start working on this follow-up patch to fix this.

Bests

#7 lore raw
In reply to Alexei Starovoitov
Quoted 16 lines · Alexei Starovoitov
Quoted 9 lines · Carlos Neira

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.

Quoted 5 lines

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.

Quoted 6 lines

Applied. Thanks.
There was one spurious trailing whitespace that I fixed in patch 3 and missing .gitignore update for test_current_pid_tgid_new_ns. Could you please follow up with another patch to fold test_current_pid_tgid_new_ns into test_progs.
I'd really like to consolidate all tests into single binary.

Thank you very much Alexei,
I'll start working on the follow up patch to add test_current_pid_tgid_new_ns into test_progs.

Bests

#8 lore raw
Quoted 25 lines · Carlos Antonio Neira Bustos
Quoted 16 lines · Alexei Starovoitov
Quoted 9 lines · Carlos Neira

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.

Quoted 5 lines

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.

Quoted 6 lines

Applied. Thanks.
There was one spurious trailing whitespace that I fixed in patch 3 and missing .gitignore update for test_current_pid_tgid_new_ns. Could you please follow up with another patch to fold test_current_pid_tgid_new_ns into test_progs.
I'd really like to consolidate all tests into single binary.

Quoted 3 lines

Thank you very much Alexei,
I'll start working on the follow up patch to add test_current_pid_tgid_new_ns into test_progs.

Hey Carlos,

Do you still plan to fold test_current_pid_tgid_new_ns into test_progs?

Quoted 1 line

Bests

A message that is not in the archive (sent off-list or not yet received): CACiB22iSFBybiAn_Z0cspWFLObZy30ZoQHnvH4kFdVsB9dinvQ@mail.gmail.com

#10 lore raw
Quoted 7 lines · carlos antonio neira bustos

Hi,

I’m sorry I’ll do the work needed this week.
Thanks for the heads up.

Bests.

No worries, thanks!

Quoted 40 lines · Andrii Nakryiko
Quoted 37 lines
Quoted 25 lines · Carlos Antonio Neira Bustos
Quoted 16 lines · Alexei Starovoitov
Quoted 9 lines · Carlos Neira

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.

Quoted 5 lines

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.

Quoted 6 lines

Applied. Thanks.
There was one spurious trailing whitespace that I fixed in patch 3 and missing .gitignore update for test_current_pid_tgid_new_ns. Could you please follow up with another patch to fold test_current_pid_tgid_new_ns into test_progs.
I'd really like to consolidate all tests into single binary.

Quoted 3 lines

Thank you very much Alexei,
I'll start working on the follow up patch to add test_current_pid_tgid_new_ns into test_progs.

Hey Carlos,

Do you still plan to fold test_current_pid_tgid_new_ns into test_progs?

Quoted 1 line

Bests