acl.c 10 KB

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