xattr.c 23 KB

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