acl.c 12 KB

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