acl.c 9.0 KB

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