xattr.c 19.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * This file is part of UBIFS.
 *
 * Copyright (C) 2006-2008 Nokia Corporation.
 *
 * Authors: Artem Bityutskiy (Битюцкий Артём)
 *          Adrian Hunter
 */

/*
 * This file implements UBIFS extended attributes support.
 *
 * Extended attributes are implemented as regular inodes with attached data,
 * which limits extended attribute size to UBIFS block size (4KiB). Names of
 * extended attributes are described by extended attribute entries (xentries),
 * which are almost identical to directory entries, but have different key type.
 *
 * In other words, the situation with extended attributes is very similar to
 * directories. Indeed, any inode (but of course not xattr inodes) may have a
 * number of associated xentries, just like directory inodes have associated
 * directory entries. Extended attribute entries store the name of the extended
 * attribute, the host inode number, and the extended attribute inode number.
 * Similarly, direntries store the name, the parent and the target inode
 * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
 * extended attributes.
 *
 * The number of extended attributes is not limited, but there is Linux
 * limitation on the maximum possible size of the list of all extended
 * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
 * the sum of all extended attribute names of the inode does not exceed that
 * limit.
 *
 * Extended attributes are synchronous, which means they are written to the
 * flash media synchronously and there is no write-back for extended attribute
 * inodes. The extended attribute values are not stored in compressed form on
 * the media.
 *
 * Since extended attributes are represented by regular inodes, they are cached
 * in the VFS inode cache. The xentries are cached in the LNC cache (see
 * tnc.c).
 *
 * ACL support is not implemented.
 */

46
#include "ubifs.h"
J
Jens Axboe 已提交
47
#include <linux/fs.h>
48
#include <linux/slab.h>
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <linux/xattr.h>

/*
 * Extended attribute type constants.
 *
 * USER_XATTR: user extended attribute ("user.*")
 * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
 * SECURITY_XATTR: security extended attribute ("security.*")
 */
enum {
	USER_XATTR,
	TRUSTED_XATTR,
	SECURITY_XATTR,
};

64 65
static const struct inode_operations empty_iops;
static const struct file_operations empty_fops;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

/**
 * create_xattr - create an extended attribute.
 * @c: UBIFS file-system description object
 * @host: host inode
 * @nm: extended attribute name
 * @value: extended attribute value
 * @size: size of extended attribute value
 *
 * This is a helper function which creates an extended attribute of name @nm
 * and value @value for inode @host. The host inode is also updated on flash
 * because the ctime and extended attribute accounting data changes. This
 * function returns zero in case of success and a negative error code in case
 * of failure.
 */
static int create_xattr(struct ubifs_info *c, struct inode *host,
82
			const struct fscrypt_name *nm, const void *value, int size)
83
{
84
	int err, names_len;
85 86 87
	struct inode *inode;
	struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
88 89
				.new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
90

91
	if (host_ui->xattr_cnt >= ubifs_xattr_max_cnt(c)) {
92
		ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
93
			  host->i_ino, host_ui->xattr_cnt);
94
		return -ENOSPC;
95
	}
96 97
	/*
	 * Linux limits the maximum size of the extended attribute names list
A
Artem Bityutskiy 已提交
98
	 * to %XATTR_LIST_MAX. This means we should not allow creating more
99 100 101
	 * extended attributes if the name list becomes larger. This limitation
	 * is artificial for UBIFS, though.
	 */
102
	names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
103
	if (names_len > XATTR_LIST_MAX) {
104
		ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
105
			  host->i_ino, names_len, XATTR_LIST_MAX);
106
		return -ENOSPC;
107
	}
108 109 110 111 112

	err = ubifs_budget_space(c, &req);
	if (err)
		return err;

113
	inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO, true);
114 115 116 117 118 119
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
		goto out_budg;
	}

	/* Re-define all operations to be "nothing" */
J
Jens Axboe 已提交
120
	inode->i_mapping->a_ops = &empty_aops;
121 122
	inode->i_op = &empty_iops;
	inode->i_fop = &empty_fops;
123

124
	inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME;
125 126 127
	ui = ubifs_inode(inode);
	ui->xattr = 1;
	ui->flags |= UBIFS_XATTR_FL;
128
	ui->data = kmemdup(value, size, GFP_NOFS);
129 130
	if (!ui->data) {
		err = -ENOMEM;
A
Artem Bityutskiy 已提交
131
		goto out_free;
132
	}
A
Artem Bityutskiy 已提交
133 134 135 136
	inode->i_size = ui->ui_size = size;
	ui->data_len = size;

	mutex_lock(&host_ui->ui_mutex);
137
	host->i_ctime = current_time(host);
138
	host_ui->xattr_cnt += 1;
139
	host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
140
	host_ui->xattr_size += CALC_XATTR_BYTES(size);
141
	host_ui->xattr_names += fname_len(nm);
142

143 144 145 146 147 148
	/*
	 * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
	 * have to set the UBIFS_CRYPT_FL flag on the host inode.
	 * To avoid multiple updates of the same inode in the same operation,
	 * let's do it here.
	 */
149
	if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
150 151
		host_ui->flags |= UBIFS_CRYPT_FL;

152 153 154
	err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
	if (err)
		goto out_cancel;
155
	ubifs_set_inode_flags(host);
156 157 158 159 160 161 162 163 164
	mutex_unlock(&host_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	insert_inode_hash(inode);
	iput(inode);
	return 0;

out_cancel:
	host_ui->xattr_cnt -= 1;
165
	host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
166
	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
167
	host_ui->xattr_names -= fname_len(nm);
168
	host_ui->flags &= ~UBIFS_CRYPT_FL;
169
	mutex_unlock(&host_ui->ui_mutex);
A
Artem Bityutskiy 已提交
170
out_free:
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	make_bad_inode(inode);
	iput(inode);
out_budg:
	ubifs_release_budget(c, &req);
	return err;
}

/**
 * change_xattr - change an extended attribute.
 * @c: UBIFS file-system description object
 * @host: host inode
 * @inode: extended attribute inode
 * @value: extended attribute value
 * @size: size of extended attribute value
 *
 * This helper function changes the value of extended attribute @inode with new
 * data from @value. Returns zero in case of success and a negative error code
 * in case of failure.
 */
static int change_xattr(struct ubifs_info *c, struct inode *host,
			struct inode *inode, const void *value, int size)
{
	int err;
	struct ubifs_inode *host_ui = ubifs_inode(host);
	struct ubifs_inode *ui = ubifs_inode(inode);
196
	void *buf = NULL;
197
	int old_size;
198
	struct ubifs_budget_req req = { .dirtied_ino = 2,
199
		.dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
200

201
	ubifs_assert(c, ui->data_len == inode->i_size);
202 203 204 205
	err = ubifs_budget_space(c, &req);
	if (err)
		return err;

206 207
	buf = kmemdup(value, size, GFP_NOFS);
	if (!buf) {
208
		err = -ENOMEM;
A
Artem Bityutskiy 已提交
209
		goto out_free;
210
	}
211 212
	kfree(ui->data);
	ui->data = buf;
213
	inode->i_size = ui->ui_size = size;
214
	old_size = ui->data_len;
215 216
	ui->data_len = size;

A
Artem Bityutskiy 已提交
217
	mutex_lock(&host_ui->ui_mutex);
218
	host->i_ctime = current_time(host);
219
	host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
A
Artem Bityutskiy 已提交
220 221
	host_ui->xattr_size += CALC_XATTR_BYTES(size);

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	/*
	 * It is important to write the host inode after the xattr inode
	 * because if the host inode gets synchronized (via 'fsync()'), then
	 * the extended attribute inode gets synchronized, because it goes
	 * before the host inode in the write-buffer.
	 */
	err = ubifs_jnl_change_xattr(c, inode, host);
	if (err)
		goto out_cancel;
	mutex_unlock(&host_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	return 0;

out_cancel:
	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
238
	host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
239
	mutex_unlock(&host_ui->ui_mutex);
A
Artem Bityutskiy 已提交
240 241
	make_bad_inode(inode);
out_free:
242 243 244 245 246 247 248 249 250 251
	ubifs_release_budget(c, &req);
	return err;
}

static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
{
	struct inode *inode;

	inode = ubifs_iget(c->vfs_sb, inum);
	if (IS_ERR(inode)) {
252
		ubifs_err(c, "dead extended attribute entry, error %d",
253 254 255 256 257
			  (int)PTR_ERR(inode));
		return inode;
	}
	if (ubifs_inode(inode)->xattr)
		return inode;
258
	ubifs_err(c, "corrupt extended attribute entry");
259 260 261 262
	iput(inode);
	return ERR_PTR(-EINVAL);
}

263
int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
264
		    size_t size, int flags, bool check_lock)
265
{
266
	struct inode *inode;
267
	struct ubifs_info *c = host->i_sb->s_fs_info;
268
	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
269 270
	struct ubifs_dent_node *xent;
	union ubifs_key key;
271
	int err;
272

273
	if (check_lock)
274
		ubifs_assert(c, inode_is_locked(host));
275 276 277 278

	if (size > UBIFS_MAX_INO_DATA)
		return -ERANGE;

279
	if (fname_len(&nm) > UBIFS_MAX_NLEN)
280
		return -ENAMETOOLONG;
281 282 283 284 285

	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
	if (!xent)
		return -ENOMEM;

286
	down_write(&ubifs_inode(host)->xattr_sem);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	/*
	 * The extended attribute entries are stored in LNC, so multiple
	 * look-ups do not involve reading the flash.
	 */
	xent_key_init(c, &key, host->i_ino, &nm);
	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
	if (err) {
		if (err != -ENOENT)
			goto out_free;

		if (flags & XATTR_REPLACE)
			/* We are asked not to create the xattr */
			err = -ENODATA;
		else
			err = create_xattr(c, host, &nm, value, size);
		goto out_free;
	}

	if (flags & XATTR_CREATE) {
		/* We are asked not to replace the xattr */
		err = -EEXIST;
		goto out_free;
	}

	inode = iget_xattr(c, le64_to_cpu(xent->inum));
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
		goto out_free;
	}

	err = change_xattr(c, host, inode, value, size);
	iput(inode);

out_free:
321
	up_write(&ubifs_inode(host)->xattr_sem);
322 323 324 325
	kfree(xent);
	return err;
}

326 327
ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
			size_t size)
328
{
329
	struct inode *inode;
330
	struct ubifs_info *c = host->i_sb->s_fs_info;
331
	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
332 333 334 335 336
	struct ubifs_inode *ui;
	struct ubifs_dent_node *xent;
	union ubifs_key key;
	int err;

337
	if (fname_len(&nm) > UBIFS_MAX_NLEN)
338
		return -ENAMETOOLONG;
339 340 341 342 343

	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
	if (!xent)
		return -ENOMEM;

344
	down_read(&ubifs_inode(host)->xattr_sem);
345 346 347 348 349
	xent_key_init(c, &key, host->i_ino, &nm);
	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
	if (err) {
		if (err == -ENOENT)
			err = -ENODATA;
350
		goto out_cleanup;
351 352 353 354 355
	}

	inode = iget_xattr(c, le64_to_cpu(xent->inum));
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
356
		goto out_cleanup;
357 358 359
	}

	ui = ubifs_inode(inode);
360 361
	ubifs_assert(c, inode->i_size == ui->data_len);
	ubifs_assert(c, ubifs_inode(host)->xattr_size > ui->data_len);
362 363 364 365 366 367 368 369 370 371 372 373 374 375

	if (buf) {
		/* If @buf is %NULL we are supposed to return the length */
		if (ui->data_len > size) {
			err = -ERANGE;
			goto out_iput;
		}

		memcpy(buf, ui->data, ui->data_len);
	}
	err = ui->data_len;

out_iput:
	iput(inode);
376 377
out_cleanup:
	up_read(&ubifs_inode(host)->xattr_sem);
378 379 380 381
	kfree(xent);
	return err;
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395
static bool xattr_visible(const char *name)
{
	/* File encryption related xattrs are for internal use only */
	if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
		return false;

	/* Show trusted namespace only for "power" users */
	if (strncmp(name, XATTR_TRUSTED_PREFIX,
		    XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
		return false;

	return true;
}

396 397 398
ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
{
	union ubifs_key key;
399
	struct inode *host = d_inode(dentry);
400 401 402 403
	struct ubifs_info *c = host->i_sb->s_fs_info;
	struct ubifs_inode *host_ui = ubifs_inode(host);
	struct ubifs_dent_node *xent, *pxent = NULL;
	int err, len, written = 0;
404
	struct fscrypt_name nm = {0};
405

A
Al Viro 已提交
406 407
	dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
		dentry, size);
408

409
	down_read(&host_ui->xattr_sem);
410
	len = host_ui->xattr_names + host_ui->xattr_cnt;
411
	if (!buffer) {
412 413 414 415
		/*
		 * We should return the minimum buffer size which will fit a
		 * null-terminated list of all the extended attribute names.
		 */
416 417 418
		err = len;
		goto out_err;
	}
419

420 421 422 423
	if (len > size) {
		err = -ERANGE;
		goto out_err;
	}
424 425 426 427

	lowest_xent_key(c, &key, host->i_ino);
	while (1) {
		xent = ubifs_tnc_next_ent(c, &key, &nm);
428
		if (IS_ERR(xent)) {
429 430 431 432
			err = PTR_ERR(xent);
			break;
		}

433 434
		fname_name(&nm) = xent->name;
		fname_len(&nm) = le16_to_cpu(xent->nlen);
435

436
		if (xattr_visible(xent->name)) {
437 438
			memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
			written += fname_len(&nm) + 1;
439 440 441 442 443 444 445
		}

		kfree(pxent);
		pxent = xent;
		key_read(c, &xent->key, &key);
	}
	kfree(pxent);
446 447
	up_read(&host_ui->xattr_sem);

448
	if (err != -ENOENT) {
449
		ubifs_err(c, "cannot find next direntry, error %d", err);
450 451 452
		return err;
	}

453
	ubifs_assert(c, written <= size);
454
	return written;
455 456 457 458

out_err:
	up_read(&host_ui->xattr_sem);
	return err;
459 460 461
}

static int remove_xattr(struct ubifs_info *c, struct inode *host,
462
			struct inode *inode, const struct fscrypt_name *nm)
463 464 465 466
{
	int err;
	struct ubifs_inode *host_ui = ubifs_inode(host);
	struct ubifs_inode *ui = ubifs_inode(inode);
467 468
	struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
469

470
	ubifs_assert(c, ui->data_len == inode->i_size);
471 472 473 474 475 476

	err = ubifs_budget_space(c, &req);
	if (err)
		return err;

	mutex_lock(&host_ui->ui_mutex);
477
	host->i_ctime = current_time(host);
478
	host_ui->xattr_cnt -= 1;
479
	host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
480
	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
481
	host_ui->xattr_names -= fname_len(nm);
482 483 484 485 486 487 488 489 490 491 492

	err = ubifs_jnl_delete_xattr(c, host, inode, nm);
	if (err)
		goto out_cancel;
	mutex_unlock(&host_ui->ui_mutex);

	ubifs_release_budget(c, &req);
	return 0;

out_cancel:
	host_ui->xattr_cnt += 1;
493
	host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
494
	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
495
	host_ui->xattr_names += fname_len(nm);
496 497 498 499 500 501
	mutex_unlock(&host_ui->ui_mutex);
	ubifs_release_budget(c, &req);
	make_bad_inode(inode);
	return err;
}

502 503 504 505 506 507 508 509 510
int ubifs_purge_xattrs(struct inode *host)
{
	union ubifs_key key;
	struct ubifs_info *c = host->i_sb->s_fs_info;
	struct ubifs_dent_node *xent, *pxent = NULL;
	struct inode *xino;
	struct fscrypt_name nm = {0};
	int err;

S
Sascha Hauer 已提交
511
	if (ubifs_inode(host)->xattr_cnt <= ubifs_xattr_max_cnt(c))
512 513 514 515 516
		return 0;

	ubifs_warn(c, "inode %lu has too many xattrs, doing a non-atomic deletion",
		   host->i_ino);

517
	down_write(&ubifs_inode(host)->xattr_sem);
518 519 520 521 522 523 524 525 526 527 528
	lowest_xent_key(c, &key, host->i_ino);
	while (1) {
		xent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(xent)) {
			err = PTR_ERR(xent);
			break;
		}

		fname_name(&nm) = xent->name;
		fname_len(&nm) = le16_to_cpu(xent->nlen);

529
		xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
530 531 532 533 534 535
		if (IS_ERR(xino)) {
			err = PTR_ERR(xino);
			ubifs_err(c, "dead directory entry '%s', error %d",
				  xent->name, err);
			ubifs_ro_mode(c, err);
			kfree(pxent);
536
			kfree(xent);
537
			goto out_err;
538 539 540 541 542 543 544 545
		}

		ubifs_assert(c, ubifs_inode(xino)->xattr);

		clear_nlink(xino);
		err = remove_xattr(c, host, xino, &nm);
		if (err) {
			kfree(pxent);
546
			kfree(xent);
547 548
			iput(xino);
			ubifs_err(c, "cannot remove xattr, error %d", err);
549
			goto out_err;
550 551 552 553 554 555 556 557 558
		}

		iput(xino);

		kfree(pxent);
		pxent = xent;
		key_read(c, &xent->key, &key);
	}
	kfree(pxent);
559 560
	up_write(&ubifs_inode(host)->xattr_sem);

561 562 563 564 565 566
	if (err != -ENOENT) {
		ubifs_err(c, "cannot find next direntry, error %d", err);
		return err;
	}

	return 0;
567 568 569 570

out_err:
	up_write(&ubifs_inode(host)->xattr_sem);
	return err;
571 572
}

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
/**
 * ubifs_evict_xattr_inode - Evict an xattr inode.
 * @c: UBIFS file-system description object
 * @xattr_inum: xattr inode number
 *
 * When an inode that hosts xattrs is being removed we have to make sure
 * that cached inodes of the xattrs also get removed from the inode cache
 * otherwise we'd waste memory. This function looks up an inode from the
 * inode cache and clears the link counter such that iput() will evict
 * the inode.
 */
void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum)
{
	struct inode *inode;

	inode = ilookup(c->vfs_sb, xattr_inum);
	if (inode) {
		clear_nlink(inode);
		iput(inode);
	}
}

595
static int ubifs_xattr_remove(struct inode *host, const char *name)
596
{
597
	struct inode *inode;
598
	struct ubifs_info *c = host->i_sb->s_fs_info;
599
	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
600 601 602 603
	struct ubifs_dent_node *xent;
	union ubifs_key key;
	int err;

604
	ubifs_assert(c, inode_is_locked(host));
605

606
	if (fname_len(&nm) > UBIFS_MAX_NLEN)
607
		return -ENAMETOOLONG;
608 609 610 611 612

	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
	if (!xent)
		return -ENOMEM;

613
	down_write(&ubifs_inode(host)->xattr_sem);
614 615 616 617 618 619 620 621 622 623 624 625 626 627
	xent_key_init(c, &key, host->i_ino, &nm);
	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
	if (err) {
		if (err == -ENOENT)
			err = -ENODATA;
		goto out_free;
	}

	inode = iget_xattr(c, le64_to_cpu(xent->inum));
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
		goto out_free;
	}

628
	ubifs_assert(c, inode->i_nlink == 1);
629
	clear_nlink(inode);
630 631
	err = remove_xattr(c, host, inode, &nm);
	if (err)
M
Miklos Szeredi 已提交
632
		set_nlink(inode, 1);
633 634 635 636 637

	/* If @i_nlink is 0, 'iput()' will delete the inode */
	iput(inode);

out_free:
638
	up_write(&ubifs_inode(host)->xattr_sem);
639 640 641
	kfree(xent);
	return err;
}
642

643
#ifdef CONFIG_UBIFS_FS_SECURITY
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
		      void *fs_info)
{
	const struct xattr *xattr;
	char *name;
	int err = 0;

	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
			       strlen(xattr->name) + 1, GFP_NOFS);
		if (!name) {
			err = -ENOMEM;
			break;
		}
		strcpy(name, XATTR_SECURITY_PREFIX);
		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
660 661 662 663
		/*
		 * creating a new inode without holding the inode rwsem,
		 * no need to check whether inode is locked.
		 */
664
		err = ubifs_xattr_set(inode, name, xattr->value,
665
				      xattr->value_len, 0, false);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
		kfree(name);
		if (err < 0)
			break;
	}

	return err;
}

int ubifs_init_security(struct inode *dentry, struct inode *inode,
			const struct qstr *qstr)
{
	int err;

	err = security_inode_init_security(inode, dentry, qstr,
					   &init_xattrs, 0);
681 682 683
	if (err) {
		struct ubifs_info *c = dentry->i_sb->s_fs_info;
		ubifs_err(c, "cannot initialize security for inode %lu, error %d",
684
			  inode->i_ino, err);
685
	}
686 687
	return err;
}
688
#endif
689

690
static int xattr_get(const struct xattr_handler *handler,
691 692 693 694 695 696
			   struct dentry *dentry, struct inode *inode,
			   const char *name, void *buffer, size_t size)
{
	dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
		inode->i_ino, dentry, size);

697
	name = xattr_full_name(handler, name);
698
	return ubifs_xattr_get(inode, name, buffer, size);
699 700
}

701
static int xattr_set(const struct xattr_handler *handler,
702 703 704
			   struct dentry *dentry, struct inode *inode,
			   const char *name, const void *value,
			   size_t size, int flags)
705 706 707 708
{
	dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
		name, inode->i_ino, dentry, size);

709 710
	name = xattr_full_name(handler, name);

711
	if (value)
712
		return ubifs_xattr_set(inode, name, value, size, flags, true);
713
	else
714
		return ubifs_xattr_remove(inode, name);
715 716
}

B
Ben Dooks 已提交
717
static const struct xattr_handler ubifs_user_xattr_handler = {
718
	.prefix = XATTR_USER_PREFIX,
719 720
	.get = xattr_get,
	.set = xattr_set,
721 722
};

B
Ben Dooks 已提交
723
static const struct xattr_handler ubifs_trusted_xattr_handler = {
724
	.prefix = XATTR_TRUSTED_PREFIX,
725 726
	.get = xattr_get,
	.set = xattr_set,
727 728
};

729
#ifdef CONFIG_UBIFS_FS_SECURITY
B
Ben Dooks 已提交
730
static const struct xattr_handler ubifs_security_xattr_handler = {
731
	.prefix = XATTR_SECURITY_PREFIX,
732 733
	.get = xattr_get,
	.set = xattr_set,
734
};
735
#endif
736 737 738 739

const struct xattr_handler *ubifs_xattr_handlers[] = {
	&ubifs_user_xattr_handler,
	&ubifs_trusted_xattr_handler,
740
#ifdef CONFIG_UBIFS_FS_SECURITY
741
	&ubifs_security_xattr_handler,
742
#endif
743 744
	NULL
};