xattr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. static int do_setxattr(struct btrfs_trans_handle *trans,
  71. struct inode *inode, const char *name,
  72. const void *value, size_t size, int flags)
  73. {
  74. struct btrfs_dir_item *di = NULL;
  75. struct btrfs_root *root = BTRFS_I(inode)->root;
  76. struct btrfs_fs_info *fs_info = root->fs_info;
  77. struct btrfs_path *path;
  78. size_t name_len = strlen(name);
  79. int ret = 0;
  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(fs_info, 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(fs_info, path,
  170. size - old_data_len);
  171. else if (size < old_data_len)
  172. btrfs_truncate_item(fs_info, path,
  173. data_size, 1);
  174. } else {
  175. /* There are other xattrs packed in the same item. */
  176. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  177. if (ret)
  178. goto out;
  179. btrfs_extend_item(fs_info, path, data_size);
  180. }
  181. item = btrfs_item_nr(slot);
  182. ptr = btrfs_item_ptr(leaf, slot, char);
  183. ptr += btrfs_item_size(leaf, item) - data_size;
  184. di = (struct btrfs_dir_item *)ptr;
  185. btrfs_set_dir_data_len(leaf, di, size);
  186. data_ptr = ((unsigned long)(di + 1)) + name_len;
  187. write_extent_buffer(leaf, value, data_ptr, size);
  188. btrfs_mark_buffer_dirty(leaf);
  189. } else {
  190. /*
  191. * Insert, and we had space for the xattr, so path->slots[0] is
  192. * where our xattr dir_item is and btrfs_insert_xattr_item()
  193. * filled it.
  194. */
  195. }
  196. out:
  197. btrfs_free_path(path);
  198. return ret;
  199. }
  200. /*
  201. * @value: "" makes the attribute to empty, NULL removes it
  202. */
  203. int btrfs_setxattr(struct btrfs_trans_handle *trans,
  204. struct inode *inode, const char *name,
  205. const void *value, size_t size, int flags)
  206. {
  207. struct btrfs_root *root = BTRFS_I(inode)->root;
  208. int ret;
  209. if (btrfs_root_readonly(root))
  210. return -EROFS;
  211. if (trans)
  212. return do_setxattr(trans, inode, name, value, size, flags);
  213. trans = btrfs_start_transaction(root, 2);
  214. if (IS_ERR(trans))
  215. return PTR_ERR(trans);
  216. ret = do_setxattr(trans, inode, name, value, size, flags);
  217. if (ret)
  218. goto out;
  219. inode_inc_iversion(inode);
  220. inode->i_ctime = current_time(inode);
  221. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  222. ret = btrfs_update_inode(trans, root, inode);
  223. BUG_ON(ret);
  224. out:
  225. btrfs_end_transaction(trans);
  226. return ret;
  227. }
  228. ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  229. {
  230. struct btrfs_key key;
  231. struct inode *inode = d_inode(dentry);
  232. struct btrfs_root *root = BTRFS_I(inode)->root;
  233. struct btrfs_path *path;
  234. int ret = 0;
  235. size_t total_size = 0, size_left = size;
  236. /*
  237. * ok we want all objects associated with this id.
  238. * NOTE: we set key.offset = 0; because we want to start with the
  239. * first xattr that we find and walk forward
  240. */
  241. key.objectid = btrfs_ino(BTRFS_I(inode));
  242. key.type = BTRFS_XATTR_ITEM_KEY;
  243. key.offset = 0;
  244. path = btrfs_alloc_path();
  245. if (!path)
  246. return -ENOMEM;
  247. path->reada = READA_FORWARD;
  248. /* search for our xattrs */
  249. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  250. if (ret < 0)
  251. goto err;
  252. while (1) {
  253. struct extent_buffer *leaf;
  254. int slot;
  255. struct btrfs_dir_item *di;
  256. struct btrfs_key found_key;
  257. u32 item_size;
  258. u32 cur;
  259. leaf = path->nodes[0];
  260. slot = path->slots[0];
  261. /* this is where we start walking through the path */
  262. if (slot >= btrfs_header_nritems(leaf)) {
  263. /*
  264. * if we've reached the last slot in this leaf we need
  265. * to go to the next leaf and reset everything
  266. */
  267. ret = btrfs_next_leaf(root, path);
  268. if (ret < 0)
  269. goto err;
  270. else if (ret > 0)
  271. break;
  272. continue;
  273. }
  274. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  275. /* check to make sure this item is what we want */
  276. if (found_key.objectid != key.objectid)
  277. break;
  278. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  279. break;
  280. if (found_key.type < BTRFS_XATTR_ITEM_KEY)
  281. goto next_item;
  282. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  283. item_size = btrfs_item_size_nr(leaf, slot);
  284. cur = 0;
  285. while (cur < item_size) {
  286. u16 name_len = btrfs_dir_name_len(leaf, di);
  287. u16 data_len = btrfs_dir_data_len(leaf, di);
  288. u32 this_len = sizeof(*di) + name_len + data_len;
  289. unsigned long name_ptr = (unsigned long)(di + 1);
  290. total_size += name_len + 1;
  291. /*
  292. * We are just looking for how big our buffer needs to
  293. * be.
  294. */
  295. if (!size)
  296. goto next;
  297. if (!buffer || (name_len + 1) > size_left) {
  298. ret = -ERANGE;
  299. goto err;
  300. }
  301. read_extent_buffer(leaf, buffer, name_ptr, name_len);
  302. buffer[name_len] = '\0';
  303. size_left -= name_len + 1;
  304. buffer += name_len + 1;
  305. next:
  306. cur += this_len;
  307. di = (struct btrfs_dir_item *)((char *)di + this_len);
  308. }
  309. next_item:
  310. path->slots[0]++;
  311. }
  312. ret = total_size;
  313. err:
  314. btrfs_free_path(path);
  315. return ret;
  316. }
  317. static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
  318. struct dentry *unused, struct inode *inode,
  319. const char *name, void *buffer, size_t size)
  320. {
  321. name = xattr_full_name(handler, name);
  322. return btrfs_getxattr(inode, name, buffer, size);
  323. }
  324. static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
  325. struct dentry *unused, struct inode *inode,
  326. const char *name, const void *buffer,
  327. size_t size, int flags)
  328. {
  329. name = xattr_full_name(handler, name);
  330. return btrfs_setxattr(NULL, inode, name, buffer, size, flags);
  331. }
  332. static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
  333. struct dentry *unused, struct inode *inode,
  334. const char *name, const void *value,
  335. size_t size, int flags)
  336. {
  337. name = xattr_full_name(handler, name);
  338. return btrfs_set_prop(inode, name, value, size, flags);
  339. }
  340. static const struct xattr_handler btrfs_security_xattr_handler = {
  341. .prefix = XATTR_SECURITY_PREFIX,
  342. .get = btrfs_xattr_handler_get,
  343. .set = btrfs_xattr_handler_set,
  344. };
  345. static const struct xattr_handler btrfs_trusted_xattr_handler = {
  346. .prefix = XATTR_TRUSTED_PREFIX,
  347. .get = btrfs_xattr_handler_get,
  348. .set = btrfs_xattr_handler_set,
  349. };
  350. static const struct xattr_handler btrfs_user_xattr_handler = {
  351. .prefix = XATTR_USER_PREFIX,
  352. .get = btrfs_xattr_handler_get,
  353. .set = btrfs_xattr_handler_set,
  354. };
  355. static const struct xattr_handler btrfs_btrfs_xattr_handler = {
  356. .prefix = XATTR_BTRFS_PREFIX,
  357. .get = btrfs_xattr_handler_get,
  358. .set = btrfs_xattr_handler_set_prop,
  359. };
  360. const struct xattr_handler *btrfs_xattr_handlers[] = {
  361. &btrfs_security_xattr_handler,
  362. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  363. &posix_acl_access_xattr_handler,
  364. &posix_acl_default_xattr_handler,
  365. #endif
  366. &btrfs_trusted_xattr_handler,
  367. &btrfs_user_xattr_handler,
  368. &btrfs_btrfs_xattr_handler,
  369. NULL,
  370. };
  371. static int btrfs_initxattrs(struct inode *inode,
  372. const struct xattr *xattr_array, void *fs_info)
  373. {
  374. const struct xattr *xattr;
  375. struct btrfs_trans_handle *trans = fs_info;
  376. unsigned int nofs_flag;
  377. char *name;
  378. int err = 0;
  379. /*
  380. * We're holding a transaction handle, so use a NOFS memory allocation
  381. * context to avoid deadlock if reclaim happens.
  382. */
  383. nofs_flag = memalloc_nofs_save();
  384. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  385. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  386. strlen(xattr->name) + 1, GFP_KERNEL);
  387. if (!name) {
  388. err = -ENOMEM;
  389. break;
  390. }
  391. strcpy(name, XATTR_SECURITY_PREFIX);
  392. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  393. err = btrfs_setxattr(trans, inode, name, xattr->value,
  394. xattr->value_len, 0);
  395. kfree(name);
  396. if (err < 0)
  397. break;
  398. }
  399. memalloc_nofs_restore(nofs_flag);
  400. return err;
  401. }
  402. int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
  403. struct inode *inode, struct inode *dir,
  404. const struct qstr *qstr)
  405. {
  406. return security_inode_init_security(inode, dir, qstr,
  407. &btrfs_initxattrs, trans);
  408. }