NFSD: Randomize clientids, session IDs, and COPY_NOTIFY tokens
Subsystems (2)
Chuck Lever
maintainercover NFSD: Randomize clientids, session IDs, and COPY_NOTIFY tokens
NFSD currently assigns a clientid from a per-netns counter, a session ID's sequence word from a global counter, and a COPY_NOTIFY stateid's so_id from a cyclic allocator with a fixed clientid word. Each of the three is a bearer token. NFSv4.0 resolves a presented stateid against the client named inside it rather than the caller, and the errors the protocol mandates (EXPIRED for an unknown clientid, BAD_STATEID for a known clientid with an unknown index) let the two halves be searched separately. Under SP4_NONE, nfsd4_sequence() does not check a credential, so a session ID alone can reach another client's slot table. Adjacent counter values let a malicious requester find a neighbor's token in a few dozen probes by counting outward from its own.
After this series, the clientid, the session ID, and the COPY_NOTIFY token's clientid word are random. Predicting a live clientid costs about 2^32 / C probes for C clients of the probe's minor version, and a session ID 2^64. An ordinary stateid's so_id stays cyclic, so it stays cheap to search once a clientid is known. RFC 8881 forbids reusing an "other" value for a different purpose, and the cyclic allocator is the non-reuse mechanism clients rely on.
Tested with the new KUnit suite, pynfs 4.0 and 4.1 over loopback, and pynfs-based probes that confirm the three tokens are unpredictable, that a LOCKT denial no longer carries a clientid, and that a COPY_NOTIFY stateid with a wrong clientid word is rejected. A same-second restart loop confirmed STALE_CLIENTID on RENEW and a clean shutdown with live COPY_NOTIFY state.
---
Chuck Lever (10):
NFSD: Serialize forced client expiry with state shutdown
NFSD: Check machine credentials before expiring a client in DESTROY_CLIENTID
NFSD: Make nn->boot_time strictly increase across state starts
NFSD: Give each client a display id independent of cl_id
NFSD: Generate the clientid at random instead of from a counter
NFSD: Stop disclosing the conflicting lock holder in a lock denial
NFSD: Destroy the COPY_NOTIFY stateid IDR at netns teardown
NFSD: Put a random word in each COPY_NOTIFY stateid
NFSD: Fill the session ID's sequence and reserved words at random
NFSD: Add KUnit tests for the random identifier generators
fs/nfsd/Kconfig | 16 ++ fs/nfsd/netns.h | 9 +- fs/nfsd/nfs4layouts.c | 10 +- fs/nfsd/nfs4proc.c | 2 - fs/nfsd/nfs4state.c | 308 ++++++++++++++++++------ fs/nfsd/nfs4state_test.c | 591 +++++++++++++++++++++++++++++++++++++++++++++++ fs/nfsd/state.h | 16 +- fs/nfsd/xdr4.h | 2 - 8 files changed, 867 insertions(+), 87 deletions(-)
---
base-commit: 19bbac00ad1cfa28e9127488f161ffc2ac03fad4
change-id: 20261029-random-stateids-c66f263d7800
Best regards,
Signature · 1 line
Chuck Lever <[email protected]>
Chuck Lever
maintainerPATCH 1/10 NFSD: Serialize forced client expiry with state shutdown
client_ctl_write() pins the client only through the nfsdfs kref and calls force_expire_client() without nn->nfsd_mutex. nfs4_state_shutdown_net() walks the client hash tables with only that mutex held. A write of "expire" that races with the shutdown unhashes its client under client_lock, so the walk never sees that client. The write's expire_client() then runs __destroy_client() after the shutdown has freed the hash tables, disabled the laundromat, freed nn->client_tracking_ops, and destroyed the nfsd_file and nfs4_file caches that the destroy puts references into.
Take nn->nfsd_mutex in force_expire_client() around the unhash and the destroy, once cl_rpc_users has drained, and proceed only while NFSD_NET_UP is set, as nfsd4_revoke_states() already requires of its callers. Under the mutex the shutdown either has not started, so the forced expiry completes before the walk, or has finished, so the write fails with ENXIO. The two waits stay outside the mutex, which the netlink export handlers also take.
--- fs/nfsd/nfs4state.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-)
fs/nfsd/nfs4state.c +20 -6
@@ -3595,7 +3595,7 @@ static const struct file_operations client_states_fops = {* so the caller has a guarantee that the client's locks are gone by * the time the write returns: */ -static void force_expire_client (struct nfs4_client *clp) +static bool force_expire_client (struct nfs4_client *clp) {struct nfsd_net *nn =net_generic (clp->net, nfsd_net_id);bool already_expired;
@@ -3608,27 +3608,40 @@ static void force_expire_client(struct nfs4_client *clp)* this client or is seen by this wait_event(). */ spin_lock (&nn->client_lock); +if (list_empty (&clp->cl_lru) && clp->cl_time) { +spin_unlock (&nn->client_lock); +return false; + } clp->cl_time =0 ;spin_unlock (&nn->client_lock);wait_event (expiry_wq,atomic_read (&clp->cl_rpc_users) ==0 ); + +mutex_lock (&nn->nfsd_mutex); +if (!test_bit (NFSD_NET_UP , &nn->flags)) { +mutex_unlock (&nn->nfsd_mutex); +return false; + }spin_lock (&nn->client_lock); already_expired =list_empty (&clp->cl_lru);if (!already_expired)unhash_client_locked (clp);spin_unlock (&nn->client_lock); -if (!already_expired)expire_client (clp); -else +mutex_unlock (&nn->nfsd_mutex); + +if (already_expired)wait_event (expiry_wq, clp->cl_nfsd_dentry ==NULL ); +return true; }static ssize_t client_ctl_write (struct file *file,const char __user *buf,size_t size,loff_t *pos) { -char *data;struct nfs4_client *clp; +ssize_t ret = -ENXIO ; +char *data; data =simple_transaction_get (file, buf, size);if (IS_ERR (data))
@@ -3638,9 +3651,10 @@ static ssize_t client_ctl_write(struct file *file, const char __user *buf, clp =get_nfsdfs_clp (file_inode (file));if (!clp)return -ENXIO ; -force_expire_client (clp); +if (force_expire_client (clp)) + ret =7 ;nfsd4_put_client (clp); -return 7 ; +return ret; }static const struct file_operations client_ctl_fops = {
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 2/10 NFSD: Check machine credentials before expiring a client in DESTROY_CLIENTID
In reply to Chuck Levernfsd4_destroy_clientid() calls mark_client_expired_locked() on a confirmed client before it checks the machine credential. mark_client_expired_locked() unhashes the client, and when the credential check then fails the error path clears clp, so expire_client() never runs. The nfs4_client is gone from every lookup table but never freed. A requester that knows only the clientid of an NFSv4.1 client that has destroyed its sessions can orphan a client that registered SP4_MACH_CRED to prevent exactly that.
Check the credential before mark_client_expired_locked(). A request carrying the wrong credential now fails with NFS4ERR_WRONG_CRED even when the client is busy, where it previously failed with NFS4ERR_DELAY first.
--- fs/nfsd/nfs4state.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +8 -3
@@ -5314,9 +5314,6 @@ nfsd4_destroy_clientid(struct svc_rqst *rqstp, status = nfserr_clientid_busy; goto out; } - status =mark_client_expired_locked (conf); -if (status) - goto out; clp = conf; }else if (unconf) clp = unconf;
@@ -5329,6 +5326,14 @@ nfsd4_destroy_clientid(struct svc_rqst *rqstp, status = nfserr_wrong_cred; goto out; } +if (conf) { +/* Unhashes conf; expire_client() must follow */ + status =mark_client_expired_locked (conf); +if (status) { + clp =NULL ; + goto out; + } + }trace_nfsd_clid_destroyed (&clp->cl_clientid);unhash_client_locked (clp); out:
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 3/10 NFSD: Make nn->boot_time strictly increase across state starts
In reply to Chuck LeverSTALE_CLIENTID() tells a clientid issued by a previous NFSD instance from a current one by its cl_boot word, (u32)nn->boot_time sampled at each state start. nfs4_state_create_net() sets nn->boot_time to ktime_get_real_seconds(), so two starts within one wall-clock second record the same cl_boot. NFSD then rejects a pre-restart clientid on an NFSv4.0 RENEW as EXPIRED instead of STALE_CLIENTID. The NFS client treats the restart as a lease expiry and does not reclaim.
nn->boot_time persists across starts within a netns. When a fresh reading is not greater than the stored value, use the stored value plus one.
--- fs/nfsd/nfs4state.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/nfsd/nfs4state.c +4 -0
@@ -10212,6 +10212,7 @@ set_max_delegations(void)static int nfs4_state_create_net (struct net *net) {struct nfsd_net *nn =net_generic (net, nfsd_net_id); +time64_t previous_boot;int i; nn->conf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE);
@@ -10233,7 +10234,10 @@ static int nfs4_state_create_net(struct net *net)INIT_LIST_HEAD (&nn->sessionid_hashtbl[i]); nn->conf_name_tree =RB_ROOT ; nn->unconf_name_tree =RB_ROOT ; + previous_boot = nn->boot_time; nn->boot_time =ktime_get_real_seconds (); +if (nn->boot_time <= previous_boot) + nn->boot_time = previous_boot +1 ; nn->boot_time_bt =ktime_get_boottime_seconds ();clear_bit (NFSD_NET_GRACE_ENDED , &nn->flags);clear_bit (NFSD_NET_GRACE_END_FORCED , &nn->flags);
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 4/10 NFSD: Give each client a display id independent of cl_id
In reply to Chuck LeverThe nfsdfs client directory name under /proc/fs/nfsd/clients/ and the two pNFS block layout fencing warnings print a short client number computed as cl_id - nn->clientid_base. For cl_id to become unpredictable, the displayed number must stop deriving from it. A random cl_id makes the subtraction a random number, and two clients can then collide on a directory name: nfsd_client_mkdir() fails with EEXIST and the client sees NFS4ERR_JUKEBOX.
Add cl_display_id, assigned in create_client() from a per-netns atomic_t counter, and print it at the three sites. Seed the counter in nfsd4_init_leases_net(), which runs once per netns rather than per nfsd start, so the numbering keeps counting across restarts as before. The printed values do not change.
atomic_inc_return() also closes a pre-existing race: the unlocked clientid_counter++ let two concurrent SETCLIENTIDs produce the same directory name.
--- fs/nfsd/netns.h | 1 + fs/nfsd/nfs4layouts.c | 10 ++++------ fs/nfsd/nfs4state.c | 5 ++++- fs/nfsd/state.h | 1 + 4 files changed, 10 insertions(+), 7 deletions(-)
fs/nfsd/netns.h +1 -0
@@ -162,6 +162,7 @@ struct nfsd_net {u32 clientid_base;u32 clientid_counter;u32 clverifier_counter; +atomic_t client_display_counter;/* * Serializes this namespace's control plane: ->nfsd_serv and the
fs/nfsd/nfs4layouts.c +4 -6
@@ -1125,7 +1125,6 @@ static void nfsd4_layout_fence_worker(struct work_struct *work)struct nfsd_file *nf;struct block_device *bdev;struct nfs4_client *clp; -struct nfsd_net *nn;spin_lock (&ls->ls_lock);if (ls->ls_fence_stopped ||list_empty (&ls->ls_layouts)) {
@@ -1143,7 +1142,6 @@ static void nfsd4_layout_fence_worker(struct work_struct *work) goto dispose; clp = ls->ls_stid.sc_client; - nn =net_generic (clp->net, nfsd_net_id); bdev = nf->nf_file->f_path.mnt->mnt_sb->s_bdev; ops = nfsd4_layout_ops[ls->ls_layout_type];if (!ops->fence_client) {
@@ -1173,9 +1171,9 @@ static void nfsd4_layout_fence_worker(struct work_struct *work)if (ops->fence_client (ls, nf)) {/* fenced ok */ nfsd_file_put (nf); -pr_warn ("%s: FENCED client[%pISpc] clid[%d] to device[%s]\n" , +pr_warn ("%s: FENCED client[%pISpc] clid[%u] to device[%s]\n" , __func__, (struct sockaddr *)&clp->cl_addr, - clp->cl_clientid.cl_id - nn->clientid_base, + clp->cl_display_id, bdev->bd_disk->disk_name); goto dispose; }
@@ -1183,9 +1181,9 @@ static void nfsd4_layout_fence_worker(struct work_struct *work)nfsd_file_put (nf);if (!clp->cl_fence_retry_warn) { -pr_warn ("%s: FENCE failed client[%pISpc] clid[%d] device[%s]\n" , +pr_warn ("%s: FENCE failed client[%pISpc] clid[%u] device[%s]\n" , __func__, (struct sockaddr *)&clp->cl_addr, - clp->cl_clientid.cl_id - nn->clientid_base, + clp->cl_display_id, bdev->bd_disk->disk_name); clp->cl_fence_retry_warn = true; }
fs/nfsd/nfs4state.c +4 -1
@@ -3983,9 +3983,10 @@ static struct nfs4_client *create_client(struct xdr_netobj name,memcpy (&clp->cl_addr, sa,sizeof (struct sockaddr_storage ));RCU_INIT_POINTER (clp->cl_cb_session,NULL ); clp->net = net; + clp->cl_display_id =atomic_inc_return (&nn->client_display_counter); clp->cl_nfsd_dentry =nfsd_client_mkdir ( nn, &clp->cl_nfsdfs, - clp->cl_clientid.cl_id - nn->clientid_base, + clp->cl_display_id, client_files, dentries); clp->cl_nfsd_info_dentry = dentries[0 ];if (!clp->cl_nfsd_dentry) {
@@ -5738,6 +5739,8 @@ nfsd4_init_leases_net(struct nfsd_net *nn) nn->clientid_base =get_random_u32 (); nn->clientid_counter = nn->clientid_base +1 ; nn->s2s_cp_cl_id = nn->clientid_counter++; +/* So the first client directory in a fresh netns is named 2. */ +atomic_set (&nn->client_display_counter,1 );atomic_set (&nn->nfs4_client_count,0 );si_meminfo (&si);
fs/nfsd/state.h +1 -0
@@ -570,6 +570,7 @@ struct nfs4_client {bool cl_mach_cred;/* SP4_MACH_CRED in force */ struct svc_cred cl_cred;/* setclientid principal */ clientid_t cl_clientid;/* generated by server */ +u32 cl_display_id;/* human-readable client number */ nfs4_verifier cl_confirm;/* generated by server */ u32 cl_minorversion;atomic_t cl_admin_revoked;/* count of admin-revoked states */
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 5/10 NFSD: Generate the clientid at random instead of from a counter
In reply to Chuck Levergen_clid() assigns cl_id from nn->clientid_counter++, so each client's clientid sits one step from the clientid issued just before it. The "other" field of every stateid embeds the clientid, and NFSv4.0 resolves a presented stateid against the client named inside it rather than the caller, so a guessed clientid is the first half of locating another client's state. A reporter enumerated a neighbor's clientid in tens of probes by counting outward from their own.
Fill cl_id from get_random_u32() instead. A collision retries NFSD4_RANDOM_RETRIES times before NFS4ERR_DELAY. Generating the id moves into each caller's nn->client_lock section, so the generation, the uniqueness scan, and the add_to_unconfirmed() insert run under one hold of the lock. The generation also runs before any mark_client_expired_locked() or unhash_client_locked(), so exhausting the retries returns NFS4ERR_DELAY with every prior record still hashed.
nn->s2s_cp_cl_id becomes the fixed value 0. The first key idr_alloc_cyclic() hands out is also 0, so without a further check an OFFLOAD_CANCEL carrying the all-zero stateid would revoke the caller's first COPY_NOTIFY state. Add a cl_boot check to manage_cpntf_state() that rejects any boot value but the current one with NFS4ERR_BAD_STATEID.
A random cl_id requires "NFSD: Make nn->boot_time strictly increase across state starts", so a same-second restart cannot reissue a prior instance's id, and "NFSD: Give each client a display id independent of cl_id", since cl_id is still 0 when the client directory is created.
A random cl_id collides with one of the K ids retired earlier this boot with probability about K / 2^32 per client, about 0.2% at K = 10 million. On such a collision, a stateid that an expired NFSv4.0 client retries can resolve to the new client's state, if the so_id, generation, and filehandle also match. NFSv4.1 is not exposed, since SEQUENCE binds the clientid to the session.
Guessing remains feasible. Locating a live clientid costs about 2^32 / C probes for C confirmed clients, and a LOCK or LOCKT denial still discloses a holder's clientid outright. "NFSD: Stop disclosing the conflicting lock holder in a lock denial" removes that path.
--- fs/nfsd/netns.h | 2 - fs/nfsd/nfs4state.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 100 insertions(+), 10 deletions(-)
fs/nfsd/netns.h +0 -2
@@ -159,8 +159,6 @@ struct nfsd_net {*/ unsigned int max_blksize; -u32 clientid_base; -u32 clientid_counter;u32 clverifier_counter;atomic_t client_display_counter;
fs/nfsd/nfs4state.c +100 -8
196 lines not on this page · raw message
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 6/10 NFSD: Stop disclosing the conflicting lock holder in a lock denial
In reply to Chuck Levernfs4_set_lock_denied() copies the conflicting lock holder's cl_clientid into ld_clientid, and nfsd4_encode_lock4denied() puts that clientid on the wire in every LOCK and LOCKT denial. One LOCKT against a file where another client holds a conflicting byte-range lock hands the requester that holder's full clientid. NFSv4.0 resolves a presented stateid against the client named inside the stateid rather than the caller, so a disclosed clientid lets an attacker skip the clientid search that a random cl_id is meant to force.
RFC 7530 Sections 16.10 and 16.11 return the conflicting lock's owner but do not require the holder's real clientid. RFC 8881 Section 18.10.3 does: a server returning NFS4ERR_DENIED MUST set the clientid of the denied owner. This change departs from that requirement on purpose. The nevermind branch already returns a zero clientid and an empty owner when a non-NFSv4 owner holds the conflict, so every client already copes with that reply, and the Linux NFS client discards the denied owner.
Route every denial through the nevermind behavior. ld_owner.data is then always NULL, so remove nfsd4_lock_release() and nfsd4_lockt_release() and their op_release entries.
--- fs/nfsd/nfs4proc.c | 2 -- fs/nfsd/nfs4state.c | 42 +++++++++--------------------------------- fs/nfsd/xdr4.h | 2 -- 3 files changed, 9 insertions(+), 37 deletions(-)
fs/nfsd/nfs4proc.c +0 -2
@@ -3923,7 +3923,6 @@ static const struct nfsd4_operation nfsd4_ops[] = { }, [OP_LOCK] = { .op_func = nfsd4_lock, - .op_release = nfsd4_lock_release, .op_flags =OP_MODIFIES_SOMETHING | OP_NONTRIVIAL_ERROR_ENCODE, .op_name = "OP_LOCK",
@@ -3932,7 +3931,6 @@ static const struct nfsd4_operation nfsd4_ops[] = { }, [OP_LOCKT] = { .op_func = nfsd4_lockt, - .op_release = nfsd4_lockt_release, .op_flags = OP_NONTRIVIAL_ERROR_ENCODE, .op_name = "OP_LOCKT", .op_rsize_bop = nfsd4_lock_rsize,
fs/nfsd/nfs4state.c +9 -33
60 lines not on this page · raw message
fs/nfsd/xdr4.h +0 -2
@@ -900,10 +900,8 @@ extern __be32 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct nfsd4_compound_state *, union nfsd4_op_u *u);extern __be32 nfsd4_lock (struct svc_rqst *rqstp,struct nfsd4_compound_state *,union nfsd4_op_u *u); -extern void nfsd4_lock_release (union nfsd4_op_u *u);extern __be32 nfsd4_lockt (struct svc_rqst *rqstp,struct nfsd4_compound_state *,union nfsd4_op_u *u); -extern void nfsd4_lockt_release (union nfsd4_op_u *u);extern __be32 nfsd4_locku (struct svc_rqst *rqstp,struct nfsd4_compound_state *,union nfsd4_op_u *u); extern __be32
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 7/10 NFSD: Destroy the COPY_NOTIFY stateid IDR at netns teardown
In reply to Chuck Levernfs4_state_destroy_net() frees a netns's client hash tables but never drains or destroys nn->s2s_cp_stateids. The IDR empties only as the walked clients' stids reach their final put, and nfs4_state_create_net() runs idr_init() on it again at the next start.
A parent stid can outlive the client walk:nfsd4_deleg_getattr_conflict() holds a delegation reference across its callback wait from any netns that exports the same inode. The re-init orphans that stid's COPY_NOTIFY entries, and a later idr_remove() by so_id on the fresh IDR can remove an unrelated entry. The IDR's radix nodes leak as well.
After the client walk, revoke every surviving entry and destroy the IDR under s2s_cp_lock. The late put then finds an empty sc_cp_list and never touches the IDR. A survivor draws no warning: a late delegation release is legitimate.
This patch depends on "NFSD: Serialize forced client expiry with state shutdown", which keeps a forced expire_client() from running after the walk.
--- fs/nfsd/nfs4state.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
fs/nfsd/nfs4state.c +15 -0
@@ -10369,6 +10369,7 @@ static void nfs4_state_destroy_net(struct net *net) {int i; +copy_stateid_t *cps_t;struct nfs4_client *clp =NULL ;struct nfsd_net *nn =net_generic (net, nfsd_net_id);
@@ -10388,6 +10389,20 @@ nfs4_state_destroy_net(struct net *net) } } +/* +* A parent stid can outlive the client walk above. Revoking its +* COPY_NOTIFY entries under s2s_cp_lock empties its sc_cp_list before +* the stid's final put, so the put never reaches an IDR destroyed here +* or reinitialized by the next start. +*/ +spin_lock (&nn->s2s_cp_lock); +idr_for_each_entry (&nn->s2s_cp_stateids, cps_t, i) +revoke_cpntf_state_locked (nn,container_of (cps_t, + struct nfs4_cpntf_state, + cp_stateid)); +idr_destroy (&nn->s2s_cp_stateids); +spin_unlock (&nn->s2s_cp_lock); +kfree (nn->sessionid_hashtbl);kfree (nn->unconf_id_hashtbl);kfree (nn->conf_id_hashtbl);
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 8/10 NFSD: Put a random word in each COPY_NOTIFY stateid
In reply to Chuck Levernfs4_init_cp_state() allocates the nn->s2s_cp_stateids key with idr_alloc_cyclic() from 0, so a COPY_NOTIFY stateid's so_id counts up across the netns and is guessable. Its cl_id word is a fixed sentinel and manage_cpntf_state() does not check the generation, so within one NFSD instance the so_id is the whole of the token.
Fill the cl_id word from get_random_u32() at creation and have manage_cpntf_state() compare the presented word with the stored one after the so_id lookup. The key stays cyclic: a random key would carry the same secret, but spread over a 2^31 key space each entry would occupy about 1.1 KB of radix nodes shared with no neighbor, against under 10 bytes for consecutive keys, with no cap on COPY_NOTIFY states per client.
The token's clientid must still never name a live client. nfsd4_read() and nfsd4_seek() try the IDR only after the per-client lookup fails with NFS4ERR_BAD_STATEID. A token word equal to the session client's cl_id, with the so_id landing on one of its stateids, would be handled as that stateid or rejected with NFS4ERR_OLD_STATEID. Keep the two spaces disjoint with one bit: nfs4_init_cp_state() sets bit 31 of the token word and nfsd4_assign_clid_locked() clears it in every clientid.
--- fs/nfsd/netns.h | 6 +----- fs/nfsd/nfs4state.c | 31 ++++++++++++++++--------------- 2 files changed, 17 insertions(+), 20 deletions(-)
fs/nfsd/netns.h +1 -5
@@ -179,11 +179,7 @@ struct nfsd_net {struct completion nfsd_net_confirm_done;struct completion nfsd_net_free_done; -/* -* clientid and stateid data for construction of net unique COPY -* stateids. -*/ -u32 s2s_cp_cl_id; +/* stateid data for construction of net unique COPY stateids */ struct idr s2s_cp_stateids;spinlock_t s2s_cp_lock;atomic_t pending_async_copies;
fs/nfsd/nfs4state.c +16 -15
@@ -70,6 +70,13 @@*/ #define NFSD4_RANDOM_RETRIES 16 +/* +* Set in every COPY_NOTIFY token's cl_id word and cleared in every +* clientid, so the per-client stateid lookup never resolves a token +* before the IDR lookup runs. +*/ +#define NFSD4_CPNTF_CLID_BIT BIT(31) +#define all_ones {{ ~0, ~0}, ~0}static const stateid_t one_stateid = { .si_generation = ~0 ,
@@ -1000,7 +1007,9 @@ static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,int new_id; stid->cs_stid.si_opaque.so_clid.cl_boot = (u32 )nn->boot_time; - stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; +/* so_id is cyclic, so this word carries the token's secret. */ + stid->cs_stid.si_opaque.so_clid.cl_id =get_random_u32 () | +NFSD4_CPNTF_CLID_BIT ;idr_preload (GFP_KERNEL );spin_lock (&nn->s2s_cp_lock);
@@ -3226,9 +3235,7 @@ static __be32 nfsd4_assign_clid_locked(struct nfs4_client *clp,return nfs_ok;for (i =0 ; i <NFSD4_RANDOM_RETRIES ; i++) { - id =get_random_u32 (); -if (id == nn->s2s_cp_cl_id) -continue ; + id =get_random_u32 () & ~NFSD4_CPNTF_CLID_BIT ;if (!cl_id_in_use_locked (nn, id)) { clp->cl_clientid.cl_id = id;return nfs_ok;
@@ -5822,8 +5829,6 @@ nfsd4_init_leases_net(struct nfsd_net *nn) nn->nfsd4_lease =90 ;/* default lease time */ nn->nfsd4_grace =90 ; nn->clverifier_counter =get_random_u32 (); -/* The COPY_NOTIFY sentinel clientid used by nfs4_init_cp_state() */ - nn->s2s_cp_cl_id =0 ;/* So the first client directory in a fresh netns is named 2. */ atomic_set (&nn->client_display_counter,1 );
@@ -8630,18 +8635,15 @@ __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,copy_stateid_t *cps_t;struct nfs4_cpntf_state *state =NULL ; -if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) -return nfserr_bad_stateid; -/* -* With a fixed sentinel clientid, the all-zero stateid and a token -* from a previous nfsd instance both match cl_id, so cl_boot has -* to carry the check. -*/ +/* A token from a previous NFSD instance can name a live key. */ if (st->si_opaque.so_clid.cl_boot != (u32 )nn->boot_time)return nfserr_bad_stateid;spin_lock (&nn->s2s_cp_lock);/* s2s_cp_stateids holds only COPY_NOTIFY stateids */ cps_t =idr_find (&nn->s2s_cp_stateids, st->si_opaque.so_id); +if (cps_t && cps_t->cs_stid.si_opaque.so_clid.cl_id != + st->si_opaque.so_clid.cl_id) + cps_t =NULL ;if (cps_t) { state = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
@@ -8651,8 +8653,7 @@ __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, sizeof(clientid_t))) {/* * OFFLOAD_CANCEL: only the creating client may cancel. -* so_id is guessable, so without this check any client -* could free another's cpntf state. +* The destination server holds the token too. */ state =NULL ; goto unlock;
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 9/10 NFSD: Fill the session ID's sequence and reserved words at random
In reply to Chuck Levergen_sessionid() fills a session ID's sequence word from a file-scope counter that counts every CREATE_SESSION across all netns, and zeroes the reserved word. Under SP4_NONE, which the Linux client requests by default, the session ID is a bearer token: nfsd4_sequence() checks no credential and nfsd4_destroy_session() succeeds for any requester holding the ID. A requester that knows a client's clientid reads the counter from its own CREATE_SESSION reply and reaches the victim's session within a few hundred SEQUENCE probes, after which it can poison the victim's reply cache, drive OPEN and LOCK against the victim's owners, or destroy the session. RFC 8881 Section 2.10.8.3 offers only SP4_MACH_CRED and SP4_SSV against this attack.
Fill the two words with one 64-bit random value. RFC 8881 places no uniqueness, reuse, or ordering rule on session IDs, so the only uniqueness NFSD needs is that no two live sessions share one:nfsd4_assign_sessionid_locked() rejects a value that matches a hashed session and returns NFS4ERR_DELAY after NFSD4_RANDOM_RETRIES misses.
Generating the ID cannot stay in init_session(), which runs after nfsd4_create_session() has expired the replaced client and confirmed the new one. nfsd4_create_session() generates the ID at the end of Phase 2, after check_slot_seqid() has ruled out a replay, and on exhaustion takes the out_expired_error path so NFS4ERR_DELAY stays out of the reply cache.
nfsd4_sequence() returns NFS4ERR_BADSESSION for a wrong clientid and a wrong sequence alike, so a requester that knows the clientid still faces a 2^64 search.
--- fs/nfsd/nfs4state.c | 59 +++++++++++++++++++++++++++++++++++++++++------------ fs/nfsd/state.h | 15 +++++++++++++- 2 files changed, 60 insertions(+), 14 deletions(-)
fs/nfsd/nfs4state.c +46 -13
@@ -92,8 +92,6 @@ static const stateid_t close_stateid = { .si_generation = 0xffffffffU, }; -static u64 current_sessionid =1 ; -bool nfsd_delegts_enabled__read_mostly = true;#define ZERO_STATEID (stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
@@ -2300,15 +2298,14 @@ void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) }static void -gen_sessionid (struct nfsd4_session *ses) +gen_sessionid (struct nfsd4_session *ses,u64 rand) {struct nfs4_client *clp = ses->se_client;struct nfsd4_sessionid *sid; sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; sid->clientid = clp->cl_clientid; - sid->sequence = current_sessionid++; - sid->reserved =0 ; +nfsd4_sessionid_set_rand (sid, rand); }/*
@@ -2633,13 +2630,15 @@ nfsd_slot_shrinker_scan(struct shrinker *s, struct shrink_control *sc)return freed; } -static void init_session (struct svc_rqst *rqstp,struct nfsd4_session *new,struct nfs4_client *clp,struct nfsd4_create_session *cses) +static void init_session (struct svc_rqst *rqstp,struct nfsd4_session *new, +struct nfs4_client *clp, +struct nfsd4_create_session *cses,u64 sessionid_rand) {int idx;struct nfsd_net *nn =net_generic (SVC_NET (rqstp), nfsd_net_id); new->se_client = clp; -gen_sessionid (new); +gen_sessionid (new, sessionid_rand);INIT_LIST_HEAD (&new->se_conns);
@@ -2700,6 +2699,29 @@ __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)return NULL ; } +/* A candidate need only differ from the ID of every live session. */ +static __be32 nfsd4_assign_sessionid_locked (struct net *net, +const clientid_t *clid,u64 *rand) +{ +struct nfs4_sessionid candidate__aligned(4) ; +struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)candidate.data; +int i; + + sid->clientid = *clid; +for (i =0 ; i <NFSD4_RANDOM_RETRIES ; i++) { +u64 value =get_random_u64 (); + +nfsd4_sessionid_set_rand (sid, value); +if (!__find_in_sessionid_hashtbl (&candidate, net)) { + *rand = value; +return nfs_ok; + } + } +pr_warn_ratelimited ("NFSD: no unused session ID after %d tries\n" , +NFSD4_RANDOM_RETRIES ); +return nfserr_jukebox; +} +static struct nfsd4_session *find_in_sessionid_hashtbl (struct nfs4_sessionid *sessionid,struct net *net,__be32 *ret)
@@ -4752,6 +4774,7 @@ nfsd4_create_session(struct svc_rqst *rqstp,struct nfs4_client *old =NULL ;struct nfsd4_session *new;struct nfsd4_conn *conn; +u64 sessionid_rand;__be32 status =0 ;struct nfsd_net *nn =net_generic (SVC_NET (rqstp), nfsd_net_id);
@@ -4808,6 +4831,16 @@ nfsd4_create_session(struct svc_rqst *rqstp, goto out_cache_error; } +/* +* Assign the session ID before Phase 3 so a failure expires and +* confirms nothing. +*/ + status =nfsd4_assign_sessionid_locked (SVC_NET (rqstp), + &cr_ses->clientid, + &sessionid_rand); +if (status) + goto out_expired_error; +/* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */ if (conf) { status = nfserr_wrong_cred;
@@ -4843,7 +4876,7 @@ nfsd4_create_session(struct svc_rqst *rqstp,/* Report the correct number of backchannel slots */ cr_ses->back_channel.maxreqs = new->se_cb_highest_slot +1 ; -init_session (rqstp, new, conf, cr_ses); +init_session (rqstp, new, conf, cr_ses, sessionid_rand);nfsd4_get_session_locked (new); memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
@@ -6360,15 +6393,15 @@ bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode) }/* -* gen_sessionid() composes a sessionid from the client's clientid and a -* sequence counter, so the sequence alone identifies the granting session. +* gen_sessionid() composes a sessionid from the client's clientid and +* a 64-bit random value, so the random half alone identifies the +* granting session. */ static void nfsd4_recall_grant_sessionid (const struct nfs4_delegation *dp,struct nfsd4_sessionid *sid) { sid->clientid = dp->dl_stid.sc_client->cl_clientid; - sid->sequence = dp->dl_recall_grant.sessionid_seq; - sid->reserved =0 ; +nfsd4_sessionid_set_rand (sid, dp->dl_recall_grant.sessionid_rand); }static bool nfsd4_recall_grant_slot_retired (struct nfs4_delegation *dp)
@@ -7135,7 +7168,7 @@ nfs4_delegation_record_grant_slot(struct nfs4_delegation *dp,if (!cstate->session)return ; sid = (struct nfsd4_sessionid *)cstate->session->se_sessionid.data; - dp->dl_recall_grant.sessionid_seq = sid->sequence; + dp->dl_recall_grant.sessionid_rand =nfsd4_sessionid_rand (sid); dp->dl_recall_grant.slotid = cstate->slot->sl_index; dp->dl_recall_grant.seqid = cstate->slot->sl_seqid; dp->dl_recall_grant.valid = true;
fs/nfsd/state.h +14 -1
@@ -316,7 +316,7 @@ struct nfs4_delegation {/* Forward-channel slot that carried the granting request */ struct { -u32 sessionid_seq; +u64 sessionid_rand;u32 slotid;u32 seqid;bool valid;
@@ -500,6 +500,19 @@ struct nfsd4_sessionid {u32 reserved; }; +/* The sequence and reserved words together hold one 64-bit random value */ +static inline void nfsd4_sessionid_set_rand (struct nfsd4_sessionid *sid, +u64 rand) +{ + sid->sequence =lower_32_bits (rand); + sid->reserved =upper_32_bits (rand); +} + +static inline u64 nfsd4_sessionid_rand (const struct nfsd4_sessionid *sid) +{ +return ((u64 )sid->reserved <<32 ) | sid->sequence; +} +/* * State Meaning Where set * --------------------------------------------------------------------------
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH 10/10 NFSD: Add KUnit tests for the random identifier generators
In reply to Chuck LeverThe retry, mask, and collision branches of the clientid and session ID generators fire about once in 2^31 attempts per hashed client, and far less often for a session ID, so no workload reaches them and no wire test can force them. The same holds for the boot_time bump on a same-second restart and for the so_id allocator's non-reuse of a freed id. Nothing in the tree today can execute those paths on demand.
Add CONFIG_NFSD_KUNIT_TEST and fs/nfsd/nfs4state_test.c, included at the bottom of nfs4state.c so the tests reach the static helpers. Wrap the three random identifier generators in functions that KUNIT_STATIC_STUB_REDIRECT can script, and factor the boot_time bump out of nfs4_state_create_net() into nfsd4_next_boot_time(), callable on a hand-built nfsd_net. Because the redirect applies only in the KUnit task, the suite scripts the random source and calls the helpers directly. It covers the retries, masks, and collisions at each generation site, the boot_time bump, the COPY_NOTIFY token checks, and so_id non-reuse. The ordering guarantees in EXCHANGE_ID, SETCLIENTID, and CREATE_SESSION need a constructed svc_rqst and are not tested here.
--- fs/nfsd/Kconfig | 16 ++ fs/nfsd/nfs4state.c | 43 +++- fs/nfsd/nfs4state_test.c | 591 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 642 insertions(+), 8 deletions(-)
fs/nfsd/Kconfig +16 -0
@@ -178,3 +178,19 @@ config NFSD_V4_POSIX_ACLS clients that implement this draft is required. If unsure, say N. + +config NFSD_KUNIT_TEST + bool "KUnit tests for NFSD state allocation" if !KUNIT_ALL_TESTS + depends on NFSD_V4 && KUNIT + depends on NFSD=m || KUNIT=y + default KUNIT_ALL_TESTS + help + This builds the KUnit tests for the random clientid, session ID, + and COPY_NOTIFY stateid generators and related state allocation in + nfs4state.c. The tests run when the nfsd module is loaded and + report their results in TAP format. + + For more information on KUnit and unit tests in general, refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N.
fs/nfsd/nfs4state.c +35 -8
91 lines not on this page · raw message
fs/nfsd/nfs4state_test.c new file +591 -0
591 lines not on this page · raw message
Signature · 1 line
2.56.0
Quoted 50 lines · Chuck Lever
NFSD currently assigns a clientid from a per-netns counter, a session ID's sequence word from a global counter, and a
COPY_NOTIFYstateid'sso_idfrom a cyclic allocator with a fixed clientid word. Each of the three is a bearer token. NFSv4.0 resolves a presented stateid against the client named inside it rather than the caller, and the errors the protocol mandates (EXPIRED for an unknown clientid,BAD_STATEIDfor a known clientid with an unknown index) let the two halves be searched separately. UnderSP4_NONE,nfsd4_sequence()does not check a credential, so a session ID alone can reach another client's slot table. Adjacent counter values let a malicious requester find a neighbor's token in a few dozen probes by counting outward from its own.After this series, the clientid, the session ID, and the
COPY_NOTIFYtoken's clientid word are random. Predicting a live clientid costs about 2^32 / C probes for C clients of the probe's minor version, and a session ID 2^64. An ordinary stateid'sso_idstays cyclic, so it stays cheap to search once a clientid is known. RFC 8881 forbids reusing an "other" value for a different purpose, and the cyclic allocator is the non-reuse mechanism clients rely on.Tested with the new KUnit suite, pynfs 4.0 and 4.1 over loopback, and pynfs-based probes that confirm the three tokens are unpredictable, that a LOCKT denial no longer carries a clientid, and that a
COPY_NOTIFYstateid with a wrong clientid word is rejected. A same-second restart loop confirmedSTALE_CLIENTIDon RENEW and a clean shutdown with liveCOPY_NOTIFYstate.---
Chuck Lever (10):
NFSD: Serialize forced client expiry with state shutdown
NFSD: Check machine credentials before expiring a client inDESTROY_CLIENTID
NFSD: Makenn->boot_timestrictly increase across state starts
NFSD: Give each client a display id independent ofcl_id
NFSD: Generate the clientid at random instead of from a counter
NFSD: Stop disclosing the conflicting lock holder in a lock denial
NFSD: Destroy theCOPY_NOTIFYstateid IDR at netns teardown
NFSD: Put a random word in eachCOPY_NOTIFYstateid
NFSD: Fill the session ID's sequence and reserved words at random
NFSD: Add KUnit tests for the random identifier generatorsfs/nfsd/Kconfig | 16 ++ fs/nfsd/netns.h | 9 +- fs/nfsd/nfs4layouts.c | 10 +- fs/nfsd/nfs4proc.c | 2 - fs/nfsd/nfs4state.c | 308 ++++++++++++++++++------ fs/nfsd/nfs4state_test.c | 591 +++++++++++++++++++++++++++++++++++++++++++++++ fs/nfsd/state.h | 16 +- fs/nfsd/xdr4.h | 2 - 8 files changed, 867 insertions(+), 87 deletions(-)
Quoted 7 lines
---
base-commit:19bbac00ad1cfa28e9127488f161ffc2ac03fad4
change-id: 20261029-random-stateids-c66f263d7800Best regards,
Signature · 1 line
Chuck Lever <[email protected]>
This seems reasonable overall, but I do wonder if trying to keep these things non-guessable is a losing battle. I guess it doesn't hurt anything.
Quoted 55 lines · Jeff Layton
Quoted 53 lines · Chuck Lever
NFSD currently assigns a clientid from a per-netns counter, a session ID's sequence word from a global counter, and a
COPY_NOTIFYstateid'sso_idfrom a cyclic allocator with a fixed clientid word. Each of the three is a bearer token. NFSv4.0 resolves a presented stateid against the client named inside it rather than the caller, and the errors the protocol mandates (EXPIRED for an unknown clientid,BAD_STATEIDfor a known clientid with an unknown index) let the two halves be searched separately. UnderSP4_NONE,nfsd4_sequence()does not check a credential, so a session ID alone can reach another client's slot table. Adjacent counter values let a malicious requester find a neighbor's token in a few dozen probes by counting outward from its own.After this series, the clientid, the session ID, and the
COPY_NOTIFYtoken's clientid word are random. Predicting a live clientid costs about 2^32 / C probes for C clients of the probe's minor version, and a session ID 2^64. An ordinary stateid'sso_idstays cyclic, so it stays cheap to search once a clientid is known. RFC 8881 forbids reusing an "other" value for a different purpose, and the cyclic allocator is the non-reuse mechanism clients rely on.Tested with the new KUnit suite, pynfs 4.0 and 4.1 over loopback, and pynfs-based probes that confirm the three tokens are unpredictable, that a LOCKT denial no longer carries a clientid, and that a
COPY_NOTIFYstateid with a wrong clientid word is rejected. A same-second restart loop confirmedSTALE_CLIENTIDon RENEW and a clean shutdown with liveCOPY_NOTIFYstate.---
Chuck Lever (10):
NFSD: Serialize forced client expiry with state shutdown
NFSD: Check machine credentials before expiring a client inDESTROY_CLIENTID
NFSD: Makenn->boot_timestrictly increase across state starts
NFSD: Give each client a display id independent ofcl_id
NFSD: Generate the clientid at random instead of from a counter
NFSD: Stop disclosing the conflicting lock holder in a lock denial
NFSD: Destroy theCOPY_NOTIFYstateid IDR at netns teardown
NFSD: Put a random word in eachCOPY_NOTIFYstateid
NFSD: Fill the session ID's sequence and reserved words at random
NFSD: Add KUnit tests for the random identifier generatorsfs/nfsd/Kconfig | 16 ++ fs/nfsd/netns.h | 9 +- fs/nfsd/nfs4layouts.c | 10 +- fs/nfsd/nfs4proc.c | 2 - fs/nfsd/nfs4state.c | 308 ++++++++++++++++++------ fs/nfsd/nfs4state_test.c | 591 +++++++++++++++++++++++++++++++++++++++++++++++ fs/nfsd/state.h | 16 +- fs/nfsd/xdr4.h | 2 - 8 files changed, 867 insertions(+), 87 deletions(-)---
base-commit:19bbac00ad1cfa28e9127488f161ffc2ac03fad4
change-id: 20261029-random-stateids-c66f263d7800
[snip]
Quoted 4 lines
This seems reasonable overall, but I do wonder if trying to keep these things non-guessable is a losing battle. I guess it doesn't hurt anything.
IMO it would be nice to have a way to turn this off at NFS server startup...
----
Bye,
Roland
Signature · 5 lines
__ . . __ (o.\ \/ /.o) [email protected] \__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer /O /==\ O\ TEL +49 641 3992797 (;O/ \/ \O;)
Chuck Lever
maintainerQuoted 1 line · Roland Mainz
IMO it would be nice to have a way to turn this off at NFS server startup...
I'd like to understand the use case better. Can you say concretely what consumes the contents of these tokens, and what breaks when they are random?
The protocol mandates that nothing on the client side should care about the non-sequence ID parts of these cookies. RFC 8881 defines the client ID as a server-chosen shorthand (Section 2.4), gives the stateid's "other" field no meaning beyond identifying a set of locks (Section 8.2.2), and declares sessionid4 as an opaque array. The seqid field is the one part of a stateid that carries client-visible semantics, and this series leaves that field alone; it is still a monotonically increasing integer.
So if a client or a middlebox is interpreting these values, that is behavior the spec does not support, and I'm not inclined to add a knob to accommodate it. On the other hand, if the need is operational, say correlating packet captures with server-side events or diffing traces between runs, there may be a better way to provide that than predictable tokens.
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)