fanotify_mark.2: ERRORS: add missing EBADF error for invalid 'dirfd'
[man-pages.git] / man2 / setfsuid.2
blob1fec339be2029bf0c554065731187a113d38af92
1 .\" Copyright (C) 1995, Thomas K. Dyas <tdyas@eden.rutgers.edu>
2 .\" and Copyright (C) 2013, 2019, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Created   1995-08-06 Thomas K. Dyas <tdyas@eden.rutgers.edu>
27 .\" Modified  2000-07-01 aeb
28 .\" Modified  2002-07-23 aeb
29 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\"     Added notes on capability requirements
31 .\"
32 .TH SETFSUID 2 2021-03-22 "Linux" "Linux Programmer's Manual"
33 .SH NAME
34 setfsuid \- set user identity used for filesystem checks
35 .SH SYNOPSIS
36 .nf
37 .B #include <sys/fsuid.h>
38 .PP
39 .BI "int setfsuid(uid_t " fsuid );
40 .fi
41 .SH DESCRIPTION
42 On Linux, a process has both a filesystem user ID and an effective user ID.
43 The (Linux-specific) filesystem user ID is used
44 for permissions checking when accessing filesystem objects,
45 while the effective user ID is used for various other kinds
46 of permissions checks (see
47 .BR credentials (7)).
48 .PP
49 Normally, the value of the process's filesystem user ID
50 is the same as the value of its effective user ID.
51 This is so, because whenever a process's effective user ID is changed,
52 the kernel also changes the filesystem user ID to be the same as
53 the new value of the effective user ID.
54 A process can cause the value of its filesystem user ID to diverge
55 from its effective user ID by using
56 .BR setfsuid ()
57 to change its filesystem user ID to the value given in
58 .IR fsuid .
59 .PP
60 Explicit calls to
61 .BR setfsuid ()
62 and
63 .BR setfsgid (2)
64 are (were) usually used only by programs such as the Linux NFS server that
65 need to change what user and group ID is used for file access without a
66 corresponding change in the real and effective user and group IDs.
67 A change in the normal user IDs for a program such as the NFS server
68 is (was) a security hole that can expose it to unwanted signals.
69 (However, this issue is historical; see below.)
70 .PP
71 .BR setfsuid ()
72 will succeed only if the caller is the superuser or if
73 .I fsuid
74 matches either the caller's real user ID, effective user ID,
75 saved set-user-ID, or current filesystem user ID.
76 .SH RETURN VALUE
77 On both success and failure,
78 this call returns the previous filesystem user ID of the caller.
79 .SH VERSIONS
80 This system call is present in Linux since version 1.2.
81 .\" This system call is present since Linux 1.1.44
82 .\" and in libc since libc 4.7.6.
83 .SH CONFORMING TO
84 .BR setfsuid ()
85 is Linux-specific and should not be used in programs intended
86 to be portable.
87 .SH NOTES
88 At the time when this system call was introduced, one process
89 could send a signal to another process with the same effective user ID.
90 This meant that if a privileged process changed its effective user ID
91 for the purpose of file permission checking,
92 then it could become vulnerable to receiving signals
93 sent by another (unprivileged) process with the same user ID.
94 The filesystem user ID attribute was thus added to allow a process to
95 change its user ID for the purposes of file permission checking without
96 at the same time becoming vulnerable to receiving unwanted signals.
97 Since Linux 2.0, signal permission handling is different (see
98 .BR kill (2)),
99 with the result that a process can change its effective user ID
100 without being vulnerable to receiving signals from unwanted processes.
101 Thus,
102 .BR setfsuid ()
103 is nowadays unneeded and should be avoided in new applications
104 (likewise for
105 .BR setfsgid (2)).
107 The original Linux
108 .BR setfsuid ()
109 system call supported only 16-bit user IDs.
110 Subsequently, Linux 2.4 added
111 .BR setfsuid32 ()
112 supporting 32-bit IDs.
113 The glibc
114 .BR setfsuid ()
115 wrapper function transparently deals with the variation across kernel versions.
116 .SS C library/kernel differences
117 In glibc 2.15 and earlier,
118 when the wrapper for this system call determines that the argument can't be
119 passed to the kernel without integer truncation (because the kernel
120 is old and does not support 32-bit user IDs),
121 it will return \-1 and set \fIerrno\fP to
122 .B EINVAL
123 without attempting
124 the system call.
125 .SH BUGS
126 No error indications of any kind are returned to the caller,
127 and the fact that both successful and unsuccessful calls return
128 the same value makes it impossible to directly determine
129 whether the call succeeded or failed.
130 Instead, the caller must resort to looking at the return value
131 from a further call such as
132 .IR setfsuid(\-1)
133 (which will always fail), in order to determine if a preceding call to
134 .BR setfsuid ()
135 changed the filesystem user ID.
136 At the very
137 least,
138 .B EPERM
139 should be returned when the call fails (because the caller lacks the
140 .B CAP_SETUID
141 capability).
142 .SH SEE ALSO
143 .BR kill (2),
144 .BR setfsgid (2),
145 .BR capabilities (7),
146 .BR credentials (7)