PageRenderTime 1055ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/xattr.c

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