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