Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

fanotify: Make sure to check event_len when copying

As a precaution, make sure we check event_len when copying to userspace.
Based on old feedback: https://lkml.kernel.org/r/542D9FE5.3010009@gmx.de

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Jan Kara <jack@suse.cz>

authored by

Kees Cook and committed by
Jan Kara
5b03a472 d6f7aa98

+8 -2
+8 -2
fs/notify/fanotify/fanotify_user.c
··· 206 206 207 207 static ssize_t copy_event_to_user(struct fsnotify_group *group, 208 208 struct fsnotify_event *event, 209 - char __user *buf) 209 + char __user *buf, size_t count) 210 210 { 211 211 struct fanotify_event_metadata fanotify_event_metadata; 212 212 struct file *f; ··· 220 220 221 221 fd = fanotify_event_metadata.fd; 222 222 ret = -EFAULT; 223 + /* 224 + * Sanity check copy size in case get_one_event() and 225 + * fill_event_metadata() event_len sizes ever get out of sync. 226 + */ 227 + if (WARN_ON_ONCE(fanotify_event_metadata.event_len > count)) 228 + goto out_close_fd; 223 229 if (copy_to_user(buf, &fanotify_event_metadata, 224 230 fanotify_event_metadata.event_len)) 225 231 goto out_close_fd; ··· 301 295 continue; 302 296 } 303 297 304 - ret = copy_event_to_user(group, kevent, buf); 298 + ret = copy_event_to_user(group, kevent, buf, count); 305 299 if (unlikely(ret == -EOPENSTALE)) { 306 300 /* 307 301 * We cannot report events with stale fd so drop it.