acl.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/acl.c
  4. *
  5. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  6. */
  7. #include <linux/quotaops.h>
  8. #include "ext4_jbd2.h"
  9. #include "ext4.h"
  10. #include "xattr.h"
  11. #include "acl.h"
  12. /*
  13. * Convert from filesystem to in-memory representation.
  14. */
  15. static struct posix_acl *
  16. ext4_acl_from_disk(const void *value, size_t size)
  17. {
  18. const char *end = (char *)value + size;
  19. int n, count;
  20. struct posix_acl *acl;
  21. if (!value)
  22. return NULL;
  23. if (size < sizeof(ext4_acl_header))
  24. return ERR_PTR(-EINVAL);
  25. if (((ext4_acl_header *)value)->a_version !=
  26. cpu_to_le32(EXT4_ACL_VERSION))
  27. return ERR_PTR(-EINVAL);
  28. value = (char *)value + sizeof(ext4_acl_header);
  29. count = ext4_acl_count(size);
  30. if (count < 0)
  31. return ERR_PTR(-EINVAL);
  32. if (count == 0)
  33. return NULL;
  34. acl = posix_acl_alloc(count, GFP_NOFS);
  35. if (!acl)
  36. return ERR_PTR(-ENOMEM);
  37. for (n = 0; n < count; n++) {
  38. ext4_acl_entry *entry =
  39. (ext4_acl_entry *)value;
  40. if ((char *)value + sizeof(ext4_acl_entry_short) > end)
  41. goto fail;
  42. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  43. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  44. switch (acl->a_entries[n].e_tag) {
  45. case ACL_USER_OBJ:
  46. case ACL_GROUP_OBJ:
  47. case ACL_MASK:
  48. case ACL_OTHER:
  49. value = (char *)value +
  50. sizeof(ext4_acl_entry_short);
  51. break;
  52. case ACL_USER:
  53. value = (char *)value + sizeof(ext4_acl_entry);
  54. if ((char *)value > end)
  55. goto fail;
  56. acl->a_entries[n].e_uid =
  57. make_kuid(&init_user_ns,
  58. le32_to_cpu(entry->e_id));
  59. break;
  60. case ACL_GROUP:
  61. value = (char *)value + sizeof(ext4_acl_entry);
  62. if ((char *)value > end)
  63. goto fail;
  64. acl->a_entries[n].e_gid =
  65. make_kgid(&init_user_ns,
  66. le32_to_cpu(entry->e_id));
  67. break;
  68. default:
  69. goto fail;
  70. }
  71. }
  72. if (value != end)
  73. goto fail;
  74. return acl;
  75. fail:
  76. posix_acl_release(acl);
  77. return ERR_PTR(-EINVAL);
  78. }
  79. /*
  80. * Convert from in-memory to filesystem representation.
  81. */
  82. static void *
  83. ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
  84. {
  85. ext4_acl_header *ext_acl;
  86. char *e;
  87. size_t n;
  88. *size = ext4_acl_size(acl->a_count);
  89. ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
  90. sizeof(ext4_acl_entry), GFP_NOFS);
  91. if (!ext_acl)
  92. return ERR_PTR(-ENOMEM);
  93. ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
  94. e = (char *)ext_acl + sizeof(ext4_acl_header);
  95. for (n = 0; n < acl->a_count; n++) {
  96. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  97. ext4_acl_entry *entry = (ext4_acl_entry *)e;
  98. entry->e_tag = cpu_to_le16(acl_e->e_tag);
  99. entry->e_perm = cpu_to_le16(acl_e->e_perm);
  100. switch (acl_e->e_tag) {
  101. case ACL_USER:
  102. entry->e_id = cpu_to_le32(
  103. from_kuid(&init_user_ns, acl_e->e_uid));
  104. e += sizeof(ext4_acl_entry);
  105. break;
  106. case ACL_GROUP:
  107. entry->e_id = cpu_to_le32(
  108. from_kgid(&init_user_ns, acl_e->e_gid));
  109. e += sizeof(ext4_acl_entry);
  110. break;
  111. case ACL_USER_OBJ:
  112. case ACL_GROUP_OBJ:
  113. case ACL_MASK:
  114. case ACL_OTHER:
  115. e += sizeof(ext4_acl_entry_short);
  116. break;
  117. default:
  118. goto fail;
  119. }
  120. }
  121. return (char *)ext_acl;
  122. fail:
  123. kfree(ext_acl);
  124. return ERR_PTR(-EINVAL);
  125. }
  126. /*
  127. * Inode operation get_posix_acl().
  128. *
  129. * inode->i_mutex: don't care
  130. */
  131. struct posix_acl *
  132. ext4_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 = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  141. break;
  142. case ACL_TYPE_DEFAULT:
  143. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  144. break;
  145. default:
  146. BUG();
  147. }
  148. retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
  149. if (retval > 0) {
  150. value = kmalloc(retval, GFP_NOFS);
  151. if (!value)
  152. return ERR_PTR(-ENOMEM);
  153. retval = ext4_xattr_get(inode, name_index, "", value, retval);
  154. }
  155. if (retval > 0)
  156. acl = ext4_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. /*
  165. * Set the access or default ACL of an inode.
  166. *
  167. * inode->i_mutex: down unless called from ext4_new_inode
  168. */
  169. static int
  170. __ext4_set_acl(handle_t *handle, struct inode *inode, int type,
  171. struct posix_acl *acl, int xattr_flags)
  172. {
  173. int name_index;
  174. void *value = NULL;
  175. size_t size = 0;
  176. int error;
  177. switch (type) {
  178. case ACL_TYPE_ACCESS:
  179. name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  180. break;
  181. case ACL_TYPE_DEFAULT:
  182. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  183. if (!S_ISDIR(inode->i_mode))
  184. return acl ? -EACCES : 0;
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. if (acl) {
  190. value = ext4_acl_to_disk(acl, &size);
  191. if (IS_ERR(value))
  192. return (int)PTR_ERR(value);
  193. }
  194. error = ext4_xattr_set_handle(handle, inode, name_index, "",
  195. value, size, xattr_flags);
  196. kfree(value);
  197. if (!error) {
  198. set_cached_acl(inode, type, acl);
  199. }
  200. return error;
  201. }
  202. int
  203. ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  204. {
  205. handle_t *handle;
  206. int error, credits, retries = 0;
  207. size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
  208. umode_t mode = inode->i_mode;
  209. int update_mode = 0;
  210. error = dquot_initialize(inode);
  211. if (error)
  212. return error;
  213. retry:
  214. error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
  215. &credits);
  216. if (error)
  217. return error;
  218. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  219. if (IS_ERR(handle))
  220. return PTR_ERR(handle);
  221. if ((type == ACL_TYPE_ACCESS) && acl) {
  222. error = posix_acl_update_mode(inode, &mode, &acl);
  223. if (error)
  224. goto out_stop;
  225. update_mode = 1;
  226. }
  227. error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
  228. if (!error && update_mode) {
  229. inode->i_mode = mode;
  230. inode->i_ctime = current_time(inode);
  231. ext4_mark_inode_dirty(handle, inode);
  232. }
  233. out_stop:
  234. ext4_journal_stop(handle);
  235. if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  236. goto retry;
  237. return error;
  238. }
  239. /*
  240. * Initialize the ACLs of a new inode. Called from ext4_new_inode.
  241. *
  242. * dir->i_mutex: down
  243. * inode->i_mutex: up (access to inode is still exclusive)
  244. */
  245. int
  246. ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  247. {
  248. struct posix_acl *default_acl, *acl;
  249. int error;
  250. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  251. if (error)
  252. return error;
  253. if (default_acl) {
  254. error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
  255. default_acl, XATTR_CREATE);
  256. posix_acl_release(default_acl);
  257. }
  258. if (acl) {
  259. if (!error)
  260. error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
  261. acl, XATTR_CREATE);
  262. posix_acl_release(acl);
  263. }
  264. return error;
  265. }