1a28faaddSChuck Lever.. SPDX-License-Identifier: GPL-2.0 2a28faaddSChuck Lever 3a28faaddSChuck Lever======================= 4a28faaddSChuck LeverNFSv4 client identifier 5a28faaddSChuck Lever======================= 6a28faaddSChuck Lever 7a28faaddSChuck LeverThis document explains how the NFSv4 protocol identifies client 8a28faaddSChuck Leverinstances in order to maintain file open and lock state during 9a28faaddSChuck Leversystem restarts. A special identifier and principal are maintained 10a28faaddSChuck Leveron each client. These can be set by administrators, scripts 11a28faaddSChuck Leverprovided by site administrators, or tools provided by Linux 12a28faaddSChuck Leverdistributors. 13a28faaddSChuck Lever 14a28faaddSChuck LeverThere are risks if a client's NFSv4 identifier and its principal 15a28faaddSChuck Leverare not chosen carefully. 16a28faaddSChuck Lever 17a28faaddSChuck Lever 18a28faaddSChuck LeverIntroduction 19a28faaddSChuck Lever------------ 20a28faaddSChuck Lever 21a28faaddSChuck LeverThe NFSv4 protocol uses "lease-based file locking". Leases help 22a28faaddSChuck LeverNFSv4 servers provide file lock guarantees and manage their 23a28faaddSChuck Leverresources. 24a28faaddSChuck Lever 25a28faaddSChuck LeverSimply put, an NFSv4 server creates a lease for each NFSv4 client. 26a28faaddSChuck LeverThe server collects each client's file open and lock state under 27a28faaddSChuck Leverthe lease for that client. 28a28faaddSChuck Lever 29a28faaddSChuck LeverThe client is responsible for periodically renewing its leases. 30a28faaddSChuck LeverWhile a lease remains valid, the server holding that lease 31a28faaddSChuck Leverguarantees the file locks the client has created remain in place. 32a28faaddSChuck Lever 33a28faaddSChuck LeverIf a client stops renewing its lease (for example, if it crashes), 34a28faaddSChuck Leverthe NFSv4 protocol allows the server to remove the client's open 35a28faaddSChuck Leverand lock state after a certain period of time. When a client 36a28faaddSChuck Leverrestarts, it indicates to servers that open and lock state 37a28faaddSChuck Leverassociated with its previous leases is no longer valid and can be 38a28faaddSChuck Leverdestroyed immediately. 39a28faaddSChuck Lever 40a28faaddSChuck LeverIn addition, each NFSv4 server manages a persistent list of client 41a28faaddSChuck Leverleases. When the server restarts and clients attempt to recover 42a28faaddSChuck Levertheir state, the server uses this list to distinguish amongst 43a28faaddSChuck Leverclients that held state before the server restarted and clients 44a28faaddSChuck Leversending fresh OPEN and LOCK requests. This enables file locks to 45a28faaddSChuck Leverpersist safely across server restarts. 46a28faaddSChuck Lever 47a28faaddSChuck LeverNFSv4 client identifiers 48a28faaddSChuck Lever------------------------ 49a28faaddSChuck Lever 50a28faaddSChuck LeverEach NFSv4 client presents an identifier to NFSv4 servers so that 51a28faaddSChuck Leverthey can associate the client with its lease. Each client's 52a28faaddSChuck Leveridentifier consists of two elements: 53a28faaddSChuck Lever 54a28faaddSChuck Lever - co_ownerid: An arbitrary but fixed string. 55a28faaddSChuck Lever 56a28faaddSChuck Lever - boot verifier: A 64-bit incarnation verifier that enables a 57a28faaddSChuck Lever server to distinguish successive boot epochs of the same client. 58a28faaddSChuck Lever 59a28faaddSChuck LeverThe NFSv4.0 specification refers to these two items as an 60a28faaddSChuck Lever"nfs_client_id4". The NFSv4.1 specification refers to these two 61a28faaddSChuck Leveritems as a "client_owner4". 62a28faaddSChuck Lever 63a28faaddSChuck LeverNFSv4 servers tie this identifier to the principal and security 64a28faaddSChuck Leverflavor that the client used when presenting it. Servers use this 65a28faaddSChuck Leverprincipal to authorize subsequent lease modification operations 66a28faaddSChuck Leversent by the client. Effectively this principal is a third element of 67a28faaddSChuck Leverthe identifier. 68a28faaddSChuck Lever 69a28faaddSChuck LeverAs part of the identity presented to servers, a good 70a28faaddSChuck Lever"co_ownerid" string has several important properties: 71a28faaddSChuck Lever 72a28faaddSChuck Lever - The "co_ownerid" string identifies the client during reboot 73a28faaddSChuck Lever recovery, therefore the string is persistent across client 74a28faaddSChuck Lever reboots. 75a28faaddSChuck Lever - The "co_ownerid" string helps servers distinguish the client 76a28faaddSChuck Lever from others, therefore the string is globally unique. Note 77a28faaddSChuck Lever that there is no central authority that assigns "co_ownerid" 78a28faaddSChuck Lever strings. 79a28faaddSChuck Lever - Because it often appears on the network in the clear, the 80a28faaddSChuck Lever "co_ownerid" string does not reveal private information about 81a28faaddSChuck Lever the client itself. 82a28faaddSChuck Lever - The content of the "co_ownerid" string is set and unchanging 83a28faaddSChuck Lever before the client attempts NFSv4 mounts after a restart. 84a28faaddSChuck Lever - The NFSv4 protocol places a 1024-byte limit on the size of the 85a28faaddSChuck Lever "co_ownerid" string. 86a28faaddSChuck Lever 87a28faaddSChuck LeverProtecting NFSv4 lease state 88a28faaddSChuck Lever---------------------------- 89a28faaddSChuck Lever 90a28faaddSChuck LeverNFSv4 servers utilize the "client_owner4" as described above to 91a28faaddSChuck Leverassign a unique lease to each client. Under this scheme, there are 92a28faaddSChuck Levercircumstances where clients can interfere with each other. This is 93a28faaddSChuck Leverreferred to as "lease stealing". 94a28faaddSChuck Lever 95a28faaddSChuck LeverIf distinct clients present the same "co_ownerid" string and use 96a28faaddSChuck Leverthe same principal (for example, AUTH_SYS and UID 0), a server is 97a28faaddSChuck Leverunable to tell that the clients are not the same. Each distinct 98a28faaddSChuck Leverclient presents a different boot verifier, so it appears to the 99a28faaddSChuck Leverserver as if there is one client that is rebooting frequently. 100a28faaddSChuck LeverNeither client can maintain open or lock state in this scenario. 101a28faaddSChuck Lever 102a28faaddSChuck LeverIf distinct clients present the same "co_ownerid" string and use 103a28faaddSChuck Leverdistinct principals, the server is likely to allow the first client 104a28faaddSChuck Leverto operate normally but reject subsequent clients with the same 105a28faaddSChuck Lever"co_ownerid" string. 106a28faaddSChuck Lever 107a28faaddSChuck LeverIf a client's "co_ownerid" string or principal are not stable, 108a28faaddSChuck Leverstate recovery after a server or client reboot is not guaranteed. 109a28faaddSChuck LeverIf a client unexpectedly restarts but presents a different 110a28faaddSChuck Lever"co_ownerid" string or principal to the server, the server orphans 111a28faaddSChuck Leverthe client's previous open and lock state. This blocks access to 112a28faaddSChuck Leverlocked files until the server removes the orphaned state. 113a28faaddSChuck Lever 114a28faaddSChuck LeverIf the server restarts and a client presents a changed "co_ownerid" 115a28faaddSChuck Leverstring or principal to the server, the server will not allow the 116a28faaddSChuck Leverclient to reclaim its open and lock state, and may give those locks 117a28faaddSChuck Leverto other clients in the meantime. This is referred to as "lock 118a28faaddSChuck Leverstealing". 119a28faaddSChuck Lever 120a28faaddSChuck LeverLease stealing and lock stealing increase the potential for denial 121a28faaddSChuck Leverof service and in rare cases even data corruption. 122a28faaddSChuck Lever 123a28faaddSChuck LeverSelecting an appropriate client identifier 124a28faaddSChuck Lever------------------------------------------ 125a28faaddSChuck Lever 126a28faaddSChuck LeverBy default, the Linux NFSv4 client implementation constructs its 127a28faaddSChuck Lever"co_ownerid" string starting with the words "Linux NFS" followed by 128a28faaddSChuck Leverthe client's UTS node name (the same node name, incidentally, that 129a28faaddSChuck Leveris used as the "machine name" in an AUTH_SYS credential). In small 130a28faaddSChuck Leverdeployments, this construction is usually adequate. Often, however, 131a28faaddSChuck Leverthe node name by itself is not adequately unique, and can change 132a28faaddSChuck Leverunexpectedly. Problematic situations include: 133a28faaddSChuck Lever 134*d56b699dSBjorn Helgaas - NFS-root (diskless) clients, where the local DHCP server (or 135a28faaddSChuck Lever equivalent) does not provide a unique host name. 136a28faaddSChuck Lever 137a28faaddSChuck Lever - "Containers" within a single Linux host. If each container has 138a28faaddSChuck Lever a separate network namespace, but does not use the UTS namespace 139a28faaddSChuck Lever to provide a unique host name, then there can be multiple NFS 140a28faaddSChuck Lever client instances with the same host name. 141a28faaddSChuck Lever 142a28faaddSChuck Lever - Clients across multiple administrative domains that access a 143a28faaddSChuck Lever common NFS server. If hostnames are not assigned centrally 144a28faaddSChuck Lever then uniqueness cannot be guaranteed unless a domain name is 145a28faaddSChuck Lever included in the hostname. 146a28faaddSChuck Lever 147a28faaddSChuck LeverLinux provides two mechanisms to add uniqueness to its "co_ownerid" 148a28faaddSChuck Leverstring: 149a28faaddSChuck Lever 150a28faaddSChuck Lever nfs.nfs4_unique_id 151a28faaddSChuck Lever This module parameter can set an arbitrary uniquifier string 152a28faaddSChuck Lever via the kernel command line, or when the "nfs" module is 153a28faaddSChuck Lever loaded. 154a28faaddSChuck Lever 1553e2a0368SDave Wysochanski /sys/fs/nfs/net/nfs_client/identifier 156a28faaddSChuck Lever This virtual file, available since Linux 5.3, is local to the 157a28faaddSChuck Lever network namespace in which it is accessed and so can provide 158a28faaddSChuck Lever distinction between network namespaces (containers) when the 159a28faaddSChuck Lever hostname remains uniform. 160a28faaddSChuck Lever 161a28faaddSChuck LeverNote that this file is empty on name-space creation. If the 162a28faaddSChuck Levercontainer system has access to some sort of per-container identity 163a28faaddSChuck Leverthen that uniquifier can be used. For example, a uniquifier might 164a28faaddSChuck Leverbe formed at boot using the container's internal identifier: 165a28faaddSChuck Lever 166a28faaddSChuck Lever sha256sum /etc/machine-id | awk '{print $1}' \\ 1673e2a0368SDave Wysochanski > /sys/fs/nfs/net/nfs_client/identifier 168a28faaddSChuck Lever 169a28faaddSChuck LeverSecurity considerations 170a28faaddSChuck Lever----------------------- 171a28faaddSChuck Lever 172a28faaddSChuck LeverThe use of cryptographic security for lease management operations 173a28faaddSChuck Leveris strongly encouraged. 174a28faaddSChuck Lever 175a28faaddSChuck LeverIf NFS with Kerberos is not configured, a Linux NFSv4 client uses 176a28faaddSChuck LeverAUTH_SYS and UID 0 as the principal part of its client identity. 177a28faaddSChuck LeverThis configuration is not only insecure, it increases the risk of 178a28faaddSChuck Leverlease and lock stealing. However, it might be the only choice for 179a28faaddSChuck Leverclient configurations that have no local persistent storage. 180a28faaddSChuck Lever"co_ownerid" string uniqueness and persistence is critical in this 181a28faaddSChuck Levercase. 182a28faaddSChuck Lever 183a28faaddSChuck LeverWhen a Kerberos keytab is present on a Linux NFS client, the client 184a28faaddSChuck Leverattempts to use one of the principals in that keytab when 185a28faaddSChuck Leveridentifying itself to servers. The "sec=" mount option does not 186a28faaddSChuck Levercontrol this behavior. Alternately, a single-user client with a 187a28faaddSChuck LeverKerberos principal can use that principal in place of the client's 188a28faaddSChuck Leverhost principal. 189a28faaddSChuck Lever 190a28faaddSChuck LeverUsing Kerberos for this purpose enables the client and server to 191a28faaddSChuck Leveruse the same lease for operations covered by all "sec=" settings. 192a28faaddSChuck LeverAdditionally, the Linux NFS client uses the RPCSEC_GSS security 193a28faaddSChuck Leverflavor with Kerberos and the integrity QOS to prevent in-transit 194a28faaddSChuck Levermodification of lease modification requests. 195a28faaddSChuck Lever 196a28faaddSChuck LeverAdditional notes 197a28faaddSChuck Lever---------------- 198a28faaddSChuck LeverThe Linux NFSv4 client establishes a single lease on each NFSv4 199a28faaddSChuck Leverserver it accesses. NFSv4 mounts from a Linux NFSv4 client of a 200a28faaddSChuck Leverparticular server then share that lease. 201a28faaddSChuck Lever 202a28faaddSChuck LeverOnce a client establishes open and lock state, the NFSv4 protocol 203a28faaddSChuck Leverenables lease state to transition to other servers, following data 204a28faaddSChuck Leverthat has been migrated. This hides data migration completely from 205a28faaddSChuck Leverrunning applications. The Linux NFSv4 client facilitates state 206a28faaddSChuck Levermigration by presenting the same "client_owner4" to all servers it 207a28faaddSChuck Leverencounters. 208a28faaddSChuck Lever 209a28faaddSChuck Lever======== 210a28faaddSChuck LeverSee Also 211a28faaddSChuck Lever======== 212a28faaddSChuck Lever 213a28faaddSChuck Lever - nfs(5) 214a28faaddSChuck Lever - kerberos(7) 215a28faaddSChuck Lever - RFC 7530 for the NFSv4.0 specification 216a28faaddSChuck Lever - RFC 8881 for the NFSv4.1 specification. 217