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(struct ucred *cred)
44 return (priv_check_cred(cred, PRIV_NFS_DAEMON, 0));
48 secpolicy_zfs(struct ucred *cred)
51 return (priv_check_cred(cred, PRIV_VFS_MOUNT, 0));
55 secpolicy_sys_config(struct ucred *cred, int checkonly __unused)
58 return (priv_check_cred(cred, PRIV_ZFS_POOL_CONFIG, 0));
62 secpolicy_zinject(struct ucred *cred)
65 return (priv_check_cred(cred, PRIV_ZFS_INJECT, 0));
69 secpolicy_fs_unmount(struct ucred *cred, struct mount *vfsp __unused)
72 return (priv_check_cred(cred, PRIV_VFS_UNMOUNT, 0));
76 secpolicy_fs_owner(struct mount *mp, struct ucred *cred)
79 if (zfs_super_owner) {
80 if (cred->cr_uid == mp->mnt_cred->cr_uid &&
81 cred->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(struct vnode *vp, struct ucred *cred)
96 if (!hardlink_check_uid)
98 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
100 return (priv_check_cred(cred, PRIV_VFS_LINK, 0));
104 secpolicy_vnode_stky_modify(struct ucred *cred)
111 secpolicy_vnode_remove(struct vnode *vp, struct ucred *cred)
114 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
116 return (priv_check_cred(cred, PRIV_VFS_ADMIN, 0));
120 secpolicy_vnode_access(struct ucred *cred, struct vnode *vp, uint64_t owner,
124 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
127 if ((accmode & VREAD) && priv_check_cred(cred, PRIV_VFS_READ, 0) != 0)
129 if ((accmode & VWRITE) &&
130 priv_check_cred(cred, PRIV_VFS_WRITE, 0) != 0) {
133 if (accmode & VEXEC) {
134 if (vp->v_type == VDIR) {
135 if (priv_check_cred(cred, PRIV_VFS_LOOKUP, 0) != 0) {
139 if (priv_check_cred(cred, PRIV_VFS_EXEC, 0) != 0) {
148 secpolicy_vnode_setdac(struct vnode *vp, struct ucred *cred, uid_t owner)
151 if (owner == cred->cr_uid)
153 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
155 return (priv_check_cred(cred, PRIV_VFS_ADMIN, 0));
159 secpolicy_vnode_setattr(struct ucred *cred, struct vnode *vp, struct vattr *vap,
160 const struct vattr *ovap, int flags,
161 int unlocked_access(void *, int, struct ucred *), void *node)
163 int mask = vap->va_mask;
166 if (mask & AT_SIZE) {
167 if (vp->v_type == VDIR)
169 error = unlocked_access(node, VWRITE, cred);
173 if (mask & AT_MODE) {
175 * If not the owner of the file then check privilege
176 * for two things: the privilege to set the mode at all
177 * and, if we're setting setuid, we also need permissions
178 * to add the set-uid bit, if we're not the owner.
179 * In the specific case of creating a set-uid root
180 * file, we need even more permissions.
182 error = secpolicy_vnode_setdac(vp, cred, ovap->va_uid);
185 error = secpolicy_setid_setsticky_clear(vp, vap, ovap, cred);
189 vap->va_mode = ovap->va_mode;
191 if (mask & (AT_UID | AT_GID)) {
192 error = secpolicy_vnode_setdac(vp, cred, ovap->va_uid);
197 * To change the owner of a file, or change the group of a file to a
198 * group of which we are not a member, the caller must have
201 if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
202 ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
203 !groupmember(vap->va_gid, cred))) {
204 if (secpolicy_fs_owner(vp->v_mount, cred) != 0) {
205 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
211 if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
212 ((mask & AT_GID) && vap->va_gid != ovap->va_gid)) {
213 secpolicy_setid_clear(vap, vp, cred);
216 if (mask & (AT_ATIME | AT_MTIME)) {
219 * If times is NULL, ... The caller must be the owner of
220 * the file, have permission to write the file, or be the
222 * If times is non-NULL, ... The caller must be the owner of
223 * the file or be the super-user.
225 error = secpolicy_vnode_setdac(vp, cred, ovap->va_uid);
226 if (error && (vap->va_vaflags & VA_UTIMES_NULL))
227 error = unlocked_access(node, VWRITE, cred);
235 secpolicy_vnode_create_gid(struct ucred *cred)
242 secpolicy_vnode_setids_setgids(struct vnode *vp, struct ucred *cred, gid_t gid)
245 if (groupmember(gid, cred))
247 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
249 return (priv_check_cred(cred, PRIV_VFS_SETGID, 0));
253 secpolicy_vnode_setid_retain(struct vnode *vp, struct ucred *cred,
254 boolean_t issuidroot __unused)
257 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
259 return (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0));
263 secpolicy_setid_clear(struct vattr *vap, struct vnode *vp, struct ucred *cred)
266 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
269 if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0) {
270 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) {
271 vap->va_mask |= AT_MODE;
272 vap->va_mode &= ~(S_ISUID|S_ISGID);
278 secpolicy_setid_setsticky_clear(struct vnode *vp, struct vattr *vap,
279 const struct vattr *ovap, struct ucred *cred)
283 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
287 * Privileged processes may set the sticky bit on non-directories,
288 * as well as set the setgid bit on a file with a group that the process
289 * is not a member of. Both of these are allowed in jail(8).
291 if (vp->v_type != VDIR && (vap->va_mode & S_ISTXT)) {
292 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
296 * Check for privilege if attempting to set the
299 if ((vap->va_mode & S_ISGID) != 0) {
300 error = secpolicy_vnode_setids_setgids(vp, cred, ovap->va_gid);
305 * Deny setting setuid if we are not the file owner.
307 if ((vap->va_mode & S_ISUID) && ovap->va_uid != cred->cr_uid) {
308 error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
316 secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct mount *vfsp)
319 return (priv_check_cred(cr, PRIV_VFS_MOUNT, 0));
323 secpolicy_vnode_owner(struct vnode *vp, cred_t *cred, uid_t owner)
326 if (owner == cred->cr_uid)
328 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
331 /* XXX: vfs_suser()? */
332 return (priv_check_cred(cred, PRIV_VFS_MOUNT_OWNER, 0));
336 secpolicy_vnode_chown(struct vnode *vp, cred_t *cred, boolean_t check_self)
339 if (secpolicy_fs_owner(vp->v_mount, cred) == 0)
341 return (priv_check_cred(cred, PRIV_VFS_CHOWN, 0));
345 secpolicy_fs_mount_clearopts(cred_t *cr, struct mount *vfsp)
348 if (priv_check_cred(cr, PRIV_VFS_MOUNT_NONUSER, 0) != 0) {
350 vfsp->vfs_flag |= VFS_NOSETUID | MNT_USER;
351 vfs_clearmntopt(vfsp, MNTOPT_SETUID);
352 vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL, 0);
358 * Check privileges for setting xvattr attributes
361 secpolicy_xvattr(struct vnode *vp, xvattr_t *xvap, uid_t owner, cred_t *cr,
365 if (secpolicy_fs_owner(vp->v_mount, cr) == 0)
367 return (priv_check_cred(cr, PRIV_VFS_SYSFLAGS, 0));