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 #include <sys/mplock2.h> 51 52 static int vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp); 53 static int vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp); 54 static int vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp); 55 56 /* 57 * These calls wrap the real vnode operations, and are called by the 58 * syscall code once the syscall has converted the path or file 59 * descriptor to a vnode (unlocked). The aclp pointer is assumed 60 * still to point to userland, so this should not be consumed within 61 * the kernel except by syscall code. Other code should directly 62 * invoke VOP_{SET,GET}ACL. 63 */ 64 65 /* 66 * Given a vnode, set its ACL. 67 */ 68 static int 69 vacl_set_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) 70 { 71 struct thread *td = curthread; 72 struct acl inkernacl; 73 struct ucred *ucred; 74 int error; 75 76 error = copyin(aclp, &inkernacl, sizeof(struct acl)); 77 if (error) 78 return(error); 79 ucred = td->td_ucred; 80 81 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 82 error = VOP_SETACL(vp, type, &inkernacl, ucred); 83 vn_unlock(vp); 84 return(error); 85 } 86 87 /* 88 * Given a vnode, get its ACL. 89 */ 90 static int 91 vacl_get_acl(struct vnode *vp, acl_type_t type, struct acl *aclp) 92 { 93 struct thread *td = curthread; 94 struct acl inkernelacl; 95 struct ucred *ucred; 96 int error; 97 98 ucred = td->td_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 ucred = td->td_ucred; 116 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 117 error = VOP_SETACL(vp, ACL_TYPE_DEFAULT, 0, ucred); 118 vn_unlock(vp); 119 return (error); 120 } 121 122 /* 123 * Given a vnode, check whether an ACL is appropriate for it 124 */ 125 static int 126 vacl_aclcheck(struct vnode *vp, acl_type_t type, struct acl *aclp) 127 { 128 struct thread *td = curthread; 129 struct ucred *ucred; 130 struct acl inkernelacl; 131 int error; 132 133 ucred = td->td_ucred; 134 error = copyin(aclp, &inkernelacl, sizeof(struct acl)); 135 if (error) 136 return(error); 137 error = VOP_ACLCHECK(vp, type, &inkernelacl, ucred); 138 return (error); 139 } 140 141 /* 142 * syscalls -- convert the path/fd to a vnode, and call vacl_whatever. 143 * Don't need to lock, as the vacl_ code will get/release any locks 144 * required. 145 */ 146 147 /* 148 * Given a file path, get an ACL for it 149 * 150 * MPALMOSTSAFE 151 */ 152 int 153 sys___acl_get_file(struct __acl_get_file_args *uap) 154 { 155 struct nlookupdata nd; 156 struct vnode *vp; 157 int error; 158 159 vp = NULL; 160 get_mplock(); 161 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 162 if (error == 0) 163 error = nlookup(&nd); 164 if (error == 0) 165 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 166 nlookup_done(&nd); 167 if (error == 0) { 168 error = vacl_get_acl(vp, uap->type, uap->aclp); 169 vrele(vp); 170 } 171 rel_mplock(); 172 173 return (error); 174 } 175 176 /* 177 * Given a file path, set an ACL for it 178 * 179 * MPALMOSTSAFE 180 */ 181 int 182 sys___acl_set_file(struct __acl_set_file_args *uap) 183 { 184 struct nlookupdata nd; 185 struct vnode *vp; 186 int error; 187 188 vp = NULL; 189 get_mplock(); 190 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 191 if (error == 0) 192 error = nlookup(&nd); 193 if (error == 0) 194 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 195 nlookup_done(&nd); 196 if (error == 0) { 197 error = vacl_set_acl(vp, uap->type, uap->aclp); 198 vrele(vp); 199 } 200 rel_mplock(); 201 202 return (error); 203 } 204 205 /* 206 * Given a file descriptor, get an ACL for it 207 * 208 * MPALMOSTSAFE 209 */ 210 int 211 sys___acl_get_fd(struct __acl_get_fd_args *uap) 212 { 213 struct thread *td = curthread; 214 struct file *fp; 215 int error; 216 217 KKASSERT(td->td_proc); 218 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 219 return(error); 220 get_mplock(); 221 error = vacl_get_acl((struct vnode *)fp->f_data, uap->type, uap->aclp); 222 rel_mplock(); 223 fdrop(fp); 224 225 return (error); 226 } 227 228 /* 229 * Given a file descriptor, set an ACL for it 230 * 231 * MPALMOSTSAFE 232 */ 233 int 234 sys___acl_set_fd(struct __acl_set_fd_args *uap) 235 { 236 struct thread *td = curthread; 237 struct file *fp; 238 int error; 239 240 KKASSERT(td->td_proc); 241 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 242 return(error); 243 get_mplock(); 244 error = vacl_set_acl((struct vnode *)fp->f_data, uap->type, uap->aclp); 245 rel_mplock(); 246 fdrop(fp); 247 return (error); 248 } 249 250 /* 251 * Given a file path, delete an ACL from it. 252 * 253 * MPALMOSTSAFE 254 */ 255 int 256 sys___acl_delete_file(struct __acl_delete_file_args *uap) 257 { 258 struct nlookupdata nd; 259 struct vnode *vp; 260 int error; 261 262 vp = NULL; 263 get_mplock(); 264 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 265 if (error == 0) 266 error = nlookup(&nd); 267 if (error == 0) 268 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 269 nlookup_done(&nd); 270 271 if (error == 0) { 272 error = vacl_delete(vp, uap->type); 273 vrele(vp); 274 } 275 rel_mplock(); 276 277 return (error); 278 } 279 280 /* 281 * Given a file path, delete an ACL from it. 282 * 283 * MPALMOSTSAFE 284 */ 285 int 286 sys___acl_delete_fd(struct __acl_delete_fd_args *uap) 287 { 288 struct thread *td = curthread; 289 struct file *fp; 290 int error; 291 292 KKASSERT(td->td_proc); 293 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 294 return(error); 295 get_mplock(); 296 error = vacl_delete((struct vnode *)fp->f_data, uap->type); 297 rel_mplock(); 298 fdrop(fp); 299 return (error); 300 } 301 302 /* 303 * Given a file path, check an ACL for it 304 * 305 * MPALMOSTSAFE 306 */ 307 int 308 sys___acl_aclcheck_file(struct __acl_aclcheck_file_args *uap) 309 { 310 struct nlookupdata nd; 311 struct vnode *vp; 312 int error; 313 314 vp = NULL; 315 get_mplock(); 316 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); 317 if (error == 0) 318 error = nlookup(&nd); 319 if (error == 0) 320 error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); 321 nlookup_done(&nd); 322 323 if (error == 0) { 324 error = vacl_aclcheck(vp, uap->type, uap->aclp); 325 vrele(vp); 326 } 327 rel_mplock(); 328 return (error); 329 } 330 331 /* 332 * Given a file descriptor, check an ACL for it 333 * 334 * MPALMOSTSAFE 335 */ 336 int 337 sys___acl_aclcheck_fd(struct __acl_aclcheck_fd_args *uap) 338 { 339 struct thread *td = curthread; 340 struct file *fp; 341 int error; 342 343 KKASSERT(td->td_proc); 344 if ((error = holdvnode(td->td_proc->p_fd, uap->filedes, &fp)) != 0) 345 return(error); 346 get_mplock(); 347 error = vacl_aclcheck((struct vnode *)fp->f_data, uap->type, uap->aclp); 348 rel_mplock(); 349 fdrop(fp); 350 return (error); 351 } 352 353