xattr.c 24 KB

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