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