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