posix_acl.c 21 KB

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