]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
pid: add pidfd_prepare()
authorChristian Brauner <brauner@kernel.org>
Mon, 27 Mar 2023 18:22:51 +0000 (20:22 +0200)
committerChristian Brauner <brauner@kernel.org>
Mon, 3 Apr 2023 09:16:56 +0000 (11:16 +0200)
Add a new helper that allows to reserve a pidfd and allocates a new
pidfd file that stashes the provided struct pid. This will allow us to
remove places that either open code this function or that call
pidfd_create() but then have to call close_fd() because there are still
failure points after pidfd_create() has been called.

Reviewed-by: Jan Kara <jack@suse.cz>
Message-Id: <20230327-pidfd-file-api-v1-1-5c0e9a3158e4@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
include/linux/pid.h
kernel/fork.c
kernel/pid.c

index 343abf22092e6b54a973dc6d457e06e317e6c9ed..b75de288a8c29ac47f10137078e094fe2a63fbe9 100644 (file)
@@ -80,6 +80,7 @@ extern struct pid *pidfd_pid(const struct file *file);
 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags);
 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);
 int pidfd_create(struct pid *pid, unsigned int flags);
+int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret);
 
 static inline struct pid *get_pid(struct pid *pid)
 {
index c0257cbee0931acff05a980d428af2172fcb6d84..a34395bd708ae850832c16f1c246c96528b74b19 100644 (file)
@@ -1951,6 +1951,91 @@ const struct file_operations pidfd_fops = {
 #endif
 };
 
+/**
+ * __pidfd_prepare - allocate a new pidfd_file and reserve a pidfd
+ * @pid:   the struct pid for which to create a pidfd
+ * @flags: flags of the new @pidfd
+ * @pidfd: the pidfd to return
+ *
+ * Allocate a new file that stashes @pid and reserve a new pidfd number in the
+ * caller's file descriptor table. The pidfd is reserved but not installed yet.
+
+ * The helper doesn't perform checks on @pid which makes it useful for pidfds
+ * created via CLONE_PIDFD where @pid has no task attached when the pidfd and
+ * pidfd file are prepared.
+ *
+ * If this function returns successfully the caller is responsible to either
+ * call fd_install() passing the returned pidfd and pidfd file as arguments in
+ * order to install the pidfd into its file descriptor table or they must use
+ * put_unused_fd() and fput() on the returned pidfd and pidfd file
+ * respectively.
+ *
+ * This function is useful when a pidfd must already be reserved but there
+ * might still be points of failure afterwards and the caller wants to ensure
+ * that no pidfd is leaked into its file descriptor table.
+ *
+ * Return: On success, a reserved pidfd is returned from the function and a new
+ *         pidfd file is returned in the last argument to the function. On
+ *         error, a negative error code is returned from the function and the
+ *         last argument remains unchanged.
+ */
+static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
+{
+       int pidfd;
+       struct file *pidfd_file;
+
+       if (flags & ~(O_NONBLOCK | O_RDWR | O_CLOEXEC))
+               return -EINVAL;
+
+       pidfd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
+       if (pidfd < 0)
+               return pidfd;
+
+       pidfd_file = anon_inode_getfile("[pidfd]", &pidfd_fops, pid,
+                                       flags | O_RDWR | O_CLOEXEC);
+       if (IS_ERR(pidfd_file)) {
+               put_unused_fd(pidfd);
+               return PTR_ERR(pidfd_file);
+       }
+       get_pid(pid); /* held by pidfd_file now */
+       *ret = pidfd_file;
+       return pidfd;
+}
+
+/**
+ * pidfd_prepare - allocate a new pidfd_file and reserve a pidfd
+ * @pid:   the struct pid for which to create a pidfd
+ * @flags: flags of the new @pidfd
+ * @pidfd: the pidfd to return
+ *
+ * Allocate a new file that stashes @pid and reserve a new pidfd number in the
+ * caller's file descriptor table. The pidfd is reserved but not installed yet.
+ *
+ * The helper verifies that @pid is used as a thread group leader.
+ *
+ * If this function returns successfully the caller is responsible to either
+ * call fd_install() passing the returned pidfd and pidfd file as arguments in
+ * order to install the pidfd into its file descriptor table or they must use
+ * put_unused_fd() and fput() on the returned pidfd and pidfd file
+ * respectively.
+ *
+ * This function is useful when a pidfd must already be reserved but there
+ * might still be points of failure afterwards and the caller wants to ensure
+ * that no pidfd is leaked into its file descriptor table.
+ *
+ * Return: On success, a reserved pidfd is returned from the function and a new
+ *         pidfd file is returned in the last argument to the function. On
+ *         error, a negative error code is returned from the function and the
+ *         last argument remains unchanged.
+ */
+int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
+{
+       if (!pid || !pid_has_task(pid, PIDTYPE_TGID))
+               return -EINVAL;
+
+       return __pidfd_prepare(pid, flags, ret);
+}
+
 static void __delayed_free_task(struct rcu_head *rhp)
 {
        struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
index 3fbc5e46b72173cf216850ea85afc24ace98fd3c..f93954a0384d3889fb6d52de977562d10be1a012 100644 (file)
@@ -594,20 +594,15 @@ struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
  */
 int pidfd_create(struct pid *pid, unsigned int flags)
 {
-       int fd;
-
-       if (!pid || !pid_has_task(pid, PIDTYPE_TGID))
-               return -EINVAL;
+       int pidfd;
+       struct file *pidfd_file;
 
-       if (flags & ~(O_NONBLOCK | O_RDWR | O_CLOEXEC))
-               return -EINVAL;
-
-       fd = anon_inode_getfd("[pidfd]", &pidfd_fops, get_pid(pid),
-                             flags | O_RDWR | O_CLOEXEC);
-       if (fd < 0)
-               put_pid(pid);
+       pidfd = pidfd_prepare(pid, flags, &pidfd_file);
+       if (pidfd < 0)
+               return pidfd;
 
-       return fd;
+       fd_install(pidfd, pidfd_file);
+       return pidfd;
 }
 
 /**