xattr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. static 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. /*
  229. * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
  230. *
  231. * Allocate memory, if not already allocated, or re-allocate correct size,
  232. * before retrieving the extended attribute.
  233. *
  234. * Returns the result of alloc, if failed, or the getxattr operation.
  235. */
  236. ssize_t
  237. vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
  238. size_t xattr_size, gfp_t flags)
  239. {
  240. const struct xattr_handler *handler;
  241. struct inode *inode = dentry->d_inode;
  242. char *value = *xattr_value;
  243. int error;
  244. error = xattr_permission(inode, name, MAY_READ);
  245. if (error)
  246. return error;
  247. handler = xattr_resolve_name(inode, &name);
  248. if (IS_ERR(handler))
  249. return PTR_ERR(handler);
  250. if (!handler->get)
  251. return -EOPNOTSUPP;
  252. error = handler->get(handler, dentry, inode, name, NULL, 0);
  253. if (error < 0)
  254. return error;
  255. if (!value || (error > xattr_size)) {
  256. value = krealloc(*xattr_value, error + 1, flags);
  257. if (!value)
  258. return -ENOMEM;
  259. memset(value, 0, error + 1);
  260. }
  261. error = handler->get(handler, dentry, inode, name, value, error);
  262. *xattr_value = value;
  263. return error;
  264. }
  265. ssize_t
  266. __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
  267. void *value, size_t size)
  268. {
  269. const struct xattr_handler *handler;
  270. handler = xattr_resolve_name(inode, &name);
  271. if (IS_ERR(handler))
  272. return PTR_ERR(handler);
  273. if (!handler->get)
  274. return -EOPNOTSUPP;
  275. return handler->get(handler, dentry, inode, name, value, size);
  276. }
  277. EXPORT_SYMBOL(__vfs_getxattr);
  278. ssize_t
  279. vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
  280. {
  281. struct inode *inode = dentry->d_inode;
  282. int error;
  283. error = xattr_permission(inode, name, MAY_READ);
  284. if (error)
  285. return error;
  286. error = security_inode_getxattr(dentry, name);
  287. if (error)
  288. return error;
  289. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  290. XATTR_SECURITY_PREFIX_LEN)) {
  291. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  292. int ret = xattr_getsecurity(inode, suffix, value, size);
  293. /*
  294. * Only overwrite the return value if a security module
  295. * is actually active.
  296. */
  297. if (ret == -EOPNOTSUPP)
  298. goto nolsm;
  299. return ret;
  300. }
  301. nolsm:
  302. return __vfs_getxattr(dentry, inode, name, value, size);
  303. }
  304. EXPORT_SYMBOL_GPL(vfs_getxattr);
  305. ssize_t
  306. vfs_listxattr(struct dentry *dentry, char *list, size_t size)
  307. {
  308. struct inode *inode = d_inode(dentry);
  309. ssize_t error;
  310. error = security_inode_listxattr(dentry);
  311. if (error)
  312. return error;
  313. if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
  314. error = inode->i_op->listxattr(dentry, list, size);
  315. } else {
  316. error = security_inode_listsecurity(inode, list, size);
  317. if (size && error > size)
  318. error = -ERANGE;
  319. }
  320. return error;
  321. }
  322. EXPORT_SYMBOL_GPL(vfs_listxattr);
  323. int
  324. __vfs_removexattr(struct dentry *dentry, const char *name)
  325. {
  326. struct inode *inode = d_inode(dentry);
  327. const struct xattr_handler *handler;
  328. handler = xattr_resolve_name(inode, &name);
  329. if (IS_ERR(handler))
  330. return PTR_ERR(handler);
  331. if (!handler->set)
  332. return -EOPNOTSUPP;
  333. return handler->set(handler, dentry, inode, name, NULL, 0, XATTR_REPLACE);
  334. }
  335. EXPORT_SYMBOL(__vfs_removexattr);
  336. int
  337. vfs_removexattr(struct dentry *dentry, const char *name)
  338. {
  339. struct inode *inode = dentry->d_inode;
  340. int error;
  341. error = xattr_permission(inode, name, MAY_WRITE);
  342. if (error)
  343. return error;
  344. inode_lock(inode);
  345. error = security_inode_removexattr(dentry, name);
  346. if (error)
  347. goto out;
  348. error = __vfs_removexattr(dentry, name);
  349. if (!error) {
  350. fsnotify_xattr(dentry);
  351. evm_inode_post_removexattr(dentry, name);
  352. }
  353. out:
  354. inode_unlock(inode);
  355. return error;
  356. }
  357. EXPORT_SYMBOL_GPL(vfs_removexattr);
  358. /*
  359. * Extended attribute SET operations
  360. */
  361. static long
  362. setxattr(struct dentry *d, const char __user *name, const void __user *value,
  363. size_t size, int flags)
  364. {
  365. int error;
  366. void *kvalue = NULL;
  367. char kname[XATTR_NAME_MAX + 1];
  368. if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
  369. return -EINVAL;
  370. error = strncpy_from_user(kname, name, sizeof(kname));
  371. if (error == 0 || error == sizeof(kname))
  372. error = -ERANGE;
  373. if (error < 0)
  374. return error;
  375. if (size) {
  376. if (size > XATTR_SIZE_MAX)
  377. return -E2BIG;
  378. kvalue = kvmalloc(size, GFP_KERNEL);
  379. if (!kvalue)
  380. return -ENOMEM;
  381. if (copy_from_user(kvalue, value, size)) {
  382. error = -EFAULT;
  383. goto out;
  384. }
  385. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  386. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  387. posix_acl_fix_xattr_from_user(kvalue, size);
  388. else if (strcmp(kname, XATTR_NAME_CAPS) == 0) {
  389. error = cap_convert_nscap(d, &kvalue, size);
  390. if (error < 0)
  391. goto out;
  392. size = error;
  393. }
  394. }
  395. error = vfs_setxattr(d, kname, kvalue, size, flags);
  396. out:
  397. kvfree(kvalue);
  398. return error;
  399. }
  400. static int path_setxattr(const char __user *pathname,
  401. const char __user *name, const void __user *value,
  402. size_t size, int flags, unsigned int lookup_flags)
  403. {
  404. struct path path;
  405. int error;
  406. retry:
  407. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  408. if (error)
  409. return error;
  410. error = mnt_want_write(path.mnt);
  411. if (!error) {
  412. error = setxattr(path.dentry, name, value, size, flags);
  413. mnt_drop_write(path.mnt);
  414. }
  415. path_put(&path);
  416. if (retry_estale(error, lookup_flags)) {
  417. lookup_flags |= LOOKUP_REVAL;
  418. goto retry;
  419. }
  420. return error;
  421. }
  422. SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
  423. const char __user *, name, const void __user *, value,
  424. size_t, size, int, flags)
  425. {
  426. return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
  427. }
  428. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  429. const char __user *, name, const void __user *, value,
  430. size_t, size, int, flags)
  431. {
  432. return path_setxattr(pathname, name, value, size, flags, 0);
  433. }
  434. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  435. const void __user *,value, size_t, size, int, flags)
  436. {
  437. struct fd f = fdget(fd);
  438. int error = -EBADF;
  439. if (!f.file)
  440. return error;
  441. audit_file(f.file);
  442. error = mnt_want_write_file_path(f.file);
  443. if (!error) {
  444. error = setxattr(f.file->f_path.dentry, name, value, size, flags);
  445. mnt_drop_write_file_path(f.file);
  446. }
  447. fdput(f);
  448. return error;
  449. }
  450. /*
  451. * Extended attribute GET operations
  452. */
  453. static ssize_t
  454. getxattr(struct dentry *d, const char __user *name, void __user *value,
  455. size_t size)
  456. {
  457. ssize_t error;
  458. void *kvalue = NULL;
  459. char kname[XATTR_NAME_MAX + 1];
  460. error = strncpy_from_user(kname, name, sizeof(kname));
  461. if (error == 0 || error == sizeof(kname))
  462. error = -ERANGE;
  463. if (error < 0)
  464. return error;
  465. if (size) {
  466. if (size > XATTR_SIZE_MAX)
  467. size = XATTR_SIZE_MAX;
  468. kvalue = kvzalloc(size, GFP_KERNEL);
  469. if (!kvalue)
  470. return -ENOMEM;
  471. }
  472. error = vfs_getxattr(d, kname, kvalue, size);
  473. if (error > 0) {
  474. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  475. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  476. posix_acl_fix_xattr_to_user(kvalue, size);
  477. if (size && copy_to_user(value, kvalue, error))
  478. error = -EFAULT;
  479. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  480. /* The file system tried to returned a value bigger
  481. than XATTR_SIZE_MAX bytes. Not possible. */
  482. error = -E2BIG;
  483. }
  484. kvfree(kvalue);
  485. return error;
  486. }
  487. static ssize_t path_getxattr(const char __user *pathname,
  488. const char __user *name, void __user *value,
  489. size_t size, unsigned int lookup_flags)
  490. {
  491. struct path path;
  492. ssize_t error;
  493. retry:
  494. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  495. if (error)
  496. return error;
  497. error = getxattr(path.dentry, name, value, size);
  498. path_put(&path);
  499. if (retry_estale(error, lookup_flags)) {
  500. lookup_flags |= LOOKUP_REVAL;
  501. goto retry;
  502. }
  503. return error;
  504. }
  505. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  506. const char __user *, name, void __user *, value, size_t, size)
  507. {
  508. return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
  509. }
  510. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  511. const char __user *, name, void __user *, value, size_t, size)
  512. {
  513. return path_getxattr(pathname, name, value, size, 0);
  514. }
  515. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  516. void __user *, value, size_t, size)
  517. {
  518. struct fd f = fdget(fd);
  519. ssize_t error = -EBADF;
  520. if (!f.file)
  521. return error;
  522. audit_file(f.file);
  523. error = getxattr(f.file->f_path.dentry, name, value, size);
  524. fdput(f);
  525. return error;
  526. }
  527. /*
  528. * Extended attribute LIST operations
  529. */
  530. static ssize_t
  531. listxattr(struct dentry *d, char __user *list, size_t size)
  532. {
  533. ssize_t error;
  534. char *klist = NULL;
  535. if (size) {
  536. if (size > XATTR_LIST_MAX)
  537. size = XATTR_LIST_MAX;
  538. klist = kvmalloc(size, GFP_KERNEL);
  539. if (!klist)
  540. return -ENOMEM;
  541. }
  542. error = vfs_listxattr(d, klist, size);
  543. if (error > 0) {
  544. if (size && copy_to_user(list, klist, error))
  545. error = -EFAULT;
  546. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  547. /* The file system tried to returned a list bigger
  548. than XATTR_LIST_MAX bytes. Not possible. */
  549. error = -E2BIG;
  550. }
  551. kvfree(klist);
  552. return error;
  553. }
  554. static ssize_t path_listxattr(const char __user *pathname, char __user *list,
  555. size_t size, unsigned int lookup_flags)
  556. {
  557. struct path path;
  558. ssize_t error;
  559. retry:
  560. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  561. if (error)
  562. return error;
  563. error = listxattr(path.dentry, list, size);
  564. path_put(&path);
  565. if (retry_estale(error, lookup_flags)) {
  566. lookup_flags |= LOOKUP_REVAL;
  567. goto retry;
  568. }
  569. return error;
  570. }
  571. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  572. size_t, size)
  573. {
  574. return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
  575. }
  576. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  577. size_t, size)
  578. {
  579. return path_listxattr(pathname, list, size, 0);
  580. }
  581. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  582. {
  583. struct fd f = fdget(fd);
  584. ssize_t error = -EBADF;
  585. if (!f.file)
  586. return error;
  587. audit_file(f.file);
  588. error = listxattr(f.file->f_path.dentry, list, size);
  589. fdput(f);
  590. return error;
  591. }
  592. /*
  593. * Extended attribute REMOVE operations
  594. */
  595. static long
  596. removexattr(struct dentry *d, const char __user *name)
  597. {
  598. int error;
  599. char kname[XATTR_NAME_MAX + 1];
  600. error = strncpy_from_user(kname, name, sizeof(kname));
  601. if (error == 0 || error == sizeof(kname))
  602. error = -ERANGE;
  603. if (error < 0)
  604. return error;
  605. return vfs_removexattr(d, kname);
  606. }
  607. static int path_removexattr(const char __user *pathname,
  608. const char __user *name, unsigned int lookup_flags)
  609. {
  610. struct path path;
  611. int error;
  612. retry:
  613. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  614. if (error)
  615. return error;
  616. error = mnt_want_write(path.mnt);
  617. if (!error) {
  618. error = removexattr(path.dentry, name);
  619. mnt_drop_write(path.mnt);
  620. }
  621. path_put(&path);
  622. if (retry_estale(error, lookup_flags)) {
  623. lookup_flags |= LOOKUP_REVAL;
  624. goto retry;
  625. }
  626. return error;
  627. }
  628. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  629. const char __user *, name)
  630. {
  631. return path_removexattr(pathname, name, LOOKUP_FOLLOW);
  632. }
  633. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  634. const char __user *, name)
  635. {
  636. return path_removexattr(pathname, name, 0);
  637. }
  638. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  639. {
  640. struct fd f = fdget(fd);
  641. int error = -EBADF;
  642. if (!f.file)
  643. return error;
  644. audit_file(f.file);
  645. error = mnt_want_write_file_path(f.file);
  646. if (!error) {
  647. error = removexattr(f.file->f_path.dentry, name);
  648. mnt_drop_write_file_path(f.file);
  649. }
  650. fdput(f);
  651. return error;
  652. }
  653. /*
  654. * Combine the results of the list() operation from every xattr_handler in the
  655. * list.
  656. */
  657. ssize_t
  658. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  659. {
  660. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  661. unsigned int size = 0;
  662. if (!buffer) {
  663. for_each_xattr_handler(handlers, handler) {
  664. if (!handler->name ||
  665. (handler->list && !handler->list(dentry)))
  666. continue;
  667. size += strlen(handler->name) + 1;
  668. }
  669. } else {
  670. char *buf = buffer;
  671. size_t len;
  672. for_each_xattr_handler(handlers, handler) {
  673. if (!handler->name ||
  674. (handler->list && !handler->list(dentry)))
  675. continue;
  676. len = strlen(handler->name);
  677. if (len + 1 > buffer_size)
  678. return -ERANGE;
  679. memcpy(buf, handler->name, len + 1);
  680. buf += len + 1;
  681. buffer_size -= len + 1;
  682. }
  683. size = buf - buffer;
  684. }
  685. return size;
  686. }
  687. EXPORT_SYMBOL(generic_listxattr);
  688. /**
  689. * xattr_full_name - Compute full attribute name from suffix
  690. *
  691. * @handler: handler of the xattr_handler operation
  692. * @name: name passed to the xattr_handler operation
  693. *
  694. * The get and set xattr handler operations are called with the remainder of
  695. * the attribute name after skipping the handler's prefix: for example, "foo"
  696. * is passed to the get operation of a handler with prefix "user." to get
  697. * attribute "user.foo". The full name is still "there" in the name though.
  698. *
  699. * Note: the list xattr handler operation when called from the vfs is passed a
  700. * NULL name; some file systems use this operation internally, with varying
  701. * semantics.
  702. */
  703. const char *xattr_full_name(const struct xattr_handler *handler,
  704. const char *name)
  705. {
  706. size_t prefix_len = strlen(xattr_prefix(handler));
  707. return name - prefix_len;
  708. }
  709. EXPORT_SYMBOL(xattr_full_name);
  710. /*
  711. * Allocate new xattr and copy in the value; but leave the name to callers.
  712. */
  713. struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
  714. {
  715. struct simple_xattr *new_xattr;
  716. size_t len;
  717. /* wrap around? */
  718. len = sizeof(*new_xattr) + size;
  719. if (len < sizeof(*new_xattr))
  720. return NULL;
  721. new_xattr = kmalloc(len, GFP_KERNEL);
  722. if (!new_xattr)
  723. return NULL;
  724. new_xattr->size = size;
  725. memcpy(new_xattr->value, value, size);
  726. return new_xattr;
  727. }
  728. /*
  729. * xattr GET operation for in-memory/pseudo filesystems
  730. */
  731. int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
  732. void *buffer, size_t size)
  733. {
  734. struct simple_xattr *xattr;
  735. int ret = -ENODATA;
  736. spin_lock(&xattrs->lock);
  737. list_for_each_entry(xattr, &xattrs->head, list) {
  738. if (strcmp(name, xattr->name))
  739. continue;
  740. ret = xattr->size;
  741. if (buffer) {
  742. if (size < xattr->size)
  743. ret = -ERANGE;
  744. else
  745. memcpy(buffer, xattr->value, xattr->size);
  746. }
  747. break;
  748. }
  749. spin_unlock(&xattrs->lock);
  750. return ret;
  751. }
  752. /**
  753. * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
  754. * @xattrs: target simple_xattr list
  755. * @name: name of the extended attribute
  756. * @value: value of the xattr. If %NULL, will remove the attribute.
  757. * @size: size of the new xattr
  758. * @flags: %XATTR_{CREATE|REPLACE}
  759. *
  760. * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
  761. * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
  762. * otherwise, fails with -ENODATA.
  763. *
  764. * Returns 0 on success, -errno on failure.
  765. */
  766. int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
  767. const void *value, size_t size, int flags)
  768. {
  769. struct simple_xattr *xattr;
  770. struct simple_xattr *new_xattr = NULL;
  771. int err = 0;
  772. /* value == NULL means remove */
  773. if (value) {
  774. new_xattr = simple_xattr_alloc(value, size);
  775. if (!new_xattr)
  776. return -ENOMEM;
  777. new_xattr->name = kstrdup(name, GFP_KERNEL);
  778. if (!new_xattr->name) {
  779. kfree(new_xattr);
  780. return -ENOMEM;
  781. }
  782. }
  783. spin_lock(&xattrs->lock);
  784. list_for_each_entry(xattr, &xattrs->head, list) {
  785. if (!strcmp(name, xattr->name)) {
  786. if (flags & XATTR_CREATE) {
  787. xattr = new_xattr;
  788. err = -EEXIST;
  789. } else if (new_xattr) {
  790. list_replace(&xattr->list, &new_xattr->list);
  791. } else {
  792. list_del(&xattr->list);
  793. }
  794. goto out;
  795. }
  796. }
  797. if (flags & XATTR_REPLACE) {
  798. xattr = new_xattr;
  799. err = -ENODATA;
  800. } else {
  801. list_add(&new_xattr->list, &xattrs->head);
  802. xattr = NULL;
  803. }
  804. out:
  805. spin_unlock(&xattrs->lock);
  806. if (xattr) {
  807. kfree(xattr->name);
  808. kfree(xattr);
  809. }
  810. return err;
  811. }
  812. static bool xattr_is_trusted(const char *name)
  813. {
  814. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  815. }
  816. static int xattr_list_one(char **buffer, ssize_t *remaining_size,
  817. const char *name)
  818. {
  819. size_t len = strlen(name) + 1;
  820. if (*buffer) {
  821. if (*remaining_size < len)
  822. return -ERANGE;
  823. memcpy(*buffer, name, len);
  824. *buffer += len;
  825. }
  826. *remaining_size -= len;
  827. return 0;
  828. }
  829. /*
  830. * xattr LIST operation for in-memory/pseudo filesystems
  831. */
  832. ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
  833. char *buffer, size_t size)
  834. {
  835. bool trusted = capable(CAP_SYS_ADMIN);
  836. struct simple_xattr *xattr;
  837. ssize_t remaining_size = size;
  838. int err = 0;
  839. #ifdef CONFIG_FS_POSIX_ACL
  840. if (inode->i_acl) {
  841. err = xattr_list_one(&buffer, &remaining_size,
  842. XATTR_NAME_POSIX_ACL_ACCESS);
  843. if (err)
  844. return err;
  845. }
  846. if (inode->i_default_acl) {
  847. err = xattr_list_one(&buffer, &remaining_size,
  848. XATTR_NAME_POSIX_ACL_DEFAULT);
  849. if (err)
  850. return err;
  851. }
  852. #endif
  853. spin_lock(&xattrs->lock);
  854. list_for_each_entry(xattr, &xattrs->head, list) {
  855. /* skip "trusted." attributes for unprivileged callers */
  856. if (!trusted && xattr_is_trusted(xattr->name))
  857. continue;
  858. err = xattr_list_one(&buffer, &remaining_size, xattr->name);
  859. if (err)
  860. break;
  861. }
  862. spin_unlock(&xattrs->lock);
  863. return err ? err : size - remaining_size;
  864. }
  865. /*
  866. * Adds an extended attribute to the list
  867. */
  868. void simple_xattr_list_add(struct simple_xattrs *xattrs,
  869. struct simple_xattr *new_xattr)
  870. {
  871. spin_lock(&xattrs->lock);
  872. list_add(&new_xattr->list, &xattrs->head);
  873. spin_unlock(&xattrs->lock);
  874. }