When we learn the e2fsprogs source code, we
find some bugs and try to fix them. At the same time,
static scanning tools were used to analyze the code,
and several possible problems were found.
Zhiqiang Liu (6):
misc: fix potential segmentation fault problem in scandir()
lib/ss/error.c: check return value malloc in ss_name()
hashmap: change return value type of ext2fs_hashmap_add()
misc/lsattr: check whether path is NULL in lsattr_dir_proc()
ext2ed: fix potential NULL pointer dereference in dupstr()
argv_parse: check return value of malloc in argv_pars
Wu Guanghao (6):
profile_create_node: set magic before strdup(name) to fix memory leak
tdb_transaction_recover: fix memory leak
zap_sector: fix memory leak
ss_add_info_dir: fix memory leak and check whether NULL pointer
ss_create_invocation: fix memory leak and check whether NULL pointer
append_pathname: append_pathname: check the value returned by realloc
to avoid segfault
contrib/android/base_fs.c | 12 +++++++++---
contrib/fsstress.c | 10 ++++++++--
ext2ed/main.c | 2 ++
lib/ext2fs/fileio.c | 11 +++++++++--
lib/ext2fs/hashmap.c | 12 ++++++++++--
lib/ext2fs/hashmap.h | 4 ++--
lib/ext2fs/tdb.c | 1 +
lib/ss/error.c | 2 ++
lib/ss/help.c | 5 +++++
lib/ss/invocation.c | 38 ++++++++++++++++++++++++++++++++------
lib/support/argv_parse.c | 2 ++
lib/support/profile.c | 3 ++-
misc/create_inode.c | 3 +++
misc/lsattr.c | 6 ++++++
misc/mke2fs.c | 1 +
15 files changed, 94 insertions(+), 18 deletions(-)
--
2.19.1
If new->magic != PROF_MAGIC_NODE, profile_free_node() don't free node.
This will cause the node to be unable to be released correctly and
a memory leak will occur.
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
Reviewed-by: Wu Bo <redacted>
---
lib/support/profile.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
In tdb_transaction_recover(), need free data before return,
otherwise it will cause memory leak.
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
Reviewed-by: Wu Bo <redacted>
---
lib/ext2fs/tdb.c | 1 +
1 file changed, 1 insertion(+)
In zap_sector(), need free buf before return,
otherwise it will cause memory leak.
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
Reviewed-by: Wu Bo <redacted>
---
misc/mke2fs.c | 1 +
1 file changed, 1 insertion(+)
In ss_add_info_dir(), need free info->info_dirs before return,
otherwise it will cause memory leak. At the same time, it is necessary
to check whether dirs[n_dirs] is a null pointer, otherwise a segmentation
fault will occur.
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
Reviewed-by: Wu Bo <redacted>
---
lib/ss/help.c | 5 +++++
1 file changed, 5 insertions(+)
In ss_create_invocation(), it is necessary to check whether
returned by malloc is a null pointer.
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
---
lib/ss/invocation.c | 38 ++++++++++++++++++++++++++++++++------
1 file changed, 32 insertions(+), 6 deletions(-)
In append_pathname(), we need to add a new path to save the value returned by realloc,
otherwise the name->path may be NULL, causing segfault
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
---
contrib/fsstress.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
In argv_parse(), return value of malloc should be checked
whether it is NULL, otherwise, it may cause a segfault error.
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
---
lib/support/argv_parse.c | 2 ++
1 file changed, 2 insertions(+)
@@ -771,6 +771,9 @@ static int scandir(const char *dir_name, struct dirent ***name_list,}// add the copy of dirent to the listtemp_list[num_dent]=(structdirent*)malloc((dent->d_reclen+3)&~3);+if(!temp_list[num_dent]){+gotoout;+}memcpy(temp_list[num_dent],dent,dent->d_reclen);num_dent++;}
In ss_name(), we should check return value of malloc(),
otherwise, it may cause a segmentation fault problem.
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
---
lib/ss/error.c | 2 ++
1 file changed, 2 insertions(+)
In ext2fs_hashmap_add(), new entry is allocated by calling
malloc(). If malloc() return NULL, it will cause a
segmentation fault problem.
Here, we change return value type of ext2fs_hashmap_add()
from void to int. If allocating new entry fails, we will
return 1, and the callers should also verify the return
value of ext2fs_hashmap_add().
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
---
contrib/android/base_fs.c | 12 +++++++++---
lib/ext2fs/fileio.c | 11 +++++++++--
lib/ext2fs/hashmap.c | 12 ++++++++++--
lib/ext2fs/hashmap.h | 4 ++--
4 files changed, 30 insertions(+), 9 deletions(-)
In lsattr_dir_proc(), if malloc() return NULL, it will cause
a segmentation fault problem.
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
---
misc/lsattr.c | 6 ++++++
1 file changed, 6 insertions(+)
In dupstr(), we should check return value of malloc().
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
Reviewed-by: Wu Bo <redacted>
---
ext2ed/main.c | 2 ++
1 file changed, 2 insertions(+)
Hello Wu,
Thank you for the fixes.
It looks like free and return operators should be placed in {} block.
{
free(buf);
return;
}
Now function returns any time block is read successfully.
Also, this patch can not be applied cleanly to the master HEAD because of wrong offsets. Please rebase.
Best regards,
Artem Blagodarenko.
quoted hunk
On 24 May 2021, at 14:20, Wu Guanghao [off-list ref] wrote:
In zap_sector(), need free buf before return,
otherwise it will cause memory leak.
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
Reviewed-by: Wu Bo <redacted>
---
misc/mke2fs.c | 1 +
1 file changed, 1 insertion(+)
Thank you for your comments, I will modify it in the v2 version.
Best regards
Wu Guanghao
在 2021/5/24 22:40, Благодаренко Артём 写道:
Hello Wu,
Thank you for the fixes.
It looks like free and return operators should be placed in {} block.
{
free(buf);
return;
}
Now function returns any time block is read successfully.
Also, this patch can not be applied cleanly to the master HEAD because of wrong offsets. Please rebase.
Best regards,
Artem Blagodarenko.
quoted
On 24 May 2021, at 14:20, Wu Guanghao [off-list ref] wrote:
In zap_sector(), need free buf before return,
otherwise it will cause memory leak.
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
Reviewed-by: Wu Bo <redacted>
---
misc/mke2fs.c | 1 +
1 file changed, 1 insertion(+)
There is a problem with this patch. "argv" is spelled as "arcv".
This problem will be resolved in the v2 version.
在 2021/5/24 19:23, Wu Guanghao 写道:
quoted hunk
In argv_parse(), return value of malloc should be checked
whether it is NULL, otherwise, it may cause a segfault error.
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
---
lib/support/argv_parse.c | 2 ++
1 file changed, 2 insertions(+)
On 24 May 2021, at 14:23, Wu Guanghao [off-list ref] wrote:
In append_pathname(), we need to add a new path to save the value returned by realloc,
otherwise the name->path may be NULL, causing segfault
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
---
contrib/fsstress.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
Hello Artem Blagodarenko,
Thank you for your review, it should be chdir instead of chadir,
I will modify it in the v2 version. Next time I will take care not to make similar mistakes.
Thanks a lot.
Thanks,
Best regards,
Wu Guanghao
在 2021/5/25 17:37, Благодаренко Артём 写道:
Hello Wu,
Thanks for the patch.
quoted
On 24 May 2021, at 14:23, Wu Guanghao [off-list ref] wrote:
In append_pathname(), we need to add a new path to save the value returned by realloc,
otherwise the name->path may be NULL, causing segfault
Signed-off-by: Wu Guanghao <redacted>
Signed-off-by: Zhiqiang Liu <redacted>
---
contrib/fsstress.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
On 24 May 2021, at 14:25, Wu Guanghao [off-list ref] wrote:
In ext2fs_hashmap_add(), new entry is allocated by calling
malloc(). If malloc() return NULL, it will cause a
segmentation fault problem.
Here, we change return value type of ext2fs_hashmap_add()
from void to int. If allocating new entry fails, we will
return 1, and the callers should also verify the return
value of ext2fs_hashmap_add().
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
---
contrib/android/base_fs.c | 12 +++++++++---
lib/ext2fs/fileio.c | 11 +++++++++--
lib/ext2fs/hashmap.c | 12 ++++++++++--
lib/ext2fs/hashmap.h | 4 ++--
4 files changed, 30 insertions(+), 9 deletions(-)
if (new_block) {
new_block->physblock = file->physblock;
- ext2fs_hashmap_add(fs->block_sha_map, new_block,
- new_block->sha, sizeof(new_block->sha));
+ int ret = ext2fs_hashmap_add(fs->block_sha_map,
+ new_block, new_block->sha,
+ sizeof(new_block->sha));
+ if (ret) {
+ retval = EXT2_ET_NO_MEMORY;
+ free(new_block);
+ new_block = NULL;
There is no need to set new_block to NULL here.. new_block is a local variable and the function returns after "fail" label.
Same for blocks above which also jump to the “fail” label.
From: Zhiqiang Liu <hidden> Date: 2021-05-29 09:55:32
On 2021/5/25 20:45, Благодаренко Артём wrote:
quoted
On 24 May 2021, at 14:25, Wu Guanghao [off-list ref] wrote:
In ext2fs_hashmap_add(), new entry is allocated by calling
malloc(). If malloc() return NULL, it will cause a
segmentation fault problem.
Here, we change return value type of ext2fs_hashmap_add()
from void to int. If allocating new entry fails, we will
return 1, and the callers should also verify the return
value of ext2fs_hashmap_add().
Signed-off-by: Zhiqiang Liu <redacted>
Signed-off-by: Wu Guanghao <redacted>
---
contrib/android/base_fs.c | 12 +++++++++---
lib/ext2fs/fileio.c | 11 +++++++++--
lib/ext2fs/hashmap.c | 12 ++++++++++--
lib/ext2fs/hashmap.h | 4 ++--
4 files changed, 30 insertions(+), 9 deletions(-)
if (new_block) {
new_block->physblock = file->physblock;
- ext2fs_hashmap_add(fs->block_sha_map, new_block,
- new_block->sha, sizeof(new_block->sha));
+ int ret = ext2fs_hashmap_add(fs->block_sha_map,
+ new_block, new_block->sha,
+ sizeof(new_block->sha));
+ if (ret) {
+ retval = EXT2_ET_NO_MEMORY;
+ free(new_block);
+ new_block = NULL;
There is no need to set new_block to NULL here.. new_block is a local variable and the function returns after "fail" label.
Same for blocks above which also jump to the “fail” label.
Thanks for your suggestion.
we will send the v2 patches.