1 /*- 2 * Copyright (c) 1999, 2000 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/kern/kern_acl.c,v 1.2.2.1 2000/07/28 18:48:16 rwatson Exp $ 27 * $DragonFly: src/sys/kern/kern_acl.c,v 1.17 2007/02/19 00:51:54 swildner Exp $ 28 */ 29 30 /* 31 * Generic routines to support file system ACLs, at a syntactic level 32 * Semantics are the responsibility of the underlying file system 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/sysproto.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/vnode.h> 41 #include <sys/lock.h> 42 #include <sys/proc.h> 43 #include <sys/nlookup.h> 44 #include <sys/file.h> 45 #include <sys/sysent.h> 46 #include <sys/errno.h> 47 #include <sys/stat.h> 48 #include <sys/acl.h> 49 50 static int vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp); 51 static int vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp); 52 static int vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp); 53 54 /* 55 * These calls wrap the real vnode operations, and are called by the 56 * syscall code once the syscall has converted the path or file 57 * descriptor to a vnode (unlocked). The aclp pointer is assumed 58 * still to point to userland, so this should not be consumed within 59 * the kernel except by syscall code. Other code should directly 60 * invoke VOP_{SET,GET}ACL. 61 */ 62 63 /* 64 * Given a vnode, set its ACL. 65 */ 66 static int 67 vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) 68 { 69 struct thread *td = curthread; 70 struct acl inkernacl; 71 struct ucred *ucred; 72 int error; 73 74 error = copyin(aclp, &inkernacl, sizeof(struct acl)); 75 if (error) 76 return(error); 77 KKASSERT(td->td_proc); 78 ucred = td->td_proc->p_ucred; 79 80 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 81 error = VOP_SETACL(vp, type, &inkernacl, ucred); 82 vn_unlock(vp); 83 return(error); 84 } 85 86 /* 87 * Given a vnode, get its ACL. 88 */ 89 static int 90 vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) 91 { 92 struct thread *td = curthread; 93 struct acl inkernelacl; 94 struct ucred *ucred; 95 int error; 96 97 KKASSERT(td->td_proc); 98 ucred = td->td_proc->p_ucred; 99 error = VOP_GETACL(vp, type, &inkernelacl, ucred); 100 if (error == 0) 101 error = copyout(&inkernelacl, aclp, sizeof(struct acl)); 102 return (error); 103 } 104 105 /* 106 * Given a vnode, delete its ACL. 107 */ 108 static int 109 vacl_delete(struct vnode *vp, acl_type_t type) 110 { 111 struct thread *td = curthread; 112 struct ucred *ucred; 113 int error; 114 115 KKASSERT(td->td_proc); 116 ucred = td->td_proc->p_ucred; 117 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 118 error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred); 119 vn_unlock(vp); 120 return (error); 121 } 122 123 /* 124 * Given a vnode, check whether an ACL is appropriate for it 125 */ 126 static int 127 vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp) 128 { 129 struct thread *td = curthread; 130 struct ucred *ucred; 131 struct acl inkernelacl; 132 int error; 133 134 KKASSERT(td->td_proc); 135 ucred = td->td_proc->p_ucred; 136 error = copyin(aclp, &inkernelacl, sizeof(struct acl)); 137 if (error) 138 return(error); 139 error = VOP_ACLCHECK(vp, type, &inkernelacl, ucred); 140 return (error); 141 } 142 143 /* 144 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever. 145 * Don't need to lock, as the vacl_ code will get/release any locks 146 * required. 147 */ 148 149 /* 150 * Given a file path, get an ACL for it 151 * 152 * MPALMOSTSAFE 153 */ 154 int 155 sys___acl_get_file(struct __acl_get_file_args *uap) 156 { 157 struct nlookupdata nd; 158 struct vnode *vp; 159 int error; 160 161 vp = NULL; 162 get_mplock(); 163 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 164 if (error == 0) 165 error = nlookup(&nd); 166 if (error == 0) 167 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 168 nlookup_done(&nd); 169 if (error == 0) { 170 error = vacl_get_acl(vp, uap->type, uap->aclp); 171 vrele(vp); 172 } 173 rel_mplock(); 174 175 return (error); 176 } 177 178 /* 179 * Given a file path, set an ACL for it 180 * 181 * MPALMOSTSAFE 182 */ 183 int 184 sys___acl_set_file(struct __acl_set_file_args *uap) 185 { 186 struct nlookupdata nd; 187 struct vnode *vp; 188 int error; 189 190 vp = NULL; 191 get_mplock(); 192 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 193 if (error == 0) 194 error = nlookup(&nd); 195 if (error == 0) 196 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 197 nlookup_done(&nd); 198 if (error == 0) { 199 error = vacl_set_acl(vp, uap->type, uap->aclp); 200 vrele(vp); 201 } 202 rel_mplock(); 203 204 return (error); 205 } 206 207 /* 208 * Given a file descriptor, get an ACL for it 209 * 210 * MPALMOSTSAFE 211 */ 212 int 213 sys___acl_get_fd(struct __acl_get_fd_args *uap) 214 { 215 struct thread *td = curthread; 216 struct file *fp; 217 int error; 218 219 KKASSERT(td->td_proc); 220 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 221 return(error); 222 get_mplock(); 223 error = vacl_get_acl((struct vnode *)fp->f_data, uap->type, uap->aclp); 224 rel_mplock(); 225 fdrop(fp); 226 227 return (error); 228 } 229 230 /* 231 * Given a file descriptor, set an ACL for it 232 * 233 * MPALMOSTSAFE 234 */ 235 int 236 sys___acl_set_fd(struct __acl_set_fd_args *uap) 237 { 238 struct thread *td = curthread; 239 struct file *fp; 240 int error; 241 242 KKASSERT(td->td_proc); 243 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 244 return(error); 245 get_mplock(); 246 error = vacl_set_acl((struct vnode *)fp->f_data, uap->type, uap->aclp); 247 rel_mplock(); 248 fdrop(fp); 249 return (error); 250 } 251 252 /* 253 * Given a file path, delete an ACL from it. 254 * 255 * MPALMOSTSAFE 256 */ 257 int 258 sys___acl_delete_file(struct __acl_delete_file_args *uap) 259 { 260 struct nlookupdata nd; 261 struct vnode *vp; 262 int error; 263 264 vp = NULL; 265 get_mplock(); 266 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 267 if (error == 0) 268 error = nlookup(&nd); 269 if (error == 0) 270 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 271 nlookup_done(&nd); 272 273 if (error == 0) { 274 error = vacl_delete(vp, uap->type); 275 vrele(vp); 276 } 277 rel_mplock(); 278 279 return (error); 280 } 281 282 /* 283 * Given a file path, delete an ACL from it. 284 * 285 * MPALMOSTSAFE 286 */ 287 int 288 sys___acl_delete_fd(struct __acl_delete_fd_args *uap) 289 { 290 struct thread *td = curthread; 291 struct file *fp; 292 int error; 293 294 KKASSERT(td->td_proc); 295 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 296 return(error); 297 get_mplock(); 298 error = vacl_delete((struct vnode *)fp->f_data, uap->type); 299 rel_mplock(); 300 fdrop(fp); 301 return (error); 302 } 303 304 /* 305 * Given a file path, check an ACL for it 306 * 307 * MPALMOSTSAFE 308 */ 309 int 310 sys___acl_aclcheck_file(struct __acl_aclcheck_file_args *uap) 311 { 312 struct nlookupdata nd; 313 struct vnode *vp; 314 int error; 315 316 vp = NULL; 317 get_mplock(); 318 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 319 if (error == 0) 320 error = nlookup(&nd); 321 if (error == 0) 322 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 323 nlookup_done(&nd); 324 325 if (error == 0) { 326 error = vacl_aclcheck(vp, uap->type, uap->aclp); 327 vrele(vp); 328 } 329 rel_mplock(); 330 return (error); 331 } 332 333 /* 334 * Given a file descriptor, check an ACL for it 335 * 336 * MPALMOSTSAFE 337 */ 338 int 339 sys___acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap) 340 { 341 struct thread *td = curthread; 342 struct file *fp; 343 int error; 344 345 KKASSERT(td->td_proc); 346 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 347 return(error); 348 get_mplock(); 349 error = vacl_aclcheck((struct vnode *)fp->f_data, uap->type, uap->aclp); 350 rel_mplock(); 351 fdrop(fp); 352 return (error); 353 } 354 355