1/*-
2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/priv.h>
32#include <sys/vnode.h>
33#include <sys/mntent.h>
34#include <sys/mount.h>
35#include <sys/stat.h>
36#include <sys/jail.h>
37#include <sys/policy.h>
38#include <sys/zfs_vfsops.h>
39
40int
41secpolicy_nfs(cred_t *cr)
42{
43
44	return (priv_check_cred(cr, PRIV_NFS_DAEMON, 0));
45}
46
47int
48secpolicy_zfs(cred_t *cr)
49{
50
51	return (priv_check_cred(cr, PRIV_VFS_MOUNT, 0));
52}
53
54int
55secpolicy_sys_config(cred_t *cr, int checkonly __unused)
56{
57
58	return (priv_check_cred(cr, PRIV_ZFS_POOL_CONFIG, 0));
59}
60
61int
62secpolicy_zinject(cred_t *cr)
63{
64
65	return (priv_check_cred(cr, PRIV_ZFS_INJECT, 0));
66}
67
68int
69secpolicy_fs_unmount(cred_t *cr, struct mount *vfsp __unused)
70{
71
72	return (priv_check_cred(cr, PRIV_VFS_UNMOUNT, 0));
73}
74
75int
76secpolicy_fs_owner(struct mount *mp, cred_t *cr)
77{
78
79	if (zfs_super_owner) {
80		if (cr->cr_uid == mp->mnt_cred->cr_uid &&
81		    cr->cr_prison == mp->mnt_cred->cr_prison) {
82			return (0);
83		}
84	}
85	return (EPERM);
86}
87
88/*
89 * This check is done in kern_link(), so we could just return 0 here.
90 */
91extern int hardlink_check_uid;
92int
93secpolicy_basic_link(vnode_t *vp, cred_t *cr)
94{
95
96	if (!hardlink_check_uid)
97		return (0);
98	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
99		return (0);
100	return (priv_check_cred(cr, PRIV_VFS_LINK, 0));
101}
102
103int
104secpolicy_vnode_stky_modify(cred_t *cr)
105{
106
107	return (EPERM);
108}
109
110int
111secpolicy_vnode_remove(vnode_t *vp, cred_t *cr)
112{
113
114	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
115		return (0);
116	return (priv_check_cred(cr, PRIV_VFS_ADMIN, 0));
117}
118
119int
120secpolicy_vnode_access(cred_t *cr, vnode_t *vp, uid_t owner, accmode_t accmode)
121{
122
123	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
124		return (0);
125
126	if ((accmode & VREAD) && priv_check_cred(cr, PRIV_VFS_READ, 0) != 0)
127		return (EACCES);
128	if ((accmode & VWRITE) &&
129	    priv_check_cred(cr, PRIV_VFS_WRITE, 0) != 0) {
130		return (EACCES);
131	}
132	if (accmode & VEXEC) {
133		if (vp->v_type == VDIR) {
134			if (priv_check_cred(cr, PRIV_VFS_LOOKUP, 0) != 0)
135				return (EACCES);
136		} else {
137			if (priv_check_cred(cr, PRIV_VFS_EXEC, 0) != 0)
138				return (EACCES);
139		}
140	}
141	return (0);
142}
143
144/*
145 * Like secpolicy_vnode_access() but we get the actual wanted mode and the
146 * current mode of the file, not the missing bits.
147 */
148int
149secpolicy_vnode_access2(cred_t *cr, vnode_t *vp, uid_t owner,
150    accmode_t curmode, accmode_t wantmode)
151{
152	accmode_t mode;
153
154	mode = ~curmode & wantmode;
155
156	if (mode == 0)
157		return (0);
158
159	return (secpolicy_vnode_access(cr, vp, owner, mode));
160}
161
162int
163secpolicy_vnode_any_access(cred_t *cr, vnode_t *vp, uid_t owner)
164{
165	static int privs[] = {
166	    PRIV_VFS_ADMIN,
167	    PRIV_VFS_READ,
168	    PRIV_VFS_WRITE,
169	    PRIV_VFS_EXEC,
170	    PRIV_VFS_LOOKUP
171	};
172	int i;
173
174	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
175		return (0);
176
177	/* Same as secpolicy_vnode_setdac */
178	if (owner == cr->cr_uid)
179		return (0);
180
181	for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
182		boolean_t allzone = B_FALSE;
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) == 0)
196			return (0);
197	}
198	return (EPERM);
199}
200
201int
202secpolicy_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, 0));
210}
211
212int
213secpolicy_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 a file to a
252		 * group of which we are not a member, the caller must have
253		 * 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, 0);
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
288int
289secpolicy_vnode_create_gid(cred_t *cr)
290{
291
292	return (EPERM);
293}
294
295int
296secpolicy_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, 0));
304}
305
306int
307secpolicy_vnode_setid_retain(vnode_t *vp, cred_t *cr,
308    boolean_t issuidroot __unused)
309{
310
311	if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
312		return (0);
313	return (priv_check_cred(cr, PRIV_VFS_RETAINSUGID, 0));
314}
315
316void
317secpolicy_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, 0)) {
325			vap->va_mask |= AT_MODE;
326			vap->va_mode &= ~(S_ISUID|S_ISGID);
327		}
328	}
329}
330
331int
332secpolicy_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, 0))
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, 0);
363		if (error)
364			return (error);
365	}
366	return (0);
367}
368
369int
370secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct mount *vfsp)
371{
372
373	return (priv_check_cred(cr, PRIV_VFS_MOUNT, 0));
374}
375
376int
377secpolicy_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, 0));
387}
388
389int
390secpolicy_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, 0));
396}
397
398void
399secpolicy_fs_mount_clearopts(cred_t *cr, struct mount *vfsp)
400{
401
402	if (priv_check_cred(cr, PRIV_VFS_MOUNT_NONUSER, 0) != 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 */
414int
415secpolicy_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, 0));
422}
423
424int
425secpolicy_smb(cred_t *cr)
426{
427
428	return (priv_check_cred(cr, PRIV_NETSMB, 0));
429}
430