PageRenderTime 161ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/xattr.c

https://bitbucket.org/emiliolopez/linux
C | 992 lines | 743 code | 114 blank | 135 comment | 166 complexity | 1f11cbb40d98b328fe2058a1de0d5fe9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. File: fs/xattr.c
  3. Extended attribute handling.
  4. Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
  5. Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
  6. Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/file.h>
  11. #include <linux/xattr.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/security.h>
  15. #include <linux/evm.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/export.h>
  18. #include <linux/fsnotify.h>
  19. #include <linux/audit.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/posix_acl_xattr.h>
  22. #include <linux/uaccess.h>
  23. #include "internal.h"
  24. static const char *
  25. strcmp_prefix(const char *a, const char *a_prefix)
  26. {
  27. while (*a_prefix && *a == *a_prefix) {
  28. a++;
  29. a_prefix++;
  30. }
  31. return *a_prefix ? NULL : a;
  32. }
  33. /*
  34. * In order to implement different sets of xattr operations for each xattr
  35. * prefix, a filesystem should create a null-terminated array of struct
  36. * xattr_handler (one for each prefix) and hang a pointer to it off of the
  37. * s_xattr field of the superblock.
  38. */
  39. #define for_each_xattr_handler(handlers, handler) \
  40. if (handlers) \
  41. for ((handler) = *(handlers)++; \
  42. (handler) != NULL; \
  43. (handler) = *(handlers)++)
  44. /*
  45. * Find the xattr_handler with the matching prefix.
  46. */
  47. static const struct xattr_handler *
  48. xattr_resolve_name(struct inode *inode, const char **name)
  49. {
  50. const struct xattr_handler **handlers = inode->i_sb->s_xattr;
  51. const struct xattr_handler *handler;
  52. if (!(inode->i_opflags & IOP_XATTR)) {
  53. if (unlikely(is_bad_inode(inode)))
  54. return ERR_PTR(-EIO);
  55. return ERR_PTR(-EOPNOTSUPP);
  56. }
  57. for_each_xattr_handler(handlers, handler) {
  58. const char *n;
  59. n = strcmp_prefix(*name, xattr_prefix(handler));
  60. if (n) {
  61. if (!handler->prefix ^ !*n) {
  62. if (*n)
  63. continue;
  64. return ERR_PTR(-EINVAL);
  65. }
  66. *name = n;
  67. return handler;
  68. }
  69. }
  70. return ERR_PTR(-EOPNOTSUPP);
  71. }
  72. /*
  73. * Check permissions for extended attribute access. This is a bit complicated
  74. * because different namespaces have very different rules.
  75. */
  76. static int
  77. xattr_permission(struct inode *inode, const char *name, int mask)
  78. {
  79. /*
  80. * We can never set or remove an extended attribute on a read-only
  81. * filesystem or on an immutable / append-only inode.
  82. */
  83. if (mask & MAY_WRITE) {
  84. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  85. return -EPERM;
  86. /*
  87. * Updating an xattr will likely cause i_uid and i_gid
  88. * to be writen back improperly if their true value is
  89. * unknown to the vfs.
  90. */
  91. if (HAS_UNMAPPED_ID(inode))
  92. return -EPERM;
  93. }
  94. /*
  95. * No restriction for security.* and system.* from the VFS. Decision
  96. * on these is left to the underlying filesystem / security module.
  97. */
  98. if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
  99. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  100. return 0;
  101. /*
  102. * The trusted.* namespace can only be accessed by privileged users.
  103. */
  104. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
  105. if (!capable(CAP_SYS_ADMIN))
  106. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  107. return 0;
  108. }
  109. /*
  110. * In the user.* namespace, only regular files and directories can have
  111. * extended attributes. For sticky directories, only the owner and
  112. * privileged users can write attributes.
  113. */
  114. if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
  115. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  116. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  117. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  118. (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
  119. return -EPERM;
  120. }
  121. return inode_permission(inode, mask);
  122. }
  123. int
  124. __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
  125. const void *value, size_t size, int flags)
  126. {
  127. const struct xattr_handler *handler;
  128. handler = xattr_resolve_name(inode, &name);
  129. if (IS_ERR(handler))
  130. return PTR_ERR(handler);
  131. if (!handler->set)
  132. return -EOPNOTSUPP;
  133. if (size == 0)
  134. value = ""; /* empty EA, do not remove */
  135. return handler->set(handler, dentry, inode, name, value, size, flags);
  136. }
  137. EXPORT_SYMBOL(__vfs_setxattr);
  138. /**
  139. * __vfs_setxattr_noperm - perform setxattr operation without performing
  140. * permission checks.
  141. *
  142. * @dentry - object to perform setxattr on
  143. * @name - xattr name to set
  144. * @value - value to set @name to
  145. * @size - size of @value
  146. * @flags - flags to pass into filesystem operations
  147. *
  148. * returns the result of the internal setxattr or setsecurity operations.
  149. *
  150. * This function requires the caller to lock the inode's i_mutex before it
  151. * is executed. It also assumes that the caller will make the appropriate
  152. * permission checks.
  153. */
  154. int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
  155. const void *value, size_t size, int flags)
  156. {
  157. struct inode *inode = dentry->d_inode;
  158. int error = -EAGAIN;
  159. int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
  160. XATTR_SECURITY_PREFIX_LEN);
  161. if (issec)
  162. inode->i_flags &= ~S_NOSEC;
  163. if (inode->i_opflags & IOP_XATTR) {
  164. error = __vfs_setxattr(dentry, inode, name, value, size, flags);
  165. if (!error) {
  166. fsnotify_xattr(dentry);
  167. security_inode_post_setxattr(dentry, name, value,
  168. size, flags);
  169. }
  170. } else {
  171. if (unlikely(is_bad_inode(inode)))
  172. return -EIO;
  173. }
  174. if (error == -EAGAIN) {
  175. error = -EOPNOTSUPP;
  176. if (issec) {
  177. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  178. error = security_inode_setsecurity(inode, suffix, value,
  179. size, flags);
  180. if (!error)
  181. fsnotify_xattr(dentry);
  182. }
  183. }
  184. return error;
  185. }
  186. int
  187. vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  188. size_t size, int flags)
  189. {
  190. struct inode *inode = dentry->d_inode;
  191. int error;
  192. error = xattr_permission(inode, name, MAY_WRITE);
  193. if (error)
  194. return error;
  195. inode_lock(inode);
  196. error = security_inode_setxattr(dentry, name, value, size, flags);
  197. if (error)
  198. goto out;
  199. error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
  200. out:
  201. inode_unlock(inode);
  202. return error;
  203. }
  204. EXPORT_SYMBOL_GPL(vfs_setxattr);
  205. ssize_t
  206. xattr_getsecurity(struct inode *inode, const char *name, void *value,
  207. size_t size)
  208. {
  209. void *buffer = NULL;
  210. ssize_t len;
  211. if (!value || !size) {
  212. len = security_inode_getsecurity(inode, name, &buffer, false);
  213. goto out_noalloc;
  214. }
  215. len = security_inode_getsecurity(inode, name, &buffer, true);
  216. if (len < 0)
  217. return len;
  218. if (size < len) {
  219. len = -ERANGE;
  220. goto out;
  221. }
  222. memcpy(value, buffer, len);
  223. out:
  224. kfree(buffer);
  225. out_noalloc:
  226. return len;
  227. }
  228. EXPORT_SYMBOL_GPL(xattr_getsecurity);
  229. /*
  230. * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
  231. *
  232. * Allocate memory, if not already allocated, or re-allocate correct size,
  233. * before retrieving the extended attribute.
  234. *
  235. * Returns the result of alloc, if failed, or the getxattr operation.
  236. */
  237. ssize_t
  238. vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
  239. size_t xattr_size, gfp_t flags)
  240. {
  241. const struct xattr_handler *handler;
  242. struct inode *inode = dentry->d_inode;
  243. char *value = *xattr_value;
  244. int error;
  245. error = xattr_permission(inode, name, MAY_READ);
  246. if (error)
  247. return error;
  248. handler = xattr_resolve_name(inode, &name);
  249. if (IS_ERR(handler))
  250. return PTR_ERR(handler);
  251. if (!handler->get)
  252. return -EOPNOTSUPP;
  253. error = handler->get(handler, dentry, inode, name, NULL, 0);
  254. if (error < 0)
  255. return error;
  256. if (!value || (error > xattr_size)) {
  257. value = krealloc(*xattr_value, error + 1, flags);
  258. if (!value)
  259. return -ENOMEM;
  260. memset(value, 0, error + 1);
  261. }
  262. error = handler->get(handler, dentry, inode, name, value, error);
  263. *xattr_value = value;
  264. return error;
  265. }
  266. ssize_t
  267. __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
  268. void *value, size_t size)
  269. {
  270. const struct xattr_handler *handler;
  271. handler = xattr_resolve_name(inode, &name);
  272. if (IS_ERR(handler))
  273. return PTR_ERR(handler);
  274. if (!handler->get)
  275. return -EOPNOTSUPP;
  276. return handler->get(handler, dentry, inode, name, value, size);
  277. }
  278. EXPORT_SYMBOL(__vfs_getxattr);
  279. ssize_t
  280. vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
  281. {
  282. struct inode *inode = dentry->d_inode;
  283. int error;
  284. error = xattr_permission(inode, name, MAY_READ);
  285. if (error)
  286. return error;
  287. error = security_inode_getxattr(dentry, name);
  288. if (error)
  289. return error;
  290. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  291. XATTR_SECURITY_PREFIX_LEN)) {
  292. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  293. int ret = xattr_getsecurity(inode, suffix, value, size);
  294. /*
  295. * Only overwrite the return value if a security module
  296. * is actually active.
  297. */
  298. if (ret == -EOPNOTSUPP)
  299. goto nolsm;
  300. return ret;
  301. }
  302. nolsm:
  303. return __vfs_getxattr(dentry, inode, name, value, size);
  304. }
  305. EXPORT_SYMBOL_GPL(vfs_getxattr);
  306. ssize_t
  307. vfs_listxattr(struct dentry *dentry, char *list, size_t size)
  308. {
  309. struct inode *inode = d_inode(dentry);
  310. ssize_t error;
  311. error = security_inode_listxattr(dentry);
  312. if (error)
  313. return error;
  314. if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
  315. error = -EOPNOTSUPP;
  316. error = inode->i_op->listxattr(dentry, list, size);
  317. } else {
  318. error = security_inode_listsecurity(inode, list, size);
  319. if (size && error > size)
  320. error = -ERANGE;
  321. }
  322. return error;
  323. }
  324. EXPORT_SYMBOL_GPL(vfs_listxattr);
  325. int
  326. __vfs_removexattr(struct dentry *dentry, const char *name)
  327. {
  328. struct inode *inode = d_inode(dentry);
  329. const struct xattr_handler *handler;
  330. handler = xattr_resolve_name(inode, &name);
  331. if (IS_ERR(handler))
  332. return PTR_ERR(handler);
  333. if (!handler->set)
  334. return -EOPNOTSUPP;
  335. return handler->set(handler, dentry, inode, name, NULL, 0, XATTR_REPLACE);
  336. }
  337. EXPORT_SYMBOL(__vfs_removexattr);
  338. int
  339. vfs_removexattr(struct dentry *dentry, const char *name)
  340. {
  341. struct inode *inode = dentry->d_inode;
  342. int error;
  343. error = xattr_permission(inode, name, MAY_WRITE);
  344. if (error)
  345. return error;
  346. inode_lock(inode);
  347. error = security_inode_removexattr(dentry, name);
  348. if (error)
  349. goto out;
  350. error = __vfs_removexattr(dentry, name);
  351. if (!error) {
  352. fsnotify_xattr(dentry);
  353. evm_inode_post_removexattr(dentry, name);
  354. }
  355. out:
  356. inode_unlock(inode);
  357. return error;
  358. }
  359. EXPORT_SYMBOL_GPL(vfs_removexattr);
  360. /*
  361. * Extended attribute SET operations
  362. */
  363. static long
  364. setxattr(struct dentry *d, const char __user *name, const void __user *value,
  365. size_t size, int flags)
  366. {
  367. int error;
  368. void *kvalue = NULL;
  369. char kname[XATTR_NAME_MAX + 1];
  370. if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
  371. return -EINVAL;
  372. error = strncpy_from_user(kname, name, sizeof(kname));
  373. if (error == 0 || error == sizeof(kname))
  374. error = -ERANGE;
  375. if (error < 0)
  376. return error;
  377. if (size) {
  378. if (size > XATTR_SIZE_MAX)
  379. return -E2BIG;
  380. kvalue = kvmalloc(size, GFP_KERNEL);
  381. if (!kvalue)
  382. return -ENOMEM;
  383. if (copy_from_user(kvalue, value, size)) {
  384. error = -EFAULT;
  385. goto out;
  386. }
  387. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  388. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  389. posix_acl_fix_xattr_from_user(kvalue, size);
  390. else if (strcmp(kname, XATTR_NAME_CAPS) == 0) {
  391. error = cap_convert_nscap(d, &kvalue, size);
  392. if (error < 0)
  393. goto out;
  394. size = error;
  395. }
  396. }
  397. error = vfs_setxattr(d, kname, kvalue, size, flags);
  398. out:
  399. kvfree(kvalue);
  400. return error;
  401. }
  402. static int path_setxattr(const char __user *pathname,
  403. const char __user *name, const void __user *value,
  404. size_t size, int flags, unsigned int lookup_flags)
  405. {
  406. struct path path;
  407. int error;
  408. retry:
  409. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  410. if (error)
  411. return error;
  412. error = mnt_want_write(path.mnt);
  413. if (!error) {
  414. error = setxattr(path.dentry, name, value, size, flags);
  415. mnt_drop_write(path.mnt);
  416. }
  417. path_put(&path);
  418. if (retry_estale(error, lookup_flags)) {
  419. lookup_flags |= LOOKUP_REVAL;
  420. goto retry;
  421. }
  422. return error;
  423. }
  424. SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
  425. const char __user *, name, const void __user *, value,
  426. size_t, size, int, flags)
  427. {
  428. return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
  429. }
  430. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  431. const char __user *, name, const void __user *, value,
  432. size_t, size, int, flags)
  433. {
  434. return path_setxattr(pathname, name, value, size, flags, 0);
  435. }
  436. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  437. const void __user *,value, size_t, size, int, flags)
  438. {
  439. struct fd f = fdget(fd);
  440. int error = -EBADF;
  441. if (!f.file)
  442. return error;
  443. audit_file(f.file);
  444. error = mnt_want_write_file_path(f.file);
  445. if (!error) {
  446. error = setxattr(f.file->f_path.dentry, name, value, size, flags);
  447. mnt_drop_write_file_path(f.file);
  448. }
  449. fdput(f);
  450. return error;
  451. }
  452. /*
  453. * Extended attribute GET operations
  454. */
  455. static ssize_t
  456. getxattr(struct dentry *d, const char __user *name, void __user *value,
  457. size_t size)
  458. {
  459. ssize_t error;
  460. void *kvalue = NULL;
  461. char kname[XATTR_NAME_MAX + 1];
  462. error = strncpy_from_user(kname, name, sizeof(kname));
  463. if (error == 0 || error == sizeof(kname))
  464. error = -ERANGE;
  465. if (error < 0)
  466. return error;
  467. if (size) {
  468. if (size > XATTR_SIZE_MAX)
  469. size = XATTR_SIZE_MAX;
  470. kvalue = kvzalloc(size, GFP_KERNEL);
  471. if (!kvalue)
  472. return -ENOMEM;
  473. }
  474. error = vfs_getxattr(d, kname, kvalue, size);
  475. if (error > 0) {
  476. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  477. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  478. posix_acl_fix_xattr_to_user(kvalue, size);
  479. if (size && copy_to_user(value, kvalue, error))
  480. error = -EFAULT;
  481. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  482. /* The file system tried to returned a value bigger
  483. than XATTR_SIZE_MAX bytes. Not possible. */
  484. error = -E2BIG;
  485. }
  486. kvfree(kvalue);
  487. return error;
  488. }
  489. static ssize_t path_getxattr(const char __user *pathname,
  490. const char __user *name, void __user *value,
  491. size_t size, unsigned int lookup_flags)
  492. {
  493. struct path path;
  494. ssize_t error;
  495. retry:
  496. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  497. if (error)
  498. return error;
  499. error = getxattr(path.dentry, name, value, size);
  500. path_put(&path);
  501. if (retry_estale(error, lookup_flags)) {
  502. lookup_flags |= LOOKUP_REVAL;
  503. goto retry;
  504. }
  505. return error;
  506. }
  507. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  508. const char __user *, name, void __user *, value, size_t, size)
  509. {
  510. return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
  511. }
  512. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  513. const char __user *, name, void __user *, value, size_t, size)
  514. {
  515. return path_getxattr(pathname, name, value, size, 0);
  516. }
  517. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  518. void __user *, value, size_t, size)
  519. {
  520. struct fd f = fdget(fd);
  521. ssize_t error = -EBADF;
  522. if (!f.file)
  523. return error;
  524. audit_file(f.file);
  525. error = getxattr(f.file->f_path.dentry, name, value, size);
  526. fdput(f);
  527. return error;
  528. }
  529. /*
  530. * Extended attribute LIST operations
  531. */
  532. static ssize_t
  533. listxattr(struct dentry *d, char __user *list, size_t size)
  534. {
  535. ssize_t error;
  536. char *klist = NULL;
  537. if (size) {
  538. if (size > XATTR_LIST_MAX)
  539. size = XATTR_LIST_MAX;
  540. klist = kvmalloc(size, GFP_KERNEL);
  541. if (!klist)
  542. return -ENOMEM;
  543. }
  544. error = vfs_listxattr(d, klist, size);
  545. if (error > 0) {
  546. if (size && copy_to_user(list, klist, error))
  547. error = -EFAULT;
  548. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  549. /* The file system tried to returned a list bigger
  550. than XATTR_LIST_MAX bytes. Not possible. */
  551. error = -E2BIG;
  552. }
  553. kvfree(klist);
  554. return error;
  555. }
  556. static ssize_t path_listxattr(const char __user *pathname, char __user *list,
  557. size_t size, unsigned int lookup_flags)
  558. {
  559. struct path path;
  560. ssize_t error;
  561. retry:
  562. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  563. if (error)
  564. return error;
  565. error = listxattr(path.dentry, list, size);
  566. path_put(&path);
  567. if (retry_estale(error, lookup_flags)) {
  568. lookup_flags |= LOOKUP_REVAL;
  569. goto retry;
  570. }
  571. return error;
  572. }
  573. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  574. size_t, size)
  575. {
  576. return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
  577. }
  578. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  579. size_t, size)
  580. {
  581. return path_listxattr(pathname, list, size, 0);
  582. }
  583. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  584. {
  585. struct fd f = fdget(fd);
  586. ssize_t error = -EBADF;
  587. if (!f.file)
  588. return error;
  589. audit_file(f.file);
  590. error = listxattr(f.file->f_path.dentry, list, size);
  591. fdput(f);
  592. return error;
  593. }
  594. /*
  595. * Extended attribute REMOVE operations
  596. */
  597. static long
  598. removexattr(struct dentry *d, const char __user *name)
  599. {
  600. int error;
  601. char kname[XATTR_NAME_MAX + 1];
  602. error = strncpy_from_user(kname, name, sizeof(kname));
  603. if (error == 0 || error == sizeof(kname))
  604. error = -ERANGE;
  605. if (error < 0)
  606. return error;
  607. return vfs_removexattr(d, kname);
  608. }
  609. static int path_removexattr(const char __user *pathname,
  610. const char __user *name, unsigned int lookup_flags)
  611. {
  612. struct path path;
  613. int error;
  614. retry:
  615. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  616. if (error)
  617. return error;
  618. error = mnt_want_write(path.mnt);
  619. if (!error) {
  620. error = removexattr(path.dentry, name);
  621. mnt_drop_write(path.mnt);
  622. }
  623. path_put(&path);
  624. if (retry_estale(error, lookup_flags)) {
  625. lookup_flags |= LOOKUP_REVAL;
  626. goto retry;
  627. }
  628. return error;
  629. }
  630. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  631. const char __user *, name)
  632. {
  633. return path_removexattr(pathname, name, LOOKUP_FOLLOW);
  634. }
  635. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  636. const char __user *, name)
  637. {
  638. return path_removexattr(pathname, name, 0);
  639. }
  640. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  641. {
  642. struct fd f = fdget(fd);
  643. int error = -EBADF;
  644. if (!f.file)
  645. return error;
  646. audit_file(f.file);
  647. error = mnt_want_write_file_path(f.file);
  648. if (!error) {
  649. error = removexattr(f.file->f_path.dentry, name);
  650. mnt_drop_write_file_path(f.file);
  651. }
  652. fdput(f);
  653. return error;
  654. }
  655. /*
  656. * Combine the results of the list() operation from every xattr_handler in the
  657. * list.
  658. */
  659. ssize_t
  660. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  661. {
  662. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  663. unsigned int size = 0;
  664. if (!buffer) {
  665. for_each_xattr_handler(handlers, handler) {
  666. if (!handler->name ||
  667. (handler->list && !handler->list(dentry)))
  668. continue;
  669. size += strlen(handler->name) + 1;
  670. }
  671. } else {
  672. char *buf = buffer;
  673. size_t len;
  674. for_each_xattr_handler(handlers, handler) {
  675. if (!handler->name ||
  676. (handler->list && !handler->list(dentry)))
  677. continue;
  678. len = strlen(handler->name);
  679. if (len + 1 > buffer_size)
  680. return -ERANGE;
  681. memcpy(buf, handler->name, len + 1);
  682. buf += len + 1;
  683. buffer_size -= len + 1;
  684. }
  685. size = buf - buffer;
  686. }
  687. return size;
  688. }
  689. EXPORT_SYMBOL(generic_listxattr);
  690. /**
  691. * xattr_full_name - Compute full attribute name from suffix
  692. *
  693. * @handler: handler of the xattr_handler operation
  694. * @name: name passed to the xattr_handler operation
  695. *
  696. * The get and set xattr handler operations are called with the remainder of
  697. * the attribute name after skipping the handler's prefix: for example, "foo"
  698. * is passed to the get operation of a handler with prefix "user." to get
  699. * attribute "user.foo". The full name is still "there" in the name though.
  700. *
  701. * Note: the list xattr handler operation when called from the vfs is passed a
  702. * NULL name; some file systems use this operation internally, with varying
  703. * semantics.
  704. */
  705. const char *xattr_full_name(const struct xattr_handler *handler,
  706. const char *name)
  707. {
  708. size_t prefix_len = strlen(xattr_prefix(handler));
  709. return name - prefix_len;
  710. }
  711. EXPORT_SYMBOL(xattr_full_name);
  712. /*
  713. * Allocate new xattr and copy in the value; but leave the name to callers.
  714. */
  715. struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
  716. {
  717. struct simple_xattr *new_xattr;
  718. size_t len;
  719. /* wrap around? */
  720. len = sizeof(*new_xattr) + size;
  721. if (len < sizeof(*new_xattr))
  722. return NULL;
  723. new_xattr = kmalloc(len, GFP_KERNEL);
  724. if (!new_xattr)
  725. return NULL;
  726. new_xattr->size = size;
  727. memcpy(new_xattr->value, value, size);
  728. return new_xattr;
  729. }
  730. /*
  731. * xattr GET operation for in-memory/pseudo filesystems
  732. */
  733. int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
  734. void *buffer, size_t size)
  735. {
  736. struct simple_xattr *xattr;
  737. int ret = -ENODATA;
  738. spin_lock(&xattrs->lock);
  739. list_for_each_entry(xattr, &xattrs->head, list) {
  740. if (strcmp(name, xattr->name))
  741. continue;
  742. ret = xattr->size;
  743. if (buffer) {
  744. if (size < xattr->size)
  745. ret = -ERANGE;
  746. else
  747. memcpy(buffer, xattr->value, xattr->size);
  748. }
  749. break;
  750. }
  751. spin_unlock(&xattrs->lock);
  752. return ret;
  753. }
  754. /**
  755. * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
  756. * @xattrs: target simple_xattr list
  757. * @name: name of the extended attribute
  758. * @value: value of the xattr. If %NULL, will remove the attribute.
  759. * @size: size of the new xattr
  760. * @flags: %XATTR_{CREATE|REPLACE}
  761. *
  762. * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
  763. * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
  764. * otherwise, fails with -ENODATA.
  765. *
  766. * Returns 0 on success, -errno on failure.
  767. */
  768. int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
  769. const void *value, size_t size, int flags)
  770. {
  771. struct simple_xattr *xattr;
  772. struct simple_xattr *new_xattr = NULL;
  773. int err = 0;
  774. /* value == NULL means remove */
  775. if (value) {
  776. new_xattr = simple_xattr_alloc(value, size);
  777. if (!new_xattr)
  778. return -ENOMEM;
  779. new_xattr->name = kstrdup(name, GFP_KERNEL);
  780. if (!new_xattr->name) {
  781. kfree(new_xattr);
  782. return -ENOMEM;
  783. }
  784. }
  785. spin_lock(&xattrs->lock);
  786. list_for_each_entry(xattr, &xattrs->head, list) {
  787. if (!strcmp(name, xattr->name)) {
  788. if (flags & XATTR_CREATE) {
  789. xattr = new_xattr;
  790. err = -EEXIST;
  791. } else if (new_xattr) {
  792. list_replace(&xattr->list, &new_xattr->list);
  793. } else {
  794. list_del(&xattr->list);
  795. }
  796. goto out;
  797. }
  798. }
  799. if (flags & XATTR_REPLACE) {
  800. xattr = new_xattr;
  801. err = -ENODATA;
  802. } else {
  803. list_add(&new_xattr->list, &xattrs->head);
  804. xattr = NULL;
  805. }
  806. out:
  807. spin_unlock(&xattrs->lock);
  808. if (xattr) {
  809. kfree(xattr->name);
  810. kfree(xattr);
  811. }
  812. return err;
  813. }
  814. static bool xattr_is_trusted(const char *name)
  815. {
  816. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  817. }
  818. static int xattr_list_one(char **buffer, ssize_t *remaining_size,
  819. const char *name)
  820. {
  821. size_t len = strlen(name) + 1;
  822. if (*buffer) {
  823. if (*remaining_size < len)
  824. return -ERANGE;
  825. memcpy(*buffer, name, len);
  826. *buffer += len;
  827. }
  828. *remaining_size -= len;
  829. return 0;
  830. }
  831. /*
  832. * xattr LIST operation for in-memory/pseudo filesystems
  833. */
  834. ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
  835. char *buffer, size_t size)
  836. {
  837. bool trusted = capable(CAP_SYS_ADMIN);
  838. struct simple_xattr *xattr;
  839. ssize_t remaining_size = size;
  840. int err = 0;
  841. #ifdef CONFIG_FS_POSIX_ACL
  842. if (inode->i_acl) {
  843. err = xattr_list_one(&buffer, &remaining_size,
  844. XATTR_NAME_POSIX_ACL_ACCESS);
  845. if (err)
  846. return err;
  847. }
  848. if (inode->i_default_acl) {
  849. err = xattr_list_one(&buffer, &remaining_size,
  850. XATTR_NAME_POSIX_ACL_DEFAULT);
  851. if (err)
  852. return err;
  853. }
  854. #endif
  855. spin_lock(&xattrs->lock);
  856. list_for_each_entry(xattr, &xattrs->head, list) {
  857. /* skip "trusted." attributes for unprivileged callers */
  858. if (!trusted && xattr_is_trusted(xattr->name))
  859. continue;
  860. err = xattr_list_one(&buffer, &remaining_size, xattr->name);
  861. if (err)
  862. break;
  863. }
  864. spin_unlock(&xattrs->lock);
  865. return err ? err : size - remaining_size;
  866. }
  867. /*
  868. * Adds an extended attribute to the list
  869. */
  870. void simple_xattr_list_add(struct simple_xattrs *xattrs,
  871. struct simple_xattr *new_xattr)
  872. {
  873. spin_lock(&xattrs->lock);
  874. list_add(&new_xattr->list, &xattrs->head);
  875. spin_unlock(&xattrs->lock);
  876. }