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