posix_acl.c 20 KB

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