fuse, fsnotify: handle embedded NUL names safely
Subsystems (6)
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
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.
---
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
@@ -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);
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(). Unlikeverify_dirent_name()infs/readdir.c, this path can create a dentry whose d_name.len extends past the NUL.For such a name,
strscpy()ininotify_handle_inode_event()and the fanotify name-copy helpers stop early. Thekmalloc()andkvmalloc()event-name buffers retain uninitialized bytes within the recorded name length, whichcopy_event_to_user()andcopy_fid_info_to_user()can expose.Some FUSE servers include a terminating NUL in namelen. Follow
nfs_prime_dcache()in commit78d04af4995a1("NFS:nfs_prime_dcachemust validate the filename") by skipping dcache instantiation without failinggetdents(). Return-EIOfromfuse_direntplus_link()soparse_dirplusfile()sendsFUSE_FORGETfor the unused lookup reference and continues readdir.
Quoted 5 lines
Fixes 0b05b18381ee ("fuse: implement NFS-like readdirplus support")Co-developed-by Gwanhyun Lee <[email protected]>Signed-off-by Gwanhyun Lee <[email protected]>Signed-off-by Junyoung Jang <[email protected]>
Quoted 22 lines
---
Changes in v2:
- Keep FUSE READDIR and READDIRPLUSgetdents()behavior unchanged.
- Skip dcache instantiation only infuse_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@@ -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);
Re: [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL
In reply to Junyoung JangQuoted 6 lines · Junyoung Jang
Some FUSE servers include a terminating NUL in namelen. Follow
nfs_prime_dcache()in commit78d04af4995a1("NFS:nfs_prime_dcachemust validate the filename") by skipping dcache instantiation without failinggetdents(). Return-EIOfromfuse_direntplus_link()soparse_dirplusfile()sendsFUSE_FORGETfor the unused lookup reference and continues readdir.
Applied, thanks.
Miklos
PATCH v2 2/3 inotify: copy full qstr length for event names
In reply to Junyoung Janginotify_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.
---
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
@@ -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) {
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 viakmalloc(), reservingname->len+ 1 bytes forevent->name, and storesname->leninevent->name_len.strscpy()stops at an embedded NUL inname->name, leaving the trailing name bytes uninitialized.copy_event_to_user()copiesevent->name_lenbytes 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()andd_path()handle length-bearing names.Fixes ce163918cd33 ("inotify_handle_event(): don't bother with strlen()")Co-developed-by Gwanhyun Lee <[email protected]>Signed-off-by Gwanhyun Lee <[email protected]>Signed-off-by Junyoung Jang <[email protected]>
Quoted 25 lines
---
Changes in v2:
- Usememcpy()as suggested by Amir Goldstein and terminate atname->len.
- Copy only the valid qstr bytes to avoid a one-byte read pastname->name.fs/notify/inotify/inotify_fsnotify.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/notify/inotify/inotify_fsnotify.c +4
-2@@ -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) {
PATCH v2 3/3 fanotify: copy full qstr length for copied event names
In reply to Junyoung Jangfanotify_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().
---
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
@@ -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' ; }/*
Re: [PATCH v2 3/3] fanotify: copy full qstr length for copied event names
Quoted 9 lines · Junyoung Jang
fanotify_alloc_name_event()reservesname->len+ 1 bytes for each copied name in akvmalloc()event.fanotify_info_copy_name()andfanotify_info_copy_name2()usestrscpy(), which stops at an embedded NUL and leaves the remaining name bytes uninitialized. With the original length stored ininfo->name_lenorinfo->name2_len,copy_fid_info_to_user()copiesname_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()andd_path().Fixes 929943b38daf ("fanotify: add support for FAN_REPORT_NAME")Co-developed-by Gwanhyun Lee <[email protected]>Signed-off-by Gwanhyun Lee <[email protected]>Signed-off-by Junyoung Jang <[email protected]>
Quoted 44 lines
---
Changes in v2:
- Usememcpy()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-nameFAN_REPORT_DFID_NAMEandFAN_RENAMEpaths were tested on tmpfs.fs/notify/fanotify/fanotify.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)fs/notify/fanotify/fanotify.h +10
-2@@ -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' ; }/*
Jan Kara
maintainerQuoted 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-namestrscpy_pad()calls with length-basedmemcpy()and explicit terminators in inotify and fanotify.Tests on
23c62304dde2in QEMU:
- Patch 1 alone: READDIR and READDIRPLUSgetdents64()returned normally; the embedded-NUL entry was not instantiated, andFUSE_FORGETreturned 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: tmpfsFAN_REPORT_DFID_NAMEcreation andFAN_RENAMEreported 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 namesfs/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