UBIFS: add missing znode freeing in tcn_insert()
Subsystems (2)
PATCH 1/1 UBIFS: add missing znode freeing in tcn_insert()
In case the zi allocation fails in the do_split label, we will fail freeing zn that we allocated before, add a missing kfree.
--- fs/ubifs/tnc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ubifs/tnc.c +3 -1
@@ -2105,8 +2105,10 @@ do_split:dbg_tnc ("creating new zroot at level %d" , znode->level +1 ); zi =kzalloc (c->max_znode_sz,GFP_NOFS ); -if (!zi) +if (!zi) { +kfree (zn);return -ENOMEM ; + } zi->child_cnt =2 ; zi->level = znode->level +1 ;
Signature · 1 line
1.8.3.2
Quoted 13 lines · Florian Fainelli
In case the zi allocation fails in the
do_splitlabel, we will fail freeing zn that we allocated before, add a missing kfree.Signed-off-by Florian Fainelli <[email protected]>--- fs/ubifs/tnc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/ubifs/tnc.c +0
-0
fs/ubifs/tnc.c do_split: · line 2112
Quoted 9 lines
fs/ubifs/tnc.c +3
-1@@ -2105,8 +2105,10 @@ do_split:dbg_tnc ("creating new zroot at level %d" , znode->level +1 ); zi =kzalloc (c->max_znode_sz,GFP_NOFS ); -if (!zi) +if (!zi) { +kfree (zn);return -ENOMEM ; + }
I'm not sure whether this is correct.
Around line 2050 we have:
... else {
/* Insert into new znode */
zi = zn; <---------
n -= keep;
/* Re-parent */
if (zn->level != 0)
zbr->znode->parent = zn;
}And later:insert_zbranch(zi, zbr, n);
By freeing zn you may introduce a use after free bug.
Signature · 2 lines
Thanks, //richard
2014-03-08 2:46 GMT-08:00 Richard Weinberger <[email protected]>:
Quoted 36 lines · Florian Fainelli
Quoted 13 lines
In case the zi allocation fails in the
do_splitlabel, we will fail freeing zn that we allocated before, add a missing kfree.Signed-off-by Florian Fainelli <[email protected]>--- fs/ubifs/tnc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/ubifs/tnc.c +0
-0fs/ubifs/tnc.c do_split: · line 2112
Quoted 9 lines
fs/ubifs/tnc.c +3
-1@@ -2105,8 +2105,10 @@ do_split:dbg_tnc ("creating new zroot at level %d" , znode->level +1 ); zi =kzalloc (c->max_znode_sz,GFP_NOFS ); -if (!zi) +if (!zi) { +kfree (zn);return -ENOMEM ; + }I'm not sure whether this is correct.
Around line 2050 we have:
... else { /* Insert into new znode */ zi = zn; <--------- n -= keep; /* Re-parent */ if (zn->level != 0) zbr->znode->parent = zn; }
Quoted 4 lines
And later:
insert_zbranch(zi, zbr, n);By freeing zn you may introduce a use after free bug.
Yes, you are right, I cannot find a good way to make sure this is not the case, variables re-use in this function makes it particularly hard to read BTW.
Signature · 1 line
Florian
Quoted 37 lines · Florian Fainelli
2014-03-08 2:46 GMT-08:00 Richard Weinberger <[email protected]>:
Quoted 36 lines · Florian Fainelli
Quoted 13 lines
In case the zi allocation fails in the
do_splitlabel, we will fail freeing zn that we allocated before, add a missing kfree.Signed-off-by Florian Fainelli <[email protected]>--- fs/ubifs/tnc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/ubifs/tnc.c +0
-0fs/ubifs/tnc.c do_split: · line 2112
Quoted 9 lines
fs/ubifs/tnc.c +3
-1@@ -2105,8 +2105,10 @@ do_split:dbg_tnc ("creating new zroot at level %d" , znode->level +1 ); zi =kzalloc (c->max_znode_sz,GFP_NOFS ); -if (!zi) +if (!zi) { +kfree (zn);return -ENOMEM ; + }I'm not sure whether this is correct.
Around line 2050 we have:
... else { /* Insert into new znode */ zi = zn; <--------- n -= keep; /* Re-parent */ if (zn->level != 0) zbr->znode->parent = zn; }
Quoted 7 lines
Quoted 4 lines
And later:
insert_zbranch(zi, zbr, n);By freeing zn you may introduce a use after free bug.
Yes, you are right, I cannot find a good way to make sure this is not the case,
I think you do not need to free it. It was allocated and inserted to TNC. TNC is a global data structure. It gets destroyed (all elements get freed) when the FS is unmounted. Or if mount fails, it gets freed on the error path.
So no need to worry about that element.
Quoted 2 lines
variables re-use in this function makes it particularly hard
to read BTW.
Yes, it could be better, I guess. But this kind of "algorithmic" code is rarely very readable, it is just not very easy. More comments could help too.
Signature · 2 lines
Best Regards, Artem Bityutskiy