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