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