xattr.c 23 KB

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