Skip to content

Commit ee12595

Browse files
Dan Carpenterjankara
authored andcommitted
fanotify: Fix stale file descriptor in copy_event_to_user()
This code calls fd_install() which gives the userspace access to the fd. Then if copy_info_records_to_user() fails it calls put_unused_fd(fd) but that will not release it and leads to a stale entry in the file descriptor table. Generally you can't trust the fd after a call to fd_install(). The fix is to delay the fd_install() until everything else has succeeded. Fortunately it requires CAP_SYS_ADMIN to reach this code so the security impact is less. Fixes: f644bc4 ("fanotify: fix copy_event_to_user() fid error clean up") Link: https://lore.kernel.org/r/20220128195656.GA26981@kili Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com> Reviewed-by: Mathias Krause <minipli@grsecurity.net> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 26291c5 commit ee12595

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

fs/notify/fanotify/fanotify_user.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,16 +701,16 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
701701
if (fanotify_is_perm_event(event->mask))
702702
FANOTIFY_PERM(event)->fd = fd;
703703

704-
if (f)
705-
fd_install(fd, f);
706-
707704
if (info_mode) {
708705
ret = copy_info_records_to_user(event, info, info_mode, pidfd,
709706
buf, count);
710707
if (ret < 0)
711708
goto out_close_fd;
712709
}
713710

711+
if (f)
712+
fd_install(fd, f);
713+
714714
return metadata.event_len;
715715

716716
out_close_fd:

0 commit comments

Comments
 (0)