acl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/xattr.h>
  15. #include <linux/posix_acl.h>
  16. #include <linux/posix_acl_xattr.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "acl.h"
  21. #include "xattr.h"
  22. #include "glock.h"
  23. #include "inode.h"
  24. #include "meta_io.h"
  25. #include "trans.h"
  26. #include "util.h"
  27. static int acl_get(struct gfs2_inode *ip, const char *name,
  28. struct posix_acl **acl, struct gfs2_ea_location *el,
  29. char **datap, unsigned int *lenp)
  30. {
  31. char *data;
  32. unsigned int len;
  33. int error;
  34. el->el_bh = NULL;
  35. if (!ip->i_eattr)
  36. return 0;
  37. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el);
  38. if (error)
  39. return error;
  40. if (!el->el_ea)
  41. return 0;
  42. if (!GFS2_EA_DATA_LEN(el->el_ea))
  43. goto out;
  44. len = GFS2_EA_DATA_LEN(el->el_ea);
  45. data = kmalloc(len, GFP_NOFS);
  46. error = -ENOMEM;
  47. if (!data)
  48. goto out;
  49. error = gfs2_ea_get_copy(ip, el, data, len);
  50. if (error < 0)
  51. goto out_kfree;
  52. error = 0;
  53. if (acl) {
  54. *acl = posix_acl_from_xattr(data, len);
  55. if (IS_ERR(*acl))
  56. error = PTR_ERR(*acl);
  57. }
  58. out_kfree:
  59. if (error || !datap) {
  60. kfree(data);
  61. } else {
  62. *datap = data;
  63. *lenp = len;
  64. }
  65. out:
  66. return error;
  67. }
  68. /**
  69. * gfs2_check_acl - Check an ACL to see if we're allowed to do something
  70. * @inode: the file we want to do something to
  71. * @mask: what we want to do
  72. *
  73. * Returns: errno
  74. */
  75. int gfs2_check_acl(struct inode *inode, int mask)
  76. {
  77. struct gfs2_ea_location el;
  78. struct posix_acl *acl = NULL;
  79. int error;
  80. error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL);
  81. brelse(el.el_bh);
  82. if (error)
  83. return error;
  84. if (acl) {
  85. error = posix_acl_permission(inode, acl, mask);
  86. posix_acl_release(acl);
  87. return error;
  88. }
  89. return -EAGAIN;
  90. }
  91. static int munge_mode(struct gfs2_inode *ip, mode_t mode)
  92. {
  93. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  94. struct buffer_head *dibh;
  95. int error;
  96. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  97. if (error)
  98. return error;
  99. error = gfs2_meta_inode_buffer(ip, &dibh);
  100. if (!error) {
  101. gfs2_assert_withdraw(sdp,
  102. (ip->i_inode.i_mode & S_IFMT) == (mode & S_IFMT));
  103. ip->i_inode.i_mode = mode;
  104. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  105. gfs2_dinode_out(ip, dibh->b_data);
  106. brelse(dibh);
  107. }
  108. gfs2_trans_end(sdp);
  109. return 0;
  110. }
  111. int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
  112. {
  113. struct gfs2_ea_location el;
  114. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  115. struct posix_acl *acl = NULL, *clone;
  116. mode_t mode = ip->i_inode.i_mode;
  117. char *data = NULL;
  118. unsigned int len;
  119. int error;
  120. if (!sdp->sd_args.ar_posix_acl)
  121. return 0;
  122. if (S_ISLNK(ip->i_inode.i_mode))
  123. return 0;
  124. error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len);
  125. brelse(el.el_bh);
  126. if (error)
  127. return error;
  128. if (!acl) {
  129. mode &= ~current_umask();
  130. if (mode != ip->i_inode.i_mode)
  131. error = munge_mode(ip, mode);
  132. return error;
  133. }
  134. clone = posix_acl_clone(acl, GFP_NOFS);
  135. error = -ENOMEM;
  136. if (!clone)
  137. goto out;
  138. posix_acl_release(acl);
  139. acl = clone;
  140. if (S_ISDIR(ip->i_inode.i_mode)) {
  141. error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
  142. GFS2_POSIX_ACL_DEFAULT, data, len, 0);
  143. if (error)
  144. goto out;
  145. }
  146. error = posix_acl_create_masq(acl, &mode);
  147. if (error < 0)
  148. goto out;
  149. if (error == 0)
  150. goto munge;
  151. posix_acl_to_xattr(acl, data, len);
  152. error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
  153. GFS2_POSIX_ACL_ACCESS, data, len, 0);
  154. if (error)
  155. goto out;
  156. munge:
  157. error = munge_mode(ip, mode);
  158. out:
  159. posix_acl_release(acl);
  160. kfree(data);
  161. return error;
  162. }
  163. int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
  164. {
  165. struct posix_acl *acl = NULL, *clone;
  166. struct gfs2_ea_location el;
  167. char *data;
  168. unsigned int len;
  169. int error;
  170. error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len);
  171. if (error)
  172. goto out_brelse;
  173. if (!acl)
  174. return gfs2_setattr_simple(ip, attr);
  175. clone = posix_acl_clone(acl, GFP_NOFS);
  176. error = -ENOMEM;
  177. if (!clone)
  178. goto out;
  179. posix_acl_release(acl);
  180. acl = clone;
  181. error = posix_acl_chmod_masq(acl, attr->ia_mode);
  182. if (!error) {
  183. posix_acl_to_xattr(acl, data, len);
  184. error = gfs2_ea_acl_chmod(ip, &el, attr, data);
  185. }
  186. out:
  187. posix_acl_release(acl);
  188. kfree(data);
  189. out_brelse:
  190. brelse(el.el_bh);
  191. return error;
  192. }
  193. static int gfs2_acl_type(const char *name)
  194. {
  195. if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
  196. return ACL_TYPE_ACCESS;
  197. if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
  198. return ACL_TYPE_DEFAULT;
  199. return -EINVAL;
  200. }
  201. static int gfs2_xattr_system_get(struct inode *inode, const char *name,
  202. void *buffer, size_t size)
  203. {
  204. int type;
  205. type = gfs2_acl_type(name);
  206. if (type < 0)
  207. return type;
  208. return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
  209. }
  210. static int gfs2_set_mode(struct inode *inode, mode_t mode)
  211. {
  212. int error = 0;
  213. if (mode != inode->i_mode) {
  214. struct iattr iattr;
  215. iattr.ia_valid = ATTR_MODE;
  216. iattr.ia_mode = mode;
  217. error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
  218. }
  219. return error;
  220. }
  221. static int gfs2_xattr_system_set(struct inode *inode, const char *name,
  222. const void *value, size_t size, int flags)
  223. {
  224. struct gfs2_sbd *sdp = GFS2_SB(inode);
  225. struct posix_acl *acl = NULL;
  226. int error = 0, type;
  227. if (!sdp->sd_args.ar_posix_acl)
  228. return -EOPNOTSUPP;
  229. type = gfs2_acl_type(name);
  230. if (type < 0)
  231. return type;
  232. if (flags & XATTR_CREATE)
  233. return -EINVAL;
  234. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  235. return value ? -EACCES : 0;
  236. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  237. return -EPERM;
  238. if (S_ISLNK(inode->i_mode))
  239. return -EOPNOTSUPP;
  240. if (!value)
  241. goto set_acl;
  242. acl = posix_acl_from_xattr(value, size);
  243. if (!acl) {
  244. /*
  245. * acl_set_file(3) may request that we set default ACLs with
  246. * zero length -- defend (gracefully) against that here.
  247. */
  248. goto out;
  249. }
  250. if (IS_ERR(acl)) {
  251. error = PTR_ERR(acl);
  252. goto out;
  253. }
  254. error = posix_acl_valid(acl);
  255. if (error)
  256. goto out_release;
  257. error = -EINVAL;
  258. if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
  259. goto out_release;
  260. if (type == ACL_TYPE_ACCESS) {
  261. mode_t mode = inode->i_mode;
  262. error = posix_acl_equiv_mode(acl, &mode);
  263. if (error <= 0) {
  264. posix_acl_release(acl);
  265. acl = NULL;
  266. if (error < 0)
  267. return error;
  268. }
  269. error = gfs2_set_mode(inode, mode);
  270. if (error)
  271. goto out_release;
  272. }
  273. set_acl:
  274. error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
  275. out_release:
  276. posix_acl_release(acl);
  277. out:
  278. return error;
  279. }
  280. struct xattr_handler gfs2_xattr_system_handler = {
  281. .prefix = XATTR_SYSTEM_PREFIX,
  282. .get = gfs2_xattr_system_get,
  283. .set = gfs2_xattr_system_set,
  284. };