Skip to contentSkip to conversation

erofs-utils: sbi->devs should be cleared after freed

PATCH Superseded Gao Xiang 2 participants 3 messages Aug 21, 2023 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: sbi->devs should be cleared after freed

Otherwise, it could cause double-free if sbi reuses when fuzzing [1].

[1] https://github.com/erofs/erofsnightly/actions/runs/5921003885/job/16053013007

---
 lib/super.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/super.c +6 -2
index 58e2574..6c5b073 100644
@@ -123,14 +123,18 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 		return ret;
 
 	ret = erofs_xattr_prefixes_init(sbi);
-	if (ret)
+	if (ret) {
 		free(sbi->devs);
+		sbi->devs = NULL;
+	}
 	return ret;
 }
 
 void erofs_put_super(struct erofs_sb_info *sbi)
 {
-	if (sbi->devs)
+	if (sbi->devs) {
 		free(sbi->devs);
+		sbi->devs = NULL;
+	}
 	erofs_xattr_prefixes_cleanup(sbi);
 }
Signature · 1 line
2.24.4
author
#1 lore raw

PATCH v2 1/1 erofs-utils: sbi->devs should be cleared after freed

Otherwise, it could cause double-free if sbi reuses when fuzzing [1].

[1] https://github.com/erofs/erofsnightly/actions/runs/5921003885/job/16053013007

---
changes since v1:
- add a missing sbi->devs = NULL in erofs_init_devices().

 lib/super.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
lib/super.c +7 -2
index 58e2574..38caf4d 100644
@@ -53,6 +53,7 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 		ret = dev_read(sbi, 0, &dis, pos, sizeof(dis));
 		if (ret < 0) {
 			free(sbi->devs);
+			sbi->devs = NULL;
 			return ret;
 		}
 
@@ -123,14 +124,18 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 		return ret;
 
 	ret = erofs_xattr_prefixes_init(sbi);
-	if (ret)
+	if (ret && sbi->devs) {
 		free(sbi->devs);
+		sbi->devs = NULL;
+	}
 	return ret;
 }
 
 void erofs_put_super(struct erofs_sb_info *sbi)
 {
-	if (sbi->devs)
+	if (sbi->devs) {
 		free(sbi->devs);
+		sbi->devs = NULL;
+	}
 	erofs_xattr_prefixes_cleanup(sbi);
 }
Signature · 1 line
2.24.4
Reviewed-by on v2 1/1
#2 lore raw
Quoted 5 lines · Gao Xiang

Otherwise, it could cause double-free if sbi reuses when fuzzing [1].

[1] https://github.com/erofs/erofsnightly/actions/runs/5921003885/job/16053013007

LGTM.

Quoted 40 lines

---
changes since v1:
- add a missing sbi->devs = NULL in erofs_init_devices().

 lib/super.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
lib/super.c +7 -2
index 58e2574..38caf4d 100644
@@ -53,6 +53,7 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 		ret = dev_read(sbi, 0, &dis, pos, sizeof(dis));
 		if (ret < 0) {
 			free(sbi->devs);
+			sbi->devs = NULL;
 			return ret;
 		}
 
@@ -123,14 +124,18 @@ int erofs_read_superblock(struct erofs_sb_info *sbi)
 		return ret;
 
 	ret = erofs_xattr_prefixes_init(sbi);
-	if (ret)
+	if (ret && sbi->devs) {
 		free(sbi->devs);
+		sbi->devs = NULL;
+	}
 	return ret;
 }
 
 void erofs_put_super(struct erofs_sb_info *sbi)
 {
-	if (sbi->devs)
+	if (sbi->devs) {
 		free(sbi->devs);
+		sbi->devs = NULL;
+	}
 	erofs_xattr_prefixes_cleanup(sbi);
 }
Signature · 2 lines
Thanks,
Jingbo