Skip to contentSkip to conversation

introduce new VFS based BPF kfuncs

PATCH Superseded Matt Bobrowski 4 participants 23 messages Jul 26, 2024 – Jul 30, 2024

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover introduce new VFS based BPF kfuncs

G'day!

The original cover letter providing background context and motivating factors around the needs for these new VFS related BPF kfuncs introduced within this patch series can be found here [0]. Please do reference that if needed.

The changes contained within this version of the patch series mainly came at the back of discussions held with Christian at LSFMMBPF recently. In summary, the primary difference within this patch series when compared to the last [1] is that I've reduced the number of VFS related BPF kfuncs being introduced, housed them under fs/, and added more selftests.

Changes since v2 [1]:

* All new VFS related BPF kfuncs now reside in fs/bpf_fs_kfuncs.c rather than kernel/trace/bpf_trace.c. This was something that was explicitly requested by Christian after discussing these new VFS related BPF kfuncs recently at LSFMMBPF.

* Dropped other initially proposed VFS related BPF kfuncs, including bpf_get_mm_exe_file(), bpf_get_task_fs_root(), bpf_get_task_fs_pwd(), and bpf_put_path().

* bpf_path_d_path() now makes use of __sz argument annotations such that the BPF verifier can enforce relevant size checks on the supplied buf that ends up being passed to d_path(). Relevant selftests have been added to assert __sz checking semantics are enforced.

[0] https://lore.kernel.org/bpf/[email protected]/
[1] https://lore.kernel.org/bpf/[email protected]/

Matt Bobrowski (3):
bpf: introduce new VFS based BPF kfuncs selftests/bpf: add negative tests for new VFS based BPF kfuncs selftests/bpf: add positive tests for new VFS based BPF kfuncs

 fs/Makefile                                   |   1 +
 fs/bpf_fs_kfuncs.c                            | 133 ++++++++++++
 .../testing/selftests/bpf/bpf_experimental.h  |  26 +++
 .../selftests/bpf/prog_tests/verifier.c       |   4 +
 .../selftests/bpf/progs/verifier_vfs_accept.c |  71 +++++++
 .../selftests/bpf/progs/verifier_vfs_reject.c | 196 ++++++++++++++++++
 6 files changed, 431 insertions(+)
 create mode 100644 fs/bpf_fs_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_vfs_accept.c
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_vfs_reject.c
Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
#1 lore raw

PATCH v3 1/3 bpf: introduce new VFS based BPF kfuncs

Add a new variant of bpf_d_path() named bpf_path_d_path() which takes the form of a BPF kfunc and enforces KF_TRUSTED_ARGS semantics onto its arguments.

This new d_path() based BPF kfunc variant is intended to address the legacy bpf_d_path() BPF helper's susceptibility to memory corruption issues [0, 1, 2] by ensuring to only operate on supplied arguments which are deemed trusted by the BPF verifier. Typically, this means that only pointers to a struct path which have been referenced counted may be supplied.

In addition to the new bpf_path_d_path() BPF kfunc, we also add a KF_ACQUIRE based BPF kfunc bpf_get_task_exe_file() and KF_RELEASE counterpart BPF kfunc bpf_put_file(). This is so that the new bpf_path_d_path() BPF kfunc can be used more flexibility from within the context of a BPF LSM program. It's rather common to ascertain the backing executable file for the calling process by performing the following walk current->mm->exe_file while instrumenting a given operation from the context of the BPF LSM program. However, walking current->mm->exe_file directly is never deemed to be OK, and doing so from both inside and outside of BPF LSM program context should be considered as a bug. Using bpf_get_task_exe_file() and in turn bpf_put_file() will allow BPF LSM programs to reliably get and put references to current->mm->exe_file.

As of now, all the newly introduced BPF kfuncs within this patch are limited to sleepable BPF LSM program types. Therefore, they may only be called when a BPF LSM program is attached to one of the listed attachment points defined within the sleepable_lsm_hooks BTF ID set.

[0] https://lore.kernel.org/bpf/CAG48ez0ppjcT=QxU-jtCUfb5xQb3mLr=5FcwddF_VKfEBPs_Dg@mail.gmail.com/
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

---
 fs/Makefile        |   1 +
 fs/bpf_fs_kfuncs.c | 133 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)
 create mode 100644 fs/bpf_fs_kfuncs.c
fs/Makefile +1 -0
index 6ecc9b0a53f2..61679fd587b7 100644
@@ -129,3 +129,4 @@ obj-$(CONFIG_EFIVAR_FS)		+= efivarfs/
 obj-$(CONFIG_EROFS_FS)		+= erofs/
 obj-$(CONFIG_VBOXSF_FS)		+= vboxsf/
 obj-$(CONFIG_ZONEFS_FS)		+= zonefs/
+obj-$(CONFIG_BPF_LSM)		+= bpf_fs_kfuncs.o
fs/bpf_fs_kfuncs.c new file +133 -0

133 lines not on this page · raw message

Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
#2 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 134 lines · Matt Bobrowski

Add a new variant of bpf_d_path() named bpf_path_d_path() which takes the form of a BPF kfunc and enforces KF_TRUSTED_ARGS semantics onto its arguments.

This new d_path() based BPF kfunc variant is intended to address the legacy bpf_d_path() BPF helper's susceptibility to memory corruption issues [0, 1, 2] by ensuring to only operate on supplied arguments which are deemed trusted by the BPF verifier. Typically, this means that only pointers to a struct path which have been referenced counted may be supplied.

In addition to the new bpf_path_d_path() BPF kfunc, we also add a KF_ACQUIRE based BPF kfunc bpf_get_task_exe_file() and KF_RELEASE counterpart BPF kfunc bpf_put_file(). This is so that the new bpf_path_d_path() BPF kfunc can be used more flexibility from within the context of a BPF LSM program. It's rather common to ascertain the backing executable file for the calling process by performing the following walk current->mm->exe_file while instrumenting a given operation from the context of the BPF LSM program. However, walking current->mm->exe_file directly is never deemed to be OK, and doing so from both inside and outside of BPF LSM program context should be considered as a bug. Using bpf_get_task_exe_file() and in turn bpf_put_file() will allow BPF LSM programs to reliably get and put references to current->mm->exe_file.

As of now, all the newly introduced BPF kfuncs within this patch are limited to sleepable BPF LSM program types. Therefore, they may only be called when a BPF LSM program is attached to one of the listed attachment points defined within the sleepable_lsm_hooks BTF ID set.

[0] https://lore.kernel.org/bpf/CAG48ez0ppjcT=QxU-jtCUfb5xQb3mLr=5FcwddF_VKfEBPs_Dg@mail.gmail.com/
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

---
 fs/Makefile        |   1 +
 fs/bpf_fs_kfuncs.c | 133 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)
 create mode 100644 fs/bpf_fs_kfuncs.c
fs/Makefile +1 -0
index 6ecc9b0a53f2..61679fd587b7 100644
@@ -129,3 +129,4 @@ obj-$(CONFIG_EFIVAR_FS)		+= efivarfs/
 obj-$(CONFIG_EROFS_FS)		+= erofs/
 obj-$(CONFIG_VBOXSF_FS)		+= vboxsf/
 obj-$(CONFIG_ZONEFS_FS)		+= zonefs/
+obj-$(CONFIG_BPF_LSM)		+= bpf_fs_kfuncs.o
fs/bpf_fs_kfuncs.c new file +78 -0
new file mode 100644
index 000000000000..3813e2a83313
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Google LLC. */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/dcache.h>
+#include <linux/err.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/init.h>
+#include <linux/mm.h>
+#include <linux/path.h>
+#include <linux/sched.h>
+
+__bpf_kfunc_start_defs();
+/**
+ * bpf_get_task_exe_file - get a reference on the exe_file struct file member of
+ *                         the mm_struct that is nested within the supplied
+ *                         task_struct
+ * @task: task_struct of which the nested mm_struct exe_file member to get a
+ * reference on
+ *
+ * Get a reference on the exe_file struct file member field of the mm_struct
+ * nested within the supplied *task*. The referenced file pointer acquired by
+ * this BPF kfunc must be released using bpf_put_file(). Failing to call
+ * bpf_put_file() on the returned referenced struct file pointer that has been
+ * acquired by this BPF kfunc will result in the BPF program being rejected by
+ * the BPF verifier.
+ *
+ * This BPF kfunc may only be called from sleepable BPF LSM programs.
+ *
+ * Internally, this BPF kfunc leans on get_task_exe_file(), such that calling
+ * bpf_get_task_exe_file() would be analogous to calling get_task_exe_file()
+ * directly in kernel context.
+ *
+ * Return: A referenced struct file pointer to the exe_file member of the
+ * mm_struct that is nested within the supplied *task*. On error, NULL is
+ * returned.
+ */
+__bpf_kfunc struct file *bpf_get_task_exe_file(struct task_struct *task)
+{
+	return get_task_exe_file(task);
+}
+
+/**
+ * bpf_put_file - put a reference on the supplied file
+ * @file: file to put a reference on
+ *
+ * Put a reference on the supplied *file*. Only referenced file pointers may be
+ * passed to this BPF kfunc. Attempting to pass an unreferenced file pointer, or
+ * any other arbitrary pointer for that matter, will result in the BPF program
+ * being rejected by the BPF verifier.
+ *
+ * This BPF kfunc may only be called from sleepable BPF LSM programs. Though
+ * fput() can be called from IRQ context, we're enforcing sleepability here.
+ */
+__bpf_kfunc void bpf_put_file(struct file *file)
+{
+	fput(file);
+}
+
+/**
+ * bpf_path_d_path - resolve the pathname for the supplied path
+ * @path: path to resolve the pathname for
+ * @buf: buffer to return the resolved pathname in
+ * @buf__sz: length of the supplied buffer
+ *
+ * Resolve the pathname for the supplied *path* and store it in *buf*. This BPF
+ * kfunc is the safer variant of the legacy bpf_d_path() helper and should be
+ * used in place of bpf_d_path() whenever possible. It enforces KF_TRUSTED_ARGS
+ * semantics, meaning that the supplied *path* must itself hold a valid
+ * reference, or else the BPF program will be outright rejected by the BPF
+ * verifier.
+ *
+ * This BPF kfunc may only be called from sleepable BPF LSM programs.
+ *
+ * Return: A positive integer corresponding to the length of the resolved

fs/bpf_fs_kfuncs.c line 88

Quoted 10 lines
fs/bpf_fs_kfuncs.c new file +10 -0
+ * pathname in *buf*, including the NUL termination character. On error, a
+ * negative integer is returned.
+ */
+__bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)
+{
+	int len;
+	char *ret;
+
+	if (buf__sz <= 0)
+		return -EINVAL;

size_t is unsigned so this should just be !buf__sz I can fix that though. The __sz thing has meaning to the verifier afaict so I guess that's fine as name then.

#3 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 140 lines · Christian Brauner
Quoted 139 lines · Matt Bobrowski

Add a new variant of bpf_d_path() named bpf_path_d_path() which takes the form of a BPF kfunc and enforces KF_TRUSTED_ARGS semantics onto its arguments.

This new d_path() based BPF kfunc variant is intended to address the legacy bpf_d_path() BPF helper's susceptibility to memory corruption issues [0, 1, 2] by ensuring to only operate on supplied arguments which are deemed trusted by the BPF verifier. Typically, this means that only pointers to a struct path which have been referenced counted may be supplied.

In addition to the new bpf_path_d_path() BPF kfunc, we also add a KF_ACQUIRE based BPF kfunc bpf_get_task_exe_file() and KF_RELEASE counterpart BPF kfunc bpf_put_file(). This is so that the new bpf_path_d_path() BPF kfunc can be used more flexibility from within the context of a BPF LSM program. It's rather common to ascertain the backing executable file for the calling process by performing the following walk current->mm->exe_file while instrumenting a given operation from the context of the BPF LSM program. However, walking current->mm->exe_file directly is never deemed to be OK, and doing so from both inside and outside of BPF LSM program context should be considered as a bug. Using bpf_get_task_exe_file() and in turn bpf_put_file() will allow BPF LSM programs to reliably get and put references to current->mm->exe_file.

As of now, all the newly introduced BPF kfuncs within this patch are limited to sleepable BPF LSM program types. Therefore, they may only be called when a BPF LSM program is attached to one of the listed attachment points defined within the sleepable_lsm_hooks BTF ID set.

[0] https://lore.kernel.org/bpf/CAG48ez0ppjcT=QxU-jtCUfb5xQb3mLr=5FcwddF_VKfEBPs_Dg@mail.gmail.com/
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

---
 fs/Makefile        |   1 +
 fs/bpf_fs_kfuncs.c | 133 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)
 create mode 100644 fs/bpf_fs_kfuncs.c
fs/Makefile +1 -0
index 6ecc9b0a53f2..61679fd587b7 100644
@@ -129,3 +129,4 @@ obj-$(CONFIG_EFIVAR_FS)		+= efivarfs/
 obj-$(CONFIG_EROFS_FS)		+= erofs/
 obj-$(CONFIG_VBOXSF_FS)		+= vboxsf/
 obj-$(CONFIG_ZONEFS_FS)		+= zonefs/
+obj-$(CONFIG_BPF_LSM)		+= bpf_fs_kfuncs.o
fs/bpf_fs_kfuncs.c new file +83 -0
new file mode 100644
index 000000000000..3813e2a83313
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Google LLC. */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/dcache.h>
+#include <linux/err.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/init.h>
+#include <linux/mm.h>
+#include <linux/path.h>
+#include <linux/sched.h>
+
+__bpf_kfunc_start_defs();
+/**
+ * bpf_get_task_exe_file - get a reference on the exe_file struct file member of
+ *                         the mm_struct that is nested within the supplied
+ *                         task_struct
+ * @task: task_struct of which the nested mm_struct exe_file member to get a
+ * reference on
+ *
+ * Get a reference on the exe_file struct file member field of the mm_struct
+ * nested within the supplied *task*. The referenced file pointer acquired by
+ * this BPF kfunc must be released using bpf_put_file(). Failing to call
+ * bpf_put_file() on the returned referenced struct file pointer that has been
+ * acquired by this BPF kfunc will result in the BPF program being rejected by
+ * the BPF verifier.
+ *
+ * This BPF kfunc may only be called from sleepable BPF LSM programs.
+ *
+ * Internally, this BPF kfunc leans on get_task_exe_file(), such that calling
+ * bpf_get_task_exe_file() would be analogous to calling get_task_exe_file()
+ * directly in kernel context.
+ *
+ * Return: A referenced struct file pointer to the exe_file member of the
+ * mm_struct that is nested within the supplied *task*. On error, NULL is
+ * returned.
+ */
+__bpf_kfunc struct file *bpf_get_task_exe_file(struct task_struct *task)
+{
+	return get_task_exe_file(task);
+}
+
+/**
+ * bpf_put_file - put a reference on the supplied file
+ * @file: file to put a reference on
+ *
+ * Put a reference on the supplied *file*. Only referenced file pointers may be
+ * passed to this BPF kfunc. Attempting to pass an unreferenced file pointer, or
+ * any other arbitrary pointer for that matter, will result in the BPF program
+ * being rejected by the BPF verifier.
+ *
+ * This BPF kfunc may only be called from sleepable BPF LSM programs. Though
+ * fput() can be called from IRQ context, we're enforcing sleepability here.
+ */
+__bpf_kfunc void bpf_put_file(struct file *file)
+{
+	fput(file);
+}
+
+/**
+ * bpf_path_d_path - resolve the pathname for the supplied path
+ * @path: path to resolve the pathname for
+ * @buf: buffer to return the resolved pathname in
+ * @buf__sz: length of the supplied buffer
+ *
+ * Resolve the pathname for the supplied *path* and store it in *buf*. This BPF
+ * kfunc is the safer variant of the legacy bpf_d_path() helper and should be
+ * used in place of bpf_d_path() whenever possible. It enforces KF_TRUSTED_ARGS
+ * semantics, meaning that the supplied *path* must itself hold a valid
+ * reference, or else the BPF program will be outright rejected by the BPF
+ * verifier.
+ *
+ * This BPF kfunc may only be called from sleepable BPF LSM programs.
+ *
+ * Return: A positive integer corresponding to the length of the resolved
+ * pathname in *buf*, including the NUL termination character. On error, a
+ * negative integer is returned.
+ */
+__bpf_kfunc int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz)
+{
Quoted 8 lines

fs/bpf_fs_kfuncs.c line 88

Quoted 5 lines
fs/bpf_fs_kfuncs.c new file +5 -0
+	int len;
+	char *ret;
+
+	if (buf__sz <= 0)
+		return -EINVAL;

size_t is unsigned so this should just be !buf__sz I can fix that though.

Sure, that would be great if you wouldn't mind?

Quoted 2 lines

The __sz thing has meaning to the verifier afaict so I guess that's fine as name then.

That's right, it's used to signal that a buffer and it's associated size exists within the BPF kfuncs argument list. Using the __sz annotation specifically allows the BPF verifier to deduce which size argument is meant to be bounded to a given buffer.

/M

#4 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

In reply to Matt Bobrowski
Quoted 9 lines · Matt Bobrowski
fs/bpf_fs_kfuncs.c new file +9 -0
+
+static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
+{
+       if (!btf_id_set8_contains(&bpf_fs_kfunc_set_ids, kfunc_id) ||
+           prog->type == BPF_PROG_TYPE_LSM)
+               return 0;
+       return -EACCES;
+}
+

fs/bpf_fs_kfuncs.c line 131

Quoted 10 lines
fs/bpf_fs_kfuncs.c new file +10 -0
+static const struct btf_kfunc_id_set bpf_fs_kfunc_set = {
+       .owner = THIS_MODULE,
+       .set = &bpf_fs_kfunc_set_ids,
+       .filter = bpf_fs_kfuncs_filter,
+};
+
+static int __init bpf_fs_kfuncs_init(void)
+{
+       return register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set);
+}

Aside from buf__sz <= 0 that Christian spotted the bpf_fs_kfuncs_filter() is a watery water. It's doing a redundant check that is already covered by

register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM,...

I'll remove it while applying.

#5 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Matt Bobrowski
fs/bpf_fs_kfuncs.c new file +13 -0
+
+static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
+{
+       if (!btf_id_set8_contains(&bpf_fs_kfunc_set_ids, kfunc_id) ||
+           prog->type == BPF_PROG_TYPE_LSM)
+               return 0;
+       return -EACCES;
+}
+
+static const struct btf_kfunc_id_set bpf_fs_kfunc_set = {
+       .owner = THIS_MODULE,
+       .set = &bpf_fs_kfunc_set_ids,
+       .filter = bpf_fs_kfuncs_filter,
Quoted 8 lines

fs/bpf_fs_kfuncs.c line 131

Quoted 6 lines
fs/bpf_fs_kfuncs.c new file +6 -0
+};
+
+static int __init bpf_fs_kfuncs_init(void)
+{
+       return register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set);
+}

Aside from buf__sz <= 0 that Christian spotted

I'm going to fix this up in v2 of this patch, so don't worry about it.

Quoted 6 lines

the bpf_fs_kfuncs_filter() is a watery water. It's doing a redundant check that is already covered by

register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM,...

I'll remove it while applying.

As discussed, this filter is currently required as without it we inadvertently allow tracing BPF programs to also use these BPF kfuncs.

/M

reviewer
#6 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

In reply to Matt Bobrowski
Quoted 1 line · Matt Bobrowski

[...]

fs/bpf_fs_kfuncs.c line 110

Quoted 10 lines
fs/bpf_fs_kfuncs.c new file +10 -0
+       len = buf + buf__sz - ret;
+       memmove(buf, ret, len);
+       return len;
+}
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
+BTF_ID_FLAGS(func, bpf_get_task_exe_file,
+            KF_ACQUIRE | KF_TRUSTED_ARGS | KF_SLEEPABLE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE | KF_SLEEPABLE)

Do we really need KF_SLEEPABLE for bpf_put_file?

Thanks,
Song

Quoted 3 lines
fs/bpf_fs_kfuncs.c new file +3 -0
+BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS | KF_SLEEPABLE)
+BTF_KFUNCS_END(bpf_fs_kfunc_set_ids)
+

[...]

#7 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 15 lines · Song Liu
Quoted 1 line · Matt Bobrowski

[...]

fs/bpf_fs_kfuncs.c line 110

Quoted 10 lines
fs/bpf_fs_kfuncs.c new file +10 -0
+       len = buf + buf__sz - ret;
+       memmove(buf, ret, len);
+       return len;
+}
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
+BTF_ID_FLAGS(func, bpf_get_task_exe_file,
+            KF_ACQUIRE | KF_TRUSTED_ARGS | KF_SLEEPABLE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE | KF_SLEEPABLE)

Do we really need KF_SLEEPABLE for bpf_put_file?

Well, the guts of fput() is annotated w/ might_sleep(), so the calling thread may presumably be involuntarily put to sleep? You can also see the guts of fput() invoking various indirect function calls i.e. ->release(), and depending on the implementation of those, they could be initiating resource release related actions which consequently could result in waiting for some I/O to be done? fput() also calls dput() and mntput() and these too can also do a bunch of teardown.

Please correct me if I've misunderstood something.

/M

reviewer
#8 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 27 lines · Matt Bobrowski
Quoted 15 lines · Song Liu
Quoted 1 line · Matt Bobrowski

[...]

fs/bpf_fs_kfuncs.c line 110

Quoted 10 lines
fs/bpf_fs_kfuncs.c new file +10 -0
+       len = buf + buf__sz - ret;
+       memmove(buf, ret, len);
+       return len;
+}
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
+BTF_ID_FLAGS(func, bpf_get_task_exe_file,
+            KF_ACQUIRE | KF_TRUSTED_ARGS | KF_SLEEPABLE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE | KF_SLEEPABLE)

Do we really need KF_SLEEPABLE for bpf_put_file?

Well, the guts of fput() is annotated w/ might_sleep(), so the calling thread may presumably be involuntarily put to sleep? You can also see the guts of fput() invoking various indirect function calls i.e. ->release(), and depending on the implementation of those, they could be initiating resource release related actions which consequently could result in waiting for some I/O to be done? fput() also calls dput() and mntput() and these too can also do a bunch of teardown.

Quoted 1 line

Please correct me if I've misunderstood something.

__fput() is annotated with might_sleep(). However, fput() doesn't not call __fput() directly. Instead, it schedules a worker to call __fput(). Therefore, it is safe to call fput() from a non-sleepable context.

Thanks,
Song

#9 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 28 lines · Song Liu
Quoted 27 lines · Matt Bobrowski
Quoted 15 lines · Song Liu
Quoted 1 line · Matt Bobrowski

[...]

fs/bpf_fs_kfuncs.c line 110

Quoted 10 lines
fs/bpf_fs_kfuncs.c new file +10 -0
+       len = buf + buf__sz - ret;
+       memmove(buf, ret, len);
+       return len;
+}
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
+BTF_ID_FLAGS(func, bpf_get_task_exe_file,
+            KF_ACQUIRE | KF_TRUSTED_ARGS | KF_SLEEPABLE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE | KF_SLEEPABLE)

Do we really need KF_SLEEPABLE for bpf_put_file?

Well, the guts of fput() is annotated w/ might_sleep(), so the calling thread may presumably be involuntarily put to sleep? You can also see the guts of fput() invoking various indirect function calls i.e. ->release(), and depending on the implementation of those, they could be initiating resource release related actions which consequently could result in waiting for some I/O to be done? fput() also calls dput() and mntput() and these too can also do a bunch of teardown.

Quoted 5 lines
Quoted 1 line

Please correct me if I've misunderstood something.

__fput() is annotated with might_sleep(). However, fput() doesn't not call __fput() directly. Instead, it schedules a worker to call __fput(). Therefore, it is safe to call fput() from a non-sleepable context.

Oh, yes, you're absolutely right. I failed to realize that, so my apologies. In that case, yes, technically bpf_put_file() does not need to be annotated w/ KF_SLEEPABLE. Now that I also think of it, one of the other and only reasons why we made this initially sleepable is because bpf_put_file() at the time was meant to be used exclusively within the same context as bpf_path_d_path(), and that is currently marked as sleepable. Although technically speaking, I think we could also make bpf_path_d_path() not restricted to only sleepable BPF LSM program types, and in turn that could mean that bpf_get_task_exe_file() also doesn't need to be restricted to sleepable BPF LSM programs.

Alexei, what do you think about relaxing the sleepable annotation across this entire set of BPF kfuncs? From a technical perspective I think it's OK, but I'd also like someone like Christian to confirm that d_path() can't actually end up sleeping. Glancing over it, I believe this to be true, but I may also be naively missing something.

/M

#10 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 2 lines

think it's OK, but I'd also like someone like Christian to confirm that d_path() can't actually end up sleeping. Glancing over it, I

We annotated ->d_dname() as non-sleepable in locking.rst so even ->d_dname() shouldn't and curently doesn't sleep. There's a few codepaths that end up calling d_path() under spinlocks but none of them should end up calling anything related to ->d_name() and so wouldn't be affected even if it did sleep.

#11 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 8 lines · Christian Brauner
Quoted 2 lines

think it's OK, but I'd also like someone like Christian to confirm that d_path() can't actually end up sleeping. Glancing over it, I

We annotated ->d_dname() as non-sleepable in locking.rst so even ->d_dname() shouldn't and curently doesn't sleep. There's a few codepaths that end up calling d_path() under spinlocks but none of them should end up calling anything related to ->d_name() and so wouldn't be affected even if it did sleep.

Wonderful, exactly what I had also concluded. In that case, I think we can relax the sleepable requirement across this suite of BPF kfuncs. Does anyone object?

/M

reviewer
#12 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

In reply to Matt Bobrowski
Quoted 31 lines · Matt Bobrowski

Add a new variant of bpf_d_path() named bpf_path_d_path() which takes the form of a BPF kfunc and enforces KF_TRUSTED_ARGS semantics onto its arguments.

This new d_path() based BPF kfunc variant is intended to address the legacy bpf_d_path() BPF helper's susceptibility to memory corruption issues [0, 1, 2] by ensuring to only operate on supplied arguments which are deemed trusted by the BPF verifier. Typically, this means that only pointers to a struct path which have been referenced counted may be supplied.

In addition to the new bpf_path_d_path() BPF kfunc, we also add a KF_ACQUIRE based BPF kfunc bpf_get_task_exe_file() and KF_RELEASE counterpart BPF kfunc bpf_put_file(). This is so that the new bpf_path_d_path() BPF kfunc can be used more flexibility from within the context of a BPF LSM program. It's rather common to ascertain the backing executable file for the calling process by performing the following walk current->mm->exe_file while instrumenting a given operation from the context of the BPF LSM program. However, walking current->mm->exe_file directly is never deemed to be OK, and doing so from both inside and outside of BPF LSM program context should be considered as a bug. Using bpf_get_task_exe_file() and in turn bpf_put_file() will allow BPF LSM programs to reliably get and put references to current->mm->exe_file.

As of now, all the newly introduced BPF kfuncs within this patch are limited to sleepable BPF LSM program types. Therefore, they may only be called when a BPF LSM program is attached to one of the listed attachment points defined within the sleepable_lsm_hooks BTF ID set.

Quoted 5 lines

[0] https://lore.kernel.org/bpf/CAG48ez0ppjcT=QxU-jtCUfb5xQb3mLr=5FcwddF_VKfEBPs_Dg@mail.gmail.com/
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

checkpatch reported a few syntax issues on this one:

https://netdev.bots.linux.dev/static/nipa/874023/13742510/checkpatch/stdout

#13 lore raw

Re: [PATCH v3 bpf-next 1/3] bpf: introduce new VFS based BPF kfuncs

Quoted 36 lines · Song Liu
Quoted 35 lines · Matt Bobrowski

Add a new variant of bpf_d_path() named bpf_path_d_path() which takes the form of a BPF kfunc and enforces KF_TRUSTED_ARGS semantics onto its arguments.

This new d_path() based BPF kfunc variant is intended to address the legacy bpf_d_path() BPF helper's susceptibility to memory corruption issues [0, 1, 2] by ensuring to only operate on supplied arguments which are deemed trusted by the BPF verifier. Typically, this means that only pointers to a struct path which have been referenced counted may be supplied.

In addition to the new bpf_path_d_path() BPF kfunc, we also add a KF_ACQUIRE based BPF kfunc bpf_get_task_exe_file() and KF_RELEASE counterpart BPF kfunc bpf_put_file(). This is so that the new bpf_path_d_path() BPF kfunc can be used more flexibility from within the context of a BPF LSM program. It's rather common to ascertain the backing executable file for the calling process by performing the following walk current->mm->exe_file while instrumenting a given operation from the context of the BPF LSM program. However, walking current->mm->exe_file directly is never deemed to be OK, and doing so from both inside and outside of BPF LSM program context should be considered as a bug. Using bpf_get_task_exe_file() and in turn bpf_put_file() will allow BPF LSM programs to reliably get and put references to current->mm->exe_file.

As of now, all the newly introduced BPF kfuncs within this patch are limited to sleepable BPF LSM program types. Therefore, they may only be called when a BPF LSM program is attached to one of the listed attachment points defined within the sleepable_lsm_hooks BTF ID set.

[0] https://lore.kernel.org/bpf/CAG48ez0ppjcT=QxU-jtCUfb5xQb3mLr=5FcwddF_VKfEBPs_Dg@mail.gmail.com/
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Quoted 5 lines
Quoted 1 line

checkpatch reported a few syntax issues on this one:

https://netdev.bots.linux.dev/static/nipa/874023/13742510/checkpatch/stdout

Thanks for making aware, all has been addressed.

/M

#14 lore raw

PATCH v3 2/3 selftests/bpf: add negative tests for new VFS based BPF kfuncs

In reply to Matt Bobrowski

Add a bunch of negative selftests responsible for asserting that the BPF verifier successfully rejects a BPF program load when the underlying BPF program misuses one of the newly introduced VFS based BPF kfuncs.

The following VFS based BPF kfuncs are extensively tested within this new selftest:

* struct file *bpf_get_task_exe_file(struct task_struct *);
* void bpf_put_file(struct file *);
* int bpf_path_d_path(struct path *, char *, size_t);

---
 .../testing/selftests/bpf/bpf_experimental.h  |  26 +++
 .../selftests/bpf/prog_tests/verifier.c       |   2 +
 .../selftests/bpf/progs/verifier_vfs_reject.c | 196 ++++++++++++++++++
 3 files changed, 224 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_vfs_reject.c
tools/testing/selftests/bpf/bpf_experimental.h +26 -0
index 828556cdc2f0..8a1ed62b4ed1 100644
@@ -195,6 +195,32 @@ extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym;
  */
 extern void bpf_throw(u64 cookie) __ksym;
 
+/* Description
+ *	Acquire a reference on the exe_file member field belonging to the
+ *	mm_struct that is nested within the supplied task_struct. The supplied
+ *	task_struct must be trusted/referenced.
+ * Returns
+ *	A referenced file pointer pointing to the exe_file member field of the
+ *	mm_struct nested in the supplied task_struct, or NULL.
+ */
+extern struct file *bpf_get_task_exe_file(struct task_struct *task) __ksym;
+
+/* Description
+ *	Release a reference on the supplied file. The supplied file must be
+ *	trusted/referenced.
+ */
+extern void bpf_put_file(struct file *file) __ksym;
+
+/* Description
+ *	Resolve a pathname for the supplied path and store it in the supplied
+ *	buffer. The supplied path must be trusted/referenced.
+ * Returns
+ *	A positive integer corresponding to the length of the resolved pathname,
+ *	including the NULL termination character, stored in the supplied
+ *	buffer. On error, a negative integer is returned.
+ */
+extern int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) __ksym;
+
 /* This macro must be used to mark the exception callback corresponding to the
  * main program. For example:
  *
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 67a49d12472c..14d74ba2188e 100644
@@ -85,6 +85,7 @@
 #include "verifier_value_or_null.skel.h"
 #include "verifier_value_ptr_arith.skel.h"
 #include "verifier_var_off.skel.h"
+#include "verifier_vfs_reject.skel.h"
 #include "verifier_xadd.skel.h"
 #include "verifier_xdp.skel.h"
 #include "verifier_xdp_direct_packet_access.skel.h"
@@ -205,6 +206,7 @@ void test_verifier_value(void)                { RUN(verifier_value); }
 void test_verifier_value_illegal_alu(void)    { RUN(verifier_value_illegal_alu); }
 void test_verifier_value_or_null(void)        { RUN(verifier_value_or_null); }
 void test_verifier_var_off(void)              { RUN(verifier_var_off); }
+void test_verifier_vfs_reject(void)	      { RUN(verifier_vfs_reject); }
 void test_verifier_xadd(void)                 { RUN(verifier_xadd); }
 void test_verifier_xdp(void)                  { RUN(verifier_xdp); }
 void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); }
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
reviewer Acked-by on v3 2/3
#15 lore raw

Re: [PATCH v3 bpf-next 2/3] selftests/bpf: add negative tests for new VFS based BPF kfuncs

Quoted 5 lines · Matt Bobrowski

Add a bunch of negative selftests responsible for asserting that the BPF verifier successfully rejects a BPF program load when the underlying BPF program misuses one of the newly introduced VFS based BPF kfuncs.

Negative tests are great. Thanks for adding them.

A few nitpicks below.

[...]

Quoted 11 lines
tools/testing/selftests/bpf/bpf_experimental.h +3 -0
index 828556cdc2f0..8a1ed62b4ed1 100644
@@ -195,6 +195,32 @@ extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym;
  */
 extern void bpf_throw(u64 cookie) __ksym;
 
+/* Description
+ *     Acquire a reference on the exe_file member field belonging to the
+ *     mm_struct that is nested within the supplied task_struct. The supplied

tools/testing/selftests/bpf/bpf_experimental.h extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym; · line 210

Quoted 10 lines
tools/testing/selftests/bpf/bpf_experimental.h +10 -0
+ *     task_struct must be trusted/referenced.
+ * Returns
+ *     A referenced file pointer pointing to the exe_file member field of the
+ *     mm_struct nested in the supplied task_struct, or NULL.
+ */
+extern struct file *bpf_get_task_exe_file(struct task_struct *task) __ksym;
+
+/* Description
+ *     Release a reference on the supplied file. The supplied file must be
+ *     trusted/referenced.

Probably replace "trusted/referenced" with "acquired".

tools/testing/selftests/bpf/bpf_experimental.h extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym; · line 223

Quoted 13 lines
tools/testing/selftests/bpf/bpf_experimental.h +13 -0
+ */
+extern void bpf_put_file(struct file *file) __ksym;
+
+/* Description
+ *     Resolve a pathname for the supplied path and store it in the supplied
+ *     buffer. The supplied path must be trusted/referenced.
+ * Returns
+ *     A positive integer corresponding to the length of the resolved pathname,
+ *     including the NULL termination character, stored in the supplied
+ *     buffer. On error, a negative integer is returned.
+ */
+extern int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) __ksym;
+

In my environment, we already have these declarations in vmlinux.h. So maybe we don't need to add them manually?

Quoted 54 lines
tools/testing/selftests/bpf/bpf_experimental.h +0 -0
 /* This macro must be used to mark the exception callback corresponding to the
  * main program. For example:
  *
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 67a49d12472c..14d74ba2188e 100644
@@ -85,6 +85,7 @@
 #include "verifier_value_or_null.skel.h"
 #include "verifier_value_ptr_arith.skel.h"
 #include "verifier_var_off.skel.h"
+#include "verifier_vfs_reject.skel.h"
 #include "verifier_xadd.skel.h"
 #include "verifier_xdp.skel.h"
 #include "verifier_xdp_direct_packet_access.skel.h"
@@ -205,6 +206,7 @@ void test_verifier_value(void)                { RUN(verifier_value); }
 void test_verifier_value_illegal_alu(void)    { RUN(verifier_value_illegal_alu); }
 void test_verifier_value_or_null(void)        { RUN(verifier_value_or_null); }
 void test_verifier_var_off(void)              { RUN(verifier_var_off); }
+void test_verifier_vfs_reject(void)          { RUN(verifier_vfs_reject); }
 void test_verifier_xadd(void)                 { RUN(verifier_xadd); }
 void test_verifier_xdp(void)                  { RUN(verifier_xdp); }
 void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); }
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c new file +25 -0
new file mode 100644
index 000000000000..27666a8ef78a
@@ -0,0 +1,196 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Google LLC. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <linux/limits.h>
+
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+static char buf[PATH_MAX];
+
+SEC("lsm.s/file_open")
+__failure __msg("Possibly NULL pointer passed to trusted arg0")
+int BPF_PROG(get_task_exe_file_kfunc_null)
+{
+       struct file *acquired;
+
+       /* Can't pass a NULL pointer to bpf_get_task_exe_file(). */
+       acquired = bpf_get_task_exe_file(NULL);
+       if (!acquired)
+               return 0;
+
+       bpf_put_file(acquired);

tools/testing/selftests/bpf/progs/verifier_vfs_reject.c line 35

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c new file +10 -0
+       return 0;
+}
+
+SEC("lsm.s/inode_getxattr")
+__failure __msg("arg#0 pointer type STRUCT task_struct must point to scalar, or struct with scalar")
+int BPF_PROG(get_task_exe_file_kfunc_fp)
+{
+       u64 x;
+       struct file *acquired;
+       struct task_struct *fp;

"fp" is a weird name for a task_struct pointer.

Other than these:

Quoted 11 lines
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c new file +11 -0
+
+       fp = (struct task_struct *)&x;
+       /* Can't pass random frame pointer to bpf_get_task_exe_file(). */
+       acquired = bpf_get_task_exe_file(fp);
+       if (!acquired)
+               return 0;
+
+       bpf_put_file(acquired);
+       return 0;
+}
+

[...]

#16 lore raw

Re: [PATCH v3 bpf-next 2/3] selftests/bpf: add negative tests for new VFS based BPF kfuncs

Quoted 10 lines · Song Liu
Quoted 5 lines · Matt Bobrowski

Add a bunch of negative selftests responsible for asserting that the BPF verifier successfully rejects a BPF program load when the underlying BPF program misuses one of the newly introduced VFS based BPF kfuncs.

Negative tests are great. Thanks for adding them.

A few nitpicks below.

Thanks for the review!

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/bpf_experimental.h +0 -0
index 828556cdc2f0..8a1ed62b4ed1 100644
@@ -195,6 +195,32 @@ extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym;
  */
 extern void bpf_throw(u64 cookie) __ksym;
 
Quoted 15 lines

tools/testing/selftests/bpf/bpf_experimental.h extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym; · line 210

Quoted 13 lines
tools/testing/selftests/bpf/bpf_experimental.h +13 -0
+/* Description
+ *     Acquire a reference on the exe_file member field belonging to the
+ *     mm_struct that is nested within the supplied task_struct. The supplied
+ *     task_struct must be trusted/referenced.
+ * Returns
+ *     A referenced file pointer pointing to the exe_file member field of the
+ *     mm_struct nested in the supplied task_struct, or NULL.
+ */
+extern struct file *bpf_get_task_exe_file(struct task_struct *task) __ksym;
+
+/* Description
+ *     Release a reference on the supplied file. The supplied file must be
+ *     trusted/referenced.

Probably replace "trusted/referenced" with "acquired".

Done.

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/bpf_experimental.h +8 -0
+ */
+extern void bpf_put_file(struct file *file) __ksym;
+
+/* Description
+ *     Resolve a pathname for the supplied path and store it in the supplied
+ *     buffer. The supplied path must be trusted/referenced.
+ * Returns
+ *     A positive integer corresponding to the length of the resolved pathname,
Quoted 8 lines

tools/testing/selftests/bpf/bpf_experimental.h extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym; · line 223

Quoted 5 lines
tools/testing/selftests/bpf/bpf_experimental.h +5 -0
+ *     including the NULL termination character, stored in the supplied
+ *     buffer. On error, a negative integer is returned.
+ */
+extern int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) __ksym;
+

In my environment, we already have these declarations in vmlinux.h. So maybe we don't need to add them manually?

Right, but that's probably when building vmlinux.h using the latest pahole I imagine? Those not using the latest pahole will probably won't already see these BPF kfuncs within the generated vmlinux.h.

Quoted 58 lines
Quoted 58 lines
tools/testing/selftests/bpf/bpf_experimental.h +0 -0
 /* This macro must be used to mark the exception callback corresponding to the
  * main program. For example:
  *
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 67a49d12472c..14d74ba2188e 100644
@@ -85,6 +85,7 @@
 #include "verifier_value_or_null.skel.h"
 #include "verifier_value_ptr_arith.skel.h"
 #include "verifier_var_off.skel.h"
+#include "verifier_vfs_reject.skel.h"
 #include "verifier_xadd.skel.h"
 #include "verifier_xdp.skel.h"
 #include "verifier_xdp_direct_packet_access.skel.h"
@@ -205,6 +206,7 @@ void test_verifier_value(void)                { RUN(verifier_value); }
 void test_verifier_value_illegal_alu(void)    { RUN(verifier_value_illegal_alu); }
 void test_verifier_value_or_null(void)        { RUN(verifier_value_or_null); }
 void test_verifier_var_off(void)              { RUN(verifier_var_off); }
+void test_verifier_vfs_reject(void)          { RUN(verifier_vfs_reject); }
 void test_verifier_xadd(void)                 { RUN(verifier_xadd); }
 void test_verifier_xdp(void)                  { RUN(verifier_xdp); }
 void test_verifier_xdp_direct_packet_access(void) { RUN(verifier_xdp_direct_packet_access); }
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c new file +29 -0
new file mode 100644
index 000000000000..27666a8ef78a
@@ -0,0 +1,196 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Google LLC. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <linux/limits.h>
+
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+
+static char buf[PATH_MAX];
+
+SEC("lsm.s/file_open")
+__failure __msg("Possibly NULL pointer passed to trusted arg0")
+int BPF_PROG(get_task_exe_file_kfunc_null)
+{
+       struct file *acquired;
+
+       /* Can't pass a NULL pointer to bpf_get_task_exe_file(). */
+       acquired = bpf_get_task_exe_file(NULL);
+       if (!acquired)
+               return 0;
+
+       bpf_put_file(acquired);
+       return 0;
+}
+
+SEC("lsm.s/inode_getxattr")
Quoted 8 lines

tools/testing/selftests/bpf/progs/verifier_vfs_reject.c line 35

Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c new file +6 -0
+__failure __msg("arg#0 pointer type STRUCT task_struct must point to scalar, or struct with scalar")
+int BPF_PROG(get_task_exe_file_kfunc_fp)
+{
+       u64 x;
+       struct file *acquired;
+       struct task_struct *fp;

"fp" is a weird name for a task_struct pointer.

OK, just want to make it clear that it was a pointer to something that exists on the current stack frame. Happy to change the name to task or something. Done.

Quoted 4 lines

Other than these:

Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c new file +11 -0
+
+       fp = (struct task_struct *)&x;
+       /* Can't pass random frame pointer to bpf_get_task_exe_file(). */
+       acquired = bpf_get_task_exe_file(fp);
+       if (!acquired)
+               return 0;
+
+       bpf_put_file(acquired);
+       return 0;
+}
+

[...]

/M

#17 lore raw

PATCH v3 3/3 selftests/bpf: add positive tests for new VFS based BPF kfuncs

In reply to Matt Bobrowski

Add a bunch of positive selftests which extensively cover the various contexts and parameters in which the new VFS based BPF kfuncs may be used from.

Again, the following VFS based BPF kfuncs are thoroughly tested within this new selftest:
* struct file *bpf_get_task_exe_file(struct task_struct *);
* void bpf_put_file(struct file *);
* int bpf_path_d_path(struct path *, char *, size_t);

---
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../selftests/bpf/progs/verifier_vfs_accept.c | 71 +++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_vfs_accept.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
index 14d74ba2188e..f8f546eba488 100644
@@ -85,6 +85,7 @@
 #include "verifier_value_or_null.skel.h"
 #include "verifier_value_ptr_arith.skel.h"
 #include "verifier_var_off.skel.h"
+#include "verifier_vfs_accept.skel.h"
 #include "verifier_vfs_reject.skel.h"
 #include "verifier_xadd.skel.h"
 #include "verifier_xdp.skel.h"
@@ -206,6 +207,7 @@ void test_verifier_value(void)                { RUN(verifier_value); }
 void test_verifier_value_illegal_alu(void)    { RUN(verifier_value_illegal_alu); }
 void test_verifier_value_or_null(void)        { RUN(verifier_value_or_null); }
 void test_verifier_var_off(void)              { RUN(verifier_var_off); }
+void test_verifier_vfs_accept(void)	      { RUN(verifier_vfs_accept); }
 void test_verifier_vfs_reject(void)	      { RUN(verifier_vfs_reject); }
 void test_verifier_xadd(void)                 { RUN(verifier_xadd); }
 void test_verifier_xdp(void)                  { RUN(verifier_xdp); }
tools/testing/selftests/bpf/progs/verifier_vfs_accept.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
reviewer Acked-by on v3 3/3
#18 lore raw

Re: [PATCH v3 bpf-next 3/3] selftests/bpf: add positive tests for new VFS based BPF kfuncs

Quoted 5 lines · Matt Bobrowski

Add a bunch of positive selftests which extensively cover the various contexts and parameters in which the new VFS based BPF kfuncs may be used from.

Quoted 7 lines

Again, the following VFS based BPF kfuncs are thoroughly tested within this new selftest:
* struct file *bpf_get_task_exe_file(struct task_struct *);
* void bpf_put_file(struct file *);
* int bpf_path_d_path(struct path *, char *, size_t);

#19 lore raw
In reply to Matt Bobrowski
Quoted 7 lines · Matt Bobrowski

G'day!

The original cover letter providing background context and motivating factors around the needs for these new VFS related BPF kfuncs introduced within this patch series can be found here [0]. Please do reference that if needed.

Quoted 6 lines

The changes contained within this version of the patch series mainly came at the back of discussions held with Christian at LSFMMBPF recently. In summary, the primary difference within this patch series when compared to the last [1] is that I've reduced the number of VFS related BPF kfuncs being introduced, housed them under fs/, and added more selftests.

I have no complaints about this now that it's been boiled down. So as far as I'm concerned I'm happy to pick this up. (I also wouldn't mind follow-up patches that move the xattr bpf kfuncs under fs/ as well.)

#20 lore raw
Quoted 15 lines · Christian Brauner
Quoted 7 lines · Matt Bobrowski

G'day!

The original cover letter providing background context and motivating factors around the needs for these new VFS related BPF kfuncs introduced within this patch series can be found here [0]. Please do reference that if needed.

Quoted 6 lines

The changes contained within this version of the patch series mainly came at the back of discussions held with Christian at LSFMMBPF recently. In summary, the primary difference within this patch series when compared to the last [1] is that I've reduced the number of VFS related BPF kfuncs being introduced, housed them under fs/, and added more selftests.

Quoted 4 lines

I have no complaints about this now that it's been boiled down. So as far as I'm concerned I'm happy to pick this up. (I also wouldn't mind follow-up patches that move the xattr bpf kfuncs under fs/ as well.)

Wonderful, thank you Christian!

I agree, those should also reside in alongside these newly added BPF kfuncs. I'll send through a patch addressing this separately. Generally, I think the same applies for any other VFS related BPF kfuncs that end up getting introduced moving forward.

/M

#21 lore raw
In reply to Christian Brauner
Quoted 16 lines · Christian Brauner
Quoted 7 lines · Matt Bobrowski

G'day!

The original cover letter providing background context and motivating factors around the needs for these new VFS related BPF kfuncs introduced within this patch series can be found here [0]. Please do reference that if needed.

Quoted 6 lines

The changes contained within this version of the patch series mainly came at the back of discussions held with Christian at LSFMMBPF recently. In summary, the primary difference within this patch series when compared to the last [1] is that I've reduced the number of VFS related BPF kfuncs being introduced, housed them under fs/, and added more selftests.

Quoted 2 lines

I have no complaints about this now that it's been boiled down. So as far as I'm concerned I'm happy to pick this up.

We very much prefer to go standard route via bpf-next
like we do for all kfuncs to avoid conflicts in selftests, and where these patches will be actively tested by CI and developers.

So please provide an Ack.
I can fix up <= while applying.

Quoted 3 lines

(I also wouldn't
mind follow-up patches that move the xattr bpf kfuncs under fs/ as well.)

np. I'm sure Song can move xattr kfunc to this newly added file.