acl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_update_mode(inode, &inode->i_mode, &acl);
  176. if (error)
  177. return error;
  178. inode->i_ctime = CURRENT_TIME_SEC;
  179. ext3_mark_inode_dirty(handle, inode);
  180. }
  181. break;
  182. case ACL_TYPE_DEFAULT:
  183. name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
  184. if (!S_ISDIR(inode->i_mode))
  185. return acl ? -EACCES : 0;
  186. break;
  187. default:
  188. return -EINVAL;
  189. }
  190. if (acl) {
  191. value = ext3_acl_to_disk(acl, &size);
  192. if (IS_ERR(value))
  193. return (int)PTR_ERR(value);
  194. }
  195. error = ext3_xattr_set_handle(handle, inode, name_index, "",
  196. value, size, 0);
  197. kfree(value);
  198. if (!error)
  199. set_cached_acl(inode, type, acl);
  200. return error;
  201. }
  202. /*
  203. * Initialize the ACLs of a new inode. Called from ext3_new_inode.
  204. *
  205. * dir->i_mutex: down
  206. * inode->i_mutex: up (access to inode is still exclusive)
  207. */
  208. int
  209. ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  210. {
  211. struct posix_acl *acl = NULL;
  212. int error = 0;
  213. if (!S_ISLNK(inode->i_mode)) {
  214. if (test_opt(dir->i_sb, POSIX_ACL)) {
  215. acl = ext3_get_acl(dir, ACL_TYPE_DEFAULT);
  216. if (IS_ERR(acl))
  217. return PTR_ERR(acl);
  218. }
  219. if (!acl)
  220. inode->i_mode &= ~current_umask();
  221. }
  222. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  223. if (S_ISDIR(inode->i_mode)) {
  224. error = ext3_set_acl(handle, inode,
  225. ACL_TYPE_DEFAULT, acl);
  226. if (error)
  227. goto cleanup;
  228. }
  229. error = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
  230. if (error < 0)
  231. return error;
  232. if (error > 0) {
  233. /* This is an extended ACL */
  234. error = ext3_set_acl(handle, 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. ext3_acl_chmod(struct inode *inode)
  257. {
  258. struct posix_acl *acl;
  259. handle_t *handle;
  260. int retries = 0;
  261. int error;
  262. if (S_ISLNK(inode->i_mode))
  263. return -EOPNOTSUPP;
  264. if (!test_opt(inode->i_sb, POSIX_ACL))
  265. return 0;
  266. acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
  267. if (IS_ERR(acl) || !acl)
  268. return PTR_ERR(acl);
  269. error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  270. if (error)
  271. return error;
  272. retry:
  273. handle = ext3_journal_start(inode,
  274. EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  275. if (IS_ERR(handle)) {
  276. error = PTR_ERR(handle);
  277. ext3_std_error(inode->i_sb, error);
  278. goto out;
  279. }
  280. error = ext3_set_acl(handle, inode, ACL_TYPE_ACCESS, acl);
  281. ext3_journal_stop(handle);
  282. if (error == -ENOSPC &&
  283. ext3_should_retry_alloc(inode->i_sb, &retries))
  284. goto retry;
  285. out:
  286. posix_acl_release(acl);
  287. return error;
  288. }
  289. /*
  290. * Extended attribute handlers
  291. */
  292. static size_t
  293. ext3_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_len,
  294. const char *name, size_t name_len, int type)
  295. {
  296. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  297. if (!test_opt(dentry->d_sb, POSIX_ACL))
  298. return 0;
  299. if (list && size <= list_len)
  300. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  301. return size;
  302. }
  303. static size_t
  304. ext3_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_len,
  305. const char *name, size_t name_len, int type)
  306. {
  307. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  308. if (!test_opt(dentry->d_sb, POSIX_ACL))
  309. return 0;
  310. if (list && size <= list_len)
  311. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  312. return size;
  313. }
  314. static int
  315. ext3_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
  316. size_t size, int type)
  317. {
  318. struct posix_acl *acl;
  319. int error;
  320. if (strcmp(name, "") != 0)
  321. return -EINVAL;
  322. if (!test_opt(dentry->d_sb, POSIX_ACL))
  323. return -EOPNOTSUPP;
  324. acl = ext3_get_acl(dentry->d_inode, type);
  325. if (IS_ERR(acl))
  326. return PTR_ERR(acl);
  327. if (acl == NULL)
  328. return -ENODATA;
  329. error = posix_acl_to_xattr(acl, buffer, size);
  330. posix_acl_release(acl);
  331. return error;
  332. }
  333. static int
  334. ext3_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
  335. size_t size, int flags, int type)
  336. {
  337. struct inode *inode = dentry->d_inode;
  338. handle_t *handle;
  339. struct posix_acl *acl;
  340. int error, retries = 0;
  341. if (strcmp(name, "") != 0)
  342. return -EINVAL;
  343. if (!test_opt(inode->i_sb, POSIX_ACL))
  344. return -EOPNOTSUPP;
  345. if (!inode_owner_or_capable(inode))
  346. return -EPERM;
  347. if (value) {
  348. acl = posix_acl_from_xattr(value, size);
  349. if (IS_ERR(acl))
  350. return PTR_ERR(acl);
  351. else if (acl) {
  352. error = posix_acl_valid(acl);
  353. if (error)
  354. goto release_and_out;
  355. }
  356. } else
  357. acl = NULL;
  358. retry:
  359. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  360. if (IS_ERR(handle))
  361. return PTR_ERR(handle);
  362. error = ext3_set_acl(handle, inode, type, acl);
  363. ext3_journal_stop(handle);
  364. if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
  365. goto retry;
  366. release_and_out:
  367. posix_acl_release(acl);
  368. return error;
  369. }
  370. const struct xattr_handler ext3_xattr_acl_access_handler = {
  371. .prefix = POSIX_ACL_XATTR_ACCESS,
  372. .flags = ACL_TYPE_ACCESS,
  373. .list = ext3_xattr_list_acl_access,
  374. .get = ext3_xattr_get_acl,
  375. .set = ext3_xattr_set_acl,
  376. };
  377. const struct xattr_handler ext3_xattr_acl_default_handler = {
  378. .prefix = POSIX_ACL_XATTR_DEFAULT,
  379. .flags = ACL_TYPE_DEFAULT,
  380. .list = ext3_xattr_list_acl_default,
  381. .get = ext3_xattr_get_acl,
  382. .set = ext3_xattr_set_acl,
  383. };