NFS: keep in-flight slots when shrinking the session slot table
Subsystems (3)
PATCH v2 1/1 NFS: keep in-flight slots when shrinking the session slot table
nfs4_reset_slot_table() shrinks the forechannel slot table to the new session's max_reqs without checking whether any of the slots being freed are still in use. The session drain it follows only snapshots highest_used_slotid once, while nfs4_setup_sequence() and nfs41_assign_slot() deliberately let privileged RPCs (sa_privileged, e.g. the DELEGRETURN sent while an inode is being evicted) allocate a slot after that snapshot. If such an RPC owns a slot with slot_nr >= max_reqs, nfs4_shrink_slot_table() kfree()s it and the reply path (decode_sequence(), nfs41_release_slot(), nfs4_free_slot()) later dereferences the freed slot: a slab-use-after-free.
Clamp the shrink target to highest_used_slotid + 1 when the table is not empty, so only slots above the highest used slot are freed, and stop resetting highest_used_slotid: the high-water mark is still needed to release those slots and to complete a later drain. max_slotid is still set to the server's new highest slotid, so new RPCs cannot use the preserved slots above the new max_reqs. A server advertising max_reqs == 0 arrives with server_highest_slotid == NFS4_NO_SLOT; make that wrap explicit.
---
v2: add Link, Fixes and stable tags, and remove local validation details from the commit message.
fs/nfs/nfs4session.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
fs/nfs/nfs4session.c +24 -2
@@ -274,8 +274,25 @@ u32 ivalue) {struct nfs4_slot **p; +u32 newsize = server_highest_slotid +1 ; +u32 highest_used = tbl->highest_used_slotid; -nfs4_shrink_slot_table (tbl, server_highest_slotid +1 ); +/* +* Privileged RPCs are allowed to allocate slots after the session +* drain has taken its snapshot, so slots above the server's new +* highest slotid may still be owned by in-flight RPCs. Shrink +* only up to the highest slot that is still in use: every slot +* above it is known to be free. +* +* A server advertising max_reqs == 0 arrives here with +* server_highest_slotid == NFS4_NO_SLOT, for which the addition +* above wraps to 0. +*/ +if (server_highest_slotid ==NFS4_NO_SLOT ) + newsize =0 ; +if (highest_used !=NFS4_NO_SLOT && newsize <= highest_used) + newsize = highest_used +1 ; +nfs4_shrink_slot_table (tbl, newsize); p = &tbl->slots;while (*p) { (*p)->seq_nr = ivalue;
@@ -283,7 +300,12 @@ (*p)->seq_nr_last_acked = ivalue -1 ; p = &(*p)->next; } - tbl->highest_used_slotid =NFS4_NO_SLOT ; +/* +* Leave highest_used_slotid untouched: when it is not +* NFS4_NO_SLOT, the high-water mark belongs to an in-flight +* privileged RPC whose slot survives the shrink, and +* nfs4_free_slot() lowers it once that RPC completes. +*/ tbl->target_highest_slotid = server_highest_slotid; tbl->server_highest_slotid = server_highest_slotid; tbl->d_target_highest_slotid =0 ;
Trond Myklebust
maintainerQuoted 12 lines · Cheng Yongkang
nfs4_reset_slot_table()shrinks the forechannel slot table to the new session'smax_reqswithout checking whether any of the slots being freed are still in use. The session drain it follows only snapshotshighest_used_slotidonce, whilenfs4_setup_sequence()andnfs41_assign_slot()deliberately let privileged RPCs (sa_privileged, e.g. the DELEGRETURN sent while an inode is being evicted) allocate a slot after that snapshot. If such an RPC owns a slot withslot_nr>=max_reqs,nfs4_shrink_slot_table()kfree()s it and the reply path (decode_sequence(),nfs41_release_slot(),nfs4_free_slot())
later dereferences the freed slot: a slab-use-after-free.
Quoted 11 lines
Clamp the shrink target to
highest_used_slotid+ 1 when the table is not empty, so only slots above the highest used slot are freed, and stop resettinghighest_used_slotid: the high-water mark is still needed
to release those slots and to complete a later drain.max_slotidis still set to the server's new highest slotid, so new RPCs cannot use the preserved slots above the new max_reqs. A server advertisingmax_reqs== 0 arrives withserver_highest_slotid==NFS4_NO_SLOT; make
that wrap explicit.
NACK. nfs4_reset_slot_table() is never called on a session table that is in use. It is called as part of the create_session workflow, and as part of the NFSv4.0 client initialisation.
Signature · 3 lines
Trond Myklebust Linux NFS client maintainer, Hammerspace [email protected], [email protected]
Quoted 3 lines · Trond Myklebust
NACK.
nfs4_reset_slot_table()is never called on a session table that is in use. It is called as part of thecreate_sessionworkflow, and as part of the NFSv4.0 client initialisation.
The case I hit is the create_session workflow run from nfs4_reset_session(). The drain there does not keep the table empty until nfs4_proc_create_session() runs. nfs4_drain_slot_tbl() returns once highest_used_slotid is NFS4_NO_SLOT, but nfs4_setup_sequence() and nfs41_assign_slot() still give slots to privileged RPCs while NFS4_SLOT_TBL_DRAINING is set. Since commit dfe1fe75e00e ("NFSv4: Fix deadlock between nfs4_evict_inode() and nfs4_opendata_get_inode()"), the DELEGRETURN sent from nfs4_evict_inode() is privileged once nfs_igrab_and_active() fails, so it can take a slot after the drain:
state manager inode eviction
nfs4_reset_session()
nfs4_begin_drain_session()
table empty, returns
nfs4_proc_destroy_session()
nfs4_evict_inode()
DELEGRETURN (privileged)
nfs4_setup_sequence()
takes slot 1
nfs4_proc_create_session()
server returns max_reqs = 1 nfs4_setup_session_slot_tables()
nfs4_realloc_slot_table()
nfs4_reset_slot_table()
nfs4_shrink_slot_table()
kfree(slot 1)
DELEGRETURN reply
decode_sequence()
reads the freed slot
I reproduced it on 7.3 with a test server that answers a GETATTR compound with NFS4ERR_BADSESSION, delays the DESTROY_SESSION and DELEGRETURN replies, and returns max_reqs = 1 instead of 4 in the new session:
BUG: KASAN: slab-use-after-free in decode_sequence+0xb3/0x690 Read of size 8 at addr ... by task kworker/u8:1/13 Workqueue: rpciod rpc_async_schedule decode_sequence+0xb3/0x690 nfs4_xdr_dec_delegreturn+0x54/0x380 call_decode+0x522/0x930 __rpc_execute+0x520/0x1270 rpc_async_schedule+0x52/0xa0 Freed by task ...: kfree nfs4_realloc_slot_table+0x597 nfs4_setup_session_slot_tables nfs4_proc_create_session nfs4_run_state_manager
The delays only widen the window. RFC 8881 lets a server destroy a session while fore channel requests are outstanding (section 18.37.3 only allows it to refuse for unanswered CB_COMPOUNDs), and lets it change ca_maxrequests for the fore channel in CREATE_SESSION (section 18.36).
v2 keeps the slots that are still in use when the table is shrunk. If you would rather keep the privileged DELEGRETURN off the slot table during a session reset, or wait for it before nfs4_proc_create_session(), I can rework it that way.
Thanks,
Cheng