chmod.2 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. .\" Copyright (c) 1980, 1991, 1993
  2. .\" The Regents of the University of California. All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  7. .\" 1. Redistributions of source code must retain the above copyright
  8. .\" notice, this list of conditions and the following disclaimer.
  9. .\" 2. Redistributions in binary form must reproduce the above copyright
  10. .\" notice, this list of conditions and the following disclaimer in the
  11. .\" documentation and/or other materials provided with the distribution.
  12. .\" 3. Neither the name of the University nor the names of its contributors
  13. .\" may be used to endorse or promote products derived from this software
  14. .\" without specific prior written permission.
  15. .\"
  16. .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  17. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  20. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. .\" SUCH DAMAGE.
  27. .\"
  28. .Dd March 30, 2021
  29. .Dt CHMOD 2
  30. .Os
  31. .Sh NAME
  32. .Nm chmod ,
  33. .Nm fchmod ,
  34. .Nm lchmod ,
  35. .Nm fchmodat
  36. .Nd change mode of file
  37. .Sh LIBRARY
  38. .Lb libc
  39. .Sh SYNOPSIS
  40. .In sys/stat.h
  41. .Ft int
  42. .Fn chmod "const char *path" "mode_t mode"
  43. .Ft int
  44. .Fn fchmod "int fd" "mode_t mode"
  45. .Ft int
  46. .Fn lchmod "const char *path" "mode_t mode"
  47. .Ft int
  48. .Fn fchmodat "int fd" "const char *path" "mode_t mode" "int flag"
  49. .Sh DESCRIPTION
  50. The file permission bits of the file named specified by
  51. .Fa path
  52. or referenced by the file descriptor
  53. .Fa fd
  54. are changed to
  55. .Fa mode .
  56. The
  57. .Fn chmod
  58. system call verifies that the process owner (user) either owns
  59. the file specified by
  60. .Fa path
  61. (or
  62. .Fa fd ) ,
  63. or
  64. is the super-user.
  65. The
  66. .Fn chmod
  67. system call follows symbolic links to operate on the target of the link
  68. rather than the link itself.
  69. .Pp
  70. The
  71. .Fn lchmod
  72. system call is similar to
  73. .Fn chmod
  74. but does not follow symbolic links.
  75. .Pp
  76. The
  77. .Fn fchmodat
  78. is equivalent to either
  79. .Fn chmod
  80. or
  81. .Fn lchmod
  82. depending on the
  83. .Fa flag
  84. except in the case where
  85. .Fa path
  86. specifies a relative path.
  87. In this case the file to be changed is determined relative to the directory
  88. associated with the file descriptor
  89. .Fa fd
  90. instead of the current working directory.
  91. The values for the
  92. .Fa flag
  93. are constructed by a bitwise-inclusive OR of flags from the following list, defined
  94. in
  95. .In fcntl.h :
  96. .Bl -tag -width indent
  97. .It Dv AT_SYMLINK_NOFOLLOW
  98. If
  99. .Fa path
  100. names a symbolic link, then the mode of the symbolic link is changed.
  101. .It Dv AT_RESOLVE_BENEATH
  102. Only walk paths below the directory specified by the
  103. .Ar fd
  104. descriptor.
  105. See the description of the
  106. .Dv O_RESOLVE_BENEATH
  107. flag in the
  108. .Xr open 2
  109. manual page.
  110. .It Dv AT_EMPTY_PATH
  111. If the
  112. .Fa path
  113. argument is an empty string, operate on the file or directory
  114. referenced by the descriptor
  115. .Fa fd .
  116. If
  117. .Fa fd
  118. is equal to
  119. .Dv AT_FDCWD ,
  120. operate on the current working directory.
  121. .El
  122. .Pp
  123. If
  124. .Fn fchmodat
  125. is passed the special value
  126. .Dv AT_FDCWD
  127. in the
  128. .Fa fd
  129. parameter, the current working directory is used.
  130. If also
  131. .Fa flag
  132. is zero, the behavior is identical to a call to
  133. .Fn chmod .
  134. .Pp
  135. A mode is created from
  136. .Em or'd
  137. permission bit masks
  138. defined in
  139. .In sys/stat.h :
  140. .Pp
  141. .Bd -literal -offset indent -compact
  142. #define S_IRWXU 0000700 /* RWX mask for owner */
  143. #define S_IRUSR 0000400 /* R for owner */
  144. #define S_IWUSR 0000200 /* W for owner */
  145. #define S_IXUSR 0000100 /* X for owner */
  146. #define S_IRWXG 0000070 /* RWX mask for group */
  147. #define S_IRGRP 0000040 /* R for group */
  148. #define S_IWGRP 0000020 /* W for group */
  149. #define S_IXGRP 0000010 /* X for group */
  150. #define S_IRWXO 0000007 /* RWX mask for other */
  151. #define S_IROTH 0000004 /* R for other */
  152. #define S_IWOTH 0000002 /* W for other */
  153. #define S_IXOTH 0000001 /* X for other */
  154. #define S_ISUID 0004000 /* set user id on execution */
  155. #define S_ISGID 0002000 /* set group id on execution */
  156. #define S_ISVTX 0001000 /* sticky bit */
  157. .Ed
  158. .Pp
  159. The non-standard
  160. .Dv S_ISTXT
  161. is a synonym for
  162. .Dv S_ISVTX .
  163. .Pp
  164. The
  165. .Fx
  166. VM system totally ignores the sticky bit
  167. .Pq Dv S_ISVTX
  168. for executables.
  169. On UFS-based file systems (FFS, LFS) the sticky
  170. bit may only be set upon directories.
  171. .Pp
  172. If mode
  173. .Dv S_ISVTX
  174. (the `sticky bit') is set on a directory,
  175. an unprivileged user may not delete or rename
  176. files of other users in that directory.
  177. The sticky bit may be
  178. set by any user on a directory which the user owns or has appropriate
  179. permissions.
  180. For more details of the properties of the sticky bit, see
  181. .Xr sticky 7 .
  182. .Pp
  183. If mode ISUID (set UID) is set on a directory,
  184. and the MNT_SUIDDIR option was used in the mount of the file system,
  185. then the owner of any new files and sub-directories
  186. created within this directory are set
  187. to be the same as the owner of that directory.
  188. If this function is enabled, new directories will inherit
  189. the bit from their parents.
  190. Execute bits are removed from
  191. the file, and it will not be given to root.
  192. This behavior does not change the
  193. requirements for the user to be allowed to write the file, but only the eventual
  194. owner after it has been created.
  195. Group inheritance is not affected.
  196. .Pp
  197. This feature is designed for use on fileservers serving PC users via
  198. ftp, SAMBA, or netatalk.
  199. It provides security holes for shell users and as
  200. such should not be used on shell machines, especially on home directories.
  201. This option requires the SUIDDIR
  202. option in the kernel to work.
  203. Only UFS file systems support this option.
  204. For more details of the suiddir mount option, see
  205. .Xr mount 8 .
  206. .Pp
  207. Writing or changing the owner of a file
  208. turns off the set-user-id and set-group-id bits
  209. unless the user is the super-user.
  210. This makes the system somewhat more secure
  211. by protecting set-user-id (set-group-id) files
  212. from remaining set-user-id (set-group-id) if they are modified,
  213. at the expense of a degree of compatibility.
  214. .Sh RETURN VALUES
  215. .Rv -std
  216. .Sh ERRORS
  217. The
  218. .Fn chmod
  219. system call
  220. will fail and the file mode will be unchanged if:
  221. .Bl -tag -width Er
  222. .It Bq Er ENOTDIR
  223. A component of the path prefix is not a directory.
  224. .It Bq Er ENAMETOOLONG
  225. A component of a pathname exceeded 255 characters,
  226. or an entire path name exceeded 1023 characters.
  227. .It Bq Er ENOENT
  228. The named file does not exist.
  229. .It Bq Er EACCES
  230. Search permission is denied for a component of the path prefix.
  231. .It Bq Er ELOOP
  232. Too many symbolic links were encountered in translating the pathname.
  233. .It Bq Er EPERM
  234. The effective user ID does not match the owner of the file and
  235. the effective user ID is not the super-user.
  236. .It Bq Er EPERM
  237. The effective user ID is not the super-user, the effective user ID do match the
  238. owner of the file, but the group ID of the file does not match the effective
  239. group ID nor one of the supplementary group IDs.
  240. .It Bq Er EPERM
  241. The named file has its immutable or append-only flag set, see the
  242. .Xr chflags 2
  243. manual page for more information.
  244. .It Bq Er EROFS
  245. The named file resides on a read-only file system.
  246. .It Bq Er EFAULT
  247. The
  248. .Fa path
  249. argument
  250. points outside the process's allocated address space.
  251. .It Bq Er EIO
  252. An I/O error occurred while reading from or writing to the file system.
  253. .It Bq Er EINTEGRITY
  254. Corrupted data was detected while reading from the file system.
  255. .It Bq Er EFTYPE
  256. The effective user ID is not the super-user, the mode includes the sticky bit
  257. .Dv ( S_ISVTX ) ,
  258. and path does not refer to a directory.
  259. .El
  260. .Pp
  261. The
  262. .Fn fchmod
  263. system call will fail if:
  264. .Bl -tag -width Er
  265. .It Bq Er EBADF
  266. The descriptor is not valid.
  267. .It Bq Er EINVAL
  268. The
  269. .Fa fd
  270. argument
  271. refers to a socket, not to a file.
  272. .It Bq Er EROFS
  273. The file resides on a read-only file system.
  274. .It Bq Er EIO
  275. An I/O error occurred while reading from or writing to the file system.
  276. .It Bq Er EINTEGRITY
  277. Corrupted data was detected while reading from the file system.
  278. .El
  279. .Pp
  280. In addition to the
  281. .Fn chmod
  282. errors,
  283. .Fn fchmodat
  284. fails if:
  285. .Bl -tag -width Er
  286. .It Bq Er EBADF
  287. The
  288. .Fa path
  289. argument does not specify an absolute path and the
  290. .Fa fd
  291. argument is neither
  292. .Fa AT_FDCWD
  293. nor a valid file descriptor open for searching.
  294. .It Bq Er EINVAL
  295. The value of the
  296. .Fa flag
  297. argument is not valid.
  298. .It Bq Er ENOTDIR
  299. The
  300. .Fa path
  301. argument is not an absolute path and
  302. .Fa fd
  303. is neither
  304. .Dv AT_FDCWD
  305. nor a file descriptor associated with a directory.
  306. .It Bq Er ENOTCAPABLE
  307. .Fa path
  308. is an absolute path,
  309. or contained a ".." component leading to a
  310. directory outside of the directory hierarchy specified by
  311. .Fa fd ,
  312. and the process is in capability mode or the
  313. .Dv AT_RESOLVE_BENEATH
  314. flag was specified.
  315. .El
  316. .Sh SEE ALSO
  317. .Xr chmod 1 ,
  318. .Xr chflags 2 ,
  319. .Xr chown 2 ,
  320. .Xr open 2 ,
  321. .Xr stat 2 ,
  322. .Xr sticky 7
  323. .Sh STANDARDS
  324. The
  325. .Fn chmod
  326. system call is expected to conform to
  327. .St -p1003.1-90 ,
  328. except for the return of
  329. .Er EFTYPE .
  330. The
  331. .Dv S_ISVTX
  332. bit on directories is expected to conform to
  333. .St -susv3 .
  334. The
  335. .Fn fchmodat
  336. system call is expected to conform to
  337. .St -p1003.1-2008 .
  338. .Sh HISTORY
  339. The
  340. .Fn chmod
  341. function appeared in
  342. .At v1 .
  343. The
  344. .Fn fchmod
  345. system call appeared in
  346. .Bx 4.2 .
  347. The
  348. .Fn lchmod
  349. system call appeared in
  350. .Fx 3.0 .
  351. The
  352. .Fn fchmodat
  353. system call appeared in
  354. .Fx 8.0 .