Skip to contentSkip to conversation

gfs2: Fix use-after-free in iomap inline data write path

PATCH Superseded Deepanshu Kartikey 2 participants 3 messages Jan 30 GFS2 file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gfs2: Fix use-after-free in iomap inline data write path

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.

---
 fs/gfs2/bmap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/gfs2/bmap.c +8 -0
index 131091520de6..e70095809e4a 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,12 @@ 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);
 
+	/* Release buffer head for inline data */
+	if (iomap->type == IOMAP_INLINE && iomap->private) {
+		brelse(iomap->private);
+		iomap->private = NULL;
+	}
+
 	switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) {
 	case IOMAP_WRITE:
 		if (flags & IOMAP_DIRECT)
Signature · 1 line
2.43.0
#1 lore raw

Hello Deepanshu,

Quoted 16 lines · Deepanshu Kartikey

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

Quoted 4 lines

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.

I agree with this analysis and fix. Some nitpicking below.

Quoted 25 lines

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.

---
 fs/gfs2/bmap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/gfs2/bmap.c +2 -0
index 131091520de6..e70095809e4a 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;
        }
 

fs/gfs2/bmap.c static int gfs2_iomap_end(struct inode *inode, loff_t pos, loff_t length, · line 1153

Quoted 8 lines
fs/gfs2/bmap.c +5 -0
@@ -1144,6 +1146,12 @@ 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);
 
+       /* Release buffer head for inline data */
+       if (iomap->type == IOMAP_INLINE && iomap->private) {
+               brelse(iomap->private);
+               iomap->private = NULL;
+       }

We're not using iomap->private for anything else and that field is zeroed out automatically, so this can be simplified to:

if (iomap->private)
brelse(iomap->private);

Quoted 7 lines
fs/gfs2/bmap.c +1 -1
+
        switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) {
        case IOMAP_WRITE:
                if (flags & IOMAP_DIRECT)
--

2.43.0

Thanks,
Andreas

#2 lore raw
Quoted 64 lines · Andreas Gruenbacher

Hello Deepanshu,

Quoted 16 lines · Deepanshu Kartikey

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

Quoted 4 lines

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.

I agree with this analysis and fix. Some nitpicking below.

Quoted 25 lines

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.

---
 fs/gfs2/bmap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/gfs2/bmap.c +2 -0
index 131091520de6..e70095809e4a 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;
        }
 

fs/gfs2/bmap.c static int gfs2_iomap_end(struct inode *inode, loff_t pos, loff_t length, · line 1153

Quoted 8 lines
fs/gfs2/bmap.c +5 -0
@@ -1144,6 +1146,12 @@ 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);
 
+       /* Release buffer head for inline data */
+       if (iomap->type == IOMAP_INLINE && iomap->private) {
+               brelse(iomap->private);
+               iomap->private = NULL;
+       }

We're not using iomap->private for anything else and that field is zeroed out automatically, so this can be simplified to:

Quoted 2 lines

if (iomap->private)
brelse(iomap->private);

And then we've also got iomap->private leaking a dibh reference in gfs2_iomap_get() and gfs2_iomap_alloc().

Quoted 10 lines
Quoted 7 lines
fs/gfs2/bmap.c +1 -1
+
        switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) {
        case IOMAP_WRITE:
                if (flags & IOMAP_DIRECT)
--

2.43.0

Thanks,
Andreas