File Index
Files and directories
Sign in
FILE

acl.c

File preview with syntax highlighting.

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