Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit baba1b29 authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Linus Torvalds
Browse files

exec: binfmt_misc: don't nullify Node->dentry in kill_node()

kill_node() nullifies/checks Node->dentry to avoid double free.  This
complicates the next changes and this is very confusing:

 - we do not need to check dentry != NULL under entries_lock,
   kill_node() is always called under inode_lock(d_inode(root)) and we
   rely on this inode_lock() anyway, without this lock the
   MISC_FMT_OPEN_FILE cleanup could race with itself.

 - if kill_inode() was already called and ->dentry == NULL we should not
   even try to close e->interp_file.

We can change bm_entry_write() to simply check !list_empty(list) before
kill_node.  Again, we rely on inode_lock(), in particular it saves us
from the race with bm_status_write(), another caller of kill_node().

Link: http://lkml.kernel.org/r/20170922143641.GA17210@redhat.com


Signed-off-by: default avatarOleg Nesterov <oleg@redhat.com>
Acked-by: default avatarKees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Ben Woodard <woodard@redhat.com>
Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Jim Foraker <foraker1@llnl.gov>
Cc: <tdhooge@llnl.gov>
Cc: Travis Gummels <tgummels@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent c2315c18
Loading
Loading
Loading
Loading
+9 −13
Original line number Diff line number Diff line
@@ -603,11 +603,7 @@ static void kill_node(Node *e)
	struct dentry *dentry;

	write_lock(&entries_lock);
	dentry = e->dentry;
	if (dentry) {
	list_del_init(&e->list);
		e->dentry = NULL;
	}
	write_unlock(&entries_lock);

	if ((e->flags & MISC_FMT_OPEN_FILE) && e->interp_file) {
@@ -615,13 +611,12 @@ static void kill_node(Node *e)
		e->interp_file = NULL;
	}

	if (dentry) {
	dentry = e->dentry;
	drop_nlink(d_inode(dentry));
	d_drop(dentry);
	dput(dentry);
	simple_release_fs(&bm_mnt, &entry_count);
}
}

/* /<entry> */

@@ -665,6 +660,7 @@ static ssize_t bm_entry_write(struct file *file, const char __user *buffer,
		root = file_inode(file)->i_sb->s_root;
		inode_lock(d_inode(root));

		if (!list_empty(&e->list))
			kill_node(e);

		inode_unlock(d_inode(root));
@@ -794,7 +790,7 @@ static ssize_t bm_status_write(struct file *file, const char __user *buffer,
		inode_lock(d_inode(root));

		while (!list_empty(&entries))
			kill_node(list_entry(entries.next, Node, list));
			kill_node(list_first_entry(&entries, Node, list));

		inode_unlock(d_inode(root));
		break;