Skip to contentSkip to conversation

jfs: fix divide error in dbNextAG

PATCH Superseded Pavel Skripkin 2 participants 3 messages Mar 19, 2022 – Mar 22, 2022 JFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 jfs: fix divide error in dbNextAG

Syzbot reported divide error in dbNextAG(). The problem was in missing validation check for malicious image.

Syzbot crafted an image with bmp->db_numag equal to 0. There wasn't any validation checks, but dbNextAG() blindly use bmp->db_numag in divide expression

Fix it by validating bmp->db_numag in dbMount() and return an error if image is malicious

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-and-tested-by [email protected]
---
 fs/jfs/jfs_dmap.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/jfs/jfs_dmap.c +4 -0
index 91f4ec93dab1..633a9aff0868 100644
@@ -148,6 +148,7 @@ static const s8 budtab[256] = {
  *	0	- success
  *	-ENOMEM	- insufficient memory
  *	-EIO	- i/o error
+ *	-EINVAL - wrong bmap data
  */
 int dbMount(struct inode *ipbmap)
 {
@@ -179,6 +180,9 @@ int dbMount(struct inode *ipbmap)
 	bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
 	bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
 	bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
+	if (!bmp->db_numag)
+		return -EINVAL;
+
 	bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
 	bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
 	bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/1 jfs: fix divide error in dbNextAG

Syzbot reported divide error in dbNextAG(). The problem was in missing validation check for malicious image.

Syzbot crafted an image with bmp->db_numag equal to 0. There wasn't any validation checks, but dbNextAG() blindly use bmp->db_numag in divide expression

Fix it by validating bmp->db_numag in dbMount() and return an error if image is malicious

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-and-tested-by [email protected]

---

Changes since v1:
- Add missing clean up in case of error

---
 fs/jfs/jfs_dmap.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/jfs/jfs_dmap.c +7 -0
index 91f4ec93dab1..d8502f4989d9 100644
@@ -148,6 +148,7 @@ static const s8 budtab[256] = {
  *	0	- success
  *	-ENOMEM	- insufficient memory
  *	-EIO	- i/o error
+ *	-EINVAL - wrong bmap data
  */
 int dbMount(struct inode *ipbmap)
 {
@@ -179,6 +180,12 @@ int dbMount(struct inode *ipbmap)
 	bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
 	bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
 	bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
+	if (!bmp->db_numag) {
+		release_metapage(mp);
+		kfree(bmp);
+		return -EINVAL;
+	}
+
 	bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
 	bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
 	bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);
Signature · 1 line
2.35.1
#2 lore raw

Thanks. I'll push this for 5.18.

Shaggy

Quoted 47 lines · Pavel Skripkin

Syzbot reported divide error in dbNextAG(). The problem was in missing validation check for malicious image.

Syzbot crafted an image with bmp->db_numag equal to 0. There wasn't any validation checks, but dbNextAG() blindly use bmp->db_numag in divide expression

Fix it by validating bmp->db_numag in dbMount() and return an error if image is malicious

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-and-tested-by [email protected]

---

Changes since v1:
- Add missing clean up in case of error

---
  fs/jfs/jfs_dmap.c | 7 +++++++
  1 file changed, 7 insertions(+)
fs/jfs/jfs_dmap.c +7 -0
index 91f4ec93dab1..d8502f4989d9 100644
@@ -148,6 +148,7 @@ static const s8 budtab[256] = {
   *	0	- success
   *	-ENOMEM	- insufficient memory
   *	-EIO	- i/o error
+ *	-EINVAL - wrong bmap data
   */
  int dbMount(struct inode *ipbmap)
  {
@@ -179,6 +180,12 @@ int dbMount(struct inode *ipbmap)
  	bmp->db_nfree = le64_to_cpu(dbmp_le->dn_nfree);
  	bmp->db_l2nbperpage = le32_to_cpu(dbmp_le->dn_l2nbperpage);
  	bmp->db_numag = le32_to_cpu(dbmp_le->dn_numag);
+	if (!bmp->db_numag) {
+		release_metapage(mp);
+		kfree(bmp);
+		return -EINVAL;
+	}
+
  	bmp->db_maxlevel = le32_to_cpu(dbmp_le->dn_maxlevel);
  	bmp->db_maxag = le32_to_cpu(dbmp_le->dn_maxag);
  	bmp->db_agpref = le32_to_cpu(dbmp_le->dn_agpref);