xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/spl/spl_policy.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/priv.h>
31 #include <sys/vnode.h>
32 #include <sys/mntent.h>
33 #include <sys/mount.h>
34 #include <sys/stat.h>
35 #include <sys/jail.h>
36 #include <sys/policy.h>
37 #include <sys/zfs_vfsops.h>
38 #include <sys/zfs_znode.h>
39 
40 
41 int
secpolicy_nfs(cred_t * cr)42 secpolicy_nfs(cred_t *cr)
43 {
44 
45 	return (priv_check_cred(cr, PRIV_NFS_DAEMON));
46 }
47 
48 int
secpolicy_zfs(cred_t * cr)49 secpolicy_zfs(cred_t *cr)
50 {
51 
52 	return (priv_check_cred(cr, PRIV_VFS_MOUNT));
53 }
54 
55 int
secpolicy_zfs_proc(cred_t * cr,proc_t * proc)56 secpolicy_zfs_proc(cred_t *cr, proc_t *proc)
57 {
58 
59 	return (priv_check_cred(cr, PRIV_VFS_MOUNT));
60 }
61 
62 int
secpolicy_sys_config(cred_t * cr,int checkonly __unused)63 secpolicy_sys_config(cred_t *cr, int checkonly __unused)
64 {
65 
66 	return (priv_check_cred(cr, PRIV_ZFS_POOL_CONFIG));
67 }
68 
69 int
secpolicy_zinject(cred_t * cr)70 secpolicy_zinject(cred_t *cr)
71 {
72 
73 	return (priv_check_cred(cr, PRIV_ZFS_INJECT));
74 }
75 
76 int
secpolicy_fs_unmount(cred_t * cr,struct mount * vfsp __unused)77 secpolicy_fs_unmount(cred_t *cr, struct mount *vfsp __unused)
78 {
79 
80 	return (priv_check_cred(cr, PRIV_VFS_UNMOUNT));
81 }
82 
83 int
secpolicy_fs_owner(struct mount * mp,cred_t * cr)84 secpolicy_fs_owner(struct mount *mp, cred_t *cr)
85 {
86 
87 	if (zfs_super_owner) {
88 		if (cr->cr_uid == mp->mnt_cred->cr_uid &&
89 		    cr->cr_prison == mp->mnt_cred->cr_prison) {
90 			return (0);
91 		}
92 	}
93 	return (EPERM);
94 }
95 
96 /*
97  * This check is done in kern_link(), so we could just return 0 here.
98  */
99 extern int hardlink_check_uid;
100 int
secpolicy_basic_link(vnode_t * vp,cred_t * cr)101 secpolicy_basic_link(vnode_t *vp, cred_t *cr)
102 {
103 
104 	if (!hardlink_check_uid)
105 		return (0);
106 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
107 		return (0);
108 	return (priv_check_cred(cr, PRIV_VFS_LINK));
109 }
110 
111 int
secpolicy_vnode_stky_modify(cred_t * cr)112 secpolicy_vnode_stky_modify(cred_t *cr)
113 {
114 
115 	return (EPERM);
116 }
117 
118 int
secpolicy_vnode_remove(vnode_t * vp,cred_t * cr)119 secpolicy_vnode_remove(vnode_t *vp, cred_t *cr)
120 {
121 
122 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
123 		return (0);
124 	return (priv_check_cred(cr, PRIV_VFS_ADMIN));
125 }
126 
127 int
secpolicy_vnode_access(cred_t * cr,vnode_t * vp,uid_t owner,accmode_t accmode)128 secpolicy_vnode_access(cred_t *cr, vnode_t *vp, uid_t owner, accmode_t accmode)
129 {
130 
131 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
132 		return (0);
133 
134 	if ((accmode & VREAD) && priv_check_cred(cr, PRIV_VFS_READ) != 0)
135 		return (EACCES);
136 	if ((accmode & VWRITE) &&
137 	    priv_check_cred(cr, PRIV_VFS_WRITE) != 0) {
138 		return (EACCES);
139 	}
140 	if (accmode & VEXEC) {
141 		if (vp->v_type == VDIR) {
142 			if (priv_check_cred(cr, PRIV_VFS_LOOKUP) != 0)
143 				return (EACCES);
144 		} else {
145 			if (priv_check_cred(cr, PRIV_VFS_EXEC) != 0)
146 				return (EACCES);
147 		}
148 	}
149 	return (0);
150 }
151 
152 /*
153  * Like secpolicy_vnode_access() but we get the actual wanted mode and the
154  * current mode of the file, not the missing bits.
155  */
156 int
secpolicy_vnode_access2(cred_t * cr,vnode_t * vp,uid_t owner,accmode_t curmode,accmode_t wantmode)157 secpolicy_vnode_access2(cred_t *cr, vnode_t *vp, uid_t owner,
158     accmode_t curmode, accmode_t wantmode)
159 {
160 	accmode_t mode;
161 
162 	mode = ~curmode & wantmode;
163 
164 	if (mode == 0)
165 		return (0);
166 
167 	return (secpolicy_vnode_access(cr, vp, owner, mode));
168 }
169 
170 int
secpolicy_vnode_any_access(cred_t * cr,vnode_t * vp,uid_t owner)171 secpolicy_vnode_any_access(cred_t *cr, vnode_t *vp, uid_t owner)
172 {
173 	static int privs[] = {
174 	    PRIV_VFS_ADMIN,
175 	    PRIV_VFS_READ,
176 	    PRIV_VFS_WRITE,
177 	    PRIV_VFS_EXEC,
178 	    PRIV_VFS_LOOKUP
179 	};
180 	int i;
181 
182 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
183 		return (0);
184 
185 	/* Same as secpolicy_vnode_setdac */
186 	if (owner == cr->cr_uid)
187 		return (0);
188 
189 	for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
190 		int priv;
191 
192 		switch (priv = privs[i]) {
193 		case PRIV_VFS_EXEC:
194 			if (vp->v_type == VDIR)
195 				continue;
196 			break;
197 		case PRIV_VFS_LOOKUP:
198 			if (vp->v_type != VDIR)
199 				continue;
200 			break;
201 		}
202 		if (priv_check_cred(cr, priv) == 0)
203 			return (0);
204 	}
205 	return (EPERM);
206 }
207 
208 int
secpolicy_vnode_setdac(vnode_t * vp,cred_t * cr,uid_t owner)209 secpolicy_vnode_setdac(vnode_t *vp, cred_t *cr, uid_t owner)
210 {
211 
212 	if (owner == cr->cr_uid)
213 		return (0);
214 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
215 		return (0);
216 	return (priv_check_cred(cr, PRIV_VFS_ADMIN));
217 }
218 
219 int
secpolicy_vnode_setattr(cred_t * cr,vnode_t * vp,struct vattr * vap,const struct vattr * ovap,int flags,int unlocked_access (void *,int,cred_t *),void * node)220 secpolicy_vnode_setattr(cred_t *cr, vnode_t *vp, struct vattr *vap,
221     const struct vattr *ovap, int flags,
222     int unlocked_access(void *, int, cred_t *), void *node)
223 {
224 	int mask = vap->va_mask;
225 	int error;
226 
227 	if (mask & AT_SIZE) {
228 		if (vp->v_type == VDIR)
229 			return (EISDIR);
230 		error = unlocked_access(node, VWRITE, cr);
231 		if (error)
232 			return (error);
233 	}
234 	if (mask & AT_MODE) {
235 		/*
236 		 * If not the owner of the file then check privilege
237 		 * for two things: the privilege to set the mode at all
238 		 * and, if we're setting setuid, we also need permissions
239 		 * to add the set-uid bit, if we're not the owner.
240 		 * In the specific case of creating a set-uid root
241 		 * file, we need even more permissions.
242 		 */
243 		error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
244 		if (error)
245 			return (error);
246 		error = secpolicy_setid_setsticky_clear(vp, vap, ovap, cr);
247 		if (error)
248 			return (error);
249 	} else {
250 		vap->va_mode = ovap->va_mode;
251 	}
252 	if (mask & (AT_UID | AT_GID)) {
253 		error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
254 		if (error)
255 			return (error);
256 
257 		/*
258 		 * To change the owner of a file, or change the group of
259 		 * a file to a group of which we are not a member, the
260 		 * caller must have privilege.
261 		 */
262 		if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
263 		    ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
264 		    !groupmember(vap->va_gid, cr))) {
265 			if (secpolicy_fs_owner(vp->v_mount, cr) != 0) {
266 				error = priv_check_cred(cr, PRIV_VFS_CHOWN);
267 				if (error)
268 					return (error);
269 			}
270 		}
271 
272 		if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
273 		    ((mask & AT_GID) && vap->va_gid != ovap->va_gid)) {
274 			secpolicy_setid_clear(vap, vp, cr);
275 		}
276 	}
277 	if (mask & (AT_ATIME | AT_MTIME)) {
278 		/*
279 		 * From utimes(2):
280 		 * If times is NULL, ... The caller must be the owner of
281 		 * the file, have permission to write the file, or be the
282 		 * super-user.
283 		 * If times is non-NULL, ... The caller must be the owner of
284 		 * the file or be the super-user.
285 		 */
286 		error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
287 		if (error && (vap->va_vaflags & VA_UTIMES_NULL))
288 			error = unlocked_access(node, VWRITE, cr);
289 		if (error)
290 			return (error);
291 	}
292 	return (0);
293 }
294 
295 int
secpolicy_vnode_create_gid(cred_t * cr)296 secpolicy_vnode_create_gid(cred_t *cr)
297 {
298 
299 	return (EPERM);
300 }
301 
302 int
secpolicy_vnode_setids_setgids(vnode_t * vp,cred_t * cr,gid_t gid)303 secpolicy_vnode_setids_setgids(vnode_t *vp, cred_t *cr, gid_t gid)
304 {
305 
306 	if (groupmember(gid, cr))
307 		return (0);
308 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
309 		return (0);
310 	return (priv_check_cred(cr, PRIV_VFS_SETGID));
311 }
312 
313 int
secpolicy_vnode_setid_retain(znode_t * zp,cred_t * cr,boolean_t issuidroot __unused)314 secpolicy_vnode_setid_retain(znode_t *zp, cred_t *cr,
315     boolean_t issuidroot __unused)
316 {
317 
318 	if (secpolicy_fs_owner(ZTOV(zp)->v_mount, cr) == 0)
319 		return (0);
320 	return (priv_check_cred(cr, PRIV_VFS_RETAINSUGID));
321 }
322 
323 void
secpolicy_setid_clear(struct vattr * vap,vnode_t * vp,cred_t * cr)324 secpolicy_setid_clear(struct vattr *vap, vnode_t *vp, cred_t *cr)
325 {
326 
327 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
328 		return;
329 
330 	if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0) {
331 		if (priv_check_cred(cr, PRIV_VFS_RETAINSUGID)) {
332 			vap->va_mask |= AT_MODE;
333 			vap->va_mode &= ~(S_ISUID|S_ISGID);
334 		}
335 	}
336 }
337 
338 int
secpolicy_setid_setsticky_clear(vnode_t * vp,struct vattr * vap,const struct vattr * ovap,cred_t * cr)339 secpolicy_setid_setsticky_clear(vnode_t *vp, struct vattr *vap,
340     const struct vattr *ovap, cred_t *cr)
341 {
342 	int error;
343 
344 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
345 		return (0);
346 
347 	/*
348 	 * Privileged processes may set the sticky bit on non-directories,
349 	 * as well as set the setgid bit on a file with a group that the process
350 	 * is not a member of. Both of these are allowed in jail(8).
351 	 */
352 	if (vp->v_type != VDIR && (vap->va_mode & S_ISTXT)) {
353 		if (priv_check_cred(cr, PRIV_VFS_STICKYFILE))
354 			return (EFTYPE);
355 	}
356 	/*
357 	 * Check for privilege if attempting to set the
358 	 * group-id bit.
359 	 */
360 	if ((vap->va_mode & S_ISGID) != 0) {
361 		error = secpolicy_vnode_setids_setgids(vp, cr, ovap->va_gid);
362 		if (error)
363 			return (error);
364 	}
365 	/*
366 	 * Deny setting setuid if we are not the file owner.
367 	 */
368 	if ((vap->va_mode & S_ISUID) && ovap->va_uid != cr->cr_uid) {
369 		error = priv_check_cred(cr, PRIV_VFS_ADMIN);
370 		if (error)
371 			return (error);
372 	}
373 	return (0);
374 }
375 
376 int
secpolicy_fs_mount(cred_t * cr,vnode_t * mvp,struct mount * vfsp)377 secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct mount *vfsp)
378 {
379 
380 	return (priv_check_cred(cr, PRIV_VFS_MOUNT));
381 }
382 
383 int
secpolicy_vnode_owner(vnode_t * vp,cred_t * cr,uid_t owner)384 secpolicy_vnode_owner(vnode_t *vp, cred_t *cr, uid_t owner)
385 {
386 
387 	if (owner == cr->cr_uid)
388 		return (0);
389 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
390 		return (0);
391 
392 	/* XXX: vfs_suser()? */
393 	return (priv_check_cred(cr, PRIV_VFS_MOUNT_OWNER));
394 }
395 
396 int
secpolicy_vnode_chown(vnode_t * vp,cred_t * cr,uid_t owner)397 secpolicy_vnode_chown(vnode_t *vp, cred_t *cr, uid_t owner)
398 {
399 
400 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
401 		return (0);
402 	return (priv_check_cred(cr, PRIV_VFS_CHOWN));
403 }
404 
405 void
secpolicy_fs_mount_clearopts(cred_t * cr,struct mount * vfsp)406 secpolicy_fs_mount_clearopts(cred_t *cr, struct mount *vfsp)
407 {
408 
409 	if (priv_check_cred(cr, PRIV_VFS_MOUNT_NONUSER) != 0) {
410 		MNT_ILOCK(vfsp);
411 		vfsp->vfs_flag |= VFS_NOSETUID | MNT_USER;
412 		vfs_clearmntopt(vfsp, MNTOPT_SETUID);
413 		vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL, 0);
414 		MNT_IUNLOCK(vfsp);
415 	}
416 }
417 
418 /*
419  * Check privileges for setting xvattr attributes
420  */
421 int
secpolicy_xvattr(vnode_t * vp,xvattr_t * xvap,uid_t owner,cred_t * cr,vtype_t vtype)422 secpolicy_xvattr(vnode_t *vp, xvattr_t *xvap, uid_t owner, cred_t *cr,
423     vtype_t vtype)
424 {
425 
426 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
427 		return (0);
428 	return (priv_check_cred(cr, PRIV_VFS_SYSFLAGS));
429 }
430 
431 int
secpolicy_smb(cred_t * cr)432 secpolicy_smb(cred_t *cr)
433 {
434 
435 	return (priv_check_cred(cr, PRIV_NETSMB));
436 }
437