posix_acl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
  4. *
  5. * Fixes from William Schumacher incorporated on 15 March 2001.
  6. * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
  7. */
  8. /*
  9. * This file contains generic functions for manipulating
  10. * POSIX 1003.1e draft standard 17 ACLs.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/atomic.h>
  15. #include <linux/fs.h>
  16. #include <linux/sched.h>
  17. #include <linux/cred.h>
  18. #include <linux/posix_acl.h>
  19. #include <linux/posix_acl_xattr.h>
  20. #include <linux/xattr.h>
  21. #include <linux/export.h>
  22. #include <linux/user_namespace.h>
  23. static struct posix_acl **acl_by_type(struct inode *inode, int type)
  24. {
  25. switch (type) {
  26. case ACL_TYPE_ACCESS:
  27. return &inode->i_acl;
  28. case ACL_TYPE_DEFAULT:
  29. return &inode->i_default_acl;
  30. default:
  31. BUG();
  32. }
  33. }
  34. struct posix_acl *get_cached_acl(struct inode *inode, int type)
  35. {
  36. struct posix_acl **p = acl_by_type(inode, type);
  37. struct posix_acl *acl;
  38. for (;;) {
  39. rcu_read_lock();
  40. acl = rcu_dereference(*p);
  41. if (!acl || is_uncached_acl(acl) ||
  42. refcount_inc_not_zero(&acl->a_refcount))
  43. break;
  44. rcu_read_unlock();
  45. cpu_relax();
  46. }
  47. rcu_read_unlock();
  48. return acl;
  49. }
  50. EXPORT_SYMBOL(get_cached_acl);
  51. struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
  52. {
  53. return rcu_dereference(*acl_by_type(inode, type));
  54. }
  55. EXPORT_SYMBOL(get_cached_acl_rcu);
  56. void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
  57. {
  58. struct posix_acl **p = acl_by_type(inode, type);
  59. struct posix_acl *old;
  60. old = xchg(p, posix_acl_dup(acl));
  61. if (!is_uncached_acl(old))
  62. posix_acl_release(old);
  63. }
  64. EXPORT_SYMBOL(set_cached_acl);
  65. static void __forget_cached_acl(struct posix_acl **p)
  66. {
  67. struct posix_acl *old;
  68. old = xchg(p, ACL_NOT_CACHED);
  69. if (!is_uncached_acl(old))
  70. posix_acl_release(old);
  71. }
  72. void forget_cached_acl(struct inode *inode, int type)
  73. {
  74. __forget_cached_acl(acl_by_type(inode, type));
  75. }
  76. EXPORT_SYMBOL(forget_cached_acl);
  77. void forget_all_cached_acls(struct inode *inode)
  78. {
  79. __forget_cached_acl(&inode->i_acl);
  80. __forget_cached_acl(&inode->i_default_acl);
  81. }
  82. EXPORT_SYMBOL(forget_all_cached_acls);
  83. struct posix_acl *get_acl(struct inode *inode, int type)
  84. {
  85. void *sentinel;
  86. struct posix_acl **p;
  87. struct posix_acl *acl;
  88. /*
  89. * The sentinel is used to detect when another operation like
  90. * set_cached_acl() or forget_cached_acl() races with get_acl().
  91. * It is guaranteed that is_uncached_acl(sentinel) is true.
  92. */
  93. acl = get_cached_acl(inode, type);
  94. if (!is_uncached_acl(acl))
  95. return acl;
  96. if (!IS_POSIXACL(inode))
  97. return NULL;
  98. sentinel = uncached_acl_sentinel(current);
  99. p = acl_by_type(inode, type);
  100. /*
  101. * If the ACL isn't being read yet, set our sentinel. Otherwise, the
  102. * current value of the ACL will not be ACL_NOT_CACHED and so our own
  103. * sentinel will not be set; another task will update the cache. We
  104. * could wait for that other task to complete its job, but it's easier
  105. * to just call ->get_acl to fetch the ACL ourself. (This is going to
  106. * be an unlikely race.)
  107. */
  108. if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED)
  109. /* fall through */ ;
  110. /*
  111. * Normally, the ACL returned by ->get_acl will be cached.
  112. * A filesystem can prevent that by calling
  113. * forget_cached_acl(inode, type) in ->get_acl.
  114. *
  115. * If the filesystem doesn't have a get_acl() function at all, we'll
  116. * just create the negative cache entry.
  117. */
  118. if (!inode->i_op->get_acl) {
  119. set_cached_acl(inode, type, NULL);
  120. return NULL;
  121. }
  122. acl = inode->i_op->get_acl(inode, type);
  123. if (IS_ERR(acl)) {
  124. /*
  125. * Remove our sentinel so that we don't block future attempts
  126. * to cache the ACL.
  127. */
  128. cmpxchg(p, sentinel, ACL_NOT_CACHED);
  129. return acl;
  130. }
  131. /*
  132. * Cache the result, but only if our sentinel is still in place.
  133. */
  134. posix_acl_dup(acl);
  135. if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
  136. posix_acl_release(acl);
  137. return acl;
  138. }
  139. EXPORT_SYMBOL(get_acl);
  140. /*
  141. * Init a fresh posix_acl
  142. */
  143. void
  144. posix_acl_init(struct posix_acl *acl, int count)
  145. {
  146. refcount_set(&acl->a_refcount, 1);
  147. acl->a_count = count;
  148. }
  149. EXPORT_SYMBOL(posix_acl_init);
  150. /*
  151. * Allocate a new ACL with the specified number of entries.
  152. */
  153. struct posix_acl *
  154. posix_acl_alloc(int count, gfp_t flags)
  155. {
  156. const size_t size = sizeof(struct posix_acl) +
  157. count * sizeof(struct posix_acl_entry);
  158. struct posix_acl *acl = kmalloc(size, flags);
  159. if (acl)
  160. posix_acl_init(acl, count);
  161. return acl;
  162. }
  163. EXPORT_SYMBOL(posix_acl_alloc);
  164. /*
  165. * Clone an ACL.
  166. */
  167. static struct posix_acl *
  168. posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
  169. {
  170. struct posix_acl *clone = NULL;
  171. if (acl) {
  172. int size = sizeof(struct posix_acl) + acl->a_count *
  173. sizeof(struct posix_acl_entry);
  174. clone = kmemdup(acl, size, flags);
  175. if (clone)
  176. refcount_set(&clone->a_refcount, 1);
  177. }
  178. return clone;
  179. }
  180. /*
  181. * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
  182. */
  183. int
  184. posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
  185. {
  186. const struct posix_acl_entry *pa, *pe;
  187. int state = ACL_USER_OBJ;
  188. int needs_mask = 0;
  189. FOREACH_ACL_ENTRY(pa, acl, pe) {
  190. if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
  191. return -EINVAL;
  192. switch (pa->e_tag) {
  193. case ACL_USER_OBJ:
  194. if (state == ACL_USER_OBJ) {
  195. state = ACL_USER;
  196. break;
  197. }
  198. return -EINVAL;
  199. case ACL_USER:
  200. if (state != ACL_USER)
  201. return -EINVAL;
  202. if (!kuid_has_mapping(user_ns, pa->e_uid))
  203. return -EINVAL;
  204. needs_mask = 1;
  205. break;
  206. case ACL_GROUP_OBJ:
  207. if (state == ACL_USER) {
  208. state = ACL_GROUP;
  209. break;
  210. }
  211. return -EINVAL;
  212. case ACL_GROUP:
  213. if (state != ACL_GROUP)
  214. return -EINVAL;
  215. if (!kgid_has_mapping(user_ns, pa->e_gid))
  216. return -EINVAL;
  217. needs_mask = 1;
  218. break;
  219. case ACL_MASK:
  220. if (state != ACL_GROUP)
  221. return -EINVAL;
  222. state = ACL_OTHER;
  223. break;
  224. case ACL_OTHER:
  225. if (state == ACL_OTHER ||
  226. (state == ACL_GROUP && !needs_mask)) {
  227. state = 0;
  228. break;
  229. }
  230. return -EINVAL;
  231. default:
  232. return -EINVAL;
  233. }
  234. }
  235. if (state == 0)
  236. return 0;
  237. return -EINVAL;
  238. }
  239. EXPORT_SYMBOL(posix_acl_valid);
  240. /*
  241. * Returns 0 if the acl can be exactly represented in the traditional
  242. * file mode permission bits, or else 1. Returns -E... on error.
  243. */
  244. int
  245. posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
  246. {
  247. const struct posix_acl_entry *pa, *pe;
  248. umode_t mode = 0;
  249. int not_equiv = 0;
  250. /*
  251. * A null ACL can always be presented as mode bits.
  252. */
  253. if (!acl)
  254. return 0;
  255. FOREACH_ACL_ENTRY(pa, acl, pe) {
  256. switch (pa->e_tag) {
  257. case ACL_USER_OBJ:
  258. mode |= (pa->e_perm & S_IRWXO) << 6;
  259. break;
  260. case ACL_GROUP_OBJ:
  261. mode |= (pa->e_perm & S_IRWXO) << 3;
  262. break;
  263. case ACL_OTHER:
  264. mode |= pa->e_perm & S_IRWXO;
  265. break;
  266. case ACL_MASK:
  267. mode = (mode & ~S_IRWXG) |
  268. ((pa->e_perm & S_IRWXO) << 3);
  269. not_equiv = 1;
  270. break;
  271. case ACL_USER:
  272. case ACL_GROUP:
  273. not_equiv = 1;
  274. break;
  275. default:
  276. return -EINVAL;
  277. }
  278. }
  279. if (mode_p)
  280. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  281. return not_equiv;
  282. }
  283. EXPORT_SYMBOL(posix_acl_equiv_mode);
  284. /*
  285. * Create an ACL representing the file mode permission bits of an inode.
  286. */
  287. struct posix_acl *
  288. posix_acl_from_mode(umode_t mode, gfp_t flags)
  289. {
  290. struct posix_acl *acl = posix_acl_alloc(3, flags);
  291. if (!acl)
  292. return ERR_PTR(-ENOMEM);
  293. acl->a_entries[0].e_tag = ACL_USER_OBJ;
  294. acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  295. acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
  296. acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
  297. acl->a_entries[2].e_tag = ACL_OTHER;
  298. acl->a_entries[2].e_perm = (mode & S_IRWXO);
  299. return acl;
  300. }
  301. EXPORT_SYMBOL(posix_acl_from_mode);
  302. /*
  303. * Return 0 if current is granted want access to the inode
  304. * by the acl. Returns -E... otherwise.
  305. */
  306. int
  307. posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
  308. {
  309. const struct posix_acl_entry *pa, *pe, *mask_obj;
  310. int found = 0;
  311. want &= MAY_READ | MAY_WRITE | MAY_EXEC;
  312. FOREACH_ACL_ENTRY(pa, acl, pe) {
  313. switch(pa->e_tag) {
  314. case ACL_USER_OBJ:
  315. /* (May have been checked already) */
  316. if (uid_eq(inode->i_uid, current_fsuid()))
  317. goto check_perm;
  318. break;
  319. case ACL_USER:
  320. if (uid_eq(pa->e_uid, current_fsuid()))
  321. goto mask;
  322. break;
  323. case ACL_GROUP_OBJ:
  324. if (in_group_p(inode->i_gid)) {
  325. found = 1;
  326. if ((pa->e_perm & want) == want)
  327. goto mask;
  328. }
  329. break;
  330. case ACL_GROUP:
  331. if (in_group_p(pa->e_gid)) {
  332. found = 1;
  333. if ((pa->e_perm & want) == want)
  334. goto mask;
  335. }
  336. break;
  337. case ACL_MASK:
  338. break;
  339. case ACL_OTHER:
  340. if (found)
  341. return -EACCES;
  342. else
  343. goto check_perm;
  344. default:
  345. return -EIO;
  346. }
  347. }
  348. return -EIO;
  349. mask:
  350. for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
  351. if (mask_obj->e_tag == ACL_MASK) {
  352. if ((pa->e_perm & mask_obj->e_perm & want) == want)
  353. return 0;
  354. return -EACCES;
  355. }
  356. }
  357. check_perm:
  358. if ((pa->e_perm & want) == want)
  359. return 0;
  360. return -EACCES;
  361. }
  362. /*
  363. * Modify acl when creating a new inode. The caller must ensure the acl is
  364. * only referenced once.
  365. *
  366. * mode_p initially must contain the mode parameter to the open() / creat()
  367. * system calls. All permissions that are not granted by the acl are removed.
  368. * The permissions in the acl are changed to reflect the mode_p parameter.
  369. */
  370. static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  371. {
  372. struct posix_acl_entry *pa, *pe;
  373. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  374. umode_t mode = *mode_p;
  375. int not_equiv = 0;
  376. /* assert(atomic_read(acl->a_refcount) == 1); */
  377. FOREACH_ACL_ENTRY(pa, acl, pe) {
  378. switch(pa->e_tag) {
  379. case ACL_USER_OBJ:
  380. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  381. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  382. break;
  383. case ACL_USER:
  384. case ACL_GROUP:
  385. not_equiv = 1;
  386. break;
  387. case ACL_GROUP_OBJ:
  388. group_obj = pa;
  389. break;
  390. case ACL_OTHER:
  391. pa->e_perm &= mode | ~S_IRWXO;
  392. mode &= pa->e_perm | ~S_IRWXO;
  393. break;
  394. case ACL_MASK:
  395. mask_obj = pa;
  396. not_equiv = 1;
  397. break;
  398. default:
  399. return -EIO;
  400. }
  401. }
  402. if (mask_obj) {
  403. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  404. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  405. } else {
  406. if (!group_obj)
  407. return -EIO;
  408. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  409. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  410. }
  411. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  412. return not_equiv;
  413. }
  414. /*
  415. * Modify the ACL for the chmod syscall.
  416. */
  417. static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
  418. {
  419. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  420. struct posix_acl_entry *pa, *pe;
  421. /* assert(atomic_read(acl->a_refcount) == 1); */
  422. FOREACH_ACL_ENTRY(pa, acl, pe) {
  423. switch(pa->e_tag) {
  424. case ACL_USER_OBJ:
  425. pa->e_perm = (mode & S_IRWXU) >> 6;
  426. break;
  427. case ACL_USER:
  428. case ACL_GROUP:
  429. break;
  430. case ACL_GROUP_OBJ:
  431. group_obj = pa;
  432. break;
  433. case ACL_MASK:
  434. mask_obj = pa;
  435. break;
  436. case ACL_OTHER:
  437. pa->e_perm = (mode & S_IRWXO);
  438. break;
  439. default:
  440. return -EIO;
  441. }
  442. }
  443. if (mask_obj) {
  444. mask_obj->e_perm = (mode & S_IRWXG) >> 3;
  445. } else {
  446. if (!group_obj)
  447. return -EIO;
  448. group_obj->e_perm = (mode & S_IRWXG) >> 3;
  449. }
  450. return 0;
  451. }
  452. int
  453. __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
  454. {
  455. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  456. int err = -ENOMEM;
  457. if (clone) {
  458. err = posix_acl_create_masq(clone, mode_p);
  459. if (err < 0) {
  460. posix_acl_release(clone);
  461. clone = NULL;
  462. }
  463. }
  464. posix_acl_release(*acl);
  465. *acl = clone;
  466. return err;
  467. }
  468. EXPORT_SYMBOL(__posix_acl_create);
  469. int
  470. __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
  471. {
  472. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  473. int err = -ENOMEM;
  474. if (clone) {
  475. err = __posix_acl_chmod_masq(clone, mode);
  476. if (err) {
  477. posix_acl_release(clone);
  478. clone = NULL;
  479. }
  480. }
  481. posix_acl_release(*acl);
  482. *acl = clone;
  483. return err;
  484. }
  485. EXPORT_SYMBOL(__posix_acl_chmod);
  486. int
  487. posix_acl_chmod(struct inode *inode, umode_t mode)
  488. {
  489. struct posix_acl *acl;
  490. int ret = 0;
  491. if (!IS_POSIXACL(inode))
  492. return 0;
  493. if (!inode->i_op->set_acl)
  494. return -EOPNOTSUPP;
  495. acl = get_acl(inode, ACL_TYPE_ACCESS);
  496. if (IS_ERR_OR_NULL(acl)) {
  497. if (acl == ERR_PTR(-EOPNOTSUPP))
  498. return 0;
  499. return PTR_ERR(acl);
  500. }
  501. ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
  502. if (ret)
  503. return ret;
  504. ret = inode->i_op->set_acl(inode, acl, ACL_TYPE_ACCESS);
  505. posix_acl_release(acl);
  506. return ret;
  507. }
  508. EXPORT_SYMBOL(posix_acl_chmod);
  509. int
  510. posix_acl_create(struct inode *dir, umode_t *mode,
  511. struct posix_acl **default_acl, struct posix_acl **acl)
  512. {
  513. struct posix_acl *p;
  514. struct posix_acl *clone;
  515. int ret;
  516. *acl = NULL;
  517. *default_acl = NULL;
  518. if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  519. return 0;
  520. p = get_acl(dir, ACL_TYPE_DEFAULT);
  521. if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
  522. *mode &= ~current_umask();
  523. return 0;
  524. }
  525. if (IS_ERR(p))
  526. return PTR_ERR(p);
  527. ret = -ENOMEM;
  528. clone = posix_acl_clone(p, GFP_NOFS);
  529. if (!clone)
  530. goto err_release;
  531. ret = posix_acl_create_masq(clone, mode);
  532. if (ret < 0)
  533. goto err_release_clone;
  534. if (ret == 0)
  535. posix_acl_release(clone);
  536. else
  537. *acl = clone;
  538. if (!S_ISDIR(*mode))
  539. posix_acl_release(p);
  540. else
  541. *default_acl = p;
  542. return 0;
  543. err_release_clone:
  544. posix_acl_release(clone);
  545. err_release:
  546. posix_acl_release(p);
  547. return ret;
  548. }
  549. EXPORT_SYMBOL_GPL(posix_acl_create);
  550. /**
  551. * posix_acl_update_mode - update mode in set_acl
  552. * @inode: target inode
  553. * @mode_p: mode (pointer) for update
  554. * @acl: acl pointer
  555. *
  556. * Update the file mode when setting an ACL: compute the new file permission
  557. * bits based on the ACL. In addition, if the ACL is equivalent to the new
  558. * file mode, set *@acl to NULL to indicate that no ACL should be set.
  559. *
  560. * As with chmod, clear the setgid bit if the caller is not in the owning group
  561. * or capable of CAP_FSETID (see inode_change_ok).
  562. *
  563. * Called from set_acl inode operations.
  564. */
  565. int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
  566. struct posix_acl **acl)
  567. {
  568. umode_t mode = inode->i_mode;
  569. int error;
  570. error = posix_acl_equiv_mode(*acl, &mode);
  571. if (error < 0)
  572. return error;
  573. if (error == 0)
  574. *acl = NULL;
  575. if (!in_group_p(inode->i_gid) &&
  576. !capable_wrt_inode_uidgid(inode, CAP_FSETID))
  577. mode &= ~S_ISGID;
  578. *mode_p = mode;
  579. return 0;
  580. }
  581. EXPORT_SYMBOL(posix_acl_update_mode);
  582. /*
  583. * Fix up the uids and gids in posix acl extended attributes in place.
  584. */
  585. static void posix_acl_fix_xattr_userns(
  586. struct user_namespace *to, struct user_namespace *from,
  587. void *value, size_t size)
  588. {
  589. struct posix_acl_xattr_header *header = value;
  590. struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
  591. int count;
  592. kuid_t uid;
  593. kgid_t gid;
  594. if (!value)
  595. return;
  596. if (size < sizeof(struct posix_acl_xattr_header))
  597. return;
  598. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  599. return;
  600. count = posix_acl_xattr_count(size);
  601. if (count < 0)
  602. return;
  603. if (count == 0)
  604. return;
  605. for (end = entry + count; entry != end; entry++) {
  606. switch(le16_to_cpu(entry->e_tag)) {
  607. case ACL_USER:
  608. uid = make_kuid(from, le32_to_cpu(entry->e_id));
  609. entry->e_id = cpu_to_le32(from_kuid(to, uid));
  610. break;
  611. case ACL_GROUP:
  612. gid = make_kgid(from, le32_to_cpu(entry->e_id));
  613. entry->e_id = cpu_to_le32(from_kgid(to, gid));
  614. break;
  615. default:
  616. break;
  617. }
  618. }
  619. }
  620. void posix_acl_fix_xattr_from_user(void *value, size_t size)
  621. {
  622. struct user_namespace *user_ns = current_user_ns();
  623. if (user_ns == &init_user_ns)
  624. return;
  625. posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
  626. }
  627. void posix_acl_fix_xattr_to_user(void *value, size_t size)
  628. {
  629. struct user_namespace *user_ns = current_user_ns();
  630. if (user_ns == &init_user_ns)
  631. return;
  632. posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
  633. }
  634. /*
  635. * Convert from extended attribute to in-memory representation.
  636. */
  637. struct posix_acl *
  638. posix_acl_from_xattr(struct user_namespace *user_ns,
  639. const void *value, size_t size)
  640. {
  641. const struct posix_acl_xattr_header *header = value;
  642. const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
  643. int count;
  644. struct posix_acl *acl;
  645. struct posix_acl_entry *acl_e;
  646. if (!value)
  647. return NULL;
  648. if (size < sizeof(struct posix_acl_xattr_header))
  649. return ERR_PTR(-EINVAL);
  650. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  651. return ERR_PTR(-EOPNOTSUPP);
  652. count = posix_acl_xattr_count(size);
  653. if (count < 0)
  654. return ERR_PTR(-EINVAL);
  655. if (count == 0)
  656. return NULL;
  657. acl = posix_acl_alloc(count, GFP_NOFS);
  658. if (!acl)
  659. return ERR_PTR(-ENOMEM);
  660. acl_e = acl->a_entries;
  661. for (end = entry + count; entry != end; acl_e++, entry++) {
  662. acl_e->e_tag = le16_to_cpu(entry->e_tag);
  663. acl_e->e_perm = le16_to_cpu(entry->e_perm);
  664. switch(acl_e->e_tag) {
  665. case ACL_USER_OBJ:
  666. case ACL_GROUP_OBJ:
  667. case ACL_MASK:
  668. case ACL_OTHER:
  669. break;
  670. case ACL_USER:
  671. acl_e->e_uid =
  672. make_kuid(user_ns,
  673. le32_to_cpu(entry->e_id));
  674. if (!uid_valid(acl_e->e_uid))
  675. goto fail;
  676. break;
  677. case ACL_GROUP:
  678. acl_e->e_gid =
  679. make_kgid(user_ns,
  680. le32_to_cpu(entry->e_id));
  681. if (!gid_valid(acl_e->e_gid))
  682. goto fail;
  683. break;
  684. default:
  685. goto fail;
  686. }
  687. }
  688. return acl;
  689. fail:
  690. posix_acl_release(acl);
  691. return ERR_PTR(-EINVAL);
  692. }
  693. EXPORT_SYMBOL (posix_acl_from_xattr);
  694. /*
  695. * Convert from in-memory to extended attribute representation.
  696. */
  697. int
  698. posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
  699. void *buffer, size_t size)
  700. {
  701. struct posix_acl_xattr_header *ext_acl = buffer;
  702. struct posix_acl_xattr_entry *ext_entry;
  703. int real_size, n;
  704. real_size = posix_acl_xattr_size(acl->a_count);
  705. if (!buffer)
  706. return real_size;
  707. if (real_size > size)
  708. return -ERANGE;
  709. ext_entry = (void *)(ext_acl + 1);
  710. ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  711. for (n=0; n < acl->a_count; n++, ext_entry++) {
  712. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  713. ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
  714. ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
  715. switch(acl_e->e_tag) {
  716. case ACL_USER:
  717. ext_entry->e_id =
  718. cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
  719. break;
  720. case ACL_GROUP:
  721. ext_entry->e_id =
  722. cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
  723. break;
  724. default:
  725. ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  726. break;
  727. }
  728. }
  729. return real_size;
  730. }
  731. EXPORT_SYMBOL (posix_acl_to_xattr);
  732. static int
  733. posix_acl_xattr_get(const struct xattr_handler *handler,
  734. struct dentry *unused, struct inode *inode,
  735. const char *name, void *value, size_t size, int flags)
  736. {
  737. struct posix_acl *acl;
  738. int error;
  739. if (!IS_POSIXACL(inode))
  740. return -EOPNOTSUPP;
  741. if (S_ISLNK(inode->i_mode))
  742. return -EOPNOTSUPP;
  743. acl = get_acl(inode, handler->flags);
  744. if (IS_ERR(acl))
  745. return PTR_ERR(acl);
  746. if (acl == NULL)
  747. return -ENODATA;
  748. error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  749. posix_acl_release(acl);
  750. return error;
  751. }
  752. int
  753. set_posix_acl(struct inode *inode, int type, struct posix_acl *acl)
  754. {
  755. if (!IS_POSIXACL(inode))
  756. return -EOPNOTSUPP;
  757. if (!inode->i_op->set_acl)
  758. return -EOPNOTSUPP;
  759. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  760. return acl ? -EACCES : 0;
  761. if (!inode_owner_or_capable(inode))
  762. return -EPERM;
  763. if (acl) {
  764. int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
  765. if (ret)
  766. return ret;
  767. }
  768. return inode->i_op->set_acl(inode, acl, type);
  769. }
  770. EXPORT_SYMBOL(set_posix_acl);
  771. static int
  772. posix_acl_xattr_set(const struct xattr_handler *handler,
  773. struct dentry *unused, struct inode *inode,
  774. const char *name, const void *value,
  775. size_t size, int flags)
  776. {
  777. struct posix_acl *acl = NULL;
  778. int ret;
  779. if (value) {
  780. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  781. if (IS_ERR(acl))
  782. return PTR_ERR(acl);
  783. }
  784. ret = set_posix_acl(inode, handler->flags, acl);
  785. posix_acl_release(acl);
  786. return ret;
  787. }
  788. static bool
  789. posix_acl_xattr_list(struct dentry *dentry)
  790. {
  791. return IS_POSIXACL(d_backing_inode(dentry));
  792. }
  793. const struct xattr_handler posix_acl_access_xattr_handler = {
  794. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  795. .flags = ACL_TYPE_ACCESS,
  796. .list = posix_acl_xattr_list,
  797. .get = posix_acl_xattr_get,
  798. .set = posix_acl_xattr_set,
  799. };
  800. EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
  801. const struct xattr_handler posix_acl_default_xattr_handler = {
  802. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  803. .flags = ACL_TYPE_DEFAULT,
  804. .list = posix_acl_xattr_list,
  805. .get = posix_acl_xattr_get,
  806. .set = posix_acl_xattr_set,
  807. };
  808. EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
  809. int simple_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  810. {
  811. int error;
  812. if (type == ACL_TYPE_ACCESS) {
  813. error = posix_acl_update_mode(inode,
  814. &inode->i_mode, &acl);
  815. if (error)
  816. return error;
  817. }
  818. inode->i_ctime = current_time(inode);
  819. set_cached_acl(inode, type, acl);
  820. return 0;
  821. }
  822. int simple_acl_create(struct inode *dir, struct inode *inode)
  823. {
  824. struct posix_acl *default_acl, *acl;
  825. int error;
  826. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  827. if (error)
  828. return error;
  829. set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  830. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  831. if (default_acl)
  832. posix_acl_release(default_acl);
  833. if (acl)
  834. posix_acl_release(acl);
  835. return 0;
  836. }