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