posix_acl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. FOREACH_ACL_ENTRY(pa, acl, pe) {
  224. switch (pa->e_tag) {
  225. case ACL_USER_OBJ:
  226. mode |= (pa->e_perm & S_IRWXO) << 6;
  227. break;
  228. case ACL_GROUP_OBJ:
  229. mode |= (pa->e_perm & S_IRWXO) << 3;
  230. break;
  231. case ACL_OTHER:
  232. mode |= pa->e_perm & S_IRWXO;
  233. break;
  234. case ACL_MASK:
  235. mode = (mode & ~S_IRWXG) |
  236. ((pa->e_perm & S_IRWXO) << 3);
  237. not_equiv = 1;
  238. break;
  239. case ACL_USER:
  240. case ACL_GROUP:
  241. not_equiv = 1;
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. }
  247. if (mode_p)
  248. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  249. return not_equiv;
  250. }
  251. EXPORT_SYMBOL(posix_acl_equiv_mode);
  252. /*
  253. * Create an ACL representing the file mode permission bits of an inode.
  254. */
  255. struct posix_acl *
  256. posix_acl_from_mode(umode_t mode, gfp_t flags)
  257. {
  258. struct posix_acl *acl = posix_acl_alloc(3, flags);
  259. if (!acl)
  260. return ERR_PTR(-ENOMEM);
  261. acl->a_entries[0].e_tag = ACL_USER_OBJ;
  262. acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  263. acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
  264. acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
  265. acl->a_entries[2].e_tag = ACL_OTHER;
  266. acl->a_entries[2].e_perm = (mode & S_IRWXO);
  267. return acl;
  268. }
  269. EXPORT_SYMBOL(posix_acl_from_mode);
  270. /*
  271. * Return 0 if current is granted want access to the inode
  272. * by the acl. Returns -E... otherwise.
  273. */
  274. int
  275. posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
  276. {
  277. const struct posix_acl_entry *pa, *pe, *mask_obj;
  278. int found = 0;
  279. want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
  280. FOREACH_ACL_ENTRY(pa, acl, pe) {
  281. switch(pa->e_tag) {
  282. case ACL_USER_OBJ:
  283. /* (May have been checked already) */
  284. if (uid_eq(inode->i_uid, current_fsuid()))
  285. goto check_perm;
  286. break;
  287. case ACL_USER:
  288. if (uid_eq(pa->e_uid, current_fsuid()))
  289. goto mask;
  290. break;
  291. case ACL_GROUP_OBJ:
  292. if (in_group_p(inode->i_gid)) {
  293. found = 1;
  294. if ((pa->e_perm & want) == want)
  295. goto mask;
  296. }
  297. break;
  298. case ACL_GROUP:
  299. if (in_group_p(pa->e_gid)) {
  300. found = 1;
  301. if ((pa->e_perm & want) == want)
  302. goto mask;
  303. }
  304. break;
  305. case ACL_MASK:
  306. break;
  307. case ACL_OTHER:
  308. if (found)
  309. return -EACCES;
  310. else
  311. goto check_perm;
  312. default:
  313. return -EIO;
  314. }
  315. }
  316. return -EIO;
  317. mask:
  318. for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
  319. if (mask_obj->e_tag == ACL_MASK) {
  320. if ((pa->e_perm & mask_obj->e_perm & want) == want)
  321. return 0;
  322. return -EACCES;
  323. }
  324. }
  325. check_perm:
  326. if ((pa->e_perm & want) == want)
  327. return 0;
  328. return -EACCES;
  329. }
  330. /*
  331. * Modify acl when creating a new inode. The caller must ensure the acl is
  332. * only referenced once.
  333. *
  334. * mode_p initially must contain the mode parameter to the open() / creat()
  335. * system calls. All permissions that are not granted by the acl are removed.
  336. * The permissions in the acl are changed to reflect the mode_p parameter.
  337. */
  338. static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  339. {
  340. struct posix_acl_entry *pa, *pe;
  341. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  342. umode_t mode = *mode_p;
  343. int not_equiv = 0;
  344. /* assert(atomic_read(acl->a_refcount) == 1); */
  345. FOREACH_ACL_ENTRY(pa, acl, pe) {
  346. switch(pa->e_tag) {
  347. case ACL_USER_OBJ:
  348. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  349. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  350. break;
  351. case ACL_USER:
  352. case ACL_GROUP:
  353. not_equiv = 1;
  354. break;
  355. case ACL_GROUP_OBJ:
  356. group_obj = pa;
  357. break;
  358. case ACL_OTHER:
  359. pa->e_perm &= mode | ~S_IRWXO;
  360. mode &= pa->e_perm | ~S_IRWXO;
  361. break;
  362. case ACL_MASK:
  363. mask_obj = pa;
  364. not_equiv = 1;
  365. break;
  366. default:
  367. return -EIO;
  368. }
  369. }
  370. if (mask_obj) {
  371. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  372. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  373. } else {
  374. if (!group_obj)
  375. return -EIO;
  376. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  377. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  378. }
  379. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  380. return not_equiv;
  381. }
  382. /*
  383. * Modify the ACL for the chmod syscall.
  384. */
  385. static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
  386. {
  387. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  388. struct posix_acl_entry *pa, *pe;
  389. /* assert(atomic_read(acl->a_refcount) == 1); */
  390. FOREACH_ACL_ENTRY(pa, acl, pe) {
  391. switch(pa->e_tag) {
  392. case ACL_USER_OBJ:
  393. pa->e_perm = (mode & S_IRWXU) >> 6;
  394. break;
  395. case ACL_USER:
  396. case ACL_GROUP:
  397. break;
  398. case ACL_GROUP_OBJ:
  399. group_obj = pa;
  400. break;
  401. case ACL_MASK:
  402. mask_obj = pa;
  403. break;
  404. case ACL_OTHER:
  405. pa->e_perm = (mode & S_IRWXO);
  406. break;
  407. default:
  408. return -EIO;
  409. }
  410. }
  411. if (mask_obj) {
  412. mask_obj->e_perm = (mode & S_IRWXG) >> 3;
  413. } else {
  414. if (!group_obj)
  415. return -EIO;
  416. group_obj->e_perm = (mode & S_IRWXG) >> 3;
  417. }
  418. return 0;
  419. }
  420. int
  421. __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
  422. {
  423. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  424. int err = -ENOMEM;
  425. if (clone) {
  426. err = posix_acl_create_masq(clone, mode_p);
  427. if (err < 0) {
  428. posix_acl_release(clone);
  429. clone = NULL;
  430. }
  431. }
  432. posix_acl_release(*acl);
  433. *acl = clone;
  434. return err;
  435. }
  436. EXPORT_SYMBOL(__posix_acl_create);
  437. int
  438. __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
  439. {
  440. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  441. int err = -ENOMEM;
  442. if (clone) {
  443. err = __posix_acl_chmod_masq(clone, mode);
  444. if (err) {
  445. posix_acl_release(clone);
  446. clone = NULL;
  447. }
  448. }
  449. posix_acl_release(*acl);
  450. *acl = clone;
  451. return err;
  452. }
  453. EXPORT_SYMBOL(__posix_acl_chmod);
  454. int
  455. posix_acl_chmod(struct inode *inode, umode_t mode)
  456. {
  457. struct posix_acl *acl;
  458. int ret = 0;
  459. if (!IS_POSIXACL(inode))
  460. return 0;
  461. if (!inode->i_op->set_acl)
  462. return -EOPNOTSUPP;
  463. acl = get_acl(inode, ACL_TYPE_ACCESS);
  464. if (IS_ERR_OR_NULL(acl)) {
  465. if (acl == ERR_PTR(-EOPNOTSUPP))
  466. return 0;
  467. return PTR_ERR(acl);
  468. }
  469. ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
  470. if (ret)
  471. return ret;
  472. ret = inode->i_op->set_acl(inode, acl, ACL_TYPE_ACCESS);
  473. posix_acl_release(acl);
  474. return ret;
  475. }
  476. EXPORT_SYMBOL(posix_acl_chmod);
  477. int
  478. posix_acl_create(struct inode *dir, umode_t *mode,
  479. struct posix_acl **default_acl, struct posix_acl **acl)
  480. {
  481. struct posix_acl *p;
  482. int ret;
  483. if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  484. goto no_acl;
  485. p = get_acl(dir, ACL_TYPE_DEFAULT);
  486. if (IS_ERR(p)) {
  487. if (p == ERR_PTR(-EOPNOTSUPP))
  488. goto apply_umask;
  489. return PTR_ERR(p);
  490. }
  491. if (!p)
  492. goto apply_umask;
  493. *acl = posix_acl_clone(p, GFP_NOFS);
  494. if (!*acl)
  495. return -ENOMEM;
  496. ret = posix_acl_create_masq(*acl, mode);
  497. if (ret < 0) {
  498. posix_acl_release(*acl);
  499. return -ENOMEM;
  500. }
  501. if (ret == 0) {
  502. posix_acl_release(*acl);
  503. *acl = NULL;
  504. }
  505. if (!S_ISDIR(*mode)) {
  506. posix_acl_release(p);
  507. *default_acl = NULL;
  508. } else {
  509. *default_acl = p;
  510. }
  511. return 0;
  512. apply_umask:
  513. *mode &= ~current_umask();
  514. no_acl:
  515. *default_acl = NULL;
  516. *acl = NULL;
  517. return 0;
  518. }
  519. EXPORT_SYMBOL_GPL(posix_acl_create);
  520. /*
  521. * Fix up the uids and gids in posix acl extended attributes in place.
  522. */
  523. static void posix_acl_fix_xattr_userns(
  524. struct user_namespace *to, struct user_namespace *from,
  525. void *value, size_t size)
  526. {
  527. posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
  528. posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
  529. int count;
  530. kuid_t uid;
  531. kgid_t gid;
  532. if (!value)
  533. return;
  534. if (size < sizeof(posix_acl_xattr_header))
  535. return;
  536. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  537. return;
  538. count = posix_acl_xattr_count(size);
  539. if (count < 0)
  540. return;
  541. if (count == 0)
  542. return;
  543. for (end = entry + count; entry != end; entry++) {
  544. switch(le16_to_cpu(entry->e_tag)) {
  545. case ACL_USER:
  546. uid = make_kuid(from, le32_to_cpu(entry->e_id));
  547. entry->e_id = cpu_to_le32(from_kuid(to, uid));
  548. break;
  549. case ACL_GROUP:
  550. gid = make_kgid(from, le32_to_cpu(entry->e_id));
  551. entry->e_id = cpu_to_le32(from_kgid(to, gid));
  552. break;
  553. default:
  554. break;
  555. }
  556. }
  557. }
  558. void posix_acl_fix_xattr_from_user(void *value, size_t size)
  559. {
  560. struct user_namespace *user_ns = current_user_ns();
  561. if (user_ns == &init_user_ns)
  562. return;
  563. posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
  564. }
  565. void posix_acl_fix_xattr_to_user(void *value, size_t size)
  566. {
  567. struct user_namespace *user_ns = current_user_ns();
  568. if (user_ns == &init_user_ns)
  569. return;
  570. posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
  571. }
  572. /*
  573. * Convert from extended attribute to in-memory representation.
  574. */
  575. struct posix_acl *
  576. posix_acl_from_xattr(struct user_namespace *user_ns,
  577. const void *value, size_t size)
  578. {
  579. posix_acl_xattr_header *header = (posix_acl_xattr_header *)value;
  580. posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end;
  581. int count;
  582. struct posix_acl *acl;
  583. struct posix_acl_entry *acl_e;
  584. if (!value)
  585. return NULL;
  586. if (size < sizeof(posix_acl_xattr_header))
  587. return ERR_PTR(-EINVAL);
  588. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  589. return ERR_PTR(-EOPNOTSUPP);
  590. count = posix_acl_xattr_count(size);
  591. if (count < 0)
  592. return ERR_PTR(-EINVAL);
  593. if (count == 0)
  594. return NULL;
  595. acl = posix_acl_alloc(count, GFP_NOFS);
  596. if (!acl)
  597. return ERR_PTR(-ENOMEM);
  598. acl_e = acl->a_entries;
  599. for (end = entry + count; entry != end; acl_e++, entry++) {
  600. acl_e->e_tag = le16_to_cpu(entry->e_tag);
  601. acl_e->e_perm = le16_to_cpu(entry->e_perm);
  602. switch(acl_e->e_tag) {
  603. case ACL_USER_OBJ:
  604. case ACL_GROUP_OBJ:
  605. case ACL_MASK:
  606. case ACL_OTHER:
  607. break;
  608. case ACL_USER:
  609. acl_e->e_uid =
  610. make_kuid(user_ns,
  611. le32_to_cpu(entry->e_id));
  612. if (!uid_valid(acl_e->e_uid))
  613. goto fail;
  614. break;
  615. case ACL_GROUP:
  616. acl_e->e_gid =
  617. make_kgid(user_ns,
  618. le32_to_cpu(entry->e_id));
  619. if (!gid_valid(acl_e->e_gid))
  620. goto fail;
  621. break;
  622. default:
  623. goto fail;
  624. }
  625. }
  626. return acl;
  627. fail:
  628. posix_acl_release(acl);
  629. return ERR_PTR(-EINVAL);
  630. }
  631. EXPORT_SYMBOL (posix_acl_from_xattr);
  632. /*
  633. * Convert from in-memory to extended attribute representation.
  634. */
  635. int
  636. posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
  637. void *buffer, size_t size)
  638. {
  639. posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer;
  640. posix_acl_xattr_entry *ext_entry;
  641. int real_size, n;
  642. real_size = posix_acl_xattr_size(acl->a_count);
  643. if (!buffer)
  644. return real_size;
  645. if (real_size > size)
  646. return -ERANGE;
  647. ext_entry = ext_acl->a_entries;
  648. ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  649. for (n=0; n < acl->a_count; n++, ext_entry++) {
  650. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  651. ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
  652. ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
  653. switch(acl_e->e_tag) {
  654. case ACL_USER:
  655. ext_entry->e_id =
  656. cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
  657. break;
  658. case ACL_GROUP:
  659. ext_entry->e_id =
  660. cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
  661. break;
  662. default:
  663. ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  664. break;
  665. }
  666. }
  667. return real_size;
  668. }
  669. EXPORT_SYMBOL (posix_acl_to_xattr);
  670. static int
  671. posix_acl_xattr_get(struct dentry *dentry, const char *name,
  672. void *value, size_t size, int type)
  673. {
  674. struct posix_acl *acl;
  675. int error;
  676. if (!IS_POSIXACL(dentry->d_inode))
  677. return -EOPNOTSUPP;
  678. if (S_ISLNK(dentry->d_inode->i_mode))
  679. return -EOPNOTSUPP;
  680. acl = get_acl(dentry->d_inode, type);
  681. if (IS_ERR(acl))
  682. return PTR_ERR(acl);
  683. if (acl == NULL)
  684. return -ENODATA;
  685. error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  686. posix_acl_release(acl);
  687. return error;
  688. }
  689. static int
  690. posix_acl_xattr_set(struct dentry *dentry, const char *name,
  691. const void *value, size_t size, int flags, int type)
  692. {
  693. struct inode *inode = dentry->d_inode;
  694. struct posix_acl *acl = NULL;
  695. int ret;
  696. if (!IS_POSIXACL(inode))
  697. return -EOPNOTSUPP;
  698. if (!inode->i_op->set_acl)
  699. return -EOPNOTSUPP;
  700. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  701. return value ? -EACCES : 0;
  702. if (!inode_owner_or_capable(inode))
  703. return -EPERM;
  704. if (value) {
  705. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  706. if (IS_ERR(acl))
  707. return PTR_ERR(acl);
  708. if (acl) {
  709. ret = posix_acl_valid(acl);
  710. if (ret)
  711. goto out;
  712. }
  713. }
  714. ret = inode->i_op->set_acl(inode, acl, type);
  715. out:
  716. posix_acl_release(acl);
  717. return ret;
  718. }
  719. static size_t
  720. posix_acl_xattr_list(struct dentry *dentry, char *list, size_t list_size,
  721. const char *name, size_t name_len, int type)
  722. {
  723. const char *xname;
  724. size_t size;
  725. if (!IS_POSIXACL(dentry->d_inode))
  726. return -EOPNOTSUPP;
  727. if (S_ISLNK(dentry->d_inode->i_mode))
  728. return -EOPNOTSUPP;
  729. if (type == ACL_TYPE_ACCESS)
  730. xname = POSIX_ACL_XATTR_ACCESS;
  731. else
  732. xname = POSIX_ACL_XATTR_DEFAULT;
  733. size = strlen(xname) + 1;
  734. if (list && size <= list_size)
  735. memcpy(list, xname, size);
  736. return size;
  737. }
  738. const struct xattr_handler posix_acl_access_xattr_handler = {
  739. .prefix = POSIX_ACL_XATTR_ACCESS,
  740. .flags = ACL_TYPE_ACCESS,
  741. .list = posix_acl_xattr_list,
  742. .get = posix_acl_xattr_get,
  743. .set = posix_acl_xattr_set,
  744. };
  745. EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
  746. const struct xattr_handler posix_acl_default_xattr_handler = {
  747. .prefix = POSIX_ACL_XATTR_DEFAULT,
  748. .flags = ACL_TYPE_DEFAULT,
  749. .list = posix_acl_xattr_list,
  750. .get = posix_acl_xattr_get,
  751. .set = posix_acl_xattr_set,
  752. };
  753. EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
  754. int simple_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  755. {
  756. int error;
  757. if (type == ACL_TYPE_ACCESS) {
  758. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  759. if (error < 0)
  760. return 0;
  761. if (error == 0)
  762. acl = NULL;
  763. }
  764. inode->i_ctime = CURRENT_TIME;
  765. set_cached_acl(inode, type, acl);
  766. return 0;
  767. }
  768. int simple_acl_create(struct inode *dir, struct inode *inode)
  769. {
  770. struct posix_acl *default_acl, *acl;
  771. int error;
  772. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  773. if (error)
  774. return error;
  775. set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  776. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  777. if (default_acl)
  778. posix_acl_release(default_acl);
  779. if (acl)
  780. posix_acl_release(acl);
  781. return 0;
  782. }