1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4
5 #include "acl.h"
6 #include "xattr.h"
7
8 #include <linux/posix_acl.h>
9
10 static const char * const acl_types[] = {
11 [ACL_USER_OBJ] = "user_obj",
12 [ACL_USER] = "user",
13 [ACL_GROUP_OBJ] = "group_obj",
14 [ACL_GROUP] = "group",
15 [ACL_MASK] = "mask",
16 [ACL_OTHER] = "other",
17 NULL,
18 };
19
bch2_acl_to_text(struct printbuf * out,const void * value,size_t size)20 void bch2_acl_to_text(struct printbuf *out, const void *value, size_t size)
21 {
22 const void *p, *end = value + size;
23
24 if (!value ||
25 size < sizeof(bch_acl_header) ||
26 ((bch_acl_header *)value)->a_version != cpu_to_le32(BCH_ACL_VERSION))
27 return;
28
29 p = value + sizeof(bch_acl_header);
30 while (p < end) {
31 const bch_acl_entry *in = p;
32 unsigned tag = le16_to_cpu(in->e_tag);
33
34 prt_str(out, acl_types[tag]);
35
36 switch (tag) {
37 case ACL_USER_OBJ:
38 case ACL_GROUP_OBJ:
39 case ACL_MASK:
40 case ACL_OTHER:
41 p += sizeof(bch_acl_entry_short);
42 break;
43 case ACL_USER:
44 prt_printf(out, " uid %u", le32_to_cpu(in->e_id));
45 p += sizeof(bch_acl_entry);
46 break;
47 case ACL_GROUP:
48 prt_printf(out, " gid %u", le32_to_cpu(in->e_id));
49 p += sizeof(bch_acl_entry);
50 break;
51 }
52
53 prt_printf(out, " %o", le16_to_cpu(in->e_perm));
54
55 if (p != end)
56 prt_char(out, ' ');
57 }
58 }
59
60 #ifdef CONFIG_BCACHEFS_POSIX_ACL
61
62 #include "fs.h"
63
64 #include <linux/fs.h>
65 #include <linux/posix_acl_xattr.h>
66 #include <linux/sched.h>
67 #include <linux/slab.h>
68
bch2_acl_size(unsigned nr_short,unsigned nr_long)69 static inline size_t bch2_acl_size(unsigned nr_short, unsigned nr_long)
70 {
71 return sizeof(bch_acl_header) +
72 sizeof(bch_acl_entry_short) * nr_short +
73 sizeof(bch_acl_entry) * nr_long;
74 }
75
acl_to_xattr_type(int type)76 static inline int acl_to_xattr_type(int type)
77 {
78 switch (type) {
79 case ACL_TYPE_ACCESS:
80 return KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS;
81 case ACL_TYPE_DEFAULT:
82 return KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT;
83 default:
84 BUG();
85 }
86 }
87
88 /*
89 * Convert from filesystem to in-memory representation.
90 */
bch2_acl_from_disk(struct btree_trans * trans,const void * value,size_t size)91 static struct posix_acl *bch2_acl_from_disk(struct btree_trans *trans,
92 const void *value, size_t size)
93 {
94 const void *p, *end = value + size;
95 struct posix_acl *acl;
96 struct posix_acl_entry *out;
97 unsigned count = 0;
98 int ret;
99
100 if (!value)
101 return NULL;
102 if (size < sizeof(bch_acl_header))
103 goto invalid;
104 if (((bch_acl_header *)value)->a_version !=
105 cpu_to_le32(BCH_ACL_VERSION))
106 goto invalid;
107
108 p = value + sizeof(bch_acl_header);
109 while (p < end) {
110 const bch_acl_entry *entry = p;
111
112 if (p + sizeof(bch_acl_entry_short) > end)
113 goto invalid;
114
115 switch (le16_to_cpu(entry->e_tag)) {
116 case ACL_USER_OBJ:
117 case ACL_GROUP_OBJ:
118 case ACL_MASK:
119 case ACL_OTHER:
120 p += sizeof(bch_acl_entry_short);
121 break;
122 case ACL_USER:
123 case ACL_GROUP:
124 p += sizeof(bch_acl_entry);
125 break;
126 default:
127 goto invalid;
128 }
129
130 count++;
131 }
132
133 if (p > end)
134 goto invalid;
135
136 if (!count)
137 return NULL;
138
139 acl = allocate_dropping_locks(trans, ret,
140 posix_acl_alloc(count, _gfp));
141 if (!acl)
142 return ERR_PTR(-ENOMEM);
143 if (ret) {
144 kfree(acl);
145 return ERR_PTR(ret);
146 }
147
148 out = acl->a_entries;
149
150 p = value + sizeof(bch_acl_header);
151 while (p < end) {
152 const bch_acl_entry *in = p;
153
154 out->e_tag = le16_to_cpu(in->e_tag);
155 out->e_perm = le16_to_cpu(in->e_perm);
156
157 switch (out->e_tag) {
158 case ACL_USER_OBJ:
159 case ACL_GROUP_OBJ:
160 case ACL_MASK:
161 case ACL_OTHER:
162 p += sizeof(bch_acl_entry_short);
163 break;
164 case ACL_USER:
165 out->e_uid = make_kuid(&init_user_ns,
166 le32_to_cpu(in->e_id));
167 p += sizeof(bch_acl_entry);
168 break;
169 case ACL_GROUP:
170 out->e_gid = make_kgid(&init_user_ns,
171 le32_to_cpu(in->e_id));
172 p += sizeof(bch_acl_entry);
173 break;
174 }
175
176 out++;
177 }
178
179 BUG_ON(out != acl->a_entries + acl->a_count);
180
181 return acl;
182 invalid:
183 pr_err("invalid acl entry");
184 return ERR_PTR(-EINVAL);
185 }
186
187 /*
188 * Convert from in-memory to filesystem representation.
189 */
190 static struct bkey_i_xattr *
bch2_acl_to_xattr(struct btree_trans * trans,const struct posix_acl * acl,int type)191 bch2_acl_to_xattr(struct btree_trans *trans,
192 const struct posix_acl *acl,
193 int type)
194 {
195 struct bkey_i_xattr *xattr;
196 bch_acl_header *acl_header;
197 const struct posix_acl_entry *acl_e, *pe;
198 void *outptr;
199 unsigned nr_short = 0, nr_long = 0, acl_len, u64s;
200
201 FOREACH_ACL_ENTRY(acl_e, acl, pe) {
202 switch (acl_e->e_tag) {
203 case ACL_USER:
204 case ACL_GROUP:
205 nr_long++;
206 break;
207 case ACL_USER_OBJ:
208 case ACL_GROUP_OBJ:
209 case ACL_MASK:
210 case ACL_OTHER:
211 nr_short++;
212 break;
213 default:
214 return ERR_PTR(-EINVAL);
215 }
216 }
217
218 acl_len = bch2_acl_size(nr_short, nr_long);
219 u64s = BKEY_U64s + xattr_val_u64s(0, acl_len);
220
221 if (u64s > U8_MAX)
222 return ERR_PTR(-E2BIG);
223
224 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
225 if (IS_ERR(xattr))
226 return xattr;
227
228 bkey_xattr_init(&xattr->k_i);
229 xattr->k.u64s = u64s;
230 xattr->v.x_type = acl_to_xattr_type(type);
231 xattr->v.x_name_len = 0;
232 xattr->v.x_val_len = cpu_to_le16(acl_len);
233
234 acl_header = xattr_val(&xattr->v);
235 acl_header->a_version = cpu_to_le32(BCH_ACL_VERSION);
236
237 outptr = (void *) acl_header + sizeof(*acl_header);
238
239 FOREACH_ACL_ENTRY(acl_e, acl, pe) {
240 bch_acl_entry *entry = outptr;
241
242 entry->e_tag = cpu_to_le16(acl_e->e_tag);
243 entry->e_perm = cpu_to_le16(acl_e->e_perm);
244 switch (acl_e->e_tag) {
245 case ACL_USER:
246 entry->e_id = cpu_to_le32(
247 from_kuid(&init_user_ns, acl_e->e_uid));
248 outptr += sizeof(bch_acl_entry);
249 break;
250 case ACL_GROUP:
251 entry->e_id = cpu_to_le32(
252 from_kgid(&init_user_ns, acl_e->e_gid));
253 outptr += sizeof(bch_acl_entry);
254 break;
255
256 case ACL_USER_OBJ:
257 case ACL_GROUP_OBJ:
258 case ACL_MASK:
259 case ACL_OTHER:
260 outptr += sizeof(bch_acl_entry_short);
261 break;
262 }
263 }
264
265 BUG_ON(outptr != xattr_val(&xattr->v) + acl_len);
266
267 return xattr;
268 }
269
bch2_get_acl(struct inode * vinode,int type,bool rcu)270 struct posix_acl *bch2_get_acl(struct inode *vinode, int type, bool rcu)
271 {
272 struct bch_inode_info *inode = to_bch_ei(vinode);
273 struct bch_fs *c = inode->v.i_sb->s_fs_info;
274 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
275 struct xattr_search_key search = X_SEARCH(acl_to_xattr_type(type), "", 0);
276 struct btree_iter iter = { NULL };
277 struct posix_acl *acl = NULL;
278
279 if (rcu)
280 return ERR_PTR(-ECHILD);
281
282 struct btree_trans *trans = bch2_trans_get(c);
283 retry:
284 bch2_trans_begin(trans);
285
286 struct bkey_s_c k = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc,
287 &hash, inode_inum(inode), &search, 0);
288 int ret = bkey_err(k);
289 if (ret)
290 goto err;
291
292 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
293 acl = bch2_acl_from_disk(trans, xattr_val(xattr.v),
294 le16_to_cpu(xattr.v->x_val_len));
295 ret = PTR_ERR_OR_ZERO(acl);
296 err:
297 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
298 goto retry;
299
300 if (ret)
301 acl = !bch2_err_matches(ret, ENOENT) ? ERR_PTR(ret) : NULL;
302
303 if (!IS_ERR_OR_NULL(acl))
304 set_cached_acl(&inode->v, type, acl);
305
306 bch2_trans_iter_exit(trans, &iter);
307 bch2_trans_put(trans);
308 return acl;
309 }
310
bch2_set_acl_trans(struct btree_trans * trans,subvol_inum inum,struct bch_inode_unpacked * inode_u,struct posix_acl * acl,int type)311 int bch2_set_acl_trans(struct btree_trans *trans, subvol_inum inum,
312 struct bch_inode_unpacked *inode_u,
313 struct posix_acl *acl, int type)
314 {
315 struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode_u);
316 int ret;
317
318 if (type == ACL_TYPE_DEFAULT &&
319 !S_ISDIR(inode_u->bi_mode))
320 return acl ? -EACCES : 0;
321
322 if (acl) {
323 struct bkey_i_xattr *xattr =
324 bch2_acl_to_xattr(trans, acl, type);
325 if (IS_ERR(xattr))
326 return PTR_ERR(xattr);
327
328 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, &hash_info,
329 inum, &xattr->k_i, 0);
330 } else {
331 struct xattr_search_key search =
332 X_SEARCH(acl_to_xattr_type(type), "", 0);
333
334 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, &hash_info,
335 inum, &search);
336 }
337
338 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
339 }
340
bch2_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,struct posix_acl * _acl,int type)341 int bch2_set_acl(struct mnt_idmap *idmap,
342 struct dentry *dentry,
343 struct posix_acl *_acl, int type)
344 {
345 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
346 struct bch_fs *c = inode->v.i_sb->s_fs_info;
347 struct btree_iter inode_iter = { NULL };
348 struct bch_inode_unpacked inode_u;
349 struct posix_acl *acl;
350 umode_t mode;
351 int ret;
352
353 mutex_lock(&inode->ei_update_lock);
354 struct btree_trans *trans = bch2_trans_get(c);
355 retry:
356 bch2_trans_begin(trans);
357 acl = _acl;
358
359 ret = bch2_subvol_is_ro_trans(trans, inode->ei_inum.subvol) ?:
360 bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
361 BTREE_ITER_intent);
362 if (ret)
363 goto btree_err;
364
365 mode = inode_u.bi_mode;
366
367 if (type == ACL_TYPE_ACCESS) {
368 ret = posix_acl_update_mode(idmap, &inode->v, &mode, &acl);
369 if (ret)
370 goto btree_err;
371 }
372
373 ret = bch2_set_acl_trans(trans, inode_inum(inode), &inode_u, acl, type);
374 if (ret)
375 goto btree_err;
376
377 inode_u.bi_ctime = bch2_current_time(c);
378 inode_u.bi_mode = mode;
379
380 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
381 bch2_trans_commit(trans, NULL, NULL, 0);
382 btree_err:
383 bch2_trans_iter_exit(trans, &inode_iter);
384
385 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
386 goto retry;
387 if (unlikely(ret))
388 goto err;
389
390 bch2_inode_update_after_write(trans, inode, &inode_u,
391 ATTR_CTIME|ATTR_MODE);
392
393 set_cached_acl(&inode->v, type, acl);
394 err:
395 bch2_trans_put(trans);
396 mutex_unlock(&inode->ei_update_lock);
397
398 return ret;
399 }
400
bch2_acl_chmod(struct btree_trans * trans,subvol_inum inum,struct bch_inode_unpacked * inode,umode_t mode,struct posix_acl ** new_acl)401 int bch2_acl_chmod(struct btree_trans *trans, subvol_inum inum,
402 struct bch_inode_unpacked *inode,
403 umode_t mode,
404 struct posix_acl **new_acl)
405 {
406 struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode);
407 struct xattr_search_key search = X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0);
408 struct btree_iter iter;
409 struct posix_acl *acl = NULL;
410
411 struct bkey_s_c k = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc,
412 &hash_info, inum, &search, BTREE_ITER_intent);
413 int ret = bkey_err(k);
414 if (ret)
415 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
416
417 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
418
419 acl = bch2_acl_from_disk(trans, xattr_val(xattr.v),
420 le16_to_cpu(xattr.v->x_val_len));
421 ret = PTR_ERR_OR_ZERO(acl);
422 if (ret)
423 goto err;
424
425 ret = allocate_dropping_locks_errcode(trans, __posix_acl_chmod(&acl, _gfp, mode));
426 if (ret)
427 goto err;
428
429 struct bkey_i_xattr *new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
430 ret = PTR_ERR_OR_ZERO(new);
431 if (ret)
432 goto err;
433
434 new->k.p = iter.pos;
435 ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
436 *new_acl = acl;
437 acl = NULL;
438 err:
439 bch2_trans_iter_exit(trans, &iter);
440 if (!IS_ERR_OR_NULL(acl))
441 kfree(acl);
442 return ret;
443 }
444
445 #endif /* CONFIG_BCACHEFS_POSIX_ACL */
446