|
Jan Kara |
c59800 |
From ee12595147ac1fbfb5bcb23837e26dd58d94b15d Mon Sep 17 00:00:00 2001
|
|
Jan Kara |
c59800 |
From: Dan Carpenter <dan.carpenter@oracle.com>
|
|
Jan Kara |
c59800 |
Date: Fri, 28 Jan 2022 22:57:01 +0300
|
|
Jan Kara |
c59800 |
Subject: [PATCH] fanotify: Fix stale file descriptor in copy_event_to_user()
|
|
Jan Kara |
c59800 |
Git-commit: ee12595147ac1fbfb5bcb23837e26dd58d94b15d
|
|
Jan Kara |
c59800 |
Patch-mainline: v5.17-rc3
|
|
Jan Kara |
c59800 |
References: bsc#1195187
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
This code calls fd_install() which gives the userspace access to the fd.
|
|
Jan Kara |
c59800 |
Then if copy_info_records_to_user() fails it calls put_unused_fd(fd) but
|
|
Jan Kara |
c59800 |
that will not release it and leads to a stale entry in the file
|
|
Jan Kara |
c59800 |
descriptor table.
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
Generally you can't trust the fd after a call to fd_install(). The fix
|
|
Jan Kara |
c59800 |
is to delay the fd_install() until everything else has succeeded.
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
Fortunately it requires CAP_SYS_ADMIN to reach this code so the security
|
|
Jan Kara |
c59800 |
impact is less.
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
Fixes: f644bc449b37 ("fanotify: fix copy_event_to_user() fid error clean up")
|
|
Jan Kara |
c59800 |
Link: https://lore.kernel.org/r/20220128195656.GA26981@kili
|
|
Jan Kara |
c59800 |
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
|
|
Jan Kara |
c59800 |
Reviewed-by: Mathias Krause <minipli@grsecurity.net>
|
|
Jan Kara |
c59800 |
Signed-off-by: Jan Kara <jack@suse.cz>
|
|
Jan Kara |
c59800 |
Acked-by: Jan Kara <jack@suse.cz>
|
|
Jan Kara |
c59800 |
---
|
|
Jan Kara |
c59800 |
fs/notify/fanotify/fanotify_user.c | 6 +++---
|
|
Jan Kara |
c59800 |
1 file changed, 3 insertions(+), 3 deletions(-)
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
--- a/fs/notify/fanotify/fanotify_user.c
|
|
Jan Kara |
c59800 |
+++ b/fs/notify/fanotify/fanotify_user.c
|
|
Jan Kara |
c59800 |
@@ -464,9 +464,6 @@ static ssize_t copy_event_to_user(struct
|
|
Jan Kara |
c59800 |
if (fanotify_is_perm_event(event->mask))
|
|
Jan Kara |
c59800 |
FANOTIFY_PERM(event)->fd = fd;
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
- if (f)
|
|
Jan Kara |
c59800 |
- fd_install(fd, f);
|
|
Jan Kara |
c59800 |
-
|
|
Jan Kara |
c59800 |
/* Event info records order is: dir fid + name, child fid */
|
|
Jan Kara |
c59800 |
if (fanotify_event_dir_fh_len(event)) {
|
|
Jan Kara |
c59800 |
info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
|
|
Jan Kara |
c59800 |
@@ -530,6 +527,9 @@ static ssize_t copy_event_to_user(struct
|
|
Jan Kara |
c59800 |
count -= ret;
|
|
Jan Kara |
c59800 |
}
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
+ if (f)
|
|
Jan Kara |
c59800 |
+ fd_install(fd, f);
|
|
Jan Kara |
c59800 |
+
|
|
Jan Kara |
c59800 |
return metadata.event_len;
|
|
Jan Kara |
c59800 |
|
|
Jan Kara |
c59800 |
out_close_fd:
|