xattr.c 26 KB

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