4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013, Joyent, Inc. All rights reserved.
25 * Copyright (C) 2016 Lawrence Livermore National Security, LLC.
27 * For Linux the vast majority of this enforcement is already handled via
28 * the standard Linux VFS permission checks. However certain administrative
29 * commands which bypass the standard mechanisms may need to make use of
33 #include <sys/policy.h>
34 #include <linux/security.h>
35 #include <linux/vfs_compat.h>
38 * The passed credentials cannot be directly verified because Linux only
39 * provides and interface to check the *current* process credentials. In
40 * order to handle this the capable() test is only run when the passed
41 * credentials match the current process credentials or the kcred. In
42 * all other cases this function must fail and return the passed err.
45 priv_policy(const cred_t *cr, int capability, boolean_t all, int err)
47 ASSERT3S(all, ==, B_FALSE);
49 if (cr != CRED() && (cr != kcred))
52 if (!capable(capability))
59 * Checks for operations that are either client-only or are used by
60 * both clients and servers.
63 secpolicy_nfs(const cred_t *cr)
65 return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EPERM));
69 * Catch all system configuration.
72 secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
74 return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EPERM));
78 * Like secpolicy_vnode_access() but we get the actual wanted mode and the
79 * current mode of the file, not the missing bits.
81 * Enforced in the Linux VFS.
84 secpolicy_vnode_access2(const cred_t *cr, struct inode *ip, uid_t owner,
85 mode_t curmode, mode_t wantmode)
91 * This is a special routine for ZFS; it is used to determine whether
92 * any of the privileges in effect allow any form of access to the
93 * file. There's no reason to audit this or any reason to record
94 * this. More work is needed to do the "KPLD" stuff.
97 secpolicy_vnode_any_access(const cred_t *cr, struct inode *ip, uid_t owner)
99 if (crgetfsuid(cr) == owner)
102 if (zpl_inode_owner_or_capable(ip))
105 if (priv_policy(cr, CAP_DAC_OVERRIDE, B_FALSE, EPERM) == 0)
108 if (priv_policy(cr, CAP_DAC_READ_SEARCH, B_FALSE, EPERM) == 0)
115 * Determine if subject can chown owner of a file.
118 secpolicy_vnode_chown(const cred_t *cr, uid_t owner)
120 if (crgetfsuid(cr) == owner)
123 return (priv_policy(cr, CAP_FOWNER, B_FALSE, EPERM));
127 * Determine if subject can change group ownership of a file.
130 secpolicy_vnode_create_gid(const cred_t *cr)
132 return (priv_policy(cr, CAP_SETGID, B_FALSE, EPERM));
136 * Policy determines whether we can remove an entry from a directory,
137 * regardless of permission bits.
140 secpolicy_vnode_remove(const cred_t *cr)
142 return (priv_policy(cr, CAP_FOWNER, B_FALSE, EPERM));
146 * Determine that subject can modify the mode of a file. allzone privilege
147 * needed when modifying root owned object.
150 secpolicy_vnode_setdac(const cred_t *cr, uid_t owner)
152 if (crgetfsuid(cr) == owner)
155 return (priv_policy(cr, CAP_FOWNER, B_FALSE, EPERM));
159 * Are we allowed to retain the set-uid/set-gid bits when
160 * changing ownership or when writing to a file?
161 * "issuid" should be true when set-uid; only in that case
162 * root ownership is checked (setgid is assumed).
164 * Enforced in the Linux VFS.
167 secpolicy_vnode_setid_retain(const cred_t *cr, boolean_t issuidroot)
173 * Determine that subject can set the file setgid flag.
176 secpolicy_vnode_setids_setgids(const cred_t *cr, gid_t gid)
178 if (crgetfsgid(cr) != gid && !groupmember(gid, cr))
179 return (priv_policy(cr, CAP_FSETID, B_FALSE, EPERM));
185 * Determine if the subject can inject faults in the ZFS fault injection
186 * framework. Requires all privileges.
189 secpolicy_zinject(const cred_t *cr)
191 return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EACCES));
195 * Determine if the subject has permission to manipulate ZFS datasets
196 * (not pools). Equivalent to the SYS_MOUNT privilege.
199 secpolicy_zfs(const cred_t *cr)
201 return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EACCES));
205 secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
207 if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
208 secpolicy_vnode_setid_retain(cr,
209 (vap->va_mode & S_ISUID) != 0 &&
210 (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
211 vap->va_mask |= AT_MODE;
212 vap->va_mode &= ~(S_ISUID|S_ISGID);
217 * Determine that subject can set the file setid flags.
220 secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
222 if (crgetfsuid(cr) == owner)
225 return (priv_policy(cr, CAP_FSETID, B_FALSE, EPERM));
229 * Determine that subject can make a file a "sticky".
231 * Enforced in the Linux VFS.
234 secpolicy_vnode_stky_modify(const cred_t *cr)
240 secpolicy_setid_setsticky_clear(struct inode *ip, vattr_t *vap,
241 const vattr_t *ovap, cred_t *cr)
245 if ((vap->va_mode & S_ISUID) != 0 &&
246 (error = secpolicy_vnode_setid_modify(cr,
247 ovap->va_uid)) != 0) {
252 * Check privilege if attempting to set the
253 * sticky bit on a non-directory.
255 if (!S_ISDIR(ip->i_mode) && (vap->va_mode & S_ISVTX) != 0 &&
256 secpolicy_vnode_stky_modify(cr) != 0) {
257 vap->va_mode &= ~S_ISVTX;
261 * Check for privilege if attempting to set the
264 if ((vap->va_mode & S_ISGID) != 0 &&
265 secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
266 vap->va_mode &= ~S_ISGID;
273 * Check privileges for setting xvattr attributes
276 secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, vtype_t vtype)
278 return (secpolicy_vnode_chown(cr, owner));
282 * Check privileges for setattr attributes.
284 * Enforced in the Linux VFS.
287 secpolicy_vnode_setattr(cred_t *cr, struct inode *ip, struct vattr *vap,
288 const struct vattr *ovap, int flags,
289 int unlocked_access(void *, int, cred_t *), void *node)
295 * Check privileges for links.
297 * Enforced in the Linux VFS.
300 secpolicy_basic_link(const cred_t *cr)