xattr.c 15 KB

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