xattr.c 15 KB

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