[PATCH v2] gfs2: Fix NULL pointer dereference in gfs2_seek_data/gfs2_seek_hole

From: William Chuang

Date: Mon Nov 16 2026 - 18:24:31 EST


gfs2_seek_data() and gfs2_seek_hole() call gfs2_glock_nq_init(), and
independent of the outcome, they call gfs2_glock_dq_uninit(). When the
gfs2_glock_nq() call in gfs2_glock_nq_init() fails, gfs2_glock_nq_init()
returns with an error code and gh.gh_gl is set NULL. This causes a NULL
pointer dereference in gfs2_glock_dq().

Fix that by only calling gfs2_glock_dq_uninit() when
gfs2_glock_nq_init() succeeds.

Fixes: 3a27411cb4bc ("gfs2: Implement SEEK_HOLE / SEEK_DATA via iomap")
Reported-by: syzbot+5a5f492ccae698fd7434@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=5a5f492ccae698fd7434
Suggested-by: Andreas Gruenbacher <agruenba@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: William Chuang <founder@xxxxxxxxxxxxxx>
---
v2: Adopt Andreas Gruenbacher's version of the fix -- skip
gfs2_glock_dq_uninit() via `goto out` when gfs2_glock_nq_init() fails,
rather than guarding the dequeue with gfs2_holder_initialized(); reword
the commit message to match. v1 posted at
https://lore.kernel.org/r/20261116180002.44336-1-founder@xxxxxxxxxxxxxx

fs/gfs2/inode.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/fs/gfs2/inode.c b/fs/gfs2/inode.c
index 2c2dc459c037..d4678e4832b3 100644
--- a/fs/gfs2/inode.c
+++ b/fs/gfs2/inode.c
@@ -2253,9 +2253,11 @@ loff_t gfs2_seek_data(struct file *file, loff_t offset)

inode_lock_shared(inode);
ret = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &gh);
- if (!ret)
- ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
+ if (ret)
+ goto out;
+ ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
gfs2_glock_dq_uninit(&gh);
+out:
inode_unlock_shared(inode);

if (ret < 0)
@@ -2272,9 +2274,11 @@ loff_t gfs2_seek_hole(struct file *file, loff_t offset)

inode_lock_shared(inode);
ret = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &gh);
- if (!ret)
- ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
+ if (ret)
+ goto out;
+ ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
gfs2_glock_dq_uninit(&gh);
+out:
inode_unlock_shared(inode);

if (ret < 0)
--
2.43.0