[PATCH] pNFS: Fix use-after-free of layout header in pnfs_update_layout()
From: bo cao
Date: Thu Dec 03 2026 - 02:55:18 EST
From: Bo Cao <treebacker@xxxxxxxxxxx>
pnfs_update_layout() drops its reference on the layout header with
pnfs_put_layout_hdr(lo) and then jumps back to the top of the
lookup_again loop. When that was the last reference the header is
freed through kfree_rcu(), but the local variable 'lo' still points
to it.
The check at the top of the loop for failed open state recovery
(added in 2fdbc20036ac) passes 'lo' to the pnfs_update_layout
tracepoint. Its TP_fast_assign reads lo->plh_flags and
lo->plh_stateid, so with the event enabled this reads freed memory.
The tracepoint is the only user of 'lo' at the top of the loop, so
without tracing nothing is dereferenced.
KASAN on v7.4-rc5 with the event enabled, LAYOUTGET failing with an
error that is retried while open state recovery has failed:
[ 282.313103] BUG: KASAN: slab-use-after-free in trace_event_raw_event_pnfs_update_layout+0x5bc/0x5d0
[ 282.313151] Read of size 8 at addr ffff000014d6fe58 by task pnfs_uaf/1905
[ 282.313153]
[ 282.313161] CPU: 0 UID: 65534 PID: 1905 Comm: pnfs_uaf Not tainted 7.4.0-rc5 #8 PREEMPT(full)
[ 282.313164] Hardware name: linux,dummy-virt (DT)
[ 282.313165] Call trace:
[ 282.313173] show_stack+0x18/0x24 (C)
[ 282.313191] dump_stack_lvl+0x74/0x8c
[ 282.313214] print_report+0x118/0x5e0
[ 282.313225] kasan_report+0xc0/0x12c
[ 282.313227] __asan_report_load8_noabort+0x20/0x2c
[ 282.313230] trace_event_raw_event_pnfs_update_layout+0x5bc/0x5d0
[ 282.313232] __traceiter_pnfs_update_layout+0xa0/0xf8
[ 282.313237] trace_pnfs_update_layout+0x100/0x19c
[ 282.313238] pnfs_update_layout+0x490/0x18c4
[ 282.313240] ff_layout_pg_get_read+0x190/0x324
[ 282.313242] ff_layout_pg_init_read+0x370/0x890
[ 282.313243] __nfs_pageio_add_request+0x8e8/0x14d8
Set lo to NULL after each put that is followed by a jump to
lookup_again, so the tracepoint takes its existing NULL branch. The
pointer is re-acquired by pnfs_find_alloc_layout() later in the loop.
The retry path is the one reported by KASAN; the other four sites
have the same pattern by inspection.
Fixes: 2fdbc20036ac ("NFS: avoid infinite loop in pnfs_update_layout.")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Bo Cao <treebacker@xxxxxxxxxxx>
---
fs/nfs/pnfs.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c
index 14f4a4b12ffd..80e5cac2906c 100644
--- a/fs/nfs/pnfs.c
+++ b/fs/nfs/pnfs.c
@@ -2235,6 +2235,7 @@ pnfs_update_layout(struct inode *ino,
if (IS_ERR(lseg))
goto out_put_layout_hdr;
pnfs_put_layout_hdr(lo);
+ lo = NULL;
goto lookup_again;
}
@@ -2252,6 +2253,7 @@ pnfs_update_layout(struct inode *ino,
lseg,
PNFS_UPDATE_LAYOUT_RETRY);
pnfs_put_layout_hdr(lo);
+ lo = NULL;
goto lookup_again;
}
trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
@@ -2288,6 +2290,7 @@ pnfs_update_layout(struct inode *ino,
goto out_put_layout_hdr;
pnfs_put_layout_hdr(lo);
dprintk("%s retrying\n", __func__);
+ lo = NULL;
goto lookup_again;
}
@@ -2305,6 +2308,7 @@ pnfs_update_layout(struct inode *ino,
clear_and_wake_up_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
&lo->plh_flags);
pnfs_put_layout_hdr(lo);
+ lo = NULL;
goto lookup_again;
}
spin_lock(&ino->i_lock);
@@ -2397,6 +2401,7 @@ pnfs_update_layout(struct inode *ino,
trace_pnfs_update_layout(ino, pos, count,
iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
pnfs_put_layout_hdr(lo);
+ lo = NULL;
goto lookup_again;
}
} else {
--
2.39.5 (Apple Git-154)