acl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * linux/fs/ext2/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/fs.h>
  10. #include "ext2.h"
  11. #include "xattr.h"
  12. #include "acl.h"
  13. /*
  14. * Convert from filesystem to in-memory representation.
  15. */
  16. static struct posix_acl *
  17. ext2_acl_from_disk(const void *value, size_t size)
  18. {
  19. const char *end = (char *)value + size;
  20. int n, count;
  21. struct posix_acl *acl;
  22. if (!value)
  23. return NULL;
  24. if (size < sizeof(ext2_acl_header))
  25. return ERR_PTR(-EINVAL);
  26. if (((ext2_acl_header *)value)->a_version !=
  27. cpu_to_le32(EXT2_ACL_VERSION))
  28. return ERR_PTR(-EINVAL);
  29. value = (char *)value + sizeof(ext2_acl_header);
  30. count = ext2_acl_count(size);
  31. if (count < 0)
  32. return ERR_PTR(-EINVAL);
  33. if (count == 0)
  34. return NULL;
  35. acl = posix_acl_alloc(count, GFP_KERNEL);
  36. if (!acl)
  37. return ERR_PTR(-ENOMEM);
  38. for (n=0; n < count; n++) {
  39. ext2_acl_entry *entry =
  40. (ext2_acl_entry *)value;
  41. if ((char *)value + sizeof(ext2_acl_entry_short) > end)
  42. goto fail;
  43. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  44. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  45. switch(acl->a_entries[n].e_tag) {
  46. case ACL_USER_OBJ:
  47. case ACL_GROUP_OBJ:
  48. case ACL_MASK:
  49. case ACL_OTHER:
  50. value = (char *)value +
  51. sizeof(ext2_acl_entry_short);
  52. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  53. break;
  54. case ACL_USER:
  55. case ACL_GROUP:
  56. value = (char *)value + sizeof(ext2_acl_entry);
  57. if ((char *)value > end)
  58. goto fail;
  59. acl->a_entries[n].e_id =
  60. le32_to_cpu(entry->e_id);
  61. break;
  62. default:
  63. goto fail;
  64. }
  65. }
  66. if (value != end)
  67. goto fail;
  68. return acl;
  69. fail:
  70. posix_acl_release(acl);
  71. return ERR_PTR(-EINVAL);
  72. }
  73. /*
  74. * Convert from in-memory to filesystem representation.
  75. */
  76. static void *
  77. ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
  78. {
  79. ext2_acl_header *ext_acl;
  80. char *e;
  81. size_t n;
  82. *size = ext2_acl_size(acl->a_count);
  83. ext_acl = (ext2_acl_header *)kmalloc(sizeof(ext2_acl_header) +
  84. acl->a_count * sizeof(ext2_acl_entry), GFP_KERNEL);
  85. if (!ext_acl)
  86. return ERR_PTR(-ENOMEM);
  87. ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
  88. e = (char *)ext_acl + sizeof(ext2_acl_header);
  89. for (n=0; n < acl->a_count; n++) {
  90. ext2_acl_entry *entry = (ext2_acl_entry *)e;
  91. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  92. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  93. switch(acl->a_entries[n].e_tag) {
  94. case ACL_USER:
  95. case ACL_GROUP:
  96. entry->e_id =
  97. cpu_to_le32(acl->a_entries[n].e_id);
  98. e += sizeof(ext2_acl_entry);
  99. break;
  100. case ACL_USER_OBJ:
  101. case ACL_GROUP_OBJ:
  102. case ACL_MASK:
  103. case ACL_OTHER:
  104. e += sizeof(ext2_acl_entry_short);
  105. break;
  106. default:
  107. goto fail;
  108. }
  109. }
  110. return (char *)ext_acl;
  111. fail:
  112. kfree(ext_acl);
  113. return ERR_PTR(-EINVAL);
  114. }
  115. static inline struct posix_acl *
  116. ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
  117. {
  118. struct posix_acl *acl = EXT2_ACL_NOT_CACHED;
  119. spin_lock(&inode->i_lock);
  120. if (*i_acl != EXT2_ACL_NOT_CACHED)
  121. acl = posix_acl_dup(*i_acl);
  122. spin_unlock(&inode->i_lock);
  123. return acl;
  124. }
  125. static inline void
  126. ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
  127. struct posix_acl *acl)
  128. {
  129. spin_lock(&inode->i_lock);
  130. if (*i_acl != EXT2_ACL_NOT_CACHED)
  131. posix_acl_release(*i_acl);
  132. *i_acl = posix_acl_dup(acl);
  133. spin_unlock(&inode->i_lock);
  134. }
  135. /*
  136. * inode->i_sem: don't care
  137. */
  138. static struct posix_acl *
  139. ext2_get_acl(struct inode *inode, int type)
  140. {
  141. struct ext2_inode_info *ei = EXT2_I(inode);
  142. int name_index;
  143. char *value = NULL;
  144. struct posix_acl *acl;
  145. int retval;
  146. if (!test_opt(inode->i_sb, POSIX_ACL))
  147. return NULL;
  148. switch(type) {
  149. case ACL_TYPE_ACCESS:
  150. acl = ext2_iget_acl(inode, &ei->i_acl);
  151. if (acl != EXT2_ACL_NOT_CACHED)
  152. return acl;
  153. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  154. break;
  155. case ACL_TYPE_DEFAULT:
  156. acl = ext2_iget_acl(inode, &ei->i_default_acl);
  157. if (acl != EXT2_ACL_NOT_CACHED)
  158. return acl;
  159. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  160. break;
  161. default:
  162. return ERR_PTR(-EINVAL);
  163. }
  164. retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
  165. if (retval > 0) {
  166. value = kmalloc(retval, GFP_KERNEL);
  167. if (!value)
  168. return ERR_PTR(-ENOMEM);
  169. retval = ext2_xattr_get(inode, name_index, "", value, retval);
  170. }
  171. if (retval > 0)
  172. acl = ext2_acl_from_disk(value, retval);
  173. else if (retval == -ENODATA || retval == -ENOSYS)
  174. acl = NULL;
  175. else
  176. acl = ERR_PTR(retval);
  177. if (value)
  178. kfree(value);
  179. if (!IS_ERR(acl)) {
  180. switch(type) {
  181. case ACL_TYPE_ACCESS:
  182. ext2_iset_acl(inode, &ei->i_acl, acl);
  183. break;
  184. case ACL_TYPE_DEFAULT:
  185. ext2_iset_acl(inode, &ei->i_default_acl, acl);
  186. break;
  187. }
  188. }
  189. return acl;
  190. }
  191. /*
  192. * inode->i_sem: down
  193. */
  194. static int
  195. ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  196. {
  197. struct ext2_inode_info *ei = EXT2_I(inode);
  198. int name_index;
  199. void *value = NULL;
  200. size_t size;
  201. int error;
  202. if (S_ISLNK(inode->i_mode))
  203. return -EOPNOTSUPP;
  204. if (!test_opt(inode->i_sb, POSIX_ACL))
  205. return 0;
  206. switch(type) {
  207. case ACL_TYPE_ACCESS:
  208. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  209. if (acl) {
  210. mode_t mode = inode->i_mode;
  211. error = posix_acl_equiv_mode(acl, &mode);
  212. if (error < 0)
  213. return error;
  214. else {
  215. inode->i_mode = mode;
  216. mark_inode_dirty(inode);
  217. if (error == 0)
  218. acl = NULL;
  219. }
  220. }
  221. break;
  222. case ACL_TYPE_DEFAULT:
  223. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  224. if (!S_ISDIR(inode->i_mode))
  225. return acl ? -EACCES : 0;
  226. break;
  227. default:
  228. return -EINVAL;
  229. }
  230. if (acl) {
  231. value = ext2_acl_to_disk(acl, &size);
  232. if (IS_ERR(value))
  233. return (int)PTR_ERR(value);
  234. }
  235. error = ext2_xattr_set(inode, name_index, "", value, size, 0);
  236. if (value)
  237. kfree(value);
  238. if (!error) {
  239. switch(type) {
  240. case ACL_TYPE_ACCESS:
  241. ext2_iset_acl(inode, &ei->i_acl, acl);
  242. break;
  243. case ACL_TYPE_DEFAULT:
  244. ext2_iset_acl(inode, &ei->i_default_acl, acl);
  245. break;
  246. }
  247. }
  248. return error;
  249. }
  250. static int
  251. ext2_check_acl(struct inode *inode, int mask)
  252. {
  253. struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  254. if (IS_ERR(acl))
  255. return PTR_ERR(acl);
  256. if (acl) {
  257. int error = posix_acl_permission(inode, acl, mask);
  258. posix_acl_release(acl);
  259. return error;
  260. }
  261. return -EAGAIN;
  262. }
  263. int
  264. ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
  265. {
  266. return generic_permission(inode, mask, ext2_check_acl);
  267. }
  268. /*
  269. * Initialize the ACLs of a new inode. Called from ext2_new_inode.
  270. *
  271. * dir->i_sem: down
  272. * inode->i_sem: up (access to inode is still exclusive)
  273. */
  274. int
  275. ext2_init_acl(struct inode *inode, struct inode *dir)
  276. {
  277. struct posix_acl *acl = NULL;
  278. int error = 0;
  279. if (!S_ISLNK(inode->i_mode)) {
  280. if (test_opt(dir->i_sb, POSIX_ACL)) {
  281. acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
  282. if (IS_ERR(acl))
  283. return PTR_ERR(acl);
  284. }
  285. if (!acl)
  286. inode->i_mode &= ~current->fs->umask;
  287. }
  288. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  289. struct posix_acl *clone;
  290. mode_t mode;
  291. if (S_ISDIR(inode->i_mode)) {
  292. error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  293. if (error)
  294. goto cleanup;
  295. }
  296. clone = posix_acl_clone(acl, GFP_KERNEL);
  297. error = -ENOMEM;
  298. if (!clone)
  299. goto cleanup;
  300. mode = inode->i_mode;
  301. error = posix_acl_create_masq(clone, &mode);
  302. if (error >= 0) {
  303. inode->i_mode = mode;
  304. if (error > 0) {
  305. /* This is an extended ACL */
  306. error = ext2_set_acl(inode,
  307. ACL_TYPE_ACCESS, clone);
  308. }
  309. }
  310. posix_acl_release(clone);
  311. }
  312. cleanup:
  313. posix_acl_release(acl);
  314. return error;
  315. }
  316. /*
  317. * Does chmod for an inode that may have an Access Control List. The
  318. * inode->i_mode field must be updated to the desired value by the caller
  319. * before calling this function.
  320. * Returns 0 on success, or a negative error number.
  321. *
  322. * We change the ACL rather than storing some ACL entries in the file
  323. * mode permission bits (which would be more efficient), because that
  324. * would break once additional permissions (like ACL_APPEND, ACL_DELETE
  325. * for directories) are added. There are no more bits available in the
  326. * file mode.
  327. *
  328. * inode->i_sem: down
  329. */
  330. int
  331. ext2_acl_chmod(struct inode *inode)
  332. {
  333. struct posix_acl *acl, *clone;
  334. int error;
  335. if (!test_opt(inode->i_sb, POSIX_ACL))
  336. return 0;
  337. if (S_ISLNK(inode->i_mode))
  338. return -EOPNOTSUPP;
  339. acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  340. if (IS_ERR(acl) || !acl)
  341. return PTR_ERR(acl);
  342. clone = posix_acl_clone(acl, GFP_KERNEL);
  343. posix_acl_release(acl);
  344. if (!clone)
  345. return -ENOMEM;
  346. error = posix_acl_chmod_masq(clone, inode->i_mode);
  347. if (!error)
  348. error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
  349. posix_acl_release(clone);
  350. return error;
  351. }
  352. /*
  353. * Extended attribut handlers
  354. */
  355. static size_t
  356. ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
  357. const char *name, size_t name_len)
  358. {
  359. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  360. if (!test_opt(inode->i_sb, POSIX_ACL))
  361. return 0;
  362. if (list && size <= list_size)
  363. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  364. return size;
  365. }
  366. static size_t
  367. ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
  368. const char *name, size_t name_len)
  369. {
  370. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  371. if (!test_opt(inode->i_sb, POSIX_ACL))
  372. return 0;
  373. if (list && size <= list_size)
  374. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  375. return size;
  376. }
  377. static int
  378. ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
  379. {
  380. struct posix_acl *acl;
  381. int error;
  382. if (!test_opt(inode->i_sb, POSIX_ACL))
  383. return -EOPNOTSUPP;
  384. acl = ext2_get_acl(inode, type);
  385. if (IS_ERR(acl))
  386. return PTR_ERR(acl);
  387. if (acl == NULL)
  388. return -ENODATA;
  389. error = posix_acl_to_xattr(acl, buffer, size);
  390. posix_acl_release(acl);
  391. return error;
  392. }
  393. static int
  394. ext2_xattr_get_acl_access(struct inode *inode, const char *name,
  395. void *buffer, size_t size)
  396. {
  397. if (strcmp(name, "") != 0)
  398. return -EINVAL;
  399. return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  400. }
  401. static int
  402. ext2_xattr_get_acl_default(struct inode *inode, const char *name,
  403. void *buffer, size_t size)
  404. {
  405. if (strcmp(name, "") != 0)
  406. return -EINVAL;
  407. return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  408. }
  409. static int
  410. ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
  411. size_t size)
  412. {
  413. struct posix_acl *acl;
  414. int error;
  415. if (!test_opt(inode->i_sb, POSIX_ACL))
  416. return -EOPNOTSUPP;
  417. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  418. return -EPERM;
  419. if (value) {
  420. acl = posix_acl_from_xattr(value, size);
  421. if (IS_ERR(acl))
  422. return PTR_ERR(acl);
  423. else if (acl) {
  424. error = posix_acl_valid(acl);
  425. if (error)
  426. goto release_and_out;
  427. }
  428. } else
  429. acl = NULL;
  430. error = ext2_set_acl(inode, type, acl);
  431. release_and_out:
  432. posix_acl_release(acl);
  433. return error;
  434. }
  435. static int
  436. ext2_xattr_set_acl_access(struct inode *inode, const char *name,
  437. const void *value, size_t size, int flags)
  438. {
  439. if (strcmp(name, "") != 0)
  440. return -EINVAL;
  441. return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  442. }
  443. static int
  444. ext2_xattr_set_acl_default(struct inode *inode, const char *name,
  445. const void *value, size_t size, int flags)
  446. {
  447. if (strcmp(name, "") != 0)
  448. return -EINVAL;
  449. return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  450. }
  451. struct xattr_handler ext2_xattr_acl_access_handler = {
  452. .prefix = POSIX_ACL_XATTR_ACCESS,
  453. .list = ext2_xattr_list_acl_access,
  454. .get = ext2_xattr_get_acl_access,
  455. .set = ext2_xattr_set_acl_access,
  456. };
  457. struct xattr_handler ext2_xattr_acl_default_handler = {
  458. .prefix = POSIX_ACL_XATTR_DEFAULT,
  459. .list = ext2_xattr_list_acl_default,
  460. .get = ext2_xattr_get_acl_default,
  461. .set = ext2_xattr_set_acl_default,
  462. };