posix_acl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * linux/fs/posix_acl.c
  3. *
  4. * Copyright (C) 2002 by Andreas Gruenbacher <a.gruenbacher@computer.org>
  5. *
  6. * Fixes from William Schumacher incorporated on 15 March 2001.
  7. * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
  8. */
  9. /*
  10. * This file contains generic functions for manipulating
  11. * POSIX 1003.1e draft standard 17 ACLs.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/atomic.h>
  16. #include <linux/fs.h>
  17. #include <linux/sched.h>
  18. #include <linux/posix_acl.h>
  19. #include <linux/export.h>
  20. #include <linux/errno.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. /*
  27. * Init a fresh posix_acl
  28. */
  29. void
  30. posix_acl_init(struct posix_acl *acl, int count)
  31. {
  32. atomic_set(&acl->a_refcount, 1);
  33. acl->a_count = count;
  34. }
  35. /*
  36. * Allocate a new ACL with the specified number of entries.
  37. */
  38. struct posix_acl *
  39. posix_acl_alloc(int count, gfp_t flags)
  40. {
  41. const size_t size = sizeof(struct posix_acl) +
  42. count * sizeof(struct posix_acl_entry);
  43. struct posix_acl *acl = kmalloc(size, flags);
  44. if (acl)
  45. posix_acl_init(acl, count);
  46. return acl;
  47. }
  48. /*
  49. * Clone an ACL.
  50. */
  51. static struct posix_acl *
  52. posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
  53. {
  54. struct posix_acl *clone = NULL;
  55. if (acl) {
  56. int size = sizeof(struct posix_acl) + acl->a_count *
  57. sizeof(struct posix_acl_entry);
  58. clone = kmemdup(acl, size, flags);
  59. if (clone)
  60. atomic_set(&clone->a_refcount, 1);
  61. }
  62. return clone;
  63. }
  64. /*
  65. * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
  66. */
  67. int
  68. posix_acl_valid(const struct posix_acl *acl)
  69. {
  70. const struct posix_acl_entry *pa, *pe;
  71. int state = ACL_USER_OBJ;
  72. unsigned int id = 0; /* keep gcc happy */
  73. int needs_mask = 0;
  74. FOREACH_ACL_ENTRY(pa, acl, pe) {
  75. if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
  76. return -EINVAL;
  77. switch (pa->e_tag) {
  78. case ACL_USER_OBJ:
  79. if (state == ACL_USER_OBJ) {
  80. id = 0;
  81. state = ACL_USER;
  82. break;
  83. }
  84. return -EINVAL;
  85. case ACL_USER:
  86. if (state != ACL_USER)
  87. return -EINVAL;
  88. if (pa->e_id == ACL_UNDEFINED_ID ||
  89. pa->e_id < id)
  90. return -EINVAL;
  91. id = pa->e_id + 1;
  92. needs_mask = 1;
  93. break;
  94. case ACL_GROUP_OBJ:
  95. if (state == ACL_USER) {
  96. id = 0;
  97. state = ACL_GROUP;
  98. break;
  99. }
  100. return -EINVAL;
  101. case ACL_GROUP:
  102. if (state != ACL_GROUP)
  103. return -EINVAL;
  104. if (pa->e_id == ACL_UNDEFINED_ID ||
  105. pa->e_id < id)
  106. return -EINVAL;
  107. id = pa->e_id + 1;
  108. needs_mask = 1;
  109. break;
  110. case ACL_MASK:
  111. if (state != ACL_GROUP)
  112. return -EINVAL;
  113. state = ACL_OTHER;
  114. break;
  115. case ACL_OTHER:
  116. if (state == ACL_OTHER ||
  117. (state == ACL_GROUP && !needs_mask)) {
  118. state = 0;
  119. break;
  120. }
  121. return -EINVAL;
  122. default:
  123. return -EINVAL;
  124. }
  125. }
  126. if (state == 0)
  127. return 0;
  128. return -EINVAL;
  129. }
  130. /*
  131. * Returns 0 if the acl can be exactly represented in the traditional
  132. * file mode permission bits, or else 1. Returns -E... on error.
  133. */
  134. int
  135. posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
  136. {
  137. const struct posix_acl_entry *pa, *pe;
  138. umode_t mode = 0;
  139. int not_equiv = 0;
  140. /*
  141. * A null ACL can always be presented as mode bits.
  142. */
  143. if (!acl)
  144. return 0;
  145. FOREACH_ACL_ENTRY(pa, acl, pe) {
  146. switch (pa->e_tag) {
  147. case ACL_USER_OBJ:
  148. mode |= (pa->e_perm & S_IRWXO) << 6;
  149. break;
  150. case ACL_GROUP_OBJ:
  151. mode |= (pa->e_perm & S_IRWXO) << 3;
  152. break;
  153. case ACL_OTHER:
  154. mode |= pa->e_perm & S_IRWXO;
  155. break;
  156. case ACL_MASK:
  157. mode = (mode & ~S_IRWXG) |
  158. ((pa->e_perm & S_IRWXO) << 3);
  159. not_equiv = 1;
  160. break;
  161. case ACL_USER:
  162. case ACL_GROUP:
  163. not_equiv = 1;
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. }
  169. if (mode_p)
  170. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  171. return not_equiv;
  172. }
  173. /*
  174. * Create an ACL representing the file mode permission bits of an inode.
  175. */
  176. struct posix_acl *
  177. posix_acl_from_mode(umode_t mode, gfp_t flags)
  178. {
  179. struct posix_acl *acl = posix_acl_alloc(3, flags);
  180. if (!acl)
  181. return ERR_PTR(-ENOMEM);
  182. acl->a_entries[0].e_tag = ACL_USER_OBJ;
  183. acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
  184. acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  185. acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
  186. acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
  187. acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
  188. acl->a_entries[2].e_tag = ACL_OTHER;
  189. acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
  190. acl->a_entries[2].e_perm = (mode & S_IRWXO);
  191. return acl;
  192. }
  193. /*
  194. * Return 0 if current is granted want access to the inode
  195. * by the acl. Returns -E... otherwise.
  196. */
  197. int
  198. posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
  199. {
  200. const struct posix_acl_entry *pa, *pe, *mask_obj;
  201. int found = 0;
  202. want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
  203. FOREACH_ACL_ENTRY(pa, acl, pe) {
  204. switch(pa->e_tag) {
  205. case ACL_USER_OBJ:
  206. /* (May have been checked already) */
  207. if (inode->i_uid == current_fsuid())
  208. goto check_perm;
  209. break;
  210. case ACL_USER:
  211. if (pa->e_id == current_fsuid())
  212. goto mask;
  213. break;
  214. case ACL_GROUP_OBJ:
  215. if (in_group_p(inode->i_gid)) {
  216. found = 1;
  217. if ((pa->e_perm & want) == want)
  218. goto mask;
  219. }
  220. break;
  221. case ACL_GROUP:
  222. if (in_group_p(pa->e_id)) {
  223. found = 1;
  224. if ((pa->e_perm & want) == want)
  225. goto mask;
  226. }
  227. break;
  228. case ACL_MASK:
  229. break;
  230. case ACL_OTHER:
  231. if (found)
  232. return -EACCES;
  233. else
  234. goto check_perm;
  235. default:
  236. return -EIO;
  237. }
  238. }
  239. return -EIO;
  240. mask:
  241. for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
  242. if (mask_obj->e_tag == ACL_MASK) {
  243. if ((pa->e_perm & mask_obj->e_perm & want) == want)
  244. return 0;
  245. return -EACCES;
  246. }
  247. }
  248. check_perm:
  249. if ((pa->e_perm & want) == want)
  250. return 0;
  251. return -EACCES;
  252. }
  253. /*
  254. * Modify acl when creating a new inode. The caller must ensure the acl is
  255. * only referenced once.
  256. *
  257. * mode_p initially must contain the mode parameter to the open() / creat()
  258. * system calls. All permissions that are not granted by the acl are removed.
  259. * The permissions in the acl are changed to reflect the mode_p parameter.
  260. */
  261. static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  262. {
  263. struct posix_acl_entry *pa, *pe;
  264. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  265. umode_t mode = *mode_p;
  266. int not_equiv = 0;
  267. /* assert(atomic_read(acl->a_refcount) == 1); */
  268. FOREACH_ACL_ENTRY(pa, acl, pe) {
  269. switch(pa->e_tag) {
  270. case ACL_USER_OBJ:
  271. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  272. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  273. break;
  274. case ACL_USER:
  275. case ACL_GROUP:
  276. not_equiv = 1;
  277. break;
  278. case ACL_GROUP_OBJ:
  279. group_obj = pa;
  280. break;
  281. case ACL_OTHER:
  282. pa->e_perm &= mode | ~S_IRWXO;
  283. mode &= pa->e_perm | ~S_IRWXO;
  284. break;
  285. case ACL_MASK:
  286. mask_obj = pa;
  287. not_equiv = 1;
  288. break;
  289. default:
  290. return -EIO;
  291. }
  292. }
  293. if (mask_obj) {
  294. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  295. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  296. } else {
  297. if (!group_obj)
  298. return -EIO;
  299. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  300. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  301. }
  302. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  303. return not_equiv;
  304. }
  305. /**
  306. * posix_acl_update_mode - update mode in set_acl
  307. *
  308. * Update the file mode when setting an ACL: compute the new file permission
  309. * bits based on the ACL. In addition, if the ACL is equivalent to the new
  310. * file mode, set *acl to NULL to indicate that no ACL should be set.
  311. *
  312. * As with chmod, clear the setgit bit if the caller is not in the owning group
  313. * or capable of CAP_FSETID (see inode_change_ok).
  314. *
  315. * Called from set_acl inode operations.
  316. */
  317. int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
  318. struct posix_acl **acl)
  319. {
  320. umode_t mode = inode->i_mode;
  321. int error;
  322. error = posix_acl_equiv_mode(*acl, &mode);
  323. if (error < 0)
  324. return error;
  325. if (error == 0)
  326. *acl = NULL;
  327. if (!in_group_p(inode->i_gid) &&
  328. !capable(CAP_FSETID))
  329. mode &= ~S_ISGID;
  330. *mode_p = mode;
  331. return 0;
  332. }
  333. EXPORT_SYMBOL(posix_acl_update_mode);
  334. /*
  335. * Modify the ACL for the chmod syscall.
  336. */
  337. static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
  338. {
  339. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  340. struct posix_acl_entry *pa, *pe;
  341. /* assert(atomic_read(acl->a_refcount) == 1); */
  342. FOREACH_ACL_ENTRY(pa, acl, pe) {
  343. switch(pa->e_tag) {
  344. case ACL_USER_OBJ:
  345. pa->e_perm = (mode & S_IRWXU) >> 6;
  346. break;
  347. case ACL_USER:
  348. case ACL_GROUP:
  349. break;
  350. case ACL_GROUP_OBJ:
  351. group_obj = pa;
  352. break;
  353. case ACL_MASK:
  354. mask_obj = pa;
  355. break;
  356. case ACL_OTHER:
  357. pa->e_perm = (mode & S_IRWXO);
  358. break;
  359. default:
  360. return -EIO;
  361. }
  362. }
  363. if (mask_obj) {
  364. mask_obj->e_perm = (mode & S_IRWXG) >> 3;
  365. } else {
  366. if (!group_obj)
  367. return -EIO;
  368. group_obj->e_perm = (mode & S_IRWXG) >> 3;
  369. }
  370. return 0;
  371. }
  372. int
  373. posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
  374. {
  375. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  376. int err = -ENOMEM;
  377. if (clone) {
  378. err = posix_acl_create_masq(clone, mode_p);
  379. if (err < 0) {
  380. posix_acl_release(clone);
  381. clone = NULL;
  382. }
  383. }
  384. posix_acl_release(*acl);
  385. *acl = clone;
  386. return err;
  387. }
  388. EXPORT_SYMBOL(posix_acl_create);
  389. int
  390. posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
  391. {
  392. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  393. int err = -ENOMEM;
  394. if (clone) {
  395. err = posix_acl_chmod_masq(clone, mode);
  396. if (err) {
  397. posix_acl_release(clone);
  398. clone = NULL;
  399. }
  400. }
  401. posix_acl_release(*acl);
  402. *acl = clone;
  403. return err;
  404. }
  405. EXPORT_SYMBOL(posix_acl_chmod);