xattr.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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/evm.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/export.h>
  18. #include <linux/fsnotify.h>
  19. #include <linux/audit.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/posix_acl_xattr.h>
  22. #include <linux/uaccess.h>
  23. static const char *
  24. strcmp_prefix(const char *a, const char *a_prefix)
  25. {
  26. while (*a_prefix && *a == *a_prefix) {
  27. a++;
  28. a_prefix++;
  29. }
  30. return *a_prefix ? NULL : a;
  31. }
  32. /*
  33. * In order to implement different sets of xattr operations for each xattr
  34. * prefix, a filesystem should create a null-terminated array of struct
  35. * xattr_handler (one for each prefix) and hang a pointer to it off of the
  36. * s_xattr field of the superblock.
  37. */
  38. #define for_each_xattr_handler(handlers, handler) \
  39. if (handlers) \
  40. for ((handler) = *(handlers)++; \
  41. (handler) != NULL; \
  42. (handler) = *(handlers)++)
  43. /*
  44. * Find the xattr_handler with the matching prefix.
  45. */
  46. static const struct xattr_handler *
  47. xattr_resolve_name(struct inode *inode, const char **name)
  48. {
  49. const struct xattr_handler **handlers = inode->i_sb->s_xattr;
  50. const struct xattr_handler *handler;
  51. if (!(inode->i_opflags & IOP_XATTR)) {
  52. if (unlikely(is_bad_inode(inode)))
  53. return ERR_PTR(-EIO);
  54. return ERR_PTR(-EOPNOTSUPP);
  55. }
  56. for_each_xattr_handler(handlers, handler) {
  57. const char *n;
  58. n = strcmp_prefix(*name, xattr_prefix(handler));
  59. if (n) {
  60. if (!handler->prefix ^ !*n) {
  61. if (*n)
  62. continue;
  63. return ERR_PTR(-EINVAL);
  64. }
  65. *name = n;
  66. return handler;
  67. }
  68. }
  69. return ERR_PTR(-EOPNOTSUPP);
  70. }
  71. /*
  72. * Check permissions for extended attribute access. This is a bit complicated
  73. * because different namespaces have very different rules.
  74. */
  75. static int
  76. xattr_permission(struct inode *inode, const char *name, int mask)
  77. {
  78. /*
  79. * We can never set or remove an extended attribute on a read-only
  80. * filesystem or on an immutable / append-only inode.
  81. */
  82. if (mask & MAY_WRITE) {
  83. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  84. return -EPERM;
  85. /*
  86. * Updating an xattr will likely cause i_uid and i_gid
  87. * to be writen back improperly if their true value is
  88. * unknown to the vfs.
  89. */
  90. if (HAS_UNMAPPED_ID(inode))
  91. return -EPERM;
  92. }
  93. /*
  94. * No restriction for security.* and system.* from the VFS. Decision
  95. * on these is left to the underlying filesystem / security module.
  96. */
  97. if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
  98. !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  99. return 0;
  100. /*
  101. * The trusted.* namespace can only be accessed by privileged users.
  102. */
  103. if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
  104. if (!capable(CAP_SYS_ADMIN))
  105. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  106. return 0;
  107. }
  108. /*
  109. * In the user.* namespace, only regular files and directories can have
  110. * extended attributes. For sticky directories, only the owner and
  111. * privileged users can write attributes.
  112. */
  113. if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
  114. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  115. return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
  116. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  117. (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
  118. return -EPERM;
  119. }
  120. return inode_permission(inode, mask);
  121. }
  122. int
  123. __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
  124. const void *value, size_t size, int flags)
  125. {
  126. const struct xattr_handler *handler;
  127. handler = xattr_resolve_name(inode, &name);
  128. if (IS_ERR(handler))
  129. return PTR_ERR(handler);
  130. if (!handler->set)
  131. return -EOPNOTSUPP;
  132. if (size == 0)
  133. value = ""; /* empty EA, do not remove */
  134. return handler->set(handler, dentry, inode, name, value, size, flags);
  135. }
  136. EXPORT_SYMBOL(__vfs_setxattr);
  137. /**
  138. * __vfs_setxattr_noperm - perform setxattr operation without performing
  139. * permission checks.
  140. *
  141. * @dentry - object to perform setxattr on
  142. * @name - xattr name to set
  143. * @value - value to set @name to
  144. * @size - size of @value
  145. * @flags - flags to pass into filesystem operations
  146. *
  147. * returns the result of the internal setxattr or setsecurity operations.
  148. *
  149. * This function requires the caller to lock the inode's i_mutex before it
  150. * is executed. It also assumes that the caller will make the appropriate
  151. * permission checks.
  152. */
  153. int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
  154. const void *value, size_t size, int flags)
  155. {
  156. struct inode *inode = dentry->d_inode;
  157. int error = -EAGAIN;
  158. int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
  159. XATTR_SECURITY_PREFIX_LEN);
  160. if (issec)
  161. inode->i_flags &= ~S_NOSEC;
  162. if (inode->i_opflags & IOP_XATTR) {
  163. error = __vfs_setxattr(dentry, inode, name, value, size, flags);
  164. if (!error) {
  165. fsnotify_xattr(dentry);
  166. security_inode_post_setxattr(dentry, name, value,
  167. size, flags);
  168. }
  169. } else {
  170. if (unlikely(is_bad_inode(inode)))
  171. return -EIO;
  172. }
  173. if (error == -EAGAIN) {
  174. error = -EOPNOTSUPP;
  175. if (issec) {
  176. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  177. error = security_inode_setsecurity(inode, suffix, value,
  178. size, flags);
  179. if (!error)
  180. fsnotify_xattr(dentry);
  181. }
  182. }
  183. return error;
  184. }
  185. /**
  186. * __vfs_setxattr_locked: set an extended attribute while holding the inode
  187. * lock
  188. *
  189. * @dentry - object to perform setxattr on
  190. * @name - xattr name to set
  191. * @value - value to set @name to
  192. * @size - size of @value
  193. * @flags - flags to pass into filesystem operations
  194. * @delegated_inode - on return, will contain an inode pointer that
  195. * a delegation was broken on, NULL if none.
  196. */
  197. int
  198. __vfs_setxattr_locked(struct dentry *dentry, const char *name,
  199. const void *value, size_t size, int flags,
  200. struct inode **delegated_inode)
  201. {
  202. struct inode *inode = dentry->d_inode;
  203. int error;
  204. error = xattr_permission(inode, name, MAY_WRITE);
  205. if (error)
  206. return error;
  207. error = security_inode_setxattr(dentry, name, value, size, flags);
  208. if (error)
  209. goto out;
  210. error = try_break_deleg(inode, delegated_inode);
  211. if (error)
  212. goto out;
  213. error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
  214. out:
  215. return error;
  216. }
  217. EXPORT_SYMBOL_GPL(__vfs_setxattr_locked);
  218. int
  219. vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  220. size_t size, int flags)
  221. {
  222. struct inode *inode = dentry->d_inode;
  223. struct inode *delegated_inode = NULL;
  224. int error;
  225. retry_deleg:
  226. inode_lock(inode);
  227. error = __vfs_setxattr_locked(dentry, name, value, size, flags,
  228. &delegated_inode);
  229. inode_unlock(inode);
  230. if (delegated_inode) {
  231. error = break_deleg_wait(&delegated_inode);
  232. if (!error)
  233. goto retry_deleg;
  234. }
  235. return error;
  236. }
  237. EXPORT_SYMBOL_GPL(vfs_setxattr);
  238. static ssize_t
  239. xattr_getsecurity(struct inode *inode, const char *name, void *value,
  240. size_t size)
  241. {
  242. void *buffer = NULL;
  243. ssize_t len;
  244. if (!value || !size) {
  245. len = security_inode_getsecurity(inode, name, &buffer, false);
  246. goto out_noalloc;
  247. }
  248. len = security_inode_getsecurity(inode, name, &buffer, true);
  249. if (len < 0)
  250. return len;
  251. if (size < len) {
  252. len = -ERANGE;
  253. goto out;
  254. }
  255. memcpy(value, buffer, len);
  256. out:
  257. kfree(buffer);
  258. out_noalloc:
  259. return len;
  260. }
  261. /*
  262. * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
  263. *
  264. * Allocate memory, if not already allocated, or re-allocate correct size,
  265. * before retrieving the extended attribute.
  266. *
  267. * Returns the result of alloc, if failed, or the getxattr operation.
  268. */
  269. ssize_t
  270. vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
  271. size_t xattr_size, gfp_t flags)
  272. {
  273. const struct xattr_handler *handler;
  274. struct inode *inode = dentry->d_inode;
  275. char *value = *xattr_value;
  276. int error;
  277. error = xattr_permission(inode, name, MAY_READ);
  278. if (error)
  279. return error;
  280. handler = xattr_resolve_name(inode, &name);
  281. if (IS_ERR(handler))
  282. return PTR_ERR(handler);
  283. if (!handler->get)
  284. return -EOPNOTSUPP;
  285. error = handler->get(handler, dentry, inode, name, NULL, 0);
  286. if (error < 0)
  287. return error;
  288. if (!value || (error > xattr_size)) {
  289. value = krealloc(*xattr_value, error + 1, flags);
  290. if (!value)
  291. return -ENOMEM;
  292. memset(value, 0, error + 1);
  293. }
  294. error = handler->get(handler, dentry, inode, name, value, error);
  295. *xattr_value = value;
  296. return error;
  297. }
  298. ssize_t
  299. __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
  300. void *value, size_t size)
  301. {
  302. const struct xattr_handler *handler;
  303. handler = xattr_resolve_name(inode, &name);
  304. if (IS_ERR(handler))
  305. return PTR_ERR(handler);
  306. if (!handler->get)
  307. return -EOPNOTSUPP;
  308. return handler->get(handler, dentry, inode, name, value, size);
  309. }
  310. EXPORT_SYMBOL(__vfs_getxattr);
  311. ssize_t
  312. vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
  313. {
  314. struct inode *inode = dentry->d_inode;
  315. int error;
  316. error = xattr_permission(inode, name, MAY_READ);
  317. if (error)
  318. return error;
  319. error = security_inode_getxattr(dentry, name);
  320. if (error)
  321. return error;
  322. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  323. XATTR_SECURITY_PREFIX_LEN)) {
  324. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  325. int ret = xattr_getsecurity(inode, suffix, value, size);
  326. /*
  327. * Only overwrite the return value if a security module
  328. * is actually active.
  329. */
  330. if (ret == -EOPNOTSUPP)
  331. goto nolsm;
  332. return ret;
  333. }
  334. nolsm:
  335. return __vfs_getxattr(dentry, inode, name, value, size);
  336. }
  337. EXPORT_SYMBOL_GPL(vfs_getxattr);
  338. ssize_t
  339. vfs_listxattr(struct dentry *dentry, char *list, size_t size)
  340. {
  341. struct inode *inode = d_inode(dentry);
  342. ssize_t error;
  343. error = security_inode_listxattr(dentry);
  344. if (error)
  345. return error;
  346. if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
  347. error = inode->i_op->listxattr(dentry, list, size);
  348. } else {
  349. error = security_inode_listsecurity(inode, list, size);
  350. if (size && error > size)
  351. error = -ERANGE;
  352. }
  353. return error;
  354. }
  355. EXPORT_SYMBOL_GPL(vfs_listxattr);
  356. int
  357. __vfs_removexattr(struct dentry *dentry, const char *name)
  358. {
  359. struct inode *inode = d_inode(dentry);
  360. const struct xattr_handler *handler;
  361. handler = xattr_resolve_name(inode, &name);
  362. if (IS_ERR(handler))
  363. return PTR_ERR(handler);
  364. if (!handler->set)
  365. return -EOPNOTSUPP;
  366. return handler->set(handler, dentry, inode, name, NULL, 0, XATTR_REPLACE);
  367. }
  368. EXPORT_SYMBOL(__vfs_removexattr);
  369. /**
  370. * __vfs_removexattr_locked: set an extended attribute while holding the inode
  371. * lock
  372. *
  373. * @dentry - object to perform setxattr on
  374. * @name - name of xattr to remove
  375. * @delegated_inode - on return, will contain an inode pointer that
  376. * a delegation was broken on, NULL if none.
  377. */
  378. int
  379. __vfs_removexattr_locked(struct dentry *dentry, const char *name,
  380. struct inode **delegated_inode)
  381. {
  382. struct inode *inode = dentry->d_inode;
  383. int error;
  384. error = xattr_permission(inode, name, MAY_WRITE);
  385. if (error)
  386. return error;
  387. error = security_inode_removexattr(dentry, name);
  388. if (error)
  389. goto out;
  390. error = try_break_deleg(inode, delegated_inode);
  391. if (error)
  392. goto out;
  393. error = __vfs_removexattr(dentry, name);
  394. if (!error) {
  395. fsnotify_xattr(dentry);
  396. evm_inode_post_removexattr(dentry, name);
  397. }
  398. out:
  399. return error;
  400. }
  401. EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
  402. int
  403. vfs_removexattr(struct dentry *dentry, const char *name)
  404. {
  405. struct inode *inode = dentry->d_inode;
  406. struct inode *delegated_inode = NULL;
  407. int error;
  408. retry_deleg:
  409. inode_lock(inode);
  410. error = __vfs_removexattr_locked(dentry, name, &delegated_inode);
  411. inode_unlock(inode);
  412. if (delegated_inode) {
  413. error = break_deleg_wait(&delegated_inode);
  414. if (!error)
  415. goto retry_deleg;
  416. }
  417. return error;
  418. }
  419. EXPORT_SYMBOL_GPL(vfs_removexattr);
  420. /*
  421. * Extended attribute SET operations
  422. */
  423. static long
  424. setxattr(struct dentry *d, const char __user *name, const void __user *value,
  425. size_t size, int flags)
  426. {
  427. int error;
  428. void *kvalue = NULL;
  429. char kname[XATTR_NAME_MAX + 1];
  430. if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
  431. return -EINVAL;
  432. error = strncpy_from_user(kname, name, sizeof(kname));
  433. if (error == 0 || error == sizeof(kname))
  434. error = -ERANGE;
  435. if (error < 0)
  436. return error;
  437. if (size) {
  438. if (size > XATTR_SIZE_MAX)
  439. return -E2BIG;
  440. kvalue = kvmalloc(size, GFP_KERNEL);
  441. if (!kvalue)
  442. return -ENOMEM;
  443. if (copy_from_user(kvalue, value, size)) {
  444. error = -EFAULT;
  445. goto out;
  446. }
  447. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  448. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  449. posix_acl_fix_xattr_from_user(kvalue, size);
  450. else if (strcmp(kname, XATTR_NAME_CAPS) == 0) {
  451. error = cap_convert_nscap(d, &kvalue, size);
  452. if (error < 0)
  453. goto out;
  454. size = error;
  455. }
  456. }
  457. error = vfs_setxattr(d, kname, kvalue, size, flags);
  458. out:
  459. kvfree(kvalue);
  460. return error;
  461. }
  462. static int path_setxattr(const char __user *pathname,
  463. const char __user *name, const void __user *value,
  464. size_t size, int flags, unsigned int lookup_flags)
  465. {
  466. struct path path;
  467. int error;
  468. retry:
  469. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  470. if (error)
  471. return error;
  472. error = mnt_want_write(path.mnt);
  473. if (!error) {
  474. error = setxattr(path.dentry, name, value, size, flags);
  475. mnt_drop_write(path.mnt);
  476. }
  477. path_put(&path);
  478. if (retry_estale(error, lookup_flags)) {
  479. lookup_flags |= LOOKUP_REVAL;
  480. goto retry;
  481. }
  482. return error;
  483. }
  484. SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
  485. const char __user *, name, const void __user *, value,
  486. size_t, size, int, flags)
  487. {
  488. return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
  489. }
  490. SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
  491. const char __user *, name, const void __user *, value,
  492. size_t, size, int, flags)
  493. {
  494. return path_setxattr(pathname, name, value, size, flags, 0);
  495. }
  496. SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
  497. const void __user *,value, size_t, size, int, flags)
  498. {
  499. struct fd f = fdget(fd);
  500. int error = -EBADF;
  501. if (!f.file)
  502. return error;
  503. audit_file(f.file);
  504. error = mnt_want_write_file(f.file);
  505. if (!error) {
  506. error = setxattr(f.file->f_path.dentry, name, value, size, flags);
  507. mnt_drop_write_file(f.file);
  508. }
  509. fdput(f);
  510. return error;
  511. }
  512. /*
  513. * Extended attribute GET operations
  514. */
  515. static ssize_t
  516. getxattr(struct dentry *d, const char __user *name, void __user *value,
  517. size_t size)
  518. {
  519. ssize_t error;
  520. void *kvalue = NULL;
  521. char kname[XATTR_NAME_MAX + 1];
  522. error = strncpy_from_user(kname, name, sizeof(kname));
  523. if (error == 0 || error == sizeof(kname))
  524. error = -ERANGE;
  525. if (error < 0)
  526. return error;
  527. if (size) {
  528. if (size > XATTR_SIZE_MAX)
  529. size = XATTR_SIZE_MAX;
  530. kvalue = kvzalloc(size, GFP_KERNEL);
  531. if (!kvalue)
  532. return -ENOMEM;
  533. }
  534. error = vfs_getxattr(d, kname, kvalue, size);
  535. if (error > 0) {
  536. if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
  537. (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
  538. posix_acl_fix_xattr_to_user(kvalue, error);
  539. if (size && copy_to_user(value, kvalue, error))
  540. error = -EFAULT;
  541. } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
  542. /* The file system tried to returned a value bigger
  543. than XATTR_SIZE_MAX bytes. Not possible. */
  544. error = -E2BIG;
  545. }
  546. kvfree(kvalue);
  547. return error;
  548. }
  549. static ssize_t path_getxattr(const char __user *pathname,
  550. const char __user *name, void __user *value,
  551. size_t size, unsigned int lookup_flags)
  552. {
  553. struct path path;
  554. ssize_t error;
  555. retry:
  556. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  557. if (error)
  558. return error;
  559. error = getxattr(path.dentry, name, value, size);
  560. path_put(&path);
  561. if (retry_estale(error, lookup_flags)) {
  562. lookup_flags |= LOOKUP_REVAL;
  563. goto retry;
  564. }
  565. return error;
  566. }
  567. SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
  568. const char __user *, name, void __user *, value, size_t, size)
  569. {
  570. return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
  571. }
  572. SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
  573. const char __user *, name, void __user *, value, size_t, size)
  574. {
  575. return path_getxattr(pathname, name, value, size, 0);
  576. }
  577. SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
  578. void __user *, value, size_t, size)
  579. {
  580. struct fd f = fdget(fd);
  581. ssize_t error = -EBADF;
  582. if (!f.file)
  583. return error;
  584. audit_file(f.file);
  585. error = getxattr(f.file->f_path.dentry, name, value, size);
  586. fdput(f);
  587. return error;
  588. }
  589. /*
  590. * Extended attribute LIST operations
  591. */
  592. static ssize_t
  593. listxattr(struct dentry *d, char __user *list, size_t size)
  594. {
  595. ssize_t error;
  596. char *klist = NULL;
  597. if (size) {
  598. if (size > XATTR_LIST_MAX)
  599. size = XATTR_LIST_MAX;
  600. klist = kvmalloc(size, GFP_KERNEL);
  601. if (!klist)
  602. return -ENOMEM;
  603. }
  604. error = vfs_listxattr(d, klist, size);
  605. if (error > 0) {
  606. if (size && copy_to_user(list, klist, error))
  607. error = -EFAULT;
  608. } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
  609. /* The file system tried to returned a list bigger
  610. than XATTR_LIST_MAX bytes. Not possible. */
  611. error = -E2BIG;
  612. }
  613. kvfree(klist);
  614. return error;
  615. }
  616. static ssize_t path_listxattr(const char __user *pathname, char __user *list,
  617. size_t size, unsigned int lookup_flags)
  618. {
  619. struct path path;
  620. ssize_t error;
  621. retry:
  622. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  623. if (error)
  624. return error;
  625. error = listxattr(path.dentry, list, size);
  626. path_put(&path);
  627. if (retry_estale(error, lookup_flags)) {
  628. lookup_flags |= LOOKUP_REVAL;
  629. goto retry;
  630. }
  631. return error;
  632. }
  633. SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
  634. size_t, size)
  635. {
  636. return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
  637. }
  638. SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
  639. size_t, size)
  640. {
  641. return path_listxattr(pathname, list, size, 0);
  642. }
  643. SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
  644. {
  645. struct fd f = fdget(fd);
  646. ssize_t error = -EBADF;
  647. if (!f.file)
  648. return error;
  649. audit_file(f.file);
  650. error = listxattr(f.file->f_path.dentry, list, size);
  651. fdput(f);
  652. return error;
  653. }
  654. /*
  655. * Extended attribute REMOVE operations
  656. */
  657. static long
  658. removexattr(struct dentry *d, const char __user *name)
  659. {
  660. int error;
  661. char kname[XATTR_NAME_MAX + 1];
  662. error = strncpy_from_user(kname, name, sizeof(kname));
  663. if (error == 0 || error == sizeof(kname))
  664. error = -ERANGE;
  665. if (error < 0)
  666. return error;
  667. return vfs_removexattr(d, kname);
  668. }
  669. static int path_removexattr(const char __user *pathname,
  670. const char __user *name, unsigned int lookup_flags)
  671. {
  672. struct path path;
  673. int error;
  674. retry:
  675. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  676. if (error)
  677. return error;
  678. error = mnt_want_write(path.mnt);
  679. if (!error) {
  680. error = removexattr(path.dentry, name);
  681. mnt_drop_write(path.mnt);
  682. }
  683. path_put(&path);
  684. if (retry_estale(error, lookup_flags)) {
  685. lookup_flags |= LOOKUP_REVAL;
  686. goto retry;
  687. }
  688. return error;
  689. }
  690. SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
  691. const char __user *, name)
  692. {
  693. return path_removexattr(pathname, name, LOOKUP_FOLLOW);
  694. }
  695. SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
  696. const char __user *, name)
  697. {
  698. return path_removexattr(pathname, name, 0);
  699. }
  700. SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
  701. {
  702. struct fd f = fdget(fd);
  703. int error = -EBADF;
  704. if (!f.file)
  705. return error;
  706. audit_file(f.file);
  707. error = mnt_want_write_file(f.file);
  708. if (!error) {
  709. error = removexattr(f.file->f_path.dentry, name);
  710. mnt_drop_write_file(f.file);
  711. }
  712. fdput(f);
  713. return error;
  714. }
  715. /*
  716. * Combine the results of the list() operation from every xattr_handler in the
  717. * list.
  718. */
  719. ssize_t
  720. generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  721. {
  722. const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
  723. unsigned int size = 0;
  724. if (!buffer) {
  725. for_each_xattr_handler(handlers, handler) {
  726. if (!handler->name ||
  727. (handler->list && !handler->list(dentry)))
  728. continue;
  729. size += strlen(handler->name) + 1;
  730. }
  731. } else {
  732. char *buf = buffer;
  733. size_t len;
  734. for_each_xattr_handler(handlers, handler) {
  735. if (!handler->name ||
  736. (handler->list && !handler->list(dentry)))
  737. continue;
  738. len = strlen(handler->name);
  739. if (len + 1 > buffer_size)
  740. return -ERANGE;
  741. memcpy(buf, handler->name, len + 1);
  742. buf += len + 1;
  743. buffer_size -= len + 1;
  744. }
  745. size = buf - buffer;
  746. }
  747. return size;
  748. }
  749. EXPORT_SYMBOL(generic_listxattr);
  750. /**
  751. * xattr_full_name - Compute full attribute name from suffix
  752. *
  753. * @handler: handler of the xattr_handler operation
  754. * @name: name passed to the xattr_handler operation
  755. *
  756. * The get and set xattr handler operations are called with the remainder of
  757. * the attribute name after skipping the handler's prefix: for example, "foo"
  758. * is passed to the get operation of a handler with prefix "user." to get
  759. * attribute "user.foo". The full name is still "there" in the name though.
  760. *
  761. * Note: the list xattr handler operation when called from the vfs is passed a
  762. * NULL name; some file systems use this operation internally, with varying
  763. * semantics.
  764. */
  765. const char *xattr_full_name(const struct xattr_handler *handler,
  766. const char *name)
  767. {
  768. size_t prefix_len = strlen(xattr_prefix(handler));
  769. return name - prefix_len;
  770. }
  771. EXPORT_SYMBOL(xattr_full_name);
  772. /*
  773. * Allocate new xattr and copy in the value; but leave the name to callers.
  774. */
  775. struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
  776. {
  777. struct simple_xattr *new_xattr;
  778. size_t len;
  779. /* wrap around? */
  780. len = sizeof(*new_xattr) + size;
  781. if (len < sizeof(*new_xattr))
  782. return NULL;
  783. new_xattr = kmalloc(len, GFP_KERNEL);
  784. if (!new_xattr)
  785. return NULL;
  786. new_xattr->size = size;
  787. memcpy(new_xattr->value, value, size);
  788. return new_xattr;
  789. }
  790. /*
  791. * xattr GET operation for in-memory/pseudo filesystems
  792. */
  793. int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
  794. void *buffer, size_t size)
  795. {
  796. struct simple_xattr *xattr;
  797. int ret = -ENODATA;
  798. spin_lock(&xattrs->lock);
  799. list_for_each_entry(xattr, &xattrs->head, list) {
  800. if (strcmp(name, xattr->name))
  801. continue;
  802. ret = xattr->size;
  803. if (buffer) {
  804. if (size < xattr->size)
  805. ret = -ERANGE;
  806. else
  807. memcpy(buffer, xattr->value, xattr->size);
  808. }
  809. break;
  810. }
  811. spin_unlock(&xattrs->lock);
  812. return ret;
  813. }
  814. /**
  815. * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
  816. * @xattrs: target simple_xattr list
  817. * @name: name of the extended attribute
  818. * @value: value of the xattr. If %NULL, will remove the attribute.
  819. * @size: size of the new xattr
  820. * @flags: %XATTR_{CREATE|REPLACE}
  821. *
  822. * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
  823. * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
  824. * otherwise, fails with -ENODATA.
  825. *
  826. * Returns 0 on success, -errno on failure.
  827. */
  828. int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
  829. const void *value, size_t size, int flags)
  830. {
  831. struct simple_xattr *xattr;
  832. struct simple_xattr *new_xattr = NULL;
  833. int err = 0;
  834. /* value == NULL means remove */
  835. if (value) {
  836. new_xattr = simple_xattr_alloc(value, size);
  837. if (!new_xattr)
  838. return -ENOMEM;
  839. new_xattr->name = kstrdup(name, GFP_KERNEL);
  840. if (!new_xattr->name) {
  841. kfree(new_xattr);
  842. return -ENOMEM;
  843. }
  844. }
  845. spin_lock(&xattrs->lock);
  846. list_for_each_entry(xattr, &xattrs->head, list) {
  847. if (!strcmp(name, xattr->name)) {
  848. if (flags & XATTR_CREATE) {
  849. xattr = new_xattr;
  850. err = -EEXIST;
  851. } else if (new_xattr) {
  852. list_replace(&xattr->list, &new_xattr->list);
  853. } else {
  854. list_del(&xattr->list);
  855. }
  856. goto out;
  857. }
  858. }
  859. if (flags & XATTR_REPLACE) {
  860. xattr = new_xattr;
  861. err = -ENODATA;
  862. } else {
  863. list_add(&new_xattr->list, &xattrs->head);
  864. xattr = NULL;
  865. }
  866. out:
  867. spin_unlock(&xattrs->lock);
  868. if (xattr) {
  869. kfree(xattr->name);
  870. kfree(xattr);
  871. }
  872. return err;
  873. }
  874. static bool xattr_is_trusted(const char *name)
  875. {
  876. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  877. }
  878. static int xattr_list_one(char **buffer, ssize_t *remaining_size,
  879. const char *name)
  880. {
  881. size_t len = strlen(name) + 1;
  882. if (*buffer) {
  883. if (*remaining_size < len)
  884. return -ERANGE;
  885. memcpy(*buffer, name, len);
  886. *buffer += len;
  887. }
  888. *remaining_size -= len;
  889. return 0;
  890. }
  891. /*
  892. * xattr LIST operation for in-memory/pseudo filesystems
  893. */
  894. ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
  895. char *buffer, size_t size)
  896. {
  897. bool trusted = capable(CAP_SYS_ADMIN);
  898. struct simple_xattr *xattr;
  899. ssize_t remaining_size = size;
  900. int err = 0;
  901. #ifdef CONFIG_FS_POSIX_ACL
  902. if (IS_POSIXACL(inode)) {
  903. if (inode->i_acl) {
  904. err = xattr_list_one(&buffer, &remaining_size,
  905. XATTR_NAME_POSIX_ACL_ACCESS);
  906. if (err)
  907. return err;
  908. }
  909. if (inode->i_default_acl) {
  910. err = xattr_list_one(&buffer, &remaining_size,
  911. XATTR_NAME_POSIX_ACL_DEFAULT);
  912. if (err)
  913. return err;
  914. }
  915. }
  916. #endif
  917. spin_lock(&xattrs->lock);
  918. list_for_each_entry(xattr, &xattrs->head, list) {
  919. /* skip "trusted." attributes for unprivileged callers */
  920. if (!trusted && xattr_is_trusted(xattr->name))
  921. continue;
  922. err = xattr_list_one(&buffer, &remaining_size, xattr->name);
  923. if (err)
  924. break;
  925. }
  926. spin_unlock(&xattrs->lock);
  927. return err ? err : size - remaining_size;
  928. }
  929. /*
  930. * Adds an extended attribute to the list
  931. */
  932. void simple_xattr_list_add(struct simple_xattrs *xattrs,
  933. struct simple_xattr *new_xattr)
  934. {
  935. spin_lock(&xattrs->lock);
  936. list_add(&new_xattr->list, &xattrs->head);
  937. spin_unlock(&xattrs->lock);
  938. }