]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
ac27a0ec | 2 | /* |
617ba13b | 3 | * linux/fs/ext4/acl.c |
ac27a0ec DK |
4 | * |
5 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> | |
6 | */ | |
7 | ||
b8cb5a54 | 8 | #include <linux/quotaops.h> |
3dcf5451 CH |
9 | #include "ext4_jbd2.h" |
10 | #include "ext4.h" | |
ac27a0ec DK |
11 | #include "xattr.h" |
12 | #include "acl.h" | |
13 | ||
14 | /* | |
15 | * Convert from filesystem to in-memory representation. | |
16 | */ | |
17 | static struct posix_acl * | |
78a8da10 | 18 | ext4_acl_from_disk(struct super_block *sb, const void *value, size_t size) |
ac27a0ec DK |
19 | { |
20 | const char *end = (char *)value + size; | |
21 | int n, count; | |
22 | struct posix_acl *acl; | |
23 | ||
24 | if (!value) | |
25 | return NULL; | |
617ba13b | 26 | if (size < sizeof(ext4_acl_header)) |
ac27a0ec | 27 | return ERR_PTR(-EINVAL); |
617ba13b MC |
28 | if (((ext4_acl_header *)value)->a_version != |
29 | cpu_to_le32(EXT4_ACL_VERSION)) | |
ac27a0ec | 30 | return ERR_PTR(-EINVAL); |
617ba13b MC |
31 | value = (char *)value + sizeof(ext4_acl_header); |
32 | count = ext4_acl_count(size); | |
ac27a0ec DK |
33 | if (count < 0) |
34 | return ERR_PTR(-EINVAL); | |
35 | if (count == 0) | |
36 | return NULL; | |
216553c4 | 37 | acl = posix_acl_alloc(count, GFP_NOFS); |
ac27a0ec DK |
38 | if (!acl) |
39 | return ERR_PTR(-ENOMEM); | |
2b2d6d01 | 40 | for (n = 0; n < count; n++) { |
617ba13b MC |
41 | ext4_acl_entry *entry = |
42 | (ext4_acl_entry *)value; | |
43 | if ((char *)value + sizeof(ext4_acl_entry_short) > end) | |
ac27a0ec DK |
44 | goto fail; |
45 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); | |
46 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); | |
2b2d6d01 TT |
47 | |
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(ext4_acl_entry_short); | |
2b2d6d01 TT |
55 | break; |
56 | ||
57 | case ACL_USER: | |
af84df93 EB |
58 | value = (char *)value + sizeof(ext4_acl_entry); |
59 | if ((char *)value > end) | |
60 | goto fail; | |
61 | acl->a_entries[n].e_uid = | |
78a8da10 | 62 | make_kuid(sb->s_user_ns, |
af84df93 | 63 | le32_to_cpu(entry->e_id)); |
78a8da10 SF |
64 | if (!uid_valid(acl->a_entries[n].e_uid)) |
65 | goto fail; | |
af84df93 | 66 | break; |
2b2d6d01 TT |
67 | case ACL_GROUP: |
68 | value = (char *)value + sizeof(ext4_acl_entry); | |
69 | if ((char *)value > end) | |
ac27a0ec | 70 | goto fail; |
af84df93 | 71 | acl->a_entries[n].e_gid = |
78a8da10 | 72 | make_kgid(sb->s_user_ns, |
af84df93 | 73 | le32_to_cpu(entry->e_id)); |
78a8da10 SF |
74 | if (!gid_valid(acl->a_entries[n].e_gid)) |
75 | goto fail; | |
2b2d6d01 TT |
76 | break; |
77 | ||
78 | default: | |
79 | goto fail; | |
ac27a0ec DK |
80 | } |
81 | } | |
82 | if (value != end) | |
83 | goto fail; | |
84 | return acl; | |
85 | ||
86 | fail: | |
87 | posix_acl_release(acl); | |
88 | return ERR_PTR(-EINVAL); | |
89 | } | |
90 | ||
91 | /* | |
92 | * Convert from in-memory to filesystem representation. | |
93 | */ | |
94 | static void * | |
78a8da10 SF |
95 | ext4_acl_to_disk(struct super_block *sb, const struct posix_acl *acl, |
96 | size_t *size) | |
ac27a0ec | 97 | { |
617ba13b | 98 | ext4_acl_header *ext_acl; |
ac27a0ec DK |
99 | char *e; |
100 | size_t n; | |
78a8da10 SF |
101 | uid_t uid; |
102 | gid_t gid; | |
ac27a0ec | 103 | |
617ba13b MC |
104 | *size = ext4_acl_size(acl->a_count); |
105 | ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count * | |
216553c4 | 106 | sizeof(ext4_acl_entry), GFP_NOFS); |
ac27a0ec DK |
107 | if (!ext_acl) |
108 | return ERR_PTR(-ENOMEM); | |
617ba13b MC |
109 | ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION); |
110 | e = (char *)ext_acl + sizeof(ext4_acl_header); | |
2b2d6d01 | 111 | for (n = 0; n < acl->a_count; n++) { |
af84df93 | 112 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
617ba13b | 113 | ext4_acl_entry *entry = (ext4_acl_entry *)e; |
af84df93 EB |
114 | entry->e_tag = cpu_to_le16(acl_e->e_tag); |
115 | entry->e_perm = cpu_to_le16(acl_e->e_perm); | |
116 | switch (acl_e->e_tag) { | |
2b2d6d01 | 117 | case ACL_USER: |
78a8da10 SF |
118 | uid = from_kuid(sb->s_user_ns, acl_e->e_uid); |
119 | if (uid == (uid_t)-1) | |
120 | goto fail; | |
121 | entry->e_id = cpu_to_le32(uid); | |
af84df93 EB |
122 | e += sizeof(ext4_acl_entry); |
123 | break; | |
2b2d6d01 | 124 | case ACL_GROUP: |
78a8da10 SF |
125 | gid = from_kgid(sb->s_user_ns, acl_e->e_gid); |
126 | if (gid == (gid_t)-1) | |
127 | goto fail; | |
128 | entry->e_id = cpu_to_le32(gid); | |
2b2d6d01 TT |
129 | e += sizeof(ext4_acl_entry); |
130 | break; | |
131 | ||
132 | case ACL_USER_OBJ: | |
133 | case ACL_GROUP_OBJ: | |
134 | case ACL_MASK: | |
135 | case ACL_OTHER: | |
136 | e += sizeof(ext4_acl_entry_short); | |
137 | break; | |
138 | ||
139 | default: | |
140 | goto fail; | |
ac27a0ec DK |
141 | } |
142 | } | |
143 | return (char *)ext_acl; | |
144 | ||
145 | fail: | |
146 | kfree(ext_acl); | |
147 | return ERR_PTR(-EINVAL); | |
148 | } | |
149 | ||
ac27a0ec DK |
150 | /* |
151 | * Inode operation get_posix_acl(). | |
152 | * | |
153 | * inode->i_mutex: don't care | |
154 | */ | |
4e34e719 | 155 | struct posix_acl * |
617ba13b | 156 | ext4_get_acl(struct inode *inode, int type) |
ac27a0ec | 157 | { |
ac27a0ec DK |
158 | int name_index; |
159 | char *value = NULL; | |
160 | struct posix_acl *acl; | |
161 | int retval; | |
162 | ||
2b2d6d01 TT |
163 | switch (type) { |
164 | case ACL_TYPE_ACCESS: | |
2b2d6d01 TT |
165 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
166 | break; | |
2b2d6d01 | 167 | case ACL_TYPE_DEFAULT: |
2b2d6d01 TT |
168 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
169 | break; | |
2b2d6d01 | 170 | default: |
073aaa1b | 171 | BUG(); |
ac27a0ec | 172 | } |
617ba13b | 173 | retval = ext4_xattr_get(inode, name_index, "", NULL, 0); |
ac27a0ec | 174 | if (retval > 0) { |
216553c4 | 175 | value = kmalloc(retval, GFP_NOFS); |
ac27a0ec DK |
176 | if (!value) |
177 | return ERR_PTR(-ENOMEM); | |
617ba13b | 178 | retval = ext4_xattr_get(inode, name_index, "", value, retval); |
ac27a0ec DK |
179 | } |
180 | if (retval > 0) | |
78a8da10 | 181 | acl = ext4_acl_from_disk(inode->i_sb, value, retval); |
ac27a0ec DK |
182 | else if (retval == -ENODATA || retval == -ENOSYS) |
183 | acl = NULL; | |
184 | else | |
185 | acl = ERR_PTR(retval); | |
186 | kfree(value); | |
187 | ||
ac27a0ec DK |
188 | return acl; |
189 | } | |
190 | ||
191 | /* | |
192 | * Set the access or default ACL of an inode. | |
193 | * | |
617ba13b | 194 | * inode->i_mutex: down unless called from ext4_new_inode |
ac27a0ec DK |
195 | */ |
196 | static int | |
64e178a7 | 197 | __ext4_set_acl(handle_t *handle, struct inode *inode, int type, |
af65207c | 198 | struct posix_acl *acl, int xattr_flags) |
ac27a0ec | 199 | { |
ac27a0ec DK |
200 | int name_index; |
201 | void *value = NULL; | |
202 | size_t size = 0; | |
203 | int error; | |
204 | ||
2b2d6d01 TT |
205 | switch (type) { |
206 | case ACL_TYPE_ACCESS: | |
207 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; | |
2b2d6d01 | 208 | break; |
ac27a0ec | 209 | |
2b2d6d01 TT |
210 | case ACL_TYPE_DEFAULT: |
211 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; | |
212 | if (!S_ISDIR(inode->i_mode)) | |
213 | return acl ? -EACCES : 0; | |
214 | break; | |
ac27a0ec | 215 | |
2b2d6d01 TT |
216 | default: |
217 | return -EINVAL; | |
ac27a0ec DK |
218 | } |
219 | if (acl) { | |
78a8da10 | 220 | value = ext4_acl_to_disk(inode->i_sb, acl, &size); |
ac27a0ec DK |
221 | if (IS_ERR(value)) |
222 | return (int)PTR_ERR(value); | |
223 | } | |
224 | ||
617ba13b | 225 | error = ext4_xattr_set_handle(handle, inode, name_index, "", |
af65207c | 226 | value, size, xattr_flags); |
ac27a0ec DK |
227 | |
228 | kfree(value); | |
397e4341 | 229 | if (!error) { |
073aaa1b | 230 | set_cached_acl(inode, type, acl); |
397e4341 | 231 | } |
2b2d6d01 | 232 | |
ac27a0ec DK |
233 | return error; |
234 | } | |
235 | ||
ac27a0ec | 236 | int |
64e178a7 | 237 | ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
ac27a0ec | 238 | { |
bc26ab5f | 239 | handle_t *handle; |
c1a5d5f6 TE |
240 | int error, credits, retries = 0; |
241 | size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0; | |
a3bb2d55 JK |
242 | umode_t mode = inode->i_mode; |
243 | int update_mode = 0; | |
bc26ab5f | 244 | |
b8cb5a54 TE |
245 | error = dquot_initialize(inode); |
246 | if (error) | |
247 | return error; | |
bc26ab5f | 248 | retry: |
af65207c TE |
249 | error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */, |
250 | &credits); | |
dec214d0 TE |
251 | if (error) |
252 | return error; | |
253 | ||
c1a5d5f6 | 254 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
64e178a7 CH |
255 | if (IS_ERR(handle)) |
256 | return PTR_ERR(handle); | |
257 | ||
a3bb2d55 JK |
258 | if ((type == ACL_TYPE_ACCESS) && acl) { |
259 | error = posix_acl_update_mode(inode, &mode, &acl); | |
260 | if (error) | |
261 | goto out_stop; | |
262 | update_mode = 1; | |
263 | } | |
264 | ||
af65207c | 265 | error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */); |
a3bb2d55 JK |
266 | if (!error && update_mode) { |
267 | inode->i_mode = mode; | |
268 | inode->i_ctime = current_time(inode); | |
269 | ext4_mark_inode_dirty(handle, inode); | |
270 | } | |
271 | out_stop: | |
bc26ab5f | 272 | ext4_journal_stop(handle); |
64e178a7 | 273 | if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
bc26ab5f | 274 | goto retry; |
ac27a0ec DK |
275 | return error; |
276 | } | |
277 | ||
278 | /* | |
64e178a7 CH |
279 | * Initialize the ACLs of a new inode. Called from ext4_new_inode. |
280 | * | |
281 | * dir->i_mutex: down | |
282 | * inode->i_mutex: up (access to inode is still exclusive) | |
ac27a0ec | 283 | */ |
64e178a7 CH |
284 | int |
285 | ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir) | |
ac27a0ec | 286 | { |
64e178a7 | 287 | struct posix_acl *default_acl, *acl; |
ac27a0ec DK |
288 | int error; |
289 | ||
64e178a7 CH |
290 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
291 | if (error) | |
292 | return error; | |
ac27a0ec | 293 | |
64e178a7 CH |
294 | if (default_acl) { |
295 | error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT, | |
af65207c | 296 | default_acl, XATTR_CREATE); |
64e178a7 CH |
297 | posix_acl_release(default_acl); |
298 | } | |
299 | if (acl) { | |
300 | if (!error) | |
301 | error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, | |
af65207c | 302 | acl, XATTR_CREATE); |
64e178a7 | 303 | posix_acl_release(acl); |
24ec19b0 | 304 | } |
ac27a0ec DK |
305 | return error; |
306 | } |