posix_acl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/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. /*
  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. FOREACH_ACL_ENTRY(pa, acl, pe) {
  141. switch (pa->e_tag) {
  142. case ACL_USER_OBJ:
  143. mode |= (pa->e_perm & S_IRWXO) << 6;
  144. break;
  145. case ACL_GROUP_OBJ:
  146. mode |= (pa->e_perm & S_IRWXO) << 3;
  147. break;
  148. case ACL_OTHER:
  149. mode |= pa->e_perm & S_IRWXO;
  150. break;
  151. case ACL_MASK:
  152. mode = (mode & ~S_IRWXG) |
  153. ((pa->e_perm & S_IRWXO) << 3);
  154. not_equiv = 1;
  155. break;
  156. case ACL_USER:
  157. case ACL_GROUP:
  158. not_equiv = 1;
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. }
  164. if (mode_p)
  165. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  166. return not_equiv;
  167. }
  168. /*
  169. * Create an ACL representing the file mode permission bits of an inode.
  170. */
  171. struct posix_acl *
  172. posix_acl_from_mode(umode_t mode, gfp_t flags)
  173. {
  174. struct posix_acl *acl = posix_acl_alloc(3, flags);
  175. if (!acl)
  176. return ERR_PTR(-ENOMEM);
  177. acl->a_entries[0].e_tag = ACL_USER_OBJ;
  178. acl->a_entries[0].e_id = ACL_UNDEFINED_ID;
  179. acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  180. acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
  181. acl->a_entries[1].e_id = ACL_UNDEFINED_ID;
  182. acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
  183. acl->a_entries[2].e_tag = ACL_OTHER;
  184. acl->a_entries[2].e_id = ACL_UNDEFINED_ID;
  185. acl->a_entries[2].e_perm = (mode & S_IRWXO);
  186. return acl;
  187. }
  188. /*
  189. * Return 0 if current is granted want access to the inode
  190. * by the acl. Returns -E... otherwise.
  191. */
  192. int
  193. posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
  194. {
  195. const struct posix_acl_entry *pa, *pe, *mask_obj;
  196. int found = 0;
  197. FOREACH_ACL_ENTRY(pa, acl, pe) {
  198. switch(pa->e_tag) {
  199. case ACL_USER_OBJ:
  200. /* (May have been checked already) */
  201. if (inode->i_uid == current_fsuid())
  202. goto check_perm;
  203. break;
  204. case ACL_USER:
  205. if (pa->e_id == current_fsuid())
  206. goto mask;
  207. break;
  208. case ACL_GROUP_OBJ:
  209. if (in_group_p(inode->i_gid)) {
  210. found = 1;
  211. if ((pa->e_perm & want) == want)
  212. goto mask;
  213. }
  214. break;
  215. case ACL_GROUP:
  216. if (in_group_p(pa->e_id)) {
  217. found = 1;
  218. if ((pa->e_perm & want) == want)
  219. goto mask;
  220. }
  221. break;
  222. case ACL_MASK:
  223. break;
  224. case ACL_OTHER:
  225. if (found)
  226. return -EACCES;
  227. else
  228. goto check_perm;
  229. default:
  230. return -EIO;
  231. }
  232. }
  233. return -EIO;
  234. mask:
  235. for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
  236. if (mask_obj->e_tag == ACL_MASK) {
  237. if ((pa->e_perm & mask_obj->e_perm & want) == want)
  238. return 0;
  239. return -EACCES;
  240. }
  241. }
  242. check_perm:
  243. if ((pa->e_perm & want) == want)
  244. return 0;
  245. return -EACCES;
  246. }
  247. /*
  248. * Modify acl when creating a new inode. The caller must ensure the acl is
  249. * only referenced once.
  250. *
  251. * mode_p initially must contain the mode parameter to the open() / creat()
  252. * system calls. All permissions that are not granted by the acl are removed.
  253. * The permissions in the acl are changed to reflect the mode_p parameter.
  254. */
  255. static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  256. {
  257. struct posix_acl_entry *pa, *pe;
  258. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  259. umode_t mode = *mode_p;
  260. int not_equiv = 0;
  261. /* assert(atomic_read(acl->a_refcount) == 1); */
  262. FOREACH_ACL_ENTRY(pa, acl, pe) {
  263. switch(pa->e_tag) {
  264. case ACL_USER_OBJ:
  265. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  266. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  267. break;
  268. case ACL_USER:
  269. case ACL_GROUP:
  270. not_equiv = 1;
  271. break;
  272. case ACL_GROUP_OBJ:
  273. group_obj = pa;
  274. break;
  275. case ACL_OTHER:
  276. pa->e_perm &= mode | ~S_IRWXO;
  277. mode &= pa->e_perm | ~S_IRWXO;
  278. break;
  279. case ACL_MASK:
  280. mask_obj = pa;
  281. not_equiv = 1;
  282. break;
  283. default:
  284. return -EIO;
  285. }
  286. }
  287. if (mask_obj) {
  288. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  289. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  290. } else {
  291. if (!group_obj)
  292. return -EIO;
  293. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  294. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  295. }
  296. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  297. return not_equiv;
  298. }
  299. /*
  300. * Modify the ACL for the chmod syscall.
  301. */
  302. static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
  303. {
  304. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  305. struct posix_acl_entry *pa, *pe;
  306. /* assert(atomic_read(acl->a_refcount) == 1); */
  307. FOREACH_ACL_ENTRY(pa, acl, pe) {
  308. switch(pa->e_tag) {
  309. case ACL_USER_OBJ:
  310. pa->e_perm = (mode & S_IRWXU) >> 6;
  311. break;
  312. case ACL_USER:
  313. case ACL_GROUP:
  314. break;
  315. case ACL_GROUP_OBJ:
  316. group_obj = pa;
  317. break;
  318. case ACL_MASK:
  319. mask_obj = pa;
  320. break;
  321. case ACL_OTHER:
  322. pa->e_perm = (mode & S_IRWXO);
  323. break;
  324. default:
  325. return -EIO;
  326. }
  327. }
  328. if (mask_obj) {
  329. mask_obj->e_perm = (mode & S_IRWXG) >> 3;
  330. } else {
  331. if (!group_obj)
  332. return -EIO;
  333. group_obj->e_perm = (mode & S_IRWXG) >> 3;
  334. }
  335. return 0;
  336. }
  337. int
  338. posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
  339. {
  340. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  341. int err = -ENOMEM;
  342. if (clone) {
  343. err = posix_acl_create_masq(clone, mode_p);
  344. if (err < 0) {
  345. posix_acl_release(clone);
  346. clone = NULL;
  347. }
  348. }
  349. posix_acl_release(*acl);
  350. *acl = clone;
  351. return err;
  352. }
  353. EXPORT_SYMBOL(posix_acl_create);
  354. int
  355. posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
  356. {
  357. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  358. int err = -ENOMEM;
  359. if (clone) {
  360. err = posix_acl_chmod_masq(clone, mode);
  361. if (err) {
  362. posix_acl_release(clone);
  363. clone = NULL;
  364. }
  365. }
  366. posix_acl_release(*acl);
  367. *acl = clone;
  368. return err;
  369. }
  370. EXPORT_SYMBOL(posix_acl_chmod);