Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext2/acl.c |
| 3 | * |
| 4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 5 | */ |
| 6 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 7 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include "ext2.h" |
| 13 | #include "xattr.h" |
| 14 | #include "acl.h" |
| 15 | |
| 16 | /* |
| 17 | * Convert from filesystem to in-memory representation. |
| 18 | */ |
| 19 | static struct posix_acl * |
| 20 | ext2_acl_from_disk(const void *value, size_t size) |
| 21 | { |
| 22 | const char *end = (char *)value + size; |
| 23 | int n, count; |
| 24 | struct posix_acl *acl; |
| 25 | |
| 26 | if (!value) |
| 27 | return NULL; |
| 28 | if (size < sizeof(ext2_acl_header)) |
| 29 | return ERR_PTR(-EINVAL); |
| 30 | if (((ext2_acl_header *)value)->a_version != |
| 31 | cpu_to_le32(EXT2_ACL_VERSION)) |
| 32 | return ERR_PTR(-EINVAL); |
| 33 | value = (char *)value + sizeof(ext2_acl_header); |
| 34 | count = ext2_acl_count(size); |
| 35 | if (count < 0) |
| 36 | return ERR_PTR(-EINVAL); |
| 37 | if (count == 0) |
| 38 | return NULL; |
| 39 | acl = posix_acl_alloc(count, GFP_KERNEL); |
| 40 | if (!acl) |
| 41 | return ERR_PTR(-ENOMEM); |
| 42 | for (n=0; n < count; n++) { |
| 43 | ext2_acl_entry *entry = |
| 44 | (ext2_acl_entry *)value; |
| 45 | if ((char *)value + sizeof(ext2_acl_entry_short) > end) |
| 46 | goto fail; |
| 47 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
| 48 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
| 49 | switch(acl->a_entries[n].e_tag) { |
| 50 | case ACL_USER_OBJ: |
| 51 | case ACL_GROUP_OBJ: |
| 52 | case ACL_MASK: |
| 53 | case ACL_OTHER: |
| 54 | value = (char *)value + |
| 55 | sizeof(ext2_acl_entry_short); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | break; |
| 57 | |
| 58 | case ACL_USER: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 59 | value = (char *)value + sizeof(ext2_acl_entry); |
| 60 | if ((char *)value > end) |
| 61 | goto fail; |
| 62 | acl->a_entries[n].e_uid = |
| 63 | make_kuid(&init_user_ns, |
| 64 | le32_to_cpu(entry->e_id)); |
| 65 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | case ACL_GROUP: |
| 67 | value = (char *)value + sizeof(ext2_acl_entry); |
| 68 | if ((char *)value > end) |
| 69 | goto fail; |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 70 | acl->a_entries[n].e_gid = |
| 71 | make_kgid(&init_user_ns, |
| 72 | le32_to_cpu(entry->e_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | break; |
| 74 | |
| 75 | default: |
| 76 | goto fail; |
| 77 | } |
| 78 | } |
| 79 | if (value != end) |
| 80 | goto fail; |
| 81 | return acl; |
| 82 | |
| 83 | fail: |
| 84 | posix_acl_release(acl); |
| 85 | return ERR_PTR(-EINVAL); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Convert from in-memory to filesystem representation. |
| 90 | */ |
| 91 | static void * |
| 92 | ext2_acl_to_disk(const struct posix_acl *acl, size_t *size) |
| 93 | { |
| 94 | ext2_acl_header *ext_acl; |
| 95 | char *e; |
| 96 | size_t n; |
| 97 | |
| 98 | *size = ext2_acl_size(acl->a_count); |
Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 99 | ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count * |
| 100 | sizeof(ext2_acl_entry), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (!ext_acl) |
| 102 | return ERR_PTR(-ENOMEM); |
| 103 | ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION); |
| 104 | e = (char *)ext_acl + sizeof(ext2_acl_header); |
| 105 | for (n=0; n < acl->a_count; n++) { |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 106 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | ext2_acl_entry *entry = (ext2_acl_entry *)e; |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 108 | entry->e_tag = cpu_to_le16(acl_e->e_tag); |
| 109 | entry->e_perm = cpu_to_le16(acl_e->e_perm); |
| 110 | switch(acl_e->e_tag) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | case ACL_USER: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 112 | entry->e_id = cpu_to_le32( |
| 113 | from_kuid(&init_user_ns, acl_e->e_uid)); |
| 114 | e += sizeof(ext2_acl_entry); |
| 115 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | case ACL_GROUP: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 117 | entry->e_id = cpu_to_le32( |
| 118 | from_kgid(&init_user_ns, acl_e->e_gid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | e += sizeof(ext2_acl_entry); |
| 120 | break; |
| 121 | |
| 122 | case ACL_USER_OBJ: |
| 123 | case ACL_GROUP_OBJ: |
| 124 | case ACL_MASK: |
| 125 | case ACL_OTHER: |
| 126 | e += sizeof(ext2_acl_entry_short); |
| 127 | break; |
| 128 | |
| 129 | default: |
| 130 | goto fail; |
| 131 | } |
| 132 | } |
| 133 | return (char *)ext_acl; |
| 134 | |
| 135 | fail: |
| 136 | kfree(ext_acl); |
| 137 | return ERR_PTR(-EINVAL); |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 141 | * inode->i_mutex: don't care |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | */ |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 143 | struct posix_acl * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | ext2_get_acl(struct inode *inode, int type) |
| 145 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | int name_index; |
| 147 | char *value = NULL; |
| 148 | struct posix_acl *acl; |
| 149 | int retval; |
| 150 | |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 151 | switch (type) { |
| 152 | case ACL_TYPE_ACCESS: |
| 153 | name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 154 | break; |
| 155 | case ACL_TYPE_DEFAULT: |
| 156 | name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 157 | break; |
| 158 | default: |
| 159 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | retval = ext2_xattr_get(inode, name_index, "", NULL, 0); |
| 162 | if (retval > 0) { |
| 163 | value = kmalloc(retval, GFP_KERNEL); |
| 164 | if (!value) |
| 165 | return ERR_PTR(-ENOMEM); |
| 166 | retval = ext2_xattr_get(inode, name_index, "", value, retval); |
| 167 | } |
| 168 | if (retval > 0) |
| 169 | acl = ext2_acl_from_disk(value, retval); |
| 170 | else if (retval == -ENODATA || retval == -ENOSYS) |
| 171 | acl = NULL; |
| 172 | else |
| 173 | acl = ERR_PTR(retval); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 174 | kfree(value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 176 | if (!IS_ERR(acl)) |
| 177 | set_cached_acl(inode, type, acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return acl; |
| 180 | } |
| 181 | |
| 182 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 183 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | */ |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 185 | int |
| 186 | ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | int name_index; |
| 189 | void *value = NULL; |
Andreas Gruenbacher | dfa0859 | 2006-02-03 03:04:13 -0800 | [diff] [blame] | 190 | size_t size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | int error; |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | switch(type) { |
| 194 | case ACL_TYPE_ACCESS: |
| 195 | name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 196 | if (acl) { |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 197 | error = posix_acl_equiv_mode(acl, &inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | if (error < 0) |
| 199 | return error; |
| 200 | else { |
Jan Kara | 523825b | 2010-06-02 16:26:51 +0200 | [diff] [blame] | 201 | inode->i_ctime = CURRENT_TIME_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | mark_inode_dirty(inode); |
| 203 | if (error == 0) |
| 204 | acl = NULL; |
| 205 | } |
| 206 | } |
| 207 | break; |
| 208 | |
| 209 | case ACL_TYPE_DEFAULT: |
| 210 | name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 211 | if (!S_ISDIR(inode->i_mode)) |
| 212 | return acl ? -EACCES : 0; |
| 213 | break; |
| 214 | |
| 215 | default: |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | if (acl) { |
| 219 | value = ext2_acl_to_disk(acl, &size); |
| 220 | if (IS_ERR(value)) |
| 221 | return (int)PTR_ERR(value); |
| 222 | } |
| 223 | |
| 224 | error = ext2_xattr_set(inode, name_index, "", value, size, 0); |
| 225 | |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 226 | kfree(value); |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 227 | if (!error) |
| 228 | set_cached_acl(inode, type, acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return error; |
| 230 | } |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | /* |
| 233 | * Initialize the ACLs of a new inode. Called from ext2_new_inode. |
| 234 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 235 | * dir->i_mutex: down |
| 236 | * inode->i_mutex: up (access to inode is still exclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | */ |
| 238 | int |
| 239 | ext2_init_acl(struct inode *inode, struct inode *dir) |
| 240 | { |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 241 | struct posix_acl *default_acl, *acl; |
| 242 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 244 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 245 | if (error) |
| 246 | return error; |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 247 | |
| 248 | if (default_acl) { |
| 249 | error = ext2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); |
| 250 | posix_acl_release(default_acl); |
| 251 | } |
| 252 | if (acl) { |
| 253 | if (!error) |
| 254 | error = ext2_set_acl(inode, acl, ACL_TYPE_ACCESS); |
| 255 | posix_acl_release(acl); |
| 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return error; |
| 258 | } |