162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci=======================
462306a36Sopenharmony_ciNFSv4 client identifier
562306a36Sopenharmony_ci=======================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciThis document explains how the NFSv4 protocol identifies client
862306a36Sopenharmony_ciinstances in order to maintain file open and lock state during
962306a36Sopenharmony_cisystem restarts. A special identifier and principal are maintained
1062306a36Sopenharmony_cion each client. These can be set by administrators, scripts
1162306a36Sopenharmony_ciprovided by site administrators, or tools provided by Linux
1262306a36Sopenharmony_cidistributors.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciThere are risks if a client's NFSv4 identifier and its principal
1562306a36Sopenharmony_ciare not chosen carefully.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciIntroduction
1962306a36Sopenharmony_ci------------
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciThe NFSv4 protocol uses "lease-based file locking". Leases help
2262306a36Sopenharmony_ciNFSv4 servers provide file lock guarantees and manage their
2362306a36Sopenharmony_ciresources.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciSimply put, an NFSv4 server creates a lease for each NFSv4 client.
2662306a36Sopenharmony_ciThe server collects each client's file open and lock state under
2762306a36Sopenharmony_cithe lease for that client.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciThe client is responsible for periodically renewing its leases.
3062306a36Sopenharmony_ciWhile a lease remains valid, the server holding that lease
3162306a36Sopenharmony_ciguarantees the file locks the client has created remain in place.
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciIf a client stops renewing its lease (for example, if it crashes),
3462306a36Sopenharmony_cithe NFSv4 protocol allows the server to remove the client's open
3562306a36Sopenharmony_ciand lock state after a certain period of time. When a client
3662306a36Sopenharmony_cirestarts, it indicates to servers that open and lock state
3762306a36Sopenharmony_ciassociated with its previous leases is no longer valid and can be
3862306a36Sopenharmony_cidestroyed immediately.
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciIn addition, each NFSv4 server manages a persistent list of client
4162306a36Sopenharmony_cileases. When the server restarts and clients attempt to recover
4262306a36Sopenharmony_citheir state, the server uses this list to distinguish amongst
4362306a36Sopenharmony_ciclients that held state before the server restarted and clients
4462306a36Sopenharmony_cisending fresh OPEN and LOCK requests. This enables file locks to
4562306a36Sopenharmony_cipersist safely across server restarts.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciNFSv4 client identifiers
4862306a36Sopenharmony_ci------------------------
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciEach NFSv4 client presents an identifier to NFSv4 servers so that
5162306a36Sopenharmony_cithey can associate the client with its lease. Each client's
5262306a36Sopenharmony_ciidentifier consists of two elements:
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci  - co_ownerid: An arbitrary but fixed string.
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci  - boot verifier: A 64-bit incarnation verifier that enables a
5762306a36Sopenharmony_ci    server to distinguish successive boot epochs of the same client.
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciThe NFSv4.0 specification refers to these two items as an
6062306a36Sopenharmony_ci"nfs_client_id4". The NFSv4.1 specification refers to these two
6162306a36Sopenharmony_ciitems as a "client_owner4".
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciNFSv4 servers tie this identifier to the principal and security
6462306a36Sopenharmony_ciflavor that the client used when presenting it. Servers use this
6562306a36Sopenharmony_ciprincipal to authorize subsequent lease modification operations
6662306a36Sopenharmony_cisent by the client. Effectively this principal is a third element of
6762306a36Sopenharmony_cithe identifier.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciAs part of the identity presented to servers, a good
7062306a36Sopenharmony_ci"co_ownerid" string has several important properties:
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci  - The "co_ownerid" string identifies the client during reboot
7362306a36Sopenharmony_ci    recovery, therefore the string is persistent across client
7462306a36Sopenharmony_ci    reboots.
7562306a36Sopenharmony_ci  - The "co_ownerid" string helps servers distinguish the client
7662306a36Sopenharmony_ci    from others, therefore the string is globally unique. Note
7762306a36Sopenharmony_ci    that there is no central authority that assigns "co_ownerid"
7862306a36Sopenharmony_ci    strings.
7962306a36Sopenharmony_ci  - Because it often appears on the network in the clear, the
8062306a36Sopenharmony_ci    "co_ownerid" string does not reveal private information about
8162306a36Sopenharmony_ci    the client itself.
8262306a36Sopenharmony_ci  - The content of the "co_ownerid" string is set and unchanging
8362306a36Sopenharmony_ci    before the client attempts NFSv4 mounts after a restart.
8462306a36Sopenharmony_ci  - The NFSv4 protocol places a 1024-byte limit on the size of the
8562306a36Sopenharmony_ci    "co_ownerid" string.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciProtecting NFSv4 lease state
8862306a36Sopenharmony_ci----------------------------
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciNFSv4 servers utilize the "client_owner4" as described above to
9162306a36Sopenharmony_ciassign a unique lease to each client. Under this scheme, there are
9262306a36Sopenharmony_cicircumstances where clients can interfere with each other. This is
9362306a36Sopenharmony_cireferred to as "lease stealing".
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciIf distinct clients present the same "co_ownerid" string and use
9662306a36Sopenharmony_cithe same principal (for example, AUTH_SYS and UID 0), a server is
9762306a36Sopenharmony_ciunable to tell that the clients are not the same. Each distinct
9862306a36Sopenharmony_ciclient presents a different boot verifier, so it appears to the
9962306a36Sopenharmony_ciserver as if there is one client that is rebooting frequently.
10062306a36Sopenharmony_ciNeither client can maintain open or lock state in this scenario.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciIf distinct clients present the same "co_ownerid" string and use
10362306a36Sopenharmony_cidistinct principals, the server is likely to allow the first client
10462306a36Sopenharmony_cito operate normally but reject subsequent clients with the same
10562306a36Sopenharmony_ci"co_ownerid" string.
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciIf a client's "co_ownerid" string or principal are not stable,
10862306a36Sopenharmony_cistate recovery after a server or client reboot is not guaranteed.
10962306a36Sopenharmony_ciIf a client unexpectedly restarts but presents a different
11062306a36Sopenharmony_ci"co_ownerid" string or principal to the server, the server orphans
11162306a36Sopenharmony_cithe client's previous open and lock state. This blocks access to
11262306a36Sopenharmony_cilocked files until the server removes the orphaned state.
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ciIf the server restarts and a client presents a changed "co_ownerid"
11562306a36Sopenharmony_cistring or principal to the server, the server will not allow the
11662306a36Sopenharmony_ciclient to reclaim its open and lock state, and may give those locks
11762306a36Sopenharmony_cito other clients in the meantime. This is referred to as "lock
11862306a36Sopenharmony_cistealing".
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ciLease stealing and lock stealing increase the potential for denial
12162306a36Sopenharmony_ciof service and in rare cases even data corruption.
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciSelecting an appropriate client identifier
12462306a36Sopenharmony_ci------------------------------------------
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ciBy default, the Linux NFSv4 client implementation constructs its
12762306a36Sopenharmony_ci"co_ownerid" string starting with the words "Linux NFS" followed by
12862306a36Sopenharmony_cithe client's UTS node name (the same node name, incidentally, that
12962306a36Sopenharmony_ciis used as the "machine name" in an AUTH_SYS credential). In small
13062306a36Sopenharmony_cideployments, this construction is usually adequate. Often, however,
13162306a36Sopenharmony_cithe node name by itself is not adequately unique, and can change
13262306a36Sopenharmony_ciunexpectedly. Problematic situations include:
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci  - NFS-root (diskless) clients, where the local DHCP server (or
13562306a36Sopenharmony_ci    equivalent) does not provide a unique host name.
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci  - "Containers" within a single Linux host.  If each container has
13862306a36Sopenharmony_ci    a separate network namespace, but does not use the UTS namespace
13962306a36Sopenharmony_ci    to provide a unique host name, then there can be multiple NFS
14062306a36Sopenharmony_ci    client instances with the same host name.
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci  - Clients across multiple administrative domains that access a
14362306a36Sopenharmony_ci    common NFS server. If hostnames are not assigned centrally
14462306a36Sopenharmony_ci    then uniqueness cannot be guaranteed unless a domain name is
14562306a36Sopenharmony_ci    included in the hostname.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciLinux provides two mechanisms to add uniqueness to its "co_ownerid"
14862306a36Sopenharmony_cistring:
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci    nfs.nfs4_unique_id
15162306a36Sopenharmony_ci      This module parameter can set an arbitrary uniquifier string
15262306a36Sopenharmony_ci      via the kernel command line, or when the "nfs" module is
15362306a36Sopenharmony_ci      loaded.
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci    /sys/fs/nfs/net/nfs_client/identifier
15662306a36Sopenharmony_ci      This virtual file, available since Linux 5.3, is local to the
15762306a36Sopenharmony_ci      network namespace in which it is accessed and so can provide
15862306a36Sopenharmony_ci      distinction between network namespaces (containers) when the
15962306a36Sopenharmony_ci      hostname remains uniform.
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciNote that this file is empty on name-space creation. If the
16262306a36Sopenharmony_cicontainer system has access to some sort of per-container identity
16362306a36Sopenharmony_cithen that uniquifier can be used. For example, a uniquifier might
16462306a36Sopenharmony_cibe formed at boot using the container's internal identifier:
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci    sha256sum /etc/machine-id | awk '{print $1}' \\
16762306a36Sopenharmony_ci        > /sys/fs/nfs/net/nfs_client/identifier
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ciSecurity considerations
17062306a36Sopenharmony_ci-----------------------
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciThe use of cryptographic security for lease management operations
17362306a36Sopenharmony_ciis strongly encouraged.
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciIf NFS with Kerberos is not configured, a Linux NFSv4 client uses
17662306a36Sopenharmony_ciAUTH_SYS and UID 0 as the principal part of its client identity.
17762306a36Sopenharmony_ciThis configuration is not only insecure, it increases the risk of
17862306a36Sopenharmony_cilease and lock stealing. However, it might be the only choice for
17962306a36Sopenharmony_ciclient configurations that have no local persistent storage.
18062306a36Sopenharmony_ci"co_ownerid" string uniqueness and persistence is critical in this
18162306a36Sopenharmony_cicase.
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ciWhen a Kerberos keytab is present on a Linux NFS client, the client
18462306a36Sopenharmony_ciattempts to use one of the principals in that keytab when
18562306a36Sopenharmony_ciidentifying itself to servers. The "sec=" mount option does not
18662306a36Sopenharmony_cicontrol this behavior. Alternately, a single-user client with a
18762306a36Sopenharmony_ciKerberos principal can use that principal in place of the client's
18862306a36Sopenharmony_cihost principal.
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ciUsing Kerberos for this purpose enables the client and server to
19162306a36Sopenharmony_ciuse the same lease for operations covered by all "sec=" settings.
19262306a36Sopenharmony_ciAdditionally, the Linux NFS client uses the RPCSEC_GSS security
19362306a36Sopenharmony_ciflavor with Kerberos and the integrity QOS to prevent in-transit
19462306a36Sopenharmony_cimodification of lease modification requests.
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ciAdditional notes
19762306a36Sopenharmony_ci----------------
19862306a36Sopenharmony_ciThe Linux NFSv4 client establishes a single lease on each NFSv4
19962306a36Sopenharmony_ciserver it accesses. NFSv4 mounts from a Linux NFSv4 client of a
20062306a36Sopenharmony_ciparticular server then share that lease.
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ciOnce a client establishes open and lock state, the NFSv4 protocol
20362306a36Sopenharmony_cienables lease state to transition to other servers, following data
20462306a36Sopenharmony_cithat has been migrated. This hides data migration completely from
20562306a36Sopenharmony_cirunning applications. The Linux NFSv4 client facilitates state
20662306a36Sopenharmony_cimigration by presenting the same "client_owner4" to all servers it
20762306a36Sopenharmony_ciencounters.
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci========
21062306a36Sopenharmony_ciSee Also
21162306a36Sopenharmony_ci========
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci  - nfs(5)
21462306a36Sopenharmony_ci  - kerberos(7)
21562306a36Sopenharmony_ci  - RFC 7530 for the NFSv4.0 specification
21662306a36Sopenharmony_ci  - RFC 8881 for the NFSv4.1 specification.
217