acl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  55. break;
  56. case ACL_USER:
  57. case ACL_GROUP:
  58. value = (char *)value + sizeof(ext4_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. ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
  80. {
  81. ext4_acl_header *ext_acl;
  82. char *e;
  83. size_t n;
  84. *size = ext4_acl_size(acl->a_count);
  85. ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
  86. sizeof(ext4_acl_entry), GFP_NOFS);
  87. if (!ext_acl)
  88. return ERR_PTR(-ENOMEM);
  89. ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
  90. e = (char *)ext_acl + sizeof(ext4_acl_header);
  91. for (n = 0; n < acl->a_count; n++) {
  92. ext4_acl_entry *entry = (ext4_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 = cpu_to_le32(acl->a_entries[n].e_id);
  99. e += sizeof(ext4_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(ext4_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 operation get_posix_acl().
  118. *
  119. * inode->i_mutex: don't care
  120. */
  121. static struct posix_acl *
  122. ext4_get_acl(struct inode *inode, int type)
  123. {
  124. int name_index;
  125. char *value = NULL;
  126. struct posix_acl *acl;
  127. int retval;
  128. if (!test_opt(inode->i_sb, POSIX_ACL))
  129. return NULL;
  130. acl = get_cached_acl(inode, type);
  131. if (acl != ACL_NOT_CACHED)
  132. return acl;
  133. switch (type) {
  134. case ACL_TYPE_ACCESS:
  135. name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  136. break;
  137. case ACL_TYPE_DEFAULT:
  138. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  139. break;
  140. default:
  141. BUG();
  142. }
  143. retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
  144. if (retval > 0) {
  145. value = kmalloc(retval, GFP_NOFS);
  146. if (!value)
  147. return ERR_PTR(-ENOMEM);
  148. retval = ext4_xattr_get(inode, name_index, "", value, retval);
  149. }
  150. if (retval > 0)
  151. acl = ext4_acl_from_disk(value, retval);
  152. else if (retval == -ENODATA || retval == -ENOSYS)
  153. acl = NULL;
  154. else
  155. acl = ERR_PTR(retval);
  156. kfree(value);
  157. if (!IS_ERR(acl))
  158. set_cached_acl(inode, type, acl);
  159. return acl;
  160. }
  161. /*
  162. * Set the access or default ACL of an inode.
  163. *
  164. * inode->i_mutex: down unless called from ext4_new_inode
  165. */
  166. static int
  167. ext4_set_acl(handle_t *handle, struct inode *inode, int type,
  168. struct posix_acl *acl)
  169. {
  170. int name_index;
  171. void *value = NULL;
  172. size_t size = 0;
  173. int error;
  174. if (S_ISLNK(inode->i_mode))
  175. return -EOPNOTSUPP;
  176. switch (type) {
  177. case ACL_TYPE_ACCESS:
  178. name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  179. if (acl) {
  180. mode_t mode = inode->i_mode;
  181. error = posix_acl_equiv_mode(acl, &mode);
  182. if (error < 0)
  183. return error;
  184. else {
  185. inode->i_mode = mode;
  186. ext4_mark_inode_dirty(handle, inode);
  187. if (error == 0)
  188. acl = NULL;
  189. }
  190. }
  191. break;
  192. case ACL_TYPE_DEFAULT:
  193. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  194. if (!S_ISDIR(inode->i_mode))
  195. return acl ? -EACCES : 0;
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. if (acl) {
  201. value = ext4_acl_to_disk(acl, &size);
  202. if (IS_ERR(value))
  203. return (int)PTR_ERR(value);
  204. }
  205. error = ext4_xattr_set_handle(handle, inode, name_index, "",
  206. value, size, 0);
  207. kfree(value);
  208. if (!error)
  209. set_cached_acl(inode, type, acl);
  210. return error;
  211. }
  212. int
  213. ext4_check_acl(struct inode *inode, int mask)
  214. {
  215. struct posix_acl *acl = ext4_get_acl(inode, ACL_TYPE_ACCESS);
  216. if (IS_ERR(acl))
  217. return PTR_ERR(acl);
  218. if (acl) {
  219. int error = posix_acl_permission(inode, acl, mask);
  220. posix_acl_release(acl);
  221. return error;
  222. }
  223. return -EAGAIN;
  224. }
  225. /*
  226. * Initialize the ACLs of a new inode. Called from ext4_new_inode.
  227. *
  228. * dir->i_mutex: down
  229. * inode->i_mutex: up (access to inode is still exclusive)
  230. */
  231. int
  232. ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  233. {
  234. struct posix_acl *acl = NULL;
  235. int error = 0;
  236. if (!S_ISLNK(inode->i_mode)) {
  237. if (test_opt(dir->i_sb, POSIX_ACL)) {
  238. acl = ext4_get_acl(dir, ACL_TYPE_DEFAULT);
  239. if (IS_ERR(acl))
  240. return PTR_ERR(acl);
  241. }
  242. if (!acl)
  243. inode->i_mode &= ~current_umask();
  244. }
  245. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  246. struct posix_acl *clone;
  247. mode_t mode;
  248. if (S_ISDIR(inode->i_mode)) {
  249. error = ext4_set_acl(handle, inode,
  250. ACL_TYPE_DEFAULT, acl);
  251. if (error)
  252. goto cleanup;
  253. }
  254. clone = posix_acl_clone(acl, GFP_NOFS);
  255. error = -ENOMEM;
  256. if (!clone)
  257. goto cleanup;
  258. mode = inode->i_mode;
  259. error = posix_acl_create_masq(clone, &mode);
  260. if (error >= 0) {
  261. inode->i_mode = mode;
  262. if (error > 0) {
  263. /* This is an extended ACL */
  264. error = ext4_set_acl(handle, inode,
  265. ACL_TYPE_ACCESS, clone);
  266. }
  267. }
  268. posix_acl_release(clone);
  269. }
  270. cleanup:
  271. posix_acl_release(acl);
  272. return error;
  273. }
  274. /*
  275. * Does chmod for an inode that may have an Access Control List. The
  276. * inode->i_mode field must be updated to the desired value by the caller
  277. * before calling this function.
  278. * Returns 0 on success, or a negative error number.
  279. *
  280. * We change the ACL rather than storing some ACL entries in the file
  281. * mode permission bits (which would be more efficient), because that
  282. * would break once additional permissions (like ACL_APPEND, ACL_DELETE
  283. * for directories) are added. There are no more bits available in the
  284. * file mode.
  285. *
  286. * inode->i_mutex: down
  287. */
  288. int
  289. ext4_acl_chmod(struct inode *inode)
  290. {
  291. struct posix_acl *acl, *clone;
  292. int error;
  293. if (S_ISLNK(inode->i_mode))
  294. return -EOPNOTSUPP;
  295. if (!test_opt(inode->i_sb, POSIX_ACL))
  296. return 0;
  297. acl = ext4_get_acl(inode, ACL_TYPE_ACCESS);
  298. if (IS_ERR(acl) || !acl)
  299. return PTR_ERR(acl);
  300. clone = posix_acl_clone(acl, GFP_KERNEL);
  301. posix_acl_release(acl);
  302. if (!clone)
  303. return -ENOMEM;
  304. error = posix_acl_chmod_masq(clone, inode->i_mode);
  305. if (!error) {
  306. handle_t *handle;
  307. int retries = 0;
  308. retry:
  309. handle = ext4_journal_start(inode,
  310. EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  311. if (IS_ERR(handle)) {
  312. error = PTR_ERR(handle);
  313. ext4_std_error(inode->i_sb, error);
  314. goto out;
  315. }
  316. error = ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, clone);
  317. ext4_journal_stop(handle);
  318. if (error == -ENOSPC &&
  319. ext4_should_retry_alloc(inode->i_sb, &retries))
  320. goto retry;
  321. }
  322. out:
  323. posix_acl_release(clone);
  324. return error;
  325. }
  326. /*
  327. * Extended attribute handlers
  328. */
  329. static size_t
  330. ext4_xattr_list_acl_access(struct inode *inode, char *list, size_t list_len,
  331. const char *name, size_t name_len)
  332. {
  333. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  334. if (!test_opt(inode->i_sb, POSIX_ACL))
  335. return 0;
  336. if (list && size <= list_len)
  337. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  338. return size;
  339. }
  340. static size_t
  341. ext4_xattr_list_acl_default(struct inode *inode, char *list, size_t list_len,
  342. const char *name, size_t name_len)
  343. {
  344. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  345. if (!test_opt(inode->i_sb, POSIX_ACL))
  346. return 0;
  347. if (list && size <= list_len)
  348. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  349. return size;
  350. }
  351. static int
  352. ext4_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
  353. {
  354. struct posix_acl *acl;
  355. int error;
  356. if (!test_opt(inode->i_sb, POSIX_ACL))
  357. return -EOPNOTSUPP;
  358. acl = ext4_get_acl(inode, type);
  359. if (IS_ERR(acl))
  360. return PTR_ERR(acl);
  361. if (acl == NULL)
  362. return -ENODATA;
  363. error = posix_acl_to_xattr(acl, buffer, size);
  364. posix_acl_release(acl);
  365. return error;
  366. }
  367. static int
  368. ext4_xattr_get_acl_access(struct inode *inode, const char *name,
  369. void *buffer, size_t size)
  370. {
  371. if (strcmp(name, "") != 0)
  372. return -EINVAL;
  373. return ext4_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  374. }
  375. static int
  376. ext4_xattr_get_acl_default(struct inode *inode, const char *name,
  377. void *buffer, size_t size)
  378. {
  379. if (strcmp(name, "") != 0)
  380. return -EINVAL;
  381. return ext4_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  382. }
  383. static int
  384. ext4_xattr_set_acl(struct inode *inode, int type, const void *value,
  385. size_t size)
  386. {
  387. handle_t *handle;
  388. struct posix_acl *acl;
  389. int error, retries = 0;
  390. if (!test_opt(inode->i_sb, POSIX_ACL))
  391. return -EOPNOTSUPP;
  392. if (!is_owner_or_cap(inode))
  393. return -EPERM;
  394. if (value) {
  395. acl = posix_acl_from_xattr(value, size);
  396. if (IS_ERR(acl))
  397. return PTR_ERR(acl);
  398. else if (acl) {
  399. error = posix_acl_valid(acl);
  400. if (error)
  401. goto release_and_out;
  402. }
  403. } else
  404. acl = NULL;
  405. retry:
  406. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  407. if (IS_ERR(handle))
  408. return PTR_ERR(handle);
  409. error = ext4_set_acl(handle, inode, type, acl);
  410. ext4_journal_stop(handle);
  411. if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  412. goto retry;
  413. release_and_out:
  414. posix_acl_release(acl);
  415. return error;
  416. }
  417. static int
  418. ext4_xattr_set_acl_access(struct inode *inode, const char *name,
  419. const void *value, size_t size, int flags)
  420. {
  421. if (strcmp(name, "") != 0)
  422. return -EINVAL;
  423. return ext4_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  424. }
  425. static int
  426. ext4_xattr_set_acl_default(struct inode *inode, const char *name,
  427. const void *value, size_t size, int flags)
  428. {
  429. if (strcmp(name, "") != 0)
  430. return -EINVAL;
  431. return ext4_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  432. }
  433. struct xattr_handler ext4_xattr_acl_access_handler = {
  434. .prefix = POSIX_ACL_XATTR_ACCESS,
  435. .list = ext4_xattr_list_acl_access,
  436. .get = ext4_xattr_get_acl_access,
  437. .set = ext4_xattr_set_acl_access,
  438. };
  439. struct xattr_handler ext4_xattr_acl_default_handler = {
  440. .prefix = POSIX_ACL_XATTR_DEFAULT,
  441. .list = ext4_xattr_list_acl_default,
  442. .get = ext4_xattr_get_acl_default,
  443. .set = ext4_xattr_set_acl_default,
  444. };