]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_acl.c
Use acl_alloc() and acl_free() instead of using uma(9) directly.
[FreeBSD/FreeBSD.git] / sys / kern / vfs_acl.c
1 /*-
2  * Copyright (c) 1999-2006 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * This software was developed by Robert Watson for the TrustedBSD Project.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * Developed by the TrustedBSD Project.
30  *
31  * ACL system calls and other functions common across different ACL types.
32  * Type-specific routines go into subr_acl_<type>.c.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_mac.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/vnode.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/file.h>
52 #include <sys/filedesc.h>
53 #include <sys/proc.h>
54 #include <sys/sysent.h>
55 #include <sys/acl.h>
56
57 #include <security/mac/mac_framework.h>
58
59 #include <vm/uma.h>
60
61 uma_zone_t      acl_zone;
62 static int      vacl_set_acl(struct thread *td, struct vnode *vp,
63                     acl_type_t type, struct acl *aclp);
64 static int      vacl_get_acl(struct thread *td, struct vnode *vp,
65                     acl_type_t type, struct acl *aclp);
66 static int      vacl_aclcheck(struct thread *td, struct vnode *vp,
67                     acl_type_t type, struct acl *aclp);
68
69 /*
70  * These calls wrap the real vnode operations, and are called by the syscall
71  * code once the syscall has converted the path or file descriptor to a vnode
72  * (unlocked).  The aclp pointer is assumed still to point to userland, so
73  * this should not be consumed within the kernel except by syscall code.
74  * Other code should directly invoke VOP_{SET,GET}ACL.
75  */
76
77 /*
78  * Given a vnode, set its ACL.
79  */
80 static int
81 vacl_set_acl(struct thread *td, struct vnode *vp, acl_type_t type,
82     struct acl *aclp)
83 {
84         struct acl *inkernelacl;
85         struct mount *mp;
86         int error;
87
88         inkernelacl = acl_alloc(M_WAITOK);
89         error = copyin(aclp, inkernelacl, sizeof(struct acl));
90         if (error)
91                 goto out;
92         error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
93         if (error != 0)
94                 goto out;
95         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
96 #ifdef MAC
97         error = mac_vnode_check_setacl(td->td_ucred, vp, type, inkernelacl);
98         if (error != 0)
99                 goto out_unlock;
100 #endif
101         error = VOP_SETACL(vp, type, inkernelacl, td->td_ucred, td);
102 #ifdef MAC
103 out_unlock:
104 #endif
105         VOP_UNLOCK(vp, 0);
106         vn_finished_write(mp);
107 out:
108         acl_free(inkernelacl);
109         return(error);
110 }
111
112 /*
113  * Given a vnode, get its ACL.
114  */
115 static int
116 vacl_get_acl(struct thread *td, struct vnode *vp, acl_type_t type,
117     struct acl *aclp)
118 {
119         struct acl *inkernelacl;
120         int error;
121
122         inkernelacl = acl_alloc(M_WAITOK);
123         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
124 #ifdef MAC
125         error = mac_vnode_check_getacl(td->td_ucred, vp, type);
126         if (error != 0)
127                 goto out;
128 #endif
129         error = VOP_GETACL(vp, type, inkernelacl, td->td_ucred, td);
130 #ifdef MAC
131 out:
132 #endif
133         VOP_UNLOCK(vp, 0);
134         if (error == 0)
135                 error = copyout(inkernelacl, aclp, sizeof(struct acl));
136         acl_free(inkernelacl);
137         return (error);
138 }
139
140 /*
141  * Given a vnode, delete its ACL.
142  */
143 static int
144 vacl_delete(struct thread *td, struct vnode *vp, acl_type_t type)
145 {
146         struct mount *mp;
147         int error;
148
149         error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
150         if (error)
151                 return (error);
152         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
153 #ifdef MAC
154         error = mac_vnode_check_deleteacl(td->td_ucred, vp, type);
155         if (error)
156                 goto out;
157 #endif
158         error = VOP_SETACL(vp, type, 0, td->td_ucred, td);
159 #ifdef MAC
160 out:
161 #endif
162         VOP_UNLOCK(vp, 0);
163         vn_finished_write(mp);
164         return (error);
165 }
166
167 /*
168  * Given a vnode, check whether an ACL is appropriate for it
169  */
170 static int
171 vacl_aclcheck(struct thread *td, struct vnode *vp, acl_type_t type,
172     struct acl *aclp)
173 {
174         struct acl *inkernelacl;
175         int error;
176
177         inkernelacl = acl_alloc(M_WAITOK);
178         error = copyin(aclp, inkernelacl, sizeof(struct acl));
179         if (error)
180                 goto out;
181         error = VOP_ACLCHECK(vp, type, inkernelacl, td->td_ucred, td);
182 out:
183         acl_free(inkernelacl);
184         return (error);
185 }
186
187 /*
188  * syscalls -- convert the path/fd to a vnode, and call vacl_whatever.  Don't
189  * need to lock, as the vacl_ code will get/release any locks required.
190  */
191
192 /*
193  * Given a file path, get an ACL for it
194  */
195 int
196 __acl_get_file(struct thread *td, struct __acl_get_file_args *uap)
197 {
198         struct nameidata nd;
199         int vfslocked, error;
200
201         NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
202         error = namei(&nd);
203         vfslocked = NDHASGIANT(&nd);
204         if (error == 0) {
205                 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
206                 NDFREE(&nd, 0);
207         }
208         VFS_UNLOCK_GIANT(vfslocked);
209         return (error);
210 }
211
212 /*
213  * Given a file path, get an ACL for it; don't follow links.
214  */
215 int
216 __acl_get_link(struct thread *td, struct __acl_get_link_args *uap)
217 {
218         struct nameidata nd;
219         int vfslocked, error;
220
221         NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
222         error = namei(&nd);
223         vfslocked = NDHASGIANT(&nd);
224         if (error == 0) {
225                 error = vacl_get_acl(td, nd.ni_vp, uap->type, uap->aclp);
226                 NDFREE(&nd, 0);
227         }
228         VFS_UNLOCK_GIANT(vfslocked);
229         return (error);
230 }
231
232 /*
233  * Given a file path, set an ACL for it.
234  */
235 int
236 __acl_set_file(struct thread *td, struct __acl_set_file_args *uap)
237 {
238         struct nameidata nd;
239         int vfslocked, error;
240
241         NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
242         error = namei(&nd);
243         vfslocked = NDHASGIANT(&nd);
244         if (error == 0) {
245                 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
246                 NDFREE(&nd, 0);
247         }
248         VFS_UNLOCK_GIANT(vfslocked);
249         return (error);
250 }
251
252 /*
253  * Given a file path, set an ACL for it; don't follow links.
254  */
255 int
256 __acl_set_link(struct thread *td, struct __acl_set_link_args *uap)
257 {
258         struct nameidata nd;
259         int vfslocked, error;
260
261         NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
262         error = namei(&nd);
263         vfslocked = NDHASGIANT(&nd);
264         if (error == 0) {
265                 error = vacl_set_acl(td, nd.ni_vp, uap->type, uap->aclp);
266                 NDFREE(&nd, 0);
267         }
268         VFS_UNLOCK_GIANT(vfslocked);
269         return (error);
270 }
271
272 /*
273  * Given a file descriptor, get an ACL for it.
274  */
275 int
276 __acl_get_fd(struct thread *td, struct __acl_get_fd_args *uap)
277 {
278         struct file *fp;
279         int vfslocked, error;
280
281         error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
282         if (error == 0) {
283                 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
284                 error = vacl_get_acl(td, fp->f_vnode, uap->type, uap->aclp);
285                 fdrop(fp, td);
286                 VFS_UNLOCK_GIANT(vfslocked);
287         }
288         return (error);
289 }
290
291 /*
292  * Given a file descriptor, set an ACL for it.
293  */
294 int
295 __acl_set_fd(struct thread *td, struct __acl_set_fd_args *uap)
296 {
297         struct file *fp;
298         int vfslocked, error;
299
300         error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
301         if (error == 0) {
302                 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
303                 error = vacl_set_acl(td, fp->f_vnode, uap->type, uap->aclp);
304                 fdrop(fp, td);
305                 VFS_UNLOCK_GIANT(vfslocked);
306         }
307         return (error);
308 }
309
310 /*
311  * Given a file path, delete an ACL from it.
312  */
313 int
314 __acl_delete_file(struct thread *td, struct __acl_delete_file_args *uap)
315 {
316         struct nameidata nd;
317         int vfslocked, error;
318
319         NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
320         error = namei(&nd);
321         vfslocked = NDHASGIANT(&nd);
322         if (error == 0) {
323                 error = vacl_delete(td, nd.ni_vp, uap->type);
324                 NDFREE(&nd, 0);
325         }
326         VFS_UNLOCK_GIANT(vfslocked);
327         return (error);
328 }
329
330 /*
331  * Given a file path, delete an ACL from it; don't follow links.
332  */
333 int
334 __acl_delete_link(struct thread *td, struct __acl_delete_link_args *uap)
335 {
336         struct nameidata nd;
337         int vfslocked, error;
338
339         NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
340         error = namei(&nd);
341         vfslocked = NDHASGIANT(&nd);
342         if (error == 0) {
343                 error = vacl_delete(td, nd.ni_vp, uap->type);
344                 NDFREE(&nd, 0);
345         }
346         VFS_UNLOCK_GIANT(vfslocked);
347         return (error);
348 }
349
350 /*
351  * Given a file path, delete an ACL from it.
352  */
353 int
354 __acl_delete_fd(struct thread *td, struct __acl_delete_fd_args *uap)
355 {
356         struct file *fp;
357         int vfslocked, error;
358
359         error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
360         if (error == 0) {
361                 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
362                 error = vacl_delete(td, fp->f_vnode, uap->type);
363                 fdrop(fp, td);
364                 VFS_UNLOCK_GIANT(vfslocked);
365         }
366         return (error);
367 }
368
369 /*
370  * Given a file path, check an ACL for it.
371  */
372 int
373 __acl_aclcheck_file(struct thread *td, struct __acl_aclcheck_file_args *uap)
374 {
375         struct nameidata        nd;
376         int vfslocked, error;
377
378         NDINIT(&nd, LOOKUP, MPSAFE|FOLLOW, UIO_USERSPACE, uap->path, td);
379         error = namei(&nd);
380         vfslocked = NDHASGIANT(&nd);
381         if (error == 0) {
382                 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
383                 NDFREE(&nd, 0);
384         }
385         VFS_UNLOCK_GIANT(vfslocked);
386         return (error);
387 }
388
389 /*
390  * Given a file path, check an ACL for it; don't follow links.
391  */
392 int
393 __acl_aclcheck_link(struct thread *td, struct __acl_aclcheck_link_args *uap)
394 {
395         struct nameidata        nd;
396         int vfslocked, error;
397
398         NDINIT(&nd, LOOKUP, MPSAFE|NOFOLLOW, UIO_USERSPACE, uap->path, td);
399         error = namei(&nd);
400         vfslocked = NDHASGIANT(&nd);
401         if (error == 0) {
402                 error = vacl_aclcheck(td, nd.ni_vp, uap->type, uap->aclp);
403                 NDFREE(&nd, 0);
404         }
405         VFS_UNLOCK_GIANT(vfslocked);
406         return (error);
407 }
408
409 /*
410  * Given a file descriptor, check an ACL for it.
411  */
412 int
413 __acl_aclcheck_fd(struct thread *td, struct __acl_aclcheck_fd_args *uap)
414 {
415         struct file *fp;
416         int vfslocked, error;
417
418         error = getvnode(td->td_proc->p_fd, uap->filedes, &fp);
419         if (error == 0) {
420                 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
421                 error = vacl_aclcheck(td, fp->f_vnode, uap->type, uap->aclp);
422                 fdrop(fp, td);
423                 VFS_UNLOCK_GIANT(vfslocked);
424         }
425         return (error);
426 }
427
428 struct acl *
429 acl_alloc(int flags)
430 {
431         struct acl *aclp;
432
433         aclp = uma_zalloc(acl_zone, flags);
434
435         return (aclp);
436 }
437
438 void
439 acl_free(struct acl *aclp)
440 {
441
442         uma_zfree(acl_zone, aclp);
443 }
444
445 /* ARGUSED */
446
447 static void
448 aclinit(void *dummy __unused)
449 {
450
451         acl_zone = uma_zcreate("ACL UMA zone", sizeof(struct acl),
452             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
453 }
454 SYSINIT(acls, SI_SUB_ACL, SI_ORDER_FIRST, aclinit, NULL);