posix_acl.c 19 KB

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