acl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext2/acl.c
  4. *
  5. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  6. */
  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. break;
  54. case ACL_USER:
  55. value = (char *)value + sizeof(ext2_acl_entry);
  56. if ((char *)value > end)
  57. goto fail;
  58. acl->a_entries[n].e_uid =
  59. make_kuid(&init_user_ns,
  60. le32_to_cpu(entry->e_id));
  61. break;
  62. case ACL_GROUP:
  63. value = (char *)value + sizeof(ext2_acl_entry);
  64. if ((char *)value > end)
  65. goto fail;
  66. acl->a_entries[n].e_gid =
  67. make_kgid(&init_user_ns,
  68. le32_to_cpu(entry->e_id));
  69. break;
  70. default:
  71. goto fail;
  72. }
  73. }
  74. if (value != end)
  75. goto fail;
  76. return acl;
  77. fail:
  78. posix_acl_release(acl);
  79. return ERR_PTR(-EINVAL);
  80. }
  81. /*
  82. * Convert from in-memory to filesystem representation.
  83. */
  84. static void *
  85. ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
  86. {
  87. ext2_acl_header *ext_acl;
  88. char *e;
  89. size_t n;
  90. *size = ext2_acl_size(acl->a_count);
  91. ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
  92. sizeof(ext2_acl_entry), GFP_KERNEL);
  93. if (!ext_acl)
  94. return ERR_PTR(-ENOMEM);
  95. ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
  96. e = (char *)ext_acl + sizeof(ext2_acl_header);
  97. for (n=0; n < acl->a_count; n++) {
  98. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  99. ext2_acl_entry *entry = (ext2_acl_entry *)e;
  100. entry->e_tag = cpu_to_le16(acl_e->e_tag);
  101. entry->e_perm = cpu_to_le16(acl_e->e_perm);
  102. switch(acl_e->e_tag) {
  103. case ACL_USER:
  104. entry->e_id = cpu_to_le32(
  105. from_kuid(&init_user_ns, acl_e->e_uid));
  106. e += sizeof(ext2_acl_entry);
  107. break;
  108. case ACL_GROUP:
  109. entry->e_id = cpu_to_le32(
  110. from_kgid(&init_user_ns, acl_e->e_gid));
  111. e += sizeof(ext2_acl_entry);
  112. break;
  113. case ACL_USER_OBJ:
  114. case ACL_GROUP_OBJ:
  115. case ACL_MASK:
  116. case ACL_OTHER:
  117. e += sizeof(ext2_acl_entry_short);
  118. break;
  119. default:
  120. goto fail;
  121. }
  122. }
  123. return (char *)ext_acl;
  124. fail:
  125. kfree(ext_acl);
  126. return ERR_PTR(-EINVAL);
  127. }
  128. /*
  129. * inode->i_mutex: don't care
  130. */
  131. struct posix_acl *
  132. ext2_get_acl(struct inode *inode, int type)
  133. {
  134. int name_index;
  135. char *value = NULL;
  136. struct posix_acl *acl;
  137. int retval;
  138. switch (type) {
  139. case ACL_TYPE_ACCESS:
  140. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  141. break;
  142. case ACL_TYPE_DEFAULT:
  143. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  144. break;
  145. default:
  146. BUG();
  147. }
  148. retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
  149. if (retval > 0) {
  150. value = kmalloc(retval, GFP_KERNEL);
  151. if (!value)
  152. return ERR_PTR(-ENOMEM);
  153. retval = ext2_xattr_get(inode, name_index, "", value, retval);
  154. }
  155. if (retval > 0)
  156. acl = ext2_acl_from_disk(value, retval);
  157. else if (retval == -ENODATA || retval == -ENOSYS)
  158. acl = NULL;
  159. else
  160. acl = ERR_PTR(retval);
  161. kfree(value);
  162. return acl;
  163. }
  164. static int
  165. __ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  166. {
  167. int name_index;
  168. void *value = NULL;
  169. size_t size = 0;
  170. int error;
  171. switch(type) {
  172. case ACL_TYPE_ACCESS:
  173. name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
  174. break;
  175. case ACL_TYPE_DEFAULT:
  176. name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  177. if (!S_ISDIR(inode->i_mode))
  178. return acl ? -EACCES : 0;
  179. break;
  180. default:
  181. return -EINVAL;
  182. }
  183. if (acl) {
  184. value = ext2_acl_to_disk(acl, &size);
  185. if (IS_ERR(value))
  186. return (int)PTR_ERR(value);
  187. }
  188. error = ext2_xattr_set(inode, name_index, "", value, size, 0);
  189. kfree(value);
  190. if (!error)
  191. set_cached_acl(inode, type, acl);
  192. return error;
  193. }
  194. /*
  195. * inode->i_mutex: down
  196. */
  197. int
  198. ext2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  199. {
  200. int error;
  201. int update_mode = 0;
  202. umode_t mode = inode->i_mode;
  203. if (type == ACL_TYPE_ACCESS && acl) {
  204. error = posix_acl_update_mode(inode, &mode, &acl);
  205. if (error)
  206. return error;
  207. update_mode = 1;
  208. }
  209. error = __ext2_set_acl(inode, acl, type);
  210. if (!error && update_mode) {
  211. inode->i_mode = mode;
  212. inode->i_ctime = current_time(inode);
  213. mark_inode_dirty(inode);
  214. }
  215. return error;
  216. }
  217. /*
  218. * Initialize the ACLs of a new inode. Called from ext2_new_inode.
  219. *
  220. * dir->i_mutex: down
  221. * inode->i_mutex: up (access to inode is still exclusive)
  222. */
  223. int
  224. ext2_init_acl(struct inode *inode, struct inode *dir)
  225. {
  226. struct posix_acl *default_acl, *acl;
  227. int error;
  228. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  229. if (error)
  230. return error;
  231. if (default_acl) {
  232. error = __ext2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  233. posix_acl_release(default_acl);
  234. }
  235. if (acl) {
  236. if (!error)
  237. error = __ext2_set_acl(inode, acl, ACL_TYPE_ACCESS);
  238. posix_acl_release(acl);
  239. }
  240. return error;
  241. }