acl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * linux/fs/ext2/acl.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. */
  6. #include <linux/capability.h>
  7. #include <linux/init.h>
  8. #include <linux/sched.h>
  9. #include <linux/slab.h>
  10. #include <linux/fs.h>
  11. #include "ext2.h"
  12. #include "xattr.h"
  13. #include "acl.h"
  14. /*
  15. * Convert from filesystem to in-memory representation.
  16. */
  17. static struct posix_acl *
  18. ext2_acl_from_disk(const void *value, size_t size)
  19. {
  20. const char *end = (char *)value + size;
  21. int n, count;
  22. struct posix_acl *acl;
  23. if (!value)
  24. return NULL;
  25. if (size < sizeof(ext2_acl_header))
  26. return ERR_PTR(-EINVAL);
  27. if (((ext2_acl_header *)value)->a_version !=
  28. cpu_to_le32(EXT2_ACL_VERSION))
  29. return ERR_PTR(-EINVAL);
  30. value = (char *)value + sizeof(ext2_acl_header);
  31. count = ext2_acl_count(size);
  32. if (count < 0)
  33. return ERR_PTR(-EINVAL);
  34. if (count == 0)
  35. return NULL;
  36. acl = posix_acl_alloc(count, GFP_KERNEL);
  37. if (!acl)
  38. return ERR_PTR(-ENOMEM);
  39. for (n=0; n < count; n++) {
  40. ext2_acl_entry *entry =
  41. (ext2_acl_entry *)value;
  42. if ((char *)value + sizeof(ext2_acl_entry_short) > end)
  43. goto fail;
  44. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  45. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  46. switch(acl->a_entries[n].e_tag) {
  47. case ACL_USER_OBJ:
  48. case ACL_GROUP_OBJ:
  49. case ACL_MASK:
  50. case ACL_OTHER:
  51. value = (char *)value +
  52. sizeof(ext2_acl_entry_short);
  53. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  54. break;
  55. case ACL_USER:
  56. case ACL_GROUP:
  57. value = (char *)value + sizeof(ext2_acl_entry);
  58. if ((char *)value > end)
  59. goto fail;
  60. acl->a_entries[n].e_id =
  61. le32_to_cpu(entry->e_id);
  62. break;
  63. default:
  64. goto fail;
  65. }
  66. }
  67. if (value != end)
  68. goto fail;
  69. return acl;
  70. fail:
  71. posix_acl_release(acl);
  72. return ERR_PTR(-EINVAL);
  73. }
  74. /*
  75. * Convert from in-memory to filesystem representation.
  76. */
  77. static void *
  78. ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
  79. {
  80. ext2_acl_header *ext_acl;
  81. char *e;
  82. size_t n;
  83. *size = ext2_acl_size(acl->a_count);
  84. ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
  85. sizeof(ext2_acl_entry), GFP_KERNEL);
  86. if (!ext_acl)
  87. return ERR_PTR(-ENOMEM);
  88. ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
  89. e = (char *)ext_acl + sizeof(ext2_acl_header);
  90. for (n=0; n < acl->a_count; n++) {
  91. ext2_acl_entry *entry = (ext2_acl_entry *)e;
  92. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  93. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  94. switch(acl->a_entries[n].e_tag) {
  95. case ACL_USER:
  96. case ACL_GROUP:
  97. entry->e_id =
  98. cpu_to_le32(acl->a_entries[n].e_id);
  99. e += sizeof(ext2_acl_entry);
  100. break;
  101. case ACL_USER_OBJ:
  102. case ACL_GROUP_OBJ:
  103. case ACL_MASK:
  104. case ACL_OTHER:
  105. e += sizeof(ext2_acl_entry_short);
  106. break;
  107. default:
  108. goto fail;
  109. }
  110. }
  111. return (char *)ext_acl;
  112. fail:
  113. kfree(ext_acl);
  114. return ERR_PTR(-EINVAL);
  115. }
  116. /*
  117. * inode->i_mutex: don't care
  118. */
  119. static struct posix_acl *
  120. ext2_get_acl(struct inode *inode, int type)
  121. {
  122. int name_index;
  123. char *value = NULL;
  124. struct posix_acl *acl;
  125. int retval;
  126. if (!test_opt(inode->i_sb, POSIX_ACL))
  127. return NULL;
  128. acl = get_cached_acl(inode, type);
  129. if (acl != ACL_NOT_CACHED)
  130. return acl;
  131. switch (type) {
  132. case ACL_TYPE_ACCESS:
  133. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  134. break;
  135. case ACL_TYPE_DEFAULT:
  136. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  137. break;
  138. default:
  139. BUG();
  140. }
  141. retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
  142. if (retval > 0) {
  143. value = kmalloc(retval, GFP_KERNEL);
  144. if (!value)
  145. return ERR_PTR(-ENOMEM);
  146. retval = ext2_xattr_get(inode, name_index, "", value, retval);
  147. }
  148. if (retval > 0)
  149. acl = ext2_acl_from_disk(value, retval);
  150. else if (retval == -ENODATA || retval == -ENOSYS)
  151. acl = NULL;
  152. else
  153. acl = ERR_PTR(retval);
  154. kfree(value);
  155. if (!IS_ERR(acl))
  156. set_cached_acl(inode, type, acl);
  157. return acl;
  158. }
  159. /*
  160. * inode->i_mutex: down
  161. */
  162. static int
  163. ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  164. {
  165. int name_index;
  166. void *value = NULL;
  167. size_t size = 0;
  168. int error;
  169. if (S_ISLNK(inode->i_mode))
  170. return -EOPNOTSUPP;
  171. if (!test_opt(inode->i_sb, POSIX_ACL))
  172. return 0;
  173. switch(type) {
  174. case ACL_TYPE_ACCESS:
  175. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  176. if (acl) {
  177. mode_t mode = inode->i_mode;
  178. error = posix_acl_equiv_mode(acl, &mode);
  179. if (error < 0)
  180. return error;
  181. else {
  182. inode->i_mode = mode;
  183. mark_inode_dirty(inode);
  184. if (error == 0)
  185. acl = NULL;
  186. }
  187. }
  188. break;
  189. case ACL_TYPE_DEFAULT:
  190. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  191. if (!S_ISDIR(inode->i_mode))
  192. return acl ? -EACCES : 0;
  193. break;
  194. default:
  195. return -EINVAL;
  196. }
  197. if (acl) {
  198. value = ext2_acl_to_disk(acl, &size);
  199. if (IS_ERR(value))
  200. return (int)PTR_ERR(value);
  201. }
  202. error = ext2_xattr_set(inode, name_index, "", value, size, 0);
  203. kfree(value);
  204. if (!error)
  205. set_cached_acl(inode, type, acl);
  206. return error;
  207. }
  208. static int
  209. ext2_check_acl(struct inode *inode, int mask)
  210. {
  211. struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  212. if (IS_ERR(acl))
  213. return PTR_ERR(acl);
  214. if (acl) {
  215. int error = posix_acl_permission(inode, acl, mask);
  216. posix_acl_release(acl);
  217. return error;
  218. }
  219. return -EAGAIN;
  220. }
  221. int
  222. ext2_permission(struct inode *inode, int mask)
  223. {
  224. return generic_permission(inode, mask, ext2_check_acl);
  225. }
  226. /*
  227. * Initialize the ACLs of a new inode. Called from ext2_new_inode.
  228. *
  229. * dir->i_mutex: down
  230. * inode->i_mutex: up (access to inode is still exclusive)
  231. */
  232. int
  233. ext2_init_acl(struct inode *inode, struct inode *dir)
  234. {
  235. struct posix_acl *acl = NULL;
  236. int error = 0;
  237. if (!S_ISLNK(inode->i_mode)) {
  238. if (test_opt(dir->i_sb, POSIX_ACL)) {
  239. acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
  240. if (IS_ERR(acl))
  241. return PTR_ERR(acl);
  242. }
  243. if (!acl)
  244. inode->i_mode &= ~current_umask();
  245. }
  246. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  247. struct posix_acl *clone;
  248. mode_t mode;
  249. if (S_ISDIR(inode->i_mode)) {
  250. error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  251. if (error)
  252. goto cleanup;
  253. }
  254. clone = posix_acl_clone(acl, GFP_KERNEL);
  255. error = -ENOMEM;
  256. if (!clone)
  257. goto cleanup;
  258. mode = inode->i_mode;
  259. error = posix_acl_create_masq(clone, &mode);
  260. if (error >= 0) {
  261. inode->i_mode = mode;
  262. if (error > 0) {
  263. /* This is an extended ACL */
  264. error = ext2_set_acl(inode,
  265. ACL_TYPE_ACCESS, clone);
  266. }
  267. }
  268. posix_acl_release(clone);
  269. }
  270. cleanup:
  271. posix_acl_release(acl);
  272. return error;
  273. }
  274. /*
  275. * Does chmod for an inode that may have an Access Control List. The
  276. * inode->i_mode field must be updated to the desired value by the caller
  277. * before calling this function.
  278. * Returns 0 on success, or a negative error number.
  279. *
  280. * We change the ACL rather than storing some ACL entries in the file
  281. * mode permission bits (which would be more efficient), because that
  282. * would break once additional permissions (like ACL_APPEND, ACL_DELETE
  283. * for directories) are added. There are no more bits available in the
  284. * file mode.
  285. *
  286. * inode->i_mutex: down
  287. */
  288. int
  289. ext2_acl_chmod(struct inode *inode)
  290. {
  291. struct posix_acl *acl, *clone;
  292. int error;
  293. if (!test_opt(inode->i_sb, POSIX_ACL))
  294. return 0;
  295. if (S_ISLNK(inode->i_mode))
  296. return -EOPNOTSUPP;
  297. acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  298. if (IS_ERR(acl) || !acl)
  299. return PTR_ERR(acl);
  300. clone = posix_acl_clone(acl, GFP_KERNEL);
  301. posix_acl_release(acl);
  302. if (!clone)
  303. return -ENOMEM;
  304. error = posix_acl_chmod_masq(clone, inode->i_mode);
  305. if (!error)
  306. error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
  307. posix_acl_release(clone);
  308. return error;
  309. }
  310. /*
  311. * Extended attribut handlers
  312. */
  313. static size_t
  314. ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
  315. const char *name, size_t name_len)
  316. {
  317. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  318. if (!test_opt(inode->i_sb, POSIX_ACL))
  319. return 0;
  320. if (list && size <= list_size)
  321. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  322. return size;
  323. }
  324. static size_t
  325. ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
  326. const char *name, size_t name_len)
  327. {
  328. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  329. if (!test_opt(inode->i_sb, POSIX_ACL))
  330. return 0;
  331. if (list && size <= list_size)
  332. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  333. return size;
  334. }
  335. static int
  336. ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
  337. {
  338. struct posix_acl *acl;
  339. int error;
  340. if (!test_opt(inode->i_sb, POSIX_ACL))
  341. return -EOPNOTSUPP;
  342. acl = ext2_get_acl(inode, type);
  343. if (IS_ERR(acl))
  344. return PTR_ERR(acl);
  345. if (acl == NULL)
  346. return -ENODATA;
  347. error = posix_acl_to_xattr(acl, buffer, size);
  348. posix_acl_release(acl);
  349. return error;
  350. }
  351. static int
  352. ext2_xattr_get_acl_access(struct inode *inode, const char *name,
  353. void *buffer, size_t size)
  354. {
  355. if (strcmp(name, "") != 0)
  356. return -EINVAL;
  357. return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  358. }
  359. static int
  360. ext2_xattr_get_acl_default(struct inode *inode, const char *name,
  361. void *buffer, size_t size)
  362. {
  363. if (strcmp(name, "") != 0)
  364. return -EINVAL;
  365. return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  366. }
  367. static int
  368. ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
  369. size_t size)
  370. {
  371. struct posix_acl *acl;
  372. int error;
  373. if (!test_opt(inode->i_sb, POSIX_ACL))
  374. return -EOPNOTSUPP;
  375. if (!is_owner_or_cap(inode))
  376. return -EPERM;
  377. if (value) {
  378. acl = posix_acl_from_xattr(value, size);
  379. if (IS_ERR(acl))
  380. return PTR_ERR(acl);
  381. else if (acl) {
  382. error = posix_acl_valid(acl);
  383. if (error)
  384. goto release_and_out;
  385. }
  386. } else
  387. acl = NULL;
  388. error = ext2_set_acl(inode, type, acl);
  389. release_and_out:
  390. posix_acl_release(acl);
  391. return error;
  392. }
  393. static int
  394. ext2_xattr_set_acl_access(struct inode *inode, const char *name,
  395. const void *value, size_t size, int flags)
  396. {
  397. if (strcmp(name, "") != 0)
  398. return -EINVAL;
  399. return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  400. }
  401. static int
  402. ext2_xattr_set_acl_default(struct inode *inode, const char *name,
  403. const void *value, size_t size, int flags)
  404. {
  405. if (strcmp(name, "") != 0)
  406. return -EINVAL;
  407. return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  408. }
  409. struct xattr_handler ext2_xattr_acl_access_handler = {
  410. .prefix = POSIX_ACL_XATTR_ACCESS,
  411. .list = ext2_xattr_list_acl_access,
  412. .get = ext2_xattr_get_acl_access,
  413. .set = ext2_xattr_set_acl_access,
  414. };
  415. struct xattr_handler ext2_xattr_acl_default_handler = {
  416. .prefix = POSIX_ACL_XATTR_DEFAULT,
  417. .list = ext2_xattr_list_acl_default,
  418. .get = ext2_xattr_get_acl_default,
  419. .set = ext2_xattr_set_acl_default,
  420. };