posix_acl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. 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. want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
  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, umode_t *mode_p)
  257. {
  258. struct posix_acl_entry *pa, *pe;
  259. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  260. umode_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, umode_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, umode_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, umode_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);