[PATCH v4] gfs2: validate log descriptor tag counts during replay

From: Kyuwon Cho

Date: Sat Dec 05 2026 - 03:22:32 EST


Journal replay trusts ld_data1 as the number of metadata or journaled-data
tags stored after a log descriptor. A corrupt journal can advertise more
tags than fit in the descriptor block, making the replay scanners read
beyond the buffer.

Validate the format-specific tag capacity and the exact descriptor length
before the replay-pass check. Besides protecting the tag scan in pass 1,
this prevents pass 0 from advancing with an invalid or zero ld_length.

Apply equivalent consistency checks to revoke descriptors. A zero revoke
count underflows while scanning, and a count that does not match ld_length
makes replay consume the wrong number of log blocks. Compute the number of
revoke blocks without overflowing the rounded-up numerator so an on-disk
count near U32_MAX cannot appear to fit in one block.

Fixes: b3b94faa5fe5 ("[GFS2] The core of GFS2")
Fixes: 18ec7d5c3f43 ("[GFS2] Make journaled data files identical to normal files on disk")
Suggested-by: Andreas Gruenbacher <agruenba@xxxxxxxxxx>
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
Changes in v4:
- Use an overflow-free block-count formula and validate the revoke case near
U32_MAX. This addresses the Sashiko AI review:
https://lore.kernel.org/all/sashiko-outbox-249266@xxxxxxxxxx/
- Keep the revoke descriptor checks added in v3 at Andreas Gruenbacher's
suggestion.

v3: https://lore.kernel.org/all/179636379461.1106205.14894674361180500595@xxxxxxxx/
v2: https://lore.kernel.org/all/179632380570.3852104.9882872917590234700@xxxxxxxx/
v1: https://lore.kernel.org/all/179620509057.2012504.1338760759852960966@xxxxxxxx/

The issue was found by an LLM-guided test generator that we are
developing as a research prototype.

A reproducer is available on request.

fs/gfs2/log.c | 2 +-
fs/gfs2/lops.c | 21 ++++++++++++++++++---
2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/fs/gfs2/log.c b/fs/gfs2/log.c
index d6876d2091c9..8dbebb432ac2 100644
--- a/fs/gfs2/log.c
+++ b/fs/gfs2/log.c
@@ -58,7 +58,7 @@ unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct)
if (nstruct > first) {
/* Subsequent struct gfs2_meta_header blocks */
second = sdp->sd_inptrs;
- blks += DIV_ROUND_UP(nstruct - first, second);
+ blks += 1 + (nstruct - first - 1) / second;
}

return blks;
diff --git a/fs/gfs2/lops.c b/fs/gfs2/lops.c
index 657edd406dd1..e08d221f038c 100644
--- a/fs/gfs2/lops.c
+++ b/fs/gfs2/lops.c
@@ -790,7 +790,12 @@ static int buf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start,
u64 blkno;
int error = 0;

- if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
+ if (be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
+ return 0;
+ if (blks > buf_limit(sdp) ||
+ be32_to_cpu(ld->ld_length) != blks + 1)
+ return -EIO;
+ if (pass != 1)
return 0;

gfs2_replay_incr_blk(jd, &start);
@@ -925,7 +930,11 @@ static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, u32 start,
int first = 1;
int error;

- if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
+ if (be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
+ return 0;
+ if (!revokes || blks != gfs2_struct2blk(sdp, revokes))
+ return -EIO;
+ if (pass != 0)
return 0;

offset = sizeof(struct gfs2_log_descriptor);
@@ -1000,13 +1009,19 @@ static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start,
__be64 *ptr, int pass)
{
struct gfs2_glock *gl = gfs2_inode_glock(jd->jd_inode);
+ struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
unsigned int blks = be32_to_cpu(ld->ld_data1);
struct buffer_head *bh_log, *bh_ip;
u64 blkno;
u64 esc;
int error = 0;

- if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
+ if (be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
+ return 0;
+ if (blks > databuf_limit(sdp) ||
+ be32_to_cpu(ld->ld_length) != blks + 1)
+ return -EIO;
+ if (pass != 1)
return 0;

gfs2_replay_incr_blk(jd, &start);