Skip to content

Commit 399504e

Browse files
author
Al Viro
committed
fix cgroup_do_mount() handling of failure exits
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>
1 parent 1c7fc5c commit 399504e

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
@@ -2033,7 +2033,7 @@ struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
20332033
struct cgroup_namespace *ns)
20342034
{
20352035
struct dentry *dentry;
2036-
bool new_sb;
2036+
bool new_sb = false;
20372037

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

@@ -2043,6 +2043,7 @@ struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
20432043
*/
20442044
if (!IS_ERR(dentry) && ns != &init_cgroup_ns) {
20452045
struct dentry *nsdentry;
2046+
struct super_block *sb = dentry->d_sb;
20462047
struct cgroup *cgrp;
20472048

20482049
mutex_lock(&cgroup_mutex);
@@ -2053,12 +2054,14 @@ struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags,
20532054
spin_unlock_irq(&css_set_lock);
20542055
mutex_unlock(&cgroup_mutex);
20552056

2056-
nsdentry = kernfs_node_dentry(cgrp->kn, dentry->d_sb);
2057+
nsdentry = kernfs_node_dentry(cgrp->kn, sb);
20572058
dput(dentry);
2059+
if (IS_ERR(nsdentry))
2060+
deactivate_locked_super(sb);
20582061
dentry = nsdentry;
20592062
}
20602063

2061-
if (IS_ERR(dentry) || !new_sb)
2064+
if (!new_sb)
20622065
cgroup_put(&root->cgrp);
20632066

20642067
return dentry;

0 commit comments

Comments
 (0)