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

From: Amir Goldstein

Date: Tue Nov 17 2026 - 09:31:55 EST


On Tue, Nov 17, 2026 at 2:52 PM Junyoung Jang <graypanda.inzag@xxxxxxxxx> 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@xxxxxxxxxxxxxxx
> Co-developed-by: Gwanhyun Lee <gwanhyunlee@xxxxxxxxx>
> Signed-off-by: Gwanhyun Lee <gwanhyunlee@xxxxxxxxx>
> Signed-off-by: Junyoung Jang <graypanda.inzag@xxxxxxxxx>

Reviewed-by: Amir Goldstein <amir73il@xxxxxxxxx>
> ---
> 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) {