xref: /freebsd/lib/libsys/setcred.2 (revision b6f4027ad9a2ede69a7ec11137cc4ea69ec2f0a0)
1*b6f4027aSOlivier Certner.\"
2*b6f4027aSOlivier Certner.\" SPDX-License-Identifier: BSD-2-Clause
3*b6f4027aSOlivier Certner.\"
4*b6f4027aSOlivier Certner.\" Copyright © 2024 The FreeBSD Foundation
5*b6f4027aSOlivier Certner.\"
6*b6f4027aSOlivier Certner.\" This documentation was written by Olivier Certner <olce.freebsd@certner.fr>
7*b6f4027aSOlivier Certner.\" at Kumacom SARL under sponsorship from the FreeBSD Foundation.
8*b6f4027aSOlivier Certner.\"
9*b6f4027aSOlivier Certner.Dd December 19, 2024
10*b6f4027aSOlivier Certner.Dt SETCRED 2
11*b6f4027aSOlivier Certner.Os
12*b6f4027aSOlivier Certner.Sh NAME
13*b6f4027aSOlivier Certner.Nm setcred
14*b6f4027aSOlivier Certner.Nd set current process credentials atomically
15*b6f4027aSOlivier Certner.Sh LIBRARY
16*b6f4027aSOlivier Certner.Lb libc
17*b6f4027aSOlivier Certner.Sh SYNOPSIS
18*b6f4027aSOlivier Certner.In sys/ucred.h
19*b6f4027aSOlivier Certner.Ft int
20*b6f4027aSOlivier Certner.Fn setcred "u_int flags" "const struct setcred *wcred" "size_t size"
21*b6f4027aSOlivier Certner.Sh DESCRIPTION
22*b6f4027aSOlivier CertnerThe
23*b6f4027aSOlivier Certner.Fn setcred
24*b6f4027aSOlivier Certnersystem call can set any combination of user-accessible credentials of the
25*b6f4027aSOlivier Certnercurrent process in an atomic manner.
26*b6f4027aSOlivier Certner.Pp
27*b6f4027aSOlivier CertnerThis system call is normally permitted only for processes having the ID of the
28*b6f4027aSOlivier Certnersuper-user (0) as their effective user ID, or not at all if the
29*b6f4027aSOlivier Certner.Xr sysctl 8
30*b6f4027aSOlivier Certnervariable
31*b6f4027aSOlivier Certner.Va security.bsd.suser_enabled
32*b6f4027aSOlivier Certneris zero or some active MAC policy specifically denies these processes.
33*b6f4027aSOlivier Certner.Pp
34*b6f4027aSOlivier CertnerSome MAC policies, such as
35*b6f4027aSOlivier Certner.Xr mac_do 4 ,
36*b6f4027aSOlivier Certnermay also allow unprivileged users to call it successfully, possibly depending on
37*b6f4027aSOlivier Certnerthe exact credentials transition requested, once again unless any active MAC
38*b6f4027aSOlivier Certnerpolicy specifically denies that.
39*b6f4027aSOlivier Certner.Pp
40*b6f4027aSOlivier CertnerThe
41*b6f4027aSOlivier Certner.Fa flags
42*b6f4027aSOlivier Certnerargument serves to indicate which process credentials should be changed by the
43*b6f4027aSOlivier Certnercall.
44*b6f4027aSOlivier CertnerAllowed flags are:
45*b6f4027aSOlivier Certner.Pp
46*b6f4027aSOlivier Certner.Bl -tag -width "SETCREDF_SUPP_GROUPS   " -compact
47*b6f4027aSOlivier Certner.It Fa SETCREDF_UID
48*b6f4027aSOlivier CertnerSet the effective user ID.
49*b6f4027aSOlivier Certner.It Fa SETCREDF_RUID
50*b6f4027aSOlivier CertnerSet the real user ID.
51*b6f4027aSOlivier Certner.It Fa SETCREDF_SVUID
52*b6f4027aSOlivier CertnerSet the saved user ID.
53*b6f4027aSOlivier Certner.It Fa SETCREDF_GID
54*b6f4027aSOlivier CertnerSet the effective group ID.
55*b6f4027aSOlivier Certner.It Fa SETCREDF_RGID
56*b6f4027aSOlivier CertnerSet the real group ID.
57*b6f4027aSOlivier Certner.It Fa SETCREDF_SVGID
58*b6f4027aSOlivier CertnerSet the saved group ID.
59*b6f4027aSOlivier Certner.It Fa SETCREDF_SUPP_GROUPS
60*b6f4027aSOlivier CertnerSet the supplementary group list.
61*b6f4027aSOlivier Certner.It Fa SETCREDF_MAC_LABEL
62*b6f4027aSOlivier CertnerSet the MAC label.
63*b6f4027aSOlivier Certner.El
64*b6f4027aSOlivier Certner.Pp
65*b6f4027aSOlivier CertnerThe
66*b6f4027aSOlivier Certner.Vt struct setcred
67*b6f4027aSOlivier Certnerstructure is currently defined as:
68*b6f4027aSOlivier Certner.Bd -literal
69*b6f4027aSOlivier Certnerstruct setcred {
70*b6f4027aSOlivier Certner	uid_t	 sc_uid;		/* effective user id */
71*b6f4027aSOlivier Certner	uid_t	 sc_ruid;		/* real user id */
72*b6f4027aSOlivier Certner	uid_t	 sc_svuid;		/* saved user id */
73*b6f4027aSOlivier Certner	gid_t	 sc_gid;		/* effective group id */
74*b6f4027aSOlivier Certner	gid_t	 sc_rgid;		/* real group id */
75*b6f4027aSOlivier Certner	gid_t	 sc_svgid;		/* saved group id */
76*b6f4027aSOlivier Certner	u_int	 sc_pad;		/* padding, unused */
77*b6f4027aSOlivier Certner	u_int	 sc_supp_groups_nb;	/* supplementary groups number */
78*b6f4027aSOlivier Certner	gid_t	*sc_supp_groups;	/* supplementary groups */
79*b6f4027aSOlivier Certner	struct mac *sc_label;		/* MAC label */
80*b6f4027aSOlivier Certner};
81*b6f4027aSOlivier Certner.Ed
82*b6f4027aSOlivier Certner.Pp
83*b6f4027aSOlivier CertnerIts fields are:
84*b6f4027aSOlivier Certner.Pp
85*b6f4027aSOlivier Certner.Bl -tag -width "sc_supp_groups_nb   " -compact
86*b6f4027aSOlivier Certner.It Fa sc_uid
87*b6f4027aSOlivier CertnerThe ID to set the effective user to, if flag
88*b6f4027aSOlivier Certner.Dv SETCREDF_UID
89*b6f4027aSOlivier Certneris specified.
90*b6f4027aSOlivier Certner.It Fa sc_ruid
91*b6f4027aSOlivier CertnerThe ID to set the real user to, if flag
92*b6f4027aSOlivier Certner.Dv SETCREDF_RUID
93*b6f4027aSOlivier Certneris specified.
94*b6f4027aSOlivier Certner.It Fa sc_svuid
95*b6f4027aSOlivier CertnerThe ID to set the saved user to, if flag
96*b6f4027aSOlivier Certner.Dv SETCREDF_SVUID
97*b6f4027aSOlivier Certneris specified.
98*b6f4027aSOlivier Certner.It Fa sc_gid
99*b6f4027aSOlivier CertnerThe ID to set the effective group to, if flag
100*b6f4027aSOlivier Certner.Dv SETCREDF_GID
101*b6f4027aSOlivier Certneris specified.
102*b6f4027aSOlivier Certner.It Fa sc_rgid
103*b6f4027aSOlivier CertnerThe ID to set the real group to, if flag
104*b6f4027aSOlivier Certner.Dv SETCREDF_RGID
105*b6f4027aSOlivier Certneris specified.
106*b6f4027aSOlivier Certner.It Fa sc_svgid
107*b6f4027aSOlivier CertnerThe ID to set the saved group to, if flag
108*b6f4027aSOlivier Certner.Dv SETCREDF_SVGID
109*b6f4027aSOlivier Certneris specified.
110*b6f4027aSOlivier Certner.It Fa sc_supp_groups_nb
111*b6f4027aSOlivier CertnerThe size of array
112*b6f4027aSOlivier Certner.Fa sc_supp_groups ,
113*b6f4027aSOlivier Certnerif flag
114*b6f4027aSOlivier Certner.Dv SETCREDF_SUPP_GROUPS
115*b6f4027aSOlivier Certneris specified.
116*b6f4027aSOlivier CertnerIt must be less than or equal to
117*b6f4027aSOlivier Certner.Dv {NGROUPS_MAX} .
118*b6f4027aSOlivier Certner.It Fa sc_supp_groups
119*b6f4027aSOlivier CertnerAn array of IDs to set the supplementary groups to, if flag
120*b6f4027aSOlivier Certner.Dv SETCREDF_SUPP_GROUPS
121*b6f4027aSOlivier Certneris specified.
122*b6f4027aSOlivier CertnerNote that all groups in this array will be set as supplementary groups only, in
123*b6f4027aSOlivier Certnercontrast to
124*b6f4027aSOlivier Certner.Xr setgroups 2
125*b6f4027aSOlivier Certnerwhich treats the first element specially as the new effective group, not adding
126*b6f4027aSOlivier Certnerit to supplementary groups.
127*b6f4027aSOlivier Certner.It Fa sc_label
128*b6f4027aSOlivier CertnerA pointer to a valid MAC label structure, e.g., built with the
129*b6f4027aSOlivier Certner.Xr mac_from_text 3
130*b6f4027aSOlivier Certnerfunction, if flag
131*b6f4027aSOlivier Certner.Dv SETCREDF_MAC_LABEL
132*b6f4027aSOlivier Certneris specified.
133*b6f4027aSOlivier Certner.El
134*b6f4027aSOlivier Certner.Pp
135*b6f4027aSOlivier CertnerFor forward compatibility and security reasons, it is recommended that users
136*b6f4027aSOlivier Certneralways initialize objects of type
137*b6f4027aSOlivier Certner.Vt struct setcred
138*b6f4027aSOlivier Certnerwith the provided initializer:
139*b6f4027aSOlivier Certner.Dv SETCRED_INITIALIZER .
140*b6f4027aSOlivier Certner.Pp
141*b6f4027aSOlivier CertnerThe
142*b6f4027aSOlivier Certner.Fa size
143*b6f4027aSOlivier Certnerargument must be the size of the passed
144*b6f4027aSOlivier Certner.Fa wcred
145*b6f4027aSOlivier Certnerstructure.
146*b6f4027aSOlivier Certner.Sh RETURN VALUES
147*b6f4027aSOlivier Certner.Rv -std
148*b6f4027aSOlivier Certner.Sh ERRORS
149*b6f4027aSOlivier CertnerThe
150*b6f4027aSOlivier Certner.Fn setcred
151*b6f4027aSOlivier Certnersystem call will fail if:
152*b6f4027aSOlivier Certner.Bl -tag -width Er
153*b6f4027aSOlivier Certner.It Bq Er EINVAL
154*b6f4027aSOlivier CertnerUnrecognized flags were passed in
155*b6f4027aSOlivier Certner.Fa flags ,
156*b6f4027aSOlivier Certneror the
157*b6f4027aSOlivier Certner.Fa size
158*b6f4027aSOlivier Certnerparameter does not match the size of
159*b6f4027aSOlivier Certner.Vt struct setcred ,
160*b6f4027aSOlivier Certneror the field
161*b6f4027aSOlivier Certner.Fa sc_supp_group_nb
162*b6f4027aSOlivier Certnerhas a value strictly greater than
163*b6f4027aSOlivier Certner.Dv {NGROUPS_MAX}
164*b6f4027aSOlivier Certner.Po if flag
165*b6f4027aSOlivier Certner.Dv SETCREDF_SUPP_GROUPS
166*b6f4027aSOlivier Certnerwas supplied
167*b6f4027aSOlivier Certner.Pc ,
168*b6f4027aSOlivier Certneror the MAC label pointed to by field
169*b6f4027aSOlivier Certner.Fa sc_label
170*b6f4027aSOlivier Certneris invalid
171*b6f4027aSOlivier Certner.Po if flag
172*b6f4027aSOlivier Certner.Dv SETCREDF_MAC_LABEL
173*b6f4027aSOlivier Certnerwas supplied
174*b6f4027aSOlivier Certner.Pc .
175*b6f4027aSOlivier Certner.It Bq Er EFAULT
176*b6f4027aSOlivier CertnerThe
177*b6f4027aSOlivier Certner.Fa wcred
178*b6f4027aSOlivier Certnerpointer, or pointers in fields
179*b6f4027aSOlivier Certner.Fa sc_supp_groups
180*b6f4027aSOlivier Certner.Po if flag
181*b6f4027aSOlivier Certner.Dv SETCREDF_SUPP_GROUPS
182*b6f4027aSOlivier Certnerwas supplied
183*b6f4027aSOlivier Certner.Pc
184*b6f4027aSOlivier Certneror
185*b6f4027aSOlivier Certner.Fa sc_label
186*b6f4027aSOlivier Certner.Po if flag
187*b6f4027aSOlivier Certner.Dv SETCREDF_MAC_LABEL
188*b6f4027aSOlivier Certnerwas supplied
189*b6f4027aSOlivier Certner.Pc
190*b6f4027aSOlivier Certnerpoint to invalid locations.
191*b6f4027aSOlivier Certner.It Bq Er EPERM
192*b6f4027aSOlivier CertnerThe user is not the super-user and/or the requested credentials transition is
193*b6f4027aSOlivier Certnernot allowed by the system or MAC modules.
194*b6f4027aSOlivier Certner.It Bq Er EOPNOTSUPP
195*b6f4027aSOlivier CertnerSome of the requested credentials have a type that the system does not support.
196*b6f4027aSOlivier CertnerThis currently can occur only if the kernel has been compiled without MAC and
197*b6f4027aSOlivier Certner.Dv SETCREDF_MAC_LABEL
198*b6f4027aSOlivier Certnerhas been passed.
199*b6f4027aSOlivier Certner.El
200*b6f4027aSOlivier Certner.Sh SEE ALSO
201*b6f4027aSOlivier Certner.Xr issetugid 2 ,
202*b6f4027aSOlivier Certner.Xr setregid 2 ,
203*b6f4027aSOlivier Certner.Xr setreuid 2 ,
204*b6f4027aSOlivier Certner.Xr setuid 2 ,
205*b6f4027aSOlivier Certner.Xr mac_text 3 ,
206*b6f4027aSOlivier Certner.Xr mac 4 ,
207*b6f4027aSOlivier Certner.Xr mac_do 4 ,
208*b6f4027aSOlivier Certner.Xr maclabel 7
209*b6f4027aSOlivier Certner.Sh STANDARDS
210*b6f4027aSOlivier CertnerThe
211*b6f4027aSOlivier Certner.Fn setcred
212*b6f4027aSOlivier Certnersystem call is specific to
213*b6f4027aSOlivier Certner.Fx .
214*b6f4027aSOlivier Certner.Pp
215*b6f4027aSOlivier CertnerA call to
216*b6f4027aSOlivier Certner.Fn setcred
217*b6f4027aSOlivier Certnerusually changes process credentials that are listed by POSIX/SUS standards.
218*b6f4027aSOlivier CertnerThe changed values then produce the effects with respect to the rest of the
219*b6f4027aSOlivier Certnersystem that are described in these standards, as if these changes had resulted
220*b6f4027aSOlivier Certnerfrom calling standard or traditional credentials-setting functions.
221*b6f4027aSOlivier CertnerCurrently, all flags but
222*b6f4027aSOlivier Certner.Dv SETCREDF_MAC_LABEL
223*b6f4027aSOlivier Certnerlead to modifying standard credentials.
224*b6f4027aSOlivier Certner.Pp
225*b6f4027aSOlivier CertnerThe only differences in using
226*b6f4027aSOlivier Certner.Fn setcred
227*b6f4027aSOlivier Certnerto change standard credentials instead of standard or traditional functions are:
228*b6f4027aSOlivier Certner.Pp
229*b6f4027aSOlivier Certner.Bl -bullet -compact
230*b6f4027aSOlivier Certner.It
231*b6f4027aSOlivier CertnerAll requested changes are performed atomically.
232*b6f4027aSOlivier Certner.It
233*b6f4027aSOlivier CertnerOnly the super-user or an unprivileged user authorized by some MAC module can
234*b6f4027aSOlivier Certnersuccessfully call
235*b6f4027aSOlivier Certner.Fn setcred ,
236*b6f4027aSOlivier Certnereven if the standard system calls would have authorized any unprivileged user to
237*b6f4027aSOlivier Certnereffect the same changes.
238*b6f4027aSOlivier CertnerFor example,
239*b6f4027aSOlivier Certner.Fn seteuid
240*b6f4027aSOlivier Certnerallows any unprivileged user to change the effective user ID to either the real
241*b6f4027aSOlivier Certneror saved ones, while
242*b6f4027aSOlivier Certner.Fn setcred
243*b6f4027aSOlivier Certnercalled with flag
244*b6f4027aSOlivier Certner.Dv SETCREDF_UID
245*b6f4027aSOlivier Certnerdoes not.
246*b6f4027aSOlivier Certner.El
247*b6f4027aSOlivier Certner.Sh HISTORY
248*b6f4027aSOlivier CertnerThe
249*b6f4027aSOlivier Certner.Fn setcred
250*b6f4027aSOlivier Certnersystem call appeared in
251*b6f4027aSOlivier Certner.Fx 15.0 .
252*b6f4027aSOlivier Certner.Pp
253*b6f4027aSOlivier CertnerTraditionally in UNIX, all credential changes beyond shuffles of effective, real
254*b6f4027aSOlivier Certnerand saved IDs have been done by setuid binaries that successively call multiple
255*b6f4027aSOlivier Certnercredentials-setting system calls and in a specific order.
256*b6f4027aSOlivier CertnerFor example, to change all user IDs to that of some unprivileged user,
257*b6f4027aSOlivier Certner.Fn setuid
258*b6f4027aSOlivier Certnermust be called last so that all other credentials-changing calls can be
259*b6f4027aSOlivier Certnerperformed successfully beforehand, as they require super-user privileges.
260*b6f4027aSOlivier Certner.Pp
261*b6f4027aSOlivier CertnerThis piecewise approach causes such a process to transiently hold high privilege
262*b6f4027aSOlivier Certnercredentials that are neither the original nor necessarily the desired final
263*b6f4027aSOlivier Certnerones.
264*b6f4027aSOlivier CertnerBesides opening a transition window where possible vulnerabilities could have
265*b6f4027aSOlivier Certnercatastrophic consequences, it makes it impossible for the kernel to enforce that
266*b6f4027aSOlivier Certneronly certain transitions of credentials are allowed.
267*b6f4027aSOlivier Certner.Pp
268*b6f4027aSOlivier CertnerThe necessity of an atomic, global approach to changing credentials clearly
269*b6f4027aSOlivier Certnerappeared while working on extending
270*b6f4027aSOlivier Certner.Xr mac_do 4
271*b6f4027aSOlivier Certnerto allow rules to authorize only specific changes of primary or supplementary
272*b6f4027aSOlivier Certnergroups, which prompted the addition of
273*b6f4027aSOlivier Certner.Fn setcred .
274*b6f4027aSOlivier Certner.Sh AUTHORS
275*b6f4027aSOlivier CertnerThe
276*b6f4027aSOlivier Certner.Fn setcred
277*b6f4027aSOlivier Certnersystem call and this manual page were written by
278*b6f4027aSOlivier Certner.An Olivier Certner Aq Mt olce.freebsd@certner.fr .
279*b6f4027aSOlivier Certner.Sh SECURITY CONSIDERATIONS
280*b6f4027aSOlivier CertnerThe same considerations as those of standard or traditional credentials-setting
281*b6f4027aSOlivier Certnersystem calls apply to
282*b6f4027aSOlivier Certner.Fn setcred ,
283*b6f4027aSOlivier Certnerexcept for the lack of atomicity of successive such calls.
284*b6f4027aSOlivier Certner.Pp
285*b6f4027aSOlivier CertnerIn particular, please consult section
286*b6f4027aSOlivier Certner.Sy SECURITY CONSIDERATIONS
287*b6f4027aSOlivier Certnerof the
288*b6f4027aSOlivier Certner.Xr setuid 2
289*b6f4027aSOlivier Certnermanual page about the absence of effect of changing standard credentials on
290*b6f4027aSOlivier Certneralready open files.
291