Skip to contentSkip to conversation

nilfs2: fix potential use after free

PATCH In review Pan Bian 2 participants 3 messages Nov 26, 2018 – Dec 3, 2018 NILFS2 filesystem

lore
Share & tools
lore

Pan Bian

author
#0 lore raw

PATCH 1/1 nilfs2: fix potential use after free

brelse(bh) is called to drop the reference count of bh when the call to nilfs_dat_translate fails. If the reference count hits 0, bh may be freed. However, bh->b_page is unlocked and put after that, which may result in a use-after-free bug. This patch moves the release operation after unlocking and putting the page.

---
 fs/nilfs2/gcinode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nilfs2/gcinode.c +3 -3
index aa3c328..a24bb29 100644
@@ -73,10 +73,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
 		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
 
 		err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
-		if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
-			brelse(bh);
+		if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
 			goto failed;
-		}
 	}
 
 	lock_buffer(bh);
@@ -102,6 +100,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  failed:
 	unlock_page(bh->b_page);
 	put_page(bh->b_page);
+	if (unlikely(err))
+		brelse(bh);
 	return err;
 }
 
Signature · 1 line
2.7.4
#1 lore raw

Hi, Pan Bian

Thank you for feeding back this patch.
I reviewed this and am thinking this must be sent to upstream.

Did you see any kernel oops on this bug ?

Regards,
Ryusuke Konishi

Quoted 40 lines · Pan Bian

brelse(bh) is called to drop the reference count of bh when the call to nilfs_dat_translate fails. If the reference count hits 0, bh may be freed. However, bh->b_page is unlocked and put after that, which may result in a use-after-free bug. This patch moves the release operation after unlocking and putting the page.

---
 fs/nilfs2/gcinode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nilfs2/gcinode.c +3 -3
index aa3c328..a24bb29 100644
@@ -73,10 +73,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
 		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
 
 		err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
-		if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
-			brelse(bh);
+		if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
 			goto failed;
-		}
 	}
 
 	lock_buffer(bh);
@@ -102,6 +100,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  failed:
 	unlock_page(bh->b_page);
 	put_page(bh->b_page);
+	if (unlikely(err))
+		brelse(bh);
 	return err;
 }
 
Signature · 1 line
2.7.4
author
#2 lore raw
Quoted 6 lines · Ryusuke Konishi

Hi, Pan Bian

Thank you for feeding back this patch.
I reviewed this and am thinking this must be sent to upstream.

Did you see any kernel oops on this bug ?

Not yet. In fact, I found it with a static method.

Best regards,
Pan Bian

Quoted 45 lines

Regards,
Ryusuke Konishi

Quoted 40 lines · Pan Bian

brelse(bh) is called to drop the reference count of bh when the call to nilfs_dat_translate fails. If the reference count hits 0, bh may be freed. However, bh->b_page is unlocked and put after that, which may result in a use-after-free bug. This patch moves the release operation after unlocking and putting the page.

---
 fs/nilfs2/gcinode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nilfs2/gcinode.c +3 -3
index aa3c328..a24bb29 100644
@@ -73,10 +73,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
 		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
 
 		err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
-		if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
-			brelse(bh);
+		if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
 			goto failed;
-		}
 	}
 
 	lock_buffer(bh);
@@ -102,6 +100,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  failed:
 	unlock_page(bh->b_page);
 	put_page(bh->b_page);
+	if (unlikely(err))
+		brelse(bh);
 	return err;
 }
 
Signature · 1 line
2.7.4