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