acl.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_rwsem: don't care
  130. */
  131. struct posix_acl *
  132. ext4_get_acl(struct inode *inode, int type, bool rcu)
  133. {
  134. int name_index;
  135. char *value = NULL;
  136. struct posix_acl *acl;
  137. int retval;
  138. if (rcu)
  139. return ERR_PTR(-ECHILD);
  140. switch (type) {
  141. case ACL_TYPE_ACCESS:
  142. name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  143. break;
  144. case ACL_TYPE_DEFAULT:
  145. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  146. break;
  147. default:
  148. BUG();
  149. }
  150. retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
  151. if (retval > 0) {
  152. value = kmalloc(retval, GFP_NOFS);
  153. if (!value)
  154. return ERR_PTR(-ENOMEM);
  155. retval = ext4_xattr_get(inode, name_index, "", value, retval);
  156. }
  157. if (retval > 0)
  158. acl = ext4_acl_from_disk(value, retval);
  159. else if (retval == -ENODATA || retval == -ENOSYS)
  160. acl = NULL;
  161. else
  162. acl = ERR_PTR(retval);
  163. kfree(value);
  164. return acl;
  165. }
  166. /*
  167. * Set the access or default ACL of an inode.
  168. *
  169. * inode->i_rwsem: down unless called from ext4_new_inode
  170. */
  171. static int
  172. __ext4_set_acl(handle_t *handle, struct inode *inode, int type,
  173. struct posix_acl *acl, int xattr_flags)
  174. {
  175. int name_index;
  176. void *value = NULL;
  177. size_t size = 0;
  178. int error;
  179. switch (type) {
  180. case ACL_TYPE_ACCESS:
  181. name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  182. break;
  183. case ACL_TYPE_DEFAULT:
  184. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  185. if (!S_ISDIR(inode->i_mode))
  186. return acl ? -EACCES : 0;
  187. break;
  188. default:
  189. return -EINVAL;
  190. }
  191. if (acl) {
  192. value = ext4_acl_to_disk(acl, &size);
  193. if (IS_ERR(value))
  194. return (int)PTR_ERR(value);
  195. }
  196. error = ext4_xattr_set_handle(handle, inode, name_index, "",
  197. value, size, xattr_flags);
  198. kfree(value);
  199. if (!error)
  200. set_cached_acl(inode, type, acl);
  201. return error;
  202. }
  203. int
  204. ext4_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  205. struct posix_acl *acl, int type)
  206. {
  207. handle_t *handle;
  208. int error, credits, retries = 0;
  209. size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
  210. struct inode *inode = d_inode(dentry);
  211. umode_t mode = inode->i_mode;
  212. int update_mode = 0;
  213. error = dquot_initialize(inode);
  214. if (error)
  215. return error;
  216. retry:
  217. error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
  218. &credits);
  219. if (error)
  220. return error;
  221. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  222. if (IS_ERR(handle))
  223. return PTR_ERR(handle);
  224. if ((type == ACL_TYPE_ACCESS) && acl) {
  225. error = posix_acl_update_mode(idmap, inode, &mode, &acl);
  226. if (error)
  227. goto out_stop;
  228. if (mode != inode->i_mode)
  229. update_mode = 1;
  230. }
  231. error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
  232. if (!error && update_mode) {
  233. inode->i_mode = mode;
  234. inode_set_ctime_current(inode);
  235. error = ext4_mark_inode_dirty(handle, inode);
  236. }
  237. out_stop:
  238. ext4_journal_stop(handle);
  239. if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  240. goto retry;
  241. return error;
  242. }
  243. /*
  244. * Initialize the ACLs of a new inode. Called from ext4_new_inode.
  245. *
  246. * dir->i_rwsem: down
  247. * inode->i_rwsem: up (access to inode is still exclusive)
  248. */
  249. int
  250. ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  251. {
  252. struct posix_acl *default_acl, *acl;
  253. int error;
  254. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  255. if (error)
  256. return error;
  257. if (default_acl) {
  258. error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
  259. default_acl, XATTR_CREATE);
  260. posix_acl_release(default_acl);
  261. } else {
  262. inode->i_default_acl = NULL;
  263. }
  264. if (acl) {
  265. if (!error)
  266. error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
  267. acl, XATTR_CREATE);
  268. posix_acl_release(acl);
  269. } else {
  270. inode->i_acl = NULL;
  271. }
  272. return error;
  273. }