Merge tag 'fixes_for_v5.13-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / fs / f2fs / acl.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
af48b85b
JK
3 * fs/f2fs/acl.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 *
8 * Portions of this code from linux/fs/ext2/acl.c
9 *
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
af48b85b
JK
11 */
12#include <linux/f2fs_fs.h>
13#include "f2fs.h"
14#include "xattr.h"
15#include "acl.h"
16
af48b85b
JK
17static inline size_t f2fs_acl_size(int count)
18{
19 if (count <= 4) {
20 return sizeof(struct f2fs_acl_header) +
21 count * sizeof(struct f2fs_acl_entry_short);
22 } else {
23 return sizeof(struct f2fs_acl_header) +
24 4 * sizeof(struct f2fs_acl_entry_short) +
25 (count - 4) * sizeof(struct f2fs_acl_entry);
26 }
27}
28
29static inline int f2fs_acl_count(size_t size)
30{
31 ssize_t s;
5f029c04 32
af48b85b
JK
33 size -= sizeof(struct f2fs_acl_header);
34 s = size - 4 * sizeof(struct f2fs_acl_entry_short);
35 if (s < 0) {
36 if (size % sizeof(struct f2fs_acl_entry_short))
37 return -1;
38 return size / sizeof(struct f2fs_acl_entry_short);
39 } else {
40 if (s % sizeof(struct f2fs_acl_entry))
41 return -1;
42 return s / sizeof(struct f2fs_acl_entry) + 4;
43 }
44}
45
46static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
47{
48 int i, count;
49 struct posix_acl *acl;
50 struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
51 struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
52 const char *end = value + size;
53
1618e6e2
CX
54 if (size < sizeof(struct f2fs_acl_header))
55 return ERR_PTR(-EINVAL);
56
af48b85b
JK
57 if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
58 return ERR_PTR(-EINVAL);
59
60 count = f2fs_acl_count(size);
61 if (count < 0)
62 return ERR_PTR(-EINVAL);
63 if (count == 0)
64 return NULL;
65
dd802406 66 acl = posix_acl_alloc(count, GFP_NOFS);
af48b85b
JK
67 if (!acl)
68 return ERR_PTR(-ENOMEM);
69
70 for (i = 0; i < count; i++) {
71
72 if ((char *)entry > end)
73 goto fail;
74
75 acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag);
76 acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
77
78 switch (acl->a_entries[i].e_tag) {
79 case ACL_USER_OBJ:
80 case ACL_GROUP_OBJ:
81 case ACL_MASK:
82 case ACL_OTHER:
af48b85b
JK
83 entry = (struct f2fs_acl_entry *)((char *)entry +
84 sizeof(struct f2fs_acl_entry_short));
85 break;
86
87 case ACL_USER:
88 acl->a_entries[i].e_uid =
89 make_kuid(&init_user_ns,
90 le32_to_cpu(entry->e_id));
91 entry = (struct f2fs_acl_entry *)((char *)entry +
92 sizeof(struct f2fs_acl_entry));
93 break;
94 case ACL_GROUP:
95 acl->a_entries[i].e_gid =
96 make_kgid(&init_user_ns,
97 le32_to_cpu(entry->e_id));
98 entry = (struct f2fs_acl_entry *)((char *)entry +
99 sizeof(struct f2fs_acl_entry));
100 break;
101 default:
102 goto fail;
103 }
104 }
105 if ((char *)entry != end)
106 goto fail;
107 return acl;
108fail:
109 posix_acl_release(acl);
110 return ERR_PTR(-EINVAL);
111}
112
1ecc0c5c
CY
113static void *f2fs_acl_to_disk(struct f2fs_sb_info *sbi,
114 const struct posix_acl *acl, size_t *size)
af48b85b
JK
115{
116 struct f2fs_acl_header *f2fs_acl;
117 struct f2fs_acl_entry *entry;
118 int i;
119
1ecc0c5c
CY
120 f2fs_acl = f2fs_kmalloc(sbi, sizeof(struct f2fs_acl_header) +
121 acl->a_count * sizeof(struct f2fs_acl_entry),
122 GFP_NOFS);
af48b85b
JK
123 if (!f2fs_acl)
124 return ERR_PTR(-ENOMEM);
125
126 f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
127 entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
128
129 for (i = 0; i < acl->a_count; i++) {
130
131 entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
132 entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
133
134 switch (acl->a_entries[i].e_tag) {
135 case ACL_USER:
136 entry->e_id = cpu_to_le32(
137 from_kuid(&init_user_ns,
138 acl->a_entries[i].e_uid));
139 entry = (struct f2fs_acl_entry *)((char *)entry +
140 sizeof(struct f2fs_acl_entry));
141 break;
142 case ACL_GROUP:
143 entry->e_id = cpu_to_le32(
144 from_kgid(&init_user_ns,
145 acl->a_entries[i].e_gid));
146 entry = (struct f2fs_acl_entry *)((char *)entry +
147 sizeof(struct f2fs_acl_entry));
148 break;
149 case ACL_USER_OBJ:
150 case ACL_GROUP_OBJ:
151 case ACL_MASK:
152 case ACL_OTHER:
153 entry = (struct f2fs_acl_entry *)((char *)entry +
154 sizeof(struct f2fs_acl_entry_short));
155 break;
156 default:
157 goto fail;
158 }
159 }
160 *size = f2fs_acl_size(acl->a_count);
161 return (void *)f2fs_acl;
162
163fail:
c8eb7024 164 kfree(f2fs_acl);
af48b85b
JK
165 return ERR_PTR(-EINVAL);
166}
167
bce8d112
JK
168static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
169 struct page *dpage)
af48b85b 170{
af48b85b
JK
171 int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
172 void *value = NULL;
173 struct posix_acl *acl;
174 int retval;
175
af48b85b
JK
176 if (type == ACL_TYPE_ACCESS)
177 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
178
bce8d112 179 retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
af48b85b 180 if (retval > 0) {
1ecc0c5c 181 value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
af48b85b
JK
182 if (!value)
183 return ERR_PTR(-ENOMEM);
bce8d112
JK
184 retval = f2fs_getxattr(inode, name_index, "", value,
185 retval, dpage);
af48b85b
JK
186 }
187
c1b75eab 188 if (retval > 0)
af48b85b 189 acl = f2fs_acl_from_disk(value, retval);
c1b75eab
JK
190 else if (retval == -ENODATA)
191 acl = NULL;
192 else
193 acl = ERR_PTR(retval);
c8eb7024 194 kfree(value);
c1b75eab 195
af48b85b
JK
196 return acl;
197}
198
bce8d112
JK
199struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
200{
201 return __f2fs_get_acl(inode, type, NULL);
202}
203
17232e83
CY
204static int f2fs_acl_update_mode(struct inode *inode, umode_t *mode_p,
205 struct posix_acl **acl)
206{
207 umode_t mode = inode->i_mode;
208 int error;
209
210 if (is_inode_flag_set(inode, FI_ACL_MODE))
211 mode = F2FS_I(inode)->i_acl_mode;
212
213 error = posix_acl_equiv_mode(*acl, &mode);
214 if (error < 0)
215 return error;
216 if (error == 0)
217 *acl = NULL;
7d6beb71
LT
218 if (!in_group_p(i_gid_into_mnt(&init_user_ns, inode)) &&
219 !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FSETID))
17232e83
CY
220 mode &= ~S_ISGID;
221 *mode_p = mode;
222 return 0;
223}
224
a6dda0e6 225static int __f2fs_set_acl(struct inode *inode, int type,
2ed2d5b3 226 struct posix_acl *acl, struct page *ipage)
af48b85b 227{
af48b85b
JK
228 int name_index;
229 void *value = NULL;
230 size_t size = 0;
231 int error;
14af20fc 232 umode_t mode = inode->i_mode;
af48b85b 233
af48b85b
JK
234 switch (type) {
235 case ACL_TYPE_ACCESS:
236 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
c925dc16 237 if (acl && !ipage) {
17232e83 238 error = f2fs_acl_update_mode(inode, &mode, &acl);
07393101 239 if (error)
af48b85b 240 return error;
14af20fc 241 set_acl_inode(inode, mode);
af48b85b
JK
242 }
243 break;
244
245 case ACL_TYPE_DEFAULT:
246 name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
247 if (!S_ISDIR(inode->i_mode))
248 return acl ? -EACCES : 0;
249 break;
250
251 default:
252 return -EINVAL;
253 }
254
255 if (acl) {
1ecc0c5c 256 value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
af48b85b 257 if (IS_ERR(value)) {
91942321 258 clear_inode_flag(inode, FI_ACL_MODE);
68390dd9 259 return PTR_ERR(value);
af48b85b
JK
260 }
261 }
262
c02745ef 263 error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
af48b85b 264
c8eb7024 265 kfree(value);
af48b85b
JK
266 if (!error)
267 set_cached_acl(inode, type, acl);
268
91942321 269 clear_inode_flag(inode, FI_ACL_MODE);
af48b85b
JK
270 return error;
271}
272
549c7297
CB
273int f2fs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
274 struct posix_acl *acl, int type)
af48b85b 275{
1f227a3e
JK
276 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
277 return -EIO;
278
a6dda0e6 279 return __f2fs_set_acl(inode, type, acl, NULL);
af48b85b
JK
280}
281
bce8d112
JK
282/*
283 * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
284 * are copied from posix_acl.c
285 */
286static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
287 gfp_t flags)
288{
289 struct posix_acl *clone = NULL;
290
291 if (acl) {
292 int size = sizeof(struct posix_acl) + acl->a_count *
293 sizeof(struct posix_acl_entry);
294 clone = kmemdup(acl, size, flags);
295 if (clone)
66717260 296 refcount_set(&clone->a_refcount, 1);
bce8d112
JK
297 }
298 return clone;
299}
300
301static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
302{
303 struct posix_acl_entry *pa, *pe;
304 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
305 umode_t mode = *mode_p;
306 int not_equiv = 0;
307
308 /* assert(atomic_read(acl->a_refcount) == 1); */
309
310 FOREACH_ACL_ENTRY(pa, acl, pe) {
c456362b 311 switch (pa->e_tag) {
bce8d112
JK
312 case ACL_USER_OBJ:
313 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
314 mode &= (pa->e_perm << 6) | ~S_IRWXU;
315 break;
316
317 case ACL_USER:
318 case ACL_GROUP:
319 not_equiv = 1;
320 break;
321
322 case ACL_GROUP_OBJ:
323 group_obj = pa;
324 break;
325
326 case ACL_OTHER:
327 pa->e_perm &= mode | ~S_IRWXO;
328 mode &= pa->e_perm | ~S_IRWXO;
329 break;
330
331 case ACL_MASK:
332 mask_obj = pa;
333 not_equiv = 1;
334 break;
335
336 default:
337 return -EIO;
338 }
339 }
340
341 if (mask_obj) {
342 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
343 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
344 } else {
345 if (!group_obj)
346 return -EIO;
347 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
348 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
349 }
350
351 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
3a912b77 352 return not_equiv;
bce8d112
JK
353}
354
355static int f2fs_acl_create(struct inode *dir, umode_t *mode,
356 struct posix_acl **default_acl, struct posix_acl **acl,
357 struct page *dpage)
358{
359 struct posix_acl *p;
272e083f 360 struct posix_acl *clone;
bce8d112
JK
361 int ret;
362
272e083f
CY
363 *acl = NULL;
364 *default_acl = NULL;
365
bce8d112 366 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
272e083f 367 return 0;
bce8d112
JK
368
369 p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
272e083f
CY
370 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
371 *mode &= ~current_umask();
372 return 0;
bce8d112 373 }
272e083f
CY
374 if (IS_ERR(p))
375 return PTR_ERR(p);
bce8d112 376
272e083f 377 clone = f2fs_acl_clone(p, GFP_NOFS);
f6176473
TY
378 if (!clone) {
379 ret = -ENOMEM;
380 goto release_acl;
381 }
bce8d112 382
272e083f 383 ret = f2fs_acl_create_masq(clone, mode);
83dfe53c 384 if (ret < 0)
f6176473 385 goto release_clone;
bce8d112 386
272e083f
CY
387 if (ret == 0)
388 posix_acl_release(clone);
389 else
390 *acl = clone;
bce8d112 391
272e083f 392 if (!S_ISDIR(*mode))
bce8d112 393 posix_acl_release(p);
272e083f 394 else
bce8d112 395 *default_acl = p;
bce8d112 396
bce8d112 397 return 0;
83dfe53c 398
f6176473 399release_clone:
272e083f 400 posix_acl_release(clone);
f6176473 401release_acl:
83dfe53c 402 posix_acl_release(p);
f6176473 403 return ret;
bce8d112
JK
404}
405
406int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
407 struct page *dpage)
af48b85b 408{
bce8d112 409 struct posix_acl *default_acl = NULL, *acl = NULL;
beb78181 410 int error;
af48b85b 411
bce8d112 412 error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
af48b85b
JK
413 if (error)
414 return error;
b8b60e1a 415
7c45729a 416 f2fs_mark_inode_dirty_sync(inode, true);
205b9822 417
a6dda0e6
CH
418 if (default_acl) {
419 error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
420 ipage);
421 posix_acl_release(default_acl);
313ed62a
CX
422 } else {
423 inode->i_default_acl = NULL;
a6dda0e6
CH
424 }
425 if (acl) {
3b6709b7 426 if (!error)
a6dda0e6
CH
427 error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
428 ipage);
429 posix_acl_release(acl);
313ed62a
CX
430 } else {
431 inode->i_acl = NULL;
af48b85b
JK
432 }
433
af48b85b
JK
434 return error;
435}