Skip to contentSkip to conversation

audit: don't use the caller's root when it has no fs

PATCH In review Nguyen Ngoc Thang 3 participants 3 messages Oct 20 – Nov 4

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

PATCH v2 1/1 audit: don't use the caller's root when it has no fs

audit_log_d_path() calls d_path(), which resolves the path relative to current->fs->root. do_exit() runs exit_fs() before exit_task_work(), so a deferred __fput() runs with current->fs == NULL and d_path() oopses:

ima_file_free() -> ima_update_xattr() -> ima_calc_file_hash()

    -> dentry_open() -> smack_file_open() -> smack_log()
    -> common_lsm_audit() -> audit_log_d_path() -> d_path()
    -> read_seqbegin(&current->fs->seq)

Without an fs there is no root to be relative to, so log the absolute path with d_absolute_path() instead.

Reported-by [email protected]
Fixes 4a9d4b024a31 ("switch fput to task_work_add")

---
Changes in v2 (Al Viro's review of v1, which patched d_path() itself):
- d_path() is meant to be relative to the caller's root, which is meaningless here, so leave it alone and fix the caller instead.
- audit_log_d_path() uses d_absolute_path() when current->fs is NULL.

Reproduced in QEMU (x86_64, KASAN, Smack+IMA+audit) with the syzbot C
reproducer: without the patch the first exiting child hits a null-ptr-deref in d_path(); with it the reproducer runs to completion with no KASAN/Oops, and the audit record still carries path="...".

Al, does this match what you had in mind? A final fput() in some unrelated task still logs a path relative to that task's root; this patch does not change that.

 kernel/audit.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/audit.c +5 -1
index 9412af9144bc..32085674d0be 100644
@@ -2218,7 +2218,11 @@ void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
 		audit_log_format(ab, "\"<no_memory>\"");
 		return;
 	}
-	p = d_path(path, pathname, PATH_MAX+11);
+	/* No fs after exit_fs(), so there is no root to be relative to */
+	if (current->fs)
+		p = d_path(path, pathname, PATH_MAX+11);
+	else
+		p = d_absolute_path(path, pathname, PATH_MAX+11);
 	if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
 		/* FIXME: can we save some information here? */
 		audit_log_format(ab, "\"<too_long>\"");
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] audit: audit_log_d_path() logs <too_long> on -EINVAL from d_absolute_path() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] audit: audit_log_d_path() logs <too_long> on -EINVAL from d_absolute_path() --

commit 414cd8bdacdc30225fedf858b3d33b72b5614c16
Author: Nguyen Ngoc Thang <[email protected]>

audit: don't use the caller's root when it has no fs

This commit updates audit_log_d_path() to call d_absolute_path() instead of d_path() when current->fs is NULL, preventing a NULL pointer dereference when deferred __fput() runs from exit_task_work() after exit_fs() in do_exit().

Quoted 7 lines
kernel/audit.c +0 -0
index 631d6d4a23cfd..1973da2eaf3f7 100644
@@ -2218,7 +2218,11 @@ void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
 		audit_log_format(ab, "\"<no_memory>\"");
 		return;

kernel/audit.c void audit_log_d_path(struct audit_buffer *ab, const char *prefix, · line 2228

Quoted 10 lines
kernel/audit.c +5 -1
 	}
-	p = d_path(path, pathname, PATH_MAX+11);
+	/* No fs after exit_fs(), so there is no root to be relative to */
+	if (current->fs)
+		p = d_path(path, pathname, PATH_MAX+11);
+	else
+		p = d_absolute_path(path, pathname, PATH_MAX+11);
 	if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
 		/* FIXME: can we save some information here? */
 		audit_log_format(ab, "\"<too_long>\"");

[Severity: Medium]
Can calling d_absolute_path() here cause audit_log_d_path() to falsely log "<too_long>" instead of the file path?

Unlike d_path(), which dispatches path->dentry->d_op->d_dname(), prepends " (deleted)" for unlinked dentries, and ignores positive return values from prepend_path(), d_absolute_path() fails with ERR_PTR(-EINVAL) whenever prepend_path() returns a value greater than 1:

fs/d_path.c:d_absolute_path() {
	struct path root = {};
	DECLARE_BUFFER(b, buf, buflen);

	prepend_char(&b, 0);
	if (unlikely(prepend_path(path, &root, &b) > 1))
		return ERR_PTR(-EINVAL);
	return extract_string(&b);
}

prepend_path() returns 2 when a mount has mnt_ns == NULL (such as a lazy-unmounted MNT_DETACH filesystem), an anonymous mount namespace (from open_tree() or fsmount()), or MNT_NS_INTERNAL (such as pipefs, sockfs, or anon_inodefs), and returns 3 when a dentry escapes its mount root.

In the do_exit() path that triggers this code, exit_nsproxy_namespaces() runs after exit_fs() and before exit_task_work():

kernel/exit.c:do_exit() {
	...
	exit_files(tsk);
	exit_fs(tsk);
	exit_nsproxy_namespaces(tsk);
	exit_task_work(tsk);
	...
}

If the exiting task holds the last reference to its mount namespace (for example, after unshare(CLONE_NEWNS) or clone(CLONE_NEWNS)), exit_nsproxy_namespaces() -> put_nsproxy() -> put_mnt_ns() -> umount_tree() runs before exit_task_work() and clears p->mnt_ns to NULL on the mounts in that namespace:

fs/namespace.c:umount_tree() {
		...
		WRITE_ONCE(p->mnt_ns, NULL);
		if (how & UMOUNT_SYNC)
			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;

		if (mnt_has_parent(p)) {
			if (needs_cover(p, how))
				leave_cover(p);
			umount_mnt(p);
		}
		...
}

When exit_task_work() subsequently runs deferred ____fput() and reaches audit_log_d_path(), d_absolute_path() will see mnt_ns == NULL and return ERR_PTR(-EINVAL), which the IS_ERR(p) check in audit_log_d_path() then reports as "<too_long>".

Would it make more sense to use a helper that preserves d_path() semantics (including d_dname(), " (deleted)", and detached mounts) with an empty root path when current->fs is NULL, rather than d_absolute_path()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Nguyen Ngoc Thang
Quoted 42 lines · On Oct 20, 2026 [email protected] wrote:

audit_log_d_path() calls d_path(), which resolves the path relative to current->fs->root. do_exit() runs exit_fs() before exit_task_work(), so a deferred __fput() runs with current->fs == NULL and d_path() oopses:

ima_file_free() -> ima_update_xattr() -> ima_calc_file_hash()

    -> dentry_open() -> smack_file_open() -> smack_log()
    -> common_lsm_audit() -> audit_log_d_path() -> d_path()
    -> read_seqbegin(&current->fs->seq)

Without an fs there is no root to be relative to, so log the absolute path with d_absolute_path() instead.

Reported-by [email protected]
Fixes 4a9d4b024a31 ("switch fput to task_work_add")

---
Changes in v2 (Al Viro's review of v1, which patched d_path() itself):
- d_path() is meant to be relative to the caller's root, which is meaningless here, so leave it alone and fix the caller instead.
- audit_log_d_path() uses d_absolute_path() when current->fs is NULL.

Reproduced in QEMU (x86_64, KASAN, Smack+IMA+audit) with the syzbot C
reproducer: without the patch the first exiting child hits a null-ptr-deref in d_path(); with it the reproducer runs to completion with no KASAN/Oops, and the audit record still carries path="...".

Al, does this match what you had in mind? A final fput() in some unrelated task still logs a path relative to that task's root; this patch does not change that.

 kernel/audit.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/audit.c +0 -0
index 9412af9144bc..32085674d0be 100644
@@ -2218,7 +2218,11 @@ void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
 		audit_log_format(ab, "\"<no_memory>\"");
 		return;

kernel/audit.c void audit_log_d_path(struct audit_buffer *ab, const char *prefix, · line 2228

Quoted 10 lines
kernel/audit.c +5 -1
 	}
-	p = d_path(path, pathname, PATH_MAX+11);
+	/* No fs after exit_fs(), so there is no root to be relative to */
+	if (current->fs)
+		p = d_path(path, pathname, PATH_MAX+11);
+	else
+		p = d_absolute_path(path, pathname, PATH_MAX+11);
 	if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
 		/* FIXME: can we save some information here? */
 		audit_log_format(ab, "\"<too_long>\"");

See the sashiko feedback on this patch for more information, but since d_absolute_path() can now error for reasons other "too_long" we should probably change the error string to "<error>" or similar.

--
paul-moore.com