| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 1 | /* | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 2 | * (C) 2005 Andreas Gruenbacher <agruen@suse.de> | 
|  | 3 | * | 
|  | 4 | * This file is released under the GPL. | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 5 | * | 
|  | 6 | * Generic ACL support for in-memory filesystems. | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 7 | */ | 
|  | 8 |  | 
|  | 9 | #include <linux/sched.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/gfp.h> | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 11 | #include <linux/fs.h> | 
|  | 12 | #include <linux/generic_acl.h> | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 13 | #include <linux/posix_acl.h> | 
|  | 14 | #include <linux/posix_acl_xattr.h> | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 15 |  | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 16 |  | 
|  | 17 | static size_t | 
|  | 18 | generic_acl_list(struct dentry *dentry, char *list, size_t list_size, | 
|  | 19 | const char *name, size_t name_len, int type) | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 20 | { | 
|  | 21 | struct posix_acl *acl; | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 22 | const char *xname; | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 23 | size_t size; | 
|  | 24 |  | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 25 | acl = get_cached_acl(dentry->d_inode, type); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 26 | if (!acl) | 
|  | 27 | return 0; | 
|  | 28 | posix_acl_release(acl); | 
|  | 29 |  | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 30 | switch (type) { | 
|  | 31 | case ACL_TYPE_ACCESS: | 
|  | 32 | xname = POSIX_ACL_XATTR_ACCESS; | 
|  | 33 | break; | 
|  | 34 | case ACL_TYPE_DEFAULT: | 
|  | 35 | xname = POSIX_ACL_XATTR_DEFAULT; | 
|  | 36 | break; | 
|  | 37 | default: | 
|  | 38 | return 0; | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 39 | } | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 40 | size = strlen(xname) + 1; | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 41 | if (list && size <= list_size) | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 42 | memcpy(list, xname, size); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 43 | return size; | 
|  | 44 | } | 
|  | 45 |  | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 46 | static int | 
|  | 47 | generic_acl_get(struct dentry *dentry, const char *name, void *buffer, | 
|  | 48 | size_t size, int type) | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 49 | { | 
|  | 50 | struct posix_acl *acl; | 
|  | 51 | int error; | 
|  | 52 |  | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 53 | if (strcmp(name, "") != 0) | 
|  | 54 | return -EINVAL; | 
|  | 55 |  | 
|  | 56 | acl = get_cached_acl(dentry->d_inode, type); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 57 | if (!acl) | 
|  | 58 | return -ENODATA; | 
|  | 59 | error = posix_acl_to_xattr(acl, buffer, size); | 
|  | 60 | posix_acl_release(acl); | 
|  | 61 |  | 
|  | 62 | return error; | 
|  | 63 | } | 
|  | 64 |  | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 65 | static int | 
|  | 66 | generic_acl_set(struct dentry *dentry, const char *name, const void *value, | 
|  | 67 | size_t size, int flags, int type) | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 68 | { | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 69 | struct inode *inode = dentry->d_inode; | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 70 | struct posix_acl *acl = NULL; | 
|  | 71 | int error; | 
|  | 72 |  | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 73 | if (strcmp(name, "") != 0) | 
|  | 74 | return -EINVAL; | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 75 | if (S_ISLNK(inode->i_mode)) | 
|  | 76 | return -EOPNOTSUPP; | 
| Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 77 | if (!is_owner_or_cap(inode)) | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 78 | return -EPERM; | 
|  | 79 | if (value) { | 
|  | 80 | acl = posix_acl_from_xattr(value, size); | 
|  | 81 | if (IS_ERR(acl)) | 
|  | 82 | return PTR_ERR(acl); | 
|  | 83 | } | 
|  | 84 | if (acl) { | 
|  | 85 | mode_t mode; | 
|  | 86 |  | 
|  | 87 | error = posix_acl_valid(acl); | 
|  | 88 | if (error) | 
|  | 89 | goto failed; | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 90 | switch (type) { | 
|  | 91 | case ACL_TYPE_ACCESS: | 
|  | 92 | mode = inode->i_mode; | 
|  | 93 | error = posix_acl_equiv_mode(acl, &mode); | 
|  | 94 | if (error < 0) | 
|  | 95 | goto failed; | 
|  | 96 | inode->i_mode = mode; | 
| Jan Kara | dad5eb6 | 2010-08-17 12:42:13 +0200 | [diff] [blame] | 97 | inode->i_ctime = CURRENT_TIME; | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 98 | if (error == 0) { | 
|  | 99 | posix_acl_release(acl); | 
|  | 100 | acl = NULL; | 
|  | 101 | } | 
|  | 102 | break; | 
|  | 103 | case ACL_TYPE_DEFAULT: | 
|  | 104 | if (!S_ISDIR(inode->i_mode)) { | 
|  | 105 | error = -EINVAL; | 
|  | 106 | goto failed; | 
|  | 107 | } | 
|  | 108 | break; | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 109 | } | 
|  | 110 | } | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 111 | set_cached_acl(inode, type, acl); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 112 | error = 0; | 
|  | 113 | failed: | 
|  | 114 | posix_acl_release(acl); | 
|  | 115 | return error; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | /** | 
|  | 119 | * generic_acl_init  -  Take care of acl inheritance at @inode create time | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 120 | * | 
|  | 121 | * Files created inside a directory with a default ACL inherit the | 
|  | 122 | * directory's default ACL. | 
|  | 123 | */ | 
|  | 124 | int | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 125 | generic_acl_init(struct inode *inode, struct inode *dir) | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 126 | { | 
|  | 127 | struct posix_acl *acl = NULL; | 
|  | 128 | mode_t mode = inode->i_mode; | 
|  | 129 | int error; | 
|  | 130 |  | 
| Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 131 | inode->i_mode = mode & ~current_umask(); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 132 | if (!S_ISLNK(inode->i_mode)) | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 133 | acl = get_cached_acl(dir, ACL_TYPE_DEFAULT); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 134 | if (acl) { | 
|  | 135 | struct posix_acl *clone; | 
|  | 136 |  | 
|  | 137 | if (S_ISDIR(inode->i_mode)) { | 
|  | 138 | clone = posix_acl_clone(acl, GFP_KERNEL); | 
|  | 139 | error = -ENOMEM; | 
|  | 140 | if (!clone) | 
|  | 141 | goto cleanup; | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 142 | set_cached_acl(inode, ACL_TYPE_DEFAULT, clone); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 143 | posix_acl_release(clone); | 
|  | 144 | } | 
|  | 145 | clone = posix_acl_clone(acl, GFP_KERNEL); | 
|  | 146 | error = -ENOMEM; | 
|  | 147 | if (!clone) | 
|  | 148 | goto cleanup; | 
|  | 149 | error = posix_acl_create_masq(clone, &mode); | 
|  | 150 | if (error >= 0) { | 
|  | 151 | inode->i_mode = mode; | 
|  | 152 | if (error > 0) | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 153 | set_cached_acl(inode, ACL_TYPE_ACCESS, clone); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 154 | } | 
|  | 155 | posix_acl_release(clone); | 
|  | 156 | } | 
|  | 157 | error = 0; | 
|  | 158 |  | 
|  | 159 | cleanup: | 
|  | 160 | posix_acl_release(acl); | 
|  | 161 | return error; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | /** | 
|  | 165 | * generic_acl_chmod  -  change the access acl of @inode upon chmod() | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 166 | * | 
|  | 167 | * A chmod also changes the permissions of the owner, group/mask, and | 
|  | 168 | * other ACL entries. | 
|  | 169 | */ | 
|  | 170 | int | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 171 | generic_acl_chmod(struct inode *inode) | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 172 | { | 
|  | 173 | struct posix_acl *acl, *clone; | 
|  | 174 | int error = 0; | 
|  | 175 |  | 
|  | 176 | if (S_ISLNK(inode->i_mode)) | 
|  | 177 | return -EOPNOTSUPP; | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 178 | acl = get_cached_acl(inode, ACL_TYPE_ACCESS); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 179 | if (acl) { | 
|  | 180 | clone = posix_acl_clone(acl, GFP_KERNEL); | 
|  | 181 | posix_acl_release(acl); | 
|  | 182 | if (!clone) | 
|  | 183 | return -ENOMEM; | 
|  | 184 | error = posix_acl_chmod_masq(clone, inode->i_mode); | 
|  | 185 | if (!error) | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 186 | set_cached_acl(inode, ACL_TYPE_ACCESS, clone); | 
| Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 187 | posix_acl_release(clone); | 
|  | 188 | } | 
|  | 189 | return error; | 
|  | 190 | } | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 191 |  | 
|  | 192 | int | 
| Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 193 | generic_check_acl(struct inode *inode, int mask, unsigned int flags) | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 194 | { | 
| Nick Piggin | 1e1743e | 2011-01-07 17:49:59 +1100 | [diff] [blame] | 195 | if (flags & IPERM_FLAG_RCU) { | 
|  | 196 | if (!negative_cached_acl(inode, ACL_TYPE_ACCESS)) | 
|  | 197 | return -ECHILD; | 
|  | 198 | } else { | 
|  | 199 | struct posix_acl *acl; | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 200 |  | 
| Nick Piggin | 1e1743e | 2011-01-07 17:49:59 +1100 | [diff] [blame] | 201 | acl = get_cached_acl(inode, ACL_TYPE_ACCESS); | 
|  | 202 | if (acl) { | 
|  | 203 | int error = posix_acl_permission(inode, acl, mask); | 
|  | 204 | posix_acl_release(acl); | 
|  | 205 | return error; | 
|  | 206 | } | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 207 | } | 
|  | 208 | return -EAGAIN; | 
|  | 209 | } | 
|  | 210 |  | 
| Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 211 | const struct xattr_handler generic_acl_access_handler = { | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 212 | .prefix = POSIX_ACL_XATTR_ACCESS, | 
|  | 213 | .flags	= ACL_TYPE_ACCESS, | 
|  | 214 | .list	= generic_acl_list, | 
|  | 215 | .get	= generic_acl_get, | 
|  | 216 | .set	= generic_acl_set, | 
|  | 217 | }; | 
|  | 218 |  | 
| Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 219 | const struct xattr_handler generic_acl_default_handler = { | 
| Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 220 | .prefix = POSIX_ACL_XATTR_DEFAULT, | 
|  | 221 | .flags	= ACL_TYPE_DEFAULT, | 
|  | 222 | .list	= generic_acl_list, | 
|  | 223 | .get	= generic_acl_get, | 
|  | 224 | .set	= generic_acl_set, | 
|  | 225 | }; |