xref: /freebsd/sys/sys/ucred.h (revision 0abeb8d8d8df1842b0c2d2276c958b0150579a1c)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _SYS_UCRED_H_
33 #define	_SYS_UCRED_H_
34 
35 #include <sys/types.h>
36 #if defined(_KERNEL) || defined(_WANT_UCRED)
37 #include <sys/_lock.h>
38 #include <sys/_mutex.h>
39 #endif
40 #include <bsm/audit.h>
41 
42 #if defined(_KERNEL) || defined(_WANT_UCRED)
43 /*
44  * Flags for cr_flags.
45  */
46 #define	CRED_FLAG_CAPMODE	0x00000001	/* In capability mode. */
47 #define	CRED_FLAG_GROUPSET	0x00000002	/* Groups have been set. */
48 
49 /*
50  * Number of groups inlined in 'struct ucred'.  It must stay reasonably low as
51  * it is also used by some functions to allocate an array of this size on the
52  * stack.
53  */
54 #define	CRED_SMALLGROUPS_NB	16
55 
56 struct label;
57 struct loginclass;
58 struct prison;
59 struct uidinfo;
60 
61 /*
62  * Credentials.
63  *
64  * Please do not inspect cr_uid directly to determine superuserness.  The
65  * priv(9) interface should be used to check for privilege.
66  *
67  * Lock reference:
68  *      c - cr_mtx
69  *
70  * Unmarked fields are constant after creation.
71  *
72  * See "Credential management" comment in kern_prot.c for more information.
73  */
74 struct ucred {
75 	struct mtx cr_mtx;
76 	long	cr_ref;			/* (c) reference count */
77 	u_int	cr_users;		/* (c) proc + thread using this cred */
78 	u_int	cr_flags;		/* credential flags */
79 	struct auditinfo_addr	cr_audit;	/* Audit properties. */
80 	int	cr_ngroups;		/* number of supplementary groups */
81 #define	cr_startcopy cr_uid
82 	uid_t	cr_uid;			/* effective user id */
83 	uid_t	cr_ruid;		/* real user id */
84 	uid_t	cr_svuid;		/* saved user id */
85 	gid_t	cr_gid;			/* effective group id */
86 	gid_t	cr_rgid;		/* real group id */
87 	gid_t	cr_svgid;		/* saved group id */
88 	struct uidinfo	*cr_uidinfo;	/* per euid resource consumption */
89 	struct uidinfo	*cr_ruidinfo;	/* per ruid resource consumption */
90 	struct prison	*cr_prison;	/* jail(2) */
91 	struct loginclass	*cr_loginclass; /* login class */
92 	void		*cr_pspare2[2];	/* general use 2 */
93 #define	cr_endcopy	cr_label
94 	struct label	*cr_label;	/* MAC label */
95 	gid_t	*cr_groups;		/* groups */
96 	int	cr_agroups;		/* Available groups */
97 	/* storage for small groups */
98 	gid_t   cr_smallgroups[CRED_SMALLGROUPS_NB];
99 };
100 #define	NOCRED	((struct ucred *)0)	/* no credential available */
101 #define	FSCRED	((struct ucred *)-1)	/* filesystem credential */
102 #endif /* _KERNEL || _WANT_UCRED */
103 
104 #define	XU_NGROUPS	16
105 
106 /*
107  * This is the external representation of struct ucred.
108  */
109 struct xucred {
110 	u_int	cr_version;		/* structure layout version */
111 	uid_t	cr_uid;			/* effective user id */
112 	short	cr_ngroups;		/* number of groups (incl. cr_gid). */
113 	union {
114 		/*
115 		 * The effective GID has been the first element of cr_groups[]
116 		 * for historical reasons.  It should be accessed using the
117 		 * 'cr_gid' identifier.  Supplementary groups should be accessed
118 		 * using cr_sgroups[].  Note that 'cr_ngroups' currently
119 		 * includes the effective GID.
120 		 *
121 		 * XXXOC: On the next API change (requires versioning), please
122 		 * replace this union with a true unaliased field 'cr_gid' and
123 		 * make sure that cr_groups[]/'cr_ngroups' only account for
124 		 * supplementary groups.
125 		 */
126 		struct {
127 			gid_t	cr_gid;		/* effective group id */
128 			gid_t	cr_sgroups[XU_NGROUPS - 1];
129 		};
130 		gid_t	cr_groups[XU_NGROUPS];	/* groups */
131 	};
132 	union {
133 		void	*_cr_unused1;	/* compatibility with old ucred */
134 		pid_t	cr_pid;
135 	};
136 };
137 #define	XUCRED_VERSION	0
138 
139 struct mac;
140 /*
141  * Structure to pass as an argument to the setcred() system call.
142  */
143 struct setcred {
144 	uid_t	 sc_uid;		/* effective user id */
145 	uid_t	 sc_ruid;		/* real user id */
146 	uid_t	 sc_svuid;		/* saved user id */
147 	gid_t	 sc_gid;		/* effective group id */
148 	gid_t	 sc_rgid;		/* real group id */
149 	gid_t	 sc_svgid;		/* saved group id */
150 	u_int	 sc_pad;		/* see 32-bit compat structure */
151 	u_int	 sc_supp_groups_nb;	/* number of supplementary groups */
152 	gid_t	*sc_supp_groups;	/* supplementary groups */
153 	struct mac *sc_label;		/* MAC label */
154 };
155 /*
156  * Initializer for 'struct setcred' variables.
157  */
158 #define	SETCRED_INITIALIZER	{ -1, -1, -1, -1, -1, -1, 0, 0, NULL, NULL }
159 
160 /*
161  * Flags to setcred().
162  */
163 #define	SETCREDF_UID		(1u << 0)
164 #define	SETCREDF_RUID		(1u << 1)
165 #define	SETCREDF_SVUID		(1u << 2)
166 #define	SETCREDF_GID		(1u << 3)
167 #define	SETCREDF_RGID		(1u << 4)
168 #define	SETCREDF_SVGID		(1u << 5)
169 #define	SETCREDF_SUPP_GROUPS	(1u << 6)
170 #define	SETCREDF_MAC_LABEL	(1u << 7)
171 
172 #ifdef _KERNEL
173 /*
174  * Masks of the currently valid flags to setcred().
175  *
176  * Please consider reserving some of the high bits in the 'flags' argument for
177  * versioning when almost all of them are in use.
178  */
179 #define	SETCREDF_MASK	(SETCREDF_UID | SETCREDF_RUID | SETCREDF_SVUID | \
180     SETCREDF_GID | SETCREDF_RGID | SETCREDF_SVGID | SETCREDF_SUPP_GROUPS | \
181     SETCREDF_MAC_LABEL)
182 
183 struct setcred32 {
184 #define	setcred32_copy_start	sc_uid
185 	uid_t	 sc_uid;
186 	uid_t	 sc_ruid;
187 	uid_t	 sc_svuid;
188 	gid_t	 sc_gid;
189 	gid_t	 sc_rgid;
190 	gid_t	 sc_svgid;
191 	u_int	 sc_pad;
192 	u_int	 sc_supp_groups_nb;
193 #define	setcred32_copy_end	sc_supp_groups
194 	uint32_t sc_supp_groups;	/* gid_t [*] */
195 	uint32_t sc_label;		/* struct mac32 [*] */
196 };
197 
198 struct thread;
199 
200 /* Common native and 32-bit compatibility entry point. */
201 int user_setcred(struct thread *td, const u_int flags,
202     const void *const uwcred, const size_t size, bool is_32bit);
203 
204 struct proc;
205 
206 struct credbatch {
207 	struct ucred *cred;
208 	u_int users;
209 	long ref;
210 };
211 
212 static inline void
credbatch_prep(struct credbatch * crb)213 credbatch_prep(struct credbatch *crb)
214 {
215 	crb->cred = NULL;
216 	crb->users = 0;
217 	crb->ref = 0;
218 }
219 void	credbatch_add(struct credbatch *crb, struct thread *td);
220 
221 static inline void
credbatch_process(struct credbatch * crb __unused)222 credbatch_process(struct credbatch *crb __unused)
223 {
224 
225 }
226 
227 void	credbatch_final(struct credbatch *crb);
228 
229 void	change_egid(struct ucred *newcred, gid_t egid);
230 void	change_euid(struct ucred *newcred, struct uidinfo *euip);
231 void	change_rgid(struct ucred *newcred, gid_t rgid);
232 void	change_ruid(struct ucred *newcred, struct uidinfo *ruip);
233 void	change_svgid(struct ucred *newcred, gid_t svgid);
234 void	change_svuid(struct ucred *newcred, uid_t svuid);
235 void	crcopy(struct ucred *dest, struct ucred *src);
236 struct ucred	*crcopysafe(struct proc *p, struct ucred *cr);
237 struct ucred	*crdup(struct ucred *cr);
238 void	crextend(struct ucred *cr, int n);
239 void	proc_set_cred(struct proc *p, struct ucred *newcred);
240 bool	proc_set_cred_enforce_proc_lim(struct proc *p, struct ucred *newcred);
241 void	proc_unset_cred(struct proc *p, bool decrement_proc_count);
242 void	crfree(struct ucred *cr);
243 struct ucred	*crcowsync(void);
244 struct ucred	*crget(void);
245 struct ucred	*crhold(struct ucred *cr);
246 struct ucred	*crcowget(struct ucred *cr);
247 void	crcowfree(struct thread *td);
248 void	cru2x(struct ucred *cr, struct xucred *xcr);
249 void	cru2xt(struct thread *td, struct xucred *xcr);
250 void	crsetgroups(struct ucred *cr, int ngrp, const gid_t *groups);
251 void	crsetgroups_and_egid(struct ucred *cr, int ngrp, const gid_t *groups,
252 	    const gid_t default_egid);
253 bool	cr_xids_subset(struct ucred *active_cred, struct ucred *obj_cred);
254 
255 /*
256  * Returns whether gid designates a primary group in cred.
257  */
258 static inline bool
group_is_primary(const gid_t gid,const struct ucred * const cred)259 group_is_primary(const gid_t gid, const struct ucred *const cred)
260 {
261 	return (gid == cred->cr_groups[0] || gid == cred->cr_rgid ||
262 	    gid == cred->cr_svgid);
263 }
264 bool	group_is_supplementary(const gid_t gid, const struct ucred *const cred);
265 bool	groupmember(gid_t gid, const struct ucred *cred);
266 bool	realgroupmember(gid_t gid, const struct ucred *cred);
267 
268 #else /* !_KERNEL */
269 
270 __BEGIN_DECLS
271 int	setcred(u_int flags, const struct setcred *wcred, size_t size);
272 __END_DECLS
273 
274 #endif /* _KERNEL */
275 
276 #endif /* !_SYS_UCRED_H_ */
277