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