xattr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Red Hat. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/fs.h>
  7. #include <linux/slab.h>
  8. #include <linux/rwsem.h>
  9. #include <linux/xattr.h>
  10. #include <linux/security.h>
  11. #include <linux/posix_acl_xattr.h>
  12. #include <linux/iversion.h>
  13. #include <linux/sched/mm.h>
  14. #include "ctree.h"
  15. #include "fs.h"
  16. #include "messages.h"
  17. #include "btrfs_inode.h"
  18. #include "transaction.h"
  19. #include "xattr.h"
  20. #include "disk-io.h"
  21. #include "props.h"
  22. #include "locking.h"
  23. #include "accessors.h"
  24. #include "dir-item.h"
  25. int btrfs_getxattr(const struct inode *inode, const char *name,
  26. void *buffer, size_t size)
  27. {
  28. struct btrfs_dir_item *di;
  29. struct btrfs_root *root = BTRFS_I(inode)->root;
  30. BTRFS_PATH_AUTO_FREE(path);
  31. struct extent_buffer *leaf;
  32. unsigned long data_ptr;
  33. path = btrfs_alloc_path();
  34. if (!path)
  35. return -ENOMEM;
  36. /* lookup the xattr by name */
  37. di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)),
  38. name, strlen(name), 0);
  39. if (!di)
  40. return -ENODATA;
  41. if (IS_ERR(di))
  42. return PTR_ERR(di);
  43. leaf = path->nodes[0];
  44. /* if size is 0, that means we want the size of the attr */
  45. if (!size)
  46. return btrfs_dir_data_len(leaf, di);
  47. /* now get the data out of our dir_item */
  48. if (btrfs_dir_data_len(leaf, di) > size)
  49. return -ERANGE;
  50. /*
  51. * The way things are packed into the leaf is like this
  52. * |struct btrfs_dir_item|name|data|
  53. * where name is the xattr name, so security.foo, and data is the
  54. * content of the xattr. data_ptr points to the location in memory
  55. * where the data starts in the in memory leaf
  56. */
  57. data_ptr = (unsigned long)((char *)(di + 1) +
  58. btrfs_dir_name_len(leaf, di));
  59. read_extent_buffer(leaf, buffer, data_ptr,
  60. btrfs_dir_data_len(leaf, di));
  61. return btrfs_dir_data_len(leaf, di);
  62. }
  63. int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
  64. const char *name, const void *value, size_t size, int flags)
  65. {
  66. struct btrfs_dir_item *di = NULL;
  67. struct btrfs_root *root = BTRFS_I(inode)->root;
  68. BTRFS_PATH_AUTO_FREE(path);
  69. size_t name_len = strlen(name);
  70. int ret = 0;
  71. ASSERT(trans);
  72. if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info))
  73. return -ENOSPC;
  74. path = btrfs_alloc_path();
  75. if (!path)
  76. return -ENOMEM;
  77. path->skip_release_on_error = true;
  78. if (!value) {
  79. di = btrfs_lookup_xattr(trans, root, path,
  80. btrfs_ino(BTRFS_I(inode)), name, name_len, -1);
  81. if (!di && (flags & XATTR_REPLACE))
  82. ret = -ENODATA;
  83. else if (IS_ERR(di))
  84. ret = PTR_ERR(di);
  85. else if (di)
  86. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  87. goto out;
  88. }
  89. /*
  90. * For a replace we can't just do the insert blindly.
  91. * Do a lookup first (read-only btrfs_search_slot), and return if xattr
  92. * doesn't exist. If it exists, fall down below to the insert/replace
  93. * path - we can't race with a concurrent xattr delete, because the VFS
  94. * locks the inode's i_mutex before calling setxattr or removexattr.
  95. */
  96. if (flags & XATTR_REPLACE) {
  97. btrfs_assert_inode_locked(BTRFS_I(inode));
  98. di = btrfs_lookup_xattr(NULL, root, path,
  99. btrfs_ino(BTRFS_I(inode)), name, name_len, 0);
  100. if (!di)
  101. ret = -ENODATA;
  102. else if (IS_ERR(di))
  103. ret = PTR_ERR(di);
  104. if (ret)
  105. goto out;
  106. btrfs_release_path(path);
  107. di = NULL;
  108. }
  109. ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)),
  110. name, name_len, value, size);
  111. if (ret == -EOVERFLOW) {
  112. /*
  113. * We have an existing item in a leaf, split_leaf couldn't
  114. * expand it. That item might have or not a dir_item that
  115. * matches our target xattr, so lets check.
  116. */
  117. ret = 0;
  118. btrfs_assert_tree_write_locked(path->nodes[0]);
  119. di = btrfs_match_dir_item_name(path, name, name_len);
  120. if (!di && !(flags & XATTR_REPLACE)) {
  121. ret = -ENOSPC;
  122. goto out;
  123. }
  124. } else if (ret == -EEXIST) {
  125. ret = 0;
  126. di = btrfs_match_dir_item_name(path, name, name_len);
  127. ASSERT(di); /* logic error */
  128. } else if (ret) {
  129. goto out;
  130. }
  131. if (di && (flags & XATTR_CREATE)) {
  132. ret = -EEXIST;
  133. goto out;
  134. }
  135. if (di) {
  136. /*
  137. * We're doing a replace, and it must be atomic, that is, at
  138. * any point in time we have either the old or the new xattr
  139. * value in the tree. We don't want readers (getxattr and
  140. * listxattrs) to miss a value, this is specially important
  141. * for ACLs.
  142. */
  143. const int slot = path->slots[0];
  144. struct extent_buffer *leaf = path->nodes[0];
  145. const u16 old_data_len = btrfs_dir_data_len(leaf, di);
  146. const u32 item_size = btrfs_item_size(leaf, slot);
  147. const u32 data_size = sizeof(*di) + name_len + size;
  148. unsigned long data_ptr;
  149. char *ptr;
  150. if (size > old_data_len) {
  151. if (btrfs_leaf_free_space(leaf) <
  152. (size - old_data_len)) {
  153. ret = -ENOSPC;
  154. goto out;
  155. }
  156. }
  157. if (old_data_len + name_len + sizeof(*di) == item_size) {
  158. /* No other xattrs packed in the same leaf item. */
  159. if (size > old_data_len)
  160. btrfs_extend_item(trans, path, size - old_data_len);
  161. else if (size < old_data_len)
  162. btrfs_truncate_item(trans, path, data_size, 1);
  163. } else {
  164. /* There are other xattrs packed in the same item. */
  165. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  166. if (ret)
  167. goto out;
  168. btrfs_extend_item(trans, path, data_size);
  169. }
  170. ptr = btrfs_item_ptr(leaf, slot, char);
  171. ptr += btrfs_item_size(leaf, slot) - data_size;
  172. di = (struct btrfs_dir_item *)ptr;
  173. btrfs_set_dir_data_len(leaf, di, size);
  174. data_ptr = ((unsigned long)(di + 1)) + name_len;
  175. write_extent_buffer(leaf, value, data_ptr, size);
  176. } else {
  177. /*
  178. * Insert, and we had space for the xattr, so path->slots[0] is
  179. * where our xattr dir_item is and btrfs_insert_xattr_item()
  180. * filled it.
  181. */
  182. }
  183. out:
  184. if (!ret) {
  185. set_bit(BTRFS_INODE_COPY_EVERYTHING,
  186. &BTRFS_I(inode)->runtime_flags);
  187. clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags);
  188. }
  189. return ret;
  190. }
  191. /*
  192. * @value: "" makes the attribute to empty, NULL removes it
  193. */
  194. int btrfs_setxattr_trans(struct inode *inode, const char *name,
  195. const void *value, size_t size, int flags)
  196. {
  197. struct btrfs_root *root = BTRFS_I(inode)->root;
  198. struct btrfs_trans_handle *trans;
  199. const bool start_trans = (current->journal_info == NULL);
  200. int ret;
  201. if (start_trans) {
  202. /*
  203. * 1 unit for inserting/updating/deleting the xattr
  204. * 1 unit for the inode item update
  205. */
  206. trans = btrfs_start_transaction(root, 2);
  207. if (IS_ERR(trans))
  208. return PTR_ERR(trans);
  209. } else {
  210. /*
  211. * This can happen when smack is enabled and a directory is being
  212. * created. It happens through d_instantiate_new(), which calls
  213. * smack_d_instantiate(), which in turn calls __vfs_setxattr() to
  214. * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the
  215. * inode. We have already reserved space for the xattr and inode
  216. * update at btrfs_mkdir(), so just use the transaction handle.
  217. * We don't join or start a transaction, as that will reset the
  218. * block_rsv of the handle and trigger a warning for the start
  219. * case.
  220. */
  221. ASSERT(strncmp(name, XATTR_SECURITY_PREFIX,
  222. XATTR_SECURITY_PREFIX_LEN) == 0);
  223. trans = current->journal_info;
  224. }
  225. ret = btrfs_setxattr(trans, inode, name, value, size, flags);
  226. if (ret)
  227. goto out;
  228. inode_inc_iversion(inode);
  229. inode_set_ctime_current(inode);
  230. ret = btrfs_update_inode(trans, BTRFS_I(inode));
  231. if (ret)
  232. btrfs_abort_transaction(trans, ret);
  233. out:
  234. if (start_trans)
  235. btrfs_end_transaction(trans);
  236. return ret;
  237. }
  238. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  239. {
  240. struct btrfs_key found_key;
  241. struct btrfs_key key;
  242. struct inode *inode = d_inode(dentry);
  243. struct btrfs_root *root = BTRFS_I(inode)->root;
  244. BTRFS_PATH_AUTO_FREE(path);
  245. int iter_ret = 0;
  246. int ret = 0;
  247. size_t total_size = 0, size_left = size;
  248. /*
  249. * ok we want all objects associated with this id.
  250. * NOTE: we set key.offset = 0; because we want to start with the
  251. * first xattr that we find and walk forward
  252. */
  253. key.objectid = btrfs_ino(BTRFS_I(inode));
  254. key.type = BTRFS_XATTR_ITEM_KEY;
  255. key.offset = 0;
  256. path = btrfs_alloc_path();
  257. if (!path)
  258. return -ENOMEM;
  259. path->reada = READA_FORWARD;
  260. /* search for our xattrs */
  261. btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
  262. struct extent_buffer *leaf;
  263. int slot;
  264. struct btrfs_dir_item *di;
  265. u32 item_size;
  266. u32 cur;
  267. leaf = path->nodes[0];
  268. slot = path->slots[0];
  269. /* check to make sure this item is what we want */
  270. if (found_key.objectid != key.objectid)
  271. break;
  272. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  273. break;
  274. if (found_key.type < BTRFS_XATTR_ITEM_KEY)
  275. continue;
  276. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  277. item_size = btrfs_item_size(leaf, slot);
  278. cur = 0;
  279. while (cur < item_size) {
  280. u16 name_len = btrfs_dir_name_len(leaf, di);
  281. u16 data_len = btrfs_dir_data_len(leaf, di);
  282. u32 this_len = sizeof(*di) + name_len + data_len;
  283. unsigned long name_ptr = (unsigned long)(di + 1);
  284. total_size += name_len + 1;
  285. /*
  286. * We are just looking for how big our buffer needs to
  287. * be.
  288. */
  289. if (!size)
  290. goto next;
  291. if (!buffer || (name_len + 1) > size_left) {
  292. iter_ret = -ERANGE;
  293. break;
  294. }
  295. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  296. buffer[name_len] = '\0';
  297. size_left -= name_len + 1;
  298. buffer += name_len + 1;
  299. next:
  300. cur += this_len;
  301. di = (struct btrfs_dir_item *)((char *)di + this_len);
  302. }
  303. }
  304. if (iter_ret < 0)
  305. ret = iter_ret;
  306. else
  307. ret = total_size;
  308. return ret;
  309. }
  310. static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
  311. struct dentry *unused, struct inode *inode,
  312. const char *name, void *buffer, size_t size)
  313. {
  314. name = xattr_full_name(handler, name);
  315. return btrfs_getxattr(inode, name, buffer, size);
  316. }
  317. static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
  318. struct mnt_idmap *idmap,
  319. struct dentry *unused, struct inode *inode,
  320. const char *name, const void *buffer,
  321. size_t size, int flags)
  322. {
  323. if (btrfs_root_readonly(BTRFS_I(inode)->root))
  324. return -EROFS;
  325. name = xattr_full_name(handler, name);
  326. return btrfs_setxattr_trans(inode, name, buffer, size, flags);
  327. }
  328. static int btrfs_xattr_handler_get_security(const struct xattr_handler *handler,
  329. struct dentry *unused,
  330. struct inode *inode,
  331. const char *name, void *buffer,
  332. size_t size)
  333. {
  334. int ret;
  335. bool is_cap = false;
  336. name = xattr_full_name(handler, name);
  337. /*
  338. * security.capability doesn't cache the results, so calls into us
  339. * constantly to see if there's a capability xattr. Cache the result
  340. * here in order to avoid wasting time doing lookups for xattrs we know
  341. * don't exist.
  342. */
  343. if (strcmp(name, XATTR_NAME_CAPS) == 0) {
  344. is_cap = true;
  345. if (test_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags))
  346. return -ENODATA;
  347. }
  348. ret = btrfs_getxattr(inode, name, buffer, size);
  349. if (ret == -ENODATA && is_cap)
  350. set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
  351. return ret;
  352. }
  353. static int btrfs_xattr_handler_set_security(const struct xattr_handler *handler,
  354. struct mnt_idmap *idmap,
  355. struct dentry *unused,
  356. struct inode *inode,
  357. const char *name,
  358. const void *buffer,
  359. size_t size, int flags)
  360. {
  361. if (btrfs_root_readonly(BTRFS_I(inode)->root))
  362. return -EROFS;
  363. name = xattr_full_name(handler, name);
  364. if (strcmp(name, XATTR_NAME_CAPS) == 0)
  365. clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
  366. return btrfs_setxattr_trans(inode, name, buffer, size, flags);
  367. }
  368. static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
  369. struct mnt_idmap *idmap,
  370. struct dentry *unused, struct inode *inode,
  371. const char *name, const void *value,
  372. size_t size, int flags)
  373. {
  374. int ret;
  375. struct btrfs_trans_handle *trans;
  376. struct btrfs_root *root = BTRFS_I(inode)->root;
  377. name = xattr_full_name(handler, name);
  378. ret = btrfs_validate_prop(BTRFS_I(inode), name, value, size);
  379. if (ret)
  380. return ret;
  381. if (btrfs_ignore_prop(BTRFS_I(inode), name))
  382. return 0;
  383. trans = btrfs_start_transaction(root, 2);
  384. if (IS_ERR(trans))
  385. return PTR_ERR(trans);
  386. ret = btrfs_set_prop(trans, BTRFS_I(inode), name, value, size, flags);
  387. if (!ret) {
  388. inode_inc_iversion(inode);
  389. inode_set_ctime_current(inode);
  390. ret = btrfs_update_inode(trans, BTRFS_I(inode));
  391. if (ret)
  392. btrfs_abort_transaction(trans, ret);
  393. }
  394. btrfs_end_transaction(trans);
  395. return ret;
  396. }
  397. static const struct xattr_handler btrfs_security_xattr_handler = {
  398. .prefix = XATTR_SECURITY_PREFIX,
  399. .get = btrfs_xattr_handler_get_security,
  400. .set = btrfs_xattr_handler_set_security,
  401. };
  402. static const struct xattr_handler btrfs_trusted_xattr_handler = {
  403. .prefix = XATTR_TRUSTED_PREFIX,
  404. .get = btrfs_xattr_handler_get,
  405. .set = btrfs_xattr_handler_set,
  406. };
  407. static const struct xattr_handler btrfs_user_xattr_handler = {
  408. .prefix = XATTR_USER_PREFIX,
  409. .get = btrfs_xattr_handler_get,
  410. .set = btrfs_xattr_handler_set,
  411. };
  412. static const struct xattr_handler btrfs_btrfs_xattr_handler = {
  413. .prefix = XATTR_BTRFS_PREFIX,
  414. .get = btrfs_xattr_handler_get,
  415. .set = btrfs_xattr_handler_set_prop,
  416. };
  417. const struct xattr_handler * const btrfs_xattr_handlers[] = {
  418. &btrfs_security_xattr_handler,
  419. &btrfs_trusted_xattr_handler,
  420. &btrfs_user_xattr_handler,
  421. &btrfs_btrfs_xattr_handler,
  422. NULL,
  423. };
  424. static int btrfs_initxattrs(struct inode *inode,
  425. const struct xattr *xattr_array, void *fs_private)
  426. {
  427. struct btrfs_trans_handle *trans = fs_private;
  428. const struct xattr *xattr;
  429. unsigned int nofs_flag;
  430. char *name;
  431. int ret = 0;
  432. /*
  433. * We're holding a transaction handle, so use a NOFS memory allocation
  434. * context to avoid deadlock if reclaim happens.
  435. */
  436. nofs_flag = memalloc_nofs_save();
  437. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  438. const size_t name_len = XATTR_SECURITY_PREFIX_LEN +
  439. strlen(xattr->name) + 1;
  440. name = kmalloc(name_len, GFP_KERNEL);
  441. if (!name) {
  442. ret = -ENOMEM;
  443. break;
  444. }
  445. scnprintf(name, name_len, "%s%s", XATTR_SECURITY_PREFIX, xattr->name);
  446. if (strcmp(name, XATTR_NAME_CAPS) == 0)
  447. clear_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags);
  448. ret = btrfs_setxattr(trans, inode, name, xattr->value,
  449. xattr->value_len, 0);
  450. kfree(name);
  451. if (ret < 0)
  452. break;
  453. }
  454. memalloc_nofs_restore(nofs_flag);
  455. return ret;
  456. }
  457. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  458. struct inode *inode, struct inode *dir,
  459. const struct qstr *qstr)
  460. {
  461. return security_inode_init_security(inode, dir, qstr,
  462. &btrfs_initxattrs, trans);
  463. }