man-pages/man2/access.2.html
2021-03-31 01:06:50 +01:00

601 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of ACCESS</TITLE>
</HEAD><BODY>
<H1>ACCESS</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2016-03-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
access, faccessat - check user's permissions for a file
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>int access(const char *</B><I>pathname</I><B>, int </B><I>mode</I><B>);</B>
<B>#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt; </B>/* Definition of AT_* constants */
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>int faccessat(int </B><I>dirfd</I><B>, const char *</B><I>pathname</I><B>, int </B><I>mode</I><B>, int </B><I>flags</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>faccessat</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.10:<DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
<DT id="3">Before glibc 2.10:<DD>
_ATFILE_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>access</B>()
checks whether the calling process can access the file
<I>pathname</I>.
If
<I>pathname</I>
is a symbolic link, it is dereferenced.
<P>
The
<I>mode</I>
specifies the accessibility check(s) to be performed,
and is either the value
<B>F_OK</B>,
or a mask consisting of the bitwise OR of one or more of
<B>R_OK</B>, <B>W_OK</B>, and <B>X_OK</B>.
<B>F_OK</B>
tests for the existence of the file.
<B>R_OK</B>, <B>W_OK</B>, and <B>X_OK</B>
test whether the file exists and grants read, write, and
execute permissions, respectively.
<P>
The check is done using the calling process's
<I>real</I>
UID and GID, rather than the effective IDs as is done when
actually attempting an operation (e.g.,
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
on the file.
Similarly, for the root user, the check uses the set of
permitted capabilities rather than the set of effective
capabilities; and for non-root users, the check uses an empty set
of capabilities.
<P>
This allows set-user-ID programs and capability-endowed programs
to easily determine the invoking user's authority.
In other words,
<B>access</B>()
does not answer the &quot;can I read/write/execute this file?&quot; question.
It answers a slightly different question:
&quot;(assuming I'm a setuid binary) can
<I>the user who invoked me</I>
read/write/execute this file?&quot;,
which gives set-user-ID programs the possibility to
prevent malicious users from causing them to read files
which users shouldn't be able to read.
<P>
If the calling process is privileged (i.e., its real UID is zero),
then an
<B>X_OK</B>
check is successful for a regular file if execute permission
is enabled for any of the file owner, group, or other.
<A NAME="lbAE">&nbsp;</A>
<H3>faccessat()</H3>
The
<B>faccessat</B>()
system call operates in exactly the same way as
<B>access</B>(),
except for the differences described here.
<P>
If the pathname given in
<I>pathname</I>
is relative, then it is interpreted relative to the directory
referred to by the file descriptor
<I>dirfd</I>
(rather than relative to the current working directory of
the calling process, as is done by
<B>access</B>()
for a relative pathname).
<P>
If
<I>pathname</I>
is relative and
<I>dirfd</I>
is the special value
<B>AT_FDCWD</B>,
then
<I>pathname</I>
is interpreted relative to the current working
directory of the calling process (like
<B>access</B>()).
<P>
If
<I>pathname</I>
is absolute, then
<I>dirfd</I>
is ignored.
<P>
<I>flags</I>
is constructed by ORing together zero or more of the following values:
<DL COMPACT>
<DT id="4"><B>AT_EACCESS</B>
<DD>
Perform access checks using the effective user and group IDs.
By default,
<B>faccessat</B>()
uses the real IDs (like
<B>access</B>()).
<DT id="5"><B>AT_SYMLINK_NOFOLLOW</B>
<DD>
If
<I>pathname</I>
is a symbolic link, do not dereference it:
instead return information about the link itself.
</DL>
<P>
See
<B><A HREF="/cgi-bin/man/man2html?2+openat">openat</A></B>(2)
for an explanation of the need for
<B>faccessat</B>().
<A NAME="lbAF">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success (all requested permissions granted, or
<I>mode</I>
is
<B>F_OK</B>
and the file exists), zero is returned.
On error (at least one bit in
<I>mode</I>
asked for a permission that is denied, or
<I>mode</I>
is
<B>F_OK</B>
and the file does not exist, or some other error occurred),
-1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAG">&nbsp;</A>
<H2>ERRORS</H2>
<B>access</B>()
and
<B>faccessat</B>()
shall fail if:
<DL COMPACT>
<DT id="6"><B>EACCES</B>
<DD>
The requested access would be denied to the file, or search permission
is denied for one of the directories in the path prefix of
<I>pathname</I>.
(See also
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
<DT id="7"><B>ELOOP</B>
<DD>
Too many symbolic links were encountered in resolving
<I>pathname</I>.
<DT id="8"><B>ENAMETOOLONG</B>
<DD>
<I>pathname</I>
is too long.
<DT id="9"><B>ENOENT</B>
<DD>
A component of
<I>pathname</I>
does not exist or is a dangling symbolic link.
<DT id="10"><B>ENOTDIR</B>
<DD>
A component used as a directory in
<I>pathname</I>
is not, in fact, a directory.
<DT id="11"><B>EROFS</B>
<DD>
Write permission was requested for a file on a read-only filesystem.
</DL>
<P>
<B>access</B>()
and
<B>faccessat</B>()
may fail if:
<DL COMPACT>
<DT id="12"><B>EFAULT</B>
<DD>
<I>pathname</I>
points outside your accessible address space.
<DT id="13"><B>EINVAL</B>
<DD>
<I>mode</I>
was incorrectly specified.
<DT id="14"><B>EIO</B>
<DD>
An I/O error occurred.
<DT id="15"><B>ENOMEM</B>
<DD>
Insufficient kernel memory was available.
<DT id="16"><B>ETXTBSY</B>
<DD>
Write access was requested to an executable which is being
executed.
</DL>
<P>
The following additional errors can occur for
<B>faccessat</B>():
<DL COMPACT>
<DT id="17"><B>EBADF</B>
<DD>
<I>dirfd</I>
is not a valid file descriptor.
<DT id="18"><B>EINVAL</B>
<DD>
Invalid flag specified in
<I>flags</I>.
<DT id="19"><B>ENOTDIR</B>
<DD>
<I>pathname</I>
is relative and
<I>dirfd</I>
is a file descriptor referring to a file other than a directory.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>VERSIONS</H2>
<B>faccessat</B>()
was added to Linux in kernel 2.6.16;
library support was added to glibc in version 2.4.
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>access</B>():
SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
<P>
<B>faccessat</B>():
POSIX.1-2008.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
<P>
<B>Warning</B>:
Using these calls to check if a user is authorized to, for example,
open a file before actually doing so using
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
creates a security hole, because the user might exploit the short time
interval between checking and opening the file to manipulate it.
<B>For this reason, the use of this system call should be avoided</B>.
(In the example just described,
a safer alternative would be to temporarily switch the process's
effective user ID to the real ID and then call
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).)
<P>
<B>access</B>()
always dereferences symbolic links.
If you need to check the permissions on a symbolic link, use
<B>faccessat</B>()
with the flag
<B>AT_SYMLINK_NOFOLLOW</B>.
<P>
These calls return an error if any of the access types in
<I>mode</I>
is denied, even if some of the other access types in
<I>mode</I>
are permitted.
<P>
If the calling process has appropriate privileges (i.e., is superuser),
POSIX.1-2001 permits an implementation to indicate success for an
<B>X_OK</B>
check even if none of the execute file permission bits are set.
Linux does not do this.
<P>
A file is accessible only if the permissions on each of the
directories in the path prefix of
<I>pathname</I>
grant search (i.e., execute) access.
If any directory is inaccessible, then the
<B>access</B>()
call fails, regardless of the permissions on the file itself.
<P>
Only access bits are checked, not the file type or contents.
Therefore, if a directory is found to be writable,
it probably means that files can be created in the directory,
and not that the directory can be written as a file.
Similarly, a DOS file may be found to be &quot;executable,&quot; but the
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
call will still fail.
<P>
These calls
may not work correctly on NFSv2 filesystems with UID mapping enabled,
because UID mapping is done on the server and hidden from the client,
which checks permissions. (NFS versions 3 and higher perform the check on
the server.)
Similar problems can occur to FUSE mounts.
<A NAME="lbAK">&nbsp;</A>
<H3>C library/kernel differences</H3>
The raw
<B>faccessat</B>()
system call takes only the first three arguments.
The
<B>AT_EACCESS</B>
and
<B>AT_SYMLINK_NOFOLLOW</B>
flags are actually implemented within the glibc wrapper function for
<B>faccessat</B>().
If either of these flags is specified, then the wrapper function employs
<B><A HREF="/cgi-bin/man/man2html?2+fstatat">fstatat</A></B>(2)
to determine access permissions.
<A NAME="lbAL">&nbsp;</A>
<H3>Glibc notes</H3>
On older kernels where
<B>faccessat</B>()
is unavailable (and when the
<B>AT_EACCESS</B>
and
<B>AT_SYMLINK_NOFOLLOW</B>
flags are not specified),
the glibc wrapper function falls back to the use of
<B>access</B>().
When
<I>pathname</I>
is a relative pathname,
glibc constructs a pathname based on the symbolic link in
<I>/proc/self/fd</I>
that corresponds to the
<I>dirfd</I>
argument.
<A NAME="lbAM">&nbsp;</A>
<H2>BUGS</H2>
In kernel 2.4 (and earlier) there is some strangeness in the handling of
<B>X_OK</B>
tests for superuser.
If all categories of execute permission are disabled
for a nondirectory file, then the only
<B>access</B>()
test that returns -1 is when
<I>mode</I>
is specified as just
<B>X_OK</B>;
if
<B>R_OK</B>
or
<B>W_OK</B>
is also specified in
<I>mode</I>,
then
<B>access</B>()
returns 0 for such files.
Early 2.6 kernels (up to and including 2.6.3)
also behaved in the same way as kernel 2.4.
<P>
In kernels before 2.6.20,
these calls ignored the effect of the
<B>MS_NOEXEC</B>
flag if it was used to
<B><A HREF="/cgi-bin/man/man2html?2+mount">mount</A></B>(2)
the underlying filesystem.
Since kernel 2.6.20, the
<B>MS_NOEXEC</B>
flag is honored.
<A NAME="lbAN">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+chmod">chmod</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+chown">chown</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setgid">setgid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setuid">setuid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+euidaccess">euidaccess</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+symlink">symlink</A></B>(7)
<A NAME="lbAO">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="20"><A HREF="#lbAB">NAME</A><DD>
<DT id="21"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="22"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="23"><A HREF="#lbAE">faccessat()</A><DD>
</DL>
<DT id="24"><A HREF="#lbAF">RETURN VALUE</A><DD>
<DT id="25"><A HREF="#lbAG">ERRORS</A><DD>
<DT id="26"><A HREF="#lbAH">VERSIONS</A><DD>
<DT id="27"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="28"><A HREF="#lbAJ">NOTES</A><DD>
<DL>
<DT id="29"><A HREF="#lbAK">C library/kernel differences</A><DD>
<DT id="30"><A HREF="#lbAL">Glibc notes</A><DD>
</DL>
<DT id="31"><A HREF="#lbAM">BUGS</A><DD>
<DT id="32"><A HREF="#lbAN">SEE ALSO</A><DD>
<DT id="33"><A HREF="#lbAO">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>