Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
16f7e0fe | 2 | #include <linux/capability.h> |
1da177e4 LT |
3 | #include <linux/fs.h> |
4 | #include <linux/posix_acl.h> | |
f466c6fd | 5 | #include "reiserfs.h" |
1da177e4 LT |
6 | #include <linux/errno.h> |
7 | #include <linux/pagemap.h> | |
8 | #include <linux/xattr.h> | |
5a0e3ad6 | 9 | #include <linux/slab.h> |
9a59f452 | 10 | #include <linux/posix_acl_xattr.h> |
c45ac888 | 11 | #include "xattr.h" |
a3063ab8 | 12 | #include "acl.h" |
17093991 | 13 | #include <linux/uaccess.h> |
1da177e4 | 14 | |
47f70d08 | 15 | static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th, |
0ab2621e | 16 | struct inode *inode, int type, |
bd4c625c | 17 | struct posix_acl *acl); |
1da177e4 | 18 | |
47f70d08 CH |
19 | |
20 | int | |
13e83a49 | 21 | reiserfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 22 | struct posix_acl *acl, int type) |
1da177e4 | 23 | { |
0ab2621e JM |
24 | int error, error2; |
25 | struct reiserfs_transaction_handle th; | |
26 | size_t jcreate_blocks; | |
47f70d08 | 27 | int size = acl ? posix_acl_xattr_size(acl->a_count) : 0; |
fcea8aed | 28 | int update_mode = 0; |
138060ba | 29 | struct inode *inode = d_inode(dentry); |
fcea8aed | 30 | umode_t mode = inode->i_mode; |
1da177e4 | 31 | |
098297b2 JM |
32 | /* |
33 | * Pessimism: We can't assume that anything from the xattr root up | |
34 | * has been created. | |
35 | */ | |
0ab2621e JM |
36 | |
37 | jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) + | |
38 | reiserfs_xattr_nblocks(inode, size) * 2; | |
39 | ||
40 | reiserfs_write_lock(inode->i_sb); | |
41 | error = journal_begin(&th, inode->i_sb, jcreate_blocks); | |
4c05141d | 42 | reiserfs_write_unlock(inode->i_sb); |
0ab2621e | 43 | if (error == 0) { |
6883cd7f | 44 | if (type == ACL_TYPE_ACCESS && acl) { |
700b7940 | 45 | error = posix_acl_update_mode(&nop_mnt_idmap, inode, |
e65ce2a5 | 46 | &mode, &acl); |
6883cd7f JK |
47 | if (error) |
48 | goto unlock; | |
fcea8aed | 49 | update_mode = 1; |
6883cd7f | 50 | } |
47f70d08 | 51 | error = __reiserfs_set_acl(&th, inode, type, acl); |
fcea8aed EF |
52 | if (!error && update_mode) |
53 | inode->i_mode = mode; | |
6883cd7f | 54 | unlock: |
4c05141d | 55 | reiserfs_write_lock(inode->i_sb); |
58d85426 | 56 | error2 = journal_end(&th); |
4c05141d | 57 | reiserfs_write_unlock(inode->i_sb); |
0ab2621e JM |
58 | if (error2) |
59 | error = error2; | |
60 | } | |
1da177e4 | 61 | |
1da177e4 LT |
62 | return error; |
63 | } | |
64 | ||
1da177e4 LT |
65 | /* |
66 | * Convert from filesystem to in-memory representation. | |
67 | */ | |
9dad943a | 68 | static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size) |
1da177e4 LT |
69 | { |
70 | const char *end = (char *)value + size; | |
71 | int n, count; | |
72 | struct posix_acl *acl; | |
73 | ||
74 | if (!value) | |
75 | return NULL; | |
76 | if (size < sizeof(reiserfs_acl_header)) | |
bd4c625c LT |
77 | return ERR_PTR(-EINVAL); |
78 | if (((reiserfs_acl_header *) value)->a_version != | |
1da177e4 LT |
79 | cpu_to_le32(REISERFS_ACL_VERSION)) |
80 | return ERR_PTR(-EINVAL); | |
81 | value = (char *)value + sizeof(reiserfs_acl_header); | |
82 | count = reiserfs_acl_count(size); | |
83 | if (count < 0) | |
84 | return ERR_PTR(-EINVAL); | |
85 | if (count == 0) | |
86 | return NULL; | |
87 | acl = posix_acl_alloc(count, GFP_NOFS); | |
88 | if (!acl) | |
89 | return ERR_PTR(-ENOMEM); | |
bd4c625c LT |
90 | for (n = 0; n < count; n++) { |
91 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value; | |
1da177e4 LT |
92 | if ((char *)value + sizeof(reiserfs_acl_entry_short) > end) |
93 | goto fail; | |
bd4c625c | 94 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
1da177e4 | 95 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
bd4c625c LT |
96 | switch (acl->a_entries[n].e_tag) { |
97 | case ACL_USER_OBJ: | |
98 | case ACL_GROUP_OBJ: | |
99 | case ACL_MASK: | |
100 | case ACL_OTHER: | |
101 | value = (char *)value + | |
102 | sizeof(reiserfs_acl_entry_short); | |
bd4c625c LT |
103 | break; |
104 | ||
105 | case ACL_USER: | |
df814654 EB |
106 | value = (char *)value + sizeof(reiserfs_acl_entry); |
107 | if ((char *)value > end) | |
108 | goto fail; | |
109 | acl->a_entries[n].e_uid = | |
110 | make_kuid(&init_user_ns, | |
111 | le32_to_cpu(entry->e_id)); | |
112 | break; | |
bd4c625c LT |
113 | case ACL_GROUP: |
114 | value = (char *)value + sizeof(reiserfs_acl_entry); | |
115 | if ((char *)value > end) | |
1da177e4 | 116 | goto fail; |
df814654 EB |
117 | acl->a_entries[n].e_gid = |
118 | make_kgid(&init_user_ns, | |
119 | le32_to_cpu(entry->e_id)); | |
bd4c625c LT |
120 | break; |
121 | ||
122 | default: | |
123 | goto fail; | |
1da177e4 LT |
124 | } |
125 | } | |
126 | if (value != end) | |
127 | goto fail; | |
128 | return acl; | |
129 | ||
cf776a7a | 130 | fail: |
1da177e4 LT |
131 | posix_acl_release(acl); |
132 | return ERR_PTR(-EINVAL); | |
133 | } | |
134 | ||
135 | /* | |
136 | * Convert from in-memory to filesystem representation. | |
137 | */ | |
9dad943a | 138 | static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size) |
1da177e4 LT |
139 | { |
140 | reiserfs_acl_header *ext_acl; | |
141 | char *e; | |
142 | int n; | |
143 | ||
144 | *size = reiserfs_acl_size(acl->a_count); | |
5cbded58 | 145 | ext_acl = kmalloc(sizeof(reiserfs_acl_header) + |
bd4c625c LT |
146 | acl->a_count * |
147 | sizeof(reiserfs_acl_entry), | |
148 | GFP_NOFS); | |
1da177e4 LT |
149 | if (!ext_acl) |
150 | return ERR_PTR(-ENOMEM); | |
151 | ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION); | |
152 | e = (char *)ext_acl + sizeof(reiserfs_acl_header); | |
bd4c625c | 153 | for (n = 0; n < acl->a_count; n++) { |
df814654 | 154 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
bd4c625c LT |
155 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e; |
156 | entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag); | |
1da177e4 | 157 | entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm); |
bd4c625c LT |
158 | switch (acl->a_entries[n].e_tag) { |
159 | case ACL_USER: | |
df814654 EB |
160 | entry->e_id = cpu_to_le32( |
161 | from_kuid(&init_user_ns, acl_e->e_uid)); | |
162 | e += sizeof(reiserfs_acl_entry); | |
163 | break; | |
bd4c625c | 164 | case ACL_GROUP: |
df814654 EB |
165 | entry->e_id = cpu_to_le32( |
166 | from_kgid(&init_user_ns, acl_e->e_gid)); | |
bd4c625c LT |
167 | e += sizeof(reiserfs_acl_entry); |
168 | break; | |
169 | ||
170 | case ACL_USER_OBJ: | |
171 | case ACL_GROUP_OBJ: | |
172 | case ACL_MASK: | |
173 | case ACL_OTHER: | |
174 | e += sizeof(reiserfs_acl_entry_short); | |
175 | break; | |
176 | ||
177 | default: | |
178 | goto fail; | |
1da177e4 LT |
179 | } |
180 | } | |
181 | return (char *)ext_acl; | |
182 | ||
cf776a7a | 183 | fail: |
1da177e4 LT |
184 | kfree(ext_acl); |
185 | return ERR_PTR(-EINVAL); | |
186 | } | |
187 | ||
188 | /* | |
189 | * Inode operation get_posix_acl(). | |
190 | * | |
1b1dcc1b | 191 | * inode->i_mutex: down |
1da177e4 LT |
192 | * BKL held [before 2.5.x] |
193 | */ | |
0cad6246 | 194 | struct posix_acl *reiserfs_get_acl(struct inode *inode, int type, bool rcu) |
1da177e4 LT |
195 | { |
196 | char *name, *value; | |
073aaa1b | 197 | struct posix_acl *acl; |
3cdc409c | 198 | int size; |
1da177e4 | 199 | int retval; |
bd4c625c | 200 | |
0cad6246 MS |
201 | if (rcu) |
202 | return ERR_PTR(-ECHILD); | |
203 | ||
bd4c625c LT |
204 | switch (type) { |
205 | case ACL_TYPE_ACCESS: | |
97d79299 | 206 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
bd4c625c LT |
207 | break; |
208 | case ACL_TYPE_DEFAULT: | |
97d79299 | 209 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
bd4c625c LT |
210 | break; |
211 | default: | |
073aaa1b | 212 | BUG(); |
bd4c625c LT |
213 | } |
214 | ||
bd4c625c | 215 | size = reiserfs_xattr_get(inode, name, NULL, 0); |
3cdc409c | 216 | if (size < 0) { |
b8a7a3a6 | 217 | if (size == -ENODATA || size == -ENOSYS) |
bd4c625c | 218 | return NULL; |
bd4c625c LT |
219 | return ERR_PTR(size); |
220 | } | |
1da177e4 | 221 | |
bd4c625c LT |
222 | value = kmalloc(size, GFP_NOFS); |
223 | if (!value) | |
224 | return ERR_PTR(-ENOMEM); | |
1da177e4 LT |
225 | |
226 | retval = reiserfs_xattr_get(inode, name, value, size); | |
227 | if (retval == -ENODATA || retval == -ENOSYS) { | |
098297b2 JM |
228 | /* |
229 | * This shouldn't actually happen as it should have | |
230 | * been caught above.. but just in case | |
231 | */ | |
1da177e4 | 232 | acl = NULL; |
bd4c625c | 233 | } else if (retval < 0) { |
1da177e4 LT |
234 | acl = ERR_PTR(retval); |
235 | } else { | |
9dad943a | 236 | acl = reiserfs_posix_acl_from_disk(value, retval); |
bd4c625c | 237 | } |
1da177e4 LT |
238 | |
239 | kfree(value); | |
240 | return acl; | |
241 | } | |
242 | ||
243 | /* | |
244 | * Inode operation set_posix_acl(). | |
245 | * | |
1b1dcc1b | 246 | * inode->i_mutex: down |
1da177e4 LT |
247 | * BKL held [before 2.5.x] |
248 | */ | |
249 | static int | |
47f70d08 | 250 | __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode, |
0ab2621e | 251 | int type, struct posix_acl *acl) |
1da177e4 | 252 | { |
bd4c625c | 253 | char *name; |
1da177e4 | 254 | void *value = NULL; |
48b32a35 | 255 | size_t size = 0; |
1da177e4 | 256 | int error; |
1da177e4 | 257 | |
bd4c625c LT |
258 | switch (type) { |
259 | case ACL_TYPE_ACCESS: | |
97d79299 | 260 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
bd4c625c LT |
261 | break; |
262 | case ACL_TYPE_DEFAULT: | |
97d79299 | 263 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
bd4c625c LT |
264 | if (!S_ISDIR(inode->i_mode)) |
265 | return acl ? -EACCES : 0; | |
266 | break; | |
267 | default: | |
268 | return -EINVAL; | |
269 | } | |
270 | ||
271 | if (acl) { | |
9dad943a | 272 | value = reiserfs_posix_acl_to_disk(acl, &size); |
bd4c625c LT |
273 | if (IS_ERR(value)) |
274 | return (int)PTR_ERR(value); | |
48b32a35 JM |
275 | } |
276 | ||
0ab2621e | 277 | error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0); |
48b32a35 JM |
278 | |
279 | /* | |
280 | * Ensure that the inode gets dirtied if we're only using | |
281 | * the mode bits and an old ACL didn't exist. We don't need | |
282 | * to check if the inode is hashed here since we won't get | |
283 | * called by reiserfs_inherit_default_acl(). | |
284 | */ | |
285 | if (error == -ENODATA) { | |
286 | error = 0; | |
287 | if (type == ACL_TYPE_ACCESS) { | |
02027d42 | 288 | inode->i_ctime = current_time(inode); |
bd4c625c | 289 | mark_inode_dirty(inode); |
bd4c625c LT |
290 | } |
291 | } | |
1da177e4 | 292 | |
833d304b | 293 | kfree(value); |
1da177e4 | 294 | |
d984561b | 295 | if (!error) |
073aaa1b | 296 | set_cached_acl(inode, type, acl); |
1da177e4 LT |
297 | |
298 | return error; | |
299 | } | |
300 | ||
098297b2 JM |
301 | /* |
302 | * dir->i_mutex: locked, | |
303 | * inode is new and not released into the wild yet | |
304 | */ | |
1da177e4 | 305 | int |
0ab2621e JM |
306 | reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th, |
307 | struct inode *dir, struct dentry *dentry, | |
bd4c625c | 308 | struct inode *inode) |
1da177e4 | 309 | { |
47f70d08 | 310 | struct posix_acl *default_acl, *acl; |
bd4c625c LT |
311 | int err = 0; |
312 | ||
313 | /* ACLs only get applied to files and directories */ | |
314 | if (S_ISLNK(inode->i_mode)) | |
315 | return 0; | |
316 | ||
098297b2 JM |
317 | /* |
318 | * ACLs can only be used on "new" objects, so if it's an old object | |
319 | * there is nothing to inherit from | |
320 | */ | |
bd4c625c LT |
321 | if (get_inode_sd_version(dir) == STAT_DATA_V1) |
322 | goto apply_umask; | |
323 | ||
098297b2 JM |
324 | /* |
325 | * Don't apply ACLs to objects in the .reiserfs_priv tree.. This | |
bd4c625c | 326 | * would be useless since permissions are ignored, and a pain because |
098297b2 JM |
327 | * it introduces locking cycles |
328 | */ | |
60e4cf67 | 329 | if (IS_PRIVATE(inode)) |
bd4c625c | 330 | goto apply_umask; |
bd4c625c | 331 | |
47f70d08 CH |
332 | err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
333 | if (err) | |
334 | return err; | |
bd4c625c | 335 | |
47f70d08 CH |
336 | if (default_acl) { |
337 | err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT, | |
338 | default_acl); | |
339 | posix_acl_release(default_acl); | |
340 | } | |
bd4c625c | 341 | if (acl) { |
47f70d08 CH |
342 | if (!err) |
343 | err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, | |
344 | acl); | |
bd4c625c | 345 | posix_acl_release(acl); |
bd4c625c LT |
346 | } |
347 | ||
348 | return err; | |
47f70d08 | 349 | |
cf776a7a | 350 | apply_umask: |
47f70d08 CH |
351 | /* no ACL, apply umask */ |
352 | inode->i_mode &= ~current_umask(); | |
353 | return err; | |
1da177e4 LT |
354 | } |
355 | ||
0ab2621e JM |
356 | /* This is used to cache the default acl before a new object is created. |
357 | * The biggest reason for this is to get an idea of how many blocks will | |
358 | * actually be required for the create operation if we must inherit an ACL. | |
359 | * An ACL write can add up to 3 object creations and an additional file write | |
360 | * so we'd prefer not to reserve that many blocks in the journal if we can. | |
361 | * It also has the advantage of not loading the ACL with a transaction open, | |
362 | * this may seem silly, but if the owner of the directory is doing the | |
363 | * creation, the ACL may not be loaded since the permissions wouldn't require | |
364 | * it. | |
365 | * We return the number of blocks required for the transaction. | |
366 | */ | |
bd4c625c | 367 | int reiserfs_cache_default_acl(struct inode *inode) |
1da177e4 | 368 | { |
0ab2621e JM |
369 | struct posix_acl *acl; |
370 | int nblocks = 0; | |
371 | ||
372 | if (IS_PRIVATE(inode)) | |
373 | return 0; | |
374 | ||
cac2f8b8 | 375 | acl = get_inode_acl(inode, ACL_TYPE_DEFAULT); |
0ab2621e JM |
376 | |
377 | if (acl && !IS_ERR(acl)) { | |
378 | int size = reiserfs_acl_size(acl->a_count); | |
379 | ||
380 | /* Other xattrs can be created during inode creation. We don't | |
381 | * want to claim too many blocks, so we check to see if we | |
9436fb4d | 382 | * need to create the tree to the xattrs, and then we |
0ab2621e JM |
383 | * just want two files. */ |
384 | nblocks = reiserfs_xattr_jcreate_nblocks(inode); | |
385 | nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb); | |
386 | ||
387 | REISERFS_I(inode)->i_flags |= i_has_xattr_dir; | |
388 | ||
389 | /* We need to account for writes + bitmaps for two files */ | |
390 | nblocks += reiserfs_xattr_nblocks(inode, size) * 4; | |
391 | posix_acl_release(acl); | |
bd4c625c LT |
392 | } |
393 | ||
0ab2621e | 394 | return nblocks; |
1da177e4 LT |
395 | } |
396 | ||
4c05141d JM |
397 | /* |
398 | * Called under i_mutex | |
399 | */ | |
138060ba | 400 | int reiserfs_acl_chmod(struct dentry *dentry) |
1da177e4 | 401 | { |
138060ba CB |
402 | struct inode *inode = d_inode(dentry); |
403 | ||
4a857011 JM |
404 | if (IS_PRIVATE(inode)) |
405 | return 0; | |
bd4c625c | 406 | if (get_inode_sd_version(inode) == STAT_DATA_V1 || |
47f70d08 | 407 | !reiserfs_posixacl(inode->i_sb)) |
bd4c625c | 408 | return 0; |
1da177e4 | 409 | |
13e83a49 | 410 | return posix_acl_chmod(&nop_mnt_idmap, dentry, inode->i_mode); |
1da177e4 | 411 | } |