acl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/acl.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. *
  8. * Portions of this code from linux/fs/ext2/acl.c
  9. *
  10. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  11. */
  12. #include <linux/fs_struct.h>
  13. #include <linux/f2fs_fs.h>
  14. #include "f2fs.h"
  15. #include "xattr.h"
  16. #include "acl.h"
  17. static inline size_t f2fs_acl_size(int count)
  18. {
  19. if (count <= 4) {
  20. return sizeof(struct f2fs_acl_header) +
  21. count * sizeof(struct f2fs_acl_entry_short);
  22. } else {
  23. return sizeof(struct f2fs_acl_header) +
  24. 4 * sizeof(struct f2fs_acl_entry_short) +
  25. (count - 4) * sizeof(struct f2fs_acl_entry);
  26. }
  27. }
  28. static inline int f2fs_acl_count(size_t size)
  29. {
  30. ssize_t s;
  31. size -= sizeof(struct f2fs_acl_header);
  32. s = size - 4 * sizeof(struct f2fs_acl_entry_short);
  33. if (s < 0) {
  34. if (size % sizeof(struct f2fs_acl_entry_short))
  35. return -1;
  36. return size / sizeof(struct f2fs_acl_entry_short);
  37. } else {
  38. if (s % sizeof(struct f2fs_acl_entry))
  39. return -1;
  40. return s / sizeof(struct f2fs_acl_entry) + 4;
  41. }
  42. }
  43. static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
  44. {
  45. int i, count;
  46. struct posix_acl *acl;
  47. struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
  48. struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
  49. const char *end = value + size;
  50. if (size < sizeof(struct f2fs_acl_header))
  51. return ERR_PTR(-EINVAL);
  52. if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
  53. return ERR_PTR(-EINVAL);
  54. count = f2fs_acl_count(size);
  55. if (count < 0)
  56. return ERR_PTR(-EINVAL);
  57. if (count == 0)
  58. return NULL;
  59. acl = posix_acl_alloc(count, GFP_NOFS);
  60. if (!acl)
  61. return ERR_PTR(-ENOMEM);
  62. for (i = 0; i < count; i++) {
  63. if ((char *)entry > end)
  64. goto fail;
  65. acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag);
  66. acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
  67. switch (acl->a_entries[i].e_tag) {
  68. case ACL_USER_OBJ:
  69. case ACL_GROUP_OBJ:
  70. case ACL_MASK:
  71. case ACL_OTHER:
  72. entry = (struct f2fs_acl_entry *)((char *)entry +
  73. sizeof(struct f2fs_acl_entry_short));
  74. break;
  75. case ACL_USER:
  76. acl->a_entries[i].e_uid =
  77. make_kuid(&init_user_ns,
  78. le32_to_cpu(entry->e_id));
  79. entry = (struct f2fs_acl_entry *)((char *)entry +
  80. sizeof(struct f2fs_acl_entry));
  81. break;
  82. case ACL_GROUP:
  83. acl->a_entries[i].e_gid =
  84. make_kgid(&init_user_ns,
  85. le32_to_cpu(entry->e_id));
  86. entry = (struct f2fs_acl_entry *)((char *)entry +
  87. sizeof(struct f2fs_acl_entry));
  88. break;
  89. default:
  90. goto fail;
  91. }
  92. }
  93. if ((char *)entry != end)
  94. goto fail;
  95. return acl;
  96. fail:
  97. posix_acl_release(acl);
  98. return ERR_PTR(-EINVAL);
  99. }
  100. static void *f2fs_acl_to_disk(struct f2fs_sb_info *sbi,
  101. const struct posix_acl *acl, size_t *size)
  102. {
  103. struct f2fs_acl_header *f2fs_acl;
  104. struct f2fs_acl_entry *entry;
  105. int i;
  106. f2fs_acl = f2fs_kmalloc(sbi, sizeof(struct f2fs_acl_header) +
  107. acl->a_count * sizeof(struct f2fs_acl_entry),
  108. GFP_NOFS);
  109. if (!f2fs_acl)
  110. return ERR_PTR(-ENOMEM);
  111. f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
  112. entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
  113. for (i = 0; i < acl->a_count; i++) {
  114. entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
  115. entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
  116. switch (acl->a_entries[i].e_tag) {
  117. case ACL_USER:
  118. entry->e_id = cpu_to_le32(
  119. from_kuid(&init_user_ns,
  120. acl->a_entries[i].e_uid));
  121. entry = (struct f2fs_acl_entry *)((char *)entry +
  122. sizeof(struct f2fs_acl_entry));
  123. break;
  124. case ACL_GROUP:
  125. entry->e_id = cpu_to_le32(
  126. from_kgid(&init_user_ns,
  127. acl->a_entries[i].e_gid));
  128. entry = (struct f2fs_acl_entry *)((char *)entry +
  129. sizeof(struct f2fs_acl_entry));
  130. break;
  131. case ACL_USER_OBJ:
  132. case ACL_GROUP_OBJ:
  133. case ACL_MASK:
  134. case ACL_OTHER:
  135. entry = (struct f2fs_acl_entry *)((char *)entry +
  136. sizeof(struct f2fs_acl_entry_short));
  137. break;
  138. default:
  139. goto fail;
  140. }
  141. }
  142. *size = f2fs_acl_size(acl->a_count);
  143. return (void *)f2fs_acl;
  144. fail:
  145. kfree(f2fs_acl);
  146. return ERR_PTR(-EINVAL);
  147. }
  148. static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
  149. struct folio *dfolio)
  150. {
  151. int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  152. void *value = NULL;
  153. struct posix_acl *acl;
  154. int retval;
  155. if (type == ACL_TYPE_ACCESS)
  156. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  157. retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dfolio);
  158. if (retval > 0) {
  159. value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
  160. if (!value)
  161. return ERR_PTR(-ENOMEM);
  162. retval = f2fs_getxattr(inode, name_index, "", value,
  163. retval, dfolio);
  164. }
  165. if (retval > 0)
  166. acl = f2fs_acl_from_disk(value, retval);
  167. else if (retval == -ENODATA)
  168. acl = NULL;
  169. else
  170. acl = ERR_PTR(retval);
  171. kfree(value);
  172. return acl;
  173. }
  174. struct posix_acl *f2fs_get_acl(struct inode *inode, int type, bool rcu)
  175. {
  176. if (rcu)
  177. return ERR_PTR(-ECHILD);
  178. return __f2fs_get_acl(inode, type, NULL);
  179. }
  180. static int f2fs_acl_update_mode(struct mnt_idmap *idmap,
  181. struct inode *inode, umode_t *mode_p,
  182. struct posix_acl **acl)
  183. {
  184. umode_t mode = inode->i_mode;
  185. int error;
  186. if (is_inode_flag_set(inode, FI_ACL_MODE))
  187. mode = F2FS_I(inode)->i_acl_mode;
  188. error = posix_acl_equiv_mode(*acl, &mode);
  189. if (error < 0)
  190. return error;
  191. if (error == 0)
  192. *acl = NULL;
  193. if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))
  194. mode &= ~S_ISGID;
  195. *mode_p = mode;
  196. return 0;
  197. }
  198. static int __f2fs_set_acl(struct mnt_idmap *idmap,
  199. struct inode *inode, int type,
  200. struct posix_acl *acl, struct folio *ifolio)
  201. {
  202. int name_index;
  203. void *value = NULL;
  204. size_t size = 0;
  205. int error;
  206. umode_t mode = inode->i_mode;
  207. switch (type) {
  208. case ACL_TYPE_ACCESS:
  209. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  210. if (acl && !ifolio) {
  211. error = f2fs_acl_update_mode(idmap, inode, &mode, &acl);
  212. if (error)
  213. return error;
  214. set_acl_inode(inode, mode);
  215. }
  216. break;
  217. case ACL_TYPE_DEFAULT:
  218. name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  219. if (!S_ISDIR(inode->i_mode))
  220. return acl ? -EACCES : 0;
  221. break;
  222. default:
  223. return -EINVAL;
  224. }
  225. if (acl) {
  226. value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
  227. if (IS_ERR(value)) {
  228. clear_inode_flag(inode, FI_ACL_MODE);
  229. return PTR_ERR(value);
  230. }
  231. }
  232. error = f2fs_setxattr(inode, name_index, "", value, size, ifolio, 0);
  233. kfree(value);
  234. if (!error)
  235. set_cached_acl(inode, type, acl);
  236. clear_inode_flag(inode, FI_ACL_MODE);
  237. return error;
  238. }
  239. int f2fs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  240. struct posix_acl *acl, int type)
  241. {
  242. struct inode *inode = d_inode(dentry);
  243. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  244. return -EIO;
  245. return __f2fs_set_acl(idmap, inode, type, acl, NULL);
  246. }
  247. /*
  248. * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
  249. * are copied from posix_acl.c
  250. */
  251. static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
  252. gfp_t flags)
  253. {
  254. struct posix_acl *clone = NULL;
  255. if (acl) {
  256. clone = kmemdup(acl, struct_size(acl, a_entries, acl->a_count),
  257. flags);
  258. if (clone)
  259. refcount_set(&clone->a_refcount, 1);
  260. }
  261. return clone;
  262. }
  263. static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  264. {
  265. struct posix_acl_entry *pa, *pe;
  266. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  267. umode_t mode = *mode_p;
  268. int not_equiv = 0;
  269. /* assert(atomic_read(acl->a_refcount) == 1); */
  270. FOREACH_ACL_ENTRY(pa, acl, pe) {
  271. switch (pa->e_tag) {
  272. case ACL_USER_OBJ:
  273. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  274. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  275. break;
  276. case ACL_USER:
  277. case ACL_GROUP:
  278. not_equiv = 1;
  279. break;
  280. case ACL_GROUP_OBJ:
  281. group_obj = pa;
  282. break;
  283. case ACL_OTHER:
  284. pa->e_perm &= mode | ~S_IRWXO;
  285. mode &= pa->e_perm | ~S_IRWXO;
  286. break;
  287. case ACL_MASK:
  288. mask_obj = pa;
  289. not_equiv = 1;
  290. break;
  291. default:
  292. return -EIO;
  293. }
  294. }
  295. if (mask_obj) {
  296. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  297. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  298. } else {
  299. if (!group_obj)
  300. return -EIO;
  301. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  302. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  303. }
  304. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  305. return not_equiv;
  306. }
  307. static int f2fs_acl_create(struct inode *dir, umode_t *mode,
  308. struct posix_acl **default_acl, struct posix_acl **acl,
  309. struct folio *dfolio)
  310. {
  311. struct posix_acl *p;
  312. struct posix_acl *clone;
  313. int ret;
  314. *acl = NULL;
  315. *default_acl = NULL;
  316. if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  317. return 0;
  318. p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dfolio);
  319. if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
  320. *mode &= ~current_umask();
  321. return 0;
  322. }
  323. if (IS_ERR(p))
  324. return PTR_ERR(p);
  325. clone = f2fs_acl_clone(p, GFP_NOFS);
  326. if (!clone) {
  327. ret = -ENOMEM;
  328. goto release_acl;
  329. }
  330. ret = f2fs_acl_create_masq(clone, mode);
  331. if (ret < 0)
  332. goto release_clone;
  333. if (ret == 0)
  334. posix_acl_release(clone);
  335. else
  336. *acl = clone;
  337. if (!S_ISDIR(*mode))
  338. posix_acl_release(p);
  339. else
  340. *default_acl = p;
  341. return 0;
  342. release_clone:
  343. posix_acl_release(clone);
  344. release_acl:
  345. posix_acl_release(p);
  346. return ret;
  347. }
  348. int f2fs_init_acl(struct inode *inode, struct inode *dir, struct folio *ifolio,
  349. struct folio *dfolio)
  350. {
  351. struct posix_acl *default_acl = NULL, *acl = NULL;
  352. int error;
  353. error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dfolio);
  354. if (error)
  355. return error;
  356. f2fs_mark_inode_dirty_sync(inode, true);
  357. if (default_acl) {
  358. error = __f2fs_set_acl(NULL, inode, ACL_TYPE_DEFAULT,
  359. default_acl, ifolio);
  360. posix_acl_release(default_acl);
  361. } else {
  362. inode->i_default_acl = NULL;
  363. }
  364. if (acl) {
  365. if (!error)
  366. error = __f2fs_set_acl(NULL, inode, ACL_TYPE_ACCESS,
  367. acl, ifolio);
  368. posix_acl_release(acl);
  369. } else {
  370. inode->i_acl = NULL;
  371. }
  372. return error;
  373. }