xattr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/syscalls.h>
  16. #include <linux/module.h>
  17. #include <linux/fsnotify.h>
  18. #include <linux/audit.h>
  19. #include <asm/uaccess.h>
  20. /*
  21. * Check permissions for extended attribute access. This is a bit complicated
  22. * because different namespaces have very different rules.
  23. */
  24. static int
  25. xattr_permission(struct inode *inode, const char *name, int mask)
  26. {
  27. /*
  28. * We can never set or remove an extended attribute on a read-only
  29. * filesystem or on an immutable / append-only inode.
  30. */
  31. if (mask & MAY_WRITE) {
  32. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  33. return -EPERM;
  34. }
  35. /*
  36. * No restriction for security.* and system.* from the VFS. Decision
  37. * on these is left to the underlying filesystem / security module.
  38. */
  39. if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
  40. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  41. return 0;
  42. /*
  43. * The trusted.* namespace can only be accessed by privileged users.
  44. */
  45. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
  46. if (!capable(CAP_SYS_ADMIN))
  47. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  48. return 0;
  49. }
  50. /*
  51. * In the user.* namespace, only regular files and directories can have
  52. * extended attributes. For sticky directories, only the owner and
  53. * privileged users can write attributes.
  54. */
  55. if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
  56. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  57. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  58. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  59. (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
  60. return -EPERM;
  61. }
  62. return inode_permission(inode, mask);
  63. }
  64. /**
  65. * __vfs_setxattr_noperm - perform setxattr operation without performing
  66. * permission checks.
  67. *
  68. * @dentry - object to perform setxattr on
  69. * @name - xattr name to set
  70. * @value - value to set @name to
  71. * @size - size of @value
  72. * @flags - flags to pass into filesystem operations
  73. *
  74. * returns the result of the internal setxattr or setsecurity operations.
  75. *
  76. * This function requires the caller to lock the inode's i_mutex before it
  77. * is executed. It also assumes that the caller will make the appropriate
  78. * permission checks.
  79. */
  80. int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
  81. const void *value, size_t size, int flags)
  82. {
  83. struct inode *inode = dentry->d_inode;
  84. int error = -EOPNOTSUPP;
  85. if (inode->i_op->setxattr) {
  86. error = inode->i_op->setxattr(dentry, name, value, size, flags);
  87. if (!error) {
  88. fsnotify_xattr(dentry);
  89. security_inode_post_setxattr(dentry, name, value,
  90. size, flags);
  91. }
  92. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  93. XATTR_SECURITY_PREFIX_LEN)) {
  94. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  95. error = security_inode_setsecurity(inode, suffix, value,
  96. size, flags);
  97. if (!error)
  98. fsnotify_xattr(dentry);
  99. }
  100. return error;
  101. }
  102. int
  103. vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  104. size_t size, int flags)
  105. {
  106. struct inode *inode = dentry->d_inode;
  107. int error;
  108. error = xattr_permission(inode, name, MAY_WRITE);
  109. if (error)
  110. return error;
  111. mutex_lock(&inode->i_mutex);
  112. error = security_inode_setxattr(dentry, name, value, size, flags);
  113. if (error)
  114. goto out;
  115. error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
  116. out:
  117. mutex_unlock(&inode->i_mutex);
  118. return error;
  119. }
  120. EXPORT_SYMBOL_GPL(vfs_setxattr);
  121. ssize_t
  122. xattr_getsecurity(struct inode *inode, const char *name, void *value,
  123. size_t size)
  124. {
  125. void *buffer = NULL;
  126. ssize_t len;
  127. if (!value || !size) {
  128. len = security_inode_getsecurity(inode, name, &buffer, false);
  129. goto out_noalloc;
  130. }
  131. len = security_inode_getsecurity(inode, name, &buffer, true);
  132. if (len < 0)
  133. return len;
  134. if (size < len) {
  135. len = -ERANGE;
  136. goto out;
  137. }
  138. memcpy(value, buffer, len);
  139. out:
  140. security_release_secctx(buffer, len);
  141. out_noalloc:
  142. return len;
  143. }
  144. EXPORT_SYMBOL_GPL(xattr_getsecurity);
  145. ssize_t
  146. vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
  147. {
  148. struct inode *inode = dentry->d_inode;
  149. int error;
  150. error = xattr_permission(inode, name, MAY_READ);
  151. if (error)
  152. return error;
  153. error = security_inode_getxattr(dentry, name);
  154. if (error)
  155. return error;
  156. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  157. XATTR_SECURITY_PREFIX_LEN)) {
  158. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  159. int ret = xattr_getsecurity(inode, suffix, value, size);
  160. /*
  161. * Only overwrite the return value if a security module
  162. * is actually active.
  163. */
  164. if (ret == -EOPNOTSUPP)
  165. goto nolsm;
  166. return ret;
  167. }
  168. nolsm:
  169. if (inode->i_op->getxattr)
  170. error = inode->i_op->getxattr(dentry, name, value, size);
  171. else
  172. error = -EOPNOTSUPP;
  173. return error;
  174. }
  175. EXPORT_SYMBOL_GPL(vfs_getxattr);
  176. ssize_t
  177. vfs_listxattr(struct dentry *d, char *list, size_t size)
  178. {
  179. ssize_t error;
  180. error = security_inode_listxattr(d);
  181. if (error)
  182. return error;
  183. error = -EOPNOTSUPP;
  184. if (d->d_inode->i_op->listxattr) {
  185. error = d->d_inode->i_op->listxattr(d, list, size);
  186. } else {
  187. error = security_inode_listsecurity(d->d_inode, list, size);
  188. if (size && error > size)
  189. error = -ERANGE;
  190. }
  191. return error;
  192. }
  193. EXPORT_SYMBOL_GPL(vfs_listxattr);
  194. int
  195. vfs_removexattr(struct dentry *dentry, const char *name)
  196. {
  197. struct inode *inode = dentry->d_inode;
  198. int error;
  199. if (!inode->i_op->removexattr)
  200. return -EOPNOTSUPP;
  201. error = xattr_permission(inode, name, MAY_WRITE);
  202. if (error)
  203. return error;
  204. error = security_inode_removexattr(dentry, name);
  205. if (error)
  206. return error;
  207. mutex_lock(&inode->i_mutex);
  208. error = inode->i_op->removexattr(dentry, name);
  209. mutex_unlock(&inode->i_mutex);
  210. if (!error)
  211. fsnotify_xattr(dentry);
  212. return error;
  213. }
  214. EXPORT_SYMBOL_GPL(vfs_removexattr);
  215. /*
  216. * Extended attribute SET operations
  217. */
  218. static long
  219. setxattr(struct dentry *d, const char __user *name, const void __user *value,
  220. size_t size, int flags)
  221. {
  222. int error;
  223. void *kvalue = NULL;
  224. char kname[XATTR_NAME_MAX + 1];
  225. if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
  226. return -EINVAL;
  227. error = strncpy_from_user(kname, name, sizeof(kname));
  228. if (error == 0 || error == sizeof(kname))
  229. error = -ERANGE;
  230. if (error < 0)
  231. return error;
  232. if (size) {
  233. if (size > XATTR_SIZE_MAX)
  234. return -E2BIG;
  235. kvalue = memdup_user(value, size);
  236. if (IS_ERR(kvalue))
  237. return PTR_ERR(kvalue);
  238. }
  239. error = vfs_setxattr(d, kname, kvalue, size, flags);
  240. kfree(kvalue);
  241. return error;
  242. }
  243. SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
  244. const char __user *, name, const void __user *, value,
  245. size_t, size, int, flags)
  246. {
  247. struct path path;
  248. int error;
  249. error = user_path(pathname, &path);
  250. if (error)
  251. return error;
  252. error = mnt_want_write(path.mnt);
  253. if (!error) {
  254. error = setxattr(path.dentry, name, value, size, flags);
  255. mnt_drop_write(path.mnt);
  256. }
  257. path_put(&path);
  258. return error;
  259. }
  260. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  261. const char __user *, name, const void __user *, value,
  262. size_t, size, int, flags)
  263. {
  264. struct path path;
  265. int error;
  266. error = user_lpath(pathname, &path);
  267. if (error)
  268. return error;
  269. error = mnt_want_write(path.mnt);
  270. if (!error) {
  271. error = setxattr(path.dentry, name, value, size, flags);
  272. mnt_drop_write(path.mnt);
  273. }
  274. path_put(&path);
  275. return error;
  276. }
  277. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  278. const void __user *,value, size_t, size, int, flags)
  279. {
  280. struct file *f;
  281. struct dentry *dentry;
  282. int error = -EBADF;
  283. f = fget(fd);
  284. if (!f)
  285. return error;
  286. dentry = f->f_path.dentry;
  287. audit_inode(NULL, dentry);
  288. error = mnt_want_write_file(f);
  289. if (!error) {
  290. error = setxattr(dentry, name, value, size, flags);
  291. mnt_drop_write(f->f_path.mnt);
  292. }
  293. fput(f);
  294. return error;
  295. }
  296. /*
  297. * Extended attribute GET operations
  298. */
  299. static ssize_t
  300. getxattr(struct dentry *d, const char __user *name, void __user *value,
  301. size_t size)
  302. {
  303. ssize_t error;
  304. void *kvalue = NULL;
  305. char kname[XATTR_NAME_MAX + 1];
  306. error = strncpy_from_user(kname, name, sizeof(kname));
  307. if (error == 0 || error == sizeof(kname))
  308. error = -ERANGE;
  309. if (error < 0)
  310. return error;
  311. if (size) {
  312. if (size > XATTR_SIZE_MAX)
  313. size = XATTR_SIZE_MAX;
  314. kvalue = kzalloc(size, GFP_KERNEL);
  315. if (!kvalue)
  316. return -ENOMEM;
  317. }
  318. error = vfs_getxattr(d, kname, kvalue, size);
  319. if (error > 0) {
  320. if (size && copy_to_user(value, kvalue, error))
  321. error = -EFAULT;
  322. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  323. /* The file system tried to returned a value bigger
  324. than XATTR_SIZE_MAX bytes. Not possible. */
  325. error = -E2BIG;
  326. }
  327. kfree(kvalue);
  328. return error;
  329. }
  330. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  331. const char __user *, name, void __user *, value, size_t, size)
  332. {
  333. struct path path;
  334. ssize_t error;
  335. error = user_path(pathname, &path);
  336. if (error)
  337. return error;
  338. error = getxattr(path.dentry, name, value, size);
  339. path_put(&path);
  340. return error;
  341. }
  342. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  343. const char __user *, name, void __user *, value, size_t, size)
  344. {
  345. struct path path;
  346. ssize_t error;
  347. error = user_lpath(pathname, &path);
  348. if (error)
  349. return error;
  350. error = getxattr(path.dentry, name, value, size);
  351. path_put(&path);
  352. return error;
  353. }
  354. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  355. void __user *, value, size_t, size)
  356. {
  357. struct file *f;
  358. ssize_t error = -EBADF;
  359. f = fget(fd);
  360. if (!f)
  361. return error;
  362. audit_inode(NULL, f->f_path.dentry);
  363. error = getxattr(f->f_path.dentry, name, value, size);
  364. fput(f);
  365. return error;
  366. }
  367. /*
  368. * Extended attribute LIST operations
  369. */
  370. static ssize_t
  371. listxattr(struct dentry *d, char __user *list, size_t size)
  372. {
  373. ssize_t error;
  374. char *klist = NULL;
  375. if (size) {
  376. if (size > XATTR_LIST_MAX)
  377. size = XATTR_LIST_MAX;
  378. klist = kmalloc(size, GFP_KERNEL);
  379. if (!klist)
  380. return -ENOMEM;
  381. }
  382. error = vfs_listxattr(d, klist, size);
  383. if (error > 0) {
  384. if (size && copy_to_user(list, klist, error))
  385. error = -EFAULT;
  386. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  387. /* The file system tried to returned a list bigger
  388. than XATTR_LIST_MAX bytes. Not possible. */
  389. error = -E2BIG;
  390. }
  391. kfree(klist);
  392. return error;
  393. }
  394. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  395. size_t, size)
  396. {
  397. struct path path;
  398. ssize_t error;
  399. error = user_path(pathname, &path);
  400. if (error)
  401. return error;
  402. error = listxattr(path.dentry, list, size);
  403. path_put(&path);
  404. return error;
  405. }
  406. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  407. size_t, size)
  408. {
  409. struct path path;
  410. ssize_t error;
  411. error = user_lpath(pathname, &path);
  412. if (error)
  413. return error;
  414. error = listxattr(path.dentry, list, size);
  415. path_put(&path);
  416. return error;
  417. }
  418. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  419. {
  420. struct file *f;
  421. ssize_t error = -EBADF;
  422. f = fget(fd);
  423. if (!f)
  424. return error;
  425. audit_inode(NULL, f->f_path.dentry);
  426. error = listxattr(f->f_path.dentry, list, size);
  427. fput(f);
  428. return error;
  429. }
  430. /*
  431. * Extended attribute REMOVE operations
  432. */
  433. static long
  434. removexattr(struct dentry *d, const char __user *name)
  435. {
  436. int error;
  437. char kname[XATTR_NAME_MAX + 1];
  438. error = strncpy_from_user(kname, name, sizeof(kname));
  439. if (error == 0 || error == sizeof(kname))
  440. error = -ERANGE;
  441. if (error < 0)
  442. return error;
  443. return vfs_removexattr(d, kname);
  444. }
  445. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  446. const char __user *, name)
  447. {
  448. struct path path;
  449. int error;
  450. error = user_path(pathname, &path);
  451. if (error)
  452. return error;
  453. error = mnt_want_write(path.mnt);
  454. if (!error) {
  455. error = removexattr(path.dentry, name);
  456. mnt_drop_write(path.mnt);
  457. }
  458. path_put(&path);
  459. return error;
  460. }
  461. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  462. const char __user *, name)
  463. {
  464. struct path path;
  465. int error;
  466. error = user_lpath(pathname, &path);
  467. if (error)
  468. return error;
  469. error = mnt_want_write(path.mnt);
  470. if (!error) {
  471. error = removexattr(path.dentry, name);
  472. mnt_drop_write(path.mnt);
  473. }
  474. path_put(&path);
  475. return error;
  476. }
  477. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  478. {
  479. struct file *f;
  480. struct dentry *dentry;
  481. int error = -EBADF;
  482. f = fget(fd);
  483. if (!f)
  484. return error;
  485. dentry = f->f_path.dentry;
  486. audit_inode(NULL, dentry);
  487. error = mnt_want_write_file(f);
  488. if (!error) {
  489. error = removexattr(dentry, name);
  490. mnt_drop_write(f->f_path.mnt);
  491. }
  492. fput(f);
  493. return error;
  494. }
  495. static const char *
  496. strcmp_prefix(const char *a, const char *a_prefix)
  497. {
  498. while (*a_prefix && *a == *a_prefix) {
  499. a++;
  500. a_prefix++;
  501. }
  502. return *a_prefix ? NULL : a;
  503. }
  504. /*
  505. * In order to implement different sets of xattr operations for each xattr
  506. * prefix with the generic xattr API, a filesystem should create a
  507. * null-terminated array of struct xattr_handler (one for each prefix) and
  508. * hang a pointer to it off of the s_xattr field of the superblock.
  509. *
  510. * The generic_fooxattr() functions will use this list to dispatch xattr
  511. * operations to the correct xattr_handler.
  512. */
  513. #define for_each_xattr_handler(handlers, handler) \
  514. for ((handler) = *(handlers)++; \
  515. (handler) != NULL; \
  516. (handler) = *(handlers)++)
  517. /*
  518. * Find the xattr_handler with the matching prefix.
  519. */
  520. static const struct xattr_handler *
  521. xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
  522. {
  523. const struct xattr_handler *handler;
  524. if (!*name)
  525. return NULL;
  526. for_each_xattr_handler(handlers, handler) {
  527. const char *n = strcmp_prefix(*name, handler->prefix);
  528. if (n) {
  529. *name = n;
  530. break;
  531. }
  532. }
  533. return handler;
  534. }
  535. /*
  536. * Find the handler for the prefix and dispatch its get() operation.
  537. */
  538. ssize_t
  539. generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
  540. {
  541. const struct xattr_handler *handler;
  542. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  543. if (!handler)
  544. return -EOPNOTSUPP;
  545. return handler->get(dentry, name, buffer, size, handler->flags);
  546. }
  547. /*
  548. * Combine the results of the list() operation from every xattr_handler in the
  549. * list.
  550. */
  551. ssize_t
  552. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  553. {
  554. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  555. unsigned int size = 0;
  556. if (!buffer) {
  557. for_each_xattr_handler(handlers, handler) {
  558. size += handler->list(dentry, NULL, 0, NULL, 0,
  559. handler->flags);
  560. }
  561. } else {
  562. char *buf = buffer;
  563. for_each_xattr_handler(handlers, handler) {
  564. size = handler->list(dentry, buf, buffer_size,
  565. NULL, 0, handler->flags);
  566. if (size > buffer_size)
  567. return -ERANGE;
  568. buf += size;
  569. buffer_size -= size;
  570. }
  571. size = buf - buffer;
  572. }
  573. return size;
  574. }
  575. /*
  576. * Find the handler for the prefix and dispatch its set() operation.
  577. */
  578. int
  579. generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
  580. {
  581. const struct xattr_handler *handler;
  582. if (size == 0)
  583. value = ""; /* empty EA, do not remove */
  584. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  585. if (!handler)
  586. return -EOPNOTSUPP;
  587. return handler->set(dentry, name, value, size, flags, handler->flags);
  588. }
  589. /*
  590. * Find the handler for the prefix and dispatch its set() operation to remove
  591. * any associated extended attribute.
  592. */
  593. int
  594. generic_removexattr(struct dentry *dentry, const char *name)
  595. {
  596. const struct xattr_handler *handler;
  597. handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
  598. if (!handler)
  599. return -EOPNOTSUPP;
  600. return handler->set(dentry, name, NULL, 0,
  601. XATTR_REPLACE, handler->flags);
  602. }
  603. EXPORT_SYMBOL(generic_getxattr);
  604. EXPORT_SYMBOL(generic_listxattr);
  605. EXPORT_SYMBOL(generic_setxattr);
  606. EXPORT_SYMBOL(generic_removexattr);