xattr.c 24 KB

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