inotify: Use strscpy() for event->name copies
authorKees Cook <kees@kernel.org>
Mon, 16 Dec 2024 22:45:15 +0000 (14:45 -0800)
committerJan Kara <jack@suse.cz>
Wed, 18 Dec 2024 10:33:40 +0000 (11:33 +0100)
Since we have already allocated "len + 1" space for event->name, make sure
that name->name cannot ever accidentally cause a copy overflow by calling
strscpy() instead of the unbounded strcpy() routine. This assists in
the ongoing efforts to remove the unsafe strcpy() API[1] from the kernel.

Link: https://github.com/KSPP/linux/issues/88
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20241216224507.work.859-kees@kernel.org
fs/notify/inotify/inotify_fsnotify.c

index 993375f0db673112808bab6fe787bfbcc2a4a238..cd7d11b0eb08b61f0e2d7bf3194516b422ec13df 100644 (file)
@@ -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)
-               strcpy(event->name, name->name);
+               strscpy(event->name, name->name, event->name_len + 1);
 
        ret = fsnotify_add_event(group, fsn_event, inotify_merge);
        if (ret) {