acl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * linux/fs/ext3/acl.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include <linux/capability.h>
  10. #include <linux/fs.h>
  11. #include <linux/ext3_jbd.h>
  12. #include <linux/ext3_fs.h>
  13. #include "xattr.h"
  14. #include "acl.h"
  15. /*
  16. * Convert from filesystem to in-memory representation.
  17. */
  18. static struct posix_acl *
  19. ext3_acl_from_disk(const void *value, size_t size)
  20. {
  21. const char *end = (char *)value + size;
  22. int n, count;
  23. struct posix_acl *acl;
  24. if (!value)
  25. return NULL;
  26. if (size < sizeof(ext3_acl_header))
  27. return ERR_PTR(-EINVAL);
  28. if (((ext3_acl_header *)value)->a_version !=
  29. cpu_to_le32(EXT3_ACL_VERSION))
  30. return ERR_PTR(-EINVAL);
  31. value = (char *)value + sizeof(ext3_acl_header);
  32. count = ext3_acl_count(size);
  33. if (count < 0)
  34. return ERR_PTR(-EINVAL);
  35. if (count == 0)
  36. return NULL;
  37. acl = posix_acl_alloc(count, GFP_NOFS);
  38. if (!acl)
  39. return ERR_PTR(-ENOMEM);
  40. for (n=0; n < count; n++) {
  41. ext3_acl_entry *entry =
  42. (ext3_acl_entry *)value;
  43. if ((char *)value + sizeof(ext3_acl_entry_short) > end)
  44. goto fail;
  45. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  46. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  47. switch(acl->a_entries[n].e_tag) {
  48. case ACL_USER_OBJ:
  49. case ACL_GROUP_OBJ:
  50. case ACL_MASK:
  51. case ACL_OTHER:
  52. value = (char *)value +
  53. sizeof(ext3_acl_entry_short);
  54. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  55. break;
  56. case ACL_USER:
  57. case ACL_GROUP:
  58. value = (char *)value + sizeof(ext3_acl_entry);
  59. if ((char *)value > end)
  60. goto fail;
  61. acl->a_entries[n].e_id =
  62. le32_to_cpu(entry->e_id);
  63. break;
  64. default:
  65. goto fail;
  66. }
  67. }
  68. if (value != end)
  69. goto fail;
  70. return acl;
  71. fail:
  72. posix_acl_release(acl);
  73. return ERR_PTR(-EINVAL);
  74. }
  75. /*
  76. * Convert from in-memory to filesystem representation.
  77. */
  78. static void *
  79. ext3_acl_to_disk(const struct posix_acl *acl, size_t *size)
  80. {
  81. ext3_acl_header *ext_acl;
  82. char *e;
  83. size_t n;
  84. *size = ext3_acl_size(acl->a_count);
  85. ext_acl = kmalloc(sizeof(ext3_acl_header) + acl->a_count *
  86. sizeof(ext3_acl_entry), GFP_NOFS);
  87. if (!ext_acl)
  88. return ERR_PTR(-ENOMEM);
  89. ext_acl->a_version = cpu_to_le32(EXT3_ACL_VERSION);
  90. e = (char *)ext_acl + sizeof(ext3_acl_header);
  91. for (n=0; n < acl->a_count; n++) {
  92. ext3_acl_entry *entry = (ext3_acl_entry *)e;
  93. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  94. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  95. switch(acl->a_entries[n].e_tag) {
  96. case ACL_USER:
  97. case ACL_GROUP:
  98. entry->e_id =
  99. cpu_to_le32(acl->a_entries[n].e_id);
  100. e += sizeof(ext3_acl_entry);
  101. break;
  102. case ACL_USER_OBJ:
  103. case ACL_GROUP_OBJ:
  104. case ACL_MASK:
  105. case ACL_OTHER:
  106. e += sizeof(ext3_acl_entry_short);
  107. break;
  108. default:
  109. goto fail;
  110. }
  111. }
  112. return (char *)ext_acl;
  113. fail:
  114. kfree(ext_acl);
  115. return ERR_PTR(-EINVAL);
  116. }
  117. /*
  118. * Inode operation get_posix_acl().
  119. *
  120. * inode->i_mutex: don't care
  121. */
  122. static struct posix_acl *
  123. ext3_get_acl(struct inode *inode, int type)
  124. {
  125. int name_index;
  126. char *value = NULL;
  127. struct posix_acl *acl;
  128. int retval;
  129. if (!test_opt(inode->i_sb, POSIX_ACL))
  130. return NULL;
  131. acl = get_cached_acl(inode, type);
  132. if (acl != ACL_NOT_CACHED)
  133. return acl;
  134. switch (type) {
  135. case ACL_TYPE_ACCESS:
  136. name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
  137. break;
  138. case ACL_TYPE_DEFAULT:
  139. name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
  140. break;
  141. default:
  142. BUG();
  143. }
  144. retval = ext3_xattr_get(inode, name_index, "", NULL, 0);
  145. if (retval > 0) {
  146. value = kmalloc(retval, GFP_NOFS);
  147. if (!value)
  148. return ERR_PTR(-ENOMEM);
  149. retval = ext3_xattr_get(inode, name_index, "", value, retval);
  150. }
  151. if (retval > 0)
  152. acl = ext3_acl_from_disk(value, retval);
  153. else if (retval == -ENODATA || retval == -ENOSYS)
  154. acl = NULL;
  155. else
  156. acl = ERR_PTR(retval);
  157. kfree(value);
  158. if (!IS_ERR(acl))
  159. set_cached_acl(inode, type, acl);
  160. return acl;
  161. }
  162. /*
  163. * Set the access or default ACL of an inode.
  164. *
  165. * inode->i_mutex: down unless called from ext3_new_inode
  166. */
  167. static int
  168. ext3_set_acl(handle_t *handle, struct inode *inode, int type,
  169. struct posix_acl *acl)
  170. {
  171. int name_index;
  172. void *value = NULL;
  173. size_t size = 0;
  174. int error;
  175. if (S_ISLNK(inode->i_mode))
  176. return -EOPNOTSUPP;
  177. switch(type) {
  178. case ACL_TYPE_ACCESS:
  179. name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
  180. if (acl) {
  181. mode_t mode = inode->i_mode;
  182. error = posix_acl_equiv_mode(acl, &mode);
  183. if (error < 0)
  184. return error;
  185. else {
  186. inode->i_mode = mode;
  187. inode->i_ctime = CURRENT_TIME_SEC;
  188. ext3_mark_inode_dirty(handle, inode);
  189. if (error == 0)
  190. acl = NULL;
  191. }
  192. }
  193. break;
  194. case ACL_TYPE_DEFAULT:
  195. name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
  196. if (!S_ISDIR(inode->i_mode))
  197. return acl ? -EACCES : 0;
  198. break;
  199. default:
  200. return -EINVAL;
  201. }
  202. if (acl) {
  203. value = ext3_acl_to_disk(acl, &size);
  204. if (IS_ERR(value))
  205. return (int)PTR_ERR(value);
  206. }
  207. error = ext3_xattr_set_handle(handle, inode, name_index, "",
  208. value, size, 0);
  209. kfree(value);
  210. if (!error)
  211. set_cached_acl(inode, type, acl);
  212. return error;
  213. }
  214. int
  215. ext3_check_acl(struct inode *inode, int mask, unsigned int flags)
  216. {
  217. struct posix_acl *acl;
  218. if (flags & IPERM_FLAG_RCU) {
  219. if (!negative_cached_acl(inode, ACL_TYPE_ACCESS))
  220. return -ECHILD;
  221. return -EAGAIN;
  222. }
  223. acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
  224. if (IS_ERR(acl))
  225. return PTR_ERR(acl);
  226. if (acl) {
  227. int error = posix_acl_permission(inode, acl, mask);
  228. posix_acl_release(acl);
  229. return error;
  230. }
  231. return -EAGAIN;
  232. }
  233. /*
  234. * Initialize the ACLs of a new inode. Called from ext3_new_inode.
  235. *
  236. * dir->i_mutex: down
  237. * inode->i_mutex: up (access to inode is still exclusive)
  238. */
  239. int
  240. ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  241. {
  242. struct posix_acl *acl = NULL;
  243. int error = 0;
  244. if (!S_ISLNK(inode->i_mode)) {
  245. if (test_opt(dir->i_sb, POSIX_ACL)) {
  246. acl = ext3_get_acl(dir, ACL_TYPE_DEFAULT);
  247. if (IS_ERR(acl))
  248. return PTR_ERR(acl);
  249. }
  250. if (!acl)
  251. inode->i_mode &= ~current_umask();
  252. }
  253. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  254. struct posix_acl *clone;
  255. mode_t mode;
  256. if (S_ISDIR(inode->i_mode)) {
  257. error = ext3_set_acl(handle, inode,
  258. ACL_TYPE_DEFAULT, acl);
  259. if (error)
  260. goto cleanup;
  261. }
  262. clone = posix_acl_clone(acl, GFP_NOFS);
  263. error = -ENOMEM;
  264. if (!clone)
  265. goto cleanup;
  266. mode = inode->i_mode;
  267. error = posix_acl_create_masq(clone, &mode);
  268. if (error >= 0) {
  269. inode->i_mode = mode;
  270. if (error > 0) {
  271. /* This is an extended ACL */
  272. error = ext3_set_acl(handle, inode,
  273. ACL_TYPE_ACCESS, clone);
  274. }
  275. }
  276. posix_acl_release(clone);
  277. }
  278. cleanup:
  279. posix_acl_release(acl);
  280. return error;
  281. }
  282. /*
  283. * Does chmod for an inode that may have an Access Control List. The
  284. * inode->i_mode field must be updated to the desired value by the caller
  285. * before calling this function.
  286. * Returns 0 on success, or a negative error number.
  287. *
  288. * We change the ACL rather than storing some ACL entries in the file
  289. * mode permission bits (which would be more efficient), because that
  290. * would break once additional permissions (like ACL_APPEND, ACL_DELETE
  291. * for directories) are added. There are no more bits available in the
  292. * file mode.
  293. *
  294. * inode->i_mutex: down
  295. */
  296. int
  297. ext3_acl_chmod(struct inode *inode)
  298. {
  299. struct posix_acl *acl, *clone;
  300. int error;
  301. if (S_ISLNK(inode->i_mode))
  302. return -EOPNOTSUPP;
  303. if (!test_opt(inode->i_sb, POSIX_ACL))
  304. return 0;
  305. acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
  306. if (IS_ERR(acl) || !acl)
  307. return PTR_ERR(acl);
  308. clone = posix_acl_clone(acl, GFP_KERNEL);
  309. posix_acl_release(acl);
  310. if (!clone)
  311. return -ENOMEM;
  312. error = posix_acl_chmod_masq(clone, inode->i_mode);
  313. if (!error) {
  314. handle_t *handle;
  315. int retries = 0;
  316. retry:
  317. handle = ext3_journal_start(inode,
  318. EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  319. if (IS_ERR(handle)) {
  320. error = PTR_ERR(handle);
  321. ext3_std_error(inode->i_sb, error);
  322. goto out;
  323. }
  324. error = ext3_set_acl(handle, inode, ACL_TYPE_ACCESS, clone);
  325. ext3_journal_stop(handle);
  326. if (error == -ENOSPC &&
  327. ext3_should_retry_alloc(inode->i_sb, &retries))
  328. goto retry;
  329. }
  330. out:
  331. posix_acl_release(clone);
  332. return error;
  333. }
  334. /*
  335. * Extended attribute handlers
  336. */
  337. static size_t
  338. ext3_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_len,
  339. const char *name, size_t name_len, int type)
  340. {
  341. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  342. if (!test_opt(dentry->d_sb, POSIX_ACL))
  343. return 0;
  344. if (list && size <= list_len)
  345. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  346. return size;
  347. }
  348. static size_t
  349. ext3_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_len,
  350. const char *name, size_t name_len, int type)
  351. {
  352. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  353. if (!test_opt(dentry->d_sb, POSIX_ACL))
  354. return 0;
  355. if (list && size <= list_len)
  356. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  357. return size;
  358. }
  359. static int
  360. ext3_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
  361. size_t size, int type)
  362. {
  363. struct posix_acl *acl;
  364. int error;
  365. if (strcmp(name, "") != 0)
  366. return -EINVAL;
  367. if (!test_opt(dentry->d_sb, POSIX_ACL))
  368. return -EOPNOTSUPP;
  369. acl = ext3_get_acl(dentry->d_inode, type);
  370. if (IS_ERR(acl))
  371. return PTR_ERR(acl);
  372. if (acl == NULL)
  373. return -ENODATA;
  374. error = posix_acl_to_xattr(acl, buffer, size);
  375. posix_acl_release(acl);
  376. return error;
  377. }
  378. static int
  379. ext3_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
  380. size_t size, int flags, int type)
  381. {
  382. struct inode *inode = dentry->d_inode;
  383. handle_t *handle;
  384. struct posix_acl *acl;
  385. int error, retries = 0;
  386. if (strcmp(name, "") != 0)
  387. return -EINVAL;
  388. if (!test_opt(inode->i_sb, POSIX_ACL))
  389. return -EOPNOTSUPP;
  390. if (!inode_owner_or_capable(inode))
  391. return -EPERM;
  392. if (value) {
  393. acl = posix_acl_from_xattr(value, size);
  394. if (IS_ERR(acl))
  395. return PTR_ERR(acl);
  396. else if (acl) {
  397. error = posix_acl_valid(acl);
  398. if (error)
  399. goto release_and_out;
  400. }
  401. } else
  402. acl = NULL;
  403. retry:
  404. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  405. if (IS_ERR(handle))
  406. return PTR_ERR(handle);
  407. error = ext3_set_acl(handle, inode, type, acl);
  408. ext3_journal_stop(handle);
  409. if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
  410. goto retry;
  411. release_and_out:
  412. posix_acl_release(acl);
  413. return error;
  414. }
  415. const struct xattr_handler ext3_xattr_acl_access_handler = {
  416. .prefix = POSIX_ACL_XATTR_ACCESS,
  417. .flags = ACL_TYPE_ACCESS,
  418. .list = ext3_xattr_list_acl_access,
  419. .get = ext3_xattr_get_acl,
  420. .set = ext3_xattr_set_acl,
  421. };
  422. const struct xattr_handler ext3_xattr_acl_default_handler = {
  423. .prefix = POSIX_ACL_XATTR_DEFAULT,
  424. .flags = ACL_TYPE_DEFAULT,
  425. .list = ext3_xattr_list_acl_default,
  426. .get = ext3_xattr_get_acl,
  427. .set = ext3_xattr_set_acl,
  428. };