xref: /openbmc/linux/fs/f2fs/acl.c (revision 5b448065)
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/f2fs_fs.h>
13 #include "f2fs.h"
14 #include "xattr.h"
15 #include "acl.h"
16 
17 static 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 
29 static inline int f2fs_acl_count(size_t size)
30 {
31 	ssize_t s;
32 
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 
46 static 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 
54 	if (size < sizeof(struct f2fs_acl_header))
55 		return ERR_PTR(-EINVAL);
56 
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 
66 	acl = posix_acl_alloc(count, GFP_NOFS);
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:
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;
108 fail:
109 	posix_acl_release(acl);
110 	return ERR_PTR(-EINVAL);
111 }
112 
113 static void *f2fs_acl_to_disk(struct f2fs_sb_info *sbi,
114 				const struct posix_acl *acl, size_t *size)
115 {
116 	struct f2fs_acl_header *f2fs_acl;
117 	struct f2fs_acl_entry *entry;
118 	int i;
119 
120 	f2fs_acl = f2fs_kmalloc(sbi, sizeof(struct f2fs_acl_header) +
121 			acl->a_count * sizeof(struct f2fs_acl_entry),
122 			GFP_NOFS);
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 
163 fail:
164 	kfree(f2fs_acl);
165 	return ERR_PTR(-EINVAL);
166 }
167 
168 static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
169 						struct page *dpage)
170 {
171 	int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
172 	void *value = NULL;
173 	struct posix_acl *acl;
174 	int retval;
175 
176 	if (type == ACL_TYPE_ACCESS)
177 		name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
178 
179 	retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
180 	if (retval > 0) {
181 		value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
182 		if (!value)
183 			return ERR_PTR(-ENOMEM);
184 		retval = f2fs_getxattr(inode, name_index, "", value,
185 							retval, dpage);
186 	}
187 
188 	if (retval > 0)
189 		acl = f2fs_acl_from_disk(value, retval);
190 	else if (retval == -ENODATA)
191 		acl = NULL;
192 	else
193 		acl = ERR_PTR(retval);
194 	kfree(value);
195 
196 	return acl;
197 }
198 
199 struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
200 {
201 	return __f2fs_get_acl(inode, type, NULL);
202 }
203 
204 static 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;
218 	if (!in_group_p(i_gid_into_mnt(&init_user_ns, inode)) &&
219 	    !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FSETID))
220 		mode &= ~S_ISGID;
221 	*mode_p = mode;
222 	return 0;
223 }
224 
225 static int __f2fs_set_acl(struct inode *inode, int type,
226 			struct posix_acl *acl, struct page *ipage)
227 {
228 	int name_index;
229 	void *value = NULL;
230 	size_t size = 0;
231 	int error;
232 	umode_t mode = inode->i_mode;
233 
234 	switch (type) {
235 	case ACL_TYPE_ACCESS:
236 		name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
237 		if (acl && !ipage) {
238 			error = f2fs_acl_update_mode(inode, &mode, &acl);
239 			if (error)
240 				return error;
241 			set_acl_inode(inode, mode);
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) {
256 		value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
257 		if (IS_ERR(value)) {
258 			clear_inode_flag(inode, FI_ACL_MODE);
259 			return PTR_ERR(value);
260 		}
261 	}
262 
263 	error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
264 
265 	kfree(value);
266 	if (!error)
267 		set_cached_acl(inode, type, acl);
268 
269 	clear_inode_flag(inode, FI_ACL_MODE);
270 	return error;
271 }
272 
273 int f2fs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
274 		 struct posix_acl *acl, int type)
275 {
276 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
277 		return -EIO;
278 
279 	return __f2fs_set_acl(inode, type, acl, NULL);
280 }
281 
282 /*
283  * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
284  * are copied from posix_acl.c
285  */
286 static 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)
296 			refcount_set(&clone->a_refcount, 1);
297 	}
298 	return clone;
299 }
300 
301 static 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) {
311 		switch (pa->e_tag) {
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;
352 	return not_equiv;
353 }
354 
355 static 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;
360 	struct posix_acl *clone;
361 	int ret;
362 
363 	*acl = NULL;
364 	*default_acl = NULL;
365 
366 	if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
367 		return 0;
368 
369 	p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
370 	if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
371 		*mode &= ~current_umask();
372 		return 0;
373 	}
374 	if (IS_ERR(p))
375 		return PTR_ERR(p);
376 
377 	clone = f2fs_acl_clone(p, GFP_NOFS);
378 	if (!clone) {
379 		ret = -ENOMEM;
380 		goto release_acl;
381 	}
382 
383 	ret = f2fs_acl_create_masq(clone, mode);
384 	if (ret < 0)
385 		goto release_clone;
386 
387 	if (ret == 0)
388 		posix_acl_release(clone);
389 	else
390 		*acl = clone;
391 
392 	if (!S_ISDIR(*mode))
393 		posix_acl_release(p);
394 	else
395 		*default_acl = p;
396 
397 	return 0;
398 
399 release_clone:
400 	posix_acl_release(clone);
401 release_acl:
402 	posix_acl_release(p);
403 	return ret;
404 }
405 
406 int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
407 							struct page *dpage)
408 {
409 	struct posix_acl *default_acl = NULL, *acl = NULL;
410 	int error;
411 
412 	error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
413 	if (error)
414 		return error;
415 
416 	f2fs_mark_inode_dirty_sync(inode, true);
417 
418 	if (default_acl) {
419 		error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
420 				       ipage);
421 		posix_acl_release(default_acl);
422 	} else {
423 		inode->i_default_acl = NULL;
424 	}
425 	if (acl) {
426 		if (!error)
427 			error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
428 					       ipage);
429 		posix_acl_release(acl);
430 	} else {
431 		inode->i_acl = NULL;
432 	}
433 
434 	return error;
435 }
436