posix_acl.c 21 KB

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