xattr.c 23 KB

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