xattr.c 25 KB

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