client-identifier.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =======================
  3. NFSv4 client identifier
  4. =======================
  5. This document explains how the NFSv4 protocol identifies client
  6. instances in order to maintain file open and lock state during
  7. system restarts. A special identifier and principal are maintained
  8. on each client. These can be set by administrators, scripts
  9. provided by site administrators, or tools provided by Linux
  10. distributors.
  11. There are risks if a client's NFSv4 identifier and its principal
  12. are not chosen carefully.
  13. Introduction
  14. ------------
  15. The NFSv4 protocol uses "lease-based file locking". Leases help
  16. NFSv4 servers provide file lock guarantees and manage their
  17. resources.
  18. Simply put, an NFSv4 server creates a lease for each NFSv4 client.
  19. The server collects each client's file open and lock state under
  20. the lease for that client.
  21. The client is responsible for periodically renewing its leases.
  22. While a lease remains valid, the server holding that lease
  23. guarantees the file locks the client has created remain in place.
  24. If a client stops renewing its lease (for example, if it crashes),
  25. the NFSv4 protocol allows the server to remove the client's open
  26. and lock state after a certain period of time. When a client
  27. restarts, it indicates to servers that open and lock state
  28. associated with its previous leases is no longer valid and can be
  29. destroyed immediately.
  30. In addition, each NFSv4 server manages a persistent list of client
  31. leases. When the server restarts and clients attempt to recover
  32. their state, the server uses this list to distinguish amongst
  33. clients that held state before the server restarted and clients
  34. sending fresh OPEN and LOCK requests. This enables file locks to
  35. persist safely across server restarts.
  36. NFSv4 client identifiers
  37. ------------------------
  38. Each NFSv4 client presents an identifier to NFSv4 servers so that
  39. they can associate the client with its lease. Each client's
  40. identifier consists of two elements:
  41. - co_ownerid: An arbitrary but fixed string.
  42. - boot verifier: A 64-bit incarnation verifier that enables a
  43. server to distinguish successive boot epochs of the same client.
  44. The NFSv4.0 specification refers to these two items as an
  45. "nfs_client_id4". The NFSv4.1 specification refers to these two
  46. items as a "client_owner4".
  47. NFSv4 servers tie this identifier to the principal and security
  48. flavor that the client used when presenting it. Servers use this
  49. principal to authorize subsequent lease modification operations
  50. sent by the client. Effectively this principal is a third element of
  51. the identifier.
  52. As part of the identity presented to servers, a good
  53. "co_ownerid" string has several important properties:
  54. - The "co_ownerid" string identifies the client during reboot
  55. recovery, therefore the string is persistent across client
  56. reboots.
  57. - The "co_ownerid" string helps servers distinguish the client
  58. from others, therefore the string is globally unique. Note
  59. that there is no central authority that assigns "co_ownerid"
  60. strings.
  61. - Because it often appears on the network in the clear, the
  62. "co_ownerid" string does not reveal private information about
  63. the client itself.
  64. - The content of the "co_ownerid" string is set and unchanging
  65. before the client attempts NFSv4 mounts after a restart.
  66. - The NFSv4 protocol places a 1024-byte limit on the size of the
  67. "co_ownerid" string.
  68. Protecting NFSv4 lease state
  69. ----------------------------
  70. NFSv4 servers utilize the "client_owner4" as described above to
  71. assign a unique lease to each client. Under this scheme, there are
  72. circumstances where clients can interfere with each other. This is
  73. referred to as "lease stealing".
  74. If distinct clients present the same "co_ownerid" string and use
  75. the same principal (for example, AUTH_SYS and UID 0), a server is
  76. unable to tell that the clients are not the same. Each distinct
  77. client presents a different boot verifier, so it appears to the
  78. server as if there is one client that is rebooting frequently.
  79. Neither client can maintain open or lock state in this scenario.
  80. If distinct clients present the same "co_ownerid" string and use
  81. distinct principals, the server is likely to allow the first client
  82. to operate normally but reject subsequent clients with the same
  83. "co_ownerid" string.
  84. If a client's "co_ownerid" string or principal are not stable,
  85. state recovery after a server or client reboot is not guaranteed.
  86. If a client unexpectedly restarts but presents a different
  87. "co_ownerid" string or principal to the server, the server orphans
  88. the client's previous open and lock state. This blocks access to
  89. locked files until the server removes the orphaned state.
  90. If the server restarts and a client presents a changed "co_ownerid"
  91. string or principal to the server, the server will not allow the
  92. client to reclaim its open and lock state, and may give those locks
  93. to other clients in the meantime. This is referred to as "lock
  94. stealing".
  95. Lease stealing and lock stealing increase the potential for denial
  96. of service and in rare cases even data corruption.
  97. Selecting an appropriate client identifier
  98. ------------------------------------------
  99. By default, the Linux NFSv4 client implementation constructs its
  100. "co_ownerid" string starting with the words "Linux NFS" followed by
  101. the client's UTS node name (the same node name, incidentally, that
  102. is used as the "machine name" in an AUTH_SYS credential). In small
  103. deployments, this construction is usually adequate. Often, however,
  104. the node name by itself is not adequately unique, and can change
  105. unexpectedly. Problematic situations include:
  106. - NFS-root (diskless) clients, where the local DHCP server (or
  107. equivalent) does not provide a unique host name.
  108. - "Containers" within a single Linux host. If each container has
  109. a separate network namespace, but does not use the UTS namespace
  110. to provide a unique host name, then there can be multiple NFS
  111. client instances with the same host name.
  112. - Clients across multiple administrative domains that access a
  113. common NFS server. If hostnames are not assigned centrally
  114. then uniqueness cannot be guaranteed unless a domain name is
  115. included in the hostname.
  116. Linux provides two mechanisms to add uniqueness to its "co_ownerid"
  117. string:
  118. nfs.nfs4_unique_id
  119. This module parameter can set an arbitrary uniquifier string
  120. via the kernel command line, or when the "nfs" module is
  121. loaded.
  122. /sys/fs/nfs/net/nfs_client/identifier
  123. This virtual file, available since Linux 5.3, is local to the
  124. network namespace in which it is accessed and so can provide
  125. distinction between network namespaces (containers) when the
  126. hostname remains uniform.
  127. Note that this file is empty on name-space creation. If the
  128. container system has access to some sort of per-container identity
  129. then that uniquifier can be used. For example, a uniquifier might
  130. be formed at boot using the container's internal identifier:
  131. sha256sum /etc/machine-id | awk '{print $1}' \\
  132. > /sys/fs/nfs/net/nfs_client/identifier
  133. Security considerations
  134. -----------------------
  135. The use of cryptographic security for lease management operations
  136. is strongly encouraged.
  137. If NFS with Kerberos is not configured, a Linux NFSv4 client uses
  138. AUTH_SYS and UID 0 as the principal part of its client identity.
  139. This configuration is not only insecure, it increases the risk of
  140. lease and lock stealing. However, it might be the only choice for
  141. client configurations that have no local persistent storage.
  142. "co_ownerid" string uniqueness and persistence is critical in this
  143. case.
  144. When a Kerberos keytab is present on a Linux NFS client, the client
  145. attempts to use one of the principals in that keytab when
  146. identifying itself to servers. The "sec=" mount option does not
  147. control this behavior. Alternately, a single-user client with a
  148. Kerberos principal can use that principal in place of the client's
  149. host principal.
  150. Using Kerberos for this purpose enables the client and server to
  151. use the same lease for operations covered by all "sec=" settings.
  152. Additionally, the Linux NFS client uses the RPCSEC_GSS security
  153. flavor with Kerberos and the integrity QOS to prevent in-transit
  154. modification of lease modification requests.
  155. Additional notes
  156. ----------------
  157. The Linux NFSv4 client establishes a single lease on each NFSv4
  158. server it accesses. NFSv4 mounts from a Linux NFSv4 client of a
  159. particular server then share that lease.
  160. Once a client establishes open and lock state, the NFSv4 protocol
  161. enables lease state to transition to other servers, following data
  162. that has been migrated. This hides data migration completely from
  163. running applications. The Linux NFSv4 client facilitates state
  164. migration by presenting the same "client_owner4" to all servers it
  165. encounters.
  166. ========
  167. See Also
  168. ========
  169. - nfs(5)
  170. - kerberos(7)
  171. - RFC 7530 for the NFSv4.0 specification
  172. - RFC 8881 for the NFSv4.1 specification.