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/mount.h>
35 #include <sys/policy.h>
38 secpolicy_zfs(struct ucred *cred)
41 return (priv_check_cred(cred, PRIV_VFS_MOUNT, 0));
45 secpolicy_sys_config(struct ucred *cred, int checkonly __unused)
48 return (priv_check_cred(cred, PRIV_ZFS_POOL_CONFIG, 0));
52 secpolicy_zinject(struct ucred *cred)
55 return (priv_check_cred(cred, PRIV_ZFS_INJECT, 0));
59 secpolicy_fs_unmount(struct ucred *cred, struct mount *vfsp __unused)
62 return (priv_check_cred(cred, PRIV_VFS_UNMOUNT, 0));
66 * This check is done in kern_link(), so we could just return 0 here.
68 extern int hardlink_check_uid;
70 secpolicy_basic_link(struct ucred *cred)
73 if (!hardlink_check_uid)
75 return (priv_check_cred(cred, PRIV_VFS_LINK, 0));
79 secpolicy_vnode_stky_modify(struct ucred *cred)
86 secpolicy_vnode_remove(struct ucred *cred)
89 return (priv_check_cred(cred, PRIV_VFS_ADMIN, 0));
93 secpolicy_vnode_access(struct ucred *cred, struct vnode *vp, uint64_t owner,
97 if ((mode & VREAD) && priv_check_cred(cred, PRIV_VFS_READ, 0) != 0) {
100 if ((mode & VWRITE) &&
101 priv_check_cred(cred, PRIV_VFS_WRITE, 0) != 0) {
105 if (vp->v_type == VDIR) {
106 if (priv_check_cred(cred, PRIV_VFS_LOOKUP, 0) != 0) {
110 if (priv_check_cred(cred, PRIV_VFS_EXEC, 0) != 0) {
119 secpolicy_vnode_setdac(struct ucred *cred, uid_t owner)
122 if (owner == cred->cr_uid)
124 return (priv_check_cred(cred, PRIV_VFS_ADMIN, 0));
128 secpolicy_vnode_setattr(struct ucred *cred, struct vnode *vp, struct vattr *vap,
129 const struct vattr *ovap, int flags,
130 int unlocked_access(void *, int, struct ucred *), void *node)
132 int mask = vap->va_mask;
135 if (mask & AT_SIZE) {
136 if (vp->v_type == VDIR)
138 error = unlocked_access(node, VWRITE, cred);
142 if (mask & AT_MODE) {
144 * If not the owner of the file then check privilege
145 * for two things: the privilege to set the mode at all
146 * and, if we're setting setuid, we also need permissions
147 * to add the set-uid bit, if we're not the owner.
148 * In the specific case of creating a set-uid root
149 * file, we need even more permissions.
151 error = secpolicy_vnode_setdac(cred, ovap->va_uid);
154 error = secpolicy_setid_setsticky_clear(vp, vap, ovap, cred);
158 vap->va_mode = ovap->va_mode;
160 if (mask & (AT_UID | AT_GID)) {
161 error = secpolicy_vnode_setdac(cred, ovap->va_uid);
166 * To change the owner of a file, or change the group of a file to a
167 * group of which we are not a member, the caller must have
170 if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
171 ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
172 !groupmember(vap->va_gid, cred))) {
173 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
178 if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
179 ((mask & AT_GID) && vap->va_gid != ovap->va_gid)) {
180 secpolicy_setid_clear(vap, cred);
183 if (mask & (AT_ATIME | AT_MTIME)) {
186 * If times is NULL, ... The caller must be the owner of
187 * the file, have permission to write the file, or be the
189 * If times is non-NULL, ... The caller must be the owner of
190 * the file or be the super-user.
192 error = secpolicy_vnode_setdac(cred, ovap->va_uid);
193 if (error && (vap->va_vaflags & VA_UTIMES_NULL))
194 error = unlocked_access(node, VWRITE, cred);
202 secpolicy_vnode_create_gid(struct ucred *cred)
209 secpolicy_vnode_setids_setgids(struct ucred *cred, gid_t gid)
212 if (!groupmember(gid, cred))
213 return (priv_check_cred(cred, PRIV_VFS_SETGID, 0));
218 secpolicy_vnode_setid_retain(struct ucred *cred, boolean_t issuidroot __unused)
221 return (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0));
225 secpolicy_setid_clear(struct vattr *vap, struct ucred *cred)
228 if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0) {
229 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) {
230 vap->va_mask |= AT_MODE;
231 vap->va_mode &= ~(S_ISUID|S_ISGID);
237 secpolicy_setid_setsticky_clear(struct vnode *vp, struct vattr *vap,
238 const struct vattr *ovap, struct ucred *cred)
243 * Privileged processes may set the sticky bit on non-directories,
244 * as well as set the setgid bit on a file with a group that the process
245 * is not a member of. Both of these are allowed in jail(8).
247 if (vp->v_type != VDIR && (vap->va_mode & S_ISTXT)) {
248 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
252 * Check for privilege if attempting to set the
255 if ((vap->va_mode & S_ISGID) != 0) {
256 error = secpolicy_vnode_setids_setgids(cred, ovap->va_gid);