/kern_2.6.32/fs/xattr.c

http://omnia2droid.googlecode.com/ · C · 691 lines · 514 code · 87 blank · 90 comment · 109 complexity · b74569c75c3e3d76eb2c40d53aa15cfe MD5 · raw file

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