xref: /linux/fs/erofs/xattr.c (revision 8bc67e4db64aa72732c474b44ea8622062c903f0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2021-2022, Alibaba Cloud
6  */
7 #include <linux/security.h>
8 #include <linux/xxhash.h>
9 #include "xattr.h"
10 
11 struct erofs_xattr_iter {
12 	struct super_block *sb;
13 	struct erofs_buf buf;
14 	erofs_off_t pos;
15 	void *kaddr;
16 
17 	char *buffer;
18 	int buffer_size, buffer_ofs;
19 
20 	/* getxattr */
21 	int index, infix_len;
22 	struct qstr name;
23 
24 	/* listxattr */
25 	struct dentry *dentry;
26 };
27 
28 static const char *erofs_xattr_prefix(unsigned int idx, struct dentry *dentry);
29 
erofs_init_inode_xattrs(struct inode * inode)30 static int erofs_init_inode_xattrs(struct inode *inode)
31 {
32 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
33 	struct erofs_inode *vi = EROFS_I(inode);
34 	struct super_block *sb = inode->i_sb;
35 	const struct erofs_xattr_ibody_header *ih;
36 	__le32 *xattr_id;
37 	erofs_off_t pos;
38 	unsigned int i;
39 	int ret = 0;
40 
41 	if (!vi->xattr_isize)
42 		return -ENODATA;
43 
44 	/* the most case is that xattrs of this inode are initialized. */
45 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
46 		/*
47 		 * paired with smp_mb() at the end of the function to ensure
48 		 * fields will only be observed after the bit is set.
49 		 */
50 		smp_mb();
51 		return 0;
52 	}
53 	if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
54 		return -ERESTARTSYS;
55 
56 	/* someone has initialized xattrs for us? */
57 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
58 		goto out_unlock;
59 
60 	/*
61 	 * bypass all xattr operations if ->xattr_isize is not greater than
62 	 * sizeof(struct erofs_xattr_ibody_header), in detail:
63 	 * 1) it is not enough to contain erofs_xattr_ibody_header then
64 	 *    ->xattr_isize should be 0 (it means no xattr);
65 	 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
66 	 *    undefined right now (maybe use later with some new sb feature).
67 	 */
68 	if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
69 		erofs_err(sb, "xattr_isize %d of nid %llu is not supported yet",
70 			  vi->xattr_isize, vi->nid);
71 		ret = -EOPNOTSUPP;
72 		goto out_unlock;
73 	} else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
74 		erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
75 		DBG_BUGON(1);
76 		ret = -EFSCORRUPTED;
77 		goto out_unlock;
78 	}
79 
80 	pos = erofs_iloc(inode) + vi->inode_isize;
81 	ih = erofs_read_metabuf(&buf, sb, pos, erofs_inode_in_metabox(inode));
82 	if (IS_ERR(ih)) {
83 		ret = PTR_ERR(ih);
84 		goto out_unlock;
85 	}
86 	vi->xattr_name_filter = le32_to_cpu(ih->h_name_filter);
87 	vi->xattr_shared_count = ih->h_shared_count;
88 	if ((u32)vi->xattr_shared_count * sizeof(__le32) >
89 	    vi->xattr_isize - sizeof(struct erofs_xattr_ibody_header)) {
90 		erofs_err(sb, "invalid h_shared_count %u @ nid %llu",
91 			  vi->xattr_shared_count, vi->nid);
92 		ret = -EFSCORRUPTED;
93 		goto out_unlock;
94 	}
95 	vi->xattr_shared_xattrs = kmalloc_objs(uint, vi->xattr_shared_count);
96 	if (!vi->xattr_shared_xattrs) {
97 		ret = -ENOMEM;
98 		goto out_unlock;
99 	}
100 
101 	/* skip the ibody header and read the shared xattr array */
102 	pos += sizeof(struct erofs_xattr_ibody_header);
103 	for (i = 0; i < vi->xattr_shared_count; ++i) {
104 		xattr_id = erofs_bread(&buf, pos + i * sizeof(__le32), true);
105 		if (IS_ERR(xattr_id)) {
106 			kfree(vi->xattr_shared_xattrs);
107 			vi->xattr_shared_xattrs = NULL;
108 			ret = PTR_ERR(xattr_id);
109 			goto out_unlock;
110 		}
111 		vi->xattr_shared_xattrs[i] = le32_to_cpu(*xattr_id);
112 	}
113 
114 	/* paired with smp_mb() at the beginning of the function. */
115 	smp_mb();
116 	set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
117 out_unlock:
118 	erofs_put_metabuf(&buf);
119 	clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
120 	return ret;
121 }
122 
erofs_xattr_copy_to_buffer(struct erofs_xattr_iter * it,unsigned int len)123 static int erofs_xattr_copy_to_buffer(struct erofs_xattr_iter *it,
124 				      unsigned int len)
125 {
126 	unsigned int slice, processed;
127 	struct super_block *sb = it->sb;
128 
129 	for (processed = 0; processed < len; processed += slice) {
130 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
131 		if (IS_ERR(it->kaddr))
132 			return PTR_ERR(it->kaddr);
133 
134 		slice = min_t(unsigned int, sb->s_blocksize -
135 				erofs_blkoff(sb, it->pos), len - processed);
136 		memcpy(it->buffer + it->buffer_ofs, it->kaddr, slice);
137 		it->buffer_ofs += slice;
138 		it->pos += slice;
139 	}
140 	return 0;
141 }
142 
erofs_listxattr_foreach(struct erofs_xattr_iter * it)143 static int erofs_listxattr_foreach(struct erofs_xattr_iter *it)
144 {
145 	struct erofs_xattr_entry entry;
146 	unsigned int base_index, name_total, prefix_len, infix_len = 0;
147 	const char *prefix, *infix = NULL;
148 	int err;
149 
150 	/* 1. handle xattr entry */
151 	entry = *(struct erofs_xattr_entry *)it->kaddr;
152 	it->pos += sizeof(struct erofs_xattr_entry);
153 
154 	base_index = entry.e_name_index;
155 	if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
156 		struct erofs_sb_info *sbi = EROFS_SB(it->sb);
157 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
158 			(entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
159 
160 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
161 			return 0;
162 		infix = pf->prefix->infix;
163 		infix_len = pf->infix_len;
164 		base_index = pf->prefix->base_index;
165 	}
166 
167 	prefix = erofs_xattr_prefix(base_index, it->dentry);
168 	if (!prefix)
169 		return 0;
170 	prefix_len = strlen(prefix);
171 	name_total = prefix_len + infix_len + entry.e_name_len + 1;
172 
173 	if (!it->buffer) {
174 		it->buffer_ofs += name_total;
175 		return 0;
176 	}
177 
178 	if (it->buffer_ofs + name_total > it->buffer_size)
179 		return -ERANGE;
180 
181 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
182 	memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
183 	it->buffer_ofs += prefix_len + infix_len;
184 
185 	/* 2. handle xattr name */
186 	err = erofs_xattr_copy_to_buffer(it, entry.e_name_len);
187 	if (err)
188 		return err;
189 
190 	it->buffer[it->buffer_ofs++] = '\0';
191 	return 0;
192 }
193 
erofs_getxattr_foreach(struct erofs_xattr_iter * it)194 static int erofs_getxattr_foreach(struct erofs_xattr_iter *it)
195 {
196 	struct super_block *sb = it->sb;
197 	struct erofs_xattr_entry entry;
198 	unsigned int slice, processed, value_sz;
199 
200 	/* 1. handle xattr entry */
201 	entry = *(struct erofs_xattr_entry *)it->kaddr;
202 	it->pos += sizeof(struct erofs_xattr_entry);
203 	value_sz = le16_to_cpu(entry.e_value_size);
204 
205 	/* should also match the infix for long name prefixes */
206 	if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
207 		struct erofs_sb_info *sbi = EROFS_SB(sb);
208 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
209 			(entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
210 
211 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
212 			return -ENODATA;
213 
214 		if (it->index != pf->prefix->base_index ||
215 		    it->name.len != entry.e_name_len + pf->infix_len)
216 			return -ENODATA;
217 
218 		if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
219 			return -ENODATA;
220 
221 		it->infix_len = pf->infix_len;
222 	} else {
223 		if (it->index != entry.e_name_index ||
224 		    it->name.len != entry.e_name_len)
225 			return -ENODATA;
226 
227 		it->infix_len = 0;
228 	}
229 
230 	/* 2. handle xattr name */
231 	for (processed = 0; processed < entry.e_name_len; processed += slice) {
232 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
233 		if (IS_ERR(it->kaddr))
234 			return PTR_ERR(it->kaddr);
235 
236 		slice = min_t(unsigned int,
237 				sb->s_blocksize - erofs_blkoff(sb, it->pos),
238 				entry.e_name_len - processed);
239 		if (memcmp(it->name.name + it->infix_len + processed,
240 			   it->kaddr, slice))
241 			return -ENODATA;
242 		it->pos += slice;
243 	}
244 
245 	/* 3. handle xattr value */
246 	if (!it->buffer) {
247 		it->buffer_ofs = value_sz;
248 		return 0;
249 	}
250 
251 	if (it->buffer_size < value_sz)
252 		return -ERANGE;
253 
254 	return erofs_xattr_copy_to_buffer(it, value_sz);
255 }
256 
erofs_xattr_iter_inline(struct erofs_xattr_iter * it,struct inode * inode,bool getxattr)257 static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
258 				   struct inode *inode, bool getxattr)
259 {
260 	struct erofs_inode *const vi = EROFS_I(inode);
261 	unsigned int xattr_header_sz, remaining, entry_sz;
262 	erofs_off_t next_pos;
263 	int ret;
264 
265 	xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
266 			  sizeof(u32) * vi->xattr_shared_count;
267 	if (xattr_header_sz >= vi->xattr_isize) {
268 		DBG_BUGON(xattr_header_sz > vi->xattr_isize);
269 		return -ENODATA;
270 	}
271 
272 	ret = erofs_init_metabuf(&it->buf, it->sb, erofs_inode_in_metabox(inode));
273 	if (ret)
274 		return ret;
275 	remaining = vi->xattr_isize - xattr_header_sz;
276 	it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
277 
278 	while (remaining) {
279 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
280 		if (IS_ERR(it->kaddr))
281 			return PTR_ERR(it->kaddr);
282 
283 		entry_sz = erofs_xattr_entry_size(it->kaddr);
284 		/* xattr on-disk corruption: xattr entry beyond xattr_isize */
285 		if (remaining < entry_sz) {
286 			DBG_BUGON(1);
287 			return -EFSCORRUPTED;
288 		}
289 		remaining -= entry_sz;
290 		next_pos = it->pos + entry_sz;
291 
292 		if (getxattr)
293 			ret = erofs_getxattr_foreach(it);
294 		else
295 			ret = erofs_listxattr_foreach(it);
296 		if ((getxattr && ret != -ENODATA) || (!getxattr && ret))
297 			break;
298 
299 		it->pos = next_pos;
300 	}
301 	return ret;
302 }
303 
erofs_xattr_iter_shared(struct erofs_xattr_iter * it,struct inode * inode,bool getxattr)304 static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
305 				   struct inode *inode, bool getxattr)
306 {
307 	struct erofs_inode *const vi = EROFS_I(inode);
308 	struct super_block *const sb = it->sb;
309 	struct erofs_sb_info *sbi = EROFS_SB(sb);
310 	unsigned int i = 0;
311 	int ret;
312 
313 	ret = erofs_init_metabuf(&it->buf, sb,
314 				 erofs_sb_has_shared_ea_in_metabox(sbi));
315 	if (ret)
316 		return ret;
317 
318 	while (i < vi->xattr_shared_count) {
319 		it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
320 				vi->xattr_shared_xattrs[i++] * sizeof(__le32);
321 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
322 		if (IS_ERR(it->kaddr))
323 			return PTR_ERR(it->kaddr);
324 
325 		if (getxattr)
326 			ret = erofs_getxattr_foreach(it);
327 		else
328 			ret = erofs_listxattr_foreach(it);
329 		if ((getxattr && ret != -ENODATA) || (!getxattr && ret))
330 			break;
331 	}
332 	return i ? ret : -ENODATA;
333 }
334 
erofs_getxattr(struct inode * inode,int index,const char * name,void * buffer,size_t buffer_size)335 static int erofs_getxattr(struct inode *inode, int index, const char *name,
336 			  void *buffer, size_t buffer_size)
337 {
338 	int ret;
339 	unsigned int hashbit;
340 	struct erofs_xattr_iter it;
341 	struct erofs_inode *vi = EROFS_I(inode);
342 	struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
343 
344 	if (!name)
345 		return -EINVAL;
346 
347 	ret = erofs_init_inode_xattrs(inode);
348 	if (ret)
349 		return ret;
350 
351 	/* reserved flag is non-zero if there's any change of on-disk format */
352 	if (erofs_sb_has_xattr_filter(sbi) && !sbi->xattr_filter_reserved) {
353 		hashbit = xxh32(name, strlen(name),
354 				EROFS_XATTR_FILTER_SEED + index);
355 		hashbit &= EROFS_XATTR_FILTER_BITS - 1;
356 		if (vi->xattr_name_filter & (1U << hashbit))
357 			return -ENODATA;
358 	}
359 
360 	it.index = index;
361 	it.name = QSTR(name);
362 	if (it.name.len > EROFS_NAME_LEN)
363 		return -ERANGE;
364 
365 	it.sb = inode->i_sb;
366 	it.buf = __EROFS_BUF_INITIALIZER;
367 	it.buffer = buffer;
368 	it.buffer_size = buffer_size;
369 	it.buffer_ofs = 0;
370 
371 	ret = erofs_xattr_iter_inline(&it, inode, true);
372 	if (ret == -ENODATA)
373 		ret = erofs_xattr_iter_shared(&it, inode, true);
374 	erofs_put_metabuf(&it.buf);
375 	return ret ? ret : it.buffer_ofs;
376 }
377 
erofs_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)378 ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
379 {
380 	int ret;
381 	struct erofs_xattr_iter it;
382 	struct inode *inode = d_inode(dentry);
383 
384 	ret = erofs_init_inode_xattrs(inode);
385 	if (ret == -ENODATA)
386 		return 0;
387 	if (ret)
388 		return ret;
389 
390 	it.sb = dentry->d_sb;
391 	it.buf = __EROFS_BUF_INITIALIZER;
392 	it.dentry = dentry;
393 	it.buffer = buffer;
394 	it.buffer_size = buffer_size;
395 	it.buffer_ofs = 0;
396 
397 	ret = erofs_xattr_iter_inline(&it, inode, false);
398 	if (!ret || ret == -ENODATA)
399 		ret = erofs_xattr_iter_shared(&it, inode, false);
400 	if (ret == -ENODATA)
401 		ret = 0;
402 	erofs_put_metabuf(&it.buf);
403 	return ret ? ret : it.buffer_ofs;
404 }
405 
erofs_xattr_user_list(struct dentry * dentry)406 static bool erofs_xattr_user_list(struct dentry *dentry)
407 {
408 	return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
409 }
410 
erofs_xattr_trusted_list(struct dentry * dentry)411 static bool erofs_xattr_trusted_list(struct dentry *dentry)
412 {
413 	return capable(CAP_SYS_ADMIN);
414 }
415 
erofs_xattr_generic_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)416 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
417 				   struct dentry *unused, struct inode *inode,
418 				   const char *name, void *buffer, size_t size)
419 {
420 	if (handler->flags == EROFS_XATTR_INDEX_USER &&
421 	    !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
422 		return -EOPNOTSUPP;
423 
424 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
425 }
426 
427 static const struct xattr_handler erofs_xattr_user_handler = {
428 	.prefix	= XATTR_USER_PREFIX,
429 	.flags	= EROFS_XATTR_INDEX_USER,
430 	.list	= erofs_xattr_user_list,
431 	.get	= erofs_xattr_generic_get,
432 };
433 
434 static const struct xattr_handler erofs_xattr_trusted_handler = {
435 	.prefix	= XATTR_TRUSTED_PREFIX,
436 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
437 	.list	= erofs_xattr_trusted_list,
438 	.get	= erofs_xattr_generic_get,
439 };
440 
441 #ifdef CONFIG_EROFS_FS_SECURITY
442 static const struct xattr_handler erofs_xattr_security_handler = {
443 	.prefix	= XATTR_SECURITY_PREFIX,
444 	.flags	= EROFS_XATTR_INDEX_SECURITY,
445 	.get	= erofs_xattr_generic_get,
446 };
447 #endif
448 
449 const struct xattr_handler * const erofs_xattr_handlers[] = {
450 	&erofs_xattr_user_handler,
451 	&erofs_xattr_trusted_handler,
452 #ifdef CONFIG_EROFS_FS_SECURITY
453 	&erofs_xattr_security_handler,
454 #endif
455 	NULL,
456 };
457 
erofs_xattr_prefix(unsigned int idx,struct dentry * dentry)458 static const char *erofs_xattr_prefix(unsigned int idx, struct dentry *dentry)
459 {
460 	static const struct xattr_handler * const xattr_handler_map[] = {
461 		[EROFS_XATTR_INDEX_USER] = &erofs_xattr_user_handler,
462 #ifdef CONFIG_EROFS_FS_POSIX_ACL
463 		[EROFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access,
464 		[EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default,
465 #endif
466 		[EROFS_XATTR_INDEX_TRUSTED] = &erofs_xattr_trusted_handler,
467 #ifdef CONFIG_EROFS_FS_SECURITY
468 		[EROFS_XATTR_INDEX_SECURITY] = &erofs_xattr_security_handler,
469 #endif
470 	};
471 	const struct xattr_handler *handler = NULL;
472 
473 	if (idx && idx < ARRAY_SIZE(xattr_handler_map)) {
474 		handler = xattr_handler_map[idx];
475 		if (xattr_handler_can_list(handler, dentry))
476 			return xattr_prefix(handler);
477 	}
478 	return NULL;
479 }
480 
erofs_xattr_prefixes_cleanup(struct super_block * sb)481 void erofs_xattr_prefixes_cleanup(struct super_block *sb)
482 {
483 	struct erofs_sb_info *sbi = EROFS_SB(sb);
484 	int i;
485 
486 	if (sbi->xattr_prefixes) {
487 		for (i = 0; i < sbi->xattr_prefix_count; i++)
488 			kfree(sbi->xattr_prefixes[i].prefix);
489 		kfree(sbi->xattr_prefixes);
490 		sbi->xattr_prefixes = NULL;
491 	}
492 }
493 
erofs_xattr_prefixes_init(struct super_block * sb)494 int erofs_xattr_prefixes_init(struct super_block *sb)
495 {
496 	struct erofs_sb_info *sbi = EROFS_SB(sb);
497 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
498 	erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
499 	struct erofs_xattr_prefix_item *pfs;
500 	int ret = 0, i, len;
501 	bool plain = erofs_sb_has_plain_xattr_pfx(sbi);
502 
503 	if (!sbi->xattr_prefix_count)
504 		return 0;
505 
506 	pfs = kzalloc_objs(*pfs, sbi->xattr_prefix_count);
507 	if (!pfs)
508 		return -ENOMEM;
509 
510 	if (!plain) {
511 		if (erofs_sb_has_metabox(sbi))
512 			(void)erofs_init_metabuf(&buf, sb, true);
513 		else if (sbi->packed_inode)
514 			buf.mapping = sbi->packed_inode->i_mapping;
515 		else
516 			plain = true;
517 	}
518 	if (plain)
519 		(void)erofs_init_metabuf(&buf, sb, false);
520 
521 	for (i = 0; i < sbi->xattr_prefix_count; i++) {
522 		void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
523 
524 		if (IS_ERR(ptr)) {
525 			ret = PTR_ERR(ptr);
526 			break;
527 		} else if (len < sizeof(*pfs->prefix) ||
528 			   len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
529 			kfree(ptr);
530 			ret = -EFSCORRUPTED;
531 			break;
532 		}
533 		pfs[i].prefix = ptr;
534 		pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
535 	}
536 
537 	erofs_put_metabuf(&buf);
538 	if (!ret && erofs_sb_has_ishare_xattrs(sbi)) {
539 		struct erofs_xattr_prefix_item *pf = pfs + sbi->ishare_xattr_prefix_id;
540 		struct erofs_xattr_long_prefix *newpfx;
541 
542 		newpfx = krealloc(pf->prefix,
543 			sizeof(*newpfx) + pf->infix_len + 1, GFP_KERNEL);
544 		if (newpfx) {
545 			newpfx->infix[pf->infix_len] = '\0';
546 			pf->prefix = newpfx;
547 		} else {
548 			ret = -ENOMEM;
549 		}
550 	}
551 	sbi->xattr_prefixes = pfs;
552 	if (ret)
553 		erofs_xattr_prefixes_cleanup(sb);
554 	return ret;
555 }
556 
557 #ifdef CONFIG_EROFS_FS_POSIX_ACL
erofs_get_acl(struct inode * inode,int type,bool rcu)558 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
559 {
560 	struct posix_acl *acl;
561 	int prefix, rc;
562 	char *value = NULL;
563 
564 	if (rcu)
565 		return ERR_PTR(-ECHILD);
566 
567 	switch (type) {
568 	case ACL_TYPE_ACCESS:
569 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
570 		break;
571 	case ACL_TYPE_DEFAULT:
572 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
573 		break;
574 	default:
575 		return ERR_PTR(-EINVAL);
576 	}
577 
578 	rc = erofs_getxattr(inode, prefix, "", NULL, 0);
579 	if (rc > 0) {
580 		value = kmalloc(rc, GFP_KERNEL);
581 		if (!value)
582 			return ERR_PTR(-ENOMEM);
583 		rc = erofs_getxattr(inode, prefix, "", value, rc);
584 	}
585 
586 	if (rc == -ENODATA)
587 		acl = NULL;
588 	else if (rc < 0)
589 		acl = ERR_PTR(rc);
590 	else
591 		acl = posix_acl_from_xattr(&init_user_ns, value, rc);
592 	kfree(value);
593 	return acl;
594 }
595 
erofs_inode_has_noacl(struct inode * inode,void * kaddr,unsigned int ofs)596 bool erofs_inode_has_noacl(struct inode *inode, void *kaddr, unsigned int ofs)
597 {
598 	static const unsigned int bitmask =
599 		BIT(21) |	/* system.posix_acl_default */
600 		BIT(30);	/* system.posix_acl_access */
601 	struct erofs_sb_info *sbi = EROFS_I_SB(inode);
602 	const struct erofs_xattr_ibody_header *ih = kaddr + ofs;
603 
604 	if (EROFS_I(inode)->xattr_isize < sizeof(*ih))
605 		return true;
606 
607 	if (erofs_sb_has_xattr_filter(sbi) && !sbi->xattr_filter_reserved &&
608 	    !check_add_overflow(ofs, sizeof(*ih), &ofs) &&
609 	    ofs <= i_blocksize(inode)) {
610 		if ((le32_to_cpu(ih->h_name_filter) & bitmask) == bitmask)
611 			return true;
612 	}
613 	return false;
614 }
615 #endif
616 
617 #ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE
erofs_xattr_fill_inode_fingerprint(struct erofs_inode_fingerprint * fp,struct inode * inode,const char * domain_id)618 int erofs_xattr_fill_inode_fingerprint(struct erofs_inode_fingerprint *fp,
619 				       struct inode *inode, const char *domain_id)
620 {
621 	struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
622 	struct erofs_xattr_prefix_item *prefix;
623 	const char *infix;
624 	int valuelen, base_index;
625 
626 	if (!test_opt(&sbi->opt, INODE_SHARE))
627 		return -EOPNOTSUPP;
628 	if (!sbi->xattr_prefixes)
629 		return -EINVAL;
630 	prefix = sbi->xattr_prefixes + sbi->ishare_xattr_prefix_id;
631 	infix = prefix->prefix->infix;
632 	base_index = prefix->prefix->base_index;
633 	valuelen = erofs_getxattr(inode, base_index, infix, NULL, 0);
634 	if (valuelen <= 0 || valuelen > (1 << sbi->blkszbits))
635 		return -EFSCORRUPTED;
636 	fp->size = valuelen + (domain_id ? strlen(domain_id) : 0);
637 	fp->opaque = kmalloc(fp->size, GFP_KERNEL);
638 	if (!fp->opaque)
639 		return -ENOMEM;
640 	if (valuelen != erofs_getxattr(inode, base_index, infix,
641 				       fp->opaque, valuelen)) {
642 		kfree(fp->opaque);
643 		fp->opaque = NULL;
644 		return -EFSCORRUPTED;
645 	}
646 	memcpy(fp->opaque + valuelen, domain_id, fp->size - valuelen);
647 	return 0;
648 }
649 #endif
650