posix_acl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <asm/atomic.h>
  16. #include <linux/fs.h>
  17. #include <linux/sched.h>
  18. #include <linux/posix_acl.h>
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. EXPORT_SYMBOL(posix_acl_init);
  22. EXPORT_SYMBOL(posix_acl_alloc);
  23. EXPORT_SYMBOL(posix_acl_clone);
  24. EXPORT_SYMBOL(posix_acl_valid);
  25. EXPORT_SYMBOL(posix_acl_equiv_mode);
  26. EXPORT_SYMBOL(posix_acl_from_mode);
  27. EXPORT_SYMBOL(posix_acl_create_masq);
  28. EXPORT_SYMBOL(posix_acl_chmod_masq);
  29. EXPORT_SYMBOL(posix_acl_permission);
  30. /*
  31. * Init a fresh posix_acl
  32. */
  33. void
  34. posix_acl_init(struct posix_acl *acl, int count)
  35. {
  36. atomic_set(&acl->a_refcount, 1);
  37. acl->a_count = count;
  38. }
  39. /*
  40. * Allocate a new ACL with the specified number of entries.
  41. */
  42. struct posix_acl *
  43. posix_acl_alloc(int count, gfp_t flags)
  44. {
  45. const size_t size = sizeof(struct posix_acl) +
  46. count * sizeof(struct posix_acl_entry);
  47. struct posix_acl *acl = kmalloc(size, flags);
  48. if (acl)
  49. posix_acl_init(acl, count);
  50. return acl;
  51. }
  52. /*
  53. * Clone an ACL.
  54. */
  55. struct posix_acl *
  56. posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
  57. {
  58. struct posix_acl *clone = NULL;
  59. if (acl) {
  60. int size = sizeof(struct posix_acl) + acl->a_count *
  61. sizeof(struct posix_acl_entry);
  62. clone = kmemdup(acl, size, flags);
  63. if (clone)
  64. atomic_set(&clone->a_refcount, 1);
  65. }
  66. return clone;
  67. }
  68. /*
  69. * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
  70. */
  71. int
  72. posix_acl_valid(const struct posix_acl *acl)
  73. {
  74. const struct posix_acl_entry *pa, *pe;
  75. int state = ACL_USER_OBJ;
  76. unsigned int id = 0; /* keep gcc happy */
  77. int needs_mask = 0;
  78. FOREACH_ACL_ENTRY(pa, acl, pe) {
  79. if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
  80. return -EINVAL;
  81. switch (pa->e_tag) {
  82. case ACL_USER_OBJ:
  83. if (state == ACL_USER_OBJ) {
  84. id = 0;
  85. state = ACL_USER;
  86. break;
  87. }
  88. return -EINVAL;
  89. case ACL_USER:
  90. if (state != ACL_USER)
  91. return -EINVAL;
  92. if (pa->e_id == ACL_UNDEFINED_ID ||
  93. pa->e_id < id)
  94. return -EINVAL;
  95. id = pa->e_id + 1;
  96. needs_mask = 1;
  97. break;
  98. case ACL_GROUP_OBJ:
  99. if (state == ACL_USER) {
  100. id = 0;
  101. state = ACL_GROUP;
  102. break;
  103. }
  104. return -EINVAL;
  105. case ACL_GROUP:
  106. if (state != ACL_GROUP)
  107. return -EINVAL;
  108. if (pa->e_id == ACL_UNDEFINED_ID ||
  109. pa->e_id < id)
  110. return -EINVAL;
  111. id = pa->e_id + 1;
  112. needs_mask = 1;
  113. break;
  114. case ACL_MASK:
  115. if (state != ACL_GROUP)
  116. return -EINVAL;
  117. state = ACL_OTHER;
  118. break;
  119. case ACL_OTHER:
  120. if (state == ACL_OTHER ||
  121. (state == ACL_GROUP && !needs_mask)) {
  122. state = 0;
  123. break;
  124. }
  125. return -EINVAL;
  126. default:
  127. return -EINVAL;
  128. }
  129. }
  130. if (state == 0)
  131. return 0;
  132. return -EINVAL;
  133. }
  134. /*
  135. * Returns 0 if the acl can be exactly represented in the traditional
  136. * file mode permission bits, or else 1. Returns -E... on error.
  137. */
  138. int
  139. posix_acl_equiv_mode(const struct posix_acl *acl, mode_t *mode_p)
  140. {
  141. const struct posix_acl_entry *pa, *pe;
  142. mode_t mode = 0;
  143. int not_equiv = 0;
  144. FOREACH_ACL_ENTRY(pa, acl, pe) {
  145. switch (pa->e_tag) {
  146. case ACL_USER_OBJ:
  147. mode |= (pa->e_perm & S_IRWXO) << 6;
  148. break;
  149. case ACL_GROUP_OBJ:
  150. mode |= (pa->e_perm & S_IRWXO) << 3;
  151. break;
  152. case ACL_OTHER:
  153. mode |= pa->e_perm & S_IRWXO;
  154. break;
  155. case ACL_MASK:
  156. mode = (mode & ~S_IRWXG) |
  157. ((pa->e_perm & S_IRWXO) << 3);
  158. not_equiv = 1;
  159. break;
  160. case ACL_USER:
  161. case ACL_GROUP:
  162. not_equiv = 1;
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. }
  168. if (mode_p)
  169. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  170. return not_equiv;
  171. }
  172. /*
  173. * Create an ACL representing the file mode permission bits of an inode.
  174. */
  175. struct posix_acl *
  176. posix_acl_from_mode(mode_t mode, gfp_t flags)
  177. {
  178. struct posix_acl *acl = posix_acl_alloc(3, flags);
  179. if (!acl)
  180. return ERR_PTR(-ENOMEM);
  181. acl->a_entries[0].e_tag = ACL_USER_OBJ;
  182. acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
  183. acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  184. acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
  185. acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
  186. acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
  187. acl->a_entries[2].e_tag = ACL_OTHER;
  188. acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
  189. acl->a_entries[2].e_perm = (mode & S_IRWXO);
  190. return acl;
  191. }
  192. /*
  193. * Return 0 if current is granted want access to the inode
  194. * by the acl. Returns -E... otherwise.
  195. */
  196. int
  197. posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
  198. {
  199. const struct posix_acl_entry *pa, *pe, *mask_obj;
  200. int found = 0;
  201. FOREACH_ACL_ENTRY(pa, acl, pe) {
  202. switch(pa->e_tag) {
  203. case ACL_USER_OBJ:
  204. /* (May have been checked already) */
  205. if (inode->i_uid == current_fsuid())
  206. goto check_perm;
  207. break;
  208. case ACL_USER:
  209. if (pa->e_id == current_fsuid())
  210. goto mask;
  211. break;
  212. case ACL_GROUP_OBJ:
  213. if (in_group_p(inode->i_gid)) {
  214. found = 1;
  215. if ((pa->e_perm & want) == want)
  216. goto mask;
  217. }
  218. break;
  219. case ACL_GROUP:
  220. if (in_group_p(pa->e_id)) {
  221. found = 1;
  222. if ((pa->e_perm & want) == want)
  223. goto mask;
  224. }
  225. break;
  226. case ACL_MASK:
  227. break;
  228. case ACL_OTHER:
  229. if (found)
  230. return -EACCES;
  231. else
  232. goto check_perm;
  233. default:
  234. return -EIO;
  235. }
  236. }
  237. return -EIO;
  238. mask:
  239. for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
  240. if (mask_obj->e_tag == ACL_MASK) {
  241. if ((pa->e_perm & mask_obj->e_perm & want) == want)
  242. return 0;
  243. return -EACCES;
  244. }
  245. }
  246. check_perm:
  247. if ((pa->e_perm & want) == want)
  248. return 0;
  249. return -EACCES;
  250. }
  251. /*
  252. * Modify acl when creating a new inode. The caller must ensure the acl is
  253. * only referenced once.
  254. *
  255. * mode_p initially must contain the mode parameter to the open() / creat()
  256. * system calls. All permissions that are not granted by the acl are removed.
  257. * The permissions in the acl are changed to reflect the mode_p parameter.
  258. */
  259. int
  260. posix_acl_create_masq(struct posix_acl *acl, mode_t *mode_p)
  261. {
  262. struct posix_acl_entry *pa, *pe;
  263. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  264. mode_t mode = *mode_p;
  265. int not_equiv = 0;
  266. /* assert(atomic_read(acl->a_refcount) == 1); */
  267. FOREACH_ACL_ENTRY(pa, acl, pe) {
  268. switch(pa->e_tag) {
  269. case ACL_USER_OBJ:
  270. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  271. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  272. break;
  273. case ACL_USER:
  274. case ACL_GROUP:
  275. not_equiv = 1;
  276. break;
  277. case ACL_GROUP_OBJ:
  278. group_obj = pa;
  279. break;
  280. case ACL_OTHER:
  281. pa->e_perm &= mode | ~S_IRWXO;
  282. mode &= pa->e_perm | ~S_IRWXO;
  283. break;
  284. case ACL_MASK:
  285. mask_obj = pa;
  286. not_equiv = 1;
  287. break;
  288. default:
  289. return -EIO;
  290. }
  291. }
  292. if (mask_obj) {
  293. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  294. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  295. } else {
  296. if (!group_obj)
  297. return -EIO;
  298. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  299. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  300. }
  301. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  302. return not_equiv;
  303. }
  304. /*
  305. * Modify the ACL for the chmod syscall.
  306. */
  307. int
  308. posix_acl_chmod_masq(struct posix_acl *acl, mode_t mode)
  309. {
  310. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  311. struct posix_acl_entry *pa, *pe;
  312. /* assert(atomic_read(acl->a_refcount) == 1); */
  313. FOREACH_ACL_ENTRY(pa, acl, pe) {
  314. switch(pa->e_tag) {
  315. case ACL_USER_OBJ:
  316. pa->e_perm = (mode & S_IRWXU) >> 6;
  317. break;
  318. case ACL_USER:
  319. case ACL_GROUP:
  320. break;
  321. case ACL_GROUP_OBJ:
  322. group_obj = pa;
  323. break;
  324. case ACL_MASK:
  325. mask_obj = pa;
  326. break;
  327. case ACL_OTHER:
  328. pa->e_perm = (mode & S_IRWXO);
  329. break;
  330. default:
  331. return -EIO;
  332. }
  333. }
  334. if (mask_obj) {
  335. mask_obj->e_perm = (mode & S_IRWXG) >> 3;
  336. } else {
  337. if (!group_obj)
  338. return -EIO;
  339. group_obj->e_perm = (mode & S_IRWXG) >> 3;
  340. }
  341. return 0;
  342. }