acl.c 12 KB

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