xref: /freebsd-14.2/lib/libc/sys/chmod.2 (revision e2ea39cb)
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.\" 4. 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 April 10, 2008
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.El
105.Pp
106If
107.Fn fchmodat
108is passed the special value
109.Dv AT_FDCWD
110in the
111.Fa fd
112parameter, the current working directory is used.
113If also
114.Fa flag
115is zero, the behavior is identical to a call to
116.Fn chmod .
117.Pp
118A mode is created from
119.Em or'd
120permission bit masks
121defined in
122.In sys/stat.h :
123.Pp
124.Bd -literal -offset indent -compact
125#define S_IRWXU 0000700    /* RWX mask for owner */
126#define S_IRUSR 0000400    /* R for owner */
127#define S_IWUSR 0000200    /* W for owner */
128#define S_IXUSR 0000100    /* X for owner */
129
130#define S_IRWXG 0000070    /* RWX mask for group */
131#define S_IRGRP 0000040    /* R for group */
132#define S_IWGRP 0000020    /* W for group */
133#define S_IXGRP 0000010    /* X for group */
134
135#define S_IRWXO 0000007    /* RWX mask for other */
136#define S_IROTH 0000004    /* R for other */
137#define S_IWOTH 0000002    /* W for other */
138#define S_IXOTH 0000001    /* X for other */
139
140#define S_ISUID 0004000    /* set user id on execution */
141#define S_ISGID 0002000    /* set group id on execution */
142#ifndef __BSD_VISIBLE
143#define S_ISTXT 0001000    /* sticky bit */
144#endif
145.Ed
146.Pp
147The
148.Fx
149VM system totally ignores the sticky bit
150.Pq Dv ISTXT
151for executables.
152On UFS-based file systems (FFS, LFS) the sticky
153bit may only be set upon directories.
154.Pp
155If mode
156.Dv ISTXT
157(the `sticky bit') is set on a directory,
158an unprivileged user may not delete or rename
159files of other users in that directory.
160The sticky bit may be
161set by any user on a directory which the user owns or has appropriate
162permissions.
163For more details of the properties of the sticky bit, see
164.Xr sticky 7 .
165.Pp
166If mode ISUID (set UID) is set on a directory,
167and the MNT_SUIDDIR option was used in the mount of the file system,
168then the owner of any new files and sub-directories
169created within this directory are set
170to be the same as the owner of that directory.
171If this function is enabled, new directories will inherit
172the bit from their parents.
173Execute bits are removed from
174the file, and it will not be given to root.
175This behavior does not change the
176requirements for the user to be allowed to write the file, but only the eventual
177owner after it has been created.
178Group inheritance is not affected.
179.Pp
180This feature is designed for use on fileservers serving PC users via
181ftp, SAMBA, or netatalk.
182It provides security holes for shell users and as
183such should not be used on shell machines, especially on home directories.
184This option requires the SUIDDIR
185option in the kernel to work.
186Only UFS file systems support this option.
187For more details of the suiddir mount option, see
188.Xr mount 8 .
189.Pp
190Writing or changing the owner of a file
191turns off the set-user-id and set-group-id bits
192unless the user is the super-user.
193This makes the system somewhat more secure
194by protecting set-user-id (set-group-id) files
195from remaining set-user-id (set-group-id) if they are modified,
196at the expense of a degree of compatibility.
197.Sh RETURN VALUES
198.Rv -std
199.Sh ERRORS
200The
201.Fn chmod
202system call
203will fail and the file mode will be unchanged if:
204.Bl -tag -width Er
205.It Bq Er ENOTDIR
206A component of the path prefix is not a directory.
207.It Bq Er ENAMETOOLONG
208A component of a pathname exceeded 255 characters,
209or an entire path name exceeded 1023 characters.
210.It Bq Er ENOENT
211The named file does not exist.
212.It Bq Er EACCES
213Search permission is denied for a component of the path prefix.
214.It Bq Er ELOOP
215Too many symbolic links were encountered in translating the pathname.
216.It Bq Er EPERM
217The effective user ID does not match the owner of the file and
218the effective user ID is not the super-user.
219.It Bq Er EPERM
220The effective user ID is not the super-user, the effective user ID do match the
221owner of the file, but the group ID of the file does not match the effective
222group ID nor one of the supplementary group IDs.
223.It Bq Er EPERM
224The named file has its immutable or append-only flag set, see the
225.Xr chflags 2
226manual page for more information.
227.It Bq Er EROFS
228The named file resides on a read-only file system.
229.It Bq Er EFAULT
230The
231.Fa path
232argument
233points outside the process's allocated address space.
234.It Bq Er EIO
235An I/O error occurred while reading from or writing to the file system.
236.It Bq Er EFTYPE
237The effective user ID is not the super-user, the mode includes the sticky bit
238.Dv ( S_ISVTX ) ,
239and path does not refer to a directory.
240.El
241.Pp
242The
243.Fn fchmod
244system call will fail if:
245.Bl -tag -width Er
246.It Bq Er EBADF
247The descriptor is not valid.
248.It Bq Er EINVAL
249The
250.Fa fd
251argument
252refers to a socket, not to a file.
253.It Bq Er EROFS
254The file resides on a read-only file system.
255.It Bq Er EIO
256An I/O error occurred while reading from or writing to the file system.
257.El
258.Pp
259In addition to the
260.Fn chmod
261errors,
262.Fn fchmodat
263fails if:
264.Bl -tag -width Er
265.It Bq Er EBADF
266The
267.Fa path
268argument does not specify an absolute path and the
269.Fa fd
270argument is neither
271.Fa AT_FDCWD
272nor a valid file descriptor open for searching.
273.It Bq Er EINVAL
274The value of the
275.Fa flag
276argument is not valid.
277.It Bq Er ENOTDIR
278The
279.Fa path
280argument is not an absolute path and
281.Fa fd
282is neither
283.Dv AT_FDCWD
284nor a file descriptor associated with a directory.
285.El
286.Sh SEE ALSO
287.Xr chmod 1 ,
288.Xr chflags 2 ,
289.Xr chown 2 ,
290.Xr open 2 ,
291.Xr stat 2 ,
292.Xr sticky 7
293.Sh STANDARDS
294The
295.Fn chmod
296system call is expected to conform to
297.St -p1003.1-90 ,
298except for the return of
299.Er EFTYPE
300and the use of
301.Dv S_ISTXT .
302The
303.Fn fchmodat
304system call follows The Open Group Extended API Set 2 specification.
305.Sh HISTORY
306The
307.Fn chmod
308function appeared in
309.At v7 .
310The
311.Fn fchmod
312system call appeared in
313.Bx 4.2 .
314The
315.Fn lchmod
316system call appeared in
317.Fx 3.0 .
318The
319.Fn fchmodat
320system call appeared in
321.Fx 8.0 .
322