2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/vnode.h>
33 #include <sys/mntent.h>
34 #include <sys/mount.h>
37 #include <sys/policy.h>
38 #include <sys/zfs_vfsops.h>
41 secpolicy_nfs(cred_t *cr)
44 return (priv_check_cred(cr, PRIV_NFS_DAEMON, 0));
48 secpolicy_zfs(cred_t *cr)
51 return (priv_check_cred(cr, PRIV_VFS_MOUNT, 0));
55 secpolicy_sys_config(cred_t *cr, int checkonly __unused)
58 return (priv_check_cred(cr, PRIV_ZFS_POOL_CONFIG, 0));
62 secpolicy_zinject(cred_t *cr)
65 return (priv_check_cred(cr, PRIV_ZFS_INJECT, 0));
69 secpolicy_fs_unmount(cred_t *cr, struct mount *vfsp __unused)
72 return (priv_check_cred(cr, PRIV_VFS_UNMOUNT, 0));
76 secpolicy_fs_owner(struct mount *mp, cred_t *cr)
79 if (zfs_super_owner) {
80 if (cr->cr_uid == mp->mnt_cred->cr_uid &&
81 cr->cr_prison == mp->mnt_cred->cr_prison) {
89 * This check is done in kern_link(), so we could just return 0 here.
91 extern int hardlink_check_uid;
93 secpolicy_basic_link(vnode_t *vp, cred_t *cr)
96 if (!hardlink_check_uid)
98 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
100 return (priv_check_cred(cr, PRIV_VFS_LINK, 0));
104 secpolicy_vnode_stky_modify(cred_t *cr)
111 secpolicy_vnode_remove(vnode_t *vp, cred_t *cr)
114 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
116 return (priv_check_cred(cr, PRIV_VFS_ADMIN, 0));
120 secpolicy_vnode_access(cred_t *cr, vnode_t *vp, uid_t owner, accmode_t accmode)
123 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
126 if ((accmode & VREAD) && priv_check_cred(cr, PRIV_VFS_READ, 0) != 0)
128 if ((accmode & VWRITE) &&
129 priv_check_cred(cr, PRIV_VFS_WRITE, 0) != 0) {
132 if (accmode & VEXEC) {
133 if (vp->v_type == VDIR) {
134 if (priv_check_cred(cr, PRIV_VFS_LOOKUP, 0) != 0)
137 if (priv_check_cred(cr, PRIV_VFS_EXEC, 0) != 0)
145 * Like secpolicy_vnode_access() but we get the actual wanted mode and the
146 * current mode of the file, not the missing bits.
149 secpolicy_vnode_access2(cred_t *cr, vnode_t *vp, uid_t owner,
150 accmode_t curmode, accmode_t wantmode)
154 mode = ~curmode & wantmode;
159 return (secpolicy_vnode_access(cr, vp, owner, mode));
163 secpolicy_vnode_any_access(cred_t *cr, vnode_t *vp, uid_t owner)
165 static int privs[] = {
174 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
177 /* Same as secpolicy_vnode_setdac */
178 if (owner == cr->cr_uid)
181 for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
182 boolean_t allzone = B_FALSE;
185 switch (priv = privs[i]) {
187 if (vp->v_type == VDIR)
190 case PRIV_VFS_LOOKUP:
191 if (vp->v_type != VDIR)
195 if (priv_check_cred(cr, priv, 0) == 0)
202 secpolicy_vnode_setdac(vnode_t *vp, cred_t *cr, uid_t owner)
205 if (owner == cr->cr_uid)
207 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
209 return (priv_check_cred(cr, PRIV_VFS_ADMIN, 0));
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)
217 int mask = vap->va_mask;
220 if (mask & AT_SIZE) {
221 if (vp->v_type == VDIR)
223 error = unlocked_access(node, VWRITE, cr);
227 if (mask & AT_MODE) {
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.
236 error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
239 error = secpolicy_setid_setsticky_clear(vp, vap, ovap, cr);
243 vap->va_mode = ovap->va_mode;
245 if (mask & (AT_UID | AT_GID)) {
246 error = secpolicy_vnode_setdac(vp, cr, ovap->va_uid);
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
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);
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);
270 if (mask & (AT_ATIME | AT_MTIME)) {
273 * If times is NULL, ... The caller must be the owner of
274 * the file, have permission to write the file, or be the
276 * If times is non-NULL, ... The caller must be the owner of
277 * the file or be the super-user.
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);
289 secpolicy_vnode_create_gid(cred_t *cr)
296 secpolicy_vnode_setids_setgids(vnode_t *vp, cred_t *cr, gid_t gid)
299 if (groupmember(gid, cr))
301 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
303 return (priv_check_cred(cr, PRIV_VFS_SETGID, 0));
307 secpolicy_vnode_setid_retain(vnode_t *vp, cred_t *cr,
308 boolean_t issuidroot __unused)
311 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
313 return (priv_check_cred(cr, PRIV_VFS_RETAINSUGID, 0));
317 secpolicy_setid_clear(struct vattr *vap, vnode_t *vp, cred_t *cr)
320 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
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);
332 secpolicy_setid_setsticky_clear(vnode_t *vp, struct vattr *vap,
333 const struct vattr *ovap, cred_t *cr)
337 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
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).
345 if (vp->v_type != VDIR && (vap->va_mode & S_ISTXT)) {
346 if (priv_check_cred(cr, PRIV_VFS_STICKYFILE, 0))
350 * Check for privilege if attempting to set the
353 if ((vap->va_mode & S_ISGID) != 0) {
354 error = secpolicy_vnode_setids_setgids(vp, cr, ovap->va_gid);
359 * Deny setting setuid if we are not the file owner.
361 if ((vap->va_mode & S_ISUID) && ovap->va_uid != cr->cr_uid) {
362 error = priv_check_cred(cr, PRIV_VFS_ADMIN, 0);
370 secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct mount *vfsp)
373 return (priv_check_cred(cr, PRIV_VFS_MOUNT, 0));
377 secpolicy_vnode_owner(vnode_t *vp, cred_t *cr, uid_t owner)
380 if (owner == cr->cr_uid)
382 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
385 /* XXX: vfs_suser()? */
386 return (priv_check_cred(cr, PRIV_VFS_MOUNT_OWNER, 0));
390 secpolicy_vnode_chown(vnode_t *vp, cred_t *cr, uid_t owner)
393 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
395 return (priv_check_cred(cr, PRIV_VFS_CHOWN, 0));
399 secpolicy_fs_mount_clearopts(cred_t *cr, struct mount *vfsp)
402 if (priv_check_cred(cr, PRIV_VFS_MOUNT_NONUSER, 0) != 0) {
404 vfsp->vfs_flag |= VFS_NOSETUID | MNT_USER;
405 vfs_clearmntopt(vfsp, MNTOPT_SETUID);
406 vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL, 0);
412 * Check privileges for setting xvattr attributes
415 secpolicy_xvattr(vnode_t *vp, xvattr_t *xvap, uid_t owner, cred_t *cr,
419 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
421 return (priv_check_cred(cr, PRIV_VFS_SYSFLAGS, 0));
425 secpolicy_smb(cred_t *cr)
428 return (priv_check_cred(cr, PRIV_NETSMB, 0));