acl.c 12 KB

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