Skip to content

Commit c1befb8

Browse files
JianyuZhangregkh
authored andcommitted
kernfs: fix a subdir count leak
Currently kernfs_link_sibling() increates parent->dir.subdirs before adding the node into parent's chidren rb tree. Because it is possible that kernfs_link_sibling() couldn't find a suitable slot and bail out, this leads to a mismatch between elevated subdir count with actual children node numbers. This patches fix this problem, by moving the subdir accouting after the actual addtion happening. Signed-off-by: Jianyu Zhan <nasa4836@gmail.com> Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a798c10 commit c1befb8

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎fs/kernfs/dir.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,6 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
232232
struct rb_node **node = &kn->parent->dir.children.rb_node;
233233
struct rb_node *parent = NULL;
234234

235-
if (kernfs_type(kn) == KERNFS_DIR)
236-
kn->parent->dir.subdirs++;
237-
238235
while (*node) {
239236
struct kernfs_node *pos;
240237
int result;
@@ -249,9 +246,15 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
249246
else
250247
return -EEXIST;
251248
}
249+
252250
/* add new node and rebalance the tree */
253251
rb_link_node(&kn->rb, parent, node);
254252
rb_insert_color(&kn->rb, &kn->parent->dir.children);
253+
254+
/* successfully added, account subdir number */
255+
if (kernfs_type(kn) == KERNFS_DIR)
256+
kn->parent->dir.subdirs++;
257+
255258
return 0;
256259
}
257260

0 commit comments

Comments
 (0)