mandatory-locking.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =====================================================
  3. Mandatory File Locking For The Linux Operating System
  4. =====================================================
  5. Andy Walker <andy@lysaker.kvaerner.no>
  6. 15 April 1996
  7. (Updated September 2007)
  8. 0. Why you should avoid mandatory locking
  9. -----------------------------------------
  10. The Linux implementation is prey to a number of difficult-to-fix race
  11. conditions which in practice make it not dependable:
  12. - The write system call checks for a mandatory lock only once
  13. at its start. It is therefore possible for a lock request to
  14. be granted after this check but before the data is modified.
  15. A process may then see file data change even while a mandatory
  16. lock was held.
  17. - Similarly, an exclusive lock may be granted on a file after
  18. the kernel has decided to proceed with a read, but before the
  19. read has actually completed, and the reading process may see
  20. the file data in a state which should not have been visible
  21. to it.
  22. - Similar races make the claimed mutual exclusion between lock
  23. and mmap similarly unreliable.
  24. 1. What is mandatory locking?
  25. ------------------------------
  26. Mandatory locking is kernel enforced file locking, as opposed to the more usual
  27. cooperative file locking used to guarantee sequential access to files among
  28. processes. File locks are applied using the flock() and fcntl() system calls
  29. (and the lockf() library routine which is a wrapper around fcntl().) It is
  30. normally a process' responsibility to check for locks on a file it wishes to
  31. update, before applying its own lock, updating the file and unlocking it again.
  32. The most commonly used example of this (and in the case of sendmail, the most
  33. troublesome) is access to a user's mailbox. The mail user agent and the mail
  34. transfer agent must guard against updating the mailbox at the same time, and
  35. prevent reading the mailbox while it is being updated.
  36. In a perfect world all processes would use and honour a cooperative, or
  37. "advisory" locking scheme. However, the world isn't perfect, and there's
  38. a lot of poorly written code out there.
  39. In trying to address this problem, the designers of System V UNIX came up
  40. with a "mandatory" locking scheme, whereby the operating system kernel would
  41. block attempts by a process to write to a file that another process holds a
  42. "read" -or- "shared" lock on, and block attempts to both read and write to a
  43. file that a process holds a "write " -or- "exclusive" lock on.
  44. The System V mandatory locking scheme was intended to have as little impact as
  45. possible on existing user code. The scheme is based on marking individual files
  46. as candidates for mandatory locking, and using the existing fcntl()/lockf()
  47. interface for applying locks just as if they were normal, advisory locks.
  48. .. Note::
  49. 1. In saying "file" in the paragraphs above I am actually not telling
  50. the whole truth. System V locking is based on fcntl(). The granularity of
  51. fcntl() is such that it allows the locking of byte ranges in files, in
  52. addition to entire files, so the mandatory locking rules also have byte
  53. level granularity.
  54. 2. POSIX.1 does not specify any scheme for mandatory locking, despite
  55. borrowing the fcntl() locking scheme from System V. The mandatory locking
  56. scheme is defined by the System V Interface Definition (SVID) Version 3.
  57. 2. Marking a file for mandatory locking
  58. ---------------------------------------
  59. A file is marked as a candidate for mandatory locking by setting the group-id
  60. bit in its file mode but removing the group-execute bit. This is an otherwise
  61. meaningless combination, and was chosen by the System V implementors so as not
  62. to break existing user programs.
  63. Note that the group-id bit is usually automatically cleared by the kernel when
  64. a setgid file is written to. This is a security measure. The kernel has been
  65. modified to recognize the special case of a mandatory lock candidate and to
  66. refrain from clearing this bit. Similarly the kernel has been modified not
  67. to run mandatory lock candidates with setgid privileges.
  68. 3. Available implementations
  69. ----------------------------
  70. I have considered the implementations of mandatory locking available with
  71. SunOS 4.1.x, Solaris 2.x and HP-UX 9.x.
  72. Generally I have tried to make the most sense out of the behaviour exhibited
  73. by these three reference systems. There are many anomalies.
  74. All the reference systems reject all calls to open() for a file on which
  75. another process has outstanding mandatory locks. This is in direct
  76. contravention of SVID 3, which states that only calls to open() with the
  77. O_TRUNC flag set should be rejected. The Linux implementation follows the SVID
  78. definition, which is the "Right Thing", since only calls with O_TRUNC can
  79. modify the contents of the file.
  80. HP-UX even disallows open() with O_TRUNC for a file with advisory locks, not
  81. just mandatory locks. That would appear to contravene POSIX.1.
  82. mmap() is another interesting case. All the operating systems mentioned
  83. prevent mandatory locks from being applied to an mmap()'ed file, but HP-UX
  84. also disallows advisory locks for such a file. SVID actually specifies the
  85. paranoid HP-UX behaviour.
  86. In my opinion only MAP_SHARED mappings should be immune from locking, and then
  87. only from mandatory locks - that is what is currently implemented.
  88. SunOS is so hopeless that it doesn't even honour the O_NONBLOCK flag for
  89. mandatory locks, so reads and writes to locked files always block when they
  90. should return EAGAIN.
  91. I'm afraid that this is such an esoteric area that the semantics described
  92. below are just as valid as any others, so long as the main points seem to
  93. agree.
  94. 4. Semantics
  95. ------------
  96. 1. Mandatory locks can only be applied via the fcntl()/lockf() locking
  97. interface - in other words the System V/POSIX interface. BSD style
  98. locks using flock() never result in a mandatory lock.
  99. 2. If a process has locked a region of a file with a mandatory read lock, then
  100. other processes are permitted to read from that region. If any of these
  101. processes attempts to write to the region it will block until the lock is
  102. released, unless the process has opened the file with the O_NONBLOCK
  103. flag in which case the system call will return immediately with the error
  104. status EAGAIN.
  105. 3. If a process has locked a region of a file with a mandatory write lock, all
  106. attempts to read or write to that region block until the lock is released,
  107. unless a process has opened the file with the O_NONBLOCK flag in which case
  108. the system call will return immediately with the error status EAGAIN.
  109. 4. Calls to open() with O_TRUNC, or to creat(), on a existing file that has
  110. any mandatory locks owned by other processes will be rejected with the
  111. error status EAGAIN.
  112. 5. Attempts to apply a mandatory lock to a file that is memory mapped and
  113. shared (via mmap() with MAP_SHARED) will be rejected with the error status
  114. EAGAIN.
  115. 6. Attempts to create a shared memory map of a file (via mmap() with MAP_SHARED)
  116. that has any mandatory locks in effect will be rejected with the error status
  117. EAGAIN.
  118. 5. Which system calls are affected?
  119. -----------------------------------
  120. Those which modify a file's contents, not just the inode. That gives read(),
  121. write(), readv(), writev(), open(), creat(), mmap(), truncate() and
  122. ftruncate(). truncate() and ftruncate() are considered to be "write" actions
  123. for the purposes of mandatory locking.
  124. The affected region is usually defined as stretching from the current position
  125. for the total number of bytes read or written. For the truncate calls it is
  126. defined as the bytes of a file removed or added (we must also consider bytes
  127. added, as a lock can specify just "the whole file", rather than a specific
  128. range of bytes.)
  129. Note 3: I may have overlooked some system calls that need mandatory lock
  130. checking in my eagerness to get this code out the door. Please let me know, or
  131. better still fix the system calls yourself and submit a patch to me or Linus.
  132. 6. Warning!
  133. -----------
  134. Not even root can override a mandatory lock, so runaway processes can wreak
  135. havoc if they lock crucial files. The way around it is to change the file
  136. permissions (remove the setgid bit) before trying to read or write to it.
  137. Of course, that might be a bit tricky if the system is hung :-(
  138. 7. The "mand" mount option
  139. --------------------------
  140. Mandatory locking is disabled on all filesystems by default, and must be
  141. administratively enabled by mounting with "-o mand". That mount option
  142. is only allowed if the mounting task has the CAP_SYS_ADMIN capability.
  143. Since kernel v4.5, it is possible to disable mandatory locking
  144. altogether by setting CONFIG_MANDATORY_FILE_LOCKING to "n". A kernel
  145. with this disabled will reject attempts to mount filesystems with the
  146. "mand" mount option with the error status EPERM.