[PATCH 3/3] [HERE BE DRAGONS - DRAFT - __UNTESTED__] fanotify: use pidfd_file_create()

From: Christian Brauner
Date: Wed Mar 22 2023 - 11:02:50 EST


Signed-off-by: Christian Brauner <brauner@xxxxxxxxxx>
---
fs/notify/fanotify/fanotify_user.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 8f430bfad487..4a8db6b5f690 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -665,6 +665,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD;
struct file *f = NULL;
int ret, pidfd = FAN_NOPIDFD, fd = FAN_NOFD;
+ struct file *pidfd_file = NULL;

pr_debug("%s: group=%p event=%p\n", __func__, group, event);

@@ -718,9 +719,11 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
!pid_has_task(event->pid, PIDTYPE_TGID)) {
pidfd = FAN_NOPIDFD;
} else {
- pidfd = pidfd_create(event->pid, 0);
- if (pidfd < 0)
+ pidfd_file = pidfd_file_create(event->pid, 0, &pidfd);
+ if (IS_ERR(pidfd_file)) {
pidfd = FAN_EPIDFD;
+ pidfd_file = NULL;
+ }
}
}

@@ -750,6 +753,8 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,

if (f)
fd_install(fd, f);
+ if (pidfd_file)
+ fd_install(pidfd, pidfd_file);

return metadata.event_len;

@@ -759,8 +764,10 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
fput(f);
}

- if (pidfd >= 0)
- close_fd(pidfd);
+ if (pidfd >= 0) {
+ put_unused_fd(pidfd);
+ fput(pidfd_file);
+ }

return ret;
}
--
2.34.1