Skip to contentSkip to conversation

fork: fix pid refcount leaks when destroying file

PATCH In review Xin Xiong 2 participants 2 messages Jul 26, 2020

Subsystems (3)
lore
Share & tools
lore

Xin Xiong

author
#0 lore raw

PATCH 1/1 fork: fix pid refcount leaks when destroying file

When clone_flags & CLONE_PIDFD is true,the function creates a new file object called pidfile,and invokes get_pid(),which increases the refcnt of pid for pidfile to hold.

The reference counting issues take place in the error handling paths. When error occurs after the construction of pidfile, the function only invokes fput() to destroy pidfile, in which the increased refcount won't be decreased, resulting in a refcount leak.

Fix this issue by adding put_pid() in the error handling path bad_fork_put_pidfd.

---
 kernel/fork.c | 1 +
 1 file changed, 1 insertion(+)
kernel/fork.c +1 -0
index 142b23645d82..7cbfb2c4fce3 100644
@@ -2319,6 +2319,7 @@ static __latent_entropy struct task_struct *copy_process(
 bad_fork_put_pidfd:
 	if (clone_flags & CLONE_PIDFD) {
 		fput(pidfile);
+		put_pid(pid);
 		put_unused_fd(pidfd);
 	}
 bad_fork_free_pid:
Signature · 1 line
2.25.1
#1 lore raw
Quoted 24 lines · Xin Xiong

When clone_flags & CLONE_PIDFD is true,the function creates a new file object called pidfile,and invokes get_pid(),which increases the refcnt of pid for pidfile to hold.

The reference counting issues take place in the error handling paths. When error occurs after the construction of pidfile, the function only invokes fput() to destroy pidfile, in which the increased refcount won't be decreased, resulting in a refcount leak.

Fix this issue by adding put_pid() in the error handling path bad_fork_put_pidfd.

---
 kernel/fork.c | 1 +
 1 file changed, 1 insertion(+)
kernel/fork.c +0 -0
index 142b23645d82..7cbfb2c4fce3 100644

kernel/fork.c static __latent_entropy struct task_struct *copy_process( · line 2323

Quoted 5 lines
kernel/fork.c +1 -0
@@ -2319,6 +2319,7 @@ static __latent_entropy struct task_struct *copy_process(
 bad_fork_put_pidfd:
 	if (clone_flags & CLONE_PIDFD) {
 		fput(pidfile);
+		put_pid(pid);
 		put_unused_fd(pidfd);

Thanks for the patch but this is actually wrong. If you look further up where pidfile is allocated you'll see the comment

get_pid(pid); /* held by pidfile now */

which I added to inidicate that the additional reference to struct pid has now been transferred to the pidfile and is released when the file is released. So the last fput(pidfile) will cause the vfs to call the files' ->release() method which in this case is

static int pidfd_release(struct inode *inode, struct file *file)

{
	struct pid *pid = file->private_data;

	file->private_data = NULL;
	put_pid(pid);
	return 0;
}

Since fput(pidfile) in the bad_fork_put_pidfd error path is the only reference to the pidfile the struct pid reference count will be decremented.

Your additional put_pid() will cause a UAF which you should've seen during testing if you injected an error by setting CLONE_PIDFD e.g. by providing NULL as the args->pidfd argument to clone3() or NULL to the parent_tidptr argument of clone() or by placing the parent into a cgroup whos current pid_max limit doesn't allow it to fork.

Thanks!
Christian