Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/acl.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 5 | */ |
| 6 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 7 | #include <linux/quotaops.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 8 | #include "ext4_jbd2.h" |
| 9 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 10 | #include "xattr.h" |
| 11 | #include "acl.h" |
| 12 | |
| 13 | /* |
| 14 | * Convert from filesystem to in-memory representation. |
| 15 | */ |
| 16 | static struct posix_acl * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 17 | ext4_acl_from_disk(const void *value, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 18 | { |
| 19 | const char *end = (char *)value + size; |
| 20 | int n, count; |
| 21 | struct posix_acl *acl; |
| 22 | |
| 23 | if (!value) |
| 24 | return NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 25 | if (size < sizeof(ext4_acl_header)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 26 | return ERR_PTR(-EINVAL); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 27 | if (((ext4_acl_header *)value)->a_version != |
| 28 | cpu_to_le32(EXT4_ACL_VERSION)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 29 | return ERR_PTR(-EINVAL); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 30 | value = (char *)value + sizeof(ext4_acl_header); |
| 31 | count = ext4_acl_count(size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 32 | if (count < 0) |
| 33 | return ERR_PTR(-EINVAL); |
| 34 | if (count == 0) |
| 35 | return NULL; |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 36 | acl = posix_acl_alloc(count, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 37 | if (!acl) |
| 38 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 39 | for (n = 0; n < count; n++) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 40 | ext4_acl_entry *entry = |
| 41 | (ext4_acl_entry *)value; |
| 42 | if ((char *)value + sizeof(ext4_acl_entry_short) > end) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 43 | goto fail; |
| 44 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
| 45 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 46 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 47 | switch (acl->a_entries[n].e_tag) { |
| 48 | case ACL_USER_OBJ: |
| 49 | case ACL_GROUP_OBJ: |
| 50 | case ACL_MASK: |
| 51 | case ACL_OTHER: |
| 52 | value = (char *)value + |
| 53 | sizeof(ext4_acl_entry_short); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 54 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 55 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 56 | case ACL_USER: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 57 | value = (char *)value + sizeof(ext4_acl_entry); |
| 58 | if ((char *)value > end) |
| 59 | goto fail; |
| 60 | acl->a_entries[n].e_uid = |
| 61 | make_kuid(&init_user_ns, |
| 62 | le32_to_cpu(entry->e_id)); |
| 63 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 64 | case ACL_GROUP: |
| 65 | value = (char *)value + sizeof(ext4_acl_entry); |
| 66 | if ((char *)value > end) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 67 | goto fail; |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 68 | acl->a_entries[n].e_gid = |
| 69 | make_kgid(&init_user_ns, |
| 70 | le32_to_cpu(entry->e_id)); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 71 | break; |
| 72 | |
| 73 | default: |
| 74 | goto fail; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | if (value != end) |
| 78 | goto fail; |
| 79 | return acl; |
| 80 | |
| 81 | fail: |
| 82 | posix_acl_release(acl); |
| 83 | return ERR_PTR(-EINVAL); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Convert from in-memory to filesystem representation. |
| 88 | */ |
| 89 | static void * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 90 | ext4_acl_to_disk(const struct posix_acl *acl, size_t *size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 91 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 92 | ext4_acl_header *ext_acl; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 93 | char *e; |
| 94 | size_t n; |
| 95 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 96 | *size = ext4_acl_size(acl->a_count); |
| 97 | ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count * |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 98 | sizeof(ext4_acl_entry), GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 99 | if (!ext_acl) |
| 100 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 101 | ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION); |
| 102 | e = (char *)ext_acl + sizeof(ext4_acl_header); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 103 | for (n = 0; n < acl->a_count; n++) { |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 104 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 105 | ext4_acl_entry *entry = (ext4_acl_entry *)e; |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 106 | entry->e_tag = cpu_to_le16(acl_e->e_tag); |
| 107 | entry->e_perm = cpu_to_le16(acl_e->e_perm); |
| 108 | switch (acl_e->e_tag) { |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 109 | case ACL_USER: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 110 | entry->e_id = cpu_to_le32( |
| 111 | from_kuid(&init_user_ns, acl_e->e_uid)); |
| 112 | e += sizeof(ext4_acl_entry); |
| 113 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 114 | case ACL_GROUP: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 115 | entry->e_id = cpu_to_le32( |
| 116 | from_kgid(&init_user_ns, acl_e->e_gid)); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 117 | e += sizeof(ext4_acl_entry); |
| 118 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 119 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 120 | case ACL_USER_OBJ: |
| 121 | case ACL_GROUP_OBJ: |
| 122 | case ACL_MASK: |
| 123 | case ACL_OTHER: |
| 124 | e += sizeof(ext4_acl_entry_short); |
| 125 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 126 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 127 | default: |
| 128 | goto fail; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | return (char *)ext_acl; |
| 132 | |
| 133 | fail: |
| 134 | kfree(ext_acl); |
| 135 | return ERR_PTR(-EINVAL); |
| 136 | } |
| 137 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 138 | /* |
| 139 | * Inode operation get_posix_acl(). |
| 140 | * |
| 141 | * inode->i_mutex: don't care |
| 142 | */ |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 143 | struct posix_acl * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 144 | ext4_get_acl(struct inode *inode, int type) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 145 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 146 | int name_index; |
| 147 | char *value = NULL; |
| 148 | struct posix_acl *acl; |
| 149 | int retval; |
| 150 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 151 | switch (type) { |
| 152 | case ACL_TYPE_ACCESS: |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 153 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 154 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 155 | case ACL_TYPE_DEFAULT: |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 156 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 157 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 158 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 159 | BUG(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 160 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 161 | retval = ext4_xattr_get(inode, name_index, "", NULL, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 162 | if (retval > 0) { |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 163 | value = kmalloc(retval, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 164 | if (!value) |
| 165 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 166 | retval = ext4_xattr_get(inode, name_index, "", value, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 167 | } |
| 168 | if (retval > 0) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 169 | acl = ext4_acl_from_disk(value, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 170 | else if (retval == -ENODATA || retval == -ENOSYS) |
| 171 | acl = NULL; |
| 172 | else |
| 173 | acl = ERR_PTR(retval); |
| 174 | kfree(value); |
| 175 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 176 | return acl; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Set the access or default ACL of an inode. |
| 181 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 182 | * inode->i_mutex: down unless called from ext4_new_inode |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 183 | */ |
| 184 | static int |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 185 | __ext4_set_acl(handle_t *handle, struct inode *inode, int type, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 186 | struct posix_acl *acl) |
| 187 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 188 | int name_index; |
| 189 | void *value = NULL; |
| 190 | size_t size = 0; |
| 191 | int error; |
| 192 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 193 | switch (type) { |
| 194 | case ACL_TYPE_ACCESS: |
| 195 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 196 | if (acl) { |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 197 | error = posix_acl_update_mode(inode, &inode->i_mode, &acl); |
| 198 | if (error) |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 199 | return error; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 200 | inode->i_ctime = current_time(inode); |
Jan Kara | 0739310 | 2016-09-19 17:39:09 +0200 | [diff] [blame] | 201 | ext4_mark_inode_dirty(handle, inode); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 202 | } |
| 203 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 204 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 205 | case ACL_TYPE_DEFAULT: |
| 206 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 207 | if (!S_ISDIR(inode->i_mode)) |
| 208 | return acl ? -EACCES : 0; |
| 209 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 210 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 211 | default: |
| 212 | return -EINVAL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 213 | } |
| 214 | if (acl) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 215 | value = ext4_acl_to_disk(acl, &size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 216 | if (IS_ERR(value)) |
| 217 | return (int)PTR_ERR(value); |
| 218 | } |
| 219 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 220 | error = ext4_xattr_set_handle(handle, inode, name_index, "", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 221 | value, size, 0); |
| 222 | |
| 223 | kfree(value); |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 224 | if (!error) |
| 225 | set_cached_acl(inode, type, acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 226 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 227 | return error; |
| 228 | } |
| 229 | |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 230 | int |
| 231 | ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
| 232 | { |
| 233 | handle_t *handle; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame^] | 234 | int error, credits, retries = 0; |
| 235 | size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0; |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 236 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 237 | error = dquot_initialize(inode); |
| 238 | if (error) |
| 239 | return error; |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 240 | retry: |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame^] | 241 | credits = ext4_xattr_set_credits(inode, acl_size); |
| 242 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 243 | if (IS_ERR(handle)) |
| 244 | return PTR_ERR(handle); |
| 245 | |
| 246 | error = __ext4_set_acl(handle, inode, type, acl); |
| 247 | ext4_journal_stop(handle); |
| 248 | if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 249 | goto retry; |
| 250 | return error; |
| 251 | } |
| 252 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 253 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 254 | * Initialize the ACLs of a new inode. Called from ext4_new_inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 255 | * |
| 256 | * dir->i_mutex: down |
| 257 | * inode->i_mutex: up (access to inode is still exclusive) |
| 258 | */ |
| 259 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 260 | ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 261 | { |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 262 | struct posix_acl *default_acl, *acl; |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 263 | int error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 264 | |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 265 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 266 | if (error) |
| 267 | return error; |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 268 | |
| 269 | if (default_acl) { |
| 270 | error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT, |
| 271 | default_acl); |
| 272 | posix_acl_release(default_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 273 | } |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 274 | if (acl) { |
| 275 | if (!error) |
| 276 | error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, |
| 277 | acl); |
| 278 | posix_acl_release(acl); |
Eugene Shatokhin | 24ec19b | 2012-11-08 15:11:11 -0500 | [diff] [blame] | 279 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 280 | return error; |
| 281 | } |