]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/os/linux/zfs/policy.c
Linux compat: Minimum kernel version 3.10
[FreeBSD/FreeBSD.git] / module / os / linux / zfs / policy.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
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.
26  *
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.
31  */
32
33 #include <sys/policy.h>
34 #include <linux/security.h>
35 #include <linux/vfs_compat.h>
36
37 /*
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.
43  */
44 static int
45 priv_policy_ns(const cred_t *cr, int capability, boolean_t all, int err,
46     struct user_namespace *ns)
47 {
48         ASSERT3S(all, ==, B_FALSE);
49
50         if (cr != CRED() && (cr != kcred))
51                 return (err);
52
53 #if defined(CONFIG_USER_NS)
54         if (!(ns ? ns_capable(ns, capability) : capable(capability)))
55 #else
56         if (!capable(capability))
57 #endif
58                 return (err);
59
60         return (0);
61 }
62
63 static int
64 priv_policy(const cred_t *cr, int capability, boolean_t all, int err)
65 {
66         return (priv_policy_ns(cr, capability, all, err, NULL));
67 }
68
69 static int
70 priv_policy_user(const cred_t *cr, int capability, boolean_t all, int err)
71 {
72         /*
73          * All priv_policy_user checks are preceded by kuid/kgid_has_mapping()
74          * checks. If we cannot do them, we shouldn't be using ns_capable()
75          * since we don't know whether the affected files are valid in our
76          * namespace.
77          */
78 #if defined(CONFIG_USER_NS)
79         return (priv_policy_ns(cr, capability, all, err, cr->user_ns));
80 #else
81         return (priv_policy_ns(cr, capability, all, err, NULL));
82 #endif
83 }
84
85 /*
86  * Checks for operations that are either client-only or are used by
87  * both clients and servers.
88  */
89 int
90 secpolicy_nfs(const cred_t *cr)
91 {
92         return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EPERM));
93 }
94
95 /*
96  * Catch all system configuration.
97  */
98 int
99 secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
100 {
101         return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EPERM));
102 }
103
104 /*
105  * Like secpolicy_vnode_access() but we get the actual wanted mode and the
106  * current mode of the file, not the missing bits.
107  *
108  * Enforced in the Linux VFS.
109  */
110 int
111 secpolicy_vnode_access2(const cred_t *cr, struct inode *ip, uid_t owner,
112     mode_t curmode, mode_t wantmode)
113 {
114         return (0);
115 }
116
117 /*
118  * This is a special routine for ZFS; it is used to determine whether
119  * any of the privileges in effect allow any form of access to the
120  * file.  There's no reason to audit this or any reason to record
121  * this.  More work is needed to do the "KPLD" stuff.
122  */
123 int
124 secpolicy_vnode_any_access(const cred_t *cr, struct inode *ip, uid_t owner)
125 {
126         if (crgetfsuid(cr) == owner)
127                 return (0);
128
129         if (inode_owner_or_capable(ip))
130                 return (0);
131
132 #if defined(CONFIG_USER_NS)
133         if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
134                 return (EPERM);
135 #endif
136
137         if (priv_policy_user(cr, CAP_DAC_OVERRIDE, B_FALSE, EPERM) == 0)
138                 return (0);
139
140         if (priv_policy_user(cr, CAP_DAC_READ_SEARCH, B_FALSE, EPERM) == 0)
141                 return (0);
142
143         return (EPERM);
144 }
145
146 /*
147  * Determine if subject can chown owner of a file.
148  */
149 int
150 secpolicy_vnode_chown(const cred_t *cr, uid_t owner)
151 {
152         if (crgetfsuid(cr) == owner)
153                 return (0);
154
155 #if defined(CONFIG_USER_NS)
156         if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
157                 return (EPERM);
158 #endif
159
160         return (priv_policy_user(cr, CAP_FOWNER, B_FALSE, EPERM));
161 }
162
163 /*
164  * Determine if subject can change group ownership of a file.
165  */
166 int
167 secpolicy_vnode_create_gid(const cred_t *cr)
168 {
169         return (priv_policy(cr, CAP_SETGID, B_FALSE, EPERM));
170 }
171
172 /*
173  * Policy determines whether we can remove an entry from a directory,
174  * regardless of permission bits.
175  */
176 int
177 secpolicy_vnode_remove(const cred_t *cr)
178 {
179         return (priv_policy(cr, CAP_FOWNER, B_FALSE, EPERM));
180 }
181
182 /*
183  * Determine that subject can modify the mode of a file.  allzone privilege
184  * needed when modifying root owned object.
185  */
186 int
187 secpolicy_vnode_setdac(const cred_t *cr, uid_t owner)
188 {
189         if (crgetfsuid(cr) == owner)
190                 return (0);
191
192 #if defined(CONFIG_USER_NS)
193         if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
194                 return (EPERM);
195 #endif
196
197         return (priv_policy_user(cr, CAP_FOWNER, B_FALSE, EPERM));
198 }
199
200 /*
201  * Are we allowed to retain the set-uid/set-gid bits when
202  * changing ownership or when writing to a file?
203  * "issuid" should be true when set-uid; only in that case
204  * root ownership is checked (setgid is assumed).
205  *
206  * Enforced in the Linux VFS.
207  */
208 int
209 secpolicy_vnode_setid_retain(const cred_t *cr, boolean_t issuidroot)
210 {
211         return (priv_policy_user(cr, CAP_FSETID, B_FALSE, EPERM));
212 }
213
214 /*
215  * Determine that subject can set the file setgid flag.
216  */
217 int
218 secpolicy_vnode_setids_setgids(const cred_t *cr, gid_t gid)
219 {
220 #if defined(CONFIG_USER_NS)
221         if (!kgid_has_mapping(cr->user_ns, SGID_TO_KGID(gid)))
222                 return (EPERM);
223 #endif
224         if (crgetfsgid(cr) != gid && !groupmember(gid, cr))
225                 return (priv_policy_user(cr, CAP_FSETID, B_FALSE, EPERM));
226
227         return (0);
228 }
229
230 /*
231  * Determine if the subject can inject faults in the ZFS fault injection
232  * framework.  Requires all privileges.
233  */
234 int
235 secpolicy_zinject(const cred_t *cr)
236 {
237         return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EACCES));
238 }
239
240 /*
241  * Determine if the subject has permission to manipulate ZFS datasets
242  * (not pools).  Equivalent to the SYS_MOUNT privilege.
243  */
244 int
245 secpolicy_zfs(const cred_t *cr)
246 {
247         return (priv_policy(cr, CAP_SYS_ADMIN, B_FALSE, EACCES));
248 }
249
250 void
251 secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
252 {
253         if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
254             secpolicy_vnode_setid_retain(cr,
255             (vap->va_mode & S_ISUID) != 0 &&
256             (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
257                 vap->va_mask |= AT_MODE;
258                 vap->va_mode &= ~(S_ISUID|S_ISGID);
259         }
260 }
261
262 /*
263  * Determine that subject can set the file setid flags.
264  */
265 static int
266 secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
267 {
268         if (crgetfsuid(cr) == owner)
269                 return (0);
270
271 #if defined(CONFIG_USER_NS)
272         if (!kuid_has_mapping(cr->user_ns, SUID_TO_KUID(owner)))
273                 return (EPERM);
274 #endif
275
276         return (priv_policy_user(cr, CAP_FSETID, B_FALSE, EPERM));
277 }
278
279 /*
280  * Determine that subject can make a file a "sticky".
281  *
282  * Enforced in the Linux VFS.
283  */
284 static int
285 secpolicy_vnode_stky_modify(const cred_t *cr)
286 {
287         return (0);
288 }
289
290 int
291 secpolicy_setid_setsticky_clear(struct inode *ip, vattr_t *vap,
292     const vattr_t *ovap, cred_t *cr)
293 {
294         int error;
295
296         if ((vap->va_mode & S_ISUID) != 0 &&
297             (error = secpolicy_vnode_setid_modify(cr,
298             ovap->va_uid)) != 0) {
299                 return (error);
300         }
301
302         /*
303          * Check privilege if attempting to set the
304          * sticky bit on a non-directory.
305          */
306         if (!S_ISDIR(ip->i_mode) && (vap->va_mode & S_ISVTX) != 0 &&
307             secpolicy_vnode_stky_modify(cr) != 0) {
308                 vap->va_mode &= ~S_ISVTX;
309         }
310
311         /*
312          * Check for privilege if attempting to set the
313          * group-id bit.
314          */
315         if ((vap->va_mode & S_ISGID) != 0 &&
316             secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
317                 vap->va_mode &= ~S_ISGID;
318         }
319
320         return (0);
321 }
322
323 /*
324  * Check privileges for setting xvattr attributes
325  */
326 int
327 secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, vtype_t vtype)
328 {
329         return (secpolicy_vnode_chown(cr, owner));
330 }
331
332 /*
333  * Check privileges for setattr attributes.
334  *
335  * Enforced in the Linux VFS.
336  */
337 int
338 secpolicy_vnode_setattr(cred_t *cr, struct inode *ip, struct vattr *vap,
339     const struct vattr *ovap, int flags,
340     int unlocked_access(void *, int, cred_t *), void *node)
341 {
342         return (0);
343 }
344
345 /*
346  * Check privileges for links.
347  *
348  * Enforced in the Linux VFS.
349  */
350 int
351 secpolicy_basic_link(const cred_t *cr)
352 {
353         return (0);
354 }