acl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. kfree(value);
  178. if (!IS_ERR(acl)) {
  179. switch(type) {
  180. case ACL_TYPE_ACCESS:
  181. ext2_iset_acl(inode, &ei->i_acl, acl);
  182. break;
  183. case ACL_TYPE_DEFAULT:
  184. ext2_iset_acl(inode, &ei->i_default_acl, acl);
  185. break;
  186. }
  187. }
  188. return acl;
  189. }
  190. /*
  191. * inode->i_sem: down
  192. */
  193. static int
  194. ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  195. {
  196. struct ext2_inode_info *ei = EXT2_I(inode);
  197. int name_index;
  198. void *value = NULL;
  199. size_t size;
  200. int error;
  201. if (S_ISLNK(inode->i_mode))
  202. return -EOPNOTSUPP;
  203. if (!test_opt(inode->i_sb, POSIX_ACL))
  204. return 0;
  205. switch(type) {
  206. case ACL_TYPE_ACCESS:
  207. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  208. if (acl) {
  209. mode_t mode = inode->i_mode;
  210. error = posix_acl_equiv_mode(acl, &mode);
  211. if (error < 0)
  212. return error;
  213. else {
  214. inode->i_mode = mode;
  215. mark_inode_dirty(inode);
  216. if (error == 0)
  217. acl = NULL;
  218. }
  219. }
  220. break;
  221. case ACL_TYPE_DEFAULT:
  222. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  223. if (!S_ISDIR(inode->i_mode))
  224. return acl ? -EACCES : 0;
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. if (acl) {
  230. value = ext2_acl_to_disk(acl, &size);
  231. if (IS_ERR(value))
  232. return (int)PTR_ERR(value);
  233. }
  234. error = ext2_xattr_set(inode, name_index, "", value, size, 0);
  235. kfree(value);
  236. if (!error) {
  237. switch(type) {
  238. case ACL_TYPE_ACCESS:
  239. ext2_iset_acl(inode, &ei->i_acl, acl);
  240. break;
  241. case ACL_TYPE_DEFAULT:
  242. ext2_iset_acl(inode, &ei->i_default_acl, acl);
  243. break;
  244. }
  245. }
  246. return error;
  247. }
  248. static int
  249. ext2_check_acl(struct inode *inode, int mask)
  250. {
  251. struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  252. if (IS_ERR(acl))
  253. return PTR_ERR(acl);
  254. if (acl) {
  255. int error = posix_acl_permission(inode, acl, mask);
  256. posix_acl_release(acl);
  257. return error;
  258. }
  259. return -EAGAIN;
  260. }
  261. int
  262. ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
  263. {
  264. return generic_permission(inode, mask, ext2_check_acl);
  265. }
  266. /*
  267. * Initialize the ACLs of a new inode. Called from ext2_new_inode.
  268. *
  269. * dir->i_sem: down
  270. * inode->i_sem: up (access to inode is still exclusive)
  271. */
  272. int
  273. ext2_init_acl(struct inode *inode, struct inode *dir)
  274. {
  275. struct posix_acl *acl = NULL;
  276. int error = 0;
  277. if (!S_ISLNK(inode->i_mode)) {
  278. if (test_opt(dir->i_sb, POSIX_ACL)) {
  279. acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
  280. if (IS_ERR(acl))
  281. return PTR_ERR(acl);
  282. }
  283. if (!acl)
  284. inode->i_mode &= ~current->fs->umask;
  285. }
  286. if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
  287. struct posix_acl *clone;
  288. mode_t mode;
  289. if (S_ISDIR(inode->i_mode)) {
  290. error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  291. if (error)
  292. goto cleanup;
  293. }
  294. clone = posix_acl_clone(acl, GFP_KERNEL);
  295. error = -ENOMEM;
  296. if (!clone)
  297. goto cleanup;
  298. mode = inode->i_mode;
  299. error = posix_acl_create_masq(clone, &mode);
  300. if (error >= 0) {
  301. inode->i_mode = mode;
  302. if (error > 0) {
  303. /* This is an extended ACL */
  304. error = ext2_set_acl(inode,
  305. ACL_TYPE_ACCESS, clone);
  306. }
  307. }
  308. posix_acl_release(clone);
  309. }
  310. cleanup:
  311. posix_acl_release(acl);
  312. return error;
  313. }
  314. /*
  315. * Does chmod for an inode that may have an Access Control List. The
  316. * inode->i_mode field must be updated to the desired value by the caller
  317. * before calling this function.
  318. * Returns 0 on success, or a negative error number.
  319. *
  320. * We change the ACL rather than storing some ACL entries in the file
  321. * mode permission bits (which would be more efficient), because that
  322. * would break once additional permissions (like ACL_APPEND, ACL_DELETE
  323. * for directories) are added. There are no more bits available in the
  324. * file mode.
  325. *
  326. * inode->i_sem: down
  327. */
  328. int
  329. ext2_acl_chmod(struct inode *inode)
  330. {
  331. struct posix_acl *acl, *clone;
  332. int error;
  333. if (!test_opt(inode->i_sb, POSIX_ACL))
  334. return 0;
  335. if (S_ISLNK(inode->i_mode))
  336. return -EOPNOTSUPP;
  337. acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
  338. if (IS_ERR(acl) || !acl)
  339. return PTR_ERR(acl);
  340. clone = posix_acl_clone(acl, GFP_KERNEL);
  341. posix_acl_release(acl);
  342. if (!clone)
  343. return -ENOMEM;
  344. error = posix_acl_chmod_masq(clone, inode->i_mode);
  345. if (!error)
  346. error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
  347. posix_acl_release(clone);
  348. return error;
  349. }
  350. /*
  351. * Extended attribut handlers
  352. */
  353. static size_t
  354. ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
  355. const char *name, size_t name_len)
  356. {
  357. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  358. if (!test_opt(inode->i_sb, POSIX_ACL))
  359. return 0;
  360. if (list && size <= list_size)
  361. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  362. return size;
  363. }
  364. static size_t
  365. ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
  366. const char *name, size_t name_len)
  367. {
  368. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  369. if (!test_opt(inode->i_sb, POSIX_ACL))
  370. return 0;
  371. if (list && size <= list_size)
  372. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  373. return size;
  374. }
  375. static int
  376. ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
  377. {
  378. struct posix_acl *acl;
  379. int error;
  380. if (!test_opt(inode->i_sb, POSIX_ACL))
  381. return -EOPNOTSUPP;
  382. acl = ext2_get_acl(inode, type);
  383. if (IS_ERR(acl))
  384. return PTR_ERR(acl);
  385. if (acl == NULL)
  386. return -ENODATA;
  387. error = posix_acl_to_xattr(acl, buffer, size);
  388. posix_acl_release(acl);
  389. return error;
  390. }
  391. static int
  392. ext2_xattr_get_acl_access(struct inode *inode, const char *name,
  393. void *buffer, size_t size)
  394. {
  395. if (strcmp(name, "") != 0)
  396. return -EINVAL;
  397. return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  398. }
  399. static int
  400. ext2_xattr_get_acl_default(struct inode *inode, const char *name,
  401. void *buffer, size_t size)
  402. {
  403. if (strcmp(name, "") != 0)
  404. return -EINVAL;
  405. return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  406. }
  407. static int
  408. ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
  409. size_t size)
  410. {
  411. struct posix_acl *acl;
  412. int error;
  413. if (!test_opt(inode->i_sb, POSIX_ACL))
  414. return -EOPNOTSUPP;
  415. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  416. return -EPERM;
  417. if (value) {
  418. acl = posix_acl_from_xattr(value, size);
  419. if (IS_ERR(acl))
  420. return PTR_ERR(acl);
  421. else if (acl) {
  422. error = posix_acl_valid(acl);
  423. if (error)
  424. goto release_and_out;
  425. }
  426. } else
  427. acl = NULL;
  428. error = ext2_set_acl(inode, type, acl);
  429. release_and_out:
  430. posix_acl_release(acl);
  431. return error;
  432. }
  433. static int
  434. ext2_xattr_set_acl_access(struct inode *inode, const char *name,
  435. const void *value, size_t size, int flags)
  436. {
  437. if (strcmp(name, "") != 0)
  438. return -EINVAL;
  439. return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  440. }
  441. static int
  442. ext2_xattr_set_acl_default(struct inode *inode, const char *name,
  443. const void *value, size_t size, int flags)
  444. {
  445. if (strcmp(name, "") != 0)
  446. return -EINVAL;
  447. return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  448. }
  449. struct xattr_handler ext2_xattr_acl_access_handler = {
  450. .prefix = POSIX_ACL_XATTR_ACCESS,
  451. .list = ext2_xattr_list_acl_access,
  452. .get = ext2_xattr_get_acl_access,
  453. .set = ext2_xattr_set_acl_access,
  454. };
  455. struct xattr_handler ext2_xattr_acl_default_handler = {
  456. .prefix = POSIX_ACL_XATTR_DEFAULT,
  457. .list = ext2_xattr_list_acl_default,
  458. .get = ext2_xattr_get_acl_default,
  459. .set = ext2_xattr_set_acl_default,
  460. };