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; |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 59 | error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 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; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 77 | if (!inode_owner_or_capable(inode)) |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 78 | return -EPERM; |
| 79 | if (value) { |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 80 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 81 | if (IS_ERR(acl)) |
| 82 | return PTR_ERR(acl); |
| 83 | } |
| 84 | if (acl) { |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 85 | error = posix_acl_valid(acl); |
| 86 | if (error) |
| 87 | goto failed; |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 88 | switch (type) { |
| 89 | case ACL_TYPE_ACCESS: |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 90 | error = posix_acl_equiv_mode(acl, &inode->i_mode); |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 91 | if (error < 0) |
| 92 | goto failed; |
Jan Kara | dad5eb6 | 2010-08-17 12:42:13 +0200 | [diff] [blame] | 93 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 94 | if (error == 0) { |
| 95 | posix_acl_release(acl); |
| 96 | acl = NULL; |
| 97 | } |
| 98 | break; |
| 99 | case ACL_TYPE_DEFAULT: |
| 100 | if (!S_ISDIR(inode->i_mode)) { |
| 101 | error = -EINVAL; |
| 102 | goto failed; |
| 103 | } |
| 104 | break; |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 107 | set_cached_acl(inode, type, acl); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 108 | error = 0; |
| 109 | failed: |
| 110 | posix_acl_release(acl); |
| 111 | return error; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * generic_acl_init - Take care of acl inheritance at @inode create time |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 116 | * |
| 117 | * Files created inside a directory with a default ACL inherit the |
| 118 | * directory's default ACL. |
| 119 | */ |
| 120 | int |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 121 | generic_acl_init(struct inode *inode, struct inode *dir) |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 122 | { |
| 123 | struct posix_acl *acl = NULL; |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 124 | int error; |
| 125 | |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 126 | if (!S_ISLNK(inode->i_mode)) |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 127 | acl = get_cached_acl(dir, ACL_TYPE_DEFAULT); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 128 | if (acl) { |
Al Viro | 95203be | 2011-07-23 02:41:54 -0400 | [diff] [blame] | 129 | if (S_ISDIR(inode->i_mode)) |
| 130 | set_cached_acl(inode, ACL_TYPE_DEFAULT, acl); |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 131 | error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode); |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 132 | if (error < 0) |
| 133 | return error; |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 134 | if (error > 0) |
| 135 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 136 | } else { |
| 137 | inode->i_mode &= ~current_umask(); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 138 | } |
| 139 | error = 0; |
| 140 | |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 141 | posix_acl_release(acl); |
| 142 | return error; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * generic_acl_chmod - change the access acl of @inode upon chmod() |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 147 | * |
| 148 | * A chmod also changes the permissions of the owner, group/mask, and |
| 149 | * other ACL entries. |
| 150 | */ |
| 151 | int |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 152 | generic_acl_chmod(struct inode *inode) |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 153 | { |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 154 | struct posix_acl *acl; |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 155 | int error = 0; |
| 156 | |
| 157 | if (S_ISLNK(inode->i_mode)) |
| 158 | return -EOPNOTSUPP; |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 159 | acl = get_cached_acl(inode, ACL_TYPE_ACCESS); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 160 | if (acl) { |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 161 | error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); |
| 162 | if (error) |
| 163 | return error; |
| 164 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 165 | posix_acl_release(acl); |
Andreas Gruenbacher | f0c8bd1 | 2006-09-29 02:01:34 -0700 | [diff] [blame] | 166 | } |
| 167 | return error; |
| 168 | } |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 169 | |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 170 | const struct xattr_handler generic_acl_access_handler = { |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 171 | .prefix = POSIX_ACL_XATTR_ACCESS, |
| 172 | .flags = ACL_TYPE_ACCESS, |
| 173 | .list = generic_acl_list, |
| 174 | .get = generic_acl_get, |
| 175 | .set = generic_acl_set, |
| 176 | }; |
| 177 | |
Stephen Hemminger | bb43545 | 2010-05-13 17:53:14 -0700 | [diff] [blame] | 178 | const struct xattr_handler generic_acl_default_handler = { |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 179 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
| 180 | .flags = ACL_TYPE_DEFAULT, |
| 181 | .list = generic_acl_list, |
| 182 | .get = generic_acl_get, |
| 183 | .set = generic_acl_set, |
| 184 | }; |