xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/spl/spl_policy.c (revision 071ab5a1f3cbfd29c8fbec27f7e619418adaf074)
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_sys_config(cred_t * cr,int checkonly __unused)56 secpolicy_sys_config(cred_t *cr, int checkonly __unused)
57 {
58 
59 	return (priv_check_cred(cr, PRIV_ZFS_POOL_CONFIG));
60 }
61 
62 int
secpolicy_zinject(cred_t * cr)63 secpolicy_zinject(cred_t *cr)
64 {
65 
66 	return (priv_check_cred(cr, PRIV_ZFS_INJECT));
67 }
68 
69 int
secpolicy_fs_unmount(cred_t * cr,struct mount * vfsp __unused)70 secpolicy_fs_unmount(cred_t *cr, struct mount *vfsp __unused)
71 {
72 
73 	return (priv_check_cred(cr, PRIV_VFS_UNMOUNT));
74 }
75 
76 int
secpolicy_fs_owner(struct mount * mp,cred_t * cr)77 secpolicy_fs_owner(struct mount *mp, cred_t *cr)
78 {
79 
80 	if (zfs_super_owner) {
81 		if (cr->cr_uid == mp->mnt_cred->cr_uid &&
82 		    cr->cr_prison == mp->mnt_cred->cr_prison) {
83 			return (0);
84 		}
85 	}
86 	return (EPERM);
87 }
88 
89 /*
90  * This check is done in kern_link(), so we could just return 0 here.
91  */
92 extern int hardlink_check_uid;
93 int
secpolicy_basic_link(vnode_t * vp,cred_t * cr)94 secpolicy_basic_link(vnode_t *vp, cred_t *cr)
95 {
96 
97 	if (!hardlink_check_uid)
98 		return (0);
99 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
100 		return (0);
101 	return (priv_check_cred(cr, PRIV_VFS_LINK));
102 }
103 
104 int
secpolicy_vnode_stky_modify(cred_t * cr)105 secpolicy_vnode_stky_modify(cred_t *cr)
106 {
107 
108 	return (EPERM);
109 }
110 
111 int
secpolicy_vnode_remove(vnode_t * vp,cred_t * cr)112 secpolicy_vnode_remove(vnode_t *vp, cred_t *cr)
113 {
114 
115 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
116 		return (0);
117 	return (priv_check_cred(cr, PRIV_VFS_ADMIN));
118 }
119 
120 int
secpolicy_vnode_access(cred_t * cr,vnode_t * vp,uid_t owner,accmode_t accmode)121 secpolicy_vnode_access(cred_t *cr, vnode_t *vp, uid_t owner, accmode_t accmode)
122 {
123 
124 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
125 		return (0);
126 
127 	if ((accmode & VREAD) && priv_check_cred(cr, PRIV_VFS_READ) != 0)
128 		return (EACCES);
129 	if ((accmode & VWRITE) &&
130 	    priv_check_cred(cr, PRIV_VFS_WRITE) != 0) {
131 		return (EACCES);
132 	}
133 	if (accmode & VEXEC) {
134 		if (vp->v_type == VDIR) {
135 			if (priv_check_cred(cr, PRIV_VFS_LOOKUP) != 0)
136 				return (EACCES);
137 		} else {
138 			if (priv_check_cred(cr, PRIV_VFS_EXEC) != 0)
139 				return (EACCES);
140 		}
141 	}
142 	return (0);
143 }
144 
145 /*
146  * Like secpolicy_vnode_access() but we get the actual wanted mode and the
147  * current mode of the file, not the missing bits.
148  */
149 int
secpolicy_vnode_access2(cred_t * cr,vnode_t * vp,uid_t owner,accmode_t curmode,accmode_t wantmode)150 secpolicy_vnode_access2(cred_t *cr, vnode_t *vp, uid_t owner,
151     accmode_t curmode, accmode_t wantmode)
152 {
153 	accmode_t mode;
154 
155 	mode = ~curmode & wantmode;
156 
157 	if (mode == 0)
158 		return (0);
159 
160 	return (secpolicy_vnode_access(cr, vp, owner, mode));
161 }
162 
163 int
secpolicy_vnode_any_access(cred_t * cr,vnode_t * vp,uid_t owner)164 secpolicy_vnode_any_access(cred_t *cr, vnode_t *vp, uid_t owner)
165 {
166 	static int privs[] = {
167 	    PRIV_VFS_ADMIN,
168 	    PRIV_VFS_READ,
169 	    PRIV_VFS_WRITE,
170 	    PRIV_VFS_EXEC,
171 	    PRIV_VFS_LOOKUP
172 	};
173 	int i;
174 
175 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
176 		return (0);
177 
178 	/* Same as secpolicy_vnode_setdac */
179 	if (owner == cr->cr_uid)
180 		return (0);
181 
182 	for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
183 		int priv;
184 
185 		switch (priv = privs[i]) {
186 		case PRIV_VFS_EXEC:
187 			if (vp->v_type == VDIR)
188 				continue;
189 			break;
190 		case PRIV_VFS_LOOKUP:
191 			if (vp->v_type != VDIR)
192 				continue;
193 			break;
194 		}
195 		if (priv_check_cred(cr, priv) == 0)
196 			return (0);
197 	}
198 	return (EPERM);
199 }
200 
201 int
secpolicy_vnode_setdac(vnode_t * vp,cred_t * cr,uid_t owner)202 secpolicy_vnode_setdac(vnode_t *vp, cred_t *cr, uid_t owner)
203 {
204 
205 	if (owner == cr->cr_uid)
206 		return (0);
207 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
208 		return (0);
209 	return (priv_check_cred(cr, PRIV_VFS_ADMIN));
210 }
211 
212 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)213 secpolicy_vnode_setattr(cred_t *cr, vnode_t *vp, struct vattr *vap,
214     const struct vattr *ovap, int flags,
215     int unlocked_access(void *, int, cred_t *), void *node)
216 {
217 	int mask = vap->va_mask;
218 	int error;
219 
220 	if (mask & AT_SIZE) {
221 		if (vp->v_type == VDIR)
222 			return (EISDIR);
223 		error = unlocked_access(node, VWRITE, cr);
224 		if (error)
225 			return (error);
226 	}
227 	if (mask & AT_MODE) {
228 		/*
229 		 * If not the owner of the file then check privilege
230 		 * for two things: the privilege to set the mode at all
231 		 * and, if we're setting setuid, we also need permissions
232 		 * to add the set-uid bit, if we're not the owner.
233 		 * In the specific case of creating a set-uid root
234 		 * file, we need even more permissions.
235 		 */
236 		error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
237 		if (error)
238 			return (error);
239 		error = secpolicy_setid_setsticky_clear(vp, vap, ovap, cr);
240 		if (error)
241 			return (error);
242 	} else {
243 		vap->va_mode = ovap->va_mode;
244 	}
245 	if (mask & (AT_UID | AT_GID)) {
246 		error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
247 		if (error)
248 			return (error);
249 
250 		/*
251 		 * To change the owner of a file, or change the group of
252 		 * a file to a group of which we are not a member, the
253 		 * caller must have privilege.
254 		 */
255 		if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
256 		    ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
257 		    !groupmember(vap->va_gid, cr))) {
258 			if (secpolicy_fs_owner(vp->v_mount, cr) != 0) {
259 				error = priv_check_cred(cr, PRIV_VFS_CHOWN);
260 				if (error)
261 					return (error);
262 			}
263 		}
264 
265 		if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
266 		    ((mask & AT_GID) && vap->va_gid != ovap->va_gid)) {
267 			secpolicy_setid_clear(vap, vp, cr);
268 		}
269 	}
270 	if (mask & (AT_ATIME | AT_MTIME)) {
271 		/*
272 		 * From utimes(2):
273 		 * If times is NULL, ... The caller must be the owner of
274 		 * the file, have permission to write the file, or be the
275 		 * super-user.
276 		 * If times is non-NULL, ... The caller must be the owner of
277 		 * the file or be the super-user.
278 		 */
279 		error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
280 		if (error && (vap->va_vaflags & VA_UTIMES_NULL))
281 			error = unlocked_access(node, VWRITE, cr);
282 		if (error)
283 			return (error);
284 	}
285 	return (0);
286 }
287 
288 int
secpolicy_vnode_create_gid(cred_t * cr)289 secpolicy_vnode_create_gid(cred_t *cr)
290 {
291 
292 	return (EPERM);
293 }
294 
295 int
secpolicy_vnode_setids_setgids(vnode_t * vp,cred_t * cr,gid_t gid)296 secpolicy_vnode_setids_setgids(vnode_t *vp, cred_t *cr, gid_t gid)
297 {
298 
299 	if (groupmember(gid, cr))
300 		return (0);
301 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
302 		return (0);
303 	return (priv_check_cred(cr, PRIV_VFS_SETGID));
304 }
305 
306 int
secpolicy_vnode_setid_retain(znode_t * zp,cred_t * cr,boolean_t issuidroot __unused)307 secpolicy_vnode_setid_retain(znode_t *zp, cred_t *cr,
308     boolean_t issuidroot __unused)
309 {
310 
311 	if (secpolicy_fs_owner(ZTOV(zp)->v_mount, cr) == 0)
312 		return (0);
313 	return (priv_check_cred(cr, PRIV_VFS_RETAINSUGID));
314 }
315 
316 void
secpolicy_setid_clear(struct vattr * vap,vnode_t * vp,cred_t * cr)317 secpolicy_setid_clear(struct vattr *vap, vnode_t *vp, cred_t *cr)
318 {
319 
320 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
321 		return;
322 
323 	if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0) {
324 		if (priv_check_cred(cr, PRIV_VFS_RETAINSUGID)) {
325 			vap->va_mask |= AT_MODE;
326 			vap->va_mode &= ~(S_ISUID|S_ISGID);
327 		}
328 	}
329 }
330 
331 int
secpolicy_setid_setsticky_clear(vnode_t * vp,struct vattr * vap,const struct vattr * ovap,cred_t * cr)332 secpolicy_setid_setsticky_clear(vnode_t *vp, struct vattr *vap,
333     const struct vattr *ovap, cred_t *cr)
334 {
335 	int error;
336 
337 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
338 		return (0);
339 
340 	/*
341 	 * Privileged processes may set the sticky bit on non-directories,
342 	 * as well as set the setgid bit on a file with a group that the process
343 	 * is not a member of. Both of these are allowed in jail(8).
344 	 */
345 	if (vp->v_type != VDIR && (vap->va_mode & S_ISTXT)) {
346 		if (priv_check_cred(cr, PRIV_VFS_STICKYFILE))
347 			return (EFTYPE);
348 	}
349 	/*
350 	 * Check for privilege if attempting to set the
351 	 * group-id bit.
352 	 */
353 	if ((vap->va_mode & S_ISGID) != 0) {
354 		error = secpolicy_vnode_setids_setgids(vp, cr, ovap->va_gid);
355 		if (error)
356 			return (error);
357 	}
358 	/*
359 	 * Deny setting setuid if we are not the file owner.
360 	 */
361 	if ((vap->va_mode & S_ISUID) && ovap->va_uid != cr->cr_uid) {
362 		error = priv_check_cred(cr, PRIV_VFS_ADMIN);
363 		if (error)
364 			return (error);
365 	}
366 	return (0);
367 }
368 
369 int
secpolicy_fs_mount(cred_t * cr,vnode_t * mvp,struct mount * vfsp)370 secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct mount *vfsp)
371 {
372 
373 	return (priv_check_cred(cr, PRIV_VFS_MOUNT));
374 }
375 
376 int
secpolicy_vnode_owner(vnode_t * vp,cred_t * cr,uid_t owner)377 secpolicy_vnode_owner(vnode_t *vp, cred_t *cr, uid_t owner)
378 {
379 
380 	if (owner == cr->cr_uid)
381 		return (0);
382 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
383 		return (0);
384 
385 	/* XXX: vfs_suser()? */
386 	return (priv_check_cred(cr, PRIV_VFS_MOUNT_OWNER));
387 }
388 
389 int
secpolicy_vnode_chown(vnode_t * vp,cred_t * cr,uid_t owner)390 secpolicy_vnode_chown(vnode_t *vp, cred_t *cr, uid_t owner)
391 {
392 
393 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
394 		return (0);
395 	return (priv_check_cred(cr, PRIV_VFS_CHOWN));
396 }
397 
398 void
secpolicy_fs_mount_clearopts(cred_t * cr,struct mount * vfsp)399 secpolicy_fs_mount_clearopts(cred_t *cr, struct mount *vfsp)
400 {
401 
402 	if (priv_check_cred(cr, PRIV_VFS_MOUNT_NONUSER) != 0) {
403 		MNT_ILOCK(vfsp);
404 		vfsp->vfs_flag |= VFS_NOSETUID | MNT_USER;
405 		vfs_clearmntopt(vfsp, MNTOPT_SETUID);
406 		vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL, 0);
407 		MNT_IUNLOCK(vfsp);
408 	}
409 }
410 
411 /*
412  * Check privileges for setting xvattr attributes
413  */
414 int
secpolicy_xvattr(vnode_t * vp,xvattr_t * xvap,uid_t owner,cred_t * cr,vtype_t vtype)415 secpolicy_xvattr(vnode_t *vp, xvattr_t *xvap, uid_t owner, cred_t *cr,
416     vtype_t vtype)
417 {
418 
419 	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
420 		return (0);
421 	return (priv_check_cred(cr, PRIV_VFS_SYSFLAGS));
422 }
423 
424 int
secpolicy_smb(cred_t * cr)425 secpolicy_smb(cred_t *cr)
426 {
427 
428 	return (priv_check_cred(cr, PRIV_NETSMB));
429 }
430