]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/access.2
access.2: tfix + wfix
[thirdparty/man-pages.git] / man2 / access.2
CommitLineData
fea681da 1.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
ac56b6a8 2.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
687dcec2 3.\" and Copyright (C) 2004, 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 4.\"
93015253 5.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 14.\"
fea681da
MK
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.
c13182ef 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 25.\" %%%LICENSE_END
fea681da
MK
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
02d79ece 41.\" 2007-06-10, mtk, various parts rewritten, and added BUGS section.
fea681da 42.\"
6d322d5f 43.TH ACCESS 2 2015-02-21 "Linux" "Linux Programmer's Manual"
fea681da 44.SH NAME
687dcec2 45access, faccessat \- check user's permissions for a file
fea681da
MK
46.SH SYNOPSIS
47.nf
48.B #include <unistd.h>
49.sp
50.BI "int access(const char *" pathname ", int " mode );
687dcec2 51
2626921f 52.BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
687dcec2
MK
53.B #include <unistd.h>
54.sp
55.BI "int faccessat(int " dirfd ", const char *" pathname ", int " \
56mode ", int " flags );
57.fi
58.sp
59.in -4n
60Feature 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
69Since glibc 2.10:
70_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
71.TP
72Before glibc 2.10:
73_ATFILE_SOURCE
74.RE
75.ad
76.PD
fea681da
MK
77.fi
78.SH DESCRIPTION
e511ffb6 79.BR access ()
02d79ece 80checks whether the calling process can access the file
fea681da
MK
81.IR pathname .
82If
83.I pathname
02d79ece 84is a symbolic link, it is dereferenced.
fea681da 85
02d79ece 86The
fea681da 87.I mode
02d79ece
MK
88specifies the accessibility check(s) to be performed,
89and is either the value
90.BR F_OK ,
60948d68 91.\" F_OK is defined as 0 on every system that I know of.
b0742c81 92or a mask consisting of the bitwise OR of one or more of
02d79ece 93.BR R_OK ", " W_OK ", and " X_OK .
fea681da 94.B F_OK
02d79ece
MK
95tests for the existence of the file.
96.BR R_OK ", " W_OK ", and " X_OK
97test whether the file exists and grants read, write, and
98execute permissions, respectively.
fea681da 99
02d79ece 100The check is done using the calling process's
fea681da 101.I real
02d79ece
MK
102UID and GID, rather than the effective IDs as is done when
103actually attempting an operation (e.g.,
104.BR open (2))
cae279c4
MK
105on the file.
106Similarly, for the root user, the check uses the set of
062d1cb3 107permitted capabilities rather than the set of effective
cae279c4 108capabilities; and for non-root users, the check uses an empty set
062d1cb3
DV
109of capabilities.
110
111This allows set-user-ID programs and capability-endowed programs
cae279c4
MK
112to easily determine the invoking user's authority.
113In other words,
062d1cb3
DV
114.BR access ()
115does not answer the "can I read/write/execute this file?" question.
cae279c4
MK
116It answers a slightly different question:
117"(assuming I'm a setuid binary) can
062d1cb3
DV
118.I the user who invoked me
119read/write/execute this file?",
cae279c4
MK
120which gives set-user-ID programs the possibility to
121prevent malicious users from causing them to read files
122which users shouldn't be able to read.
fea681da 123
02d79ece
MK
124If the calling process is privileged (i.e., its real UID is zero),
125then an
fea681da 126.B X_OK
02d79ece
MK
127check is successful for a regular file if execute permission
128is enabled for any of the file owner, group, or other.
e04fe041 129.SS faccessat()
687dcec2
MK
130The
131.BR faccessat ()
132system call operates in exactly the same way as
cadd38ba 133.BR access (),
687dcec2
MK
134except for the differences described here.
135
136If the pathname given in
137.I pathname
138is relative, then it is interpreted relative to the directory
139referred to by the file descriptor
140.I dirfd
141(rather than relative to the current working directory of
142the calling process, as is done by
9d3ea7a2 143.BR access ()
687dcec2
MK
144for a relative pathname).
145
146If
147.I pathname
148is relative and
149.I dirfd
150is the special value
151.BR AT_FDCWD ,
152then
153.I pathname
154is interpreted relative to the current working
155directory of the calling process (like
98d3114d 156.BR access ()).
687dcec2
MK
157
158If
159.I pathname
160is absolute, then
161.I dirfd
162is ignored.
163
164.I flags
165is constructed by ORing together zero or more of the following values:
166.TP
167.B AT_EACCESS
168Perform access checks using the effective user and group IDs.
169By default,
170.BR faccessat ()
171uses the real IDs (like
98d3114d 172.BR access ()).
687dcec2
MK
173.TP
174.B AT_SYMLINK_NOFOLLOW
175If
176.I pathname
177is a symbolic link, do not dereference it:
178instead return information about the link itself.
2907fa3e
MK
179.PP
180See
181.BR openat (2)
182for an explanation of the need for
183.BR faccessat ().
d282bb24 184.SH RETURN VALUE
72d34312
MK
185On success (all requested permissions granted, or
186.I mode
187is
188.B F_OK
189and the file exists), zero is returned.
fea681da
MK
190On error (at least one bit in
191.I mode
72d34312
MK
192asked for a permission that is denied, or
193.I mode
194is
195.B F_OK
196and the file does not exist, or some other error occurred),
fea681da
MK
197\-1 is returned, and
198.I errno
199is set appropriately.
200.SH ERRORS
e511ffb6 201.BR access ()
32e370f2
MK
202and
203.BR faccessat ()
fea681da
MK
204shall fail if:
205.TP
206.B EACCES
dfaae362 207The requested access would be denied to the file, or search permission
fea681da
MK
208is denied for one of the directories in the path prefix of
209.IR pathname .
210(See also
ad7cc990 211.BR path_resolution (7).)
fea681da
MK
212.TP
213.B ELOOP
214Too many symbolic links were encountered in resolving
215.IR pathname .
216.TP
217.B ENAMETOOLONG
218.I pathname
219is too long.
220.TP
221.B ENOENT
33e820cb 222A component of
fea681da 223.I pathname
33e820cb 224does not exist or is a dangling symbolic link.
fea681da
MK
225.TP
226.B ENOTDIR
227A component used as a directory in
228.I pathname
229is not, in fact, a directory.
230.TP
231.B EROFS
9ee4a2b6 232Write permission was requested for a file on a read-only filesystem.
fea681da 233.PP
e511ffb6 234.BR access ()
32e370f2
MK
235and
236.BR faccessat ()
fea681da
MK
237may fail if:
238.TP
239.B EFAULT
240.I pathname
241points outside your accessible address space.
242.TP
243.B EINVAL
244.I mode
245was incorrectly specified.
246.TP
247.B EIO
248An I/O error occurred.
249.TP
250.B ENOMEM
251Insufficient kernel memory was available.
252.TP
253.B ETXTBSY
254Write access was requested to an executable which is being
255executed.
687dcec2
MK
256.PP
257The following additional errors can occur for
258.BR faccessat ():
259.TP
260.B EBADF
261.I dirfd
262is not a valid file descriptor.
263.TP
264.B EINVAL
265Invalid flag specified in
266.IR flags .
267.TP
268.B ENOTDIR
269.I pathname
270is relative and
271.I dirfd
272is a file descriptor referring to a file other than a directory.
273.SH VERSIONS
274.BR faccessat ()
275was added to Linux in kernel 2.6.16;
276library support was added to glibc in version 2.4.
d282bb24 277.SH CONFORMING TO
9d3ea7a2 278.BR access ():
b9f4755d 279SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
687dcec2 280
cadd38ba 281.BR faccessat ():
687dcec2 282POSIX.1-2008.
b07cd0a9 283.SH NOTES
fea681da 284.PP
4df883b9 285.BR Warning :
32e370f2 286Using these calls to check if a user is authorized to, for example,
dfaae362 287open a file before actually doing so using
fea681da
MK
288.BR open (2)
289creates a security hole, because the user might exploit the short time
290interval between checking and opening the file to manipulate it.
02d79ece 291.BR "For this reason, the use of this system call should be avoided" .
47135643
MK
292(In the example just described,
293a safer alternative would be to temporarily switch the process's
294effective user ID to the real ID and then call
295.BR open (2).)
02d79ece 296.PP
27d47e71 297.BR access ()
05b92251
MK
298always dereferences symbolic links.
299If you need to check the permissions on a symbolic link, use
6fdbc779 300.BR faccessat (2)
05b92251
MK
301with the flag
302.BR AT_SYMLINK_NOFOLLOW .
303.PP
32e370f2 304These calls return an error if any of the access types in
02d79ece
MK
305.I mode
306is denied, even if some of the other access types in
307.I mode
308are permitted.
309.PP
310If the calling process has appropriate privileges (i.e., is superuser),
7a35b981 311POSIX.1-2001 permits an implementation to indicate success for an
02d79ece
MK
312.B X_OK
313check even if none of the execute file permission bits are set.
d8ccd692 314.\" HPU-UX 11 and Tru64 5.1 do this.
02d79ece
MK
315Linux does not do this.
316.PP
33a0ccb2 317A file is accessible only if the permissions on each of the
02d79ece
MK
318directories in the path prefix of
319.I pathname
320grant search (i.e., execute) access.
321If any directory is inaccessible, then the
322.BR access ()
323call will fail, regardless of the permissions on the file itself.
324.PP
325Only access bits are checked, not the file type or contents.
326Therefore, if a directory is found to be writable,
327it probably means that files can be created in the directory,
328and not that the directory can be written as a file.
329Similarly, a DOS file may be found to be "executable," but the
330.BR execve (2)
331call will still fail.
332.PP
32e370f2 333These calls
1a7d4eb7 334may not work correctly on NFSv2 filesystems with UID mapping enabled,
02d79ece 335because UID mapping is done on the server and hidden from the client,
1a7d4eb7
BF
336which checks permissions. (NFS versions 3 and higher perform the check on
337the server.)
e0a699c1 338Similar problems can occur to FUSE mounts.
92692952
MK
339.\"
340.\"
8f692313 341.SS C library/kernel ABI differences
2907fa3e
MK
342The raw
343.BR faccessat ()
344system call takes only the first three arguments.
687dcec2
MK
345The
346.B AT_EACCESS
347and
348.B AT_SYMLINK_NOFOLLOW
349flags are actually implemented within the glibc wrapper function for
350.BR faccessat ().
351If either of these flags is specified, then the wrapper function employs
352.BR fstatat (2)
353to determine access permissions.
cf81f364
MK
354.SS Glibc notes
355On older kernels where
356.BR faccessat ()
357is unavailable (and when the
358.B AT_EACCESS
359and
360.B AT_SYMLINK_NOFOLLOW
361flags are not specified),
362the glibc wrapper function falls back to the use of
363.BR access ().
364When
365.I pathname
366is a relative pathname,
367glibc constructs a pathname based on the symbolic link in
368.IR /proc/self/fd
369that corresponds to the
370.IR dirfd
371argument.
02d79ece
MK
372.SH BUGS
373In kernel 2.4 (and earlier) there is some strangeness in the handling of
374.B X_OK
375tests for superuser.
376If all categories of execute permission are disabled
24b74457 377for a nondirectory file, then the only
02d79ece
MK
378.BR access ()
379test that returns \-1 is when
380.I mode
381is specified as just
382.BR X_OK ;
383if
384.B R_OK
385or
386.B W_OK
387is also specified in
388.IR mode ,
389then
390.BR access ()
391returns 0 for such files.
392.\" This behavior appears to have been an implementation accident.
393Early 2.6 kernels (up to and including 2.6.3)
394also behaved in the same way as kernel 2.4.
395
a1d5f77c 396In kernels before 2.6.20,
32e370f2 397these calls ignored the effect of the
a1d5f77c
MK
398.B MS_NOEXEC
399flag if it was used to
400.BR mount (2)
9ee4a2b6 401the underlying filesystem.
32e370f2
MK
402Since kernel 2.6.20, the
403.B MS_NOEXEC
8d0418af 404flag is honored.
d282bb24 405.SH SEE ALSO
fea681da
MK
406.BR chmod (2),
407.BR chown (2),
408.BR open (2),
fea681da
MK
409.BR setgid (2),
410.BR setuid (2),
d975050c 411.BR stat (2),
70d1cb2d 412.BR euidaccess (3),
53a1443c 413.BR credentials (7),
687dcec2
MK
414.BR path_resolution (7),
415.BR symlink (7)