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

256 lines
6.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SETUID</TITLE>
</HEAD><BODY>
<H1>SETUID</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<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>
setuid - set user identity
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<P>
<B>int setuid(uid_t </B><I>uid</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>setuid</B>()
sets the effective user ID of the calling process.
If the calling process is privileged
(more precisely: if the process has the
<B>CAP_SETUID</B>
capability in its user namespace),
the real UID and saved set-user-ID are also set.
<P>
Under Linux,
<B>setuid</B>()
is implemented like the POSIX version with the
<B>_POSIX_SAVED_IDS</B>
feature.
This allows a set-user-ID (other than root) program to drop all of its user
privileges, do some un-privileged work, and then reengage the original
effective user ID in a secure manner.
<P>
If the user is root or the program is set-user-ID-root, special care must be
taken:
<B>setuid</B>()
checks the effective user ID of the caller and if it is
the superuser, all process-related user ID's are set to
<I>uid</I>.
After this has occurred, it is impossible for the program to regain root
privileges.
<P>
Thus, a set-user-ID-root program wishing to temporarily drop root
privileges, assume the identity of an unprivileged user, and then regain
root privileges afterward cannot use
<B>setuid</B>().
You can accomplish this with
<B><A HREF="/cgi-bin/man/man2html?2+seteuid">seteuid</A></B>(2).
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<P>
<I>Note</I>:
there are cases where
<B>setuid</B>()
can fail even when the caller is UID 0;
it is a grave security error to omit checking for a failure return from
<B>setuid</B>().
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EAGAIN</B>
<DD>
The call would change the caller's real UID (i.e.,
<I>uid</I>
does not match the caller's real UID),
but there was a temporary failure allocating the
necessary kernel data structures.
<DT id="2"><B>EAGAIN</B>
<DD>
<I>uid</I>
does not match the real user ID of the caller and this call would
bring the number of processes belonging to the real user ID
<I>uid</I>
over the caller's
<B>RLIMIT_NPROC</B>
resource limit.
Since Linux 3.1, this error case no longer occurs
(but robust applications should check for this error);
see the description of
<B>EAGAIN</B>
in
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<DT id="3"><B>EINVAL</B>
<DD>
The user ID specified in
<I>uid</I>
is not valid in this user namespace.
<DT id="4"><B>EPERM</B>
<DD>
The user is not privileged (Linux: does not have the
<B>CAP_SETUID</B>
capability in its user namespace) and
<I>uid</I>
does not match the real UID or saved set-user-ID of the calling process.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, SVr4.
Not quite compatible with the 4.4BSD call, which
sets all of the real, saved, and effective user IDs.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
Linux has the concept of the filesystem user ID, normally equal to the
effective user ID.
The
<B>setuid</B>()
call also sets the filesystem user ID of the calling process.
See
<B><A HREF="/cgi-bin/man/man2html?2+setfsuid">setfsuid</A></B>(2).
<P>
If
<I>uid</I>
is different from the old effective UID, the process will
be forbidden from leaving core dumps.
<P>
The original Linux
<B>setuid</B>()
system call supported only 16-bit user IDs.
Subsequently, Linux 2.4 added
<B>setuid32</B>()
supporting 32-bit IDs.
The glibc
<B>setuid</B>()
wrapper function transparently deals with the variation across kernel versions.
<A NAME="lbAI">&nbsp;</A>
<H3>C library/kernel differences</H3>
At the kernel level, user IDs and group IDs are a per-thread attribute.
However, POSIX requires that all threads in a process
share the same credentials.
The NPTL threading implementation handles the POSIX requirements by
providing wrapper functions for
the various system calls that change process UIDs and GIDs.
These wrapper functions (including the one for
<B>setuid</B>())
employ a signal-based technique to ensure
that when one thread changes credentials,
all of the other threads in the process also change their credentials.
For details, see
<B><A HREF="/cgi-bin/man/man2html?7+nptl">nptl</A></B>(7).
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+getuid">getuid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+seteuid">seteuid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setfsuid">setfsuid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setreuid">setreuid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+user_namespaces">user_namespaces</A></B>(7)
<A NAME="lbAK">&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="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="11"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="12"><A HREF="#lbAI">C library/kernel differences</A><DD>
</DL>
<DT id="13"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="14"><A HREF="#lbAK">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:34 GMT, March 31, 2021
</BODY>
</HTML>