posix_acl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. clone = posix_acl_clone(p, GFP_NOFS);
  526. if (!clone)
  527. goto no_mem;
  528. ret = posix_acl_create_masq(clone, mode);
  529. if (ret < 0)
  530. goto no_mem_clone;
  531. if (ret == 0)
  532. posix_acl_release(clone);
  533. else
  534. *acl = clone;
  535. if (!S_ISDIR(*mode))
  536. posix_acl_release(p);
  537. else
  538. *default_acl = p;
  539. return 0;
  540. no_mem_clone:
  541. posix_acl_release(clone);
  542. no_mem:
  543. posix_acl_release(p);
  544. return -ENOMEM;
  545. }
  546. EXPORT_SYMBOL_GPL(posix_acl_create);
  547. /*
  548. * Fix up the uids and gids in posix acl extended attributes in place.
  549. */
  550. static void posix_acl_fix_xattr_userns(
  551. struct user_namespace *to, struct user_namespace *from,
  552. void *value, size_t size)
  553. {
  554. posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
  555. posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
  556. int count;
  557. kuid_t uid;
  558. kgid_t gid;
  559. if (!value)
  560. return;
  561. if (size < sizeof(posix_acl_xattr_header))
  562. return;
  563. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  564. return;
  565. count = posix_acl_xattr_count(size);
  566. if (count < 0)
  567. return;
  568. if (count == 0)
  569. return;
  570. for (end = entry + count; entry != end; entry++) {
  571. switch(le16_to_cpu(entry->e_tag)) {
  572. case ACL_USER:
  573. uid = make_kuid(from, le32_to_cpu(entry->e_id));
  574. entry->e_id = cpu_to_le32(from_kuid(to, uid));
  575. break;
  576. case ACL_GROUP:
  577. gid = make_kgid(from, le32_to_cpu(entry->e_id));
  578. entry->e_id = cpu_to_le32(from_kgid(to, gid));
  579. break;
  580. default:
  581. break;
  582. }
  583. }
  584. }
  585. void posix_acl_fix_xattr_from_user(void *value, size_t size)
  586. {
  587. struct user_namespace *user_ns = current_user_ns();
  588. if (user_ns == &init_user_ns)
  589. return;
  590. posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
  591. }
  592. void posix_acl_fix_xattr_to_user(void *value, size_t size)
  593. {
  594. struct user_namespace *user_ns = current_user_ns();
  595. if (user_ns == &init_user_ns)
  596. return;
  597. posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
  598. }
  599. /*
  600. * Convert from extended attribute to in-memory representation.
  601. */
  602. struct posix_acl *
  603. posix_acl_from_xattr(struct user_namespace *user_ns,
  604. const void *value, size_t size)
  605. {
  606. posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
  607. posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
  608. int count;
  609. struct posix_acl *acl;
  610. struct posix_acl_entry *acl_e;
  611. if (!value)
  612. return NULL;
  613. if (size < sizeof(posix_acl_xattr_header))
  614. return ERR_PTR(-EINVAL);
  615. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  616. return ERR_PTR(-EOPNOTSUPP);
  617. count = posix_acl_xattr_count(size);
  618. if (count < 0)
  619. return ERR_PTR(-EINVAL);
  620. if (count == 0)
  621. return NULL;
  622. acl = posix_acl_alloc(count, GFP_NOFS);
  623. if (!acl)
  624. return ERR_PTR(-ENOMEM);
  625. acl_e = acl->a_entries;
  626. for (end = entry + count; entry != end; acl_e++, entry++) {
  627. acl_e->e_tag = le16_to_cpu(entry->e_tag);
  628. acl_e->e_perm = le16_to_cpu(entry->e_perm);
  629. switch(acl_e->e_tag) {
  630. case ACL_USER_OBJ:
  631. case ACL_GROUP_OBJ:
  632. case ACL_MASK:
  633. case ACL_OTHER:
  634. break;
  635. case ACL_USER:
  636. acl_e->e_uid =
  637. make_kuid(user_ns,
  638. le32_to_cpu(entry->e_id));
  639. if (!uid_valid(acl_e->e_uid))
  640. goto fail;
  641. break;
  642. case ACL_GROUP:
  643. acl_e->e_gid =
  644. make_kgid(user_ns,
  645. le32_to_cpu(entry->e_id));
  646. if (!gid_valid(acl_e->e_gid))
  647. goto fail;
  648. break;
  649. default:
  650. goto fail;
  651. }
  652. }
  653. return acl;
  654. fail:
  655. posix_acl_release(acl);
  656. return ERR_PTR(-EINVAL);
  657. }
  658. EXPORT_SYMBOL (posix_acl_from_xattr);
  659. /*
  660. * Convert from in-memory to extended attribute representation.
  661. */
  662. int
  663. posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
  664. void *buffer, size_t size)
  665. {
  666. posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
  667. posix_acl_xattr_entry *ext_entry;
  668. int real_size, n;
  669. real_size = posix_acl_xattr_size(acl->a_count);
  670. if (!buffer)
  671. return real_size;
  672. if (real_size > size)
  673. return -ERANGE;
  674. ext_entry = ext_acl->a_entries;
  675. ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  676. for (n=0; n < acl->a_count; n++, ext_entry++) {
  677. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  678. ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
  679. ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
  680. switch(acl_e->e_tag) {
  681. case ACL_USER:
  682. ext_entry->e_id =
  683. cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
  684. break;
  685. case ACL_GROUP:
  686. ext_entry->e_id =
  687. cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
  688. break;
  689. default:
  690. ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  691. break;
  692. }
  693. }
  694. return real_size;
  695. }
  696. EXPORT_SYMBOL (posix_acl_to_xattr);
  697. static int
  698. posix_acl_xattr_get(const struct xattr_handler *handler,
  699. struct dentry *unused, struct inode *inode,
  700. const char *name, void *value, size_t size)
  701. {
  702. struct posix_acl *acl;
  703. int error;
  704. if (!IS_POSIXACL(inode))
  705. return -EOPNOTSUPP;
  706. if (S_ISLNK(inode->i_mode))
  707. return -EOPNOTSUPP;
  708. acl = get_acl(inode, handler->flags);
  709. if (IS_ERR(acl))
  710. return PTR_ERR(acl);
  711. if (acl == NULL)
  712. return -ENODATA;
  713. error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  714. posix_acl_release(acl);
  715. return error;
  716. }
  717. int
  718. set_posix_acl(struct inode *inode, int type, struct posix_acl *acl)
  719. {
  720. if (!IS_POSIXACL(inode))
  721. return -EOPNOTSUPP;
  722. if (!inode->i_op->set_acl)
  723. return -EOPNOTSUPP;
  724. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  725. return acl ? -EACCES : 0;
  726. if (!inode_owner_or_capable(inode))
  727. return -EPERM;
  728. if (acl) {
  729. int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
  730. if (ret)
  731. return ret;
  732. }
  733. return inode->i_op->set_acl(inode, acl, type);
  734. }
  735. EXPORT_SYMBOL(set_posix_acl);
  736. static int
  737. posix_acl_xattr_set(const struct xattr_handler *handler,
  738. struct dentry *unused, struct inode *inode,
  739. const char *name, const void *value,
  740. size_t size, int flags)
  741. {
  742. struct posix_acl *acl = NULL;
  743. int ret;
  744. if (value) {
  745. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  746. if (IS_ERR(acl))
  747. return PTR_ERR(acl);
  748. }
  749. ret = set_posix_acl(inode, handler->flags, acl);
  750. posix_acl_release(acl);
  751. return ret;
  752. }
  753. static bool
  754. posix_acl_xattr_list(struct dentry *dentry)
  755. {
  756. return IS_POSIXACL(d_backing_inode(dentry));
  757. }
  758. const struct xattr_handler posix_acl_access_xattr_handler = {
  759. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  760. .flags = ACL_TYPE_ACCESS,
  761. .list = posix_acl_xattr_list,
  762. .get = posix_acl_xattr_get,
  763. .set = posix_acl_xattr_set,
  764. };
  765. EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
  766. const struct xattr_handler posix_acl_default_xattr_handler = {
  767. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  768. .flags = ACL_TYPE_DEFAULT,
  769. .list = posix_acl_xattr_list,
  770. .get = posix_acl_xattr_get,
  771. .set = posix_acl_xattr_set,
  772. };
  773. EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
  774. int simple_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  775. {
  776. int error;
  777. if (type == ACL_TYPE_ACCESS) {
  778. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  779. if (error < 0)
  780. return 0;
  781. if (error == 0)
  782. acl = NULL;
  783. }
  784. inode->i_ctime = CURRENT_TIME;
  785. set_cached_acl(inode, type, acl);
  786. return 0;
  787. }
  788. int simple_acl_create(struct inode *dir, struct inode *inode)
  789. {
  790. struct posix_acl *default_acl, *acl;
  791. int error;
  792. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  793. if (error)
  794. return error;
  795. set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  796. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  797. if (default_acl)
  798. posix_acl_release(default_acl);
  799. if (acl)
  800. posix_acl_release(acl);
  801. return 0;
  802. }