]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/access.2
access.2: Post-merge reworking of text
[thirdparty/man-pages.git] / man2 / access.2
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2004, 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified 1993-07-21 Rik Faith (faith@cs.unc.edu)
28 .\" Modified 1994-08-21 by Michael Chastain (mec@shell.portal.com):
29 .\" Removed note about old kernel (pre-1.1.44) using wrong id on path.
30 .\" Modified 1996-03-18 by Martin Schulze (joey@infodrom.north.de):
31 .\" Stated more clearly how it behaves with symbolic links.
32 .\" Added correction due to Nick Duffek (nsd@bbc.com), aeb, 960426
33 .\" Modified 1996-09-07 by Michael Haardt:
34 .\" Restrictions for NFS
35 .\" Modified 1997-09-09 by Joseph S. Myers <jsm28@cam.ac.uk>
36 .\" Modified 1998-01-13 by Michael Haardt:
37 .\" Using access is often insecure
38 .\" Modified 2001-10-16 by aeb
39 .\" Modified 2002-04-23 by Roger Luethi <rl@hellgate.ch>
40 .\" Modified 2004-06-23 by Michael Kerrisk
41 .\" 2007-06-10, mtk, various parts rewritten, and added BUGS section.
42 .\"
43 .TH ACCESS 2 2014-02-21 "Linux" "Linux Programmer's Manual"
44 .SH NAME
45 access, faccessat \- check user's permissions for a file
46 .SH SYNOPSIS
47 .nf
48 .B #include <unistd.h>
49 .sp
50 .BI "int access(const char *" pathname ", int " mode );
51
52 .B #include <fcntl.h> /* Definition of AT_* constants */
53 .B #include <unistd.h>
54 .sp
55 .BI "int faccessat(int " dirfd ", const char *" pathname ", int " \
56 mode ", int " flags );
57 .fi
58 .sp
59 .in -4n
60 Feature Test Macro Requirements for glibc (see
61 .BR feature_test_macros (7)):
62 .in
63 .sp
64 .BR faccessat ():
65 .PD 0
66 .ad l
67 .RS 4
68 .TP 4
69 Since glibc 2.10:
70 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
71 .TP
72 Before glibc 2.10:
73 _ATFILE_SOURCE
74 .RE
75 .ad
76 .PD
77 .fi
78 .SH DESCRIPTION
79 .BR access ()
80 checks whether the calling process can access the file
81 .IR pathname .
82 If
83 .I pathname
84 is a symbolic link, it is dereferenced.
85
86 The
87 .I mode
88 specifies the accessibility check(s) to be performed,
89 and is either the value
90 .BR F_OK ,
91 .\" F_OK is defined as 0 on every system that I know of.
92 or a mask consisting of the bitwise OR of one or more of
93 .BR R_OK ", " W_OK ", and " X_OK .
94 .B F_OK
95 tests for the existence of the file.
96 .BR R_OK ", " W_OK ", and " X_OK
97 test whether the file exists and grants read, write, and
98 execute permissions, respectively.
99
100 The check is done using the calling process's
101 .I real
102 UID and GID, rather than the effective IDs as is done when
103 actually attempting an operation (e.g.,
104 .BR open (2))
105 on the file.
106 This allows set-user-ID programs to
107 easily determine the invoking user's authority.
108
109 If the calling process is privileged (i.e., its real UID is zero),
110 then an
111 .B X_OK
112 check is successful for a regular file if execute permission
113 is enabled for any of the file owner, group, or other.
114 .SS faccessat ()
115 The
116 .BR faccessat ()
117 system call operates in exactly the same way as
118 .BR access (2),
119 except for the differences described here.
120
121 If the pathname given in
122 .I pathname
123 is relative, then it is interpreted relative to the directory
124 referred to by the file descriptor
125 .I dirfd
126 (rather than relative to the current working directory of
127 the calling process, as is done by
128 .BR access (2)
129 for a relative pathname).
130
131 If
132 .I pathname
133 is relative and
134 .I dirfd
135 is the special value
136 .BR AT_FDCWD ,
137 then
138 .I pathname
139 is interpreted relative to the current working
140 directory of the calling process (like
141 .BR access (2)).
142
143 If
144 .I pathname
145 is absolute, then
146 .I dirfd
147 is ignored.
148
149 .I flags
150 is constructed by ORing together zero or more of the following values:
151 .TP
152 .B AT_EACCESS
153 Perform access checks using the effective user and group IDs.
154 By default,
155 .BR faccessat ()
156 uses the real IDs (like
157 .BR access (2)).
158 .TP
159 .B AT_SYMLINK_NOFOLLOW
160 If
161 .I pathname
162 is a symbolic link, do not dereference it:
163 instead return information about the link itself.
164 .SH "RETURN VALUE"
165 On success (all requested permissions granted, or
166 .I mode
167 is
168 .B F_OK
169 and the file exists), zero is returned.
170 On error (at least one bit in
171 .I mode
172 asked for a permission that is denied, or
173 .I mode
174 is
175 .B F_OK
176 and the file does not exist, or some other error occurred),
177 \-1 is returned, and
178 .I errno
179 is set appropriately.
180 .SH ERRORS
181 .BR access ()
182 and
183 .BR faccessat ()
184 shall fail if:
185 .TP
186 .B EACCES
187 The requested access would be denied to the file, or search permission
188 is denied for one of the directories in the path prefix of
189 .IR pathname .
190 (See also
191 .BR path_resolution (7).)
192 .TP
193 .B ELOOP
194 Too many symbolic links were encountered in resolving
195 .IR pathname .
196 .TP
197 .B ENAMETOOLONG
198 .I pathname
199 is too long.
200 .TP
201 .B ENOENT
202 A component of
203 .I pathname
204 does not exist or is a dangling symbolic link.
205 .TP
206 .B ENOTDIR
207 A component used as a directory in
208 .I pathname
209 is not, in fact, a directory.
210 .TP
211 .B EROFS
212 Write permission was requested for a file on a read-only filesystem.
213 .PP
214 .BR access ()
215 and
216 .BR faccessat ()
217 may fail if:
218 .TP
219 .B EFAULT
220 .I pathname
221 points outside your accessible address space.
222 .TP
223 .B EINVAL
224 .I mode
225 was incorrectly specified.
226 .TP
227 .B EIO
228 An I/O error occurred.
229 .TP
230 .B ENOMEM
231 Insufficient kernel memory was available.
232 .TP
233 .B ETXTBSY
234 Write access was requested to an executable which is being
235 executed.
236 .PP
237 The following additional errors can occur for
238 .BR faccessat ():
239 .TP
240 .B EBADF
241 .I dirfd
242 is not a valid file descriptor.
243 .TP
244 .B EINVAL
245 Invalid flag specified in
246 .IR flags .
247 .TP
248 .B ENOTDIR
249 .I pathname
250 is relative and
251 .I dirfd
252 is a file descriptor referring to a file other than a directory.
253 .SH VERSIONS
254 .BR faccessat ()
255 was added to Linux in kernel 2.6.16;
256 library support was added to glibc in version 2.4.
257 .SH "CONFORMING TO"
258 .BR access (2):
259 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
260
261 .BR faccessat (2):
262 POSIX.1-2008.
263 .SH NOTES
264 .PP
265 .BR Warning :
266 Using these calls to check if a user is authorized to, for example,
267 open a file before actually doing so using
268 .BR open (2)
269 creates a security hole, because the user might exploit the short time
270 interval between checking and opening the file to manipulate it.
271 .BR "For this reason, the use of this system call should be avoided" .
272 (In the example just described,
273 a safer alternative would be to temporarily switch the process's
274 effective user ID to the real ID and then call
275 .BR open (2).)
276 .PP
277 .BR access ()
278 always dereferences symbolic links.
279 If you need to check the permissions on a symbolic link, use
280 .BR faccessat (2)
281 with the flag
282 .BR AT_SYMLINK_NOFOLLOW .
283 .PP
284 These calls return an error if any of the access types in
285 .I mode
286 is denied, even if some of the other access types in
287 .I mode
288 are permitted.
289 .PP
290 If the calling process has appropriate privileges (i.e., is superuser),
291 POSIX.1-2001 permits an implementation to indicate success for an
292 .B X_OK
293 check even if none of the execute file permission bits are set.
294 .\" HPU-UX 11 and Tru64 5.1 do this.
295 Linux does not do this.
296 .PP
297 A file is accessible only if the permissions on each of the
298 directories in the path prefix of
299 .I pathname
300 grant search (i.e., execute) access.
301 If any directory is inaccessible, then the
302 .BR access ()
303 call will fail, regardless of the permissions on the file itself.
304 .PP
305 Only access bits are checked, not the file type or contents.
306 Therefore, if a directory is found to be writable,
307 it probably means that files can be created in the directory,
308 and not that the directory can be written as a file.
309 Similarly, a DOS file may be found to be "executable," but the
310 .BR execve (2)
311 call will still fail.
312 .PP
313 These calls
314 may not work correctly on NFSv2 filesystems with UID mapping enabled,
315 because UID mapping is done on the server and hidden from the client,
316 which checks permissions. (NFS versions 3 and higher perform the check on
317 the server.)
318 Similar problems can occur to FUSE mounts.
319 .\"
320 .\"
321 .SS faccessat ()
322 See
323 .BR openat (2)
324 for an explanation of the need for
325 .BR faccessat ().
326
327 .IR Warning :
328 .BR faccessat ()
329 is subject to the same kinds of races as
330 .BR access (2)
331 and
332 .BR euidaccess (3).
333 .SS Glibc notes
334 The raw system call takes only the first three arguments.
335 The
336 .B AT_EACCESS
337 and
338 .B AT_SYMLINK_NOFOLLOW
339 flags are actually implemented within the glibc wrapper function for
340 .BR faccessat ().
341 If either of these flags is specified, then the wrapper function employs
342 .BR fstatat (2)
343 to determine access permissions.
344 .SH BUGS
345 In kernel 2.4 (and earlier) there is some strangeness in the handling of
346 .B X_OK
347 tests for superuser.
348 If all categories of execute permission are disabled
349 for a nondirectory file, then the only
350 .BR access ()
351 test that returns \-1 is when
352 .I mode
353 is specified as just
354 .BR X_OK ;
355 if
356 .B R_OK
357 or
358 .B W_OK
359 is also specified in
360 .IR mode ,
361 then
362 .BR access ()
363 returns 0 for such files.
364 .\" This behavior appears to have been an implementation accident.
365 Early 2.6 kernels (up to and including 2.6.3)
366 also behaved in the same way as kernel 2.4.
367
368 In kernels before 2.6.20,
369 these calls ignored the effect of the
370 .B MS_NOEXEC
371 flag if it was used to
372 .BR mount (2)
373 the underlying filesystem.
374 Since kernel 2.6.20, the
375 .B MS_NOEXEC
376 is honored
377 .SH "SEE ALSO"
378 .BR chmod (2),
379 .BR chown (2),
380 .BR open (2),
381 .BR setgid (2),
382 .BR setuid (2),
383 .BR stat (2),
384 .BR euidaccess (3),
385 .BR credentials (7),
386 .BR path_resolution (7),
387 .BR symlink (7)