acl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. break;
  54. case ACL_USER:
  55. value = (char *)value + sizeof(ext2_acl_entry);
  56. if ((char *)value > end)
  57. goto fail;
  58. acl->a_entries[n].e_uid =
  59. make_kuid(&init_user_ns,
  60. le32_to_cpu(entry->e_id));
  61. break;
  62. case ACL_GROUP:
  63. value = (char *)value + sizeof(ext2_acl_entry);
  64. if ((char *)value > end)
  65. goto fail;
  66. acl->a_entries[n].e_gid =
  67. make_kgid(&init_user_ns,
  68. le32_to_cpu(entry->e_id));
  69. break;
  70. default:
  71. goto fail;
  72. }
  73. }
  74. if (value != end)
  75. goto fail;
  76. return acl;
  77. fail:
  78. posix_acl_release(acl);
  79. return ERR_PTR(-EINVAL);
  80. }
  81. /*
  82. * Convert from in-memory to filesystem representation.
  83. */
  84. static void *
  85. ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
  86. {
  87. ext2_acl_header *ext_acl;
  88. char *e;
  89. size_t n;
  90. *size = ext2_acl_size(acl->a_count);
  91. ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
  92. sizeof(ext2_acl_entry), GFP_KERNEL);
  93. if (!ext_acl)
  94. return ERR_PTR(-ENOMEM);
  95. ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
  96. e = (char *)ext_acl + sizeof(ext2_acl_header);
  97. for (n=0; n < acl->a_count; n++) {
  98. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  99. ext2_acl_entry *entry = (ext2_acl_entry *)e;
  100. entry->e_tag = cpu_to_le16(acl_e->e_tag);
  101. entry->e_perm = cpu_to_le16(acl_e->e_perm);
  102. switch(acl_e->e_tag) {
  103. case ACL_USER:
  104. entry->e_id = cpu_to_le32(
  105. from_kuid(&init_user_ns, acl_e->e_uid));
  106. e += sizeof(ext2_acl_entry);
  107. break;
  108. case ACL_GROUP:
  109. entry->e_id = cpu_to_le32(
  110. from_kgid(&init_user_ns, acl_e->e_gid));
  111. e += sizeof(ext2_acl_entry);
  112. break;
  113. case ACL_USER_OBJ:
  114. case ACL_GROUP_OBJ:
  115. case ACL_MASK:
  116. case ACL_OTHER:
  117. e += sizeof(ext2_acl_entry_short);
  118. break;
  119. default:
  120. goto fail;
  121. }
  122. }
  123. return (char *)ext_acl;
  124. fail:
  125. kfree(ext_acl);
  126. return ERR_PTR(-EINVAL);
  127. }
  128. /*
  129. * inode->i_mutex: don't care
  130. */
  131. struct posix_acl *
  132. ext2_get_acl(struct inode *inode, int type)
  133. {
  134. int name_index;
  135. char *value = NULL;
  136. struct posix_acl *acl;
  137. int retval;
  138. if (!test_opt(inode->i_sb, POSIX_ACL))
  139. return NULL;
  140. acl = get_cached_acl(inode, type);
  141. if (acl != ACL_NOT_CACHED)
  142. return acl;
  143. switch (type) {
  144. case ACL_TYPE_ACCESS:
  145. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  146. break;
  147. case ACL_TYPE_DEFAULT:
  148. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  149. break;
  150. default:
  151. BUG();
  152. }
  153. retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
  154. if (retval > 0) {
  155. value = kmalloc(retval, GFP_KERNEL);
  156. if (!value)
  157. return ERR_PTR(-ENOMEM);
  158. retval = ext2_xattr_get(inode, name_index, "", value, retval);
  159. }
  160. if (retval > 0)
  161. acl = ext2_acl_from_disk(value, retval);
  162. else if (retval == -ENODATA || retval == -ENOSYS)
  163. acl = NULL;
  164. else
  165. acl = ERR_PTR(retval);
  166. kfree(value);
  167. if (!IS_ERR(acl))
  168. set_cached_acl(inode, type, acl);
  169. return acl;
  170. }
  171. /*
  172. * inode->i_mutex: down
  173. */
  174. static int
  175. ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  176. {
  177. int name_index;
  178. void *value = NULL;
  179. size_t size = 0;
  180. int error;
  181. if (S_ISLNK(inode->i_mode))
  182. return -EOPNOTSUPP;
  183. if (!test_opt(inode->i_sb, POSIX_ACL))
  184. return 0;
  185. switch(type) {
  186. case ACL_TYPE_ACCESS:
  187. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  188. if (acl) {
  189. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  190. if (error < 0)
  191. return error;
  192. else {
  193. inode->i_ctime = CURRENT_TIME_SEC;
  194. mark_inode_dirty(inode);
  195. if (error == 0)
  196. acl = NULL;
  197. }
  198. }
  199. break;
  200. case ACL_TYPE_DEFAULT:
  201. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  202. if (!S_ISDIR(inode->i_mode))
  203. return acl ? -EACCES : 0;
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. if (acl) {
  209. value = ext2_acl_to_disk(acl, &size);
  210. if (IS_ERR(value))
  211. return (int)PTR_ERR(value);
  212. }
  213. error = ext2_xattr_set(inode, name_index, "", value, size, 0);
  214. kfree(value);
  215. if (!error)
  216. set_cached_acl(inode, type, acl);
  217. return error;
  218. }
  219. /*
  220. * Initialize the ACLs of a new inode. Called from ext2_new_inode.
  221. *
  222. * dir->i_mutex: down
  223. * inode->i_mutex: up (access to inode is still exclusive)
  224. */
  225. int
  226. ext2_init_acl(struct inode *inode, struct inode *dir)
  227. {
  228. struct posix_acl *acl = NULL;
  229. int error = 0;
  230. if (!S_ISLNK(inode->i_mode)) {
  231. if (test_opt(dir->i_sb, POSIX_ACL)) {
  232. acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
  233. if (IS_ERR(acl))
  234. return PTR_ERR(acl);
  235. }
  236. if (!acl)
  237. inode->i_mode &= ~current_umask();
  238. }
  239. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  240. if (S_ISDIR(inode->i_mode)) {
  241. error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  242. if (error)
  243. goto cleanup;
  244. }
  245. error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
  246. if (error < 0)
  247. return error;
  248. if (error > 0) {
  249. /* This is an extended ACL */
  250. error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
  251. }
  252. }
  253. cleanup:
  254. posix_acl_release(acl);
  255. return error;
  256. }
  257. /*
  258. * Does chmod for an inode that may have an Access Control List. The
  259. * inode->i_mode field must be updated to the desired value by the caller
  260. * before calling this function.
  261. * Returns 0 on success, or a negative error number.
  262. *
  263. * We change the ACL rather than storing some ACL entries in the file
  264. * mode permission bits (which would be more efficient), because that
  265. * would break once additional permissions (like ACL_APPEND, ACL_DELETE
  266. * for directories) are added. There are no more bits available in the
  267. * file mode.
  268. *
  269. * inode->i_mutex: down
  270. */
  271. int
  272. ext2_acl_chmod(struct inode *inode)
  273. {
  274. struct posix_acl *acl;
  275. int error;
  276. if (!test_opt(inode->i_sb, POSIX_ACL))
  277. return 0;
  278. if (S_ISLNK(inode->i_mode))
  279. return -EOPNOTSUPP;
  280. acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  281. if (IS_ERR(acl) || !acl)
  282. return PTR_ERR(acl);
  283. error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  284. if (error)
  285. return error;
  286. error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
  287. posix_acl_release(acl);
  288. return error;
  289. }
  290. /*
  291. * Extended attribut handlers
  292. */
  293. static size_t
  294. ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
  295. const char *name, size_t name_len, int type)
  296. {
  297. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  298. if (!test_opt(dentry->d_sb, POSIX_ACL))
  299. return 0;
  300. if (list && size <= list_size)
  301. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  302. return size;
  303. }
  304. static size_t
  305. ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
  306. const char *name, size_t name_len, int type)
  307. {
  308. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  309. if (!test_opt(dentry->d_sb, POSIX_ACL))
  310. return 0;
  311. if (list && size <= list_size)
  312. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  313. return size;
  314. }
  315. static int
  316. ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
  317. size_t size, int type)
  318. {
  319. struct posix_acl *acl;
  320. int error;
  321. if (strcmp(name, "") != 0)
  322. return -EINVAL;
  323. if (!test_opt(dentry->d_sb, POSIX_ACL))
  324. return -EOPNOTSUPP;
  325. acl = ext2_get_acl(dentry->d_inode, type);
  326. if (IS_ERR(acl))
  327. return PTR_ERR(acl);
  328. if (acl == NULL)
  329. return -ENODATA;
  330. error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  331. posix_acl_release(acl);
  332. return error;
  333. }
  334. static int
  335. ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
  336. size_t size, int flags, int type)
  337. {
  338. struct posix_acl *acl;
  339. int error;
  340. if (strcmp(name, "") != 0)
  341. return -EINVAL;
  342. if (!test_opt(dentry->d_sb, POSIX_ACL))
  343. return -EOPNOTSUPP;
  344. if (!inode_owner_or_capable(dentry->d_inode))
  345. return -EPERM;
  346. if (value) {
  347. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  348. if (IS_ERR(acl))
  349. return PTR_ERR(acl);
  350. else if (acl) {
  351. error = posix_acl_valid(acl);
  352. if (error)
  353. goto release_and_out;
  354. }
  355. } else
  356. acl = NULL;
  357. error = ext2_set_acl(dentry->d_inode, type, acl);
  358. release_and_out:
  359. posix_acl_release(acl);
  360. return error;
  361. }
  362. const struct xattr_handler ext2_xattr_acl_access_handler = {
  363. .prefix = POSIX_ACL_XATTR_ACCESS,
  364. .flags = ACL_TYPE_ACCESS,
  365. .list = ext2_xattr_list_acl_access,
  366. .get = ext2_xattr_get_acl,
  367. .set = ext2_xattr_set_acl,
  368. };
  369. const struct xattr_handler ext2_xattr_acl_default_handler = {
  370. .prefix = POSIX_ACL_XATTR_DEFAULT,
  371. .flags = ACL_TYPE_DEFAULT,
  372. .list = ext2_xattr_list_acl_default,
  373. .get = ext2_xattr_get_acl,
  374. .set = ext2_xattr_set_acl,
  375. };