xref: /freebsd/lib/libsys/chmod.2 (revision bfd03046d18776ea70785ca1ef36dfc60822de3b)
18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993
28269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
108269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
118269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
138269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
148269e767SBrooks Davis.\"    without specific prior written permission.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268269e767SBrooks Davis.\" SUCH DAMAGE.
278269e767SBrooks Davis.\"
28*bfd03046SMark Johnston.Dd October 31, 2024
298269e767SBrooks Davis.Dt CHMOD 2
308269e767SBrooks Davis.Os
318269e767SBrooks Davis.Sh NAME
328269e767SBrooks Davis.Nm chmod ,
338269e767SBrooks Davis.Nm fchmod ,
348269e767SBrooks Davis.Nm lchmod ,
358269e767SBrooks Davis.Nm fchmodat
368269e767SBrooks Davis.Nd change mode of file
378269e767SBrooks Davis.Sh LIBRARY
388269e767SBrooks Davis.Lb libc
398269e767SBrooks Davis.Sh SYNOPSIS
408269e767SBrooks Davis.In sys/stat.h
418269e767SBrooks Davis.Ft int
428269e767SBrooks Davis.Fn chmod "const char *path" "mode_t mode"
438269e767SBrooks Davis.Ft int
448269e767SBrooks Davis.Fn fchmod "int fd" "mode_t mode"
458269e767SBrooks Davis.Ft int
468269e767SBrooks Davis.Fn lchmod "const char *path" "mode_t mode"
478269e767SBrooks Davis.Ft int
488269e767SBrooks Davis.Fn fchmodat "int fd" "const char *path" "mode_t mode" "int flag"
498269e767SBrooks Davis.Sh DESCRIPTION
508269e767SBrooks DavisThe file permission bits of the file named specified by
518269e767SBrooks Davis.Fa path
528269e767SBrooks Davisor referenced by the file descriptor
538269e767SBrooks Davis.Fa fd
548269e767SBrooks Davisare changed to
558269e767SBrooks Davis.Fa mode .
568269e767SBrooks DavisThe
578269e767SBrooks Davis.Fn chmod
588269e767SBrooks Davissystem call verifies that the process owner (user) either owns
598269e767SBrooks Davisthe file specified by
608269e767SBrooks Davis.Fa path
618269e767SBrooks Davis(or
628269e767SBrooks Davis.Fa fd ) ,
638269e767SBrooks Davisor
648269e767SBrooks Davisis the super-user.
658269e767SBrooks DavisThe
668269e767SBrooks Davis.Fn chmod
678269e767SBrooks Davissystem call follows symbolic links to operate on the target of the link
688269e767SBrooks Davisrather than the link itself.
698269e767SBrooks Davis.Pp
708269e767SBrooks DavisThe
718269e767SBrooks Davis.Fn lchmod
728269e767SBrooks Davissystem call is similar to
738269e767SBrooks Davis.Fn chmod
748269e767SBrooks Davisbut does not follow symbolic links.
758269e767SBrooks Davis.Pp
768269e767SBrooks DavisThe
778269e767SBrooks Davis.Fn fchmodat
788269e767SBrooks Davisis equivalent to either
798269e767SBrooks Davis.Fn chmod
808269e767SBrooks Davisor
818269e767SBrooks Davis.Fn lchmod
828269e767SBrooks Davisdepending on the
838269e767SBrooks Davis.Fa flag
848269e767SBrooks Davisexcept in the case where
858269e767SBrooks Davis.Fa path
868269e767SBrooks Davisspecifies a relative path.
878269e767SBrooks DavisIn this case the file to be changed is determined relative to the directory
888269e767SBrooks Davisassociated with the file descriptor
898269e767SBrooks Davis.Fa fd
908269e767SBrooks Davisinstead of the current working directory.
918269e767SBrooks DavisThe values for the
928269e767SBrooks Davis.Fa flag
938269e767SBrooks Davisare constructed by a bitwise-inclusive OR of flags from the following list, defined
948269e767SBrooks Davisin
958269e767SBrooks Davis.In fcntl.h :
968269e767SBrooks Davis.Bl -tag -width indent
978269e767SBrooks Davis.It Dv AT_SYMLINK_NOFOLLOW
988269e767SBrooks DavisIf
998269e767SBrooks Davis.Fa path
1008269e767SBrooks Davisnames a symbolic link, then the mode of the symbolic link is changed.
1018269e767SBrooks Davis.It Dv AT_RESOLVE_BENEATH
1028269e767SBrooks DavisOnly walk paths below the directory specified by the
1038269e767SBrooks Davis.Ar fd
1048269e767SBrooks Davisdescriptor.
1058269e767SBrooks DavisSee the description of the
1068269e767SBrooks Davis.Dv O_RESOLVE_BENEATH
1078269e767SBrooks Davisflag in the
1088269e767SBrooks Davis.Xr open 2
1098269e767SBrooks Davismanual page.
1108269e767SBrooks Davis.It Dv AT_EMPTY_PATH
1118269e767SBrooks DavisIf the
1128269e767SBrooks Davis.Fa path
1138269e767SBrooks Davisargument is an empty string, operate on the file or directory
1148269e767SBrooks Davisreferenced by the descriptor
1158269e767SBrooks Davis.Fa fd .
1168269e767SBrooks DavisIf
1178269e767SBrooks Davis.Fa fd
1188269e767SBrooks Davisis equal to
1198269e767SBrooks Davis.Dv AT_FDCWD ,
1208269e767SBrooks Davisoperate on the current working directory.
1218269e767SBrooks Davis.El
1228269e767SBrooks Davis.Pp
1238269e767SBrooks DavisIf
1248269e767SBrooks Davis.Fn fchmodat
1258269e767SBrooks Davisis passed the special value
1268269e767SBrooks Davis.Dv AT_FDCWD
1278269e767SBrooks Davisin the
1288269e767SBrooks Davis.Fa fd
1298269e767SBrooks Davisparameter, the current working directory is used.
1308269e767SBrooks DavisIf also
1318269e767SBrooks Davis.Fa flag
1328269e767SBrooks Davisis zero, the behavior is identical to a call to
1338269e767SBrooks Davis.Fn chmod .
1348269e767SBrooks Davis.Pp
1358269e767SBrooks DavisA mode is created from
1368269e767SBrooks Davis.Em or'd
1378269e767SBrooks Davispermission bit masks
1388269e767SBrooks Davisdefined in
1398269e767SBrooks Davis.In sys/stat.h :
1408269e767SBrooks Davis.Pp
1418269e767SBrooks Davis.Bd -literal -offset indent -compact
1428269e767SBrooks Davis#define S_IRWXU 0000700    /* RWX mask for owner */
1438269e767SBrooks Davis#define S_IRUSR 0000400    /* R for owner */
1448269e767SBrooks Davis#define S_IWUSR 0000200    /* W for owner */
1458269e767SBrooks Davis#define S_IXUSR 0000100    /* X for owner */
1468269e767SBrooks Davis
1478269e767SBrooks Davis#define S_IRWXG 0000070    /* RWX mask for group */
1488269e767SBrooks Davis#define S_IRGRP 0000040    /* R for group */
1498269e767SBrooks Davis#define S_IWGRP 0000020    /* W for group */
1508269e767SBrooks Davis#define S_IXGRP 0000010    /* X for group */
1518269e767SBrooks Davis
1528269e767SBrooks Davis#define S_IRWXO 0000007    /* RWX mask for other */
1538269e767SBrooks Davis#define S_IROTH 0000004    /* R for other */
1548269e767SBrooks Davis#define S_IWOTH 0000002    /* W for other */
1558269e767SBrooks Davis#define S_IXOTH 0000001    /* X for other */
1568269e767SBrooks Davis
1578269e767SBrooks Davis#define S_ISUID 0004000    /* set user id on execution */
1588269e767SBrooks Davis#define S_ISGID 0002000    /* set group id on execution */
1598269e767SBrooks Davis#define S_ISVTX 0001000    /* sticky bit */
1608269e767SBrooks Davis.Ed
1618269e767SBrooks Davis.Pp
1628269e767SBrooks DavisThe non-standard
1638269e767SBrooks Davis.Dv S_ISTXT
1648269e767SBrooks Davisis a synonym for
1658269e767SBrooks Davis.Dv S_ISVTX .
1668269e767SBrooks Davis.Pp
1678269e767SBrooks DavisThe
1688269e767SBrooks Davis.Fx
1698269e767SBrooks DavisVM system totally ignores the sticky bit
1708269e767SBrooks Davis.Pq Dv S_ISVTX
1718269e767SBrooks Davisfor executables.
1728269e767SBrooks DavisOn UFS-based file systems (FFS, LFS) the sticky
1738269e767SBrooks Davisbit may only be set upon directories.
1748269e767SBrooks Davis.Pp
1758269e767SBrooks DavisIf mode
1768269e767SBrooks Davis.Dv S_ISVTX
1778269e767SBrooks Davis(the `sticky bit') is set on a directory,
1788269e767SBrooks Davisan unprivileged user may not delete or rename
1798269e767SBrooks Davisfiles of other users in that directory.
1808269e767SBrooks DavisThe sticky bit may be
1818269e767SBrooks Davisset by any user on a directory which the user owns or has appropriate
1828269e767SBrooks Davispermissions.
1838269e767SBrooks DavisFor more details of the properties of the sticky bit, see
1848269e767SBrooks Davis.Xr sticky 7 .
1858269e767SBrooks Davis.Pp
1868269e767SBrooks DavisIf mode ISUID (set UID) is set on a directory,
1878269e767SBrooks Davisand the MNT_SUIDDIR option was used in the mount of the file system,
1888269e767SBrooks Davisthen the owner of any new files and sub-directories
1898269e767SBrooks Daviscreated within this directory are set
1908269e767SBrooks Davisto be the same as the owner of that directory.
1918269e767SBrooks DavisIf this function is enabled, new directories will inherit
1928269e767SBrooks Davisthe bit from their parents.
1938269e767SBrooks DavisExecute bits are removed from
1948269e767SBrooks Davisthe file, and it will not be given to root.
1958269e767SBrooks DavisThis behavior does not change the
1968269e767SBrooks Davisrequirements for the user to be allowed to write the file, but only the eventual
1978269e767SBrooks Davisowner after it has been created.
1988269e767SBrooks DavisGroup inheritance is not affected.
1998269e767SBrooks Davis.Pp
2008269e767SBrooks DavisThis feature is designed for use on fileservers serving PC users via
2018269e767SBrooks Davisftp, SAMBA, or netatalk.
2028269e767SBrooks DavisIt provides security holes for shell users and as
2038269e767SBrooks Davissuch should not be used on shell machines, especially on home directories.
2048269e767SBrooks DavisThis option requires the SUIDDIR
2058269e767SBrooks Davisoption in the kernel to work.
2068269e767SBrooks DavisOnly UFS file systems support this option.
2078269e767SBrooks DavisFor more details of the suiddir mount option, see
2088269e767SBrooks Davis.Xr mount 8 .
2098269e767SBrooks Davis.Pp
2108269e767SBrooks DavisWriting or changing the owner of a file
2118269e767SBrooks Davisturns off the set-user-id and set-group-id bits
2128269e767SBrooks Davisunless the user is the super-user.
2138269e767SBrooks DavisThis makes the system somewhat more secure
2148269e767SBrooks Davisby protecting set-user-id (set-group-id) files
2158269e767SBrooks Davisfrom remaining set-user-id (set-group-id) if they are modified,
2168269e767SBrooks Davisat the expense of a degree of compatibility.
217*bfd03046SMark Johnston.Pp
218*bfd03046SMark JohnstonWhile it is normally an error to invoke
219*bfd03046SMark Johnston.Fn fchmod
220*bfd03046SMark Johnstonon a socket, it is possible to do so on
221*bfd03046SMark Johnston.Dv AF_LOCAL
222*bfd03046SMark Johnstonsockets before they are bound to a file name; see
223*bfd03046SMark Johnston.Xr unix 4 .
2248269e767SBrooks Davis.Sh RETURN VALUES
2258269e767SBrooks Davis.Rv -std
2268269e767SBrooks Davis.Sh ERRORS
2278269e767SBrooks DavisThe
2288269e767SBrooks Davis.Fn chmod
2298269e767SBrooks Davissystem call
2308269e767SBrooks Daviswill fail and the file mode will be unchanged if:
2318269e767SBrooks Davis.Bl -tag -width Er
2328269e767SBrooks Davis.It Bq Er ENOTDIR
2338269e767SBrooks DavisA component of the path prefix is not a directory.
2348269e767SBrooks Davis.It Bq Er ENAMETOOLONG
2358269e767SBrooks DavisA component of a pathname exceeded 255 characters,
2368269e767SBrooks Davisor an entire path name exceeded 1023 characters.
2378269e767SBrooks Davis.It Bq Er ENOENT
2388269e767SBrooks DavisThe named file does not exist.
2398269e767SBrooks Davis.It Bq Er EACCES
2408269e767SBrooks DavisSearch permission is denied for a component of the path prefix.
2418269e767SBrooks Davis.It Bq Er ELOOP
2428269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname.
2438269e767SBrooks Davis.It Bq Er EPERM
2448269e767SBrooks DavisThe effective user ID does not match the owner of the file and
2458269e767SBrooks Davisthe effective user ID is not the super-user.
2468269e767SBrooks Davis.It Bq Er EPERM
2478269e767SBrooks DavisThe effective user ID is not the super-user, the effective user ID do match the
2488269e767SBrooks Davisowner of the file, but the group ID of the file does not match the effective
2498269e767SBrooks Davisgroup ID nor one of the supplementary group IDs.
2508269e767SBrooks Davis.It Bq Er EPERM
2518269e767SBrooks DavisThe named file has its immutable or append-only flag set, see the
2528269e767SBrooks Davis.Xr chflags 2
2538269e767SBrooks Davismanual page for more information.
2548269e767SBrooks Davis.It Bq Er EROFS
2558269e767SBrooks DavisThe named file resides on a read-only file system.
2568269e767SBrooks Davis.It Bq Er EFAULT
2578269e767SBrooks DavisThe
2588269e767SBrooks Davis.Fa path
2598269e767SBrooks Davisargument
2608269e767SBrooks Davispoints outside the process's allocated address space.
2618269e767SBrooks Davis.It Bq Er EIO
2628269e767SBrooks DavisAn I/O error occurred while reading from or writing to the file system.
2638269e767SBrooks Davis.It Bq Er EINTEGRITY
2648269e767SBrooks DavisCorrupted data was detected while reading from the file system.
2658269e767SBrooks Davis.It Bq Er EFTYPE
2668269e767SBrooks DavisThe effective user ID is not the super-user, the mode includes the sticky bit
2678269e767SBrooks Davis.Dv ( S_ISVTX ) ,
2688269e767SBrooks Davisand path does not refer to a directory.
2698269e767SBrooks Davis.El
2708269e767SBrooks Davis.Pp
2718269e767SBrooks DavisThe
2728269e767SBrooks Davis.Fn fchmod
2738269e767SBrooks Davissystem call will fail if:
2748269e767SBrooks Davis.Bl -tag -width Er
2758269e767SBrooks Davis.It Bq Er EBADF
2768269e767SBrooks DavisThe descriptor is not valid.
2778269e767SBrooks Davis.It Bq Er EINVAL
2788269e767SBrooks DavisThe
2798269e767SBrooks Davis.Fa fd
2808269e767SBrooks Davisargument
2818269e767SBrooks Davisrefers to a socket, not to a file.
2828269e767SBrooks Davis.It Bq Er EROFS
2838269e767SBrooks DavisThe file resides on a read-only file system.
2848269e767SBrooks Davis.It Bq Er EIO
2858269e767SBrooks DavisAn I/O error occurred while reading from or writing to the file system.
2868269e767SBrooks Davis.It Bq Er EINTEGRITY
2878269e767SBrooks DavisCorrupted data was detected while reading from the file system.
2888269e767SBrooks Davis.El
2898269e767SBrooks Davis.Pp
2908269e767SBrooks DavisIn addition to the
2918269e767SBrooks Davis.Fn chmod
2928269e767SBrooks Daviserrors,
2938269e767SBrooks Davis.Fn fchmodat
2948269e767SBrooks Davisfails if:
2958269e767SBrooks Davis.Bl -tag -width Er
2968269e767SBrooks Davis.It Bq Er EBADF
2978269e767SBrooks DavisThe
2988269e767SBrooks Davis.Fa path
2998269e767SBrooks Davisargument does not specify an absolute path and the
3008269e767SBrooks Davis.Fa fd
3018269e767SBrooks Davisargument is neither
3028269e767SBrooks Davis.Fa AT_FDCWD
3038269e767SBrooks Davisnor a valid file descriptor open for searching.
3048269e767SBrooks Davis.It Bq Er EINVAL
3058269e767SBrooks DavisThe value of the
3068269e767SBrooks Davis.Fa flag
3078269e767SBrooks Davisargument is not valid.
3088269e767SBrooks Davis.It Bq Er ENOTDIR
3098269e767SBrooks DavisThe
3108269e767SBrooks Davis.Fa path
3118269e767SBrooks Davisargument is not an absolute path and
3128269e767SBrooks Davis.Fa fd
3138269e767SBrooks Davisis neither
3148269e767SBrooks Davis.Dv AT_FDCWD
3158269e767SBrooks Davisnor a file descriptor associated with a directory.
3168269e767SBrooks Davis.It Bq Er ENOTCAPABLE
3178269e767SBrooks Davis.Fa path
3188269e767SBrooks Davisis an absolute path,
3198269e767SBrooks Davisor contained a ".." component leading to a
3208269e767SBrooks Davisdirectory outside of the directory hierarchy specified by
3218269e767SBrooks Davis.Fa fd ,
3228269e767SBrooks Davisand the process is in capability mode or the
3238269e767SBrooks Davis.Dv AT_RESOLVE_BENEATH
3248269e767SBrooks Davisflag was specified.
3258269e767SBrooks Davis.El
3268269e767SBrooks Davis.Sh SEE ALSO
3278269e767SBrooks Davis.Xr chmod 1 ,
3288269e767SBrooks Davis.Xr chflags 2 ,
3298269e767SBrooks Davis.Xr chown 2 ,
3308269e767SBrooks Davis.Xr open 2 ,
3318269e767SBrooks Davis.Xr stat 2 ,
3328269e767SBrooks Davis.Xr sticky 7
3338269e767SBrooks Davis.Sh STANDARDS
3348269e767SBrooks DavisThe
3358269e767SBrooks Davis.Fn chmod
3368269e767SBrooks Davissystem call is expected to conform to
3378269e767SBrooks Davis.St -p1003.1-90 ,
3388269e767SBrooks Davisexcept for the return of
3398269e767SBrooks Davis.Er EFTYPE .
3408269e767SBrooks DavisThe
3418269e767SBrooks Davis.Dv S_ISVTX
3428269e767SBrooks Davisbit on directories is expected to conform to
3438269e767SBrooks Davis.St -susv3 .
3448269e767SBrooks DavisThe
3458269e767SBrooks Davis.Fn fchmodat
3468269e767SBrooks Davissystem call is expected to conform to
3478269e767SBrooks Davis.St -p1003.1-2008 .
3488269e767SBrooks Davis.Sh HISTORY
3498269e767SBrooks DavisThe
3508269e767SBrooks Davis.Fn chmod
3518269e767SBrooks Davisfunction appeared in
3528269e767SBrooks Davis.At v1 .
3538269e767SBrooks DavisThe
3548269e767SBrooks Davis.Fn fchmod
3558269e767SBrooks Davissystem call appeared in
3568269e767SBrooks Davis.Bx 4.2 .
3578269e767SBrooks DavisThe
3588269e767SBrooks Davis.Fn lchmod
3598269e767SBrooks Davissystem call appeared in
3608269e767SBrooks Davis.Fx 3.0 .
3618269e767SBrooks DavisThe
3628269e767SBrooks Davis.Fn fchmodat
3638269e767SBrooks Davissystem call appeared in
3648269e767SBrooks Davis.Fx 8.0 .
365