xref: /freebsd/share/man/man9/ucred.9 (revision 571dba6ec9f25ecf7582dc2192daf1ceea70065f)
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.\"
290db0f192SDima Dorfman.Dd March 3, 2002
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 *"
574772483cSRuslan Ermilov.Fn crdup "struct ucred *cr"
580db0f192SDima Dorfman.Ft void
590db0f192SDima Dorfman.Fn cru2x "struct ucred *cr" "struct xucred *xcr"
60e2eca5feSChad David.Ft void
61e2eca5feSChad David.Fn cred_update_thread "struct thread *td"
624772483cSRuslan Ermilov.Sh DESCRIPTION
634772483cSRuslan ErmilovThe
644772483cSRuslan Ermilov.Nm
654772483cSRuslan Ermilovfamily of functions is used to manage user credential structures
664772483cSRuslan Ermilov.Pq Vt "struct ucred"
674772483cSRuslan Ermilovwithin the kernel.
684772483cSRuslan Ermilov.Pp
694772483cSRuslan ErmilovThe
704772483cSRuslan Ermilov.Fn crget
714772483cSRuslan Ermilovfunction allocates memory
724772483cSRuslan Ermilovfor a new structure, sets its reference count to 1, and
734772483cSRuslan Ermilovinitializes its lock.
744772483cSRuslan Ermilov.Pp
754772483cSRuslan ErmilovThe
764772483cSRuslan Ermilov.Fn crhold
774772483cSRuslan Ermilovfunction increases the reference count on the credential.
784772483cSRuslan Ermilov.Pp
794772483cSRuslan ErmilovThe
804772483cSRuslan Ermilov.Fn crfree
814772483cSRuslan Ermilovfunction decreases the reference count on the credential.
824772483cSRuslan ErmilovIf the count drops to 0, the storage for the structure is freed.
834772483cSRuslan Ermilov.Pp
844772483cSRuslan ErmilovThe
8557813e7fSRuslan Ermilov.Fn crshared
8657813e7fSRuslan Ermilovfunction returns true if the credential is shared.
8757813e7fSRuslan ErmilovA credential is considered to be shared if its reference
8857813e7fSRuslan Ermilovcount is greater than one.
8957813e7fSRuslan Ermilov.Pp
9057813e7fSRuslan ErmilovThe
914772483cSRuslan Ermilov.Fn crcopy
9257813e7fSRuslan Ermilovfunction copies the contents of the source (template)
9357813e7fSRuslan Ermilovcredential into the destination template.
9457813e7fSRuslan ErmilovThe
9557813e7fSRuslan Ermilov.Vt uidinfo
9657813e7fSRuslan Ermilovstructure within the destination is referenced
9757813e7fSRuslan Ermilovby calling
9857813e7fSRuslan Ermilov.Xr uihold 9 .
994772483cSRuslan Ermilov.Pp
1004772483cSRuslan ErmilovThe
1014772483cSRuslan Ermilov.Fn crdup
10257813e7fSRuslan Ermilovfunction allocates memory for a new structure and copies the
10357813e7fSRuslan Ermilovcontents of
10457813e7fSRuslan Ermilov.Fa cr
10557813e7fSRuslan Ermilovinto it.
10657813e7fSRuslan ErmilovThe actual copying is performed by
10757813e7fSRuslan Ermilov.Fn crcopy .
1080db0f192SDima Dorfman.Pp
1090db0f192SDima DorfmanThe
1100db0f192SDima Dorfman.Fn cru2x
1110db0f192SDima Dorfmanfunction converts a
1120db0f192SDima Dorfman.Vt ucred
1130db0f192SDima Dorfmanstructure to an
1140db0f192SDima Dorfman.Vt xucred
1150db0f192SDima Dorfmanstructure.
1160db0f192SDima DorfmanThat is,
1170db0f192SDima Dorfmanit copies data from
1180db0f192SDima Dorfman.Fa cr
1190db0f192SDima Dorfmanto
1200db0f192SDima Dorfman.Fa xcr ;
1210db0f192SDima Dorfmanit ignores fields in the former that are not present in the latter
1220db0f192SDima Dorfman(e.g.,
1230db0f192SDima Dorfman.Va cr_uidinfo ) ,
1240db0f192SDima Dorfmanand appropriately sets fields in the latter that are not present in
1250db0f192SDima Dorfmanthe former
1260db0f192SDima Dorfman(e.g.,
1270db0f192SDima Dorfman.Va cr_version ) .
128e2eca5feSChad David.Pp
129e2eca5feSChad DavidThe
130e2eca5feSChad David.Fn cred_update_thread
131e2eca5feSChad Davidfunction sets the credentials of
132e2eca5feSChad David.Fa td
133e2eca5feSChad Davidto that of its process, freeing its old credential if required.
1344772483cSRuslan Ermilov.Sh RETURN VALUES
135e2eca5feSChad David.Fn crget ,
136e2eca5feSChad David.Fn crhold
137e2eca5feSChad Davidand
138e2eca5feSChad David.Fn crdup
139e2eca5feSChad Davidall return a pointer to a
1404772483cSRuslan Ermilov.Vt ucred
1414772483cSRuslan Ermilovstructure.
142e2eca5feSChad David.Pp
143e2eca5feSChad David.Fn crshared
144e2eca5feSChad Davidreturns 0 if the credential has a reference count greater than 1;
145e2eca5feSChad Davidotherwise, 1 is returned.
146b388e223SJulian Elischer.Sh USAGE NOTES
147fd3083e0SRobert WatsonAs of
148fd3083e0SRobert Watson.Fx 5.0 ,
149fd3083e0SRobert Watsonthe
150b388e223SJulian Elischer.Vt ucred
151fd3083e0SRobert Watsonstructure contains extensible fields.
152fd3083e0SRobert WatsonThis means that the correct protocol must always be followed to create
153fd3083e0SRobert Watsona fresh and writable credential structure: new credentials must always
154fd3083e0SRobert Watsonbe derived from existing credentials using
155fd3083e0SRobert Watson.Fn crget
156fd3083e0SRobert Watsonand
157fd3083e0SRobert Watson.Fn crcopy .
158fd3083e0SRobert Watson.Pp
159fd3083e0SRobert WatsonIn the common case, credentials required for access control decisions are
160fd3083e0SRobert Watsonused in a read-only manner.
161fd3083e0SRobert WatsonIn these circumstances, the thread credential
162fd3083e0SRobert Watson.Va td_ucred
163fd3083e0SRobert Watsonshould be used, as it requires no locking to access safely, and remains stable
164fd3083e0SRobert Watsonfor the duration of the call even in the face of a multi-threaded
165fd3083e0SRobert Watsonapplication changing the process credentials from another thread.
166fd3083e0SRobert WatsonPrimitives such as
167fd3083e0SRobert Watson.Xr suser 9
168fd3083e0SRobert Watsonwill assume the use of
169fd3083e0SRobert Watson.Va td_ucred
170fd3083e0SRobert Watsonunless explicitly specified using
171fd3083e0SRobert Watson.Xr suser_cred 9 .
172fd3083e0SRobert Watson.Pp
173fd3083e0SRobert WatsonDuring a process credential update, the process lock must be held across
174fd3083e0SRobert Watsoncheck and update, to prevent race conditions.
175fd3083e0SRobert WatsonThe process credential,
176fd3083e0SRobert Watson.Va td->td_proc->p_ucred ,
177fd3083e0SRobert Watsonmust be used both for check and update.
178fd3083e0SRobert WatsonIf a process credential is updated during a system call and checks against
179fd3083e0SRobert Watsonthe thread credential are to be made later during the same system call,
180fd3083e0SRobert Watsonthe thread credential must also be refreshed from the process credential
181fd3083e0SRobert Watsonso as to prevent use of a stale value.
182fd3083e0SRobert WatsonTo avoid this scenario, it is recommended that system calls updating the
1836c8c6098SRuslan Ermilovprocess credential be designed to avoid other authorization functions.
184fd3083e0SRobert Watson.Pp
185fd3083e0SRobert WatsonIf temporarily elevated privileges are required for a thread, the thread
186fd3083e0SRobert Watsoncredential can by replaced for the duration of an activity, or for
187fd3083e0SRobert Watsonthe remainder of the system call.
188fd3083e0SRobert WatsonHowever, as a thread credential is often shared, appropriate care should be
189fd3083e0SRobert Watsontaken to make sure modifications are made to a writable credential
190fd3083e0SRobert Watsonthrough the use of
191fd3083e0SRobert Watson.Fn crget
192fd3083e0SRobert Watsonand
193fd3083e0SRobert Watson.Fn crcopy .
194fd3083e0SRobert Watson.Pp
195fd3083e0SRobert WatsonCaution should be exercised when checking authorization for a thread or
196fd3083e0SRobert Watsonprocess perform an operation on another thread or process.
197fd3083e0SRobert WatsonAs a result of temporary elevation, the target thread credential should
198fd3083e0SRobert Watson.Em never
199fd3083e0SRobert Watsonbe used as the target credential in an access control decision: the process
200fd3083e0SRobert Watsoncredential associated with the thread,
201fd3083e0SRobert Watson.Va td->td_proc->p_ucred ,
202fd3083e0SRobert Watsonshould be used instead.
203fd3083e0SRobert WatsonFor example,
204fd3083e0SRobert Watson.Xr p_candebug 9
205fd3083e0SRobert Watsonaccepts a target process, not a target thread, for access control purposes.
2064772483cSRuslan Ermilov.Sh SEE ALSO
2074772483cSRuslan Ermilov.Xr uihold 9
2084772483cSRuslan Ermilov.Sh AUTHORS
209571dba6eSHiten PandyaThis manual page was written by
2104772483cSRuslan Ermilov.An Chad David Aq davidc@acns.ab.ca .
211