xattr.c 13 KB

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