[PATCH 3/3] fanotify: zero-pad copied event names after an embedded NUL
From: Junyoung Jang
Date: Sun Nov 15 2026 - 16:10:25 EST
fanotify_alloc_name_event() sizes the variable-length event buffer from
the qstr lengths and allocates it via kvmalloc() without zeroing.
fanotify_info_copy_name() and fanotify_info_copy_name2() record
name->len in info->name_len and info->name2_len, but copy the strings
with strscpy().
If a qstr contains an embedded NUL byte, strscpy() stops at the first
NUL and leaves the trailing bytes of the allocated name buffer
uninitialized. copy_fid_info_to_user() then copies name_len + 1 bytes
to userspace, leaking uninitialized kernel heap memory.
This mismatch dates back to
commit 929943b38daf ("fanotify: add support for FAN_REPORT_NAME") and
remained after commit 0fdbe845534f
("fanotify: replace deprecated strcpy in fanotify_info_copy_{name,name2}").
Switch both helpers to strscpy_pad() so any trailing bytes up to the
recorded length are zeroed.
Fixes: 929943b38daf ("fanotify: add support for FAN_REPORT_NAME")
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>
---
Note: default FUSE mounts report a zero fsid (-ENODEV on fanotify_mark()
with FAN_REPORT_DFID_NAME), so this patch was runtime-tested on tmpfs
with FAN_REPORT_DFID_NAME create and FAN_RENAME events.
fs/notify/fanotify/fanotify.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index c25cf5c1fc70..4a1f14aa4974 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -225,14 +225,14 @@ static inline void fanotify_info_copy_name(struct fanotify_info *info,
return;
info->name_len = name->len;
- strscpy(fanotify_info_name(info), name->name, name->len + 1);
+ strscpy_pad(fanotify_info_name(info), name->name, name->len + 1);
}
static inline void fanotify_info_copy_name2(struct fanotify_info *info,
const struct qstr *name)
{
info->name2_len = name->len;
- strscpy(fanotify_info_name2(info), name->name, name->len + 1);
+ strscpy_pad(fanotify_info_name2(info), name->name, name->len + 1);
}
/*
--
2.43.0