xattr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. 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, 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. 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, 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. for ((handler) = *(handlers)++; \
  575. (handler) != NULL; \
  576. (handler) = *(handlers)++)
  577. /*
  578. * Find the xattr_handler with the matching prefix.
  579. */
  580. static const struct xattr_handler *
  581. xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
  582. {
  583. const struct xattr_handler *handler;
  584. if (!*name)
  585. return NULL;
  586. for_each_xattr_handler(handlers, handler) {
  587. const char *n;
  588. n = strcmp_prefix(*name, xattr_prefix(handler));
  589. if (n) {
  590. if (!handler->prefix ^ !*n) {
  591. if (*n)
  592. continue;
  593. return ERR_PTR(-EINVAL);
  594. }
  595. *name = n;
  596. return handler;
  597. }
  598. }
  599. return ERR_PTR(-EOPNOTSUPP);
  600. }
  601. /*
  602. * Find the handler for the prefix and dispatch its get() operation.
  603. */
  604. ssize_t
  605. generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
  606. {
  607. const struct xattr_handler *handler;
  608. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  609. if (IS_ERR(handler))
  610. return PTR_ERR(handler);
  611. return handler->get(handler, dentry, name, buffer, size);
  612. }
  613. /*
  614. * Combine the results of the list() operation from every xattr_handler in the
  615. * list.
  616. */
  617. ssize_t
  618. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  619. {
  620. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  621. unsigned int size = 0;
  622. if (!buffer) {
  623. for_each_xattr_handler(handlers, handler) {
  624. if (!handler->name ||
  625. (handler->list && !handler->list(dentry)))
  626. continue;
  627. size += strlen(handler->name) + 1;
  628. }
  629. } else {
  630. char *buf = buffer;
  631. size_t len;
  632. for_each_xattr_handler(handlers, handler) {
  633. if (!handler->name ||
  634. (handler->list && !handler->list(dentry)))
  635. continue;
  636. len = strlen(handler->name);
  637. if (len + 1 > buffer_size)
  638. return -ERANGE;
  639. memcpy(buf, handler->name, len + 1);
  640. buf += len + 1;
  641. buffer_size -= len + 1;
  642. }
  643. size = buf - buffer;
  644. }
  645. return size;
  646. }
  647. /*
  648. * Find the handler for the prefix and dispatch its set() operation.
  649. */
  650. int
  651. generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
  652. {
  653. const struct xattr_handler *handler;
  654. if (size == 0)
  655. value = ""; /* empty EA, do not remove */
  656. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  657. if (IS_ERR(handler))
  658. return PTR_ERR(handler);
  659. return handler->set(handler, dentry, name, value, size, flags);
  660. }
  661. /*
  662. * Find the handler for the prefix and dispatch its set() operation to remove
  663. * any associated extended attribute.
  664. */
  665. int
  666. generic_removexattr(struct dentry *dentry, const char *name)
  667. {
  668. const struct xattr_handler *handler;
  669. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  670. if (IS_ERR(handler))
  671. return PTR_ERR(handler);
  672. return handler->set(handler, dentry, name, NULL, 0, XATTR_REPLACE);
  673. }
  674. EXPORT_SYMBOL(generic_getxattr);
  675. EXPORT_SYMBOL(generic_listxattr);
  676. EXPORT_SYMBOL(generic_setxattr);
  677. EXPORT_SYMBOL(generic_removexattr);
  678. /**
  679. * xattr_full_name - Compute full attribute name from suffix
  680. *
  681. * @handler: handler of the xattr_handler operation
  682. * @name: name passed to the xattr_handler operation
  683. *
  684. * The get and set xattr handler operations are called with the remainder of
  685. * the attribute name after skipping the handler's prefix: for example, "foo"
  686. * is passed to the get operation of a handler with prefix "user." to get
  687. * attribute "user.foo". The full name is still "there" in the name though.
  688. *
  689. * Note: the list xattr handler operation when called from the vfs is passed a
  690. * NULL name; some file systems use this operation internally, with varying
  691. * semantics.
  692. */
  693. const char *xattr_full_name(const struct xattr_handler *handler,
  694. const char *name)
  695. {
  696. size_t prefix_len = strlen(xattr_prefix(handler));
  697. return name - prefix_len;
  698. }
  699. EXPORT_SYMBOL(xattr_full_name);
  700. /*
  701. * Allocate new xattr and copy in the value; but leave the name to callers.
  702. */
  703. struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
  704. {
  705. struct simple_xattr *new_xattr;
  706. size_t len;
  707. /* wrap around? */
  708. len = sizeof(*new_xattr) + size;
  709. if (len < sizeof(*new_xattr))
  710. return NULL;
  711. new_xattr = kmalloc(len, GFP_KERNEL);
  712. if (!new_xattr)
  713. return NULL;
  714. new_xattr->size = size;
  715. memcpy(new_xattr->value, value, size);
  716. return new_xattr;
  717. }
  718. /*
  719. * xattr GET operation for in-memory/pseudo filesystems
  720. */
  721. int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
  722. void *buffer, size_t size)
  723. {
  724. struct simple_xattr *xattr;
  725. int ret = -ENODATA;
  726. spin_lock(&xattrs->lock);
  727. list_for_each_entry(xattr, &xattrs->head, list) {
  728. if (strcmp(name, xattr->name))
  729. continue;
  730. ret = xattr->size;
  731. if (buffer) {
  732. if (size < xattr->size)
  733. ret = -ERANGE;
  734. else
  735. memcpy(buffer, xattr->value, xattr->size);
  736. }
  737. break;
  738. }
  739. spin_unlock(&xattrs->lock);
  740. return ret;
  741. }
  742. /**
  743. * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
  744. * @xattrs: target simple_xattr list
  745. * @name: name of the extended attribute
  746. * @value: value of the xattr. If %NULL, will remove the attribute.
  747. * @size: size of the new xattr
  748. * @flags: %XATTR_{CREATE|REPLACE}
  749. *
  750. * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
  751. * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
  752. * otherwise, fails with -ENODATA.
  753. *
  754. * Returns 0 on success, -errno on failure.
  755. */
  756. int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
  757. const void *value, size_t size, int flags)
  758. {
  759. struct simple_xattr *xattr;
  760. struct simple_xattr *new_xattr = NULL;
  761. int err = 0;
  762. /* value == NULL means remove */
  763. if (value) {
  764. new_xattr = simple_xattr_alloc(value, size);
  765. if (!new_xattr)
  766. return -ENOMEM;
  767. new_xattr->name = kstrdup(name, GFP_KERNEL);
  768. if (!new_xattr->name) {
  769. kfree(new_xattr);
  770. return -ENOMEM;
  771. }
  772. }
  773. spin_lock(&xattrs->lock);
  774. list_for_each_entry(xattr, &xattrs->head, list) {
  775. if (!strcmp(name, xattr->name)) {
  776. if (flags & XATTR_CREATE) {
  777. xattr = new_xattr;
  778. err = -EEXIST;
  779. } else if (new_xattr) {
  780. list_replace(&xattr->list, &new_xattr->list);
  781. } else {
  782. list_del(&xattr->list);
  783. }
  784. goto out;
  785. }
  786. }
  787. if (flags & XATTR_REPLACE) {
  788. xattr = new_xattr;
  789. err = -ENODATA;
  790. } else {
  791. list_add(&new_xattr->list, &xattrs->head);
  792. xattr = NULL;
  793. }
  794. out:
  795. spin_unlock(&xattrs->lock);
  796. if (xattr) {
  797. kfree(xattr->name);
  798. kfree(xattr);
  799. }
  800. return err;
  801. }
  802. static bool xattr_is_trusted(const char *name)
  803. {
  804. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  805. }
  806. static int xattr_list_one(char **buffer, ssize_t *remaining_size,
  807. const char *name)
  808. {
  809. size_t len = strlen(name) + 1;
  810. if (*buffer) {
  811. if (*remaining_size < len)
  812. return -ERANGE;
  813. memcpy(*buffer, name, len);
  814. *buffer += len;
  815. }
  816. *remaining_size -= len;
  817. return 0;
  818. }
  819. /*
  820. * xattr LIST operation for in-memory/pseudo filesystems
  821. */
  822. ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
  823. char *buffer, size_t size)
  824. {
  825. bool trusted = capable(CAP_SYS_ADMIN);
  826. struct simple_xattr *xattr;
  827. ssize_t remaining_size = size;
  828. int err;
  829. #ifdef CONFIG_FS_POSIX_ACL
  830. if (inode->i_acl) {
  831. err = xattr_list_one(&buffer, &remaining_size,
  832. XATTR_NAME_POSIX_ACL_ACCESS);
  833. if (err)
  834. return err;
  835. }
  836. if (inode->i_default_acl) {
  837. err = xattr_list_one(&buffer, &remaining_size,
  838. XATTR_NAME_POSIX_ACL_DEFAULT);
  839. if (err)
  840. return err;
  841. }
  842. #endif
  843. spin_lock(&xattrs->lock);
  844. list_for_each_entry(xattr, &xattrs->head, list) {
  845. /* skip "trusted." attributes for unprivileged callers */
  846. if (!trusted && xattr_is_trusted(xattr->name))
  847. continue;
  848. err = xattr_list_one(&buffer, &remaining_size, xattr->name);
  849. if (err)
  850. return err;
  851. }
  852. spin_unlock(&xattrs->lock);
  853. return size - remaining_size;
  854. }
  855. /*
  856. * Adds an extended attribute to the list
  857. */
  858. void simple_xattr_list_add(struct simple_xattrs *xattrs,
  859. struct simple_xattr *new_xattr)
  860. {
  861. spin_lock(&xattrs->lock);
  862. list_add(&new_xattr->list, &xattrs->head);
  863. spin_unlock(&xattrs->lock);
  864. }