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. kuid_t prev_uid = INVALID_UID;
  73. kgid_t prev_gid = INVALID_GID;
  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. state = ACL_USER;
  82. break;
  83. }
  84. return -EINVAL;
  85. case ACL_USER:
  86. if (state != ACL_USER)
  87. return -EINVAL;
  88. if (!uid_valid(pa->e_uid))
  89. return -EINVAL;
  90. if (uid_valid(prev_uid) &&
  91. uid_lte(pa->e_uid, prev_uid))
  92. return -EINVAL;
  93. prev_uid = pa->e_uid;
  94. needs_mask = 1;
  95. break;
  96. case ACL_GROUP_OBJ:
  97. if (state == ACL_USER) {
  98. state = ACL_GROUP;
  99. break;
  100. }
  101. return -EINVAL;
  102. case ACL_GROUP:
  103. if (state != ACL_GROUP)
  104. return -EINVAL;
  105. if (!gid_valid(pa->e_gid))
  106. return -EINVAL;
  107. if (gid_valid(prev_gid) &&
  108. gid_lte(pa->e_gid, prev_gid))
  109. return -EINVAL;
  110. prev_gid = pa->e_gid;
  111. needs_mask = 1;
  112. break;
  113. case ACL_MASK:
  114. if (state != ACL_GROUP)
  115. return -EINVAL;
  116. state = ACL_OTHER;
  117. break;
  118. case ACL_OTHER:
  119. if (state == ACL_OTHER ||
  120. (state == ACL_GROUP && !needs_mask)) {
  121. state = 0;
  122. break;
  123. }
  124. return -EINVAL;
  125. default:
  126. return -EINVAL;
  127. }
  128. }
  129. if (state == 0)
  130. return 0;
  131. return -EINVAL;
  132. }
  133. /*
  134. * Returns 0 if the acl can be exactly represented in the traditional
  135. * file mode permission bits, or else 1. Returns -E... on error.
  136. */
  137. int
  138. posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
  139. {
  140. const struct posix_acl_entry *pa, *pe;
  141. umode_t mode = 0;
  142. int not_equiv = 0;
  143. FOREACH_ACL_ENTRY(pa, acl, pe) {
  144. switch (pa->e_tag) {
  145. case ACL_USER_OBJ:
  146. mode |= (pa->e_perm & S_IRWXO) << 6;
  147. break;
  148. case ACL_GROUP_OBJ:
  149. mode |= (pa->e_perm & S_IRWXO) << 3;
  150. break;
  151. case ACL_OTHER:
  152. mode |= pa->e_perm & S_IRWXO;
  153. break;
  154. case ACL_MASK:
  155. mode = (mode & ~S_IRWXG) |
  156. ((pa->e_perm & S_IRWXO) << 3);
  157. not_equiv = 1;
  158. break;
  159. case ACL_USER:
  160. case ACL_GROUP:
  161. not_equiv = 1;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. }
  167. if (mode_p)
  168. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  169. return not_equiv;
  170. }
  171. /*
  172. * Create an ACL representing the file mode permission bits of an inode.
  173. */
  174. struct posix_acl *
  175. posix_acl_from_mode(umode_t mode, gfp_t flags)
  176. {
  177. struct posix_acl *acl = posix_acl_alloc(3, flags);
  178. if (!acl)
  179. return ERR_PTR(-ENOMEM);
  180. acl->a_entries[0].e_tag = ACL_USER_OBJ;
  181. acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  182. acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
  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_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 (uid_eq(inode->i_uid, current_fsuid()))
  203. goto check_perm;
  204. break;
  205. case ACL_USER:
  206. if (uid_eq(pa->e_uid, 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_gid)) {
  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);