Skip to contentSkip to conversation

erofs-utils: fix memory leaks and allocation issue

PATCH Superseded Yuezhang Mo 2 participants 5 messages Sep 9, 2025 – Sep 10, 2025 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: fix memory leaks and allocation issue

This patch addresses 4 memory leaks and 1 allocation issue to ensure proper cleanup and allocation:

1. Fixed memory leak by freeing sbi->zmgr in z_erofs_compress_exit().
2. Fixed memory leak by freeing inode->chunkindexes in erofs_iput().
3. Fixed incorrect allocation of chunk index array in erofs_rebuild_write_blob_index() to ensure correct array allocation and avoid out-of-bounds access.
4. Fixed memory leak of struct erofs_blobchunk not being freed by calling erofs_blob_exit() in rebuild mode.
5. Fix memory leak by calling erofs_put_super().
In main(), erofs_read_superblock() is called only to get the block size. In erofs_mkfs_rebuild_load_trees(), erofs_read_superblock() is called again, causing sbi->devs to be overwritten without being released.

---
 lib/compress.c | 2 ++
 lib/inode.c    | 3 +++
 lib/rebuild.c  | 2 +-
 mkfs/main.c    | 3 ++-
 4 files changed, 8 insertions(+), 2 deletions(-)
lib/compress.c +2 -0
index 622a205..dd537ec 100644
@@ -2171,5 +2171,7 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
 		}
 #endif
 	}
+
+	free(sbi->zmgr);
 	return 0;
 }
lib/inode.c +3 -0
index 7ee6b3d..0882875 100644
@@ -159,6 +159,9 @@ unsigned int erofs_iput(struct erofs_inode *inode)
 	} else {
 		free(inode->i_link);
 	}
+
+	if (inode->chunkindexes)
+		free(inode->chunkindexes);
 	free(inode);
 	return 0;
 }
lib/rebuild.c +1 -1
index 0358567..18e5204 100644
@@ -186,7 +186,7 @@ static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 
 	unit = sizeof(struct erofs_inode_chunk_index);
 	inode->extent_isize = count * unit;
-	idx = malloc(max(sizeof(*idx), sizeof(void *)));
+	idx = calloc(count, max(sizeof(*idx), sizeof(void *)));
 	if (!idx)
 		return -ENOMEM;
 	inode->chunkindexes = idx;
mkfs/main.c +2 -1
index 28588db..bcde787 100644
@@ -1702,6 +1702,7 @@ int main(int argc, char **argv)
 			goto exit;
 		}
 		mkfs_blkszbits = src->blkszbits;
+		erofs_put_super(src);
 	}
 
 	if (!incremental_mode)
@@ -1908,7 +1909,7 @@ exit:
 	erofs_dev_close(&g_sbi);
 	erofs_cleanup_compress_hints();
 	erofs_cleanup_exclude_rules();
-	if (cfg.c_chunkbits)
+	if (cfg.c_chunkbits || source_mode == EROFS_MKFS_SOURCE_REBUILD)
 		erofs_blob_exit();
 	erofs_xattr_cleanup_name_prefixes();
 	erofs_rebuild_cleanup();
Signature · 1 line
2.43.0
#1 lore raw

Hi Yuezhang,

Quoted 16 lines · Yuezhang Mo

This patch addresses 4 memory leaks and 1 allocation issue to ensure proper cleanup and allocation:

1. Fixed memory leak by freeing sbi->zmgr in z_erofs_compress_exit().
2. Fixed memory leak by freeing inode->chunkindexes in erofs_iput().
3. Fixed incorrect allocation of chunk index array in erofs_rebuild_write_blob_index() to ensure correct array allocation and avoid out-of-bounds access.
4. Fixed memory leak of struct erofs_blobchunk not being freed by calling erofs_blob_exit() in rebuild mode.
5. Fix memory leak by calling erofs_put_super().
In main(), erofs_read_superblock() is called only to get the block size. In erofs_mkfs_rebuild_load_trees(), erofs_read_superblock() is called again, causing sbi->devs to be overwritten without being released.

Quoted 3 lines

Thanks for your patch and sorry for a bit delay...

Quoted 24 lines
---
  lib/compress.c | 2 ++
  lib/inode.c    | 3 +++
  lib/rebuild.c  | 2 +-
  mkfs/main.c    | 3 ++-
  4 files changed, 8 insertions(+), 2 deletions(-)
lib/compress.c +2 -0
index 622a205..dd537ec 100644
@@ -2171,5 +2171,7 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
  		}
  #endif
  	}
+
+	free(sbi->zmgr);
  	return 0;
  }
lib/inode.c +0 -0
index 7ee6b3d..0882875 100644

lib/inode.c unsigned int erofs_iput(struct erofs_inode *inode) · line 164

Quoted 6 lines
lib/inode.c +3 -0
@@ -159,6 +159,9 @@ unsigned int erofs_iput(struct erofs_inode *inode)
  	} else {
  		free(inode->i_link);
  	}
+
+	if (inode->chunkindexes)
+		free(inode->chunkindexes);

For now, this needs a check

	if (inode->datalayout == EROFS_INODE_CHUNK_BASED)
		free(inode->chunkindexes);

I admit it's not very clean, but otherwise it doesn't work since chunkindexes is in a union.

Quoted 21 lines
lib/inode.c +0 -0
  	free(inode);
  	return 0;
  }
lib/rebuild.c +1 -1
index 0358567..18e5204 100644
@@ -186,7 +186,7 @@ static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 
  	unit = sizeof(struct erofs_inode_chunk_index);
  	inode->extent_isize = count * unit;
-	idx = malloc(max(sizeof(*idx), sizeof(void *)));
+	idx = calloc(count, max(sizeof(*idx), sizeof(void *)));
  	if (!idx)
  		return -ENOMEM;
  	inode->chunkindexes = idx;
mkfs/main.c +0 -0
index 28588db..bcde787 100644

mkfs/main.c int main(int argc, char **argv) · line 1705

Quoted 4 lines
mkfs/main.c +1 -0
@@ -1702,6 +1702,7 @@ int main(int argc, char **argv)
  			goto exit;
  		}
  		mkfs_blkszbits = src->blkszbits;
+		erofs_put_super(src);

Do you really need to fix this now (considering mkfs.erofs will exit finally)?

In priciple, I think it should be fixed with a reference and something similiar to the kernel fill_super.

I'm not sure if you have more time to improve this anyway, but the current usage is not perfect on my side...

Thanks,
Gao Xiang

Quoted 12 lines
mkfs/main.c +1 -1
  	}
 
  	if (!incremental_mode)
@@ -1908,7 +1909,7 @@ exit:
  	erofs_dev_close(&g_sbi);
  	erofs_cleanup_compress_hints();
  	erofs_cleanup_exclude_rules();
-	if (cfg.c_chunkbits)
+	if (cfg.c_chunkbits || source_mode == EROFS_MKFS_SOURCE_REBUILD)
  		erofs_blob_exit();
  	erofs_xattr_cleanup_name_prefixes();
  	erofs_rebuild_cleanup();
#2 lore raw
Quoted 58 lines · Gao Xiang

Hi Yuezhang,

Quoted 16 lines · Yuezhang Mo

This patch addresses 4 memory leaks and 1 allocation issue to ensure proper cleanup and allocation:

1. Fixed memory leak by freeing sbi->zmgr in z_erofs_compress_exit().
2. Fixed memory leak by freeing inode->chunkindexes in erofs_iput().
3. Fixed incorrect allocation of chunk index array in erofs_rebuild_write_blob_index() to ensure correct array allocation and avoid out-of-bounds access.
4. Fixed memory leak of struct erofs_blobchunk not being freed by calling erofs_blob_exit() in rebuild mode.
5. Fix memory leak by calling erofs_put_super().
In main(), erofs_read_superblock() is called only to get the block size. In erofs_mkfs_rebuild_load_trees(), erofs_read_superblock() is called again, causing sbi->devs to be overwritten without being released.

Quoted 3 lines

Thanks for your patch and sorry for a bit delay...

Quoted 24 lines
---
  lib/compress.c | 2 ++
  lib/inode.c    | 3 +++
  lib/rebuild.c  | 2 +-
  mkfs/main.c    | 3 ++-
  4 files changed, 8 insertions(+), 2 deletions(-)
lib/compress.c +2 -0
index 622a205..dd537ec 100644
@@ -2171,5 +2171,7 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
              }
  #endif
      }
+
+     free(sbi->zmgr);
      return 0;
  }
lib/inode.c +0 -0
index 7ee6b3d..0882875 100644

lib/inode.c unsigned int erofs_iput(struct erofs_inode *inode) · line 164

Quoted 6 lines
lib/inode.c +3 -0
@@ -159,6 +159,9 @@ unsigned int erofs_iput(struct erofs_inode *inode)
      } else {
              free(inode->i_link);
      }
+
+     if (inode->chunkindexes)
+             free(inode->chunkindexes);

For now, this needs a check

Quoted 6 lines
        if (inode->datalayout == EROFS_INODE_CHUNK_BASED)
                free(inode->chunkindexes);

I admit it's not very clean, but otherwise it doesn't work since chunkindexes is in a union.

Okay, I will change to this check.

Quoted 20 lines
Quoted 20 lines
lib/inode.c +0 -0
      free(inode);
      return 0;
  }
lib/rebuild.c +1 -1
index 0358567..18e5204 100644
@@ -186,7 +186,7 @@ static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 
      unit = sizeof(struct erofs_inode_chunk_index);
      inode->extent_isize = count * unit;
-     idx = malloc(max(sizeof(*idx), sizeof(void *)));
+     idx = calloc(count, max(sizeof(*idx), sizeof(void *)));
      if (!idx)
              return -ENOMEM;
      inode->chunkindexes = idx;
mkfs/main.c +0 -0
index 28588db..bcde787 100644
Quoted 8 lines

mkfs/main.c int main(int argc, char **argv) · line 1705

Quoted 5 lines
mkfs/main.c +1 -0
@@ -1702,6 +1702,7 @@ int main(int argc, char **argv)
                      goto exit;
              }
              mkfs_blkszbits = src->blkszbits;
+             erofs_put_super(src);

Do you really need to fix this now (considering mkfs.erofs will exit finally)?

As you said, mkfs.erofs will exit finally, it won't affect users without this fix.

The main purpose of this patch is to fix the memory allocation issue in erofs_rebuild_write_blob_index(). It will cause a segmentation fault if there are deduplications(If there are few files, no segmentation fault occurs, but the files will be inaccessible.).

Quoted 6 lines

In priciple, I think it should be fixed with a reference and something similiar to the kernel fill_super.

I'm not sure if you have more time to improve this anyway, but the current usage is not perfect on my side...

The memory leak is caused by the erofs_sb_info of the first blob device being initialized twice, how to fix with reference? I do not get your point.

#3 lore raw
Quoted 97 lines · On 2025/9/9 18:01, [email protected] wrote:
Quoted 58 lines · Gao Xiang

Hi Yuezhang,

Quoted 16 lines · Yuezhang Mo

This patch addresses 4 memory leaks and 1 allocation issue to ensure proper cleanup and allocation:

1. Fixed memory leak by freeing sbi->zmgr in z_erofs_compress_exit().
2. Fixed memory leak by freeing inode->chunkindexes in erofs_iput().
3. Fixed incorrect allocation of chunk index array in erofs_rebuild_write_blob_index() to ensure correct array allocation and avoid out-of-bounds access.
4. Fixed memory leak of struct erofs_blobchunk not being freed by calling erofs_blob_exit() in rebuild mode.
5. Fix memory leak by calling erofs_put_super().
In main(), erofs_read_superblock() is called only to get the block size. In erofs_mkfs_rebuild_load_trees(), erofs_read_superblock() is called again, causing sbi->devs to be overwritten without being released.

Quoted 3 lines

Thanks for your patch and sorry for a bit delay...

Quoted 24 lines
---
   lib/compress.c | 2 ++
   lib/inode.c    | 3 +++
   lib/rebuild.c  | 2 +-
   mkfs/main.c    | 3 ++-
   4 files changed, 8 insertions(+), 2 deletions(-)
lib/compress.c +2 -0
index 622a205..dd537ec 100644
@@ -2171,5 +2171,7 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
               }
   #endif
       }
+
+     free(sbi->zmgr);
       return 0;
   }
lib/inode.c +0 -0
index 7ee6b3d..0882875 100644

lib/inode.c unsigned int erofs_iput(struct erofs_inode *inode) · line 164

Quoted 6 lines
lib/inode.c +3 -0
@@ -159,6 +159,9 @@ unsigned int erofs_iput(struct erofs_inode *inode)
       } else {
               free(inode->i_link);
       }
+
+     if (inode->chunkindexes)
+             free(inode->chunkindexes);

For now, this needs a check

Quoted 6 lines
         if (inode->datalayout == EROFS_INODE_CHUNK_BASED)
                 free(inode->chunkindexes);

I admit it's not very clean, but otherwise it doesn't work since chunkindexes is in a union.

Okay, I will change to this check.

Quoted 20 lines
Quoted 20 lines
lib/inode.c +0 -0
       free(inode);
       return 0;
   }
lib/rebuild.c +1 -1
index 0358567..18e5204 100644
@@ -186,7 +186,7 @@ static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 
       unit = sizeof(struct erofs_inode_chunk_index);
       inode->extent_isize = count * unit;
-     idx = malloc(max(sizeof(*idx), sizeof(void *)));
+     idx = calloc(count, max(sizeof(*idx), sizeof(void *)));
       if (!idx)
               return -ENOMEM;
       inode->chunkindexes = idx;
mkfs/main.c +0 -0
index 28588db..bcde787 100644
Quoted 8 lines

mkfs/main.c int main(int argc, char **argv) · line 1705

Quoted 5 lines
mkfs/main.c +1 -0
@@ -1702,6 +1702,7 @@ int main(int argc, char **argv)
                       goto exit;
               }
               mkfs_blkszbits = src->blkszbits;
+             erofs_put_super(src);

Do you really need to fix this now (considering mkfs.erofs will exit finally)?

Quoted 8 lines

As you said, mkfs.erofs will exit finally, it won't affect users without this fix.

The main purpose of this patch is to fix the memory allocation issue in erofs_rebuild_write_blob_index(). It will cause a segmentation fault if there are deduplications(If there are few files, no segmentation fault occurs, but the files will be inaccessible.).

Yes, so I tend to not fix erofs_put_super() in this patch.

Quoted 11 lines
Quoted 7 lines

In priciple, I think it should be fixed with a reference and something similiar to the kernel fill_super.

I'm not sure if you have more time to improve this anyway, but the current usage is not perfect on my side...

The memory leak is caused by the erofs_sb_info of the first blob device being initialized twice, how to fix with reference? I do not get your point.

I think we should skip erofs_read_superblock() if sbi is initialized at least.

As for reference I meant vfs_get_super() likewise, it calls fill_super() if .s_root is NULL.

Thanks,
Gao Xiang

#4 lore raw
Quoted 117 lines · On 2025/9/9 18:14 Gao Xiang wrote:
Quoted 97 lines · On 2025/9/9 18:01, [email protected] wrote:
Quoted 58 lines · Gao Xiang

Hi Yuezhang,

Quoted 16 lines · Yuezhang Mo

This patch addresses 4 memory leaks and 1 allocation issue to ensure proper cleanup and allocation:

1. Fixed memory leak by freeing sbi->zmgr in z_erofs_compress_exit().
2. Fixed memory leak by freeing inode->chunkindexes in erofs_iput().
3. Fixed incorrect allocation of chunk index array in erofs_rebuild_write_blob_index() to ensure correct array allocation and avoid out-of-bounds access.
4. Fixed memory leak of struct erofs_blobchunk not being freed by calling erofs_blob_exit() in rebuild mode.
5. Fix memory leak by calling erofs_put_super().
In main(), erofs_read_superblock() is called only to get the block size. In erofs_mkfs_rebuild_load_trees(), erofs_read_superblock() is called again, causing sbi->devs to be overwritten without being released.

Quoted 3 lines

Thanks for your patch and sorry for a bit delay...

Quoted 24 lines
---
   lib/compress.c | 2 ++
   lib/inode.c    | 3 +++
   lib/rebuild.c  | 2 +-
   mkfs/main.c    | 3 ++-
   4 files changed, 8 insertions(+), 2 deletions(-)
lib/compress.c +2 -0
index 622a205..dd537ec 100644
@@ -2171,5 +2171,7 @@ int z_erofs_compress_exit(struct erofs_sb_info *sbi)
               }
   #endif
       }
+
+     free(sbi->zmgr);
       return 0;
   }
lib/inode.c +0 -0
index 7ee6b3d..0882875 100644

lib/inode.c unsigned int erofs_iput(struct erofs_inode *inode) · line 164

Quoted 6 lines
lib/inode.c +3 -0
@@ -159,6 +159,9 @@ unsigned int erofs_iput(struct erofs_inode *inode)
       } else {
               free(inode->i_link);
       }
+
+     if (inode->chunkindexes)
+             free(inode->chunkindexes);

For now, this needs a check

Quoted 6 lines
         if (inode->datalayout == EROFS_INODE_CHUNK_BASED)
                 free(inode->chunkindexes);

I admit it's not very clean, but otherwise it doesn't work since chunkindexes is in a union.

Okay, I will change to this check.

Quoted 20 lines
Quoted 20 lines
lib/inode.c +0 -0
       free(inode);
       return 0;
   }
lib/rebuild.c +1 -1
index 0358567..18e5204 100644
@@ -186,7 +186,7 @@ static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 
       unit = sizeof(struct erofs_inode_chunk_index);
       inode->extent_isize = count * unit;
-     idx = malloc(max(sizeof(*idx), sizeof(void *)));
+     idx = calloc(count, max(sizeof(*idx), sizeof(void *)));
       if (!idx)
               return -ENOMEM;
       inode->chunkindexes = idx;
mkfs/main.c +0 -0
index 28588db..bcde787 100644
Quoted 8 lines

mkfs/main.c int main(int argc, char **argv) · line 1705

Quoted 5 lines
mkfs/main.c +1 -0
@@ -1702,6 +1702,7 @@ int main(int argc, char **argv)
                       goto exit;
               }
               mkfs_blkszbits = src->blkszbits;
+             erofs_put_super(src);

Do you really need to fix this now (considering mkfs.erofs will exit finally)?

Quoted 8 lines

As you said, mkfs.erofs will exit finally, it won't affect users without this fix.

The main purpose of this patch is to fix the memory allocation issue in erofs_rebuild_write_blob_index(). It will cause a segmentation fault if there are deduplications(If there are few files, no segmentation fault occurs, but the files will be inaccessible.).

Yes, so I tend to not fix erofs_put_super() in this patch.

Quoted 8 lines
Quoted 7 lines

In priciple, I think it should be fixed with a reference and something similiar to the kernel fill_super.

I'm not sure if you have more time to improve this anyway, but the current usage is not perfect on my side...

Quoted 6 lines
Quoted 3 lines

The memory leak is caused by the erofs_sb_info of the first blob device being initialized twice, how to fix with reference? I do not get your point.

I think we should skip erofs_read_superblock() if sbi is initialized at least.

I will skip erofs_read_superblock() if sbi->devs is not NULL.

Quoted 3 lines

As for reference I meant vfs_get_super() likewise, it calls fill_super() if .s_root is NULL.