Skip to content

Commit 7a8b048

Browse files
Al Virogregkh
authored andcommitted
fix cgroup_do_mount() handling of failure exits
commit 399504e upstream. same story as with last May fixes in sysfs (7b745a4 "unfuck sysfs_mount()"); new_sb is left uninitialized in case of early errors in kernfs_mount_ns() and papering over it by treating any error from kernfs_mount_ns() as equivalent to !new_ns ends up conflating the cases when objects had never been transferred to a superblock with ones when that has happened and resulting new superblock had been dropped. Easily fixed (same way as in sysfs case). Additionally, there's a superblock leak on kernfs_node_dentry() failure *and* a dentry leak inside kernfs_node_dentry() itself - the latter on probably impossible errors, but the former not impossible to trigger (as the matter of fact, injecting allocation failures at that point *does* trigger it). Cc: stable@kernel.org Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3b8da13 commit 7a8b048

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎fs/kernfs/mount.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,10 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
196196
return dentry;
197197

198198
knparent = find_next_ancestor(kn, NULL);
199-
if (WARN_ON(!knparent))
199+
if (WARN_ON(!knparent)) {
200+
dput(dentry);
200201
return ERR_PTR(-EINVAL);
202+
}
201203

202204
do {
203205
struct dentry *dtmp;
@@ -206,8 +208,10 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
206208
if (kn == knparent)
207209
return dentry;
208210
kntmp = find_next_ancestor(kn, knparent);
209-
if (WARN_ON(!kntmp))
211+
if (WARN_ON(!kntmp)) {
212+
dput(dentry);
210213
return ERR_PTR(-EINVAL);
214+
}
211215
dtmp = lookup_one_len_unlocked(kntmp->name, dentry,
212216
strlen(kntmp->name));
213217
dput(dentry);

‎kernel/cgroup/cgroup.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
19981998
struct cgroup_namespace *ns)
19991999
{
20002000
struct dentry *dentry;
2001-
bool new_sb;
2001+
bool new_sb = false;
20022002

20032003
dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb);
20042004

@@ -2008,6 +2008,7 @@ struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
20082008
*/
20092009
if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
20102010
struct dentry *nsdentry;
2011+
struct super_block *sb = dentry->d_sb;
20112012
struct cgroup *cgrp;
20122013

20132014
mutex_lock(&cgroup_mutex);
@@ -2018,12 +2019,14 @@ struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
20182019
spin_unlock_irq(&css_set_lock);
20192020
mutex_unlock(&cgroup_mutex);
20202021

2021-
nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
2022+
nsdentry = kernfs_node_dentry(cgrp->kn, sb);
20222023
dput(dentry);
2024+
if (IS_ERR(nsdentry))
2025+
deactivate_locked_super(sb);
20232026
dentry = nsdentry;
20242027
}
20252028

2026-
if (IS_ERR(dentry) || !new_sb)
2029+
if (!new_sb)
20272030
cgroup_put(&root->cgrp);
20282031

20292032
return dentry;

0 commit comments

Comments
 (0)