1CHOWN(3P)                  POSIX Programmer's Manual                 CHOWN(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       chown, fchownat — change owner and group of a file relative  to  direc‐
14       tory file descriptor
15

SYNOPSIS

17       #include <unistd.h>
18
19       int chown(const char *path, uid_t owner, gid_t group);
20       int fchownat(int fd, const char *path, uid_t owner, gid_t group,
21           int flag);
22

DESCRIPTION

24       The  chown()  function  shall  change the user and group ownership of a
25       file.
26
27       The path argument points to a pathname naming a file. The user  ID  and
28       group ID of the named file shall be set to the numeric values contained
29       in owner and group, respectively.
30
31       Only processes with an effective user ID equal to the user  ID  of  the
32       file or with appropriate privileges may change the ownership of a file.
33       If _POSIX_CHOWN_RESTRICTED is in effect for path:
34
35        *  Changing the user ID is restricted to  processes  with  appropriate
36           privileges.
37
38        *  Changing  the  group ID is permitted to a process with an effective
39           user ID equal to the user ID of the file, but  without  appropriate
40           privileges,  if and only if owner is equal to the file's user ID or
41           (uid_t)−1 and group is equal either to the calling process'  effec‐
42           tive group ID or to one of its supplementary group IDs.
43
44       If  the  specified  file is a regular file, one or more of the S_IXUSR,
45       S_IXGRP, or S_IXOTH bits of the file mode are set, and the process does
46       not  have  appropriate  privileges,  the set-user-ID (S_ISUID) and set-
47       group-ID (S_ISGID) bits of the file mode shall be cleared upon success‐
48       ful  return from chown().  If the specified file is a regular file, one
49       or more of the S_IXUSR, S_IXGRP, or S_IXOTH bits of the file  mode  are
50       set,  and the process has appropriate privileges, it is implementation-
51       defined whether the set-user-ID and set-group-ID bits are  altered.  If
52       the  chown()  function  is successfully invoked on a file that is not a
53       regular file and one or more of the S_IXUSR, S_IXGRP, or  S_IXOTH  bits
54       of  the file mode are set, the set-user-ID and set-group-ID bits may be
55       cleared.
56
57       If owner or group is specified as (uid_t)−1 or (gid_t)−1, respectively,
58       the  corresponding  ID of the file shall not be changed.  If both owner
59       and group are −1, the times need not be updated.
60
61       Upon successful completion, chown() shall mark for update the last file
62       status change timestamp of the file.
63
64       The fchownat() function shall be equivalent to the chown() and lchown()
65       functions except in the case where path specifies a relative  path.  In
66       this  case  the file to be changed is determined relative to the direc‐
67       tory associated with the file descriptor  fd  instead  of  the  current
68       working  directory. If the file descriptor was opened without O_SEARCH,
69       the function shall check whether directory searches are permitted using
70       the  current  permissions of the directory underlying the file descrip‐
71       tor. If the file descriptor was  opened  with  O_SEARCH,  the  function
72       shall not perform the check.
73
74       Values for flag are constructed by a bitwise-inclusive OR of flags from
75       the following list, defined in <fcntl.h>:
76
77       AT_SYMLINK_NOFOLLOW
78             If path names a symbolic link, ownership of the symbolic link  is
79             changed.
80
81       If fchownat() is passed the special value AT_FDCWD in the fd parameter,
82       the current working directory shall be used and the behavior  shall  be
83       identical  to  a call to chown() or lchown() respectively, depending on
84       whether or not the AT_SYMLINK_NOFOLLOW bit is set in the flag argument.
85

RETURN VALUE

87       Upon successful completion, these functions shall return 0.  Otherwise,
88       these functions shall return −1 and set errno to indicate the error. If
89       −1 is returned, no changes are made in the user ID and group ID of  the
90       file.
91

ERRORS

93       These functions shall fail if:
94
95       EACCES Search permission is denied on a component of the path prefix.
96
97       ELOOP  A loop exists in symbolic links encountered during resolution of
98              the path argument.
99
100       ENAMETOOLONG
101              The  length  of  a  component  of  a  pathname  is  longer  than
102              {NAME_MAX}.
103
104       ENOENT A component of path does not name an existing file or path is an
105              empty string.
106
107       ENOTDIR
108              A component of the path prefix names an existing  file  that  is
109              neither  a  directory nor a symbolic link to a directory, or the
110              path argument contains at least one  non-<slash>  character  and
111              ends  with  one or more trailing <slash> characters and the last
112              pathname component names an existing  file  that  is  neither  a
113              directory nor a symbolic link to a directory.
114
115       EPERM  The  effective  user ID does not match the owner of the file, or
116              the calling process does not  have  appropriate  privileges  and
117              _POSIX_CHOWN_RESTRICTED   indicates   that   such  privilege  is
118              required.
119
120       EROFS  The named file resides on a read-only file system.
121
122       The fchownat() function shall fail if:
123
124       EACCES fd was not opened with  O_SEARCH  and  the  permissions  of  the
125              directory underlying fd do not permit directory searches.
126
127       EBADF  The  path  argument does not specify an absolute path and the fd
128              argument is neither AT_FDCWD nor a valid  file  descriptor  open
129              for reading or searching.
130
131       ENOTDIR
132              The  path  argument  is  not  an  absolute path and fd is a file
133              descriptor associated with a non-directory file.
134
135       These functions may fail if:
136
137       EIO    An I/O error occurred while reading or writing to the file  sys‐
138              tem.
139
140       EINTR  The  chown()  function  was  interrupted  by  a signal which was
141              caught.
142
143       EINVAL The owner or group ID supplied is not a value supported  by  the
144              implementation.
145
146       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
147              resolution of the path argument.
148
149       ENAMETOOLONG
150              The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
151              tion  of  a symbolic link produced an intermediate result with a
152              length that exceeds {PATH_MAX}.
153
154       The fchownat() function may fail if:
155
156       EINVAL The value of the flag argument is not valid.
157
158       The following sections are informative.
159

EXAMPLES

161       None.
162

APPLICATION USAGE

164       Although chown() can be used on some implementations by the file  owner
165       to  change the owner and group to any desired values, the only portable
166       use of this function is to change the group of a file to the  effective
167       GID of the calling process or to a member of its group set.
168

RATIONALE

170       System  III  and System V allow a user to give away files; that is, the
171       owner of a file may change its user ID to anything. This is  a  serious
172       problem  for implementations that are intended to meet government secu‐
173       rity regulations.  Version 7 and 4.3 BSD permit only the  superuser  to
174       change  the  user  ID  of a file. Some government agencies (usually not
175       ones concerned directly with security) find this limitation too confin‐
176       ing.  This volume of POSIX.1‐2008 uses may to permit secure implementa‐
177       tions while not disallowing System V.
178
179       System III and System V allow the owner of a file to change  the  group
180       ID  to  anything.  Version  7  permits only the superuser to change the
181       group ID of a file.  4.3 BSD permits the owner to change the  group  ID
182       of a file to its effective group ID or to any of the groups in the list
183       of supplementary group IDs, but to no others.
184
185       The POSIX.1‐1990 standard requires that the chown() function invoked by
186       a  non-appropriate privileged process clear the S_ISGID and the S_ISUID
187       bits for regular files, and permits them to be cleared for other  types
188       of  files. This is so that changes in accessibility do not accidentally
189       cause files to become security holes.  Unfortunately,  requiring  these
190       bits  to be cleared on non-executable data files also clears the manda‐
191       tory file locking bit (shared with S_ISGID), which is an  extension  on
192       many implementations (it first appeared in System V). These bits should
193       only be required to be cleared on regular files that have one  or  more
194       of their execute bits set.
195
196       The  purpose of the fchownat() function is to enable changing ownership
197       of files in directories other than the current working directory  with‐
198       out  exposure  to race conditions. Any part of the path of a file could
199       be changed in parallel to a call to chown() or lchown(),  resulting  in
200       unspecified  behavior.  By  opening  a  file  descriptor for the target
201       directory and using the fchownat() function it can be  guaranteed  that
202       the changed file is located relative to the desired directory.
203

FUTURE DIRECTIONS

205       None.
206

SEE ALSO

208       chmod(), fpathconf(), lchown()
209
210       The  Base Definitions volume of POSIX.1‐2008, <fcntl.h>, <sys_types.h>,
211       <unistd.h>
212
214       Portions of this text are reprinted and reproduced in  electronic  form
215       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
216       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
217       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
218       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
219       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
220       event of any discrepancy between this version and the original IEEE and
221       The  Open Group Standard, the original IEEE and The Open Group Standard
222       is the referee document. The original Standard can be  obtained  online
223       at http://www.unix.org/online.html .
224
225       Any  typographical  or  formatting  errors that appear in this page are
226       most likely to have been introduced during the conversion of the source
227       files  to  man page format. To report such errors, see https://www.ker
228       nel.org/doc/man-pages/reporting_bugs.html .
229
230
231
232IEEE/The Open Group                  2013                            CHOWN(3P)
Impressum