xattr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 = kmalloc(size, GFP_KERNEL);
  209. if (!kvalue)
  210. return -ENOMEM;
  211. if (copy_from_user(kvalue, value, size)) {
  212. kfree(kvalue);
  213. return -EFAULT;
  214. }
  215. }
  216. error = vfs_setxattr(d, kname, kvalue, size, flags);
  217. kfree(kvalue);
  218. return error;
  219. }
  220. SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
  221. const char __user *, name, const void __user *, value,
  222. size_t, size, int, flags)
  223. {
  224. struct path path;
  225. int error;
  226. error = user_path(pathname, &path);
  227. if (error)
  228. return error;
  229. error = mnt_want_write(path.mnt);
  230. if (!error) {
  231. error = setxattr(path.dentry, name, value, size, flags);
  232. mnt_drop_write(path.mnt);
  233. }
  234. path_put(&path);
  235. return error;
  236. }
  237. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  238. const char __user *, name, const void __user *, value,
  239. size_t, size, int, flags)
  240. {
  241. struct path path;
  242. int error;
  243. error = user_lpath(pathname, &path);
  244. if (error)
  245. return error;
  246. error = mnt_want_write(path.mnt);
  247. if (!error) {
  248. error = setxattr(path.dentry, name, value, size, flags);
  249. mnt_drop_write(path.mnt);
  250. }
  251. path_put(&path);
  252. return error;
  253. }
  254. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  255. const void __user *,value, size_t, size, int, flags)
  256. {
  257. struct file *f;
  258. struct dentry *dentry;
  259. int error = -EBADF;
  260. f = fget(fd);
  261. if (!f)
  262. return error;
  263. dentry = f->f_path.dentry;
  264. audit_inode(NULL, dentry);
  265. error = mnt_want_write(f->f_path.mnt);
  266. if (!error) {
  267. error = setxattr(dentry, name, value, size, flags);
  268. mnt_drop_write(f->f_path.mnt);
  269. }
  270. fput(f);
  271. return error;
  272. }
  273. /*
  274. * Extended attribute GET operations
  275. */
  276. static ssize_t
  277. getxattr(struct dentry *d, const char __user *name, void __user *value,
  278. size_t size)
  279. {
  280. ssize_t error;
  281. void *kvalue = NULL;
  282. char kname[XATTR_NAME_MAX + 1];
  283. error = strncpy_from_user(kname, name, sizeof(kname));
  284. if (error == 0 || error == sizeof(kname))
  285. error = -ERANGE;
  286. if (error < 0)
  287. return error;
  288. if (size) {
  289. if (size > XATTR_SIZE_MAX)
  290. size = XATTR_SIZE_MAX;
  291. kvalue = kzalloc(size, GFP_KERNEL);
  292. if (!kvalue)
  293. return -ENOMEM;
  294. }
  295. error = vfs_getxattr(d, kname, kvalue, size);
  296. if (error > 0) {
  297. if (size && copy_to_user(value, kvalue, error))
  298. error = -EFAULT;
  299. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  300. /* The file system tried to returned a value bigger
  301. than XATTR_SIZE_MAX bytes. Not possible. */
  302. error = -E2BIG;
  303. }
  304. kfree(kvalue);
  305. return error;
  306. }
  307. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  308. const char __user *, name, void __user *, value, size_t, size)
  309. {
  310. struct path path;
  311. ssize_t error;
  312. error = user_path(pathname, &path);
  313. if (error)
  314. return error;
  315. error = getxattr(path.dentry, name, value, size);
  316. path_put(&path);
  317. return error;
  318. }
  319. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  320. const char __user *, name, void __user *, value, size_t, size)
  321. {
  322. struct path path;
  323. ssize_t error;
  324. error = user_lpath(pathname, &path);
  325. if (error)
  326. return error;
  327. error = getxattr(path.dentry, name, value, size);
  328. path_put(&path);
  329. return error;
  330. }
  331. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  332. void __user *, value, size_t, size)
  333. {
  334. struct file *f;
  335. ssize_t error = -EBADF;
  336. f = fget(fd);
  337. if (!f)
  338. return error;
  339. audit_inode(NULL, f->f_path.dentry);
  340. error = getxattr(f->f_path.dentry, name, value, size);
  341. fput(f);
  342. return error;
  343. }
  344. /*
  345. * Extended attribute LIST operations
  346. */
  347. static ssize_t
  348. listxattr(struct dentry *d, char __user *list, size_t size)
  349. {
  350. ssize_t error;
  351. char *klist = NULL;
  352. if (size) {
  353. if (size > XATTR_LIST_MAX)
  354. size = XATTR_LIST_MAX;
  355. klist = kmalloc(size, GFP_KERNEL);
  356. if (!klist)
  357. return -ENOMEM;
  358. }
  359. error = vfs_listxattr(d, klist, size);
  360. if (error > 0) {
  361. if (size && copy_to_user(list, klist, error))
  362. error = -EFAULT;
  363. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  364. /* The file system tried to returned a list bigger
  365. than XATTR_LIST_MAX bytes. Not possible. */
  366. error = -E2BIG;
  367. }
  368. kfree(klist);
  369. return error;
  370. }
  371. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  372. size_t, size)
  373. {
  374. struct path path;
  375. ssize_t error;
  376. error = user_path(pathname, &path);
  377. if (error)
  378. return error;
  379. error = listxattr(path.dentry, list, size);
  380. path_put(&path);
  381. return error;
  382. }
  383. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  384. size_t, size)
  385. {
  386. struct path path;
  387. ssize_t error;
  388. error = user_lpath(pathname, &path);
  389. if (error)
  390. return error;
  391. error = listxattr(path.dentry, list, size);
  392. path_put(&path);
  393. return error;
  394. }
  395. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  396. {
  397. struct file *f;
  398. ssize_t error = -EBADF;
  399. f = fget(fd);
  400. if (!f)
  401. return error;
  402. audit_inode(NULL, f->f_path.dentry);
  403. error = listxattr(f->f_path.dentry, list, size);
  404. fput(f);
  405. return error;
  406. }
  407. /*
  408. * Extended attribute REMOVE operations
  409. */
  410. static long
  411. removexattr(struct dentry *d, const char __user *name)
  412. {
  413. int error;
  414. char kname[XATTR_NAME_MAX + 1];
  415. error = strncpy_from_user(kname, name, sizeof(kname));
  416. if (error == 0 || error == sizeof(kname))
  417. error = -ERANGE;
  418. if (error < 0)
  419. return error;
  420. return vfs_removexattr(d, kname);
  421. }
  422. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  423. const char __user *, name)
  424. {
  425. struct path path;
  426. int error;
  427. error = user_path(pathname, &path);
  428. if (error)
  429. return error;
  430. error = mnt_want_write(path.mnt);
  431. if (!error) {
  432. error = removexattr(path.dentry, name);
  433. mnt_drop_write(path.mnt);
  434. }
  435. path_put(&path);
  436. return error;
  437. }
  438. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  439. const char __user *, name)
  440. {
  441. struct path path;
  442. int error;
  443. error = user_lpath(pathname, &path);
  444. if (error)
  445. return error;
  446. error = mnt_want_write(path.mnt);
  447. if (!error) {
  448. error = removexattr(path.dentry, name);
  449. mnt_drop_write(path.mnt);
  450. }
  451. path_put(&path);
  452. return error;
  453. }
  454. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  455. {
  456. struct file *f;
  457. struct dentry *dentry;
  458. int error = -EBADF;
  459. f = fget(fd);
  460. if (!f)
  461. return error;
  462. dentry = f->f_path.dentry;
  463. audit_inode(NULL, dentry);
  464. error = mnt_want_write(f->f_path.mnt);
  465. if (!error) {
  466. error = removexattr(dentry, name);
  467. mnt_drop_write(f->f_path.mnt);
  468. }
  469. fput(f);
  470. return error;
  471. }
  472. static const char *
  473. strcmp_prefix(const char *a, const char *a_prefix)
  474. {
  475. while (*a_prefix && *a == *a_prefix) {
  476. a++;
  477. a_prefix++;
  478. }
  479. return *a_prefix ? NULL : a;
  480. }
  481. /*
  482. * In order to implement different sets of xattr operations for each xattr
  483. * prefix with the generic xattr API, a filesystem should create a
  484. * null-terminated array of struct xattr_handler (one for each prefix) and
  485. * hang a pointer to it off of the s_xattr field of the superblock.
  486. *
  487. * The generic_fooxattr() functions will use this list to dispatch xattr
  488. * operations to the correct xattr_handler.
  489. */
  490. #define for_each_xattr_handler(handlers, handler) \
  491. for ((handler) = *(handlers)++; \
  492. (handler) != NULL; \
  493. (handler) = *(handlers)++)
  494. /*
  495. * Find the xattr_handler with the matching prefix.
  496. */
  497. static struct xattr_handler *
  498. xattr_resolve_name(struct xattr_handler **handlers, const char **name)
  499. {
  500. struct xattr_handler *handler;
  501. if (!*name)
  502. return NULL;
  503. for_each_xattr_handler(handlers, handler) {
  504. const char *n = strcmp_prefix(*name, handler->prefix);
  505. if (n) {
  506. *name = n;
  507. break;
  508. }
  509. }
  510. return handler;
  511. }
  512. /*
  513. * Find the handler for the prefix and dispatch its get() operation.
  514. */
  515. ssize_t
  516. generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
  517. {
  518. struct xattr_handler *handler;
  519. struct inode *inode = dentry->d_inode;
  520. handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
  521. if (!handler)
  522. return -EOPNOTSUPP;
  523. return handler->get(inode, name, buffer, size);
  524. }
  525. /*
  526. * Combine the results of the list() operation from every xattr_handler in the
  527. * list.
  528. */
  529. ssize_t
  530. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  531. {
  532. struct inode *inode = dentry->d_inode;
  533. struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
  534. unsigned int size = 0;
  535. if (!buffer) {
  536. for_each_xattr_handler(handlers, handler)
  537. size += handler->list(inode, NULL, 0, NULL, 0);
  538. } else {
  539. char *buf = buffer;
  540. for_each_xattr_handler(handlers, handler) {
  541. size = handler->list(inode, buf, buffer_size, NULL, 0);
  542. if (size > buffer_size)
  543. return -ERANGE;
  544. buf += size;
  545. buffer_size -= size;
  546. }
  547. size = buf - buffer;
  548. }
  549. return size;
  550. }
  551. /*
  552. * Find the handler for the prefix and dispatch its set() operation.
  553. */
  554. int
  555. generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
  556. {
  557. struct xattr_handler *handler;
  558. struct inode *inode = dentry->d_inode;
  559. if (size == 0)
  560. value = ""; /* empty EA, do not remove */
  561. handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
  562. if (!handler)
  563. return -EOPNOTSUPP;
  564. return handler->set(inode, name, value, size, flags);
  565. }
  566. /*
  567. * Find the handler for the prefix and dispatch its set() operation to remove
  568. * any associated extended attribute.
  569. */
  570. int
  571. generic_removexattr(struct dentry *dentry, const char *name)
  572. {
  573. struct xattr_handler *handler;
  574. struct inode *inode = dentry->d_inode;
  575. handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
  576. if (!handler)
  577. return -EOPNOTSUPP;
  578. return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
  579. }
  580. EXPORT_SYMBOL(generic_getxattr);
  581. EXPORT_SYMBOL(generic_listxattr);
  582. EXPORT_SYMBOL(generic_setxattr);
  583. EXPORT_SYMBOL(generic_removexattr);