xattr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. */
  10. /*
  11. * This file implements UBIFS extended attributes support.
  12. *
  13. * Extended attributes are implemented as regular inodes with attached data,
  14. * which limits extended attribute size to UBIFS block size (4KiB). Names of
  15. * extended attributes are described by extended attribute entries (xentries),
  16. * which are almost identical to directory entries, but have different key type.
  17. *
  18. * In other words, the situation with extended attributes is very similar to
  19. * directories. Indeed, any inode (but of course not xattr inodes) may have a
  20. * number of associated xentries, just like directory inodes have associated
  21. * directory entries. Extended attribute entries store the name of the extended
  22. * attribute, the host inode number, and the extended attribute inode number.
  23. * Similarly, direntries store the name, the parent and the target inode
  24. * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
  25. * extended attributes.
  26. *
  27. * The number of extended attributes is not limited, but there is Linux
  28. * limitation on the maximum possible size of the list of all extended
  29. * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
  30. * the sum of all extended attribute names of the inode does not exceed that
  31. * limit.
  32. *
  33. * Extended attributes are synchronous, which means they are written to the
  34. * flash media synchronously and there is no write-back for extended attribute
  35. * inodes. The extended attribute values are not stored in compressed form on
  36. * the media.
  37. *
  38. * Since extended attributes are represented by regular inodes, they are cached
  39. * in the VFS inode cache. The xentries are cached in the LNC cache (see
  40. * tnc.c).
  41. *
  42. * ACL support is not implemented.
  43. */
  44. #include "ubifs.h"
  45. #include <linux/fs.h>
  46. #include <linux/slab.h>
  47. #include <linux/xattr.h>
  48. static const struct inode_operations empty_iops;
  49. static const struct file_operations empty_fops;
  50. /**
  51. * create_xattr - create an extended attribute.
  52. * @c: UBIFS file-system description object
  53. * @host: host inode
  54. * @nm: extended attribute name
  55. * @value: extended attribute value
  56. * @size: size of extended attribute value
  57. *
  58. * This is a helper function which creates an extended attribute of name @nm
  59. * and value @value for inode @host. The host inode is also updated on flash
  60. * because the ctime and extended attribute accounting data changes. This
  61. * function returns zero in case of success and a negative error code in case
  62. * of failure.
  63. */
  64. static int create_xattr(struct ubifs_info *c, struct inode *host,
  65. const struct fscrypt_name *nm, const void *value, int size)
  66. {
  67. int err, names_len;
  68. struct inode *inode;
  69. struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
  70. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  71. .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
  72. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  73. if (host_ui->xattr_cnt >= ubifs_xattr_max_cnt(c)) {
  74. ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
  75. host->i_ino, host_ui->xattr_cnt);
  76. return -ENOSPC;
  77. }
  78. /*
  79. * Linux limits the maximum size of the extended attribute names list
  80. * to %XATTR_LIST_MAX. This means we should not allow creating more
  81. * extended attributes if the name list becomes larger. This limitation
  82. * is artificial for UBIFS, though.
  83. */
  84. names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
  85. if (names_len > XATTR_LIST_MAX) {
  86. ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
  87. host->i_ino, names_len, XATTR_LIST_MAX);
  88. return -ENOSPC;
  89. }
  90. err = ubifs_budget_space(c, &req);
  91. if (err)
  92. return err;
  93. inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO, true);
  94. if (IS_ERR(inode)) {
  95. err = PTR_ERR(inode);
  96. goto out_budg;
  97. }
  98. /* Re-define all operations to be "nothing" */
  99. inode->i_mapping->a_ops = &empty_aops;
  100. inode->i_op = &empty_iops;
  101. inode->i_fop = &empty_fops;
  102. inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME;
  103. ui = ubifs_inode(inode);
  104. ui->xattr = 1;
  105. ui->flags |= UBIFS_XATTR_FL;
  106. ui->data = kmemdup(value, size, GFP_NOFS);
  107. if (!ui->data) {
  108. err = -ENOMEM;
  109. goto out_free;
  110. }
  111. inode->i_size = ui->ui_size = size;
  112. ui->data_len = size;
  113. mutex_lock(&host_ui->ui_mutex);
  114. inode_set_ctime_current(host);
  115. host_ui->xattr_cnt += 1;
  116. host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
  117. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  118. host_ui->xattr_names += fname_len(nm);
  119. /*
  120. * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
  121. * have to set the UBIFS_CRYPT_FL flag on the host inode.
  122. * To avoid multiple updates of the same inode in the same operation,
  123. * let's do it here.
  124. */
  125. if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
  126. host_ui->flags |= UBIFS_CRYPT_FL;
  127. err = ubifs_jnl_update(c, host, nm, inode, 0, 1, 0);
  128. if (err)
  129. goto out_cancel;
  130. ubifs_set_inode_flags(host);
  131. mutex_unlock(&host_ui->ui_mutex);
  132. ubifs_release_budget(c, &req);
  133. insert_inode_hash(inode);
  134. iput(inode);
  135. return 0;
  136. out_cancel:
  137. host_ui->xattr_cnt -= 1;
  138. host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
  139. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  140. host_ui->xattr_names -= fname_len(nm);
  141. host_ui->flags &= ~UBIFS_CRYPT_FL;
  142. mutex_unlock(&host_ui->ui_mutex);
  143. out_free:
  144. make_bad_inode(inode);
  145. iput(inode);
  146. out_budg:
  147. ubifs_release_budget(c, &req);
  148. return err;
  149. }
  150. /**
  151. * change_xattr - change an extended attribute.
  152. * @c: UBIFS file-system description object
  153. * @host: host inode
  154. * @inode: extended attribute inode
  155. * @value: extended attribute value
  156. * @size: size of extended attribute value
  157. *
  158. * This helper function changes the value of extended attribute @inode with new
  159. * data from @value. Returns zero in case of success and a negative error code
  160. * in case of failure.
  161. */
  162. static int change_xattr(struct ubifs_info *c, struct inode *host,
  163. struct inode *inode, const void *value, int size)
  164. {
  165. int err;
  166. struct ubifs_inode *host_ui = ubifs_inode(host);
  167. struct ubifs_inode *ui = ubifs_inode(inode);
  168. void *buf = NULL;
  169. int old_size;
  170. struct ubifs_budget_req req = { .dirtied_ino = 2,
  171. .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
  172. ubifs_assert(c, ui->data_len == inode->i_size);
  173. err = ubifs_budget_space(c, &req);
  174. if (err)
  175. return err;
  176. buf = kmemdup(value, size, GFP_NOFS);
  177. if (!buf) {
  178. err = -ENOMEM;
  179. goto out_free;
  180. }
  181. kfree(ui->data);
  182. ui->data = buf;
  183. inode->i_size = ui->ui_size = size;
  184. old_size = ui->data_len;
  185. ui->data_len = size;
  186. mutex_lock(&host_ui->ui_mutex);
  187. inode_set_ctime_current(host);
  188. host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
  189. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  190. /*
  191. * It is important to write the host inode after the xattr inode
  192. * because if the host inode gets synchronized (via 'fsync()'), then
  193. * the extended attribute inode gets synchronized, because it goes
  194. * before the host inode in the write-buffer.
  195. */
  196. err = ubifs_jnl_change_xattr(c, inode, host);
  197. if (err)
  198. goto out_cancel;
  199. mutex_unlock(&host_ui->ui_mutex);
  200. ubifs_release_budget(c, &req);
  201. return 0;
  202. out_cancel:
  203. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  204. host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
  205. mutex_unlock(&host_ui->ui_mutex);
  206. make_bad_inode(inode);
  207. out_free:
  208. ubifs_release_budget(c, &req);
  209. return err;
  210. }
  211. static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
  212. {
  213. struct inode *inode;
  214. inode = ubifs_iget(c->vfs_sb, inum);
  215. if (IS_ERR(inode)) {
  216. ubifs_err(c, "dead extended attribute entry, error %d",
  217. (int)PTR_ERR(inode));
  218. return inode;
  219. }
  220. if (ubifs_inode(inode)->xattr)
  221. return inode;
  222. ubifs_err(c, "corrupt extended attribute entry");
  223. iput(inode);
  224. return ERR_PTR(-EINVAL);
  225. }
  226. int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
  227. size_t size, int flags, bool check_lock)
  228. {
  229. struct inode *inode;
  230. struct ubifs_info *c = host->i_sb->s_fs_info;
  231. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  232. struct ubifs_dent_node *xent;
  233. union ubifs_key key;
  234. int err;
  235. if (check_lock)
  236. ubifs_assert(c, inode_is_locked(host));
  237. if (size > UBIFS_MAX_INO_DATA)
  238. return -ERANGE;
  239. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  240. return -ENAMETOOLONG;
  241. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  242. if (!xent)
  243. return -ENOMEM;
  244. down_write(&ubifs_inode(host)->xattr_sem);
  245. /*
  246. * The extended attribute entries are stored in LNC, so multiple
  247. * look-ups do not involve reading the flash.
  248. */
  249. xent_key_init(c, &key, host->i_ino, &nm);
  250. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  251. if (err) {
  252. if (err != -ENOENT)
  253. goto out_free;
  254. if (flags & XATTR_REPLACE)
  255. /* We are asked not to create the xattr */
  256. err = -ENODATA;
  257. else
  258. err = create_xattr(c, host, &nm, value, size);
  259. goto out_free;
  260. }
  261. if (flags & XATTR_CREATE) {
  262. /* We are asked not to replace the xattr */
  263. err = -EEXIST;
  264. goto out_free;
  265. }
  266. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  267. if (IS_ERR(inode)) {
  268. err = PTR_ERR(inode);
  269. goto out_free;
  270. }
  271. err = change_xattr(c, host, inode, value, size);
  272. iput(inode);
  273. out_free:
  274. up_write(&ubifs_inode(host)->xattr_sem);
  275. kfree(xent);
  276. return err;
  277. }
  278. ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
  279. size_t size)
  280. {
  281. struct inode *inode;
  282. struct ubifs_info *c = host->i_sb->s_fs_info;
  283. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  284. struct ubifs_inode *ui;
  285. struct ubifs_dent_node *xent;
  286. union ubifs_key key;
  287. int err;
  288. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  289. return -ENAMETOOLONG;
  290. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  291. if (!xent)
  292. return -ENOMEM;
  293. down_read(&ubifs_inode(host)->xattr_sem);
  294. xent_key_init(c, &key, host->i_ino, &nm);
  295. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  296. if (err) {
  297. if (err == -ENOENT)
  298. err = -ENODATA;
  299. goto out_cleanup;
  300. }
  301. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  302. if (IS_ERR(inode)) {
  303. err = PTR_ERR(inode);
  304. goto out_cleanup;
  305. }
  306. ui = ubifs_inode(inode);
  307. ubifs_assert(c, inode->i_size == ui->data_len);
  308. ubifs_assert(c, ubifs_inode(host)->xattr_size > ui->data_len);
  309. if (buf) {
  310. /* If @buf is %NULL we are supposed to return the length */
  311. if (ui->data_len > size) {
  312. err = -ERANGE;
  313. goto out_iput;
  314. }
  315. memcpy(buf, ui->data, ui->data_len);
  316. }
  317. err = ui->data_len;
  318. out_iput:
  319. iput(inode);
  320. out_cleanup:
  321. up_read(&ubifs_inode(host)->xattr_sem);
  322. kfree(xent);
  323. return err;
  324. }
  325. static bool xattr_visible(const char *name)
  326. {
  327. /* File encryption related xattrs are for internal use only */
  328. if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
  329. return false;
  330. /* Show trusted namespace only for "power" users */
  331. if (strncmp(name, XATTR_TRUSTED_PREFIX,
  332. XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
  333. return false;
  334. return true;
  335. }
  336. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  337. {
  338. union ubifs_key key;
  339. struct inode *host = d_inode(dentry);
  340. struct ubifs_info *c = host->i_sb->s_fs_info;
  341. struct ubifs_inode *host_ui = ubifs_inode(host);
  342. struct ubifs_dent_node *xent, *pxent = NULL;
  343. int err, len, written = 0;
  344. struct fscrypt_name nm = {0};
  345. dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
  346. dentry, size);
  347. down_read(&host_ui->xattr_sem);
  348. len = host_ui->xattr_names + host_ui->xattr_cnt;
  349. if (!buffer) {
  350. /*
  351. * We should return the minimum buffer size which will fit a
  352. * null-terminated list of all the extended attribute names.
  353. */
  354. err = len;
  355. goto out_err;
  356. }
  357. if (len > size) {
  358. err = -ERANGE;
  359. goto out_err;
  360. }
  361. lowest_xent_key(c, &key, host->i_ino);
  362. while (1) {
  363. xent = ubifs_tnc_next_ent(c, &key, &nm);
  364. if (IS_ERR(xent)) {
  365. err = PTR_ERR(xent);
  366. break;
  367. }
  368. fname_name(&nm) = xent->name;
  369. fname_len(&nm) = le16_to_cpu(xent->nlen);
  370. if (xattr_visible(xent->name)) {
  371. memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
  372. written += fname_len(&nm) + 1;
  373. }
  374. kfree(pxent);
  375. pxent = xent;
  376. key_read(c, &xent->key, &key);
  377. }
  378. kfree(pxent);
  379. up_read(&host_ui->xattr_sem);
  380. if (err != -ENOENT) {
  381. ubifs_err(c, "cannot find next direntry, error %d", err);
  382. return err;
  383. }
  384. ubifs_assert(c, written <= size);
  385. return written;
  386. out_err:
  387. up_read(&host_ui->xattr_sem);
  388. return err;
  389. }
  390. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  391. struct inode *inode, const struct fscrypt_name *nm)
  392. {
  393. int err;
  394. struct ubifs_inode *host_ui = ubifs_inode(host);
  395. struct ubifs_inode *ui = ubifs_inode(inode);
  396. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  397. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  398. ubifs_assert(c, ui->data_len == inode->i_size);
  399. err = ubifs_budget_space(c, &req);
  400. if (err)
  401. return err;
  402. mutex_lock(&host_ui->ui_mutex);
  403. inode_set_ctime_current(host);
  404. host_ui->xattr_cnt -= 1;
  405. host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
  406. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  407. host_ui->xattr_names -= fname_len(nm);
  408. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  409. if (err)
  410. goto out_cancel;
  411. mutex_unlock(&host_ui->ui_mutex);
  412. ubifs_release_budget(c, &req);
  413. return 0;
  414. out_cancel:
  415. host_ui->xattr_cnt += 1;
  416. host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
  417. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  418. host_ui->xattr_names += fname_len(nm);
  419. mutex_unlock(&host_ui->ui_mutex);
  420. ubifs_release_budget(c, &req);
  421. make_bad_inode(inode);
  422. return err;
  423. }
  424. int ubifs_purge_xattrs(struct inode *host)
  425. {
  426. union ubifs_key key;
  427. struct ubifs_info *c = host->i_sb->s_fs_info;
  428. struct ubifs_dent_node *xent, *pxent = NULL;
  429. struct inode *xino;
  430. struct fscrypt_name nm = {0};
  431. int err;
  432. if (ubifs_inode(host)->xattr_cnt <= ubifs_xattr_max_cnt(c))
  433. return 0;
  434. ubifs_warn(c, "inode %lu has too many xattrs, doing a non-atomic deletion",
  435. host->i_ino);
  436. down_write(&ubifs_inode(host)->xattr_sem);
  437. lowest_xent_key(c, &key, host->i_ino);
  438. while (1) {
  439. xent = ubifs_tnc_next_ent(c, &key, &nm);
  440. if (IS_ERR(xent)) {
  441. err = PTR_ERR(xent);
  442. break;
  443. }
  444. fname_name(&nm) = xent->name;
  445. fname_len(&nm) = le16_to_cpu(xent->nlen);
  446. xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
  447. if (IS_ERR(xino)) {
  448. err = PTR_ERR(xino);
  449. ubifs_err(c, "dead directory entry '%s', error %d",
  450. xent->name, err);
  451. ubifs_ro_mode(c, err);
  452. goto out_err;
  453. }
  454. ubifs_assert(c, ubifs_inode(xino)->xattr);
  455. clear_nlink(xino);
  456. err = remove_xattr(c, host, xino, &nm);
  457. iput(xino);
  458. if (err) {
  459. ubifs_err(c, "cannot remove xattr, error %d", err);
  460. goto out_err;
  461. }
  462. kfree(pxent);
  463. pxent = xent;
  464. key_read(c, &xent->key, &key);
  465. }
  466. kfree(pxent);
  467. up_write(&ubifs_inode(host)->xattr_sem);
  468. if (err != -ENOENT) {
  469. ubifs_err(c, "cannot find next direntry, error %d", err);
  470. return err;
  471. }
  472. return 0;
  473. out_err:
  474. kfree(pxent);
  475. kfree(xent);
  476. up_write(&ubifs_inode(host)->xattr_sem);
  477. return err;
  478. }
  479. static int ubifs_xattr_remove(struct inode *host, const char *name)
  480. {
  481. struct inode *inode;
  482. struct ubifs_info *c = host->i_sb->s_fs_info;
  483. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  484. struct ubifs_dent_node *xent;
  485. union ubifs_key key;
  486. int err;
  487. ubifs_assert(c, inode_is_locked(host));
  488. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  489. return -ENAMETOOLONG;
  490. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  491. if (!xent)
  492. return -ENOMEM;
  493. down_write(&ubifs_inode(host)->xattr_sem);
  494. xent_key_init(c, &key, host->i_ino, &nm);
  495. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  496. if (err) {
  497. if (err == -ENOENT)
  498. err = -ENODATA;
  499. goto out_free;
  500. }
  501. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  502. if (IS_ERR(inode)) {
  503. err = PTR_ERR(inode);
  504. goto out_free;
  505. }
  506. ubifs_assert(c, inode->i_nlink == 1);
  507. clear_nlink(inode);
  508. err = remove_xattr(c, host, inode, &nm);
  509. if (err)
  510. set_nlink(inode, 1);
  511. /* If @i_nlink is 0, 'iput()' will delete the inode */
  512. iput(inode);
  513. out_free:
  514. up_write(&ubifs_inode(host)->xattr_sem);
  515. kfree(xent);
  516. return err;
  517. }
  518. #ifdef CONFIG_UBIFS_FS_SECURITY
  519. static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
  520. void *fs_info)
  521. {
  522. const struct xattr *xattr;
  523. char *name;
  524. int err = 0;
  525. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  526. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  527. strlen(xattr->name) + 1, GFP_NOFS);
  528. if (!name) {
  529. err = -ENOMEM;
  530. break;
  531. }
  532. strcpy(name, XATTR_SECURITY_PREFIX);
  533. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  534. /*
  535. * creating a new inode without holding the inode rwsem,
  536. * no need to check whether inode is locked.
  537. */
  538. err = ubifs_xattr_set(inode, name, xattr->value,
  539. xattr->value_len, 0, false);
  540. kfree(name);
  541. if (err < 0)
  542. break;
  543. }
  544. return err;
  545. }
  546. int ubifs_init_security(struct inode *dentry, struct inode *inode,
  547. const struct qstr *qstr)
  548. {
  549. int err;
  550. err = security_inode_init_security(inode, dentry, qstr,
  551. &init_xattrs, NULL);
  552. if (err) {
  553. struct ubifs_info *c = dentry->i_sb->s_fs_info;
  554. ubifs_err(c, "cannot initialize security for inode %lu, error %d",
  555. inode->i_ino, err);
  556. }
  557. return err;
  558. }
  559. #endif
  560. static int xattr_get(const struct xattr_handler *handler,
  561. struct dentry *dentry, struct inode *inode,
  562. const char *name, void *buffer, size_t size)
  563. {
  564. dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
  565. inode->i_ino, dentry, size);
  566. name = xattr_full_name(handler, name);
  567. return ubifs_xattr_get(inode, name, buffer, size);
  568. }
  569. static int xattr_set(const struct xattr_handler *handler,
  570. struct mnt_idmap *idmap,
  571. struct dentry *dentry, struct inode *inode,
  572. const char *name, const void *value,
  573. size_t size, int flags)
  574. {
  575. dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
  576. name, inode->i_ino, dentry, size);
  577. name = xattr_full_name(handler, name);
  578. if (value)
  579. return ubifs_xattr_set(inode, name, value, size, flags, true);
  580. else
  581. return ubifs_xattr_remove(inode, name);
  582. }
  583. static const struct xattr_handler ubifs_user_xattr_handler = {
  584. .prefix = XATTR_USER_PREFIX,
  585. .get = xattr_get,
  586. .set = xattr_set,
  587. };
  588. static const struct xattr_handler ubifs_trusted_xattr_handler = {
  589. .prefix = XATTR_TRUSTED_PREFIX,
  590. .get = xattr_get,
  591. .set = xattr_set,
  592. };
  593. #ifdef CONFIG_UBIFS_FS_SECURITY
  594. static const struct xattr_handler ubifs_security_xattr_handler = {
  595. .prefix = XATTR_SECURITY_PREFIX,
  596. .get = xattr_get,
  597. .set = xattr_set,
  598. };
  599. #endif
  600. const struct xattr_handler * const ubifs_xattr_handlers[] = {
  601. &ubifs_user_xattr_handler,
  602. &ubifs_trusted_xattr_handler,
  603. #ifdef CONFIG_UBIFS_FS_SECURITY
  604. &ubifs_security_xattr_handler,
  605. #endif
  606. NULL
  607. };