xattr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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, inode, 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. inode_lock(inode);
  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. inode_unlock(inode);
  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, inode, 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, inode, name, value, error);
  181. *xattr_value = value;
  182. return error;
  183. }
  184. ssize_t
  185. vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
  186. {
  187. struct inode *inode = dentry->d_inode;
  188. int error;
  189. error = xattr_permission(inode, name, MAY_READ);
  190. if (error)
  191. return error;
  192. error = security_inode_getxattr(dentry, name);
  193. if (error)
  194. return error;
  195. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  196. XATTR_SECURITY_PREFIX_LEN)) {
  197. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  198. int ret = xattr_getsecurity(inode, suffix, value, size);
  199. /*
  200. * Only overwrite the return value if a security module
  201. * is actually active.
  202. */
  203. if (ret == -EOPNOTSUPP)
  204. goto nolsm;
  205. return ret;
  206. }
  207. nolsm:
  208. if (inode->i_op->getxattr)
  209. error = inode->i_op->getxattr(dentry, inode, name, value, size);
  210. else
  211. error = -EOPNOTSUPP;
  212. return error;
  213. }
  214. EXPORT_SYMBOL_GPL(vfs_getxattr);
  215. ssize_t
  216. vfs_listxattr(struct dentry *d, char *list, size_t size)
  217. {
  218. ssize_t error;
  219. error = security_inode_listxattr(d);
  220. if (error)
  221. return error;
  222. error = -EOPNOTSUPP;
  223. if (d->d_inode->i_op->listxattr) {
  224. error = d->d_inode->i_op->listxattr(d, list, size);
  225. } else {
  226. error = security_inode_listsecurity(d->d_inode, list, size);
  227. if (size && error > size)
  228. error = -ERANGE;
  229. }
  230. return error;
  231. }
  232. EXPORT_SYMBOL_GPL(vfs_listxattr);
  233. int
  234. vfs_removexattr(struct dentry *dentry, const char *name)
  235. {
  236. struct inode *inode = dentry->d_inode;
  237. int error;
  238. if (!inode->i_op->removexattr)
  239. return -EOPNOTSUPP;
  240. error = xattr_permission(inode, name, MAY_WRITE);
  241. if (error)
  242. return error;
  243. inode_lock(inode);
  244. error = security_inode_removexattr(dentry, name);
  245. if (error)
  246. goto out;
  247. error = inode->i_op->removexattr(dentry, name);
  248. if (!error) {
  249. fsnotify_xattr(dentry);
  250. evm_inode_post_removexattr(dentry, name);
  251. }
  252. out:
  253. inode_unlock(inode);
  254. return error;
  255. }
  256. EXPORT_SYMBOL_GPL(vfs_removexattr);
  257. /*
  258. * Extended attribute SET operations
  259. */
  260. static long
  261. setxattr(struct dentry *d, const char __user *name, const void __user *value,
  262. size_t size, int flags)
  263. {
  264. int error;
  265. void *kvalue = NULL;
  266. char kname[XATTR_NAME_MAX + 1];
  267. if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
  268. return -EINVAL;
  269. error = strncpy_from_user(kname, name, sizeof(kname));
  270. if (error == 0 || error == sizeof(kname))
  271. error = -ERANGE;
  272. if (error < 0)
  273. return error;
  274. if (size) {
  275. if (size > XATTR_SIZE_MAX)
  276. return -E2BIG;
  277. kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
  278. if (!kvalue) {
  279. kvalue = vmalloc(size);
  280. if (!kvalue)
  281. return -ENOMEM;
  282. }
  283. if (copy_from_user(kvalue, value, size)) {
  284. error = -EFAULT;
  285. goto out;
  286. }
  287. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  288. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  289. posix_acl_fix_xattr_from_user(kvalue, size);
  290. }
  291. error = vfs_setxattr(d, kname, kvalue, size, flags);
  292. out:
  293. kvfree(kvalue);
  294. return error;
  295. }
  296. static int path_setxattr(const char __user *pathname,
  297. const char __user *name, const void __user *value,
  298. size_t size, int flags, unsigned int lookup_flags)
  299. {
  300. struct path path;
  301. int error;
  302. retry:
  303. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  304. if (error)
  305. return error;
  306. error = mnt_want_write(path.mnt);
  307. if (!error) {
  308. error = setxattr(path.dentry, name, value, size, flags);
  309. mnt_drop_write(path.mnt);
  310. }
  311. path_put(&path);
  312. if (retry_estale(error, lookup_flags)) {
  313. lookup_flags |= LOOKUP_REVAL;
  314. goto retry;
  315. }
  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. return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
  323. }
  324. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  325. const char __user *, name, const void __user *, value,
  326. size_t, size, int, flags)
  327. {
  328. return path_setxattr(pathname, name, value, size, flags, 0);
  329. }
  330. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  331. const void __user *,value, size_t, size, int, flags)
  332. {
  333. struct fd f = fdget(fd);
  334. int error = -EBADF;
  335. if (!f.file)
  336. return error;
  337. audit_file(f.file);
  338. error = mnt_want_write_file(f.file);
  339. if (!error) {
  340. error = setxattr(f.file->f_path.dentry, name, value, size, flags);
  341. mnt_drop_write_file(f.file);
  342. }
  343. fdput(f);
  344. return error;
  345. }
  346. /*
  347. * Extended attribute GET operations
  348. */
  349. static ssize_t
  350. getxattr(struct dentry *d, const char __user *name, void __user *value,
  351. size_t size)
  352. {
  353. ssize_t error;
  354. void *kvalue = NULL;
  355. char kname[XATTR_NAME_MAX + 1];
  356. error = strncpy_from_user(kname, name, sizeof(kname));
  357. if (error == 0 || error == sizeof(kname))
  358. error = -ERANGE;
  359. if (error < 0)
  360. return error;
  361. if (size) {
  362. if (size > XATTR_SIZE_MAX)
  363. size = XATTR_SIZE_MAX;
  364. kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  365. if (!kvalue) {
  366. kvalue = vmalloc(size);
  367. if (!kvalue)
  368. return -ENOMEM;
  369. }
  370. }
  371. error = vfs_getxattr(d, kname, kvalue, size);
  372. if (error > 0) {
  373. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  374. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  375. posix_acl_fix_xattr_to_user(kvalue, size);
  376. if (size && copy_to_user(value, kvalue, error))
  377. error = -EFAULT;
  378. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  379. /* The file system tried to returned a value bigger
  380. than XATTR_SIZE_MAX bytes. Not possible. */
  381. error = -E2BIG;
  382. }
  383. kvfree(kvalue);
  384. return error;
  385. }
  386. static ssize_t path_getxattr(const char __user *pathname,
  387. const char __user *name, void __user *value,
  388. size_t size, unsigned int lookup_flags)
  389. {
  390. struct path path;
  391. ssize_t error;
  392. retry:
  393. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  394. if (error)
  395. return error;
  396. error = getxattr(path.dentry, name, value, size);
  397. path_put(&path);
  398. if (retry_estale(error, lookup_flags)) {
  399. lookup_flags |= LOOKUP_REVAL;
  400. goto retry;
  401. }
  402. return error;
  403. }
  404. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  405. const char __user *, name, void __user *, value, size_t, size)
  406. {
  407. return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
  408. }
  409. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  410. const char __user *, name, void __user *, value, size_t, size)
  411. {
  412. return path_getxattr(pathname, name, value, size, 0);
  413. }
  414. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  415. void __user *, value, size_t, size)
  416. {
  417. struct fd f = fdget(fd);
  418. ssize_t error = -EBADF;
  419. if (!f.file)
  420. return error;
  421. audit_file(f.file);
  422. error = getxattr(f.file->f_path.dentry, name, value, size);
  423. fdput(f);
  424. return error;
  425. }
  426. /*
  427. * Extended attribute LIST operations
  428. */
  429. static ssize_t
  430. listxattr(struct dentry *d, char __user *list, size_t size)
  431. {
  432. ssize_t error;
  433. char *klist = NULL;
  434. if (size) {
  435. if (size > XATTR_LIST_MAX)
  436. size = XATTR_LIST_MAX;
  437. klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
  438. if (!klist) {
  439. klist = vmalloc(size);
  440. if (!klist)
  441. return -ENOMEM;
  442. }
  443. }
  444. error = vfs_listxattr(d, klist, size);
  445. if (error > 0) {
  446. if (size && copy_to_user(list, klist, error))
  447. error = -EFAULT;
  448. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  449. /* The file system tried to returned a list bigger
  450. than XATTR_LIST_MAX bytes. Not possible. */
  451. error = -E2BIG;
  452. }
  453. kvfree(klist);
  454. return error;
  455. }
  456. static ssize_t path_listxattr(const char __user *pathname, char __user *list,
  457. size_t size, unsigned int lookup_flags)
  458. {
  459. struct path path;
  460. ssize_t error;
  461. retry:
  462. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  463. if (error)
  464. return error;
  465. error = listxattr(path.dentry, list, size);
  466. path_put(&path);
  467. if (retry_estale(error, lookup_flags)) {
  468. lookup_flags |= LOOKUP_REVAL;
  469. goto retry;
  470. }
  471. return error;
  472. }
  473. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  474. size_t, size)
  475. {
  476. return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
  477. }
  478. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  479. size_t, size)
  480. {
  481. return path_listxattr(pathname, list, size, 0);
  482. }
  483. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  484. {
  485. struct fd f = fdget(fd);
  486. ssize_t error = -EBADF;
  487. if (!f.file)
  488. return error;
  489. audit_file(f.file);
  490. error = listxattr(f.file->f_path.dentry, list, size);
  491. fdput(f);
  492. return error;
  493. }
  494. /*
  495. * Extended attribute REMOVE operations
  496. */
  497. static long
  498. removexattr(struct dentry *d, const char __user *name)
  499. {
  500. int error;
  501. char kname[XATTR_NAME_MAX + 1];
  502. error = strncpy_from_user(kname, name, sizeof(kname));
  503. if (error == 0 || error == sizeof(kname))
  504. error = -ERANGE;
  505. if (error < 0)
  506. return error;
  507. return vfs_removexattr(d, kname);
  508. }
  509. static int path_removexattr(const char __user *pathname,
  510. const char __user *name, unsigned int lookup_flags)
  511. {
  512. struct path path;
  513. int error;
  514. retry:
  515. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  516. if (error)
  517. return error;
  518. error = mnt_want_write(path.mnt);
  519. if (!error) {
  520. error = removexattr(path.dentry, name);
  521. mnt_drop_write(path.mnt);
  522. }
  523. path_put(&path);
  524. if (retry_estale(error, lookup_flags)) {
  525. lookup_flags |= LOOKUP_REVAL;
  526. goto retry;
  527. }
  528. return error;
  529. }
  530. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  531. const char __user *, name)
  532. {
  533. return path_removexattr(pathname, name, LOOKUP_FOLLOW);
  534. }
  535. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  536. const char __user *, name)
  537. {
  538. return path_removexattr(pathname, name, 0);
  539. }
  540. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  541. {
  542. struct fd f = fdget(fd);
  543. int error = -EBADF;
  544. if (!f.file)
  545. return error;
  546. audit_file(f.file);
  547. error = mnt_want_write_file(f.file);
  548. if (!error) {
  549. error = removexattr(f.file->f_path.dentry, name);
  550. mnt_drop_write_file(f.file);
  551. }
  552. fdput(f);
  553. return error;
  554. }
  555. static const char *
  556. strcmp_prefix(const char *a, const char *a_prefix)
  557. {
  558. while (*a_prefix && *a == *a_prefix) {
  559. a++;
  560. a_prefix++;
  561. }
  562. return *a_prefix ? NULL : a;
  563. }
  564. /*
  565. * In order to implement different sets of xattr operations for each xattr
  566. * prefix with the generic xattr API, a filesystem should create a
  567. * null-terminated array of struct xattr_handler (one for each prefix) and
  568. * hang a pointer to it off of the s_xattr field of the superblock.
  569. *
  570. * The generic_fooxattr() functions will use this list to dispatch xattr
  571. * operations to the correct xattr_handler.
  572. */
  573. #define for_each_xattr_handler(handlers, handler) \
  574. if (handlers) \
  575. for ((handler) = *(handlers)++; \
  576. (handler) != NULL; \
  577. (handler) = *(handlers)++)
  578. /*
  579. * Find the xattr_handler with the matching prefix.
  580. */
  581. static const struct xattr_handler *
  582. xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
  583. {
  584. const struct xattr_handler *handler;
  585. if (!*name)
  586. return ERR_PTR(-EINVAL);
  587. for_each_xattr_handler(handlers, handler) {
  588. const char *n;
  589. n = strcmp_prefix(*name, xattr_prefix(handler));
  590. if (n) {
  591. if (!handler->prefix ^ !*n) {
  592. if (*n)
  593. continue;
  594. return ERR_PTR(-EINVAL);
  595. }
  596. *name = n;
  597. return handler;
  598. }
  599. }
  600. return ERR_PTR(-EOPNOTSUPP);
  601. }
  602. /*
  603. * Find the handler for the prefix and dispatch its get() operation.
  604. */
  605. ssize_t
  606. generic_getxattr(struct dentry *dentry, struct inode *inode,
  607. const char *name, void *buffer, size_t size)
  608. {
  609. const struct xattr_handler *handler;
  610. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  611. if (IS_ERR(handler))
  612. return PTR_ERR(handler);
  613. return handler->get(handler, dentry, inode,
  614. name, buffer, size);
  615. }
  616. /*
  617. * Combine the results of the list() operation from every xattr_handler in the
  618. * list.
  619. */
  620. ssize_t
  621. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  622. {
  623. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  624. unsigned int size = 0;
  625. if (!buffer) {
  626. for_each_xattr_handler(handlers, handler) {
  627. if (!handler->name ||
  628. (handler->list && !handler->list(dentry)))
  629. continue;
  630. size += strlen(handler->name) + 1;
  631. }
  632. } else {
  633. char *buf = buffer;
  634. size_t len;
  635. for_each_xattr_handler(handlers, handler) {
  636. if (!handler->name ||
  637. (handler->list && !handler->list(dentry)))
  638. continue;
  639. len = strlen(handler->name);
  640. if (len + 1 > buffer_size)
  641. return -ERANGE;
  642. memcpy(buf, handler->name, len + 1);
  643. buf += len + 1;
  644. buffer_size -= len + 1;
  645. }
  646. size = buf - buffer;
  647. }
  648. return size;
  649. }
  650. /*
  651. * Find the handler for the prefix and dispatch its set() operation.
  652. */
  653. int
  654. generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
  655. const void *value, size_t size, int flags)
  656. {
  657. const struct xattr_handler *handler;
  658. if (size == 0)
  659. value = ""; /* empty EA, do not remove */
  660. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  661. if (IS_ERR(handler))
  662. return PTR_ERR(handler);
  663. return handler->set(handler, dentry, inode, name, value, size, flags);
  664. }
  665. /*
  666. * Find the handler for the prefix and dispatch its set() operation to remove
  667. * any associated extended attribute.
  668. */
  669. int
  670. generic_removexattr(struct dentry *dentry, const char *name)
  671. {
  672. const struct xattr_handler *handler;
  673. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  674. if (IS_ERR(handler))
  675. return PTR_ERR(handler);
  676. return handler->set(handler, dentry, d_inode(dentry), name, NULL,
  677. 0, XATTR_REPLACE);
  678. }
  679. EXPORT_SYMBOL(generic_getxattr);
  680. EXPORT_SYMBOL(generic_listxattr);
  681. EXPORT_SYMBOL(generic_setxattr);
  682. EXPORT_SYMBOL(generic_removexattr);
  683. /**
  684. * xattr_full_name - Compute full attribute name from suffix
  685. *
  686. * @handler: handler of the xattr_handler operation
  687. * @name: name passed to the xattr_handler operation
  688. *
  689. * The get and set xattr handler operations are called with the remainder of
  690. * the attribute name after skipping the handler's prefix: for example, "foo"
  691. * is passed to the get operation of a handler with prefix "user." to get
  692. * attribute "user.foo". The full name is still "there" in the name though.
  693. *
  694. * Note: the list xattr handler operation when called from the vfs is passed a
  695. * NULL name; some file systems use this operation internally, with varying
  696. * semantics.
  697. */
  698. const char *xattr_full_name(const struct xattr_handler *handler,
  699. const char *name)
  700. {
  701. size_t prefix_len = strlen(xattr_prefix(handler));
  702. return name - prefix_len;
  703. }
  704. EXPORT_SYMBOL(xattr_full_name);
  705. /*
  706. * Allocate new xattr and copy in the value; but leave the name to callers.
  707. */
  708. struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
  709. {
  710. struct simple_xattr *new_xattr;
  711. size_t len;
  712. /* wrap around? */
  713. len = sizeof(*new_xattr) + size;
  714. if (len < sizeof(*new_xattr))
  715. return NULL;
  716. new_xattr = kmalloc(len, GFP_KERNEL);
  717. if (!new_xattr)
  718. return NULL;
  719. new_xattr->size = size;
  720. memcpy(new_xattr->value, value, size);
  721. return new_xattr;
  722. }
  723. /*
  724. * xattr GET operation for in-memory/pseudo filesystems
  725. */
  726. int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
  727. void *buffer, size_t size)
  728. {
  729. struct simple_xattr *xattr;
  730. int ret = -ENODATA;
  731. spin_lock(&xattrs->lock);
  732. list_for_each_entry(xattr, &xattrs->head, list) {
  733. if (strcmp(name, xattr->name))
  734. continue;
  735. ret = xattr->size;
  736. if (buffer) {
  737. if (size < xattr->size)
  738. ret = -ERANGE;
  739. else
  740. memcpy(buffer, xattr->value, xattr->size);
  741. }
  742. break;
  743. }
  744. spin_unlock(&xattrs->lock);
  745. return ret;
  746. }
  747. /**
  748. * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
  749. * @xattrs: target simple_xattr list
  750. * @name: name of the extended attribute
  751. * @value: value of the xattr. If %NULL, will remove the attribute.
  752. * @size: size of the new xattr
  753. * @flags: %XATTR_{CREATE|REPLACE}
  754. *
  755. * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
  756. * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
  757. * otherwise, fails with -ENODATA.
  758. *
  759. * Returns 0 on success, -errno on failure.
  760. */
  761. int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
  762. const void *value, size_t size, int flags)
  763. {
  764. struct simple_xattr *xattr;
  765. struct simple_xattr *new_xattr = NULL;
  766. int err = 0;
  767. /* value == NULL means remove */
  768. if (value) {
  769. new_xattr = simple_xattr_alloc(value, size);
  770. if (!new_xattr)
  771. return -ENOMEM;
  772. new_xattr->name = kstrdup(name, GFP_KERNEL);
  773. if (!new_xattr->name) {
  774. kfree(new_xattr);
  775. return -ENOMEM;
  776. }
  777. }
  778. spin_lock(&xattrs->lock);
  779. list_for_each_entry(xattr, &xattrs->head, list) {
  780. if (!strcmp(name, xattr->name)) {
  781. if (flags & XATTR_CREATE) {
  782. xattr = new_xattr;
  783. err = -EEXIST;
  784. } else if (new_xattr) {
  785. list_replace(&xattr->list, &new_xattr->list);
  786. } else {
  787. list_del(&xattr->list);
  788. }
  789. goto out;
  790. }
  791. }
  792. if (flags & XATTR_REPLACE) {
  793. xattr = new_xattr;
  794. err = -ENODATA;
  795. } else {
  796. list_add(&new_xattr->list, &xattrs->head);
  797. xattr = NULL;
  798. }
  799. out:
  800. spin_unlock(&xattrs->lock);
  801. if (xattr) {
  802. kfree(xattr->name);
  803. kfree(xattr);
  804. }
  805. return err;
  806. }
  807. static bool xattr_is_trusted(const char *name)
  808. {
  809. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  810. }
  811. static int xattr_list_one(char **buffer, ssize_t *remaining_size,
  812. const char *name)
  813. {
  814. size_t len = strlen(name) + 1;
  815. if (*buffer) {
  816. if (*remaining_size < len)
  817. return -ERANGE;
  818. memcpy(*buffer, name, len);
  819. *buffer += len;
  820. }
  821. *remaining_size -= len;
  822. return 0;
  823. }
  824. /*
  825. * xattr LIST operation for in-memory/pseudo filesystems
  826. */
  827. ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
  828. char *buffer, size_t size)
  829. {
  830. bool trusted = capable(CAP_SYS_ADMIN);
  831. struct simple_xattr *xattr;
  832. ssize_t remaining_size = size;
  833. int err = 0;
  834. #ifdef CONFIG_FS_POSIX_ACL
  835. if (inode->i_acl) {
  836. err = xattr_list_one(&buffer, &remaining_size,
  837. XATTR_NAME_POSIX_ACL_ACCESS);
  838. if (err)
  839. return err;
  840. }
  841. if (inode->i_default_acl) {
  842. err = xattr_list_one(&buffer, &remaining_size,
  843. XATTR_NAME_POSIX_ACL_DEFAULT);
  844. if (err)
  845. return err;
  846. }
  847. #endif
  848. spin_lock(&xattrs->lock);
  849. list_for_each_entry(xattr, &xattrs->head, list) {
  850. /* skip "trusted." attributes for unprivileged callers */
  851. if (!trusted && xattr_is_trusted(xattr->name))
  852. continue;
  853. err = xattr_list_one(&buffer, &remaining_size, xattr->name);
  854. if (err)
  855. break;
  856. }
  857. spin_unlock(&xattrs->lock);
  858. return err ? err : size - remaining_size;
  859. }
  860. /*
  861. * Adds an extended attribute to the list
  862. */
  863. void simple_xattr_list_add(struct simple_xattrs *xattrs,
  864. struct simple_xattr *new_xattr)
  865. {
  866. spin_lock(&xattrs->lock);
  867. list_add(&new_xattr->list, &xattrs->head);
  868. spin_unlock(&xattrs->lock);
  869. }