* [PATCH v2 0/3] fuse, fsnotify: handle embedded NUL names safely
@ 2026-11-17 13:51 Junyoung Jang
2026-11-17 13:51 ` [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL Junyoung Jang
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Junyoung Jang @ 2026-11-17 13:51 UTC (permalink / raw)
To: Miklos Szeredi, Jan Kara
Cc: Junyoung Jang, Amir Goldstein, Matt Bobrowski, Gwanhyun Lee,
fuse-devel, linux-fsdevel, linux-kernel
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.
Link: https://lore.kernel.org/linux-fsdevel/20261115210952.2140036-1-graypanda.inzag@gmail.com/
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL
2026-11-17 13:51 [PATCH v2 0/3] fuse, fsnotify: handle embedded NUL names safely Junyoung Jang
@ 2026-11-17 13:51 ` Junyoung Jang
2026-11-17 14:35 ` Amir Goldstein
2026-11-17 13:51 ` [PATCH v2 2/3] inotify: copy full qstr length for event names Junyoung Jang
2026-11-17 13:51 ` [PATCH v2 3/3] fanotify: copy full qstr length for copied " Junyoung Jang
2 siblings, 1 reply; 7+ messages in thread
From: Junyoung Jang @ 2026-11-17 13:51 UTC (permalink / raw)
To: Miklos Szeredi, Jan Kara
Cc: Junyoung Jang, Amir Goldstein, Matt Bobrowski, Gwanhyun Lee,
fuse-devel, linux-fsdevel, linux-kernel, stable
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")
Cc: stable@vger.kernel.org
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
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(+)
diff --git a/fs/fuse/readdir.c b/fs/fuse/readdir.c
index 1f6cf565477f..6b2468b575d9 100644
--- a/fs/fuse/readdir.c
+++ b/fs/fuse/readdir.c
@@ -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);
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 2/3] inotify: copy full qstr length for event names
2026-11-17 13:51 [PATCH v2 0/3] fuse, fsnotify: handle embedded NUL names safely Junyoung Jang
2026-11-17 13:51 ` [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL Junyoung Jang
@ 2026-11-17 13:51 ` Junyoung Jang
2026-11-17 14:31 ` Amir Goldstein
2026-11-17 13:51 ` [PATCH v2 3/3] fanotify: copy full qstr length for copied " Junyoung Jang
2 siblings, 1 reply; 7+ messages in thread
From: Junyoung Jang @ 2026-11-17 13:51 UTC (permalink / raw)
To: Miklos Szeredi, Jan Kara
Cc: Junyoung Jang, Amir Goldstein, Matt Bobrowski, Gwanhyun Lee,
fuse-devel, linux-fsdevel, linux-kernel, stable
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()")
Cc: stable@vger.kernel.org
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
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(-)
diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
index 6e3af6452dcc..ecbfc9b4e99c 100644
--- a/fs/notify/inotify/inotify_fsnotify.c
+++ b/fs/notify/inotify/inotify_fsnotify.c
@@ -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) {
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 3/3] fanotify: copy full qstr length for copied event names
2026-11-17 13:51 [PATCH v2 0/3] fuse, fsnotify: handle embedded NUL names safely Junyoung Jang
2026-11-17 13:51 ` [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL Junyoung Jang
2026-11-17 13:51 ` [PATCH v2 2/3] inotify: copy full qstr length for event names Junyoung Jang
@ 2026-11-17 13:51 ` Junyoung Jang
2026-11-17 14:33 ` Amir Goldstein
2 siblings, 1 reply; 7+ messages in thread
From: Junyoung Jang @ 2026-11-17 13:51 UTC (permalink / raw)
To: Miklos Szeredi, Jan Kara
Cc: Junyoung Jang, Amir Goldstein, Matt Bobrowski, Gwanhyun Lee,
fuse-devel, linux-fsdevel, linux-kernel, stable
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")
Cc: stable@vger.kernel.org
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
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(-)
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index c25cf5c1fc70..2d6b5c2c6f25 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -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';
}
/*
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/3] inotify: copy full qstr length for event names
2026-11-17 13:51 ` [PATCH v2 2/3] inotify: copy full qstr length for event names Junyoung Jang
@ 2026-11-17 14:31 ` Amir Goldstein
0 siblings, 0 replies; 7+ messages in thread
From: Amir Goldstein @ 2026-11-17 14:31 UTC (permalink / raw)
To: Junyoung Jang
Cc: Miklos Szeredi, Jan Kara, Matt Bobrowski, Gwanhyun Lee,
fuse-devel, linux-fsdevel, linux-kernel, stable
On Tue, Nov 17, 2026 at 2:52 PM Junyoung Jang <graypanda.inzag@gmail.com> wrote:
>
> 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()")
> Cc: stable@vger.kernel.org
> Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
> Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
> Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
> ---
> 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(-)
>
> diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
> index 6e3af6452dcc..ecbfc9b4e99c 100644
> --- a/fs/notify/inotify/inotify_fsnotify.c
> +++ b/fs/notify/inotify/inotify_fsnotify.c
> @@ -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) {
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 3/3] fanotify: copy full qstr length for copied event names
2026-11-17 13:51 ` [PATCH v2 3/3] fanotify: copy full qstr length for copied " Junyoung Jang
@ 2026-11-17 14:33 ` Amir Goldstein
0 siblings, 0 replies; 7+ messages in thread
From: Amir Goldstein @ 2026-11-17 14:33 UTC (permalink / raw)
To: Junyoung Jang
Cc: Miklos Szeredi, Jan Kara, Matt Bobrowski, Gwanhyun Lee,
fuse-devel, linux-fsdevel, linux-kernel, stable
On Tue, Nov 17, 2026 at 2:52 PM Junyoung Jang <graypanda.inzag@gmail.com> wrote:
>
> 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")
> Cc: stable@vger.kernel.org
> Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
> Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
> Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
> ---
> 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(-)
>
> diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
> index c25cf5c1fc70..2d6b5c2c6f25 100644
> --- a/fs/notify/fanotify/fanotify.h
> +++ b/fs/notify/fanotify/fanotify.h
> @@ -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';
> }
>
> /*
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL
2026-11-17 13:51 ` [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL Junyoung Jang
@ 2026-11-17 14:35 ` Amir Goldstein
0 siblings, 0 replies; 7+ messages in thread
From: Amir Goldstein @ 2026-11-17 14:35 UTC (permalink / raw)
To: Junyoung Jang
Cc: Miklos Szeredi, Jan Kara, Matt Bobrowski, Gwanhyun Lee,
fuse-devel, linux-fsdevel, linux-kernel, stable
On Tue, Nov 17, 2026 at 2:52 PM Junyoung Jang <graypanda.inzag@gmail.com> wrote:
>
> 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")
> Cc: stable@vger.kernel.org
> Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
> Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
> Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
> ---
> 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(+)
>
> diff --git a/fs/fuse/readdir.c b/fs/fuse/readdir.c
> index 1f6cf565477f..6b2468b575d9 100644
> --- a/fs/fuse/readdir.c
> +++ b/fs/fuse/readdir.c
> @@ -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);
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-17 14:35 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 13:51 [PATCH v2 0/3] fuse, fsnotify: handle embedded NUL names safely Junyoung Jang
2026-11-17 13:51 ` [PATCH v2 1/3] fuse: skip dcache instantiation for readdirplus names with embedded NUL Junyoung Jang
2026-11-17 14:35 ` Amir Goldstein
2026-11-17 13:51 ` [PATCH v2 2/3] inotify: copy full qstr length for event names Junyoung Jang
2026-11-17 14:31 ` Amir Goldstein
2026-11-17 13:51 ` [PATCH v2 3/3] fanotify: copy full qstr length for copied " Junyoung Jang
2026-11-17 14:33 ` Amir Goldstein
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®