posix_acl.c 21 KB

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