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