xfs_acl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (c) 2008, Christoph Hellwig
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_acl.h"
  20. #include "xfs_attr.h"
  21. #include "xfs_bmap_btree.h"
  22. #include "xfs_inode.h"
  23. #include "xfs_vnodeops.h"
  24. #include "xfs_trace.h"
  25. #include <linux/slab.h>
  26. #include <linux/xattr.h>
  27. #include <linux/posix_acl_xattr.h>
  28. /*
  29. * Locking scheme:
  30. * - all ACL updates are protected by inode->i_mutex, which is taken before
  31. * calling into this file.
  32. */
  33. STATIC struct posix_acl *
  34. xfs_acl_from_disk(struct xfs_acl *aclp)
  35. {
  36. struct posix_acl_entry *acl_e;
  37. struct posix_acl *acl;
  38. struct xfs_acl_entry *ace;
  39. unsigned int count, i;
  40. count = be32_to_cpu(aclp->acl_cnt);
  41. if (count > XFS_ACL_MAX_ENTRIES)
  42. return ERR_PTR(-EFSCORRUPTED);
  43. acl = posix_acl_alloc(count, GFP_KERNEL);
  44. if (!acl)
  45. return ERR_PTR(-ENOMEM);
  46. for (i = 0; i < count; i++) {
  47. acl_e = &acl->a_entries[i];
  48. ace = &aclp->acl_entry[i];
  49. /*
  50. * The tag is 32 bits on disk and 16 bits in core.
  51. *
  52. * Because every access to it goes through the core
  53. * format first this is not a problem.
  54. */
  55. acl_e->e_tag = be32_to_cpu(ace->ae_tag);
  56. acl_e->e_perm = be16_to_cpu(ace->ae_perm);
  57. switch (acl_e->e_tag) {
  58. case ACL_USER:
  59. case ACL_GROUP:
  60. acl_e->e_id = be32_to_cpu(ace->ae_id);
  61. break;
  62. case ACL_USER_OBJ:
  63. case ACL_GROUP_OBJ:
  64. case ACL_MASK:
  65. case ACL_OTHER:
  66. acl_e->e_id = ACL_UNDEFINED_ID;
  67. break;
  68. default:
  69. goto fail;
  70. }
  71. }
  72. return acl;
  73. fail:
  74. posix_acl_release(acl);
  75. return ERR_PTR(-EINVAL);
  76. }
  77. STATIC void
  78. xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
  79. {
  80. const struct posix_acl_entry *acl_e;
  81. struct xfs_acl_entry *ace;
  82. int i;
  83. aclp->acl_cnt = cpu_to_be32(acl->a_count);
  84. for (i = 0; i < acl->a_count; i++) {
  85. ace = &aclp->acl_entry[i];
  86. acl_e = &acl->a_entries[i];
  87. ace->ae_tag = cpu_to_be32(acl_e->e_tag);
  88. ace->ae_id = cpu_to_be32(acl_e->e_id);
  89. ace->ae_perm = cpu_to_be16(acl_e->e_perm);
  90. }
  91. }
  92. struct posix_acl *
  93. xfs_get_acl(struct inode *inode, int type)
  94. {
  95. struct xfs_inode *ip = XFS_I(inode);
  96. struct posix_acl *acl;
  97. struct xfs_acl *xfs_acl;
  98. int len = sizeof(struct xfs_acl);
  99. unsigned char *ea_name;
  100. int error;
  101. acl = get_cached_acl(inode, type);
  102. if (acl != ACL_NOT_CACHED)
  103. return acl;
  104. switch (type) {
  105. case ACL_TYPE_ACCESS:
  106. ea_name = SGI_ACL_FILE;
  107. break;
  108. case ACL_TYPE_DEFAULT:
  109. ea_name = SGI_ACL_DEFAULT;
  110. break;
  111. default:
  112. BUG();
  113. }
  114. /*
  115. * If we have a cached ACLs value just return it, not need to
  116. * go out to the disk.
  117. */
  118. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  119. if (!xfs_acl)
  120. return ERR_PTR(-ENOMEM);
  121. error = -xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
  122. &len, ATTR_ROOT);
  123. if (error) {
  124. /*
  125. * If the attribute doesn't exist make sure we have a negative
  126. * cache entry, for any other error assume it is transient and
  127. * leave the cache entry as ACL_NOT_CACHED.
  128. */
  129. if (error == -ENOATTR) {
  130. acl = NULL;
  131. goto out_update_cache;
  132. }
  133. goto out;
  134. }
  135. acl = xfs_acl_from_disk(xfs_acl);
  136. if (IS_ERR(acl))
  137. goto out;
  138. out_update_cache:
  139. set_cached_acl(inode, type, acl);
  140. out:
  141. kfree(xfs_acl);
  142. return acl;
  143. }
  144. STATIC int
  145. xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  146. {
  147. struct xfs_inode *ip = XFS_I(inode);
  148. unsigned char *ea_name;
  149. int error;
  150. if (S_ISLNK(inode->i_mode))
  151. return -EOPNOTSUPP;
  152. switch (type) {
  153. case ACL_TYPE_ACCESS:
  154. ea_name = SGI_ACL_FILE;
  155. break;
  156. case ACL_TYPE_DEFAULT:
  157. if (!S_ISDIR(inode->i_mode))
  158. return acl ? -EACCES : 0;
  159. ea_name = SGI_ACL_DEFAULT;
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. if (acl) {
  165. struct xfs_acl *xfs_acl;
  166. int len;
  167. xfs_acl = kzalloc(sizeof(struct xfs_acl), GFP_KERNEL);
  168. if (!xfs_acl)
  169. return -ENOMEM;
  170. xfs_acl_to_disk(xfs_acl, acl);
  171. len = sizeof(struct xfs_acl) -
  172. (sizeof(struct xfs_acl_entry) *
  173. (XFS_ACL_MAX_ENTRIES - acl->a_count));
  174. error = -xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
  175. len, ATTR_ROOT);
  176. kfree(xfs_acl);
  177. } else {
  178. /*
  179. * A NULL ACL argument means we want to remove the ACL.
  180. */
  181. error = -xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  182. /*
  183. * If the attribute didn't exist to start with that's fine.
  184. */
  185. if (error == -ENOATTR)
  186. error = 0;
  187. }
  188. if (!error)
  189. set_cached_acl(inode, type, acl);
  190. return error;
  191. }
  192. int
  193. xfs_check_acl(struct inode *inode, int mask, unsigned int flags)
  194. {
  195. struct xfs_inode *ip;
  196. struct posix_acl *acl;
  197. int error = -EAGAIN;
  198. ip = XFS_I(inode);
  199. trace_xfs_check_acl(ip);
  200. /*
  201. * If there is no attribute fork no ACL exists on this inode and
  202. * we can skip the whole exercise.
  203. */
  204. if (!XFS_IFORK_Q(ip))
  205. return -EAGAIN;
  206. if (flags & IPERM_FLAG_RCU) {
  207. if (!negative_cached_acl(inode, ACL_TYPE_ACCESS))
  208. return -ECHILD;
  209. return -EAGAIN;
  210. }
  211. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  212. if (IS_ERR(acl))
  213. return PTR_ERR(acl);
  214. if (acl) {
  215. error = posix_acl_permission(inode, acl, mask);
  216. posix_acl_release(acl);
  217. }
  218. return error;
  219. }
  220. static int
  221. xfs_set_mode(struct inode *inode, mode_t mode)
  222. {
  223. int error = 0;
  224. if (mode != inode->i_mode) {
  225. struct iattr iattr;
  226. iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
  227. iattr.ia_mode = mode;
  228. iattr.ia_ctime = current_fs_time(inode->i_sb);
  229. error = -xfs_setattr(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  230. }
  231. return error;
  232. }
  233. static int
  234. xfs_acl_exists(struct inode *inode, unsigned char *name)
  235. {
  236. int len = sizeof(struct xfs_acl);
  237. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  238. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  239. }
  240. int
  241. posix_acl_access_exists(struct inode *inode)
  242. {
  243. return xfs_acl_exists(inode, SGI_ACL_FILE);
  244. }
  245. int
  246. posix_acl_default_exists(struct inode *inode)
  247. {
  248. if (!S_ISDIR(inode->i_mode))
  249. return 0;
  250. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  251. }
  252. /*
  253. * No need for i_mutex because the inode is not yet exposed to the VFS.
  254. */
  255. int
  256. xfs_inherit_acl(struct inode *inode, struct posix_acl *default_acl)
  257. {
  258. struct posix_acl *clone;
  259. mode_t mode;
  260. int error = 0, inherit = 0;
  261. if (S_ISDIR(inode->i_mode)) {
  262. error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  263. if (error)
  264. return error;
  265. }
  266. clone = posix_acl_clone(default_acl, GFP_KERNEL);
  267. if (!clone)
  268. return -ENOMEM;
  269. mode = inode->i_mode;
  270. error = posix_acl_create_masq(clone, &mode);
  271. if (error < 0)
  272. goto out_release_clone;
  273. /*
  274. * If posix_acl_create_masq returns a positive value we need to
  275. * inherit a permission that can't be represented using the Unix
  276. * mode bits and we actually need to set an ACL.
  277. */
  278. if (error > 0)
  279. inherit = 1;
  280. error = xfs_set_mode(inode, mode);
  281. if (error)
  282. goto out_release_clone;
  283. if (inherit)
  284. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  285. out_release_clone:
  286. posix_acl_release(clone);
  287. return error;
  288. }
  289. int
  290. xfs_acl_chmod(struct inode *inode)
  291. {
  292. struct posix_acl *acl, *clone;
  293. int error;
  294. if (S_ISLNK(inode->i_mode))
  295. return -EOPNOTSUPP;
  296. acl = xfs_get_acl(inode, ACL_TYPE_ACCESS);
  297. if (IS_ERR(acl) || !acl)
  298. return PTR_ERR(acl);
  299. clone = posix_acl_clone(acl, GFP_KERNEL);
  300. posix_acl_release(acl);
  301. if (!clone)
  302. return -ENOMEM;
  303. error = posix_acl_chmod_masq(clone, inode->i_mode);
  304. if (!error)
  305. error = xfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  306. posix_acl_release(clone);
  307. return error;
  308. }
  309. static int
  310. xfs_xattr_acl_get(struct dentry *dentry, const char *name,
  311. void *value, size_t size, int type)
  312. {
  313. struct posix_acl *acl;
  314. int error;
  315. acl = xfs_get_acl(dentry->d_inode, type);
  316. if (IS_ERR(acl))
  317. return PTR_ERR(acl);
  318. if (acl == NULL)
  319. return -ENODATA;
  320. error = posix_acl_to_xattr(acl, value, size);
  321. posix_acl_release(acl);
  322. return error;
  323. }
  324. static int
  325. xfs_xattr_acl_set(struct dentry *dentry, const char *name,
  326. const void *value, size_t size, int flags, int type)
  327. {
  328. struct inode *inode = dentry->d_inode;
  329. struct posix_acl *acl = NULL;
  330. int error = 0;
  331. if (flags & XATTR_CREATE)
  332. return -EINVAL;
  333. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  334. return value ? -EACCES : 0;
  335. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  336. return -EPERM;
  337. if (!value)
  338. goto set_acl;
  339. acl = posix_acl_from_xattr(value, size);
  340. if (!acl) {
  341. /*
  342. * acl_set_file(3) may request that we set default ACLs with
  343. * zero length -- defend (gracefully) against that here.
  344. */
  345. goto out;
  346. }
  347. if (IS_ERR(acl)) {
  348. error = PTR_ERR(acl);
  349. goto out;
  350. }
  351. error = posix_acl_valid(acl);
  352. if (error)
  353. goto out_release;
  354. error = -EINVAL;
  355. if (acl->a_count > XFS_ACL_MAX_ENTRIES)
  356. goto out_release;
  357. if (type == ACL_TYPE_ACCESS) {
  358. mode_t mode = inode->i_mode;
  359. error = posix_acl_equiv_mode(acl, &mode);
  360. if (error <= 0) {
  361. posix_acl_release(acl);
  362. acl = NULL;
  363. if (error < 0)
  364. return error;
  365. }
  366. error = xfs_set_mode(inode, mode);
  367. if (error)
  368. goto out_release;
  369. }
  370. set_acl:
  371. error = xfs_set_acl(inode, type, acl);
  372. out_release:
  373. posix_acl_release(acl);
  374. out:
  375. return error;
  376. }
  377. const struct xattr_handler xfs_xattr_acl_access_handler = {
  378. .prefix = POSIX_ACL_XATTR_ACCESS,
  379. .flags = ACL_TYPE_ACCESS,
  380. .get = xfs_xattr_acl_get,
  381. .set = xfs_xattr_acl_set,
  382. };
  383. const struct xattr_handler xfs_xattr_acl_default_handler = {
  384. .prefix = POSIX_ACL_XATTR_DEFAULT,
  385. .flags = ACL_TYPE_DEFAULT,
  386. .get = xfs_xattr_acl_get,
  387. .set = xfs_xattr_acl_set,
  388. };