acl.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * linux/fs/ext3/acl.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. */
  6. #include "ext3.h"
  7. #include "xattr.h"
  8. #include "acl.h"
  9. /*
  10. * Convert from filesystem to in-memory representation.
  11. */
  12. static struct posix_acl *
  13. ext3_acl_from_disk(const void *value, size_t size)
  14. {
  15. const char *end = (char *)value + size;
  16. int n, count;
  17. struct posix_acl *acl;
  18. if (!value)
  19. return NULL;
  20. if (size < sizeof(ext3_acl_header))
  21. return ERR_PTR(-EINVAL);
  22. if (((ext3_acl_header *)value)->a_version !=
  23. cpu_to_le32(EXT3_ACL_VERSION))
  24. return ERR_PTR(-EINVAL);
  25. value = (char *)value + sizeof(ext3_acl_header);
  26. count = ext3_acl_count(size);
  27. if (count < 0)
  28. return ERR_PTR(-EINVAL);
  29. if (count == 0)
  30. return NULL;
  31. acl = posix_acl_alloc(count, GFP_NOFS);
  32. if (!acl)
  33. return ERR_PTR(-ENOMEM);
  34. for (n=0; n < count; n++) {
  35. ext3_acl_entry *entry =
  36. (ext3_acl_entry *)value;
  37. if ((char *)value + sizeof(ext3_acl_entry_short) > end)
  38. goto fail;
  39. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  40. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  41. switch(acl->a_entries[n].e_tag) {
  42. case ACL_USER_OBJ:
  43. case ACL_GROUP_OBJ:
  44. case ACL_MASK:
  45. case ACL_OTHER:
  46. value = (char *)value +
  47. sizeof(ext3_acl_entry_short);
  48. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  49. break;
  50. case ACL_USER:
  51. case ACL_GROUP:
  52. value = (char *)value + sizeof(ext3_acl_entry);
  53. if ((char *)value > end)
  54. goto fail;
  55. acl->a_entries[n].e_id =
  56. le32_to_cpu(entry->e_id);
  57. break;
  58. default:
  59. goto fail;
  60. }
  61. }
  62. if (value != end)
  63. goto fail;
  64. return acl;
  65. fail:
  66. posix_acl_release(acl);
  67. return ERR_PTR(-EINVAL);
  68. }
  69. /*
  70. * Convert from in-memory to filesystem representation.
  71. */
  72. static void *
  73. ext3_acl_to_disk(const struct posix_acl *acl, size_t *size)
  74. {
  75. ext3_acl_header *ext_acl;
  76. char *e;
  77. size_t n;
  78. *size = ext3_acl_size(acl->a_count);
  79. ext_acl = kmalloc(sizeof(ext3_acl_header) + acl->a_count *
  80. sizeof(ext3_acl_entry), GFP_NOFS);
  81. if (!ext_acl)
  82. return ERR_PTR(-ENOMEM);
  83. ext_acl->a_version = cpu_to_le32(EXT3_ACL_VERSION);
  84. e = (char *)ext_acl + sizeof(ext3_acl_header);
  85. for (n=0; n < acl->a_count; n++) {
  86. ext3_acl_entry *entry = (ext3_acl_entry *)e;
  87. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  88. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  89. switch(acl->a_entries[n].e_tag) {
  90. case ACL_USER:
  91. case ACL_GROUP:
  92. entry->e_id =
  93. cpu_to_le32(acl->a_entries[n].e_id);
  94. e += sizeof(ext3_acl_entry);
  95. break;
  96. case ACL_USER_OBJ:
  97. case ACL_GROUP_OBJ:
  98. case ACL_MASK:
  99. case ACL_OTHER:
  100. e += sizeof(ext3_acl_entry_short);
  101. break;
  102. default:
  103. goto fail;
  104. }
  105. }
  106. return (char *)ext_acl;
  107. fail:
  108. kfree(ext_acl);
  109. return ERR_PTR(-EINVAL);
  110. }
  111. /*
  112. * Inode operation get_posix_acl().
  113. *
  114. * inode->i_mutex: don't care
  115. */
  116. struct posix_acl *
  117. ext3_get_acl(struct inode *inode, int type)
  118. {
  119. int name_index;
  120. char *value = NULL;
  121. struct posix_acl *acl;
  122. int retval;
  123. if (!test_opt(inode->i_sb, POSIX_ACL))
  124. return NULL;
  125. acl = get_cached_acl(inode, type);
  126. if (acl != ACL_NOT_CACHED)
  127. return acl;
  128. switch (type) {
  129. case ACL_TYPE_ACCESS:
  130. name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
  131. break;
  132. case ACL_TYPE_DEFAULT:
  133. name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
  134. break;
  135. default:
  136. BUG();
  137. }
  138. retval = ext3_xattr_get(inode, name_index, "", NULL, 0);
  139. if (retval > 0) {
  140. value = kmalloc(retval, GFP_NOFS);
  141. if (!value)
  142. return ERR_PTR(-ENOMEM);
  143. retval = ext3_xattr_get(inode, name_index, "", value, retval);
  144. }
  145. if (retval > 0)
  146. acl = ext3_acl_from_disk(value, retval);
  147. else if (retval == -ENODATA || retval == -ENOSYS)
  148. acl = NULL;
  149. else
  150. acl = ERR_PTR(retval);
  151. kfree(value);
  152. if (!IS_ERR(acl))
  153. set_cached_acl(inode, type, acl);
  154. return acl;
  155. }
  156. /*
  157. * Set the access or default ACL of an inode.
  158. *
  159. * inode->i_mutex: down unless called from ext3_new_inode
  160. */
  161. static int
  162. ext3_set_acl(handle_t *handle, struct inode *inode, int type,
  163. 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. switch(type) {
  172. case ACL_TYPE_ACCESS:
  173. name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
  174. if (acl) {
  175. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  176. if (error < 0)
  177. return error;
  178. else {
  179. inode->i_ctime = CURRENT_TIME_SEC;
  180. ext3_mark_inode_dirty(handle, inode);
  181. if (error == 0)
  182. acl = NULL;
  183. }
  184. }
  185. break;
  186. case ACL_TYPE_DEFAULT:
  187. name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
  188. if (!S_ISDIR(inode->i_mode))
  189. return acl ? -EACCES : 0;
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. if (acl) {
  195. value = ext3_acl_to_disk(acl, &size);
  196. if (IS_ERR(value))
  197. return (int)PTR_ERR(value);
  198. }
  199. error = ext3_xattr_set_handle(handle, inode, name_index, "",
  200. value, size, 0);
  201. kfree(value);
  202. if (!error)
  203. set_cached_acl(inode, type, acl);
  204. return error;
  205. }
  206. /*
  207. * Initialize the ACLs of a new inode. Called from ext3_new_inode.
  208. *
  209. * dir->i_mutex: down
  210. * inode->i_mutex: up (access to inode is still exclusive)
  211. */
  212. int
  213. ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  214. {
  215. struct posix_acl *acl = NULL;
  216. int error = 0;
  217. if (!S_ISLNK(inode->i_mode)) {
  218. if (test_opt(dir->i_sb, POSIX_ACL)) {
  219. acl = ext3_get_acl(dir, ACL_TYPE_DEFAULT);
  220. if (IS_ERR(acl))
  221. return PTR_ERR(acl);
  222. }
  223. if (!acl)
  224. inode->i_mode &= ~current_umask();
  225. }
  226. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  227. if (S_ISDIR(inode->i_mode)) {
  228. error = ext3_set_acl(handle, inode,
  229. ACL_TYPE_DEFAULT, acl);
  230. if (error)
  231. goto cleanup;
  232. }
  233. error = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
  234. if (error < 0)
  235. return error;
  236. if (error > 0) {
  237. /* This is an extended ACL */
  238. error = ext3_set_acl(handle, 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. ext3_acl_chmod(struct inode *inode)
  261. {
  262. struct posix_acl *acl;
  263. handle_t *handle;
  264. int retries = 0;
  265. int error;
  266. if (S_ISLNK(inode->i_mode))
  267. return -EOPNOTSUPP;
  268. if (!test_opt(inode->i_sb, POSIX_ACL))
  269. return 0;
  270. acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
  271. if (IS_ERR(acl) || !acl)
  272. return PTR_ERR(acl);
  273. error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  274. if (error)
  275. return error;
  276. retry:
  277. handle = ext3_journal_start(inode,
  278. EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  279. if (IS_ERR(handle)) {
  280. error = PTR_ERR(handle);
  281. ext3_std_error(inode->i_sb, error);
  282. goto out;
  283. }
  284. error = ext3_set_acl(handle, inode, ACL_TYPE_ACCESS, acl);
  285. ext3_journal_stop(handle);
  286. if (error == -ENOSPC &&
  287. ext3_should_retry_alloc(inode->i_sb, &retries))
  288. goto retry;
  289. out:
  290. posix_acl_release(acl);
  291. return error;
  292. }
  293. /*
  294. * Extended attribute handlers
  295. */
  296. static size_t
  297. ext3_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_len,
  298. const char *name, size_t name_len, int type)
  299. {
  300. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  301. if (!test_opt(dentry->d_sb, POSIX_ACL))
  302. return 0;
  303. if (list && size <= list_len)
  304. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  305. return size;
  306. }
  307. static size_t
  308. ext3_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_len,
  309. const char *name, size_t name_len, int type)
  310. {
  311. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  312. if (!test_opt(dentry->d_sb, POSIX_ACL))
  313. return 0;
  314. if (list && size <= list_len)
  315. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  316. return size;
  317. }
  318. static int
  319. ext3_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
  320. size_t size, 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. acl = ext3_get_acl(dentry->d_inode, type);
  329. if (IS_ERR(acl))
  330. return PTR_ERR(acl);
  331. if (acl == NULL)
  332. return -ENODATA;
  333. error = posix_acl_to_xattr(acl, buffer, size);
  334. posix_acl_release(acl);
  335. return error;
  336. }
  337. static int
  338. ext3_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
  339. size_t size, int flags, int type)
  340. {
  341. struct inode *inode = dentry->d_inode;
  342. handle_t *handle;
  343. struct posix_acl *acl;
  344. int error, retries = 0;
  345. if (strcmp(name, "") != 0)
  346. return -EINVAL;
  347. if (!test_opt(inode->i_sb, POSIX_ACL))
  348. return -EOPNOTSUPP;
  349. if (!inode_owner_or_capable(inode))
  350. return -EPERM;
  351. if (value) {
  352. acl = posix_acl_from_xattr(value, size);
  353. if (IS_ERR(acl))
  354. return PTR_ERR(acl);
  355. else if (acl) {
  356. error = posix_acl_valid(acl);
  357. if (error)
  358. goto release_and_out;
  359. }
  360. } else
  361. acl = NULL;
  362. retry:
  363. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  364. if (IS_ERR(handle))
  365. return PTR_ERR(handle);
  366. error = ext3_set_acl(handle, inode, type, acl);
  367. ext3_journal_stop(handle);
  368. if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
  369. goto retry;
  370. release_and_out:
  371. posix_acl_release(acl);
  372. return error;
  373. }
  374. const struct xattr_handler ext3_xattr_acl_access_handler = {
  375. .prefix = POSIX_ACL_XATTR_ACCESS,
  376. .flags = ACL_TYPE_ACCESS,
  377. .list = ext3_xattr_list_acl_access,
  378. .get = ext3_xattr_get_acl,
  379. .set = ext3_xattr_set_acl,
  380. };
  381. const struct xattr_handler ext3_xattr_acl_default_handler = {
  382. .prefix = POSIX_ACL_XATTR_DEFAULT,
  383. .flags = ACL_TYPE_DEFAULT,
  384. .list = ext3_xattr_list_acl_default,
  385. .get = ext3_xattr_get_acl,
  386. .set = ext3_xattr_set_acl,
  387. };