1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/drivers/staging/erofs/xattr.c
4  *
5  * Copyright (C) 2017-2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file COPYING in the main directory of the Linux
11  * distribution for more details.
12  */
13 #include <linux/security.h>
14 #include "xattr.h"
15 
16 struct xattr_iter {
17 	struct super_block *sb;
18 	struct page *page;
19 	void *kaddr;
20 
21 	erofs_blk_t blkaddr;
22 	unsigned ofs;
23 };
24 
xattr_iter_end(struct xattr_iter * it,bool atomic)25 static inline void xattr_iter_end(struct xattr_iter *it, bool atomic)
26 {
27 	/* only init_inode_xattrs use non-atomic once */
28 	if (unlikely(!atomic))
29 		kunmap(it->page);
30 	else
31 		kunmap_atomic(it->kaddr);
32 	unlock_page(it->page);
33 	put_page(it->page);
34 }
35 
init_inode_xattrs(struct inode * inode)36 static void init_inode_xattrs(struct inode *inode)
37 {
38 	struct xattr_iter it;
39 	unsigned i;
40 	struct erofs_xattr_ibody_header *ih;
41 	struct erofs_sb_info *sbi;
42 	struct erofs_vnode *vi;
43 	bool atomic_map;
44 
45 	if (likely(inode_has_inited_xattr(inode)))
46 		return;
47 
48 	vi = EROFS_V(inode);
49 	BUG_ON(!vi->xattr_isize);
50 
51 	sbi = EROFS_I_SB(inode);
52 	it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
53 	it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
54 
55 	it.page = erofs_get_inline_page(inode, it.blkaddr);
56 	BUG_ON(IS_ERR(it.page));
57 
58 	/* read in shared xattr array (non-atomic, see kmalloc below) */
59 	it.kaddr = kmap(it.page);
60 	atomic_map = false;
61 
62 	ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
63 
64 	vi->xattr_shared_count = ih->h_shared_count;
65 	vi->xattr_shared_xattrs = (unsigned *)kmalloc_array(
66 		vi->xattr_shared_count, sizeof(unsigned),
67 		GFP_KERNEL | __GFP_NOFAIL);
68 
69 	/* let's skip ibody header */
70 	it.ofs += sizeof(struct erofs_xattr_ibody_header);
71 
72 	for (i = 0; i < vi->xattr_shared_count; ++i) {
73 		if (unlikely(it.ofs >= EROFS_BLKSIZ)) {
74 			/* cannot be unaligned */
75 			BUG_ON(it.ofs != EROFS_BLKSIZ);
76 			xattr_iter_end(&it, atomic_map);
77 
78 			it.page = erofs_get_meta_page(inode->i_sb,
79 				++it.blkaddr, S_ISDIR(inode->i_mode));
80 			BUG_ON(IS_ERR(it.page));
81 
82 			it.kaddr = kmap_atomic(it.page);
83 			atomic_map = true;
84 			it.ofs = 0;
85 		}
86 		vi->xattr_shared_xattrs[i] =
87 			le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
88 		it.ofs += sizeof(__le32);
89 	}
90 	xattr_iter_end(&it, atomic_map);
91 
92 	inode_set_inited_xattr(inode);
93 }
94 
95 struct xattr_iter_handlers {
96 	int (*entry)(struct xattr_iter *, struct erofs_xattr_entry *);
97 	int (*name)(struct xattr_iter *, unsigned, char *, unsigned);
98 	int (*alloc_buffer)(struct xattr_iter *, unsigned);
99 	void (*value)(struct xattr_iter *, unsigned, char *, unsigned);
100 };
101 
xattr_iter_fixup(struct xattr_iter * it)102 static void xattr_iter_fixup(struct xattr_iter *it)
103 {
104 	if (unlikely(it->ofs >= EROFS_BLKSIZ)) {
105 		xattr_iter_end(it, true);
106 
107 		it->blkaddr += erofs_blknr(it->ofs);
108 		it->page = erofs_get_meta_page(it->sb, it->blkaddr, false);
109 		BUG_ON(IS_ERR(it->page));
110 
111 		it->kaddr = kmap_atomic(it->page);
112 		it->ofs = erofs_blkoff(it->ofs);
113 	}
114 }
115 
inline_xattr_iter_begin(struct xattr_iter * it,struct inode * inode)116 static int inline_xattr_iter_begin(struct xattr_iter *it,
117 	struct inode *inode)
118 {
119 	struct erofs_vnode *const vi = EROFS_V(inode);
120 	struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
121 	unsigned xattr_header_sz, inline_xattr_ofs;
122 
123 	xattr_header_sz = inlinexattr_header_size(inode);
124 	if (unlikely(xattr_header_sz >= vi->xattr_isize)) {
125 		BUG_ON(xattr_header_sz > vi->xattr_isize);
126 		return -ENOATTR;
127 	}
128 
129 	inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
130 
131 	it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
132 	it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
133 
134 	it->page = erofs_get_inline_page(inode, it->blkaddr);
135 	BUG_ON(IS_ERR(it->page));
136 	it->kaddr = kmap_atomic(it->page);
137 
138 	return vi->xattr_isize - xattr_header_sz;
139 }
140 
xattr_foreach(struct xattr_iter * it,struct xattr_iter_handlers * op,unsigned * tlimit)141 static int xattr_foreach(struct xattr_iter *it,
142 	struct xattr_iter_handlers *op, unsigned *tlimit)
143 {
144 	struct erofs_xattr_entry entry;
145 	unsigned value_sz, processed, slice;
146 	int err;
147 
148 	/* 0. fixup blkaddr, ofs, ipage */
149 	xattr_iter_fixup(it);
150 
151 	/*
152 	 * 1. read xattr entry to the memory,
153 	 *    since we do EROFS_XATTR_ALIGN
154 	 *    therefore entry should be in the page
155 	 */
156 	entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
157 	if (tlimit != NULL) {
158 		unsigned entry_sz = EROFS_XATTR_ENTRY_SIZE(&entry);
159 
160 		BUG_ON(*tlimit < entry_sz);
161 		*tlimit -= entry_sz;
162 	}
163 
164 	it->ofs += sizeof(struct erofs_xattr_entry);
165 	value_sz = le16_to_cpu(entry.e_value_size);
166 
167 	/* handle entry */
168 	err = op->entry(it, &entry);
169 	if (err) {
170 		it->ofs += entry.e_name_len + value_sz;
171 		goto out;
172 	}
173 
174 	/* 2. handle xattr name (ofs will finally be at the end of name) */
175 	processed = 0;
176 
177 	while (processed < entry.e_name_len) {
178 		if (it->ofs >= EROFS_BLKSIZ) {
179 			BUG_ON(it->ofs > EROFS_BLKSIZ);
180 
181 			xattr_iter_fixup(it);
182 			it->ofs = 0;
183 		}
184 
185 		slice = min_t(unsigned, PAGE_SIZE - it->ofs,
186 			entry.e_name_len - processed);
187 
188 		/* handle name */
189 		err = op->name(it, processed, it->kaddr + it->ofs, slice);
190 		if (err) {
191 			it->ofs += entry.e_name_len - processed + value_sz;
192 			goto out;
193 		}
194 
195 		it->ofs += slice;
196 		processed += slice;
197 	}
198 
199 	/* 3. handle xattr value */
200 	processed = 0;
201 
202 	if (op->alloc_buffer != NULL) {
203 		err = op->alloc_buffer(it, value_sz);
204 		if (err) {
205 			it->ofs += value_sz;
206 			goto out;
207 		}
208 	}
209 
210 	while (processed < value_sz) {
211 		if (it->ofs >= EROFS_BLKSIZ) {
212 			BUG_ON(it->ofs > EROFS_BLKSIZ);
213 			xattr_iter_fixup(it);
214 			it->ofs = 0;
215 		}
216 
217 		slice = min_t(unsigned, PAGE_SIZE - it->ofs,
218 			value_sz - processed);
219 		op->value(it, processed, it->kaddr + it->ofs, slice);
220 		it->ofs += slice;
221 		processed += slice;
222 	}
223 
224 out:
225 	/* we assume that ofs is aligned with 4 bytes */
226 	it->ofs = EROFS_XATTR_ALIGN(it->ofs);
227 	return err;
228 }
229 
230 struct getxattr_iter {
231 	struct xattr_iter it;
232 
233 	char *buffer;
234 	int buffer_size, index;
235 	struct qstr name;
236 };
237 
xattr_entrymatch(struct xattr_iter * _it,struct erofs_xattr_entry * entry)238 static int xattr_entrymatch(struct xattr_iter *_it,
239 	struct erofs_xattr_entry *entry)
240 {
241 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
242 
243 	return (it->index != entry->e_name_index ||
244 		it->name.len != entry->e_name_len) ? -ENOATTR : 0;
245 }
246 
xattr_namematch(struct xattr_iter * _it,unsigned processed,char * buf,unsigned len)247 static int xattr_namematch(struct xattr_iter *_it,
248 	unsigned processed, char *buf, unsigned len)
249 {
250 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
251 
252 	return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
253 }
254 
xattr_checkbuffer(struct xattr_iter * _it,unsigned value_sz)255 static int xattr_checkbuffer(struct xattr_iter *_it,
256 	unsigned value_sz)
257 {
258 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
259 	int err = it->buffer_size < value_sz ? -ERANGE : 0;
260 
261 	it->buffer_size = value_sz;
262 	return it->buffer == NULL ? 1 : err;
263 }
264 
xattr_copyvalue(struct xattr_iter * _it,unsigned processed,char * buf,unsigned len)265 static void xattr_copyvalue(struct xattr_iter *_it,
266 	unsigned processed, char *buf, unsigned len)
267 {
268 	struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
269 
270 	memcpy(it->buffer + processed, buf, len);
271 }
272 
273 static struct xattr_iter_handlers find_xattr_handlers = {
274 	.entry = xattr_entrymatch,
275 	.name = xattr_namematch,
276 	.alloc_buffer = xattr_checkbuffer,
277 	.value = xattr_copyvalue
278 };
279 
inline_getxattr(struct inode * inode,struct getxattr_iter * it)280 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
281 {
282 	int ret;
283 	unsigned remaining;
284 
285 	ret = inline_xattr_iter_begin(&it->it, inode);
286 	if (ret < 0)
287 		return ret;
288 
289 	remaining = ret;
290 	while (remaining) {
291 		ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
292 		if (ret >= 0)
293 			break;
294 	}
295 	xattr_iter_end(&it->it, true);
296 
297 	return ret < 0 ? ret : it->buffer_size;
298 }
299 
shared_getxattr(struct inode * inode,struct getxattr_iter * it)300 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
301 {
302 	struct erofs_vnode *const vi = EROFS_V(inode);
303 	struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
304 	unsigned i;
305 	int ret = -ENOATTR;
306 
307 	for (i = 0; i < vi->xattr_shared_count; ++i) {
308 		erofs_blk_t blkaddr =
309 			xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
310 
311 		it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
312 
313 		if (!i || blkaddr != it->it.blkaddr) {
314 			if (i)
315 				xattr_iter_end(&it->it, true);
316 
317 			it->it.page = erofs_get_meta_page(inode->i_sb,
318 				blkaddr, false);
319 			BUG_ON(IS_ERR(it->it.page));
320 			it->it.kaddr = kmap_atomic(it->it.page);
321 			it->it.blkaddr = blkaddr;
322 		}
323 
324 		ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
325 		if (ret >= 0)
326 			break;
327 	}
328 	if (vi->xattr_shared_count)
329 		xattr_iter_end(&it->it, true);
330 
331 	return ret < 0 ? ret : it->buffer_size;
332 }
333 
erofs_xattr_user_list(struct dentry * dentry)334 static bool erofs_xattr_user_list(struct dentry *dentry)
335 {
336 	return test_opt(EROFS_SB(dentry->d_sb), XATTR_USER);
337 }
338 
erofs_xattr_trusted_list(struct dentry * dentry)339 static bool erofs_xattr_trusted_list(struct dentry *dentry)
340 {
341 	return capable(CAP_SYS_ADMIN);
342 }
343 
erofs_getxattr(struct inode * inode,int index,const char * name,void * buffer,size_t buffer_size)344 int erofs_getxattr(struct inode *inode, int index,
345 	const char *name,
346 	void *buffer, size_t buffer_size)
347 {
348 	int ret;
349 	struct getxattr_iter it;
350 
351 	if (unlikely(name == NULL))
352 		return -EINVAL;
353 
354 	init_inode_xattrs(inode);
355 
356 	it.index = index;
357 
358 	it.name.len = strlen(name);
359 	if (it.name.len > EROFS_NAME_LEN)
360 		return -ERANGE;
361 	it.name.name = name;
362 
363 	it.buffer = buffer;
364 	it.buffer_size = buffer_size;
365 
366 	it.it.sb = inode->i_sb;
367 	ret = inline_getxattr(inode, &it);
368 	if (ret == -ENOATTR)
369 		ret = shared_getxattr(inode, &it);
370 	return ret;
371 }
372 
erofs_xattr_generic_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)373 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
374 		struct dentry *unused, struct inode *inode,
375 		const char *name, void *buffer, size_t size)
376 {
377 	struct erofs_vnode *const vi = EROFS_V(inode);
378 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
379 
380 	switch (handler->flags) {
381 	case EROFS_XATTR_INDEX_USER:
382 		if (!test_opt(sbi, XATTR_USER))
383 			return -EOPNOTSUPP;
384 		break;
385 	case EROFS_XATTR_INDEX_TRUSTED:
386 		if (!capable(CAP_SYS_ADMIN))
387 			return -EPERM;
388 		break;
389 	case EROFS_XATTR_INDEX_SECURITY:
390 		break;
391 	default:
392 		return -EINVAL;
393 	}
394 
395 	if (!vi->xattr_isize)
396 		return -ENOATTR;
397 
398 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
399 }
400 
401 const struct xattr_handler erofs_xattr_user_handler = {
402 	.prefix	= XATTR_USER_PREFIX,
403 	.flags	= EROFS_XATTR_INDEX_USER,
404 	.list	= erofs_xattr_user_list,
405 	.get	= erofs_xattr_generic_get,
406 };
407 
408 const struct xattr_handler erofs_xattr_trusted_handler = {
409 	.prefix	= XATTR_TRUSTED_PREFIX,
410 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
411 	.list	= erofs_xattr_trusted_list,
412 	.get	= erofs_xattr_generic_get,
413 };
414 
415 #ifdef CONFIG_EROFS_FS_SECURITY
416 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
417 	.prefix	= XATTR_SECURITY_PREFIX,
418 	.flags	= EROFS_XATTR_INDEX_SECURITY,
419 	.get	= erofs_xattr_generic_get,
420 };
421 #endif
422 
423 const struct xattr_handler *erofs_xattr_handlers[] = {
424 	&erofs_xattr_user_handler,
425 #ifdef CONFIG_EROFS_FS_POSIX_ACL
426 	&posix_acl_access_xattr_handler,
427 	&posix_acl_default_xattr_handler,
428 #endif
429 	&erofs_xattr_trusted_handler,
430 #ifdef CONFIG_EROFS_FS_SECURITY
431 	&erofs_xattr_security_handler,
432 #endif
433 	NULL,
434 };
435 
436 struct listxattr_iter {
437 	struct xattr_iter it;
438 
439 	struct dentry *dentry;
440 	char *buffer;
441 	int buffer_size, buffer_ofs;
442 };
443 
xattr_entrylist(struct xattr_iter * _it,struct erofs_xattr_entry * entry)444 static int xattr_entrylist(struct xattr_iter *_it,
445 	struct erofs_xattr_entry *entry)
446 {
447 	struct listxattr_iter *it =
448 		container_of(_it, struct listxattr_iter, it);
449 	unsigned prefix_len;
450 	const char *prefix;
451 
452 	const struct xattr_handler *h =
453 		erofs_xattr_handler(entry->e_name_index);
454 
455 	if (h == NULL || (h->list != NULL && !h->list(it->dentry)))
456 		return 1;
457 
458 	/* Note that at least one of 'prefix' and 'name' should be non-NULL */
459 	prefix = h->prefix != NULL ? h->prefix : h->name;
460 	prefix_len = strlen(prefix);
461 
462 	if (it->buffer == NULL) {
463 		it->buffer_ofs += prefix_len + entry->e_name_len + 1;
464 		return 1;
465 	}
466 
467 	if (it->buffer_ofs + prefix_len
468 		+ entry->e_name_len + 1 > it->buffer_size)
469 		return -ERANGE;
470 
471 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
472 	it->buffer_ofs += prefix_len;
473 	return 0;
474 }
475 
xattr_namelist(struct xattr_iter * _it,unsigned processed,char * buf,unsigned len)476 static int xattr_namelist(struct xattr_iter *_it,
477 	unsigned processed, char *buf, unsigned len)
478 {
479 	struct listxattr_iter *it =
480 		container_of(_it, struct listxattr_iter, it);
481 
482 	memcpy(it->buffer + it->buffer_ofs, buf, len);
483 	it->buffer_ofs += len;
484 	return 0;
485 }
486 
xattr_skipvalue(struct xattr_iter * _it,unsigned value_sz)487 static int xattr_skipvalue(struct xattr_iter *_it,
488 	unsigned value_sz)
489 {
490 	struct listxattr_iter *it =
491 		container_of(_it, struct listxattr_iter, it);
492 
493 	it->buffer[it->buffer_ofs++] = '\0';
494 	return 1;
495 }
496 
497 static struct xattr_iter_handlers list_xattr_handlers = {
498 	.entry = xattr_entrylist,
499 	.name = xattr_namelist,
500 	.alloc_buffer = xattr_skipvalue,
501 	.value = NULL
502 };
503 
inline_listxattr(struct listxattr_iter * it)504 static int inline_listxattr(struct listxattr_iter *it)
505 {
506 	int ret;
507 	unsigned remaining;
508 
509 	ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
510 	if (ret < 0)
511 		return ret;
512 
513 	remaining = ret;
514 	while (remaining) {
515 		ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
516 		if (ret < 0)
517 			break;
518 	}
519 	xattr_iter_end(&it->it, true);
520 	return ret < 0 ? ret : it->buffer_ofs;
521 }
522 
shared_listxattr(struct listxattr_iter * it)523 static int shared_listxattr(struct listxattr_iter *it)
524 {
525 	struct inode *const inode = d_inode(it->dentry);
526 	struct erofs_vnode *const vi = EROFS_V(inode);
527 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
528 	unsigned i;
529 	int ret = 0;
530 
531 	for (i = 0; i < vi->xattr_shared_count; ++i) {
532 		erofs_blk_t blkaddr =
533 			xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
534 
535 		it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
536 		if (!i || blkaddr != it->it.blkaddr) {
537 			if (i)
538 				xattr_iter_end(&it->it, true);
539 
540 			it->it.page = erofs_get_meta_page(inode->i_sb,
541 				blkaddr, false);
542 			BUG_ON(IS_ERR(it->it.page));
543 			it->it.kaddr = kmap_atomic(it->it.page);
544 			it->it.blkaddr = blkaddr;
545 		}
546 
547 		ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
548 		if (ret < 0)
549 			break;
550 	}
551 	if (vi->xattr_shared_count)
552 		xattr_iter_end(&it->it, true);
553 
554 	return ret < 0 ? ret : it->buffer_ofs;
555 }
556 
erofs_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)557 ssize_t erofs_listxattr(struct dentry *dentry,
558 	char *buffer, size_t buffer_size)
559 {
560 	int ret;
561 	struct listxattr_iter it;
562 
563 	init_inode_xattrs(d_inode(dentry));
564 
565 	it.dentry = dentry;
566 	it.buffer = buffer;
567 	it.buffer_size = buffer_size;
568 	it.buffer_ofs = 0;
569 
570 	it.it.sb = dentry->d_sb;
571 
572 	ret = inline_listxattr(&it);
573 	if (ret < 0 && ret != -ENOATTR)
574 		return ret;
575 	return shared_listxattr(&it);
576 }
577 
578