generic_acl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
  3. *
  4. * This file is released under the GPL.
  5. *
  6. * Generic ACL support for in-memory filesystems.
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/gfp.h>
  10. #include <linux/fs.h>
  11. #include <linux/generic_acl.h>
  12. #include <linux/posix_acl.h>
  13. #include <linux/posix_acl_xattr.h>
  14. static size_t
  15. generic_acl_list(struct dentry *dentry, char *list, size_t list_size,
  16. const char *name, size_t name_len, int type)
  17. {
  18. struct posix_acl *acl;
  19. const char *xname;
  20. size_t size;
  21. acl = get_cached_acl(dentry->d_inode, type);
  22. if (!acl)
  23. return 0;
  24. posix_acl_release(acl);
  25. switch (type) {
  26. case ACL_TYPE_ACCESS:
  27. xname = POSIX_ACL_XATTR_ACCESS;
  28. break;
  29. case ACL_TYPE_DEFAULT:
  30. xname = POSIX_ACL_XATTR_DEFAULT;
  31. break;
  32. default:
  33. return 0;
  34. }
  35. size = strlen(xname) + 1;
  36. if (list && size <= list_size)
  37. memcpy(list, xname, size);
  38. return size;
  39. }
  40. static int
  41. generic_acl_get(struct dentry *dentry, const char *name, void *buffer,
  42. size_t size, int type)
  43. {
  44. struct posix_acl *acl;
  45. int error;
  46. if (strcmp(name, "") != 0)
  47. return -EINVAL;
  48. acl = get_cached_acl(dentry->d_inode, type);
  49. if (!acl)
  50. return -ENODATA;
  51. error = posix_acl_to_xattr(acl, buffer, size);
  52. posix_acl_release(acl);
  53. return error;
  54. }
  55. static int
  56. generic_acl_set(struct dentry *dentry, const char *name, const void *value,
  57. size_t size, int flags, int type)
  58. {
  59. struct inode *inode = dentry->d_inode;
  60. struct posix_acl *acl = NULL;
  61. int error;
  62. if (strcmp(name, "") != 0)
  63. return -EINVAL;
  64. if (S_ISLNK(inode->i_mode))
  65. return -EOPNOTSUPP;
  66. if (!inode_owner_or_capable(inode))
  67. return -EPERM;
  68. if (value) {
  69. acl = posix_acl_from_xattr(value, size);
  70. if (IS_ERR(acl))
  71. return PTR_ERR(acl);
  72. }
  73. if (acl) {
  74. mode_t mode;
  75. error = posix_acl_valid(acl);
  76. if (error)
  77. goto failed;
  78. switch (type) {
  79. case ACL_TYPE_ACCESS:
  80. mode = inode->i_mode;
  81. error = posix_acl_equiv_mode(acl, &mode);
  82. if (error < 0)
  83. goto failed;
  84. inode->i_mode = mode;
  85. inode->i_ctime = CURRENT_TIME;
  86. if (error == 0) {
  87. posix_acl_release(acl);
  88. acl = NULL;
  89. }
  90. break;
  91. case ACL_TYPE_DEFAULT:
  92. if (!S_ISDIR(inode->i_mode)) {
  93. error = -EINVAL;
  94. goto failed;
  95. }
  96. break;
  97. }
  98. }
  99. set_cached_acl(inode, type, acl);
  100. error = 0;
  101. failed:
  102. posix_acl_release(acl);
  103. return error;
  104. }
  105. /**
  106. * generic_acl_init - Take care of acl inheritance at @inode create time
  107. *
  108. * Files created inside a directory with a default ACL inherit the
  109. * directory's default ACL.
  110. */
  111. int
  112. generic_acl_init(struct inode *inode, struct inode *dir)
  113. {
  114. struct posix_acl *acl = NULL;
  115. mode_t mode = inode->i_mode;
  116. int error;
  117. inode->i_mode = mode & ~current_umask();
  118. if (!S_ISLNK(inode->i_mode))
  119. acl = get_cached_acl(dir, ACL_TYPE_DEFAULT);
  120. if (acl) {
  121. struct posix_acl *clone;
  122. if (S_ISDIR(inode->i_mode)) {
  123. clone = posix_acl_clone(acl, GFP_KERNEL);
  124. error = -ENOMEM;
  125. if (!clone)
  126. goto cleanup;
  127. set_cached_acl(inode, ACL_TYPE_DEFAULT, clone);
  128. posix_acl_release(clone);
  129. }
  130. clone = posix_acl_clone(acl, GFP_KERNEL);
  131. error = -ENOMEM;
  132. if (!clone)
  133. goto cleanup;
  134. error = posix_acl_create_masq(clone, &mode);
  135. if (error >= 0) {
  136. inode->i_mode = mode;
  137. if (error > 0)
  138. set_cached_acl(inode, ACL_TYPE_ACCESS, clone);
  139. }
  140. posix_acl_release(clone);
  141. }
  142. error = 0;
  143. cleanup:
  144. posix_acl_release(acl);
  145. return error;
  146. }
  147. /**
  148. * generic_acl_chmod - change the access acl of @inode upon chmod()
  149. *
  150. * A chmod also changes the permissions of the owner, group/mask, and
  151. * other ACL entries.
  152. */
  153. int
  154. generic_acl_chmod(struct inode *inode)
  155. {
  156. struct posix_acl *acl, *clone;
  157. int error = 0;
  158. if (S_ISLNK(inode->i_mode))
  159. return -EOPNOTSUPP;
  160. acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  161. if (acl) {
  162. clone = posix_acl_clone(acl, GFP_KERNEL);
  163. posix_acl_release(acl);
  164. if (!clone)
  165. return -ENOMEM;
  166. error = posix_acl_chmod_masq(clone, inode->i_mode);
  167. if (!error)
  168. set_cached_acl(inode, ACL_TYPE_ACCESS, clone);
  169. posix_acl_release(clone);
  170. }
  171. return error;
  172. }
  173. int
  174. generic_check_acl(struct inode *inode, int mask, unsigned int flags)
  175. {
  176. if (flags & IPERM_FLAG_RCU) {
  177. if (!negative_cached_acl(inode, ACL_TYPE_ACCESS))
  178. return -ECHILD;
  179. } else {
  180. struct posix_acl *acl;
  181. acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  182. if (acl) {
  183. int error = posix_acl_permission(inode, acl, mask);
  184. posix_acl_release(acl);
  185. return error;
  186. }
  187. }
  188. return -EAGAIN;
  189. }
  190. const struct xattr_handler generic_acl_access_handler = {
  191. .prefix = POSIX_ACL_XATTR_ACCESS,
  192. .flags = ACL_TYPE_ACCESS,
  193. .list = generic_acl_list,
  194. .get = generic_acl_get,
  195. .set = generic_acl_set,
  196. };
  197. const struct xattr_handler generic_acl_default_handler = {
  198. .prefix = POSIX_ACL_XATTR_DEFAULT,
  199. .flags = ACL_TYPE_DEFAULT,
  200. .list = generic_acl_list,
  201. .get = generic_acl_get,
  202. .set = generic_acl_set,
  203. };