acl.c 9.8 KB

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