xattr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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/namei.h>
  13. #include <linux/security.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/module.h>
  16. #include <linux/fsnotify.h>
  17. #include <linux/audit.h>
  18. #include <asm/uaccess.h>
  19. /*
  20. * Check permissions for extended attribute access. This is a bit complicated
  21. * because different namespaces have very different rules.
  22. */
  23. static int
  24. xattr_permission(struct inode *inode, const char *name, int mask)
  25. {
  26. /*
  27. * We can never set or remove an extended attribute on a read-only
  28. * filesystem or on an immutable / append-only inode.
  29. */
  30. if (mask & MAY_WRITE) {
  31. if (IS_RDONLY(inode))
  32. return -EROFS;
  33. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  34. return -EPERM;
  35. }
  36. /*
  37. * No restriction for security.* and system.* from the VFS. Decision
  38. * on these is left to the underlying filesystem / security module.
  39. */
  40. if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
  41. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  42. return 0;
  43. /*
  44. * The trusted.* namespace can only be accessed by a privileged user.
  45. */
  46. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
  47. return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
  48. /* In user.* namespace, only regular files and directories can have
  49. * extended attributes. For sticky directories, only the owner and
  50. * privileged user can write attributes.
  51. */
  52. if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
  53. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  54. return -EPERM;
  55. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  56. (mask & MAY_WRITE) && (current->fsuid != inode->i_uid) &&
  57. !capable(CAP_FOWNER))
  58. return -EPERM;
  59. }
  60. return permission(inode, mask, NULL);
  61. }
  62. int
  63. vfs_setxattr(struct dentry *dentry, char *name, void *value,
  64. size_t size, int flags)
  65. {
  66. struct inode *inode = dentry->d_inode;
  67. int error;
  68. error = xattr_permission(inode, name, MAY_WRITE);
  69. if (error)
  70. return error;
  71. mutex_lock(&inode->i_mutex);
  72. error = security_inode_setxattr(dentry, name, value, size, flags);
  73. if (error)
  74. goto out;
  75. error = -EOPNOTSUPP;
  76. if (inode->i_op->setxattr) {
  77. error = inode->i_op->setxattr(dentry, name, value, size, flags);
  78. if (!error) {
  79. fsnotify_xattr(dentry);
  80. security_inode_post_setxattr(dentry, name, value,
  81. size, flags);
  82. }
  83. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  84. XATTR_SECURITY_PREFIX_LEN)) {
  85. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  86. error = security_inode_setsecurity(inode, suffix, value,
  87. size, flags);
  88. if (!error)
  89. fsnotify_xattr(dentry);
  90. }
  91. out:
  92. mutex_unlock(&inode->i_mutex);
  93. return error;
  94. }
  95. EXPORT_SYMBOL_GPL(vfs_setxattr);
  96. ssize_t
  97. vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
  98. {
  99. struct inode *inode = dentry->d_inode;
  100. int error;
  101. error = xattr_permission(inode, name, MAY_READ);
  102. if (error)
  103. return error;
  104. error = security_inode_getxattr(dentry, name);
  105. if (error)
  106. return error;
  107. if (inode->i_op->getxattr)
  108. error = inode->i_op->getxattr(dentry, name, value, size);
  109. else
  110. error = -EOPNOTSUPP;
  111. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  112. XATTR_SECURITY_PREFIX_LEN)) {
  113. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  114. int ret = security_inode_getsecurity(inode, suffix, value,
  115. size, error);
  116. /*
  117. * Only overwrite the return value if a security module
  118. * is actually active.
  119. */
  120. if (ret != -EOPNOTSUPP)
  121. error = ret;
  122. }
  123. return error;
  124. }
  125. EXPORT_SYMBOL_GPL(vfs_getxattr);
  126. ssize_t
  127. vfs_listxattr(struct dentry *d, char *list, size_t size)
  128. {
  129. ssize_t error;
  130. error = security_inode_listxattr(d);
  131. if (error)
  132. return error;
  133. error = -EOPNOTSUPP;
  134. if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
  135. error = d->d_inode->i_op->listxattr(d, list, size);
  136. } else {
  137. error = security_inode_listsecurity(d->d_inode, list, size);
  138. if (size && error > size)
  139. error = -ERANGE;
  140. }
  141. return error;
  142. }
  143. EXPORT_SYMBOL_GPL(vfs_listxattr);
  144. int
  145. vfs_removexattr(struct dentry *dentry, char *name)
  146. {
  147. struct inode *inode = dentry->d_inode;
  148. int error;
  149. if (!inode->i_op->removexattr)
  150. return -EOPNOTSUPP;
  151. error = xattr_permission(inode, name, MAY_WRITE);
  152. if (error)
  153. return error;
  154. error = security_inode_removexattr(dentry, name);
  155. if (error)
  156. return error;
  157. mutex_lock(&inode->i_mutex);
  158. error = inode->i_op->removexattr(dentry, name);
  159. mutex_unlock(&inode->i_mutex);
  160. if (!error)
  161. fsnotify_xattr(dentry);
  162. return error;
  163. }
  164. EXPORT_SYMBOL_GPL(vfs_removexattr);
  165. /*
  166. * Extended attribute SET operations
  167. */
  168. static long
  169. setxattr(struct dentry *d, char __user *name, void __user *value,
  170. size_t size, int flags)
  171. {
  172. int error;
  173. void *kvalue = NULL;
  174. char kname[XATTR_NAME_MAX + 1];
  175. if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
  176. return -EINVAL;
  177. error = strncpy_from_user(kname, name, sizeof(kname));
  178. if (error == 0 || error == sizeof(kname))
  179. error = -ERANGE;
  180. if (error < 0)
  181. return error;
  182. if (size) {
  183. if (size > XATTR_SIZE_MAX)
  184. return -E2BIG;
  185. kvalue = kmalloc(size, GFP_KERNEL);
  186. if (!kvalue)
  187. return -ENOMEM;
  188. if (copy_from_user(kvalue, value, size)) {
  189. kfree(kvalue);
  190. return -EFAULT;
  191. }
  192. }
  193. error = vfs_setxattr(d, kname, kvalue, size, flags);
  194. kfree(kvalue);
  195. return error;
  196. }
  197. asmlinkage long
  198. sys_setxattr(char __user *path, char __user *name, void __user *value,
  199. size_t size, int flags)
  200. {
  201. struct nameidata nd;
  202. int error;
  203. error = user_path_walk(path, &nd);
  204. if (error)
  205. return error;
  206. error = setxattr(nd.dentry, name, value, size, flags);
  207. path_release(&nd);
  208. return error;
  209. }
  210. asmlinkage long
  211. sys_lsetxattr(char __user *path, char __user *name, void __user *value,
  212. size_t size, int flags)
  213. {
  214. struct nameidata nd;
  215. int error;
  216. error = user_path_walk_link(path, &nd);
  217. if (error)
  218. return error;
  219. error = setxattr(nd.dentry, name, value, size, flags);
  220. path_release(&nd);
  221. return error;
  222. }
  223. asmlinkage long
  224. sys_fsetxattr(int fd, char __user *name, void __user *value,
  225. size_t size, int flags)
  226. {
  227. struct file *f;
  228. struct dentry *dentry;
  229. int error = -EBADF;
  230. f = fget(fd);
  231. if (!f)
  232. return error;
  233. dentry = f->f_path.dentry;
  234. audit_inode(NULL, dentry->d_inode);
  235. error = setxattr(dentry, name, value, size, flags);
  236. fput(f);
  237. return error;
  238. }
  239. /*
  240. * Extended attribute GET operations
  241. */
  242. static ssize_t
  243. getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
  244. {
  245. ssize_t error;
  246. void *kvalue = NULL;
  247. char kname[XATTR_NAME_MAX + 1];
  248. error = strncpy_from_user(kname, name, sizeof(kname));
  249. if (error == 0 || error == sizeof(kname))
  250. error = -ERANGE;
  251. if (error < 0)
  252. return error;
  253. if (size) {
  254. if (size > XATTR_SIZE_MAX)
  255. size = XATTR_SIZE_MAX;
  256. kvalue = kzalloc(size, GFP_KERNEL);
  257. if (!kvalue)
  258. return -ENOMEM;
  259. }
  260. error = vfs_getxattr(d, kname, kvalue, size);
  261. if (error > 0) {
  262. if (size && copy_to_user(value, kvalue, error))
  263. error = -EFAULT;
  264. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  265. /* The file system tried to returned a value bigger
  266. than XATTR_SIZE_MAX bytes. Not possible. */
  267. error = -E2BIG;
  268. }
  269. kfree(kvalue);
  270. return error;
  271. }
  272. asmlinkage ssize_t
  273. sys_getxattr(char __user *path, char __user *name, void __user *value,
  274. size_t size)
  275. {
  276. struct nameidata nd;
  277. ssize_t error;
  278. error = user_path_walk(path, &nd);
  279. if (error)
  280. return error;
  281. error = getxattr(nd.dentry, name, value, size);
  282. path_release(&nd);
  283. return error;
  284. }
  285. asmlinkage ssize_t
  286. sys_lgetxattr(char __user *path, char __user *name, void __user *value,
  287. size_t size)
  288. {
  289. struct nameidata nd;
  290. ssize_t error;
  291. error = user_path_walk_link(path, &nd);
  292. if (error)
  293. return error;
  294. error = getxattr(nd.dentry, name, value, size);
  295. path_release(&nd);
  296. return error;
  297. }
  298. asmlinkage ssize_t
  299. sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
  300. {
  301. struct file *f;
  302. ssize_t error = -EBADF;
  303. f = fget(fd);
  304. if (!f)
  305. return error;
  306. error = getxattr(f->f_path.dentry, name, value, size);
  307. fput(f);
  308. return error;
  309. }
  310. /*
  311. * Extended attribute LIST operations
  312. */
  313. static ssize_t
  314. listxattr(struct dentry *d, char __user *list, size_t size)
  315. {
  316. ssize_t error;
  317. char *klist = NULL;
  318. if (size) {
  319. if (size > XATTR_LIST_MAX)
  320. size = XATTR_LIST_MAX;
  321. klist = kmalloc(size, GFP_KERNEL);
  322. if (!klist)
  323. return -ENOMEM;
  324. }
  325. error = vfs_listxattr(d, klist, size);
  326. if (error > 0) {
  327. if (size && copy_to_user(list, klist, error))
  328. error = -EFAULT;
  329. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  330. /* The file system tried to returned a list bigger
  331. than XATTR_LIST_MAX bytes. Not possible. */
  332. error = -E2BIG;
  333. }
  334. kfree(klist);
  335. return error;
  336. }
  337. asmlinkage ssize_t
  338. sys_listxattr(char __user *path, char __user *list, size_t size)
  339. {
  340. struct nameidata nd;
  341. ssize_t error;
  342. error = user_path_walk(path, &nd);
  343. if (error)
  344. return error;
  345. error = listxattr(nd.dentry, list, size);
  346. path_release(&nd);
  347. return error;
  348. }
  349. asmlinkage ssize_t
  350. sys_llistxattr(char __user *path, char __user *list, size_t size)
  351. {
  352. struct nameidata nd;
  353. ssize_t error;
  354. error = user_path_walk_link(path, &nd);
  355. if (error)
  356. return error;
  357. error = listxattr(nd.dentry, list, size);
  358. path_release(&nd);
  359. return error;
  360. }
  361. asmlinkage ssize_t
  362. sys_flistxattr(int fd, char __user *list, size_t size)
  363. {
  364. struct file *f;
  365. ssize_t error = -EBADF;
  366. f = fget(fd);
  367. if (!f)
  368. return error;
  369. error = listxattr(f->f_path.dentry, list, size);
  370. fput(f);
  371. return error;
  372. }
  373. /*
  374. * Extended attribute REMOVE operations
  375. */
  376. static long
  377. removexattr(struct dentry *d, char __user *name)
  378. {
  379. int error;
  380. char kname[XATTR_NAME_MAX + 1];
  381. error = strncpy_from_user(kname, name, sizeof(kname));
  382. if (error == 0 || error == sizeof(kname))
  383. error = -ERANGE;
  384. if (error < 0)
  385. return error;
  386. return vfs_removexattr(d, kname);
  387. }
  388. asmlinkage long
  389. sys_removexattr(char __user *path, char __user *name)
  390. {
  391. struct nameidata nd;
  392. int error;
  393. error = user_path_walk(path, &nd);
  394. if (error)
  395. return error;
  396. error = removexattr(nd.dentry, name);
  397. path_release(&nd);
  398. return error;
  399. }
  400. asmlinkage long
  401. sys_lremovexattr(char __user *path, char __user *name)
  402. {
  403. struct nameidata nd;
  404. int error;
  405. error = user_path_walk_link(path, &nd);
  406. if (error)
  407. return error;
  408. error = removexattr(nd.dentry, name);
  409. path_release(&nd);
  410. return error;
  411. }
  412. asmlinkage long
  413. sys_fremovexattr(int fd, char __user *name)
  414. {
  415. struct file *f;
  416. struct dentry *dentry;
  417. int error = -EBADF;
  418. f = fget(fd);
  419. if (!f)
  420. return error;
  421. dentry = f->f_path.dentry;
  422. audit_inode(NULL, dentry->d_inode);
  423. error = removexattr(dentry, name);
  424. fput(f);
  425. return error;
  426. }
  427. static const char *
  428. strcmp_prefix(const char *a, const char *a_prefix)
  429. {
  430. while (*a_prefix && *a == *a_prefix) {
  431. a++;
  432. a_prefix++;
  433. }
  434. return *a_prefix ? NULL : a;
  435. }
  436. /*
  437. * In order to implement different sets of xattr operations for each xattr
  438. * prefix with the generic xattr API, a filesystem should create a
  439. * null-terminated array of struct xattr_handler (one for each prefix) and
  440. * hang a pointer to it off of the s_xattr field of the superblock.
  441. *
  442. * The generic_fooxattr() functions will use this list to dispatch xattr
  443. * operations to the correct xattr_handler.
  444. */
  445. #define for_each_xattr_handler(handlers, handler) \
  446. for ((handler) = *(handlers)++; \
  447. (handler) != NULL; \
  448. (handler) = *(handlers)++)
  449. /*
  450. * Find the xattr_handler with the matching prefix.
  451. */
  452. static struct xattr_handler *
  453. xattr_resolve_name(struct xattr_handler **handlers, const char **name)
  454. {
  455. struct xattr_handler *handler;
  456. if (!*name)
  457. return NULL;
  458. for_each_xattr_handler(handlers, handler) {
  459. const char *n = strcmp_prefix(*name, handler->prefix);
  460. if (n) {
  461. *name = n;
  462. break;
  463. }
  464. }
  465. return handler;
  466. }
  467. /*
  468. * Find the handler for the prefix and dispatch its get() operation.
  469. */
  470. ssize_t
  471. generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
  472. {
  473. struct xattr_handler *handler;
  474. struct inode *inode = dentry->d_inode;
  475. handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
  476. if (!handler)
  477. return -EOPNOTSUPP;
  478. return handler->get(inode, name, buffer, size);
  479. }
  480. /*
  481. * Combine the results of the list() operation from every xattr_handler in the
  482. * list.
  483. */
  484. ssize_t
  485. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  486. {
  487. struct inode *inode = dentry->d_inode;
  488. struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
  489. unsigned int size = 0;
  490. if (!buffer) {
  491. for_each_xattr_handler(handlers, handler)
  492. size += handler->list(inode, NULL, 0, NULL, 0);
  493. } else {
  494. char *buf = buffer;
  495. for_each_xattr_handler(handlers, handler) {
  496. size = handler->list(inode, buf, buffer_size, NULL, 0);
  497. if (size > buffer_size)
  498. return -ERANGE;
  499. buf += size;
  500. buffer_size -= size;
  501. }
  502. size = buf - buffer;
  503. }
  504. return size;
  505. }
  506. /*
  507. * Find the handler for the prefix and dispatch its set() operation.
  508. */
  509. int
  510. generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
  511. {
  512. struct xattr_handler *handler;
  513. struct inode *inode = dentry->d_inode;
  514. if (size == 0)
  515. value = ""; /* empty EA, do not remove */
  516. handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
  517. if (!handler)
  518. return -EOPNOTSUPP;
  519. return handler->set(inode, name, value, size, flags);
  520. }
  521. /*
  522. * Find the handler for the prefix and dispatch its set() operation to remove
  523. * any associated extended attribute.
  524. */
  525. int
  526. generic_removexattr(struct dentry *dentry, const char *name)
  527. {
  528. struct xattr_handler *handler;
  529. struct inode *inode = dentry->d_inode;
  530. handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
  531. if (!handler)
  532. return -EOPNOTSUPP;
  533. return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
  534. }
  535. EXPORT_SYMBOL(generic_getxattr);
  536. EXPORT_SYMBOL(generic_listxattr);
  537. EXPORT_SYMBOL(generic_setxattr);
  538. EXPORT_SYMBOL(generic_removexattr);