Skip to contentSkip to conversation

fuse, fsnotify: handle embedded NUL names safely

PATCH Applied Junyoung Jang 4 participants 9 messages Nov 17 – Nov 18

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

v2 cover fuse, fsnotify: handle embedded NUL names safely

FUSE READDIRPLUS can instantiate a dentry whose length-bearing name contains an embedded NUL. The fsnotify name-copy paths then stop at that NUL while retaining the original qstr length, allowing event-name buffers to expose uninitialized bytes.

Patch 1 skips dcache instantiation for these READDIRPLUS entries while preserving getdents() behavior. Patches 2 and 3 copy the full qstr name into inotify and fanotify events. Each patch applies independently to the base commit.

A custom LLM agent assisted with initial discovery and reproducer setup; all root-cause analysis, patches, and runtime tests were manually reviewed and verified by the authors.

---
Changes in v2:
- Follow Amir Goldstein's review: preserve FUSE readdir output and skip dcache instantiation only for invalid READDIRPLUS names.
- Replace the event-name strscpy_pad() calls with length-based memcpy() and explicit terminators in inotify and fanotify.

Tests on 23c62304dde2 in QEMU:
- Patch 1 alone: READDIR and READDIRPLUS getdents64() returned normally; the embedded-NUL entry was not instantiated, and FUSE_FORGET returned its unused lookup reference.
- Patch 2 alone: the FUSE READDIRPLUS/inotify reproducer returned the original qstr bytes after the NUL, with no stale slab bytes detected.
- Patch 3 alone: tmpfs FAN_REPORT_DFID_NAME creation and FAN_RENAME reported the expected names, NUL terminators, and zeroed alignment.
- All three together: the above FUSE and tmpfs regression probes passed.

Junyoung Jang (3):
fuse: skip dcache instantiation for readdirplus names with embedded
NUL
inotify: copy full qstr length for event names
fanotify: copy full qstr length for copied event names

 fs/fuse/readdir.c                    |  2 ++
 fs/notify/fanotify/fanotify.h        | 12 ++++++++++--
 fs/notify/inotify/inotify_fsnotify.c |  6 ++++--
 3 files changed, 16 insertions(+), 4 deletions(-)

base-commit: 23c62304dde29abe92013d85910d03a77dada35d

#1 lore raw

PATCH v2 1/3 fuse: skip dcache instantiation for readdirplus names with embedded NUL

FUSE READDIRPLUS can pass a length-bearing qstr with an embedded NUL to d_splice_alias(). Unlike verify_dirent_name() in fs/readdir.c, this path can create a dentry whose d_name.len extends past the NUL.

For such a name, strscpy() in inotify_handle_inode_event() and the fanotify name-copy helpers stop early. The kmalloc() and kvmalloc() event-name buffers retain uninitialized bytes within the recorded name length, which copy_event_to_user() and copy_fid_info_to_user() can expose.

Some FUSE servers include a terminating NUL in namelen. Follow nfs_prime_dcache() in commit 78d04af4995a1 ("NFS: nfs_prime_dcache must validate the filename") by skipping dcache instantiation without failing getdents(). Return -EIO from fuse_direntplus_link() so parse_dirplusfile() sends FUSE_FORGET for the unused lookup reference and continues readdir.

Fixes 0b05b18381ee ("fuse: implement NFS-like readdirplus support")

---
Changes in v2:
- Keep FUSE READDIR and READDIRPLUS getdents() behavior unchanged.
- Skip dcache instantiation only in fuse_direntplus_link(), following Amir Goldstein's NFS precedent; forget the unused lookup reference.

 fs/fuse/readdir.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fuse/readdir.c +2 -0
index 1f6cf565477f..6b2468b575d9 100644
@@ -191,6 +191,8 @@ static int fuse_direntplus_link(struct file *file,
 		return -EIO;
 	if (fuse_invalid_attr(&o->attr))
 		return -EIO;
+	if (memchr(name.name, '\0', name.len))
+		return -EIO;
 
 	fc = get_fuse_conn(dir);
 	epoch = atomic_read(&fc->epoch);
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL

Quoted 17 lines · Junyoung Jang

FUSE READDIRPLUS can pass a length-bearing qstr with an embedded NUL to d_splice_alias(). Unlike verify_dirent_name() in fs/readdir.c, this path can create a dentry whose d_name.len extends past the NUL.

For such a name, strscpy() in inotify_handle_inode_event() and the fanotify name-copy helpers stop early. The kmalloc() and kvmalloc() event-name buffers retain uninitialized bytes within the recorded name length, which copy_event_to_user() and copy_fid_info_to_user() can expose.

Some FUSE servers include a terminating NUL in namelen. Follow nfs_prime_dcache() in commit 78d04af4995a1 ("NFS: nfs_prime_dcache must validate the filename") by skipping dcache instantiation without failing getdents(). Return -EIO from fuse_direntplus_link() so parse_dirplusfile() sends FUSE_FORGET for the unused lookup reference and continues readdir.

Quoted 5 lines
Fixes 0b05b18381ee ("fuse: implement NFS-like readdirplus support")
Quoted 22 lines

---
Changes in v2:
- Keep FUSE READDIR and READDIRPLUS getdents() behavior unchanged.
- Skip dcache instantiation only in fuse_direntplus_link(), following Amir Goldstein's NFS precedent; forget the unused lookup reference.

 fs/fuse/readdir.c | 2 ++
 1 file changed, 2 insertions(+)
fs/fuse/readdir.c +2 -0
index 1f6cf565477f..6b2468b575d9 100644
@@ -191,6 +191,8 @@ static int fuse_direntplus_link(struct file *file,
                return -EIO;
        if (fuse_invalid_attr(&o->attr))
                return -EIO;
+       if (memchr(name.name, '\0', name.len))
+               return -EIO;
 
        fc = get_fuse_conn(dir);
        epoch = atomic_read(&fc->epoch);
maintainer Applied
#3 lore raw

Re: [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL

In reply to Junyoung Jang
Quoted 6 lines · Junyoung Jang

Some FUSE servers include a terminating NUL in namelen. Follow nfs_prime_dcache() in commit 78d04af4995a1 ("NFS: nfs_prime_dcache must validate the filename") by skipping dcache instantiation without failing getdents(). Return -EIO from fuse_direntplus_link() so parse_dirplusfile() sends FUSE_FORGET for the unused lookup reference and continues readdir.

Applied, thanks.

Miklos

#4 lore raw

PATCH v2 2/3 inotify: copy full qstr length for event names

In reply to Junyoung Jang

inotify_handle_inode_event() allocates the variable-length event via kmalloc(), reserving name->len + 1 bytes for event->name, and stores name->len in event->name_len. strscpy() stops at an embedded NUL in name->name, leaving the trailing name bytes uninitialized. copy_event_to_user() copies event->name_len bytes from that buffer to userspace, leaking uninitialized kernel slab memory. The terminator and alignment padding are zeroed separately in userspace.

Use memcpy() to copy the full qstr length and write the terminator separately, as __d_alloc() and d_path() handle length-bearing names.

Fixes ce163918cd33 ("inotify_handle_event(): don't bother with strlen()")

---
Changes in v2:
- Use memcpy() as suggested by Amir Goldstein and terminate at name->len.
- Copy only the valid qstr bytes to avoid a one-byte read past name->name.

 fs/notify/inotify/inotify_fsnotify.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +4 -2
index 6e3af6452dcc..ecbfc9b4e99c 100644
@@ -120,8 +120,10 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
 	event->wd = wd;
 	event->sync_cookie = cookie;
 	event->name_len = len;
-	if (len)
-		strscpy(event->name, name->name, event->name_len + 1);
+	if (len) {
+		memcpy(event->name, name->name, len);
+		event->name[len] = '\0';
+	}
 
 	ret = fsnotify_add_event(group, fsn_event, inotify_merge);
 	if (ret) {
reviewer Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] inotify: copy full qstr length for event names

Quoted 9 lines · Junyoung Jang

inotify_handle_inode_event() allocates the variable-length event via kmalloc(), reserving name->len + 1 bytes for event->name, and stores name->len in event->name_len. strscpy() stops at an embedded NUL in name->name, leaving the trailing name bytes uninitialized. copy_event_to_user() copies event->name_len bytes from that buffer to userspace, leaking uninitialized kernel slab memory. The terminator and alignment padding are zeroed separately in userspace.

Quoted 8 lines

Use memcpy() to copy the full qstr length and write the terminator separately, as __d_alloc() and d_path() handle length-bearing names.

Fixes ce163918cd33 ("inotify_handle_event(): don't bother with strlen()")
Quoted 25 lines

---
Changes in v2:
- Use memcpy() as suggested by Amir Goldstein and terminate at name->len.
- Copy only the valid qstr bytes to avoid a one-byte read past name->name.

 fs/notify/inotify/inotify_fsnotify.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/notify/inotify/inotify_fsnotify.c +4 -2
index 6e3af6452dcc..ecbfc9b4e99c 100644
@@ -120,8 +120,10 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
        event->wd = wd;
        event->sync_cookie = cookie;
        event->name_len = len;
-       if (len)
-               strscpy(event->name, name->name, event->name_len + 1);
+       if (len) {
+               memcpy(event->name, name->name, len);
+               event->name[len] = '\0';
+       }
 
        ret = fsnotify_add_event(group, fsn_event, inotify_merge);
        if (ret) {
#6 lore raw

PATCH v2 3/3 fanotify: copy full qstr length for copied event names

In reply to Junyoung Jang

fanotify_alloc_name_event() reserves name->len + 1 bytes for each copied name in a kvmalloc() event. fanotify_info_copy_name() and fanotify_info_copy_name2() use strscpy(), which stops at an embedded NUL and leaves the remaining name bytes uninitialized. With the original length stored in info->name_len or info->name2_len, copy_fid_info_to_user() copies name_len + 1 bytes to userspace, leaking uninitialized kernel heap memory.

Use memcpy() for the full qstr length and write the terminator separately, consistent with __d_alloc() and d_path().

Fixes 929943b38daf ("fanotify: add support for FAN_REPORT_NAME")

---
Changes in v2:
- Use memcpy() for both copied names, as suggested by Amir Goldstein.
- Write each terminator explicitly without reading past the source qstr.

FUSE fanotify mark setup on a zero-fsid mount returned -ENODEV; the normal-name FAN_REPORT_DFID_NAME and FAN_RENAME paths were tested on tmpfs.

 fs/notify/fanotify/fanotify.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/notify/fanotify/fanotify.h +10 -2
index c25cf5c1fc70..2d6b5c2c6f25 100644
@@ -221,18 +221,26 @@ static inline void fanotify_info_set_file_fh(struct fanotify_info *info,
 static inline void fanotify_info_copy_name(struct fanotify_info *info,
 					   const struct qstr *name)
 {
+	char *dst;
+
 	if (WARN_ON_ONCE(info->name2_len > 0))
 		return;
 
 	info->name_len = name->len;
-	strscpy(fanotify_info_name(info), name->name, name->len + 1);
+	dst = fanotify_info_name(info);
+	memcpy(dst, name->name, name->len);
+	dst[name->len] = '\0';
 }
 
 static inline void fanotify_info_copy_name2(struct fanotify_info *info,
 					    const struct qstr *name)
 {
+	char *dst;
+
 	info->name2_len = name->len;
-	strscpy(fanotify_info_name2(info), name->name, name->len + 1);
+	dst = fanotify_info_name2(info);
+	memcpy(dst, name->name, name->len);
+	dst[name->len] = '\0';
 }
 
 /*
reviewer Reviewed-by on v2 3/3
#7 lore raw

Re: [PATCH v2 3/3] fanotify: copy full qstr length for copied event names

Quoted 9 lines · Junyoung Jang

fanotify_alloc_name_event() reserves name->len + 1 bytes for each copied name in a kvmalloc() event. fanotify_info_copy_name() and fanotify_info_copy_name2() use strscpy(), which stops at an embedded NUL and leaves the remaining name bytes uninitialized. With the original length stored in info->name_len or info->name2_len, copy_fid_info_to_user() copies name_len + 1 bytes to userspace, leaking uninitialized kernel heap memory.

Quoted 8 lines

Use memcpy() for the full qstr length and write the terminator separately, consistent with __d_alloc() and d_path().

Fixes 929943b38daf ("fanotify: add support for FAN_REPORT_NAME")
Quoted 44 lines

---
Changes in v2:
- Use memcpy() for both copied names, as suggested by Amir Goldstein.
- Write each terminator explicitly without reading past the source qstr.

FUSE fanotify mark setup on a zero-fsid mount returned -ENODEV; the normal-name FAN_REPORT_DFID_NAME and FAN_RENAME paths were tested on tmpfs.

 fs/notify/fanotify/fanotify.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
fs/notify/fanotify/fanotify.h +10 -2
index c25cf5c1fc70..2d6b5c2c6f25 100644
@@ -221,18 +221,26 @@ static inline void fanotify_info_set_file_fh(struct fanotify_info *info,
 static inline void fanotify_info_copy_name(struct fanotify_info *info,
                                           const struct qstr *name)
 {
+       char *dst;
+
        if (WARN_ON_ONCE(info->name2_len > 0))
                return;
 
        info->name_len = name->len;
-       strscpy(fanotify_info_name(info), name->name, name->len + 1);
+       dst = fanotify_info_name(info);
+       memcpy(dst, name->name, name->len);
+       dst[name->len] = '\0';
 }
 
 static inline void fanotify_info_copy_name2(struct fanotify_info *info,
                                            const struct qstr *name)
 {
+       char *dst;
+
        info->name2_len = name->len;
-       strscpy(fanotify_info_name2(info), name->name, name->len + 1);
+       dst = fanotify_info_name2(info);
+       memcpy(dst, name->name, name->len);
+       dst[name->len] = '\0';
 }
 
 /*
maintainer
#8 lore raw
In reply to Junyoung Jang
Quoted 10 lines · Junyoung Jang

FUSE READDIRPLUS can instantiate a dentry whose length-bearing name contains an embedded NUL. The fsnotify name-copy paths then stop at that NUL while retaining the original qstr length, allowing event-name buffers to expose uninitialized bytes.

Patch 1 skips dcache instantiation for these READDIRPLUS entries while preserving getdents() behavior. Patches 2 and 3 copy the full qstr name into inotify and fanotify events. Each patch applies independently to the base commit.

Quoted 5 lines

A custom LLM agent assisted with initial discovery and reproducer setup; all root-cause analysis, patches, and runtime tests were manually reviewed and verified by the authors.

Thanks, I've added patches 2 & 3 to my tree. Patch 1 is for Miklos so I've left it alone.

Honza

Quoted 31 lines

---
Changes in v2:
- Follow Amir Goldstein's review: preserve FUSE readdir output and skip dcache instantiation only for invalid READDIRPLUS names.
- Replace the event-name strscpy_pad() calls with length-based memcpy() and explicit terminators in inotify and fanotify.

Tests on 23c62304dde2 in QEMU:
- Patch 1 alone: READDIR and READDIRPLUS getdents64() returned normally; the embedded-NUL entry was not instantiated, and FUSE_FORGET returned its unused lookup reference.
- Patch 2 alone: the FUSE READDIRPLUS/inotify reproducer returned the original qstr bytes after the NUL, with no stale slab bytes detected.
- Patch 3 alone: tmpfs FAN_REPORT_DFID_NAME creation and FAN_RENAME reported the expected names, NUL terminators, and zeroed alignment.
- All three together: the above FUSE and tmpfs regression probes passed.

Junyoung Jang (3):
fuse: skip dcache instantiation for readdirplus names with embedded
NUL
inotify: copy full qstr length for event names
fanotify: copy full qstr length for copied event names

 fs/fuse/readdir.c                    |  2 ++
 fs/notify/fanotify/fanotify.h        | 12 ++++++++++--
 fs/notify/inotify/inotify_fsnotify.c |  6 ++++--
 3 files changed, 16 insertions(+), 4 deletions(-)

base-commit: 23c62304dde29abe92013d85910d03a77dada35d

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR