Skip to contentSkip to conversation

gfs2: Fix use-after-free and reference leaks in iomap inline data paths

PATCH In review Deepanshu Kartikey 2 participants 2 messages Jan 30 GFS2 file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gfs2: Fix use-after-free and reference leaks in iomap inline data paths

The inline data buffer head (dibh) is being released prematurely in gfs2_iomap_begin() via release_metapath(), while iomap->inline_data still points to dibh->b_data. This causes a use-after-free when iomap_write_end_inline() later attempts to write to the inline data area.

Additionally, gfs2_iomap_get() and gfs2_iomap_alloc() leak buffer head references when they call __gfs2_iomap_get() for inline data files, as they don't release the dibh reference stored in iomap->private.

The bug sequence for the use-after-free:
1. __gfs2_iomap_get() calls gfs2_meta_inode_buffer() to read inode metadata into dibh
2. Sets iomap->inline_data = dibh->b_data + sizeof(struct gfs2_dinode)
3. Calls release_metapath() which calls brelse(dibh), dropping refcount to 0
4. kswapd reclaims the page (~39ms later in the syzbot report)
5. iomap_write_end_inline() tries to memcpy() to iomap->inline_data
6. KASAN detects use-after-free write to freed memory

Fix by storing dibh in iomap->private and incrementing its refcount with get_bh() in __gfs2_iomap_get(). The buffer is then properly released in:
- gfs2_iomap_end() for the iomap iterator path
- gfs2_iomap_get() before returning
- gfs2_iomap_alloc() before returning

This ensures the page stays alive for the entire iomap operation and prevents reference leaks.

Changes in v2:
- Simplified iomap->private cleanup in gfs2_iomap_end() per feedback from Andreas Gruenbacher (removed redundant type check and explicit NULL assignment)
- Added missing brelse(iomap->private) in gfs2_iomap_get() to fix buffer head reference leak
- Added missing brelse(iomap->private) in gfs2_iomap_alloc() to fix buffer head reference leak

Note: A C reproducer is not available for this issue. The fix is based on analysis of the KASAN report and code review.

---
 fs/gfs2/bmap.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/gfs2/bmap.c +9 -0
index 131091520de6..81a958694d1f 100644
@@ -887,6 +887,8 @@ static int __gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
 			      sizeof(struct gfs2_dinode);
 		iomap->type = IOMAP_INLINE;
 		iomap->inline_data = dibh->b_data + sizeof(struct gfs2_dinode);
+		iomap->private = dibh;
+		get_bh(dibh);
 		goto out;
 	}
 
@@ -1144,6 +1146,9 @@ static int gfs2_iomap_end(struct inode *inode, loff_t pos, loff_t length,
 	struct gfs2_inode *ip = GFS2_I(inode);
 	struct gfs2_sbd *sdp = GFS2_SB(inode);
 
+	if (iomap->private)
+		brelse(iomap->private);
+
 	switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) {
 	case IOMAP_WRITE:
 		if (flags & IOMAP_DIRECT)
@@ -1419,6 +1424,8 @@ int gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
 
 	ret = __gfs2_iomap_get(inode, pos, length, 0, iomap, &mp);
 	release_metapath(&mp);
+	if (iomap->private)
+		brelse(iomap->private);
 	return ret;
 }
 
@@ -1432,6 +1439,8 @@ int gfs2_iomap_alloc(struct inode *inode, loff_t pos, loff_t length,
 	if (!ret && iomap->type == IOMAP_HOLE)
 		ret = __gfs2_iomap_alloc(inode, iomap, &mp);
 	release_metapath(&mp);
+	if (iomap->private)
+		brelse(iomap->private);
 	return ret;
 }
 
Signature · 1 line
2.43.0
#1 lore raw

I'm going to go with the below version instead.

Thanks,
Andreas

--

The inline data buffer head (dibh) is being released prematurely in gfs2_iomap_begin() via release_metapath() while iomap->inline_data still points to dibh->b_data. This causes a use-after-free when iomap_write_end_inline() later attempts to write to the inline data area.

The bug sequence:
1. gfs2_iomap_begin() calls gfs2_meta_inode_buffer() to read inode metadata into dibh
2. Sets iomap->inline_data = dibh->b_data + sizeof(struct gfs2_dinode)
3. Calls release_metapath() which calls brelse(dibh), dropping refcount to 0
4. kswapd reclaims the page (~39ms later in the syzbot report)
5. iomap_write_end_inline() tries to memcpy() to iomap->inline_data
6. KASAN detects use-after-free write to freed memory

Fix by storing dibh in iomap->private and incrementing its refcount with get_bh() in gfs2_iomap_begin(). The buffer is then properly released in gfs2_iomap_end() after the inline write completes, ensuring the page stays alive for the entire iomap operation.

Note: A C reproducer is not available for this issue. The fix is based on analysis of the KASAN report and code review showing the buffer head is freed before use.

[agruenba: Take buffer head reference in gfs2_iomap_begin() to avoid leaks in gfs2_iomap_get() and gfs2_iomap_alloc().]

Reported-by [email protected]
Fixes d0a22a4b03b8 ("gfs2: Fix iomap write page reclaim deadlock")
---
 fs/gfs2/bmap.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
fs/gfs2/bmap.c +12 -1
index 131091520de6..fdcac8e3f2ba 100644
@@ -1127,10 +1127,18 @@ static int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
 			goto out_unlock;
 		break;
 	default:
-		goto out_unlock;
+		goto out;
 	}
 
 	ret = gfs2_iomap_begin_write(inode, pos, length, flags, iomap, &mp);
+	if (ret)
+		goto out_unlock;
+
+out:
+	if (iomap->type == IOMAP_INLINE) {
+		iomap->private = metapath_dibh(&mp);
+		get_bh(iomap->private);
+	}
 
 out_unlock:
 	release_metapath(&mp);
@@ -1144,6 +1152,9 @@ static int gfs2_iomap_end(struct inode *inode, loff_t pos, loff_t length,
 	struct gfs2_inode *ip = GFS2_I(inode);
 	struct gfs2_sbd *sdp = GFS2_SB(inode);
 
+	if (iomap->private)
+		brelse(iomap->private);
+
 	switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) {
 	case IOMAP_WRITE:
 		if (flags & IOMAP_DIRECT)
Signature · 1 line
2.52.0