xattr.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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/filelock.h>
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/xattr.h>
  14. #include <linux/mount.h>
  15. #include <linux/namei.h>
  16. #include <linux/security.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 * const *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. * may_write_xattr - check whether inode allows writing xattr
  75. * @idmap: idmap of the mount the inode was found from
  76. * @inode: the inode on which to set an xattr
  77. *
  78. * Check whether the inode allows writing xattrs. Specifically, we can never
  79. * set or remove an extended attribute on a read-only filesystem or on an
  80. * immutable / append-only inode.
  81. *
  82. * We also need to ensure that the inode has a mapping in the mount to
  83. * not risk writing back invalid i_{g,u}id values.
  84. *
  85. * Return: On success zero is returned. On error a negative errno is returned.
  86. */
  87. int may_write_xattr(struct mnt_idmap *idmap, struct inode *inode)
  88. {
  89. if (IS_IMMUTABLE(inode))
  90. return -EPERM;
  91. if (IS_APPEND(inode))
  92. return -EPERM;
  93. if (HAS_UNMAPPED_ID(idmap, inode))
  94. return -EPERM;
  95. return 0;
  96. }
  97. /*
  98. * Check permissions for extended attribute access. This is a bit complicated
  99. * because different namespaces have very different rules.
  100. */
  101. static int
  102. xattr_permission(struct mnt_idmap *idmap, struct inode *inode,
  103. const char *name, int mask)
  104. {
  105. if (mask & MAY_WRITE) {
  106. int ret;
  107. ret = may_write_xattr(idmap, inode);
  108. if (ret)
  109. return ret;
  110. }
  111. /*
  112. * No restriction for security.* and system.* from the VFS. Decision
  113. * on these is left to the underlying filesystem / security module.
  114. */
  115. if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
  116. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  117. return 0;
  118. /*
  119. * The trusted.* namespace can only be accessed by privileged users.
  120. */
  121. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
  122. if (!capable(CAP_SYS_ADMIN))
  123. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  124. return 0;
  125. }
  126. /*
  127. * In the user.* namespace, only regular files and directories can have
  128. * extended attributes. For sticky directories, only the owner and
  129. * privileged users can write attributes.
  130. */
  131. if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
  132. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  133. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  134. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  135. (mask & MAY_WRITE) &&
  136. !inode_owner_or_capable(idmap, inode))
  137. return -EPERM;
  138. }
  139. return inode_permission(idmap, inode, mask);
  140. }
  141. /*
  142. * Look for any handler that deals with the specified namespace.
  143. */
  144. int
  145. xattr_supports_user_prefix(struct inode *inode)
  146. {
  147. const struct xattr_handler * const *handlers = inode->i_sb->s_xattr;
  148. const struct xattr_handler *handler;
  149. if (!(inode->i_opflags & IOP_XATTR)) {
  150. if (unlikely(is_bad_inode(inode)))
  151. return -EIO;
  152. return -EOPNOTSUPP;
  153. }
  154. for_each_xattr_handler(handlers, handler) {
  155. if (!strncmp(xattr_prefix(handler), XATTR_USER_PREFIX,
  156. XATTR_USER_PREFIX_LEN))
  157. return 0;
  158. }
  159. return -EOPNOTSUPP;
  160. }
  161. EXPORT_SYMBOL(xattr_supports_user_prefix);
  162. int
  163. __vfs_setxattr(struct mnt_idmap *idmap, struct dentry *dentry,
  164. struct inode *inode, const char *name, const void *value,
  165. size_t size, int flags)
  166. {
  167. const struct xattr_handler *handler;
  168. if (is_posix_acl_xattr(name))
  169. return -EOPNOTSUPP;
  170. handler = xattr_resolve_name(inode, &name);
  171. if (IS_ERR(handler))
  172. return PTR_ERR(handler);
  173. if (!handler->set)
  174. return -EOPNOTSUPP;
  175. if (size == 0)
  176. value = ""; /* empty EA, do not remove */
  177. return handler->set(handler, idmap, dentry, inode, name, value,
  178. size, flags);
  179. }
  180. EXPORT_SYMBOL(__vfs_setxattr);
  181. /**
  182. * __vfs_setxattr_noperm - perform setxattr operation without performing
  183. * permission checks.
  184. *
  185. * @idmap: idmap of the mount the inode was found from
  186. * @dentry: object to perform setxattr on
  187. * @name: xattr name to set
  188. * @value: value to set @name to
  189. * @size: size of @value
  190. * @flags: flags to pass into filesystem operations
  191. *
  192. * returns the result of the internal setxattr or setsecurity operations.
  193. *
  194. * This function requires the caller to lock the inode's i_mutex before it
  195. * is executed. It also assumes that the caller will make the appropriate
  196. * permission checks.
  197. */
  198. int __vfs_setxattr_noperm(struct mnt_idmap *idmap,
  199. struct dentry *dentry, const char *name,
  200. const void *value, size_t size, int flags)
  201. {
  202. struct inode *inode = dentry->d_inode;
  203. int error = -EAGAIN;
  204. int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
  205. XATTR_SECURITY_PREFIX_LEN);
  206. if (issec)
  207. inode->i_flags &= ~S_NOSEC;
  208. if (inode->i_opflags & IOP_XATTR) {
  209. error = __vfs_setxattr(idmap, dentry, inode, name, value,
  210. size, flags);
  211. if (!error) {
  212. fsnotify_xattr(dentry);
  213. security_inode_post_setxattr(dentry, name, value,
  214. size, flags);
  215. }
  216. } else {
  217. if (unlikely(is_bad_inode(inode)))
  218. return -EIO;
  219. }
  220. if (error == -EAGAIN) {
  221. error = -EOPNOTSUPP;
  222. if (issec) {
  223. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  224. error = security_inode_setsecurity(inode, suffix, value,
  225. size, flags);
  226. if (!error)
  227. fsnotify_xattr(dentry);
  228. }
  229. }
  230. return error;
  231. }
  232. /**
  233. * __vfs_setxattr_locked - set an extended attribute while holding the inode
  234. * lock
  235. *
  236. * @idmap: idmap of the mount of the target inode
  237. * @dentry: object to perform setxattr on
  238. * @name: xattr name to set
  239. * @value: value to set @name to
  240. * @size: size of @value
  241. * @flags: flags to pass into filesystem operations
  242. * @delegated_inode: on return, will contain an inode pointer that
  243. * a delegation was broken on, NULL if none.
  244. */
  245. int
  246. __vfs_setxattr_locked(struct mnt_idmap *idmap, struct dentry *dentry,
  247. const char *name, const void *value, size_t size,
  248. int flags, struct inode **delegated_inode)
  249. {
  250. struct inode *inode = dentry->d_inode;
  251. int error;
  252. error = xattr_permission(idmap, inode, name, MAY_WRITE);
  253. if (error)
  254. return error;
  255. error = security_inode_setxattr(idmap, dentry, name, value, size,
  256. flags);
  257. if (error)
  258. goto out;
  259. error = try_break_deleg(inode, delegated_inode);
  260. if (error)
  261. goto out;
  262. error = __vfs_setxattr_noperm(idmap, dentry, name, value,
  263. size, flags);
  264. out:
  265. return error;
  266. }
  267. EXPORT_SYMBOL_GPL(__vfs_setxattr_locked);
  268. int
  269. vfs_setxattr(struct mnt_idmap *idmap, struct dentry *dentry,
  270. const char *name, const void *value, size_t size, int flags)
  271. {
  272. struct inode *inode = dentry->d_inode;
  273. struct inode *delegated_inode = NULL;
  274. const void *orig_value = value;
  275. int error;
  276. if (size && strcmp(name, XATTR_NAME_CAPS) == 0) {
  277. error = cap_convert_nscap(idmap, dentry, &value, size);
  278. if (error < 0)
  279. return error;
  280. size = error;
  281. }
  282. retry_deleg:
  283. inode_lock(inode);
  284. error = __vfs_setxattr_locked(idmap, dentry, name, value, size,
  285. flags, &delegated_inode);
  286. inode_unlock(inode);
  287. if (delegated_inode) {
  288. error = break_deleg_wait(&delegated_inode);
  289. if (!error)
  290. goto retry_deleg;
  291. }
  292. if (value != orig_value)
  293. kfree(value);
  294. return error;
  295. }
  296. EXPORT_SYMBOL_GPL(vfs_setxattr);
  297. static ssize_t
  298. xattr_getsecurity(struct mnt_idmap *idmap, struct inode *inode,
  299. const char *name, void *value, size_t size)
  300. {
  301. void *buffer = NULL;
  302. ssize_t len;
  303. if (!value || !size) {
  304. len = security_inode_getsecurity(idmap, inode, name,
  305. &buffer, false);
  306. goto out_noalloc;
  307. }
  308. len = security_inode_getsecurity(idmap, inode, name, &buffer,
  309. true);
  310. if (len < 0)
  311. return len;
  312. if (size < len) {
  313. len = -ERANGE;
  314. goto out;
  315. }
  316. memcpy(value, buffer, len);
  317. out:
  318. kfree(buffer);
  319. out_noalloc:
  320. return len;
  321. }
  322. /*
  323. * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
  324. *
  325. * Allocate memory, if not already allocated, or re-allocate correct size,
  326. * before retrieving the extended attribute. The xattr value buffer should
  327. * always be freed by the caller, even on error.
  328. *
  329. * Returns the result of alloc, if failed, or the getxattr operation.
  330. */
  331. int
  332. vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
  333. const char *name, char **xattr_value, size_t xattr_size,
  334. gfp_t flags)
  335. {
  336. const struct xattr_handler *handler;
  337. struct inode *inode = dentry->d_inode;
  338. char *value = *xattr_value;
  339. int error;
  340. error = xattr_permission(idmap, inode, name, MAY_READ);
  341. if (error)
  342. return error;
  343. handler = xattr_resolve_name(inode, &name);
  344. if (IS_ERR(handler))
  345. return PTR_ERR(handler);
  346. if (!handler->get)
  347. return -EOPNOTSUPP;
  348. error = handler->get(handler, dentry, inode, name, NULL, 0);
  349. if (error < 0)
  350. return error;
  351. if (!value || (error > xattr_size)) {
  352. value = krealloc(*xattr_value, error + 1, flags);
  353. if (!value)
  354. return -ENOMEM;
  355. memset(value, 0, error + 1);
  356. }
  357. error = handler->get(handler, dentry, inode, name, value, error);
  358. *xattr_value = value;
  359. return error;
  360. }
  361. ssize_t
  362. __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
  363. void *value, size_t size)
  364. {
  365. const struct xattr_handler *handler;
  366. if (is_posix_acl_xattr(name))
  367. return -EOPNOTSUPP;
  368. handler = xattr_resolve_name(inode, &name);
  369. if (IS_ERR(handler))
  370. return PTR_ERR(handler);
  371. if (!handler->get)
  372. return -EOPNOTSUPP;
  373. return handler->get(handler, dentry, inode, name, value, size);
  374. }
  375. EXPORT_SYMBOL(__vfs_getxattr);
  376. ssize_t
  377. vfs_getxattr(struct mnt_idmap *idmap, struct dentry *dentry,
  378. const char *name, void *value, size_t size)
  379. {
  380. struct inode *inode = dentry->d_inode;
  381. int error;
  382. error = xattr_permission(idmap, inode, name, MAY_READ);
  383. if (error)
  384. return error;
  385. error = security_inode_getxattr(dentry, name);
  386. if (error)
  387. return error;
  388. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  389. XATTR_SECURITY_PREFIX_LEN)) {
  390. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  391. int ret = xattr_getsecurity(idmap, inode, suffix, value,
  392. size);
  393. /*
  394. * Only overwrite the return value if a security module
  395. * is actually active.
  396. */
  397. if (ret == -EOPNOTSUPP)
  398. goto nolsm;
  399. return ret;
  400. }
  401. nolsm:
  402. return __vfs_getxattr(dentry, inode, name, value, size);
  403. }
  404. EXPORT_SYMBOL_GPL(vfs_getxattr);
  405. /**
  406. * vfs_listxattr - retrieve \0 separated list of xattr names
  407. * @dentry: the dentry from whose inode the xattr names are retrieved
  408. * @list: buffer to store xattr names into
  409. * @size: size of the buffer
  410. *
  411. * This function returns the names of all xattrs associated with the
  412. * inode of @dentry.
  413. *
  414. * Note, for legacy reasons the vfs_listxattr() function lists POSIX
  415. * ACLs as well. Since POSIX ACLs are decoupled from IOP_XATTR the
  416. * vfs_listxattr() function doesn't check for this flag since a
  417. * filesystem could implement POSIX ACLs without implementing any other
  418. * xattrs.
  419. *
  420. * However, since all codepaths that remove IOP_XATTR also assign of
  421. * inode operations that either don't implement or implement a stub
  422. * ->listxattr() operation.
  423. *
  424. * Return: On success, the size of the buffer that was used. On error a
  425. * negative error code.
  426. */
  427. ssize_t
  428. vfs_listxattr(struct dentry *dentry, char *list, size_t size)
  429. {
  430. struct inode *inode = d_inode(dentry);
  431. ssize_t error;
  432. error = security_inode_listxattr(dentry);
  433. if (error)
  434. return error;
  435. if (inode->i_op->listxattr) {
  436. error = inode->i_op->listxattr(dentry, list, size);
  437. } else {
  438. error = security_inode_listsecurity(inode, list, size);
  439. if (size && error > size)
  440. error = -ERANGE;
  441. }
  442. return error;
  443. }
  444. EXPORT_SYMBOL_GPL(vfs_listxattr);
  445. int
  446. __vfs_removexattr(struct mnt_idmap *idmap, struct dentry *dentry,
  447. const char *name)
  448. {
  449. struct inode *inode = d_inode(dentry);
  450. const struct xattr_handler *handler;
  451. if (is_posix_acl_xattr(name))
  452. return -EOPNOTSUPP;
  453. handler = xattr_resolve_name(inode, &name);
  454. if (IS_ERR(handler))
  455. return PTR_ERR(handler);
  456. if (!handler->set)
  457. return -EOPNOTSUPP;
  458. return handler->set(handler, idmap, dentry, inode, name, NULL, 0,
  459. XATTR_REPLACE);
  460. }
  461. EXPORT_SYMBOL(__vfs_removexattr);
  462. /**
  463. * __vfs_removexattr_locked - set an extended attribute while holding the inode
  464. * lock
  465. *
  466. * @idmap: idmap of the mount of the target inode
  467. * @dentry: object to perform setxattr on
  468. * @name: name of xattr to remove
  469. * @delegated_inode: on return, will contain an inode pointer that
  470. * a delegation was broken on, NULL if none.
  471. */
  472. int
  473. __vfs_removexattr_locked(struct mnt_idmap *idmap,
  474. struct dentry *dentry, const char *name,
  475. struct inode **delegated_inode)
  476. {
  477. struct inode *inode = dentry->d_inode;
  478. int error;
  479. error = xattr_permission(idmap, inode, name, MAY_WRITE);
  480. if (error)
  481. return error;
  482. error = security_inode_removexattr(idmap, dentry, name);
  483. if (error)
  484. goto out;
  485. error = try_break_deleg(inode, delegated_inode);
  486. if (error)
  487. goto out;
  488. error = __vfs_removexattr(idmap, dentry, name);
  489. if (error)
  490. return error;
  491. fsnotify_xattr(dentry);
  492. security_inode_post_removexattr(dentry, name);
  493. out:
  494. return error;
  495. }
  496. EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
  497. int
  498. vfs_removexattr(struct mnt_idmap *idmap, struct dentry *dentry,
  499. const char *name)
  500. {
  501. struct inode *inode = dentry->d_inode;
  502. struct inode *delegated_inode = NULL;
  503. int error;
  504. retry_deleg:
  505. inode_lock(inode);
  506. error = __vfs_removexattr_locked(idmap, dentry,
  507. name, &delegated_inode);
  508. inode_unlock(inode);
  509. if (delegated_inode) {
  510. error = break_deleg_wait(&delegated_inode);
  511. if (!error)
  512. goto retry_deleg;
  513. }
  514. return error;
  515. }
  516. EXPORT_SYMBOL_GPL(vfs_removexattr);
  517. /*
  518. * Extended attribute SET operations
  519. */
  520. int setxattr_copy(const char __user *name, struct xattr_ctx *ctx)
  521. {
  522. int error;
  523. if (ctx->flags & ~(XATTR_CREATE|XATTR_REPLACE))
  524. return -EINVAL;
  525. error = strncpy_from_user(ctx->kname->name, name,
  526. sizeof(ctx->kname->name));
  527. if (error == 0 || error == sizeof(ctx->kname->name))
  528. return -ERANGE;
  529. if (error < 0)
  530. return error;
  531. error = 0;
  532. if (ctx->size) {
  533. if (ctx->size > XATTR_SIZE_MAX)
  534. return -E2BIG;
  535. ctx->kvalue = vmemdup_user(ctx->cvalue, ctx->size);
  536. if (IS_ERR(ctx->kvalue)) {
  537. error = PTR_ERR(ctx->kvalue);
  538. ctx->kvalue = NULL;
  539. }
  540. }
  541. return error;
  542. }
  543. int do_setxattr(struct mnt_idmap *idmap, struct dentry *dentry,
  544. struct xattr_ctx *ctx)
  545. {
  546. if (is_posix_acl_xattr(ctx->kname->name))
  547. return do_set_acl(idmap, dentry, ctx->kname->name,
  548. ctx->kvalue, ctx->size);
  549. return vfs_setxattr(idmap, dentry, ctx->kname->name,
  550. ctx->kvalue, ctx->size, ctx->flags);
  551. }
  552. static int path_setxattr(const char __user *pathname,
  553. const char __user *name, const void __user *value,
  554. size_t size, int flags, unsigned int lookup_flags)
  555. {
  556. struct xattr_name kname;
  557. struct xattr_ctx ctx = {
  558. .cvalue = value,
  559. .kvalue = NULL,
  560. .size = size,
  561. .kname = &kname,
  562. .flags = flags,
  563. };
  564. struct path path;
  565. int error;
  566. error = setxattr_copy(name, &ctx);
  567. if (error)
  568. return error;
  569. retry:
  570. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  571. if (error)
  572. goto out;
  573. error = mnt_want_write(path.mnt);
  574. if (!error) {
  575. error = do_setxattr(mnt_idmap(path.mnt), path.dentry, &ctx);
  576. mnt_drop_write(path.mnt);
  577. }
  578. path_put(&path);
  579. if (retry_estale(error, lookup_flags)) {
  580. lookup_flags |= LOOKUP_REVAL;
  581. goto retry;
  582. }
  583. out:
  584. kvfree(ctx.kvalue);
  585. return error;
  586. }
  587. SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
  588. const char __user *, name, const void __user *, value,
  589. size_t, size, int, flags)
  590. {
  591. return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
  592. }
  593. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  594. const char __user *, name, const void __user *, value,
  595. size_t, size, int, flags)
  596. {
  597. return path_setxattr(pathname, name, value, size, flags, 0);
  598. }
  599. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  600. const void __user *,value, size_t, size, int, flags)
  601. {
  602. struct xattr_name kname;
  603. struct xattr_ctx ctx = {
  604. .cvalue = value,
  605. .kvalue = NULL,
  606. .size = size,
  607. .kname = &kname,
  608. .flags = flags,
  609. };
  610. int error;
  611. CLASS(fd, f)(fd);
  612. if (!fd_file(f))
  613. return -EBADF;
  614. audit_file(fd_file(f));
  615. error = setxattr_copy(name, &ctx);
  616. if (error)
  617. return error;
  618. error = mnt_want_write_file(fd_file(f));
  619. if (!error) {
  620. error = do_setxattr(file_mnt_idmap(fd_file(f)),
  621. fd_file(f)->f_path.dentry, &ctx);
  622. mnt_drop_write_file(fd_file(f));
  623. }
  624. kvfree(ctx.kvalue);
  625. return error;
  626. }
  627. /*
  628. * Extended attribute GET operations
  629. */
  630. ssize_t
  631. do_getxattr(struct mnt_idmap *idmap, struct dentry *d,
  632. struct xattr_ctx *ctx)
  633. {
  634. ssize_t error;
  635. char *kname = ctx->kname->name;
  636. if (ctx->size) {
  637. if (ctx->size > XATTR_SIZE_MAX)
  638. ctx->size = XATTR_SIZE_MAX;
  639. ctx->kvalue = kvzalloc(ctx->size, GFP_KERNEL);
  640. if (!ctx->kvalue)
  641. return -ENOMEM;
  642. }
  643. if (is_posix_acl_xattr(ctx->kname->name))
  644. error = do_get_acl(idmap, d, kname, ctx->kvalue, ctx->size);
  645. else
  646. error = vfs_getxattr(idmap, d, kname, ctx->kvalue, ctx->size);
  647. if (error > 0) {
  648. if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))
  649. error = -EFAULT;
  650. } else if (error == -ERANGE && ctx->size >= XATTR_SIZE_MAX) {
  651. /* The file system tried to returned a value bigger
  652. than XATTR_SIZE_MAX bytes. Not possible. */
  653. error = -E2BIG;
  654. }
  655. return error;
  656. }
  657. static ssize_t
  658. getxattr(struct mnt_idmap *idmap, struct dentry *d,
  659. const char __user *name, void __user *value, size_t size)
  660. {
  661. ssize_t error;
  662. struct xattr_name kname;
  663. struct xattr_ctx ctx = {
  664. .value = value,
  665. .kvalue = NULL,
  666. .size = size,
  667. .kname = &kname,
  668. .flags = 0,
  669. };
  670. error = strncpy_from_user(kname.name, name, sizeof(kname.name));
  671. if (error == 0 || error == sizeof(kname.name))
  672. error = -ERANGE;
  673. if (error < 0)
  674. return error;
  675. error = do_getxattr(idmap, d, &ctx);
  676. kvfree(ctx.kvalue);
  677. return error;
  678. }
  679. static ssize_t path_getxattr(const char __user *pathname,
  680. const char __user *name, void __user *value,
  681. size_t size, unsigned int lookup_flags)
  682. {
  683. struct path path;
  684. ssize_t error;
  685. retry:
  686. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  687. if (error)
  688. return error;
  689. error = getxattr(mnt_idmap(path.mnt), path.dentry, name, value, size);
  690. path_put(&path);
  691. if (retry_estale(error, lookup_flags)) {
  692. lookup_flags |= LOOKUP_REVAL;
  693. goto retry;
  694. }
  695. return error;
  696. }
  697. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  698. const char __user *, name, void __user *, value, size_t, size)
  699. {
  700. return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
  701. }
  702. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  703. const char __user *, name, void __user *, value, size_t, size)
  704. {
  705. return path_getxattr(pathname, name, value, size, 0);
  706. }
  707. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  708. void __user *, value, size_t, size)
  709. {
  710. struct fd f = fdget(fd);
  711. ssize_t error = -EBADF;
  712. if (!fd_file(f))
  713. return error;
  714. audit_file(fd_file(f));
  715. error = getxattr(file_mnt_idmap(fd_file(f)), fd_file(f)->f_path.dentry,
  716. name, value, size);
  717. fdput(f);
  718. return error;
  719. }
  720. /*
  721. * Extended attribute LIST operations
  722. */
  723. static ssize_t
  724. listxattr(struct dentry *d, char __user *list, size_t size)
  725. {
  726. ssize_t error;
  727. char *klist = NULL;
  728. if (size) {
  729. if (size > XATTR_LIST_MAX)
  730. size = XATTR_LIST_MAX;
  731. klist = kvmalloc(size, GFP_KERNEL);
  732. if (!klist)
  733. return -ENOMEM;
  734. }
  735. error = vfs_listxattr(d, klist, size);
  736. if (error > 0) {
  737. if (size && copy_to_user(list, klist, error))
  738. error = -EFAULT;
  739. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  740. /* The file system tried to returned a list bigger
  741. than XATTR_LIST_MAX bytes. Not possible. */
  742. error = -E2BIG;
  743. }
  744. kvfree(klist);
  745. return error;
  746. }
  747. static ssize_t path_listxattr(const char __user *pathname, char __user *list,
  748. size_t size, unsigned int lookup_flags)
  749. {
  750. struct path path;
  751. ssize_t error;
  752. retry:
  753. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  754. if (error)
  755. return error;
  756. error = listxattr(path.dentry, list, size);
  757. path_put(&path);
  758. if (retry_estale(error, lookup_flags)) {
  759. lookup_flags |= LOOKUP_REVAL;
  760. goto retry;
  761. }
  762. return error;
  763. }
  764. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  765. size_t, size)
  766. {
  767. return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
  768. }
  769. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  770. size_t, size)
  771. {
  772. return path_listxattr(pathname, list, size, 0);
  773. }
  774. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  775. {
  776. struct fd f = fdget(fd);
  777. ssize_t error = -EBADF;
  778. if (!fd_file(f))
  779. return error;
  780. audit_file(fd_file(f));
  781. error = listxattr(fd_file(f)->f_path.dentry, list, size);
  782. fdput(f);
  783. return error;
  784. }
  785. /*
  786. * Extended attribute REMOVE operations
  787. */
  788. static long
  789. removexattr(struct mnt_idmap *idmap, struct dentry *d, const char *name)
  790. {
  791. if (is_posix_acl_xattr(name))
  792. return vfs_remove_acl(idmap, d, name);
  793. return vfs_removexattr(idmap, d, name);
  794. }
  795. static int path_removexattr(const char __user *pathname,
  796. const char __user *name, unsigned int lookup_flags)
  797. {
  798. struct path path;
  799. int error;
  800. char kname[XATTR_NAME_MAX + 1];
  801. error = strncpy_from_user(kname, name, sizeof(kname));
  802. if (error == 0 || error == sizeof(kname))
  803. error = -ERANGE;
  804. if (error < 0)
  805. return error;
  806. retry:
  807. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  808. if (error)
  809. return error;
  810. error = mnt_want_write(path.mnt);
  811. if (!error) {
  812. error = removexattr(mnt_idmap(path.mnt), path.dentry, kname);
  813. mnt_drop_write(path.mnt);
  814. }
  815. path_put(&path);
  816. if (retry_estale(error, lookup_flags)) {
  817. lookup_flags |= LOOKUP_REVAL;
  818. goto retry;
  819. }
  820. return error;
  821. }
  822. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  823. const char __user *, name)
  824. {
  825. return path_removexattr(pathname, name, LOOKUP_FOLLOW);
  826. }
  827. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  828. const char __user *, name)
  829. {
  830. return path_removexattr(pathname, name, 0);
  831. }
  832. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  833. {
  834. struct fd f = fdget(fd);
  835. char kname[XATTR_NAME_MAX + 1];
  836. int error = -EBADF;
  837. if (!fd_file(f))
  838. return error;
  839. audit_file(fd_file(f));
  840. error = strncpy_from_user(kname, name, sizeof(kname));
  841. if (error == 0 || error == sizeof(kname))
  842. error = -ERANGE;
  843. if (error < 0)
  844. return error;
  845. error = mnt_want_write_file(fd_file(f));
  846. if (!error) {
  847. error = removexattr(file_mnt_idmap(fd_file(f)),
  848. fd_file(f)->f_path.dentry, kname);
  849. mnt_drop_write_file(fd_file(f));
  850. }
  851. fdput(f);
  852. return error;
  853. }
  854. int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name)
  855. {
  856. size_t len;
  857. len = strlen(name) + 1;
  858. if (*buffer) {
  859. if (*remaining_size < len)
  860. return -ERANGE;
  861. memcpy(*buffer, name, len);
  862. *buffer += len;
  863. }
  864. *remaining_size -= len;
  865. return 0;
  866. }
  867. /**
  868. * generic_listxattr - run through a dentry's xattr list() operations
  869. * @dentry: dentry to list the xattrs
  870. * @buffer: result buffer
  871. * @buffer_size: size of @buffer
  872. *
  873. * Combine the results of the list() operation from every xattr_handler in the
  874. * xattr_handler stack.
  875. *
  876. * Note that this will not include the entries for POSIX ACLs.
  877. */
  878. ssize_t
  879. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  880. {
  881. const struct xattr_handler *handler, * const *handlers = dentry->d_sb->s_xattr;
  882. ssize_t remaining_size = buffer_size;
  883. int err = 0;
  884. for_each_xattr_handler(handlers, handler) {
  885. if (!handler->name || (handler->list && !handler->list(dentry)))
  886. continue;
  887. err = xattr_list_one(&buffer, &remaining_size, handler->name);
  888. if (err)
  889. return err;
  890. }
  891. return err ? err : buffer_size - remaining_size;
  892. }
  893. EXPORT_SYMBOL(generic_listxattr);
  894. /**
  895. * xattr_full_name - Compute full attribute name from suffix
  896. *
  897. * @handler: handler of the xattr_handler operation
  898. * @name: name passed to the xattr_handler operation
  899. *
  900. * The get and set xattr handler operations are called with the remainder of
  901. * the attribute name after skipping the handler's prefix: for example, "foo"
  902. * is passed to the get operation of a handler with prefix "user." to get
  903. * attribute "user.foo". The full name is still "there" in the name though.
  904. *
  905. * Note: the list xattr handler operation when called from the vfs is passed a
  906. * NULL name; some file systems use this operation internally, with varying
  907. * semantics.
  908. */
  909. const char *xattr_full_name(const struct xattr_handler *handler,
  910. const char *name)
  911. {
  912. size_t prefix_len = strlen(xattr_prefix(handler));
  913. return name - prefix_len;
  914. }
  915. EXPORT_SYMBOL(xattr_full_name);
  916. /**
  917. * simple_xattr_space - estimate the memory used by a simple xattr
  918. * @name: the full name of the xattr
  919. * @size: the size of its value
  920. *
  921. * This takes no account of how much larger the two slab objects actually are:
  922. * that would depend on the slab implementation, when what is required is a
  923. * deterministic number, which grows with name length and size and quantity.
  924. *
  925. * Return: The approximate number of bytes of memory used by such an xattr.
  926. */
  927. size_t simple_xattr_space(const char *name, size_t size)
  928. {
  929. /*
  930. * Use "40" instead of sizeof(struct simple_xattr), to return the
  931. * same result on 32-bit and 64-bit, and even if simple_xattr grows.
  932. */
  933. return 40 + size + strlen(name);
  934. }
  935. /**
  936. * simple_xattr_free - free an xattr object
  937. * @xattr: the xattr object
  938. *
  939. * Free the xattr object. Can handle @xattr being NULL.
  940. */
  941. void simple_xattr_free(struct simple_xattr *xattr)
  942. {
  943. if (xattr)
  944. kfree(xattr->name);
  945. kvfree(xattr);
  946. }
  947. /**
  948. * simple_xattr_alloc - allocate new xattr object
  949. * @value: value of the xattr object
  950. * @size: size of @value
  951. *
  952. * Allocate a new xattr object and initialize respective members. The caller is
  953. * responsible for handling the name of the xattr.
  954. *
  955. * Return: On success a new xattr object is returned. On failure NULL is
  956. * returned.
  957. */
  958. struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
  959. {
  960. struct simple_xattr *new_xattr;
  961. size_t len;
  962. /* wrap around? */
  963. len = sizeof(*new_xattr) + size;
  964. if (len < sizeof(*new_xattr))
  965. return NULL;
  966. new_xattr = kvmalloc(len, GFP_KERNEL_ACCOUNT);
  967. if (!new_xattr)
  968. return NULL;
  969. new_xattr->size = size;
  970. memcpy(new_xattr->value, value, size);
  971. return new_xattr;
  972. }
  973. /**
  974. * rbtree_simple_xattr_cmp - compare xattr name with current rbtree xattr entry
  975. * @key: xattr name
  976. * @node: current node
  977. *
  978. * Compare the xattr name with the xattr name attached to @node in the rbtree.
  979. *
  980. * Return: Negative value if continuing left, positive if continuing right, 0
  981. * if the xattr attached to @node matches @key.
  982. */
  983. static int rbtree_simple_xattr_cmp(const void *key, const struct rb_node *node)
  984. {
  985. const char *xattr_name = key;
  986. const struct simple_xattr *xattr;
  987. xattr = rb_entry(node, struct simple_xattr, rb_node);
  988. return strcmp(xattr->name, xattr_name);
  989. }
  990. /**
  991. * rbtree_simple_xattr_node_cmp - compare two xattr rbtree nodes
  992. * @new_node: new node
  993. * @node: current node
  994. *
  995. * Compare the xattr attached to @new_node with the xattr attached to @node.
  996. *
  997. * Return: Negative value if continuing left, positive if continuing right, 0
  998. * if the xattr attached to @new_node matches the xattr attached to @node.
  999. */
  1000. static int rbtree_simple_xattr_node_cmp(struct rb_node *new_node,
  1001. const struct rb_node *node)
  1002. {
  1003. struct simple_xattr *xattr;
  1004. xattr = rb_entry(new_node, struct simple_xattr, rb_node);
  1005. return rbtree_simple_xattr_cmp(xattr->name, node);
  1006. }
  1007. /**
  1008. * simple_xattr_get - get an xattr object
  1009. * @xattrs: the header of the xattr object
  1010. * @name: the name of the xattr to retrieve
  1011. * @buffer: the buffer to store the value into
  1012. * @size: the size of @buffer
  1013. *
  1014. * Try to find and retrieve the xattr object associated with @name.
  1015. * If @buffer is provided store the value of @xattr in @buffer
  1016. * otherwise just return the length. The size of @buffer is limited
  1017. * to XATTR_SIZE_MAX which currently is 65536.
  1018. *
  1019. * Return: On success the length of the xattr value is returned. On error a
  1020. * negative error code is returned.
  1021. */
  1022. int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
  1023. void *buffer, size_t size)
  1024. {
  1025. struct simple_xattr *xattr = NULL;
  1026. struct rb_node *rbp;
  1027. int ret = -ENODATA;
  1028. read_lock(&xattrs->lock);
  1029. rbp = rb_find(name, &xattrs->rb_root, rbtree_simple_xattr_cmp);
  1030. if (rbp) {
  1031. xattr = rb_entry(rbp, struct simple_xattr, rb_node);
  1032. ret = xattr->size;
  1033. if (buffer) {
  1034. if (size < xattr->size)
  1035. ret = -ERANGE;
  1036. else
  1037. memcpy(buffer, xattr->value, xattr->size);
  1038. }
  1039. }
  1040. read_unlock(&xattrs->lock);
  1041. return ret;
  1042. }
  1043. /**
  1044. * simple_xattr_set - set an xattr object
  1045. * @xattrs: the header of the xattr object
  1046. * @name: the name of the xattr to retrieve
  1047. * @value: the value to store along the xattr
  1048. * @size: the size of @value
  1049. * @flags: the flags determining how to set the xattr
  1050. *
  1051. * Set a new xattr object.
  1052. * If @value is passed a new xattr object will be allocated. If XATTR_REPLACE
  1053. * is specified in @flags a matching xattr object for @name must already exist.
  1054. * If it does it will be replaced with the new xattr object. If it doesn't we
  1055. * fail. If XATTR_CREATE is specified and a matching xattr does already exist
  1056. * we fail. If it doesn't we create a new xattr. If @flags is zero we simply
  1057. * insert the new xattr replacing any existing one.
  1058. *
  1059. * If @value is empty and a matching xattr object is found we delete it if
  1060. * XATTR_REPLACE is specified in @flags or @flags is zero.
  1061. *
  1062. * If @value is empty and no matching xattr object for @name is found we do
  1063. * nothing if XATTR_CREATE is specified in @flags or @flags is zero. For
  1064. * XATTR_REPLACE we fail as mentioned above.
  1065. *
  1066. * Return: On success, the removed or replaced xattr is returned, to be freed
  1067. * by the caller; or NULL if none. On failure a negative error code is returned.
  1068. */
  1069. struct simple_xattr *simple_xattr_set(struct simple_xattrs *xattrs,
  1070. const char *name, const void *value,
  1071. size_t size, int flags)
  1072. {
  1073. struct simple_xattr *old_xattr = NULL, *new_xattr = NULL;
  1074. struct rb_node *parent = NULL, **rbp;
  1075. int err = 0, ret;
  1076. /* value == NULL means remove */
  1077. if (value) {
  1078. new_xattr = simple_xattr_alloc(value, size);
  1079. if (!new_xattr)
  1080. return ERR_PTR(-ENOMEM);
  1081. new_xattr->name = kstrdup(name, GFP_KERNEL_ACCOUNT);
  1082. if (!new_xattr->name) {
  1083. simple_xattr_free(new_xattr);
  1084. return ERR_PTR(-ENOMEM);
  1085. }
  1086. }
  1087. write_lock(&xattrs->lock);
  1088. rbp = &xattrs->rb_root.rb_node;
  1089. while (*rbp) {
  1090. parent = *rbp;
  1091. ret = rbtree_simple_xattr_cmp(name, *rbp);
  1092. if (ret < 0)
  1093. rbp = &(*rbp)->rb_left;
  1094. else if (ret > 0)
  1095. rbp = &(*rbp)->rb_right;
  1096. else
  1097. old_xattr = rb_entry(*rbp, struct simple_xattr, rb_node);
  1098. if (old_xattr)
  1099. break;
  1100. }
  1101. if (old_xattr) {
  1102. /* Fail if XATTR_CREATE is requested and the xattr exists. */
  1103. if (flags & XATTR_CREATE) {
  1104. err = -EEXIST;
  1105. goto out_unlock;
  1106. }
  1107. if (new_xattr)
  1108. rb_replace_node(&old_xattr->rb_node,
  1109. &new_xattr->rb_node, &xattrs->rb_root);
  1110. else
  1111. rb_erase(&old_xattr->rb_node, &xattrs->rb_root);
  1112. } else {
  1113. /* Fail if XATTR_REPLACE is requested but no xattr is found. */
  1114. if (flags & XATTR_REPLACE) {
  1115. err = -ENODATA;
  1116. goto out_unlock;
  1117. }
  1118. /*
  1119. * If XATTR_CREATE or no flags are specified together with a
  1120. * new value simply insert it.
  1121. */
  1122. if (new_xattr) {
  1123. rb_link_node(&new_xattr->rb_node, parent, rbp);
  1124. rb_insert_color(&new_xattr->rb_node, &xattrs->rb_root);
  1125. }
  1126. /*
  1127. * If XATTR_CREATE or no flags are specified and neither an
  1128. * old or new xattr exist then we don't need to do anything.
  1129. */
  1130. }
  1131. out_unlock:
  1132. write_unlock(&xattrs->lock);
  1133. if (!err)
  1134. return old_xattr;
  1135. simple_xattr_free(new_xattr);
  1136. return ERR_PTR(err);
  1137. }
  1138. static bool xattr_is_trusted(const char *name)
  1139. {
  1140. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  1141. }
  1142. static bool xattr_is_maclabel(const char *name)
  1143. {
  1144. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  1145. return !strncmp(name, XATTR_SECURITY_PREFIX,
  1146. XATTR_SECURITY_PREFIX_LEN) &&
  1147. security_ismaclabel(suffix);
  1148. }
  1149. /**
  1150. * simple_xattr_list - list all xattr objects
  1151. * @inode: inode from which to get the xattrs
  1152. * @xattrs: the header of the xattr object
  1153. * @buffer: the buffer to store all xattrs into
  1154. * @size: the size of @buffer
  1155. *
  1156. * List all xattrs associated with @inode. If @buffer is NULL we returned
  1157. * the required size of the buffer. If @buffer is provided we store the
  1158. * xattrs value into it provided it is big enough.
  1159. *
  1160. * Note, the number of xattr names that can be listed with listxattr(2) is
  1161. * limited to XATTR_LIST_MAX aka 65536 bytes. If a larger buffer is passed
  1162. * then vfs_listxattr() caps it to XATTR_LIST_MAX and if more xattr names
  1163. * are found it will return -E2BIG.
  1164. *
  1165. * Return: On success the required size or the size of the copied xattrs is
  1166. * returned. On error a negative error code is returned.
  1167. */
  1168. ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
  1169. char *buffer, size_t size)
  1170. {
  1171. bool trusted = ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
  1172. struct simple_xattr *xattr;
  1173. struct rb_node *rbp;
  1174. ssize_t remaining_size = size;
  1175. int err = 0;
  1176. err = posix_acl_listxattr(inode, &buffer, &remaining_size);
  1177. if (err)
  1178. return err;
  1179. err = security_inode_listsecurity(inode, buffer, remaining_size);
  1180. if (err < 0)
  1181. return err;
  1182. if (buffer) {
  1183. if (remaining_size < err)
  1184. return -ERANGE;
  1185. buffer += err;
  1186. }
  1187. remaining_size -= err;
  1188. err = 0;
  1189. read_lock(&xattrs->lock);
  1190. for (rbp = rb_first(&xattrs->rb_root); rbp; rbp = rb_next(rbp)) {
  1191. xattr = rb_entry(rbp, struct simple_xattr, rb_node);
  1192. /* skip "trusted." attributes for unprivileged callers */
  1193. if (!trusted && xattr_is_trusted(xattr->name))
  1194. continue;
  1195. /* skip MAC labels; these are provided by LSM above */
  1196. if (xattr_is_maclabel(xattr->name))
  1197. continue;
  1198. err = xattr_list_one(&buffer, &remaining_size, xattr->name);
  1199. if (err)
  1200. break;
  1201. }
  1202. read_unlock(&xattrs->lock);
  1203. return err ? err : size - remaining_size;
  1204. }
  1205. /**
  1206. * rbtree_simple_xattr_less - compare two xattr rbtree nodes
  1207. * @new_node: new node
  1208. * @node: current node
  1209. *
  1210. * Compare the xattr attached to @new_node with the xattr attached to @node.
  1211. * Note that this function technically tolerates duplicate entries.
  1212. *
  1213. * Return: True if insertion point in the rbtree is found.
  1214. */
  1215. static bool rbtree_simple_xattr_less(struct rb_node *new_node,
  1216. const struct rb_node *node)
  1217. {
  1218. return rbtree_simple_xattr_node_cmp(new_node, node) < 0;
  1219. }
  1220. /**
  1221. * simple_xattr_add - add xattr objects
  1222. * @xattrs: the header of the xattr object
  1223. * @new_xattr: the xattr object to add
  1224. *
  1225. * Add an xattr object to @xattrs. This assumes no replacement or removal
  1226. * of matching xattrs is wanted. Should only be called during inode
  1227. * initialization when a few distinct initial xattrs are supposed to be set.
  1228. */
  1229. void simple_xattr_add(struct simple_xattrs *xattrs,
  1230. struct simple_xattr *new_xattr)
  1231. {
  1232. write_lock(&xattrs->lock);
  1233. rb_add(&new_xattr->rb_node, &xattrs->rb_root, rbtree_simple_xattr_less);
  1234. write_unlock(&xattrs->lock);
  1235. }
  1236. /**
  1237. * simple_xattrs_init - initialize new xattr header
  1238. * @xattrs: header to initialize
  1239. *
  1240. * Initialize relevant fields of a an xattr header.
  1241. */
  1242. void simple_xattrs_init(struct simple_xattrs *xattrs)
  1243. {
  1244. xattrs->rb_root = RB_ROOT;
  1245. rwlock_init(&xattrs->lock);
  1246. }
  1247. /**
  1248. * simple_xattrs_free - free xattrs
  1249. * @xattrs: xattr header whose xattrs to destroy
  1250. * @freed_space: approximate number of bytes of memory freed from @xattrs
  1251. *
  1252. * Destroy all xattrs in @xattr. When this is called no one can hold a
  1253. * reference to any of the xattrs anymore.
  1254. */
  1255. void simple_xattrs_free(struct simple_xattrs *xattrs, size_t *freed_space)
  1256. {
  1257. struct rb_node *rbp;
  1258. if (freed_space)
  1259. *freed_space = 0;
  1260. rbp = rb_first(&xattrs->rb_root);
  1261. while (rbp) {
  1262. struct simple_xattr *xattr;
  1263. struct rb_node *rbp_next;
  1264. rbp_next = rb_next(rbp);
  1265. xattr = rb_entry(rbp, struct simple_xattr, rb_node);
  1266. rb_erase(&xattr->rb_node, &xattrs->rb_root);
  1267. if (freed_space)
  1268. *freed_space += simple_xattr_space(xattr->name,
  1269. xattr->size);
  1270. simple_xattr_free(xattr);
  1271. rbp = rbp_next;
  1272. }
  1273. }