acl.c 12 KB

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