original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 lines
9.0 KiB

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