Re: [PATCH 2/3] inotify: zero-pad event names after an embedded NUL
From: Junyoung Jang
Date: Mon Nov 16 2026 - 07:25:28 EST
On Mon, Nov 16, 2026 at 7:32 PM Amir Goldstein <amir73il@xxxxxxxxx> wrote:
>
> On Sun, Nov 15, 2026 at 10:10 PM Junyoung Jang
> <graypanda.inzag@xxxxxxxxx> wrote:
> >
> > inotify_handle_inode_event() allocates the variable-length event with
> > kmalloc() without zeroing the name buffer, sets event->name_len to
> > name->len, and copies name->name with strscpy().
> >
> > If a dentry qstr contains an embedded NUL byte (for example, via a
> > crafted FUSE READDIRPLUS entry aliasing an open directory), strscpy()
> > stops at the first NUL while event->name_len still covers the trailing
> > bytes. copy_event_to_user() then copies event->name_len bytes to
> > userspace, leaking uninitialized slab memory from the tail of the
> > event allocation.
> >
> > This mismatch dates back to
> > commit ce163918cd33 ("inotify_handle_event(): don't bother with strlen()"),
> > which replaced strlen(file_name->name) with file_name->len, and remained
> > in commit b8f2688258f8 ("inotify: Use strscpy() for event->name copies").
> >
> > Switch to strscpy_pad() so any trailing bytes up to event->name_len + 1
> > are zeroed.
> >
> > Fixes: ce163918cd33 ("inotify_handle_event(): don't bother with strlen()")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Co-developed-by: Gwanhyun Lee <gwanhyunlee@xxxxxxxxx>
> > Signed-off-by: Gwanhyun Lee <gwanhyunlee@xxxxxxxxx>
> > Signed-off-by: Junyoung Jang <graypanda.inzag@xxxxxxxxx>
> > ---
> > fs/notify/inotify/inotify_fsnotify.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
> > index 6e3af6452dcc..78eb1e92a14c 100644
> > --- a/fs/notify/inotify/inotify_fsnotify.c
> > +++ b/fs/notify/inotify/inotify_fsnotify.c
> > @@ -121,7 +121,7 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
> > event->sync_cookie = cookie;
> > event->name_len = len;
> > if (len)
> > - strscpy(event->name, name->name, event->name_len + 1);
> > + strscpy_pad(event->name, name->name, event->name_len + 1);
> >
>
> I am not sure it is inotify/fanotify responsibility to hide awkward names
> which filesystem emits.
>
> First of all, I think the solution should be consistent with d_path(), so memcpy
> instead of strcpy here may be enough to avoid leaking uninit memory.
>
> If we want to be nicer to userspace, then we could for example pad the
> garbage bytes with "???" for here and for d_path(), but I don't think
> this is worth the effort and also don't think that hiding the fs flaky name
> with strscpy_pad() is the best way to go.
>
> Thanks,
> Amir.
Hi Amir,
Agreed, matching __d_alloc() and d_path() is cleaner than zero-masking
the trailing bytes.
For v2, I will switch both inotify (patch 2) and fanotify (patch 3) to
memcpy(..., len) followed by an explicit '\0' at index len so the full
allocated buffer is initialized. I will give others a bit more time to
comment before sending v2.
Thanks,
Junyoung