xref: /freebsd-14-stable/sys/sys/ucred.h (revision fca3395674d41fded108e493c6b5695bb6dcd077)
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  *	@(#)ucred.h	8.4 (Berkeley) 1/9/95
32  */
33 
34 #ifndef _SYS_UCRED_H_
35 #define	_SYS_UCRED_H_
36 
37 #include <sys/types.h>
38 #if defined(_KERNEL) || defined(_WANT_UCRED)
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
41 #endif
42 #include <bsm/audit.h>
43 
44 #if defined(_KERNEL) || defined(_WANT_UCRED)
45 struct loginclass;
46 
47 /*
48  * Flags for cr_flags.
49  */
50 #define	CRED_FLAG_CAPMODE	0x00000001	/* In capability mode. */
51 
52 /*
53  * Number of groups inlined in 'struct ucred'.  It must stay reasonably low as
54  * it is also used by some functions to allocate an array of this size on the
55  * stack.
56  */
57 #define	CRED_SMALLGROUPS_NB	16
58 
59 struct label;
60 struct prison;
61 struct uidinfo;
62 
63 /*
64  * Credentials.
65  *
66  * Please do not inspect cr_uid directly to determine superuserness.  The
67  * priv(9) interface should be used to check for privilege.
68  *
69  * Lock reference:
70  *      c - cr_mtx
71  *
72  * Unmarked fields are constant after creation.
73  *
74  * See "Credential management" comment in kern_prot.c for more information.
75  */
76 struct ucred {
77 	struct mtx cr_mtx;
78 	long	cr_ref;			/* (c) reference count */
79 	u_int	cr_users;		/* (c) proc + thread using this cred */
80 	u_int	cr_flags;		/* credential flags */
81 	struct auditinfo_addr	cr_audit;	/* Audit properties. */
82 #define	cr_startcopy cr_uid
83 	uid_t	cr_uid;			/* effective user id */
84 	uid_t	cr_ruid;		/* real user id */
85 	uid_t	cr_svuid;		/* saved user id */
86 	/*
87 	 * XXXOC: On the next ABI change, please move 'cr_ngroups' out of the
88 	 * copied area (crcopy() already copes with this change).
89 	 */
90 	int	cr_ngroups;		/* number of groups */
91 	gid_t	cr_rgid;		/* real group id */
92 	gid_t	cr_svgid;		/* saved group id */
93 	struct uidinfo	*cr_uidinfo;	/* per euid resource consumption */
94 	struct uidinfo	*cr_ruidinfo;	/* per ruid resource consumption */
95 	struct prison	*cr_prison;	/* jail(2) */
96 	struct loginclass	*cr_loginclass; /* login class */
97 	void		*cr_pspare2[2];	/* general use 2 */
98 #define	cr_endcopy	cr_label
99 	struct label	*cr_label;	/* MAC label */
100 	gid_t	*cr_groups;		/* groups */
101 	int	cr_agroups;		/* Available groups */
102 	/* storage for small groups */
103 	gid_t   cr_smallgroups[CRED_SMALLGROUPS_NB];
104 };
105 #define	NOCRED	((struct ucred *)0)	/* no credential available */
106 #define	FSCRED	((struct ucred *)-1)	/* filesystem credential */
107 #endif /* _KERNEL || _WANT_UCRED */
108 
109 #define	XU_NGROUPS	16
110 
111 /*
112  * This is the external representation of struct ucred.
113  */
114 struct xucred {
115 	u_int	cr_version;		/* structure layout version */
116 	uid_t	cr_uid;			/* effective user id */
117 	short	cr_ngroups;		/* number of groups */
118 	gid_t	cr_groups[XU_NGROUPS];	/* groups */
119 	union {
120 		void	*_cr_unused1;	/* compatibility with old ucred */
121 		pid_t	cr_pid;
122 	};
123 };
124 #define	XUCRED_VERSION	0
125 
126 /* This can be used for both ucred and xucred structures. */
127 #define	cr_gid cr_groups[0]
128 
129 struct mac;
130 /*
131  * Structure to pass as an argument to the setcred() system call.
132  */
133 struct setcred {
134 	uid_t	 sc_uid;		/* effective user id */
135 	uid_t	 sc_ruid;		/* real user id */
136 	uid_t	 sc_svuid;		/* saved user id */
137 	gid_t	 sc_gid;		/* effective group id */
138 	gid_t	 sc_rgid;		/* real group id */
139 	gid_t	 sc_svgid;		/* saved group id */
140 	u_int	 sc_pad;		/* see 32-bit compat structure */
141 	u_int	 sc_supp_groups_nb;	/* number of supplementary groups */
142 	gid_t	*sc_supp_groups;	/* supplementary groups */
143 	struct mac *sc_label;		/* MAC label */
144 };
145 /*
146  * Initializer for 'struct setcred' variables.
147  */
148 #define	SETCRED_INITIALIZER	{ -1, -1, -1, -1, -1, -1, 0, 0, NULL, NULL }
149 
150 /*
151  * Flags to setcred().
152  */
153 #define	SETCREDF_UID		(1u << 0)
154 #define	SETCREDF_RUID		(1u << 1)
155 #define	SETCREDF_SVUID		(1u << 2)
156 #define	SETCREDF_GID		(1u << 3)
157 #define	SETCREDF_RGID		(1u << 4)
158 #define	SETCREDF_SVGID		(1u << 5)
159 #define	SETCREDF_SUPP_GROUPS	(1u << 6)
160 #define	SETCREDF_MAC_LABEL	(1u << 7)
161 
162 #ifdef _KERNEL
163 /*
164  * Masks of the currently valid flags to setcred().
165  *
166  * Please consider reserving some of the high bits in the 'flags' argument for
167  * versioning when almost all of them are in use.
168  */
169 #define	SETCREDF_MASK	(SETCREDF_UID | SETCREDF_RUID | SETCREDF_SVUID | \
170     SETCREDF_GID | SETCREDF_RGID | SETCREDF_SVGID | SETCREDF_SUPP_GROUPS | \
171     SETCREDF_MAC_LABEL)
172 
173 struct setcred32 {
174 #define	setcred32_copy_start	sc_uid
175 	uid_t	 sc_uid;
176 	uid_t	 sc_ruid;
177 	uid_t	 sc_svuid;
178 	gid_t	 sc_gid;
179 	gid_t	 sc_rgid;
180 	gid_t	 sc_svgid;
181 	u_int	 sc_pad;
182 	u_int	 sc_supp_groups_nb;
183 #define	setcred32_copy_end	sc_supp_groups
184 	uint32_t sc_supp_groups;	/* gid_t [*] */
185 	uint32_t sc_label;		/* struct mac32 [*] */
186 };
187 
188 struct thread;
189 
190 /* Common native and 32-bit compatibility entry point. */
191 int user_setcred(struct thread *td, const u_int flags,
192     const void *const uwcred, const size_t size, bool is_32bit);
193 
194 struct proc;
195 
196 struct credbatch {
197 	struct ucred *cred;
198 	u_int users;
199 	long ref;
200 };
201 
202 static inline void
credbatch_prep(struct credbatch * crb)203 credbatch_prep(struct credbatch *crb)
204 {
205 	crb->cred = NULL;
206 	crb->users = 0;
207 	crb->ref = 0;
208 }
209 void	credbatch_add(struct credbatch *crb, struct thread *td);
210 
211 static inline void
credbatch_process(struct credbatch * crb __unused)212 credbatch_process(struct credbatch *crb __unused)
213 {
214 
215 }
216 
217 void	credbatch_final(struct credbatch *crb);
218 
219 void	change_egid(struct ucred *newcred, gid_t egid);
220 void	change_euid(struct ucred *newcred, struct uidinfo *euip);
221 void	change_rgid(struct ucred *newcred, gid_t rgid);
222 void	change_ruid(struct ucred *newcred, struct uidinfo *ruip);
223 void	change_svgid(struct ucred *newcred, gid_t svgid);
224 void	change_svuid(struct ucred *newcred, uid_t svuid);
225 void	crcopy(struct ucred *dest, struct ucred *src);
226 struct ucred	*crcopysafe(struct proc *p, struct ucred *cr);
227 struct ucred	*crdup(struct ucred *cr);
228 void	crextend(struct ucred *cr, int n);
229 void	proc_set_cred(struct proc *p, struct ucred *newcred);
230 bool	proc_set_cred_enforce_proc_lim(struct proc *p, struct ucred *newcred);
231 void	proc_unset_cred(struct proc *p, bool decrement_proc_count);
232 void	crfree(struct ucred *cr);
233 struct ucred	*crcowsync(void);
234 struct ucred	*crget(void);
235 struct ucred	*crhold(struct ucred *cr);
236 struct ucred	*crcowget(struct ucred *cr);
237 void	crcowfree(struct thread *td);
238 void	cru2x(struct ucred *cr, struct xucred *xcr);
239 void	cru2xt(struct thread *td, struct xucred *xcr);
240 void	crsetgroups(struct ucred *cr, int ngrp, const gid_t *groups);
241 void	crsetgroups_fallback(struct ucred *cr, int ngrp, const gid_t *groups,
242 	    const gid_t fallback);
243 
244 /*
245  * Returns whether gid designates a primary group in cred.
246  */
247 static inline bool
group_is_primary(const gid_t gid,const struct ucred * const cred)248 group_is_primary(const gid_t gid, const struct ucred *const cred)
249 {
250 	return (gid == cred->cr_groups[0] || gid == cred->cr_rgid ||
251 	    gid == cred->cr_svgid);
252 }
253 bool	group_is_supplementary(const gid_t gid, const struct ucred *const cred);
254 bool	groupmember(gid_t gid, const struct ucred *cred);
255 bool	realgroupmember(gid_t gid, const struct ucred *cred);
256 
257 #else /* !_KERNEL */
258 
259 __BEGIN_DECLS
260 int	setcred(u_int flags, const struct setcred *wcred, size_t size);
261 __END_DECLS
262 
263 #endif /* _KERNEL */
264 
265 #endif /* !_SYS_UCRED_H_ */
266