Fix O_APPEND for Linux 3.15 and older kernels
[zfs.git] / module / os / linux / zfs / policy.c
blob5a52092bb90aed4b72f4e55755eff7b7f065b6f3
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
30 * this functionality.
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.
44 static int
45 priv_policy_ns(const cred_t *cr, int capability, int err,
46 struct user_namespace *ns)
48 if (cr != CRED() && (cr != kcred))
49 return (err);
51 #if defined(CONFIG_USER_NS)
52 if (!(ns ? ns_capable(ns, capability) : capable(capability)))
53 #else
54 if (!capable(capability))
55 #endif
56 return (err);
58 return (0);
61 static int
62 priv_policy(const cred_t *cr, int capability, int err)
64 return (priv_policy_ns(cr, capability, err, NULL));
67 static int
68 priv_policy_user(const cred_t *cr, int capability, int err)
71 * All priv_policy_user checks are preceded by kuid/kgid_has_mapping()
72 * checks. If we cannot do them, we shouldn't be using ns_capable()
73 * since we don't know whether the affected files are valid in our
74 * namespace.
76 #if defined(CONFIG_USER_NS)
77 return (priv_policy_ns(cr, capability, err, cr->user_ns));
78 #else
79 return (priv_policy_ns(cr, capability, err, NULL));
80 #endif
84 * Checks for operations that are either client-only or are used by
85 * both clients and servers.
87 int
88 secpolicy_nfs(const cred_t *cr)
90 return (priv_policy(cr, CAP_SYS_ADMIN, EPERM));
94 * Catch all system configuration.
96 int
97 secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
99 return (priv_policy(cr, CAP_SYS_ADMIN, EPERM));
103 * Like secpolicy_vnode_access() but we get the actual wanted mode and the
104 * current mode of the file, not the missing bits.
106 * Enforced in the Linux VFS.
109 secpolicy_vnode_access2(const cred_t *cr, struct inode *ip, uid_t owner,
110 mode_t curmode, mode_t wantmode)
112 return (0);
116 * This is a special routine for ZFS; it is used to determine whether
117 * any of the privileges in effect allow any form of access to the
118 * file. There's no reason to audit this or any reason to record
119 * this. More work is needed to do the "KPLD" stuff.
122 secpolicy_vnode_any_access(const cred_t *cr, struct inode *ip, uid_t owner)
124 if (crgetuid(cr) == owner)
125 return (0);
127 if (zpl_inode_owner_or_capable(kcred->user_ns, ip))
128 return (0);
130 #if defined(CONFIG_USER_NS)
131 if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
132 return (EPERM);
133 #endif
135 if (priv_policy_user(cr, CAP_DAC_OVERRIDE, EPERM) == 0)
136 return (0);
138 if (priv_policy_user(cr, CAP_DAC_READ_SEARCH, EPERM) == 0)
139 return (0);
141 return (EPERM);
145 * Determine if subject can chown owner of a file.
148 secpolicy_vnode_chown(const cred_t *cr, uid_t owner)
150 if (crgetuid(cr) == owner)
151 return (0);
153 #if defined(CONFIG_USER_NS)
154 if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
155 return (EPERM);
156 #endif
158 return (priv_policy_user(cr, CAP_FOWNER, EPERM));
162 * Determine if subject can change group ownership of a file.
165 secpolicy_vnode_create_gid(const cred_t *cr)
167 return (priv_policy(cr, CAP_SETGID, EPERM));
171 * Policy determines whether we can remove an entry from a directory,
172 * regardless of permission bits.
175 secpolicy_vnode_remove(const cred_t *cr)
177 return (priv_policy(cr, CAP_FOWNER, EPERM));
181 * Determine that subject can modify the mode of a file. allzone privilege
182 * needed when modifying root owned object.
185 secpolicy_vnode_setdac(const cred_t *cr, uid_t owner)
187 if (crgetuid(cr) == owner)
188 return (0);
190 #if defined(CONFIG_USER_NS)
191 if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
192 return (EPERM);
193 #endif
195 return (priv_policy_user(cr, CAP_FOWNER, EPERM));
199 * Are we allowed to retain the set-uid/set-gid bits when
200 * changing ownership or when writing to a file?
201 * "issuid" should be true when set-uid; only in that case
202 * root ownership is checked (setgid is assumed).
204 * Enforced in the Linux VFS.
207 secpolicy_vnode_setid_retain(struct znode *zp __maybe_unused, const cred_t *cr,
208 boolean_t issuidroot)
210 return (priv_policy_user(cr, CAP_FSETID, EPERM));
214 * Determine that subject can set the file setgid flag.
217 secpolicy_vnode_setids_setgids(const cred_t *cr, gid_t gid)
219 #if defined(CONFIG_USER_NS)
220 if (!kgid_has_mapping(cr->user_ns, SGID_TO_KGID(gid)))
221 return (EPERM);
222 #endif
223 if (crgetgid(cr) != gid && !groupmember(gid, cr))
224 return (priv_policy_user(cr, CAP_FSETID, EPERM));
226 return (0);
230 * Determine if the subject can inject faults in the ZFS fault injection
231 * framework. Requires all privileges.
234 secpolicy_zinject(const cred_t *cr)
236 return (priv_policy(cr, CAP_SYS_ADMIN, EACCES));
240 * Determine if the subject has permission to manipulate ZFS datasets
241 * (not pools). Equivalent to the SYS_MOUNT privilege.
244 secpolicy_zfs(const cred_t *cr)
246 return (priv_policy(cr, CAP_SYS_ADMIN, EACCES));
250 * Equivalent to secpolicy_zfs(), but works even if the cred_t is not that of
251 * the current process. Takes both cred_t and proc_t so that this can work
252 * easily on all platforms.
254 * The has_capability() function was first exported in the 4.10 Linux kernel
255 * then backported to some LTS kernels. Prior to this change there was no
256 * mechanism to perform this check therefore EACCES is returned when the
257 * functionality is not present in the kernel.
260 secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
262 #if defined(HAVE_HAS_CAPABILITY)
263 if (!has_capability(proc, CAP_SYS_ADMIN))
264 return (EACCES);
265 return (0);
266 #else
267 return (EACCES);
268 #endif
271 void
272 secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
274 if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
275 secpolicy_vnode_setid_retain(NULL, cr,
276 (vap->va_mode & S_ISUID) != 0 &&
277 (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
278 vap->va_mask |= AT_MODE;
279 vap->va_mode &= ~(S_ISUID|S_ISGID);
284 * Determine that subject can set the file setid flags.
286 static int
287 secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
289 if (crgetuid(cr) == owner)
290 return (0);
292 #if defined(CONFIG_USER_NS)
293 if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
294 return (EPERM);
295 #endif
297 return (priv_policy_user(cr, CAP_FSETID, EPERM));
301 * Determine that subject can make a file a "sticky".
303 * Enforced in the Linux VFS.
305 static int
306 secpolicy_vnode_stky_modify(const cred_t *cr)
308 return (0);
312 secpolicy_setid_setsticky_clear(struct inode *ip, vattr_t *vap,
313 const vattr_t *ovap, cred_t *cr)
315 int error;
317 if ((vap->va_mode & S_ISUID) != 0 &&
318 (error = secpolicy_vnode_setid_modify(cr,
319 ovap->va_uid)) != 0) {
320 return (error);
324 * Check privilege if attempting to set the
325 * sticky bit on a non-directory.
327 if (!S_ISDIR(ip->i_mode) && (vap->va_mode & S_ISVTX) != 0 &&
328 secpolicy_vnode_stky_modify(cr) != 0) {
329 vap->va_mode &= ~S_ISVTX;
333 * Check for privilege if attempting to set the
334 * group-id bit.
336 if ((vap->va_mode & S_ISGID) != 0 &&
337 secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
338 vap->va_mode &= ~S_ISGID;
341 return (0);
345 * Check privileges for setting xvattr attributes
348 secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, mode_t type)
350 return (secpolicy_vnode_chown(cr, owner));
354 * Check privileges for setattr attributes.
356 * Enforced in the Linux VFS.
359 secpolicy_vnode_setattr(cred_t *cr, struct inode *ip, struct vattr *vap,
360 const struct vattr *ovap, int flags,
361 int unlocked_access(void *, int, cred_t *), void *node)
363 return (0);
367 * Check privileges for links.
369 * Enforced in the Linux VFS.
372 secpolicy_basic_link(const cred_t *cr)
374 return (0);