提交 d40244bd 编写于 作者: D Dan Carpenter 提交者: Zheng Zengkai

fanotify: Fix stale file descriptor in copy_event_to_user()

stable inclusion
from stable-v5.10.97
commit 7b4741644cf718c422187e74fb07661ef1d68e85
bugzilla: https://gitee.com/openeuler/kernel/issues/I55O0O

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=7b4741644cf718c422187e74fb07661ef1d68e85

--------------------------------

commit ee125951 upstream.

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: f644bc44 ("fanotify: fix copy_event_to_user() fid error clean up")
Link: https://lore.kernel.org/r/20220128195656.GA26981@kiliSigned-off-by: NDan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: NMathias Krause <minipli@grsecurity.net>
Signed-off-by: NJan Kara <jack@suse.cz>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NYu Liao <liaoyu15@huawei.com>
Reviewed-by: NWei Li <liwei391@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 70cc064e
...@@ -366,9 +366,6 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, ...@@ -366,9 +366,6 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
if (fanotify_is_perm_event(event->mask)) if (fanotify_is_perm_event(event->mask))
FANOTIFY_PERM(event)->fd = fd; FANOTIFY_PERM(event)->fd = fd;
if (f)
fd_install(fd, f);
/* Event info records order is: dir fid + name, child fid */ /* Event info records order is: dir fid + name, child fid */
if (fanotify_event_dir_fh_len(event)) { if (fanotify_event_dir_fh_len(event)) {
info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME : info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
...@@ -432,6 +429,9 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, ...@@ -432,6 +429,9 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
count -= ret; count -= ret;
} }
if (f)
fd_install(fd, f);
return metadata.event_len; return metadata.event_len;
out_close_fd: out_close_fd:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册