Skip to contentSkip to conversation

bpf: Allow bpf_d_path() from filp_close_sync()

PATCH Superseded Ihor Solodrai 4 participants 6 messages Sep 24 – Sep 25

Subsystems (7)
VFS Kselftest BPF Tracing +3 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ihor Solodrai

author reviewer
#0 lore raw

PATCH 1/1 bpf: Allow bpf_d_path() from filp_close_sync()

The d_path selftest closes its descriptors with close_range() so that its fentry program on filp_close() runs. The vfs commit 46ace7e4dc4b ("fs: make close_range() synchronous") switched close_range() to filp_close_sync(), so the program still attaches but is never called:

test_d_path_basic:FAIL:close trampoline for filp_close was not called

With that series, close(), close_range() and the closes on exec and exit all go through filp_close_sync(). Few paths still reach filp_close(), dup2() being one of them, so a program on filp_close() no longer sees most file closes.

Allow bpf_d_path() from filp_close_sync(). It takes the same arguments as filp_close() and the program runs before the file is flushed and its last reference is dropped, so file->f_path is still valid. Keep filp_close() in the allowlist for existing programs.

Attach the selftest program to filp_close_sync() and keep triggering it with close_range(), since filp_close_sync() may be inlined into close().

Assisted-by LLM

---

The issue was found by BPF CI when running on linux-next:
https://github.com/kernel-patches/bpf/actions/runs/35923662147/job/107398237366

---
 kernel/trace/bpf_trace.c                        | 1 +
 tools/testing/selftests/bpf/prog_tests/d_path.c | 7 ++++---
 tools/testing/selftests/bpf/progs/test_d_path.c | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)
kernel/trace/bpf_trace.c +1 -0
index 29260951aa87..09a27810ca27 100644
@@ -972,6 +972,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, filp_close_sync)
 BTF_SET_END(btf_allowlist_d_path)
 
 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
tools/testing/selftests/bpf/prog_tests/d_path.c +4 -3
index 1a2a2f1abf03..7665e1d28a64 100644
@@ -109,8 +109,9 @@ static int trigger_fstat_events(pid_t pid)
 	fstat(indicatorfd, &fileStat);
 
 out_close:
-	/* sys_close no longer triggers filp_close, but we can
-	 * call sys_close_range instead which still does
+	/*
+	 * filp_close_sync() may be inlined into close(2), so use
+	 * close_range(2), which calls it directly.
 	 */
 	syscall_close(pipefd[0]);
 	syscall_close(pipefd[1]);
@@ -165,7 +166,7 @@ static void test_d_path_basic(void)
 
 	if (CHECK(!bss->called_close,
 		  "close",
-		  "trampoline for filp_close was not called\n"))
+		  "trampoline for filp_close_sync was not called\n"))
 		goto cleanup;
 
 	for (int i = 0; i < MAX_FILES; i++) {
tools/testing/selftests/bpf/progs/test_d_path.c +1 -1
index 561b2f861808..edb5494a47f7 100644
@@ -41,7 +41,7 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
 	return 0;
 }
 
-SEC("fentry/filp_close")
+SEC("fentry/filp_close_sync")
 int BPF_PROG(prog_close, struct file *file, void *id)
 {
 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] bpf: missing filp_close_sync dependency breaks resolve_btfids and selftest - [Low] bpf: failure to…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] bpf: missing filp_close_sync dependency breaks resolve_btfids and selftest
- [Low] bpf: failure to migrate CHECK() to ASSERT_*() when updating selftest --

commit f620a29b55750df4341321e76f9065c834f91fa3
Author: Ihor Solodrai <[email protected]>

bpf: Allow bpf_d_path() from filp_close_sync()

The d_path selftest closes its descriptors with close_range() so that its fentry program on filp_close() runs. The vfs commit 46ace7e4dc4b ("fs: make close_range() synchronous") switched close_range() to filp_close_sync(), so the program still attaches but is never called. This commit allows bpf_d_path() from filp_close_sync() and updates the selftest.

kernel/trace/bpf_trace.c BTF_ID(func, vfs_fallocate) · line 975

Quoted 9 lines
kernel/trace/bpf_trace.c +1 -0
index 29260951aa871..09a27810ca27f 100644
@@ -972,6 +972,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, filp_close_sync)

[Severity: High]
Does filp_close_sync exist in the current tree? Since the referenced VFS commit (46ace7e4dc4b) might not be present yet, adding its BTF ID here will break the kernel build during resolve_btfids due to a missing dependency.

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index 1a2a2f1abf033..7665e1d28a641 100644

[ ... ]

tools/testing/selftests/bpf/prog_tests/d_path.c static void test_d_path_basic(void) · line 169

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +1 -1
@@ -165,7 +166,7 @@ static void test_d_path_basic(void)
 
 	if (CHECK(!bss->called_close,
 		  "close",
-		  "trampoline for filp_close was not called\n"))
+		  "trampoline for filp_close_sync was not called\n"))

[Severity: Low]
Since this CHECK() macro in test_d_path_basic() is being modified, shouldn't it be migrated to the modern ASSERT_*() macros? BPF subsystem guidelines require replacing deprecated CHECK() macros when updating existing tests.

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
 		goto cleanup;
 
 	for (int i = 0; i < MAX_FILES; i++) {

[ ... ]

tools/testing/selftests/bpf/progs/test_d_path.c int BPF_PROG(prog_stat, struct path *path, struct kstat *stat, · line 44

Quoted 10 lines
tools/testing/selftests/bpf/progs/test_d_path.c +1 -1
index 561b2f861808e..edb5494a47f74 100644
@@ -41,7 +41,7 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
 	return 0;
 }
 
-SEC("fentry/filp_close")
+SEC("fentry/filp_close_sync")

[Severity: High]
Will the test load fail unconditionally here? Attaching a BPF program to fentry/filp_close_sync when the function doesn't yet exist in the tree will cause an error. Applying this patch without its VFS dependency introduces a cross-tree dependency failure regression.

Quoted 3 lines
tools/testing/selftests/bpf/progs/test_d_path.c +0 -0
 int BPF_PROG(prog_close, struct file *file, void *id)
 {
 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#2 lore raw
Quoted 17 lines · On 9/24/26 1:52 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] bpf: missing filp_close_sync dependency breaks resolve_btfids and selftest
- [Low] bpf: failure to migrate CHECK() to ASSERT_*() when updating selftest --

commit f620a29b55750df4341321e76f9065c834f91fa3
Author: Ihor Solodrai <[email protected]>

bpf: Allow bpf_d_path() from filp_close_sync()

The d_path selftest closes its descriptors with close_range() so that its fentry program on filp_close() runs. The vfs commit 46ace7e4dc4b ("fs: make close_range() synchronous") switched close_range() to filp_close_sync(), so the program still attaches but is never called. This commit allows bpf_d_path() from filp_close_sync() and updates the selftest.

Quoted 14 lines

kernel/trace/bpf_trace.c BTF_ID(func, vfs_fallocate) · line 975

Quoted 9 lines
kernel/trace/bpf_trace.c +1 -0
index 29260951aa871..09a27810ca27f 100644
@@ -972,6 +972,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, filp_close_sync)

[Severity: High]
Does filp_close_sync exist in the current tree? Since the referenced VFS commit (46ace7e4dc4b) might not be present yet, adding its BTF ID here will break the kernel build during resolve_btfids due to a missing dependency.

The patch obviously depends on 46ace7e4dc4b ("fs: make close_range() synchronous") being in the tree.

Quoted 10 lines

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index 1a2a2f1abf033..7665e1d28a641 100644

[ ... ]

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
@@ -165,7 +166,7 @@ static void test_d_path_basic(void)
 
Quoted 9 lines

tools/testing/selftests/bpf/prog_tests/d_path.c static void test_d_path_basic(void) · line 169

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +1 -1
 	if (CHECK(!bss->called_close,
 		  "close",
-		  "trampoline for filp_close was not called\n"))
+		  "trampoline for filp_close_sync was not called\n"))

[Severity: Low]
Since this CHECK() macro in test_d_path_basic() is being modified, shouldn't it be migrated to the modern ASSERT_*() macros? BPF subsystem guidelines require replacing deprecated CHECK() macros when updating existing tests.

That's a selftests/bpf nit, not relevant to the issue addressed by the patch.

Quoted 15 lines
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
 		goto cleanup;
 
 	for (int i = 0; i < MAX_FILES; i++) {

[ ... ]

Quoted 8 lines
tools/testing/selftests/bpf/progs/test_d_path.c +0 -0
index 561b2f861808e..edb5494a47f74 100644
@@ -41,7 +41,7 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
 	return 0;
 }
 
Quoted 8 lines

tools/testing/selftests/bpf/progs/test_d_path.c int BPF_PROG(prog_stat, struct path *path, struct kstat *stat, · line 44

Quoted 2 lines
tools/testing/selftests/bpf/progs/test_d_path.c +1 -1
-SEC("fentry/filp_close")
+SEC("fentry/filp_close_sync")

[Severity: High]
Will the test load fail unconditionally here? Attaching a BPF program to fentry/filp_close_sync when the function doesn't yet exist in the tree will cause an error. Applying this patch without its VFS dependency introduces a cross-tree dependency failure regression.

See above.

Quoted 5 lines
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_d_path.c +0 -0
 int BPF_PROG(prog_close, struct file *file, void *id)
 {
 	pid_t pid = bpf_get_current_pid_tgid() >> 32;
#3 lore raw
In reply to Ihor Solodrai
Can the commit message clarify which exec closes it refers to? At 46ace7e4dc4b, begin_new_exec() calls do_close_on_exec(), which still closes O_CLOEXEC descriptors with filp_close():
Quoted 6 lines

The d_path selftest closes its descriptors with close_range() so that its fentry program on filp_close() runs. The vfs commit 46ace7e4dc4b ("fs: make close_range() synchronous") switched close_range() to filp_close_sync(), so the program still attaches but is never called:

Quoted 6 lines

test_d_path_basic:FAIL:close trampoline for filp_close was not called

With that series, close(), close_range() and the closes on exec and exit all go through filp_close_sync(). Few paths still reach filp_close(), dup2() being one of them, so a program on filp_close() no longer sees most file closes.

Can the commit message clarify which exec closes it refers to? At 46ace7e4dc4b, begin_new_exec() calls do_close_on_exec(), which still closes O_CLOEXEC descriptors with filp_close():

fs/file.c:do_close_on_exec() {
    ...
    spin_unlock(&files->file_lock);
    filp_close(file, files);
    cond_resched();
}

So close-on-exec stays on filp_close() rather than moving to filp_close_sync(). Only close(2), __range_close() and close_files() were converted in that series. After this patch, a program moved from filp_close to filp_close_sync would silently stop seeing descriptors closed by exec.

Quoted 5 lines

Allow bpf_d_path() from filp_close_sync(). It takes the same arguments as filp_close() and the program runs before the file is flushed and its last reference is dropped, so file->f_path is still valid. Keep filp_close() in the allowlist for existing programs.

This explains why fentry is safe, but does the reasoning hold for fexit as well? The commit message says "the program runs before the file is flushed and its last reference is dropped," but that applies only to entry, not exit. Looking at filp_close_sync() (a546b2ef2120):

    retval = filp_flush(filp, id);
    fput_close_sync(filp);

fput_close_sync() calls __fput() synchronously when this is the last reference. __fput() does:

    dput(dentry);
    mntput(mnt);
    file_free(file);

So by the time a fexit trampoline runs, the dentry reference is gone and the struct file has gone back to filp_cache (SLAB_TYPESAFE_BY_RCU, reusable immediately). For pipes and sockets (alloc_file_pseudo uses d_alloc_pseudo which sets DCACHE_NORCU), dentry_free() calls __d_free() directly with no RCU delay.

Quoted 12 lines

Attach the selftest program to filp_close_sync() and keep triggering it with close_range(), since filp_close_sync() may be inlined into close().

Assisted-by LLM
kernel/trace/bpf_trace.c +0 -0
index 195f78db9bda..49d5ec983e72 100644

kernel/trace/bpf_trace.c BTF_ID(func, vfs_fallocate) · line 976

Quoted 5 lines
kernel/trace/bpf_trace.c +1 -0
@@ -972,6 +972,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, filp_close_sync)
 BTF_SET_END(btf_allowlist_d_path)

Can a fexit program on filp_close_sync() safely call bpf_d_path() on file->f_path that has already been freed? bpf_d_path_allowed() only checks the attach target, not the attach type:

    return btf_id_set_contains(&btf_allowlist_d_path,
                               prog->aux->attach_btf_id);

and check_helper_call() uses only this callback to gate the helper. check_attach_target() accepts BPF_TRACE_FEXIT on any function.

For example: SEC("fexit/filp_close_sync") prog calls bpf_d_path(&file->f_path, buf, sz). close_range() on a pipe fd calls __range_close() -> filp_close_sync() -> fput_close_sync() -> __fput() -> dput() which frees the NORCU dentry, then file_free() frees the file. Then the trampoline runs the fexit prog -> bpf_d_path() -> copy_from_kernel_nofault(&copy, path) reads the freed file -> d_path(&copy) -> path->dentry->d_op->d_dname(path->dentry, ...) which reads freed dentry memory and calls through a function pointer loaded from it.

The existing filp_close entry does not have this problem: fput_close() -> __fput_deferred() queues ____fput with task_work_add(TWA_RESUME), so __fput() runs only on return to user space, after fexit. filp_close_sync is the first allowlist entry that drops the last reference to its own argument.

Would it work to allow bpf_d_path from filp_close_sync only when prog->expected_attach_type == BPF_TRACE_FENTRY?

Quoted 3 lines
kernel/trace/bpf_trace.c +0 -0
 static bool bpf_d_path_allowed(const struct bpf_prog *prog)

[ ... ]

Quoted 30 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +4 -3
index 1a2a2f1abf03..7665e1d28a64 100644
@@ -109,8 +109,9 @@ static int trigger_fstat_events(pid_t pid)
 	fstat(indicatorfd, &fileStat);
 
 out_close:
-	/* sys_close no longer triggers filp_close, but we can
-	 * call sys_close_range instead which still does
+	/*
+	 * filp_close_sync() may be inlined into close(2), so use
+	 * close_range(2), which calls it directly.
 	 */
 	syscall_close(pipefd[0]);
 	syscall_close(pipefd[1]);
@@ -165,7 +166,7 @@ static void test_d_path_basic(void)
 
 	if (CHECK(!bss->called_close,
 		  "close",
-		  "trampoline for filp_close was not called\n"))
+		  "trampoline for filp_close_sync was not called\n"))
 		goto cleanup;
 
 	for (int i = 0; i < MAX_FILES; i++) {
tools/testing/selftests/bpf/progs/test_d_path.c +0 -0
index 561b2f861808..edb5494a47f7 100644

tools/testing/selftests/bpf/progs/test_d_path.c int BPF_PROG(prog_stat, struct path *path, struct kstat *stat, · line 47

Quoted 8 lines
tools/testing/selftests/bpf/progs/test_d_path.c +1 -1
@@ -41,7 +41,7 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
 	return 0;
 }
 
-SEC("fentry/filp_close")
+SEC("fentry/filp_close_sync")
 int BPF_PROG(prog_close, struct file *file, void *id)
 {
 	pid_t pid = bpf_get_current_pid_tgid() >> 32;

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/36058459433

author reviewer
#4 lore raw
Quoted 95 lines · On 9/24/26 2:24 PM, [email protected] wrote:
Quoted 6 lines

The d_path selftest closes its descriptors with close_range() so that its fentry program on filp_close() runs. The vfs commit 46ace7e4dc4b ("fs: make close_range() synchronous") switched close_range() to filp_close_sync(), so the program still attaches but is never called:

Quoted 6 lines

test_d_path_basic:FAIL:close trampoline for filp_close was not called

With that series, close(), close_range() and the closes on exec and exit all go through filp_close_sync(). Few paths still reach filp_close(), dup2() being one of them, so a program on filp_close() no longer sees most file closes.

Can the commit message clarify which exec closes it refers to? At 46ace7e4dc4b, begin_new_exec() calls do_close_on_exec(), which still closes O_CLOEXEC descriptors with filp_close():

fs/file.c:do_close_on_exec() {
    ...
    spin_unlock(&files->file_lock);
    filp_close(file, files);
    cond_resched();
}

So close-on-exec stays on filp_close() rather than moving to filp_close_sync(). Only close(2), __range_close() and close_files() were converted in that series. After this patch, a program moved from filp_close to filp_close_sync would silently stop seeing descriptors closed by exec.

Quoted 5 lines

Allow bpf_d_path() from filp_close_sync(). It takes the same arguments as filp_close() and the program runs before the file is flushed and its last reference is dropped, so file->f_path is still valid. Keep filp_close() in the allowlist for existing programs.

This explains why fentry is safe, but does the reasoning hold for fexit as well? The commit message says "the program runs before the file is flushed and its last reference is dropped," but that applies only to entry, not exit. Looking at filp_close_sync() (a546b2ef2120):

    retval = filp_flush(filp, id);
    fput_close_sync(filp);

fput_close_sync() calls __fput() synchronously when this is the last reference. __fput() does:

    dput(dentry);
    mntput(mnt);
    file_free(file);

So by the time a fexit trampoline runs, the dentry reference is gone and the struct file has gone back to filp_cache (SLAB_TYPESAFE_BY_RCU, reusable immediately). For pipes and sockets (alloc_file_pseudo uses d_alloc_pseudo which sets DCACHE_NORCU), dentry_free() calls __d_free() directly with no RCU delay.

Quoted 12 lines

Attach the selftest program to filp_close_sync() and keep triggering it with close_range(), since filp_close_sync() may be inlined into close().

Assisted-by LLM
kernel/trace/bpf_trace.c +0 -0
index 195f78db9bda..49d5ec983e72 100644

kernel/trace/bpf_trace.c BTF_ID(func, vfs_fallocate) · line 976

Quoted 5 lines
kernel/trace/bpf_trace.c +1 -0
@@ -972,6 +972,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, filp_close_sync)
 BTF_SET_END(btf_allowlist_d_path)

Can a fexit program on filp_close_sync() safely call bpf_d_path() on file->f_path that has already been freed? bpf_d_path_allowed() only checks the attach target, not the attach type:

    return btf_id_set_contains(&btf_allowlist_d_path,
                               prog->aux->attach_btf_id);

and check_helper_call() uses only this callback to gate the helper. check_attach_target() accepts BPF_TRACE_FEXIT on any function.

For example: SEC("fexit/filp_close_sync") prog calls bpf_d_path(&file->f_path, buf, sz). close_range() on a pipe fd calls __range_close() -> filp_close_sync() -> fput_close_sync() -> __fput() -> dput() which frees the NORCU dentry, then file_free() frees the file. Then the trampoline runs the fexit prog -> bpf_d_path() -> copy_from_kernel_nofault(&copy, path) reads the freed file -> d_path(&copy) -> path->dentry->d_op->d_dname(path->dentry, ...) which reads freed dentry memory and calls through a function pointer loaded from it.

Quoted 8 lines

The existing filp_close entry does not have this problem: fput_close() -> __fput_deferred() queues ____fput with task_work_add(TWA_RESUME), so __fput() runs only on return to user space, after fexit. filp_close_sync is the first allowlist entry that drops the last reference to its own argument.

Would it work to allow bpf_d_path from filp_close_sync only when prog->expected_attach_type == BPF_TRACE_FENTRY?

Both claude and codex refuse to build a reproducer for this because they "take extra care with some cybersecurity requests".

But the issue appears to be real, and bot's suggestion should work (pasting a diff at the bottom).

Christian, could you please confirm that allowing filp_close_sync on fentry, but not on fexit makes sense? Should I respin with that?

If anyone has ideas for alternative solutions to this bug or original issue, I'd be happy to try that as well.

As it stands, many BPF programs attaching to filp_close will probably get broken with 46ace7e4dc4b ("fs: make close_range() synchronous") reaching the mainline.

Thanks!

kernel/trace/bpf_trace.c +14 -3
index 49d5ec983e72..42dc01756f8a 100644
@@ -972,9 +972,12 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
-BTF_ID(func, filp_close_sync)
 BTF_SET_END(btf_allowlist_d_path)
 
+BTF_SET_START(btf_allowlist_d_path_fentry)
+BTF_ID(func, filp_close_sync)
+BTF_SET_END(btf_allowlist_d_path_fentry)
+
 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
 {
        if (prog->type == BPF_PROG_TYPE_TRACING &&
@@ -984,8 +987,16 @@ static bool bpf_d_path_allowed(const struct bpf_prog *prog)
        if (prog->type == BPF_PROG_TYPE_LSM)
                return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
 
-       return btf_id_set_contains(&btf_allowlist_d_path,
-                                  prog->aux->attach_btf_id);
+       if (btf_id_set_contains(&btf_allowlist_d_path,
+                               prog->aux->attach_btf_id))
+               return true;
+
+       if (btf_id_set_contains(&btf_allowlist_d_path_fentry,
+                               prog->aux->attach_btf_id))
+               return prog->expected_attach_type == BPF_TRACE_FENTRY ||
+                      prog->expected_attach_type == BPF_TRACE_FENTRY_MULTI;
+
+       return false;
 }
 
 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
Quoted 52 lines
Quoted 4 lines
kernel/trace/bpf_trace.c +0 -0
 static bool bpf_d_path_allowed(const struct bpf_prog *prog)

[ ... ]

Quoted 30 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +4 -3
index 1a2a2f1abf03..7665e1d28a64 100644
@@ -109,8 +109,9 @@ static int trigger_fstat_events(pid_t pid)
 	fstat(indicatorfd, &fileStat);
 
 out_close:
-	/* sys_close no longer triggers filp_close, but we can
-	 * call sys_close_range instead which still does
+	/*
+	 * filp_close_sync() may be inlined into close(2), so use
+	 * close_range(2), which calls it directly.
 	 */
 	syscall_close(pipefd[0]);
 	syscall_close(pipefd[1]);
@@ -165,7 +166,7 @@ static void test_d_path_basic(void)
 
 	if (CHECK(!bss->called_close,
 		  "close",
-		  "trampoline for filp_close was not called\n"))
+		  "trampoline for filp_close_sync was not called\n"))
 		goto cleanup;
 
 	for (int i = 0; i < MAX_FILES; i++) {
tools/testing/selftests/bpf/progs/test_d_path.c +0 -0
index 561b2f861808..edb5494a47f7 100644

tools/testing/selftests/bpf/progs/test_d_path.c int BPF_PROG(prog_stat, struct path *path, struct kstat *stat, · line 47

Quoted 8 lines
tools/testing/selftests/bpf/progs/test_d_path.c +1 -1
@@ -41,7 +41,7 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
 	return 0;
 }
 
-SEC("fentry/filp_close")
+SEC("fentry/filp_close_sync")
 int BPF_PROG(prog_close, struct file *file, void *id)
 {
 	pid_t pid = bpf_get_current_pid_tgid() >> 32;

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/36058459433

#5 lore raw
Quoted 102 lines · Ihor Solodrai
Quoted 101 lines · On 9/24/26 2:24 PM, [email protected] wrote:
Quoted 6 lines

The d_path selftest closes its descriptors with close_range() so that its fentry program on filp_close() runs. The vfs commit 46ace7e4dc4b ("fs: make close_range() synchronous") switched close_range() to filp_close_sync(), so the program still attaches but is never called:

Quoted 6 lines

test_d_path_basic:FAIL:close trampoline for filp_close was not called

With that series, close(), close_range() and the closes on exec and exit all go through filp_close_sync(). Few paths still reach filp_close(), dup2() being one of them, so a program on filp_close() no longer sees most file closes.

Can the commit message clarify which exec closes it refers to? At 46ace7e4dc4b, begin_new_exec() calls do_close_on_exec(), which still closes O_CLOEXEC descriptors with filp_close():

fs/file.c:do_close_on_exec() {
    ...
    spin_unlock(&files->file_lock);
    filp_close(file, files);
    cond_resched();
}

So close-on-exec stays on filp_close() rather than moving to filp_close_sync(). Only close(2), __range_close() and close_files() were converted in that series. After this patch, a program moved from filp_close to filp_close_sync would silently stop seeing descriptors closed by exec.

Quoted 5 lines

Allow bpf_d_path() from filp_close_sync(). It takes the same arguments as filp_close() and the program runs before the file is flushed and its last reference is dropped, so file->f_path is still valid. Keep filp_close() in the allowlist for existing programs.

This explains why fentry is safe, but does the reasoning hold for fexit as well? The commit message says "the program runs before the file is flushed and its last reference is dropped," but that applies only to entry, not exit. Looking at filp_close_sync() (a546b2ef2120):

    retval = filp_flush(filp, id);
    fput_close_sync(filp);

fput_close_sync() calls __fput() synchronously when this is the last reference. __fput() does:

    dput(dentry);
    mntput(mnt);
    file_free(file);

So by the time a fexit trampoline runs, the dentry reference is gone and the struct file has gone back to filp_cache (SLAB_TYPESAFE_BY_RCU, reusable immediately). For pipes and sockets (alloc_file_pseudo uses d_alloc_pseudo which sets DCACHE_NORCU), dentry_free() calls __d_free() directly with no RCU delay.

Quoted 12 lines

Attach the selftest program to filp_close_sync() and keep triggering it with close_range(), since filp_close_sync() may be inlined into close().

Assisted-by LLM
kernel/trace/bpf_trace.c +0 -0
index 195f78db9bda..49d5ec983e72 100644

kernel/trace/bpf_trace.c BTF_ID(func, vfs_fallocate) · line 976

Quoted 5 lines
kernel/trace/bpf_trace.c +1 -0
@@ -972,6 +972,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, filp_close_sync)
 BTF_SET_END(btf_allowlist_d_path)

Can a fexit program on filp_close_sync() safely call bpf_d_path() on file->f_path that has already been freed? bpf_d_path_allowed() only checks the attach target, not the attach type:

    return btf_id_set_contains(&btf_allowlist_d_path,
                               prog->aux->attach_btf_id);

and check_helper_call() uses only this callback to gate the helper. check_attach_target() accepts BPF_TRACE_FEXIT on any function.

For example: SEC("fexit/filp_close_sync") prog calls bpf_d_path(&file->f_path, buf, sz). close_range() on a pipe fd calls __range_close() -> filp_close_sync() -> fput_close_sync() -> __fput() -> dput() which frees the NORCU dentry, then file_free() frees the file. Then the trampoline runs the fexit prog -> bpf_d_path() -> copy_from_kernel_nofault(&copy, path) reads the freed file -> d_path(&copy) -> path->dentry->d_op->d_dname(path->dentry, ...) which reads freed dentry memory and calls through a function pointer loaded from it.

The existing filp_close entry does not have this problem: fput_close() -> __fput_deferred() queues ____fput with task_work_add(TWA_RESUME), so __fput() runs only on return to user space, after fexit. filp_close_sync is the first allowlist entry that drops the last reference to its own argument.

Quoted 5 lines
Quoted 2 lines

Would it work to allow bpf_d_path from filp_close_sync only when prog->expected_attach_type == BPF_TRACE_FENTRY?

Both claude and codex refuse to build a reproducer for this because they "take extra care with some cybersecurity requests".

I hate this.

Quoted 5 lines

But the issue appears to be real, and bot's suggestion should work (pasting a diff at the bottom).

Christian, could you please confirm that allowing filp_close_sync on fentry, but not on fexit makes sense? Should I respin with that?

Seems fine to me.