xattr.c 29 KB

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