xref: /freebsd/sys/sys/ucred.h (revision f61c48f4403fc8c69f9b933d3f1bd9663008b0eb)
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 		 * Special little hack to avoid needing a cr_gid macro, which
116 		 * would cause problems if one were to use it with struct ucred
117 		 * which also has a cr_groups member.
118 		 */
119 		struct {
120 			gid_t	cr_gid;		/* effective group id */
121 			gid_t	cr_sgroups[XU_NGROUPS - 1];
122 		};
123 
124 		gid_t	cr_groups[XU_NGROUPS];	/* groups */
125 	};
126 	union {
127 		void	*_cr_unused1;	/* compatibility with old ucred */
128 		pid_t	cr_pid;
129 	};
130 };
131 #define	XUCRED_VERSION	0
132 
133 struct mac;
134 /*
135  * Structure to pass as an argument to the setcred() system call.
136  */
137 struct setcred {
138 	uid_t	 sc_uid;		/* effective user id */
139 	uid_t	 sc_ruid;		/* real user id */
140 	uid_t	 sc_svuid;		/* saved user id */
141 	gid_t	 sc_gid;		/* effective group id */
142 	gid_t	 sc_rgid;		/* real group id */
143 	gid_t	 sc_svgid;		/* saved group id */
144 	u_int	 sc_pad;		/* see 32-bit compat structure */
145 	u_int	 sc_supp_groups_nb;	/* number of supplementary groups */
146 	gid_t	*sc_supp_groups;	/* supplementary groups */
147 	struct mac *sc_label;		/* MAC label */
148 };
149 /*
150  * Initializer for 'struct setcred' variables.
151  */
152 #define	SETCRED_INITIALIZER	{ -1, -1, -1, -1, -1, -1, 0, 0, NULL, NULL }
153 
154 /*
155  * Flags to setcred().
156  */
157 #define	SETCREDF_UID		(1u << 0)
158 #define	SETCREDF_RUID		(1u << 1)
159 #define	SETCREDF_SVUID		(1u << 2)
160 #define	SETCREDF_GID		(1u << 3)
161 #define	SETCREDF_RGID		(1u << 4)
162 #define	SETCREDF_SVGID		(1u << 5)
163 #define	SETCREDF_SUPP_GROUPS	(1u << 6)
164 #define	SETCREDF_MAC_LABEL	(1u << 7)
165 
166 #ifdef _KERNEL
167 /*
168  * Masks of the currently valid flags to setcred().
169  *
170  * Please consider reserving some of the high bits in the 'flags' argument for
171  * versioning when almost all of them are in use.
172  */
173 #define	SETCREDF_MASK	(SETCREDF_UID | SETCREDF_RUID | SETCREDF_SVUID | \
174     SETCREDF_GID | SETCREDF_RGID | SETCREDF_SVGID | SETCREDF_SUPP_GROUPS | \
175     SETCREDF_MAC_LABEL)
176 
177 struct setcred32 {
178 #define	setcred32_copy_start	sc_uid
179 	uid_t	 sc_uid;
180 	uid_t	 sc_ruid;
181 	uid_t	 sc_svuid;
182 	gid_t	 sc_gid;
183 	gid_t	 sc_rgid;
184 	gid_t	 sc_svgid;
185 	u_int	 sc_pad;
186 	u_int	 sc_supp_groups_nb;
187 #define	setcred32_copy_end	sc_supp_groups
188 	uint32_t sc_supp_groups;	/* gid_t [*] */
189 	uint32_t sc_label;		/* struct mac32 [*] */
190 };
191 
192 struct thread;
193 
194 /* Common native and 32-bit compatibility entry point. */
195 int user_setcred(struct thread *td, const u_int flags,
196     const void *const uwcred, const size_t size, bool is_32bit);
197 
198 struct proc;
199 
200 struct credbatch {
201 	struct ucred *cred;
202 	u_int users;
203 	long ref;
204 };
205 
206 static inline void
credbatch_prep(struct credbatch * crb)207 credbatch_prep(struct credbatch *crb)
208 {
209 	crb->cred = NULL;
210 	crb->users = 0;
211 	crb->ref = 0;
212 }
213 void	credbatch_add(struct credbatch *crb, struct thread *td);
214 
215 static inline void
credbatch_process(struct credbatch * crb __unused)216 credbatch_process(struct credbatch *crb __unused)
217 {
218 
219 }
220 
221 void	credbatch_final(struct credbatch *crb);
222 
223 void	change_egid(struct ucred *newcred, gid_t egid);
224 void	change_euid(struct ucred *newcred, struct uidinfo *euip);
225 void	change_rgid(struct ucred *newcred, gid_t rgid);
226 void	change_ruid(struct ucred *newcred, struct uidinfo *ruip);
227 void	change_svgid(struct ucred *newcred, gid_t svgid);
228 void	change_svuid(struct ucred *newcred, uid_t svuid);
229 void	crcopy(struct ucred *dest, struct ucred *src);
230 struct ucred	*crcopysafe(struct proc *p, struct ucred *cr);
231 struct ucred	*crdup(struct ucred *cr);
232 void	crextend(struct ucred *cr, int n);
233 void	proc_set_cred(struct proc *p, struct ucred *newcred);
234 bool	proc_set_cred_enforce_proc_lim(struct proc *p, struct ucred *newcred);
235 void	proc_unset_cred(struct proc *p, bool decrement_proc_count);
236 void	crfree(struct ucred *cr);
237 struct ucred	*crcowsync(void);
238 struct ucred	*crget(void);
239 struct ucred	*crhold(struct ucred *cr);
240 struct ucred	*crcowget(struct ucred *cr);
241 void	crcowfree(struct thread *td);
242 void	cru2x(struct ucred *cr, struct xucred *xcr);
243 void	cru2xt(struct thread *td, struct xucred *xcr);
244 void	crsetgroups(struct ucred *cr, int ngrp, const gid_t *groups);
245 void	crsetgroups_and_egid(struct ucred *cr, int ngrp, const gid_t *groups,
246 	    const gid_t default_egid);
247 bool	cr_xids_subset(struct ucred *active_cred, struct ucred *obj_cred);
248 
249 /*
250  * Returns whether gid designates a primary group in cred.
251  */
252 static inline bool
group_is_primary(const gid_t gid,const struct ucred * const cred)253 group_is_primary(const gid_t gid, const struct ucred *const cred)
254 {
255 	return (gid == cred->cr_groups[0] || gid == cred->cr_rgid ||
256 	    gid == cred->cr_svgid);
257 }
258 bool	group_is_supplementary(const gid_t gid, const struct ucred *const cred);
259 bool	groupmember(gid_t gid, const struct ucred *cred);
260 bool	realgroupmember(gid_t gid, const struct ucred *cred);
261 
262 #else /* !_KERNEL */
263 
264 __BEGIN_DECLS
265 int	setcred(u_int flags, const struct setcred *wcred, size_t size);
266 __END_DECLS
267 
268 #endif /* _KERNEL */
269 
270 #endif /* !_SYS_UCRED_H_ */
271