posix_acl.c 8.5 KB

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