xref: /freebsd/share/man/man9/ucred.9 (revision e8477da2127aded5cc67f0a1eb6173b51de08a12)
14772483cSRuslan Ermilov.\"
24772483cSRuslan Ermilov.\" Copyright (C) 2001 Chad David <davidc@acns.ab.ca>. All rights reserved.
34772483cSRuslan Ermilov.\"
44772483cSRuslan Ermilov.\" Redistribution and use in source and binary forms, with or without
54772483cSRuslan Ermilov.\" modification, are permitted provided that the following conditions
64772483cSRuslan Ermilov.\" are met:
74772483cSRuslan Ermilov.\" 1. Redistributions of source code must retain the above copyright
84772483cSRuslan Ermilov.\"    notice(s), this list of conditions and the following disclaimer as
94772483cSRuslan Ermilov.\"    the first lines of this file unmodified other than the possible
104772483cSRuslan Ermilov.\"    addition of one or more copyright notices.
114772483cSRuslan Ermilov.\" 2. Redistributions in binary form must reproduce the above copyright
124772483cSRuslan Ermilov.\"    notice(s), this list of conditions and the following disclaimer in the
134772483cSRuslan Ermilov.\"    documentation and/or other materials provided with the distribution.
144772483cSRuslan Ermilov.\"
154772483cSRuslan Ermilov.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
164772483cSRuslan Ermilov.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
174772483cSRuslan Ermilov.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
184772483cSRuslan Ermilov.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
194772483cSRuslan Ermilov.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
204772483cSRuslan Ermilov.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
214772483cSRuslan Ermilov.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
224772483cSRuslan Ermilov.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234772483cSRuslan Ermilov.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244772483cSRuslan Ermilov.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
254772483cSRuslan Ermilov.\" DAMAGE.
264772483cSRuslan Ermilov.\"
274772483cSRuslan Ermilov.\" $FreeBSD$
284772483cSRuslan Ermilov.\"
29e8477da2SBrooks Davis.Dd June 19, 2009
304772483cSRuslan Ermilov.Dt UCRED 9
314772483cSRuslan Ermilov.Os
324772483cSRuslan Ermilov.Sh NAME
334772483cSRuslan Ermilov.Nm ucred ,
344772483cSRuslan Ermilov.Nm crget ,
354772483cSRuslan Ermilov.Nm crhold ,
364772483cSRuslan Ermilov.Nm crfree ,
3757813e7fSRuslan Ermilov.Nm crshared ,
384772483cSRuslan Ermilov.Nm crcopy ,
390db0f192SDima Dorfman.Nm crdup ,
40e2eca5feSChad David.Nm cru2x ,
416c8c6098SRuslan Ermilov.Nm cred_update_thread
424772483cSRuslan Ermilov.Nd "functions related to user credentials"
434772483cSRuslan Ermilov.Sh SYNOPSIS
444772483cSRuslan Ermilov.In sys/param.h
454772483cSRuslan Ermilov.In sys/ucred.h
464772483cSRuslan Ermilov.Ft "struct ucred *"
474772483cSRuslan Ermilov.Fn crget void
48f16b3c0dSChad David.Ft "struct ucred *"
494772483cSRuslan Ermilov.Fn crhold "struct ucred *cr"
504772483cSRuslan Ermilov.Ft void
514772483cSRuslan Ermilov.Fn crfree "struct ucred *cr"
5257813e7fSRuslan Ermilov.Ft int
5357813e7fSRuslan Ermilov.Fn crshared "struct ucred *cr"
5457813e7fSRuslan Ermilov.Ft void
5557813e7fSRuslan Ermilov.Fn crcopy "struct ucred *dest" "struct ucred *src"
564772483cSRuslan Ermilov.Ft "struct ucred *"
57e8477da2SBrooks Davis.Fn crcopysafe "struct proc *p" "struct ucred *cr"
58e8477da2SBrooks Davis.Ft "struct ucred *"
594772483cSRuslan Ermilov.Fn crdup "struct ucred *cr"
600db0f192SDima Dorfman.Ft void
61e8477da2SBrooks Davis.Fn crsetgroups "struct ucred *cr" "int ngrp" "gid_t *groups"
62e8477da2SBrooks Davis.Ft void
630db0f192SDima Dorfman.Fn cru2x "struct ucred *cr" "struct xucred *xcr"
64e2eca5feSChad David.Ft void
65e2eca5feSChad David.Fn cred_update_thread "struct thread *td"
664772483cSRuslan Ermilov.Sh DESCRIPTION
674772483cSRuslan ErmilovThe
684772483cSRuslan Ermilov.Nm
694772483cSRuslan Ermilovfamily of functions is used to manage user credential structures
704772483cSRuslan Ermilov.Pq Vt "struct ucred"
714772483cSRuslan Ermilovwithin the kernel.
724772483cSRuslan Ermilov.Pp
734772483cSRuslan ErmilovThe
744772483cSRuslan Ermilov.Fn crget
754772483cSRuslan Ermilovfunction allocates memory
764772483cSRuslan Ermilovfor a new structure, sets its reference count to 1, and
774772483cSRuslan Ermilovinitializes its lock.
784772483cSRuslan Ermilov.Pp
794772483cSRuslan ErmilovThe
804772483cSRuslan Ermilov.Fn crhold
814772483cSRuslan Ermilovfunction increases the reference count on the credential.
824772483cSRuslan Ermilov.Pp
834772483cSRuslan ErmilovThe
844772483cSRuslan Ermilov.Fn crfree
854772483cSRuslan Ermilovfunction decreases the reference count on the credential.
864772483cSRuslan ErmilovIf the count drops to 0, the storage for the structure is freed.
874772483cSRuslan Ermilov.Pp
884772483cSRuslan ErmilovThe
8957813e7fSRuslan Ermilov.Fn crshared
9057813e7fSRuslan Ermilovfunction returns true if the credential is shared.
9157813e7fSRuslan ErmilovA credential is considered to be shared if its reference
9257813e7fSRuslan Ermilovcount is greater than one.
9357813e7fSRuslan Ermilov.Pp
9457813e7fSRuslan ErmilovThe
954772483cSRuslan Ermilov.Fn crcopy
9657813e7fSRuslan Ermilovfunction copies the contents of the source (template)
9757813e7fSRuslan Ermilovcredential into the destination template.
9857813e7fSRuslan ErmilovThe
9957813e7fSRuslan Ermilov.Vt uidinfo
10057813e7fSRuslan Ermilovstructure within the destination is referenced
10157813e7fSRuslan Ermilovby calling
10257813e7fSRuslan Ermilov.Xr uihold 9 .
1034772483cSRuslan Ermilov.Pp
1044772483cSRuslan ErmilovThe
105e8477da2SBrooks Davis.Fn crcopysafe
106e8477da2SBrooks Davisfunction copies the current credential associated with the process
107e8477da2SBrooks Davis.Fa p
108e8477da2SBrooks Davisinto the newly allocated credential
109e8477da2SBrooks Davis.Fa cr .
110e8477da2SBrooks DavisThe process lock on
111e8477da2SBrooks Davis.Fa p
112e8477da2SBrooks Davismust be held and will be dropped and reacquired as needed to allocate
113e8477da2SBrooks Davisgroup storage space in
114e8477da2SBrooks Davis.Fa cr .
115e8477da2SBrooks Davis.Pp
116e8477da2SBrooks DavisThe
1174772483cSRuslan Ermilov.Fn crdup
11857813e7fSRuslan Ermilovfunction allocates memory for a new structure and copies the
11957813e7fSRuslan Ermilovcontents of
12057813e7fSRuslan Ermilov.Fa cr
12157813e7fSRuslan Ermilovinto it.
12257813e7fSRuslan ErmilovThe actual copying is performed by
12357813e7fSRuslan Ermilov.Fn crcopy .
1240db0f192SDima Dorfman.Pp
1250db0f192SDima DorfmanThe
126e8477da2SBrooks Davis.Fn crsetgroups
127e8477da2SBrooks Davisfunction sets the
128e8477da2SBrooks Davis.Va cr_groups
129e8477da2SBrooks Davisand
130e8477da2SBrooks Davis.Va cr_ngroups
131e8477da2SBrooks Davisvariables and allocates space as needed.
132e8477da2SBrooks DavisIt also truncates the group list to the current maximum number of
133e8477da2SBrooks Davisgroups.
134e8477da2SBrooks DavisNo other mechanism should be used to modify the
135e8477da2SBrooks Davis.Va cr_groups
136e8477da2SBrooks Davisarray except for updating the primary group via assignment to
137e8477da2SBrooks Davis.Va cr_groups[0] .
138e8477da2SBrooks Davis.Pp
139e8477da2SBrooks DavisThe
1400db0f192SDima Dorfman.Fn cru2x
1410db0f192SDima Dorfmanfunction converts a
1420db0f192SDima Dorfman.Vt ucred
1430db0f192SDima Dorfmanstructure to an
1440db0f192SDima Dorfman.Vt xucred
1450db0f192SDima Dorfmanstructure.
1460db0f192SDima DorfmanThat is,
1470db0f192SDima Dorfmanit copies data from
1480db0f192SDima Dorfman.Fa cr
1490db0f192SDima Dorfmanto
1500db0f192SDima Dorfman.Fa xcr ;
1510db0f192SDima Dorfmanit ignores fields in the former that are not present in the latter
1520db0f192SDima Dorfman(e.g.,
1530db0f192SDima Dorfman.Va cr_uidinfo ) ,
1540db0f192SDima Dorfmanand appropriately sets fields in the latter that are not present in
1550db0f192SDima Dorfmanthe former
1560db0f192SDima Dorfman(e.g.,
1570db0f192SDima Dorfman.Va cr_version ) .
158e2eca5feSChad David.Pp
159e2eca5feSChad DavidThe
160e2eca5feSChad David.Fn cred_update_thread
161e2eca5feSChad Davidfunction sets the credentials of
162e2eca5feSChad David.Fa td
163e2eca5feSChad Davidto that of its process, freeing its old credential if required.
1644772483cSRuslan Ermilov.Sh RETURN VALUES
165e2eca5feSChad David.Fn crget ,
166e8477da2SBrooks Davis.Fn crhold ,
167e8477da2SBrooks Davis.Fn crdup ,
168e2eca5feSChad Davidand
169e8477da2SBrooks Davis.Fn crcopysafe
170e2eca5feSChad Davidall return a pointer to a
1714772483cSRuslan Ermilov.Vt ucred
1724772483cSRuslan Ermilovstructure.
173e2eca5feSChad David.Pp
174e2eca5feSChad David.Fn crshared
175e2eca5feSChad Davidreturns 0 if the credential has a reference count greater than 1;
176e2eca5feSChad Davidotherwise, 1 is returned.
177b388e223SJulian Elischer.Sh USAGE NOTES
178fd3083e0SRobert WatsonAs of
179fd3083e0SRobert Watson.Fx 5.0 ,
180fd3083e0SRobert Watsonthe
181b388e223SJulian Elischer.Vt ucred
182fd3083e0SRobert Watsonstructure contains extensible fields.
183fd3083e0SRobert WatsonThis means that the correct protocol must always be followed to create
184fd3083e0SRobert Watsona fresh and writable credential structure: new credentials must always
185fd3083e0SRobert Watsonbe derived from existing credentials using
186e8477da2SBrooks Davis.Fn crget ,
187e8477da2SBrooks Davis.Fn crcopy ,
188fd3083e0SRobert Watsonand
189e8477da2SBrooks Davis.Fn crcopysafe .
190fd3083e0SRobert Watson.Pp
191fd3083e0SRobert WatsonIn the common case, credentials required for access control decisions are
192fd3083e0SRobert Watsonused in a read-only manner.
193fd3083e0SRobert WatsonIn these circumstances, the thread credential
194fd3083e0SRobert Watson.Va td_ucred
195fd3083e0SRobert Watsonshould be used, as it requires no locking to access safely, and remains stable
196fd3083e0SRobert Watsonfor the duration of the call even in the face of a multi-threaded
197fd3083e0SRobert Watsonapplication changing the process credentials from another thread.
198fd3083e0SRobert Watson.Pp
199fd3083e0SRobert WatsonDuring a process credential update, the process lock must be held across
200fd3083e0SRobert Watsoncheck and update, to prevent race conditions.
201fd3083e0SRobert WatsonThe process credential,
202fd3083e0SRobert Watson.Va td->td_proc->p_ucred ,
203fd3083e0SRobert Watsonmust be used both for check and update.
204fd3083e0SRobert WatsonIf a process credential is updated during a system call and checks against
205fd3083e0SRobert Watsonthe thread credential are to be made later during the same system call,
206fd3083e0SRobert Watsonthe thread credential must also be refreshed from the process credential
207fd3083e0SRobert Watsonso as to prevent use of a stale value.
208fd3083e0SRobert WatsonTo avoid this scenario, it is recommended that system calls updating the
2096c8c6098SRuslan Ermilovprocess credential be designed to avoid other authorization functions.
210fd3083e0SRobert Watson.Pp
211fd3083e0SRobert WatsonIf temporarily elevated privileges are required for a thread, the thread
212fd3083e0SRobert Watsoncredential can by replaced for the duration of an activity, or for
213fd3083e0SRobert Watsonthe remainder of the system call.
214fd3083e0SRobert WatsonHowever, as a thread credential is often shared, appropriate care should be
215fd3083e0SRobert Watsontaken to make sure modifications are made to a writable credential
216fd3083e0SRobert Watsonthrough the use of
217fd3083e0SRobert Watson.Fn crget
218fd3083e0SRobert Watsonand
219fd3083e0SRobert Watson.Fn crcopy .
220fd3083e0SRobert Watson.Pp
221fd3083e0SRobert WatsonCaution should be exercised when checking authorization for a thread or
222fd3083e0SRobert Watsonprocess perform an operation on another thread or process.
223fd3083e0SRobert WatsonAs a result of temporary elevation, the target thread credential should
224fd3083e0SRobert Watson.Em never
225fd3083e0SRobert Watsonbe used as the target credential in an access control decision: the process
226fd3083e0SRobert Watsoncredential associated with the thread,
227fd3083e0SRobert Watson.Va td->td_proc->p_ucred ,
228fd3083e0SRobert Watsonshould be used instead.
229fd3083e0SRobert WatsonFor example,
230fd3083e0SRobert Watson.Xr p_candebug 9
231fd3083e0SRobert Watsonaccepts a target process, not a target thread, for access control purposes.
2324772483cSRuslan Ermilov.Sh SEE ALSO
2334772483cSRuslan Ermilov.Xr uihold 9
2344772483cSRuslan Ermilov.Sh AUTHORS
235571dba6eSHiten PandyaThis manual page was written by
2364772483cSRuslan Ermilov.An Chad David Aq davidc@acns.ab.ca .
237