posix_acl.c 9.2 KB

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