acl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. inode->i_ctime = CURRENT_TIME_SEC;
  184. mark_inode_dirty(inode);
  185. if (error == 0)
  186. acl = NULL;
  187. }
  188. }
  189. break;
  190. case ACL_TYPE_DEFAULT:
  191. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  192. if (!S_ISDIR(inode->i_mode))
  193. return acl ? -EACCES : 0;
  194. break;
  195. default:
  196. return -EINVAL;
  197. }
  198. if (acl) {
  199. value = ext2_acl_to_disk(acl, &size);
  200. if (IS_ERR(value))
  201. return (int)PTR_ERR(value);
  202. }
  203. error = ext2_xattr_set(inode, name_index, "", value, size, 0);
  204. kfree(value);
  205. if (!error)
  206. set_cached_acl(inode, type, acl);
  207. return error;
  208. }
  209. int
  210. ext2_check_acl(struct inode *inode, int mask)
  211. {
  212. struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  213. if (IS_ERR(acl))
  214. return PTR_ERR(acl);
  215. if (acl) {
  216. int error = posix_acl_permission(inode, acl, mask);
  217. posix_acl_release(acl);
  218. return error;
  219. }
  220. return -EAGAIN;
  221. }
  222. /*
  223. * Initialize the ACLs of a new inode. Called from ext2_new_inode.
  224. *
  225. * dir->i_mutex: down
  226. * inode->i_mutex: up (access to inode is still exclusive)
  227. */
  228. int
  229. ext2_init_acl(struct inode *inode, struct inode *dir)
  230. {
  231. struct posix_acl *acl = NULL;
  232. int error = 0;
  233. if (!S_ISLNK(inode->i_mode)) {
  234. if (test_opt(dir->i_sb, POSIX_ACL)) {
  235. acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
  236. if (IS_ERR(acl))
  237. return PTR_ERR(acl);
  238. }
  239. if (!acl)
  240. inode->i_mode &= ~current_umask();
  241. }
  242. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  243. struct posix_acl *clone;
  244. mode_t mode;
  245. if (S_ISDIR(inode->i_mode)) {
  246. error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  247. if (error)
  248. goto cleanup;
  249. }
  250. clone = posix_acl_clone(acl, GFP_KERNEL);
  251. error = -ENOMEM;
  252. if (!clone)
  253. goto cleanup;
  254. mode = inode->i_mode;
  255. error = posix_acl_create_masq(clone, &mode);
  256. if (error >= 0) {
  257. inode->i_mode = mode;
  258. if (error > 0) {
  259. /* This is an extended ACL */
  260. error = ext2_set_acl(inode,
  261. ACL_TYPE_ACCESS, clone);
  262. }
  263. }
  264. posix_acl_release(clone);
  265. }
  266. cleanup:
  267. posix_acl_release(acl);
  268. return error;
  269. }
  270. /*
  271. * Does chmod for an inode that may have an Access Control List. The
  272. * inode->i_mode field must be updated to the desired value by the caller
  273. * before calling this function.
  274. * Returns 0 on success, or a negative error number.
  275. *
  276. * We change the ACL rather than storing some ACL entries in the file
  277. * mode permission bits (which would be more efficient), because that
  278. * would break once additional permissions (like ACL_APPEND, ACL_DELETE
  279. * for directories) are added. There are no more bits available in the
  280. * file mode.
  281. *
  282. * inode->i_mutex: down
  283. */
  284. int
  285. ext2_acl_chmod(struct inode *inode)
  286. {
  287. struct posix_acl *acl, *clone;
  288. int error;
  289. if (!test_opt(inode->i_sb, POSIX_ACL))
  290. return 0;
  291. if (S_ISLNK(inode->i_mode))
  292. return -EOPNOTSUPP;
  293. acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  294. if (IS_ERR(acl) || !acl)
  295. return PTR_ERR(acl);
  296. clone = posix_acl_clone(acl, GFP_KERNEL);
  297. posix_acl_release(acl);
  298. if (!clone)
  299. return -ENOMEM;
  300. error = posix_acl_chmod_masq(clone, inode->i_mode);
  301. if (!error)
  302. error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
  303. posix_acl_release(clone);
  304. return error;
  305. }
  306. /*
  307. * Extended attribut handlers
  308. */
  309. static size_t
  310. ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
  311. const char *name, size_t name_len, int type)
  312. {
  313. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  314. if (!test_opt(dentry->d_sb, POSIX_ACL))
  315. return 0;
  316. if (list && size <= list_size)
  317. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  318. return size;
  319. }
  320. static size_t
  321. ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
  322. const char *name, size_t name_len, int type)
  323. {
  324. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  325. if (!test_opt(dentry->d_sb, POSIX_ACL))
  326. return 0;
  327. if (list && size <= list_size)
  328. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  329. return size;
  330. }
  331. static int
  332. ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
  333. size_t size, int type)
  334. {
  335. struct posix_acl *acl;
  336. int error;
  337. if (strcmp(name, "") != 0)
  338. return -EINVAL;
  339. if (!test_opt(dentry->d_sb, POSIX_ACL))
  340. return -EOPNOTSUPP;
  341. acl = ext2_get_acl(dentry->d_inode, type);
  342. if (IS_ERR(acl))
  343. return PTR_ERR(acl);
  344. if (acl == NULL)
  345. return -ENODATA;
  346. error = posix_acl_to_xattr(acl, buffer, size);
  347. posix_acl_release(acl);
  348. return error;
  349. }
  350. static int
  351. ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
  352. size_t size, int flags, int type)
  353. {
  354. struct posix_acl *acl;
  355. int error;
  356. if (strcmp(name, "") != 0)
  357. return -EINVAL;
  358. if (!test_opt(dentry->d_sb, POSIX_ACL))
  359. return -EOPNOTSUPP;
  360. if (!is_owner_or_cap(dentry->d_inode))
  361. return -EPERM;
  362. if (value) {
  363. acl = posix_acl_from_xattr(value, size);
  364. if (IS_ERR(acl))
  365. return PTR_ERR(acl);
  366. else if (acl) {
  367. error = posix_acl_valid(acl);
  368. if (error)
  369. goto release_and_out;
  370. }
  371. } else
  372. acl = NULL;
  373. error = ext2_set_acl(dentry->d_inode, type, acl);
  374. release_and_out:
  375. posix_acl_release(acl);
  376. return error;
  377. }
  378. const struct xattr_handler ext2_xattr_acl_access_handler = {
  379. .prefix = POSIX_ACL_XATTR_ACCESS,
  380. .flags = ACL_TYPE_ACCESS,
  381. .list = ext2_xattr_list_acl_access,
  382. .get = ext2_xattr_get_acl,
  383. .set = ext2_xattr_set_acl,
  384. };
  385. const struct xattr_handler ext2_xattr_acl_default_handler = {
  386. .prefix = POSIX_ACL_XATTR_DEFAULT,
  387. .flags = ACL_TYPE_DEFAULT,
  388. .list = ext2_xattr_list_acl_default,
  389. .get = ext2_xattr_get_acl,
  390. .set = ext2_xattr_set_acl,
  391. };