Skip to content

Commit 967faa2

Browse files
Olga Kornievskaiagregkh
Olga Kornievskaia
authored andcommitted
nfsd: fix race between laundromat and free_stateid
commit 8dd91e8 upstream. There is a race between laundromat handling of revoked delegations and a client sending free_stateid operation. Laundromat thread finds that delegation has expired and needs to be revoked so it marks the delegation stid revoked and it puts it on a reaper list but then it unlock the state lock and the actual delegation revocation happens without the lock. Once the stid is marked revoked a racing free_stateid processing thread does the following (1) it calls list_del_init() which removes it from the reaper list and (2) frees the delegation stid structure. The laundromat thread ends up not calling the revoke_delegation() function for this particular delegation but that means it will no release the lock lease that exists on the file. Now, a new open for this file comes in and ends up finding that lease list isn't empty and calls nfsd_breaker_owns_lease() which ends up trying to derefence a freed delegation stateid. Leading to the followint use-after-free KASAN warning: kernel: ================================================================== kernel: BUG: KASAN: slab-use-after-free in nfsd_breaker_owns_lease+0x140/0x160 [nfsd] kernel: Read of size 8 at addr ffff0000e73cd0c8 by task nfsd/6205 kernel: kernel: CPU: 2 UID: 0 PID: 6205 Comm: nfsd Kdump: loaded Not tainted 6.11.0-rc7+ #9 kernel: Hardware name: Apple Inc. Apple Virtualization Generic Platform, BIOS 2069.0.0.0.0 08/03/2024 kernel: Call trace: kernel: dump_backtrace+0x98/0x120 kernel: show_stack+0x1c/0x30 kernel: dump_stack_lvl+0x80/0xe8 kernel: print_address_description.constprop.0+0x84/0x390 kernel: print_report+0xa4/0x268 kernel: kasan_report+0xb4/0xf8 kernel: __asan_report_load8_noabort+0x1c/0x28 kernel: nfsd_breaker_owns_lease+0x140/0x160 [nfsd] kernel: nfsd_file_do_acquire+0xb3c/0x11d0 [nfsd] kernel: nfsd_file_acquire_opened+0x84/0x110 [nfsd] kernel: nfs4_get_vfs_file+0x634/0x958 [nfsd] kernel: nfsd4_process_open2+0xa40/0x1a40 [nfsd] kernel: nfsd4_open+0xa08/0xe80 [nfsd] kernel: nfsd4_proc_compound+0xb8c/0x2130 [nfsd] kernel: nfsd_dispatch+0x22c/0x718 [nfsd] kernel: svc_process_common+0x8e8/0x1960 [sunrpc] kernel: svc_process+0x3d4/0x7e0 [sunrpc] kernel: svc_handle_xprt+0x828/0xe10 [sunrpc] kernel: svc_recv+0x2cc/0x6a8 [sunrpc] kernel: nfsd+0x270/0x400 [nfsd] kernel: kthread+0x288/0x310 kernel: ret_from_fork+0x10/0x20 This patch proposes a fixed that's based on adding 2 new additional stid's sc_status values that help coordinate between the laundromat and other operations (nfsd4_free_stateid() and nfsd4_delegreturn()). First to make sure, that once the stid is marked revoked, it is not removed by the nfsd4_free_stateid(), the laundromat take a reference on the stateid. Then, coordinating whether the stid has been put on the cl_revoked list or we are processing FREE_STATEID and need to make sure to remove it from the list, each check that state and act accordingly. If laundromat has added to the cl_revoke list before the arrival of FREE_STATEID, then nfsd4_free_stateid() knows to remove it from the list. If nfsd4_free_stateid() finds that operations arrived before laundromat has placed it on cl_revoke list, it marks the state freed and then laundromat will no longer add it to the list. Also, for nfsd4_delegreturn() when looking for the specified stid, we need to access stid that are marked removed or freeable, it means the laundromat has started processing it but hasn't finished and this delegreturn needs to return nfserr_deleg_revoked and not nfserr_bad_stateid. The latter will not trigger a FREE_STATEID and the lack of it will leave this stid on the cl_revoked list indefinitely. Fixes: 2d4a532 ("nfsd: ensure that clp->cl_revoked list is protected by clp->cl_lock") CC: [email protected] Signed-off-by: Olga Kornievskaia <[email protected]> Signed-off-by: Chuck Lever <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b2b7f4c commit 967faa2

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

fs/nfsd/nfs4state.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,21 +1358,47 @@ static void destroy_delegation(struct nfs4_delegation *dp)
13581358
destroy_unhashed_deleg(dp);
13591359
}
13601360

1361+
/**
1362+
* revoke_delegation - perform nfs4 delegation structure cleanup
1363+
* @dp: pointer to the delegation
1364+
*
1365+
* This function assumes that it's called either from the administrative
1366+
* interface (nfsd4_revoke_states()) that's revoking a specific delegation
1367+
* stateid or it's called from a laundromat thread (nfsd4_landromat()) that
1368+
* determined that this specific state has expired and needs to be revoked
1369+
* (both mark state with the appropriate stid sc_status mode). It is also
1370+
* assumed that a reference was taken on the @dp state.
1371+
*
1372+
* If this function finds that the @dp state is SC_STATUS_FREED it means
1373+
* that a FREE_STATEID operation for this stateid has been processed and
1374+
* we can proceed to removing it from recalled list. However, if @dp state
1375+
* isn't marked SC_STATUS_FREED, it means we need place it on the cl_revoked
1376+
* list and wait for the FREE_STATEID to arrive from the client. At the same
1377+
* time, we need to mark it as SC_STATUS_FREEABLE to indicate to the
1378+
* nfsd4_free_stateid() function that this stateid has already been added
1379+
* to the cl_revoked list and that nfsd4_free_stateid() is now responsible
1380+
* for removing it from the list. Inspection of where the delegation state
1381+
* in the revocation process is protected by the clp->cl_lock.
1382+
*/
13611383
static void revoke_delegation(struct nfs4_delegation *dp)
13621384
{
13631385
struct nfs4_client *clp = dp->dl_stid.sc_client;
13641386

13651387
WARN_ON(!list_empty(&dp->dl_recall_lru));
1388+
WARN_ON_ONCE(!(dp->dl_stid.sc_status &
1389+
(SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)));
13661390

13671391
trace_nfsd_stid_revoke(&dp->dl_stid);
13681392

1369-
if (dp->dl_stid.sc_status &
1370-
(SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)) {
1371-
spin_lock(&clp->cl_lock);
1372-
refcount_inc(&dp->dl_stid.sc_count);
1373-
list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1374-
spin_unlock(&clp->cl_lock);
1393+
spin_lock(&clp->cl_lock);
1394+
if (dp->dl_stid.sc_status & SC_STATUS_FREED) {
1395+
list_del_init(&dp->dl_recall_lru);
1396+
goto out;
13751397
}
1398+
list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1399+
dp->dl_stid.sc_status |= SC_STATUS_FREEABLE;
1400+
out:
1401+
spin_unlock(&clp->cl_lock);
13761402
destroy_unhashed_deleg(dp);
13771403
}
13781404

@@ -1781,6 +1807,7 @@ void nfsd4_revoke_states(struct net *net, struct super_block *sb)
17811807
mutex_unlock(&stp->st_mutex);
17821808
break;
17831809
case SC_TYPE_DELEG:
1810+
refcount_inc(&stid->sc_count);
17841811
dp = delegstateid(stid);
17851812
spin_lock(&state_lock);
17861813
if (!unhash_delegation_locked(
@@ -6544,6 +6571,7 @@ nfs4_laundromat(struct nfsd_net *nn)
65446571
dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
65456572
if (!state_expired(&lt, dp->dl_time))
65466573
break;
6574+
refcount_inc(&dp->dl_stid.sc_count);
65476575
unhash_delegation_locked(dp, SC_STATUS_REVOKED);
65486576
list_add(&dp->dl_recall_lru, &reaplist);
65496577
}
@@ -7161,7 +7189,9 @@ nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
71617189
s->sc_status |= SC_STATUS_CLOSED;
71627190
spin_unlock(&s->sc_lock);
71637191
dp = delegstateid(s);
7164-
list_del_init(&dp->dl_recall_lru);
7192+
if (s->sc_status & SC_STATUS_FREEABLE)
7193+
list_del_init(&dp->dl_recall_lru);
7194+
s->sc_status |= SC_STATUS_FREED;
71657195
spin_unlock(&cl->cl_lock);
71667196
nfs4_put_stid(s);
71677197
ret = nfs_ok;
@@ -7491,7 +7521,9 @@ nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
74917521
if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
74927522
return status;
74937523

7494-
status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, 0, &s, nn);
7524+
status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG,
7525+
SC_STATUS_REVOKED | SC_STATUS_FREEABLE,
7526+
&s, nn);
74957527
if (status)
74967528
goto out;
74977529
dp = delegstateid(s);

fs/nfsd/state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ struct nfs4_stid {
113113
/* For a deleg stateid kept around only to process free_stateid's: */
114114
#define SC_STATUS_REVOKED BIT(1)
115115
#define SC_STATUS_ADMIN_REVOKED BIT(2)
116+
#define SC_STATUS_FREEABLE BIT(3)
117+
#define SC_STATUS_FREED BIT(4)
116118
unsigned short sc_status;
117119

118120
struct list_head sc_cp_list;

0 commit comments

Comments
 (0)