xref: /freebsd-13-stable/sys/sys/ucred.h (revision f872814e2d7a8841411569fc707b028463c7656b)
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 struct loginclass;
45 
46 #define	XU_NGROUPS	16
47 
48 #if defined(_KERNEL) || defined(_WANT_UCRED)
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 /*
57  * Credentials.
58  *
59  * Please do not inspect cr_uid directly to determine superuserness.  The
60  * priv(9) interface should be used to check for privilege.
61  *
62  * Lock reference:
63  *      c - cr_mtx
64  *
65  * Unmarked fields are constant after creation.
66  *
67  * See "Credential management" comment in kern_prot.c for more information.
68  */
69 struct ucred {
70 	struct mtx cr_mtx;
71 	u_int	cr_ref;			/* (c) reference count */
72 	u_int	cr_users;		/* (c) proc + thread using this cred */
73 	struct auditinfo_addr	cr_audit;	/* Audit properties. */
74 #define	cr_startcopy cr_uid
75 	uid_t	cr_uid;			/* effective user id */
76 	uid_t	cr_ruid;		/* real user id */
77 	uid_t	cr_svuid;		/* saved user id */
78 	/*
79 	 * XXXOC: On the next ABI change, please move 'cr_ngroups' out of the
80 	 * copied area (crcopy() already copes with this change).
81 	 */
82 	int	cr_ngroups;		/* number of groups */
83 	gid_t	cr_rgid;		/* real group id */
84 	gid_t	cr_svgid;		/* saved group id */
85 	struct uidinfo	*cr_uidinfo;	/* per euid resource consumption */
86 	struct uidinfo	*cr_ruidinfo;	/* per ruid resource consumption */
87 	struct prison	*cr_prison;	/* jail(2) */
88 	struct loginclass	*cr_loginclass; /* login class */
89 	u_int		cr_flags;	/* credential flags */
90 	void		*cr_pspare2[2];	/* general use 2 */
91 #define	cr_endcopy	cr_label
92 	struct label	*cr_label;	/* MAC label */
93 	gid_t	*cr_groups;		/* groups */
94 	int	cr_agroups;		/* Available groups */
95 	/* storage for small groups */
96 	gid_t   cr_smallgroups[CRED_SMALLGROUPS_NB];
97 };
98 #define	NOCRED	((struct ucred *)0)	/* no credential available */
99 #define	FSCRED	((struct ucred *)-1)	/* filesystem credential */
100 #endif /* _KERNEL || _WANT_UCRED */
101 
102 /*
103  * Flags for cr_flags.
104  */
105 #define	CRED_FLAG_CAPMODE	0x00000001	/* In capability mode. */
106 
107 /*
108  * This is the external representation of struct ucred.
109  */
110 struct xucred {
111 	u_int	cr_version;		/* structure layout version */
112 	uid_t	cr_uid;			/* effective user id */
113 	short	cr_ngroups;		/* number of groups */
114 	gid_t	cr_groups[XU_NGROUPS];	/* groups */
115 	union {
116 		void	*_cr_unused1;	/* compatibility with old ucred */
117 		pid_t	cr_pid;
118 	};
119 };
120 #define	XUCRED_VERSION	0
121 
122 /* This can be used for both ucred and xucred structures. */
123 #define	cr_gid cr_groups[0]
124 
125 #ifdef _KERNEL
126 struct proc;
127 struct thread;
128 
129 struct credbatch {
130 	struct ucred *cred;
131 	int users;
132 	int ref;
133 };
134 
135 static inline void
credbatch_prep(struct credbatch * crb)136 credbatch_prep(struct credbatch *crb)
137 {
138 	crb->cred = NULL;
139 	crb->users = 0;
140 	crb->ref = 0;
141 }
142 void	credbatch_add(struct credbatch *crb, struct thread *td);
143 
144 static inline void
credbatch_process(struct credbatch * crb __unused)145 credbatch_process(struct credbatch *crb __unused)
146 {
147 
148 }
149 
150 void	credbatch_final(struct credbatch *crb);
151 
152 void	change_egid(struct ucred *newcred, gid_t egid);
153 void	change_euid(struct ucred *newcred, struct uidinfo *euip);
154 void	change_rgid(struct ucred *newcred, gid_t rgid);
155 void	change_ruid(struct ucred *newcred, struct uidinfo *ruip);
156 void	change_svgid(struct ucred *newcred, gid_t svgid);
157 void	change_svuid(struct ucred *newcred, uid_t svuid);
158 void	crcopy(struct ucred *dest, struct ucred *src);
159 struct ucred	*crcopysafe(struct proc *p, struct ucred *cr);
160 struct ucred	*crdup(struct ucred *cr);
161 void	crextend(struct ucred *cr, int n);
162 void	proc_set_cred(struct proc *p, struct ucred *newcred);
163 bool	proc_set_cred_enforce_proc_lim(struct proc *p, struct ucred *newcred);
164 void	proc_unset_cred(struct proc *p, bool decrement_proc_count);
165 void	crfree(struct ucred *cr);
166 struct ucred	*crcowsync(void);
167 struct ucred	*crget(void);
168 struct ucred	*crhold(struct ucred *cr);
169 struct ucred	*crcowget(struct ucred *cr);
170 void	crcowfree(struct thread *td);
171 void	cru2x(struct ucred *cr, struct xucred *xcr);
172 void	cru2xt(struct thread *td, struct xucred *xcr);
173 void	crsetgroups(struct ucred *cr, int ngrp, const gid_t *groups);
174 void	crsetgroups_fallback(struct ucred *cr, int ngrp, const gid_t *groups,
175 	    const gid_t fallback);
176 
177 /*
178  * Returns whether gid designates a primary group in cred.
179  */
180 static inline int
group_is_primary(const gid_t gid,const struct ucred * const cred)181 group_is_primary(const gid_t gid, const struct ucred *const cred)
182 {
183 	return (gid == cred->cr_groups[0] || gid == cred->cr_rgid ||
184 	    gid == cred->cr_svgid);
185 }
186 int	group_is_supplementary(const gid_t gid, const struct ucred *const cred);
187 int	groupmember(gid_t gid, const struct ucred *cred);
188 int	realgroupmember(gid_t gid, const struct ucred *cred);
189 #endif /* _KERNEL */
190 
191 #endif /* !_SYS_UCRED_H_ */
192