1 /*-
2  * Copyright (c) 1999-2002, 2007-2008 Robert N. M. Watson
3  * Copyright (c) 2001-2005 Networks Associates Technology, Inc.
4  * Copyright (c) 2005 Tom Rhodes
5  * Copyright (c) 2006 SPARTA, Inc.
6  * All rights reserved.
7  *
8  * This software was developed by Robert Watson for the TrustedBSD Project.
9  * It was later enhanced by Tom Rhodes for the TrustedBSD Project.
10  *
11  * This software was developed for the FreeBSD Project in part by Network
12  * Associates Laboratories, the Security Research Division of Network
13  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
14  * as part of the DARPA CHATS research program.
15  *
16  * This software was enhanced by SPARTA ISSO under SPAWAR contract
17  * N66001-04-C-6019 ("SEFOS").
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/acl.h>
43 #include <sys/kernel.h>
44 #include <sys/jail.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/priv.h>
51 #include <sys/systm.h>
52 #include <sys/vnode.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <sys/stat.h>
56 
57 #include <security/mac/mac_policy.h>
58 #include <security/mac_bsdextended/mac_bsdextended.h>
59 #include <security/mac_bsdextended/ugidfw_internal.h>
60 
61 int
ugidfw_vnode_check_access(struct ucred * cred,struct vnode * vp,struct label * vplabel,accmode_t accmode)62 ugidfw_vnode_check_access(struct ucred *cred, struct vnode *vp,
63     struct label *vplabel, accmode_t accmode)
64 {
65 
66 	return (ugidfw_check_vp(cred, vp, ugidfw_accmode2mbi(accmode)));
67 }
68 
69 int
ugidfw_vnode_check_chdir(struct ucred * cred,struct vnode * dvp,struct label * dvplabel)70 ugidfw_vnode_check_chdir(struct ucred *cred, struct vnode *dvp,
71     struct label *dvplabel)
72 {
73 
74 	return (ugidfw_check_vp(cred, dvp, MBI_EXEC));
75 }
76 
77 int
ugidfw_vnode_check_chroot(struct ucred * cred,struct vnode * dvp,struct label * dvplabel)78 ugidfw_vnode_check_chroot(struct ucred *cred, struct vnode *dvp,
79     struct label *dvplabel)
80 {
81 
82 	return (ugidfw_check_vp(cred, dvp, MBI_EXEC));
83 }
84 
85 int
ugidfw_check_create_vnode(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct componentname * cnp,struct vattr * vap)86 ugidfw_check_create_vnode(struct ucred *cred, struct vnode *dvp,
87     struct label *dvplabel, struct componentname *cnp, struct vattr *vap)
88 {
89 
90 	return (ugidfw_check_vp(cred, dvp, MBI_WRITE));
91 }
92 
93 int
ugidfw_vnode_check_deleteacl(struct ucred * cred,struct vnode * vp,struct label * vplabel,acl_type_t type)94 ugidfw_vnode_check_deleteacl(struct ucred *cred, struct vnode *vp,
95     struct label *vplabel, acl_type_t type)
96 {
97 
98 	return (ugidfw_check_vp(cred, vp, MBI_ADMIN));
99 }
100 
101 int
ugidfw_vnode_check_deleteextattr(struct ucred * cred,struct vnode * vp,struct label * vplabel,int attrnamespace,const char * name)102 ugidfw_vnode_check_deleteextattr(struct ucred *cred, struct vnode *vp,
103     struct label *vplabel, int attrnamespace, const char *name)
104 {
105 
106 	return (ugidfw_check_vp(cred, vp, MBI_WRITE));
107 }
108 
109 int
ugidfw_vnode_check_exec(struct ucred * cred,struct vnode * vp,struct label * vplabel,struct image_params * imgp,struct label * execlabel)110 ugidfw_vnode_check_exec(struct ucred *cred, struct vnode *vp,
111     struct label *vplabel, struct image_params *imgp,
112     struct label *execlabel)
113 {
114 
115 	return (ugidfw_check_vp(cred, vp, MBI_READ|MBI_EXEC));
116 }
117 
118 int
ugidfw_vnode_check_getacl(struct ucred * cred,struct vnode * vp,struct label * vplabel,acl_type_t type)119 ugidfw_vnode_check_getacl(struct ucred *cred, struct vnode *vp,
120     struct label *vplabel, acl_type_t type)
121 {
122 
123 	return (ugidfw_check_vp(cred, vp, MBI_STAT));
124 }
125 
126 int
ugidfw_vnode_check_getextattr(struct ucred * cred,struct vnode * vp,struct label * vplabel,int attrnamespace,const char * name)127 ugidfw_vnode_check_getextattr(struct ucred *cred, struct vnode *vp,
128     struct label *vplabel, int attrnamespace, const char *name)
129 {
130 
131 	return (ugidfw_check_vp(cred, vp, MBI_READ));
132 }
133 
134 int
ugidfw_vnode_check_link(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * label,struct componentname * cnp)135 ugidfw_vnode_check_link(struct ucred *cred, struct vnode *dvp,
136     struct label *dvplabel, struct vnode *vp, struct label *label,
137     struct componentname *cnp)
138 {
139 	int error;
140 
141 	error = ugidfw_check_vp(cred, dvp, MBI_WRITE);
142 	if (error)
143 		return (error);
144 	error = ugidfw_check_vp(cred, vp, MBI_WRITE);
145 	if (error)
146 		return (error);
147 	return (0);
148 }
149 
150 int
ugidfw_vnode_check_listextattr(struct ucred * cred,struct vnode * vp,struct label * vplabel,int attrnamespace)151 ugidfw_vnode_check_listextattr(struct ucred *cred, struct vnode *vp,
152     struct label *vplabel, int attrnamespace)
153 {
154 
155 	return (ugidfw_check_vp(cred, vp, MBI_READ));
156 }
157 
158 int
ugidfw_vnode_check_lookup(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct componentname * cnp)159 ugidfw_vnode_check_lookup(struct ucred *cred, struct vnode *dvp,
160     struct label *dvplabel, struct componentname *cnp)
161 {
162 
163 	return (ugidfw_check_vp(cred, dvp, MBI_EXEC));
164 }
165 
166 int
ugidfw_vnode_check_open(struct ucred * cred,struct vnode * vp,struct label * vplabel,accmode_t accmode)167 ugidfw_vnode_check_open(struct ucred *cred, struct vnode *vp,
168     struct label *vplabel, accmode_t accmode)
169 {
170 
171 	return (ugidfw_check_vp(cred, vp, ugidfw_accmode2mbi(accmode)));
172 }
173 
174 int
ugidfw_vnode_check_readdir(struct ucred * cred,struct vnode * dvp,struct label * dvplabel)175 ugidfw_vnode_check_readdir(struct ucred *cred, struct vnode *dvp,
176     struct label *dvplabel)
177 {
178 
179 	return (ugidfw_check_vp(cred, dvp, MBI_READ));
180 }
181 
182 int
ugidfw_vnode_check_readdlink(struct ucred * cred,struct vnode * vp,struct label * vplabel)183 ugidfw_vnode_check_readdlink(struct ucred *cred, struct vnode *vp,
184     struct label *vplabel)
185 {
186 
187 	return (ugidfw_check_vp(cred, vp, MBI_READ));
188 }
189 
190 int
ugidfw_vnode_check_rename_from(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,struct componentname * cnp)191 ugidfw_vnode_check_rename_from(struct ucred *cred, struct vnode *dvp,
192     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
193     struct componentname *cnp)
194 {
195 	int error;
196 
197 	error = ugidfw_check_vp(cred, dvp, MBI_WRITE);
198 	if (error)
199 		return (error);
200 	return (ugidfw_check_vp(cred, vp, MBI_WRITE));
201 }
202 
203 int
ugidfw_vnode_check_rename_to(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,int samedir,struct componentname * cnp)204 ugidfw_vnode_check_rename_to(struct ucred *cred, struct vnode *dvp,
205     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
206     int samedir, struct componentname *cnp)
207 {
208 	int error;
209 
210 	error = ugidfw_check_vp(cred, dvp, MBI_WRITE);
211 	if (error)
212 		return (error);
213 	if (vp != NULL)
214 		error = ugidfw_check_vp(cred, vp, MBI_WRITE);
215 	return (error);
216 }
217 
218 int
ugidfw_vnode_check_revoke(struct ucred * cred,struct vnode * vp,struct label * vplabel)219 ugidfw_vnode_check_revoke(struct ucred *cred, struct vnode *vp,
220     struct label *vplabel)
221 {
222 
223 	return (ugidfw_check_vp(cred, vp, MBI_ADMIN));
224 }
225 
226 int
ugidfw_check_setacl_vnode(struct ucred * cred,struct vnode * vp,struct label * vplabel,acl_type_t type,struct acl * acl)227 ugidfw_check_setacl_vnode(struct ucred *cred, struct vnode *vp,
228     struct label *vplabel, acl_type_t type, struct acl *acl)
229 {
230 
231 	return (ugidfw_check_vp(cred, vp, MBI_ADMIN));
232 }
233 
234 int
ugidfw_vnode_check_setextattr(struct ucred * cred,struct vnode * vp,struct label * vplabel,int attrnamespace,const char * name)235 ugidfw_vnode_check_setextattr(struct ucred *cred, struct vnode *vp,
236     struct label *vplabel, int attrnamespace, const char *name)
237 {
238 
239 	return (ugidfw_check_vp(cred, vp, MBI_WRITE));
240 }
241 
242 int
ugidfw_vnode_check_setflags(struct ucred * cred,struct vnode * vp,struct label * vplabel,u_long flags)243 ugidfw_vnode_check_setflags(struct ucred *cred, struct vnode *vp,
244     struct label *vplabel, u_long flags)
245 {
246 
247 	return (ugidfw_check_vp(cred, vp, MBI_ADMIN));
248 }
249 
250 int
ugidfw_vnode_check_setmode(struct ucred * cred,struct vnode * vp,struct label * vplabel,mode_t mode)251 ugidfw_vnode_check_setmode(struct ucred *cred, struct vnode *vp,
252     struct label *vplabel, mode_t mode)
253 {
254 
255 	return (ugidfw_check_vp(cred, vp, MBI_ADMIN));
256 }
257 
258 int
ugidfw_vnode_check_setowner(struct ucred * cred,struct vnode * vp,struct label * vplabel,uid_t uid,gid_t gid)259 ugidfw_vnode_check_setowner(struct ucred *cred, struct vnode *vp,
260     struct label *vplabel, uid_t uid, gid_t gid)
261 {
262 
263 	return (ugidfw_check_vp(cred, vp, MBI_ADMIN));
264 }
265 
266 int
ugidfw_vnode_check_setutimes(struct ucred * cred,struct vnode * vp,struct label * vplabel,struct timespec atime,struct timespec utime)267 ugidfw_vnode_check_setutimes(struct ucred *cred, struct vnode *vp,
268     struct label *vplabel, struct timespec atime, struct timespec utime)
269 {
270 
271 	return (ugidfw_check_vp(cred, vp, MBI_ADMIN));
272 }
273 
274 int
ugidfw_vnode_check_stat(struct ucred * active_cred,struct ucred * file_cred,struct vnode * vp,struct label * vplabel)275 ugidfw_vnode_check_stat(struct ucred *active_cred,
276     struct ucred *file_cred, struct vnode *vp, struct label *vplabel)
277 {
278 
279 	return (ugidfw_check_vp(active_cred, vp, MBI_STAT));
280 }
281 
282 int
ugidfw_vnode_check_unlink(struct ucred * cred,struct vnode * dvp,struct label * dvplabel,struct vnode * vp,struct label * vplabel,struct componentname * cnp)283 ugidfw_vnode_check_unlink(struct ucred *cred, struct vnode *dvp,
284     struct label *dvplabel, struct vnode *vp, struct label *vplabel,
285     struct componentname *cnp)
286 {
287 	int error;
288 
289 	error = ugidfw_check_vp(cred, dvp, MBI_WRITE);
290 	if (error)
291 		return (error);
292 	return (ugidfw_check_vp(cred, vp, MBI_WRITE));
293 }
294