xattr_acl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/capability.h>
  3. #include <linux/fs.h>
  4. #include <linux/posix_acl.h>
  5. #include "reiserfs.h"
  6. #include <linux/errno.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/xattr.h>
  9. #include <linux/slab.h>
  10. #include <linux/posix_acl_xattr.h>
  11. #include "xattr.h"
  12. #include "acl.h"
  13. #include <linux/uaccess.h>
  14. static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th,
  15. struct inode *inode, int type,
  16. struct posix_acl *acl);
  17. int
  18. reiserfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  19. {
  20. int error, error2;
  21. struct reiserfs_transaction_handle th;
  22. size_t jcreate_blocks;
  23. int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
  24. int update_mode = 0;
  25. umode_t mode = inode->i_mode;
  26. /*
  27. * Pessimism: We can't assume that anything from the xattr root up
  28. * has been created.
  29. */
  30. jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
  31. reiserfs_xattr_nblocks(inode, size) * 2;
  32. reiserfs_write_lock(inode->i_sb);
  33. error = journal_begin(&th, inode->i_sb, jcreate_blocks);
  34. reiserfs_write_unlock(inode->i_sb);
  35. if (error == 0) {
  36. if (type == ACL_TYPE_ACCESS && acl) {
  37. error = posix_acl_update_mode(inode, &mode, &acl);
  38. if (error)
  39. goto unlock;
  40. update_mode = 1;
  41. }
  42. error = __reiserfs_set_acl(&th, inode, type, acl);
  43. if (!error && update_mode)
  44. inode->i_mode = mode;
  45. unlock:
  46. reiserfs_write_lock(inode->i_sb);
  47. error2 = journal_end(&th);
  48. reiserfs_write_unlock(inode->i_sb);
  49. if (error2)
  50. error = error2;
  51. }
  52. return error;
  53. }
  54. /*
  55. * Convert from filesystem to in-memory representation.
  56. */
  57. static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
  58. {
  59. const char *end = (char *)value + size;
  60. int n, count;
  61. struct posix_acl *acl;
  62. if (!value)
  63. return NULL;
  64. if (size < sizeof(reiserfs_acl_header))
  65. return ERR_PTR(-EINVAL);
  66. if (((reiserfs_acl_header *) value)->a_version !=
  67. cpu_to_le32(REISERFS_ACL_VERSION))
  68. return ERR_PTR(-EINVAL);
  69. value = (char *)value + sizeof(reiserfs_acl_header);
  70. count = reiserfs_acl_count(size);
  71. if (count < 0)
  72. return ERR_PTR(-EINVAL);
  73. if (count == 0)
  74. return NULL;
  75. acl = posix_acl_alloc(count, GFP_NOFS);
  76. if (!acl)
  77. return ERR_PTR(-ENOMEM);
  78. for (n = 0; n < count; n++) {
  79. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
  80. if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
  81. goto fail;
  82. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  83. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  84. switch (acl->a_entries[n].e_tag) {
  85. case ACL_USER_OBJ:
  86. case ACL_GROUP_OBJ:
  87. case ACL_MASK:
  88. case ACL_OTHER:
  89. value = (char *)value +
  90. sizeof(reiserfs_acl_entry_short);
  91. break;
  92. case ACL_USER:
  93. value = (char *)value + sizeof(reiserfs_acl_entry);
  94. if ((char *)value > end)
  95. goto fail;
  96. acl->a_entries[n].e_uid =
  97. make_kuid(&init_user_ns,
  98. le32_to_cpu(entry->e_id));
  99. break;
  100. case ACL_GROUP:
  101. value = (char *)value + sizeof(reiserfs_acl_entry);
  102. if ((char *)value > end)
  103. goto fail;
  104. acl->a_entries[n].e_gid =
  105. make_kgid(&init_user_ns,
  106. le32_to_cpu(entry->e_id));
  107. break;
  108. default:
  109. goto fail;
  110. }
  111. }
  112. if (value != end)
  113. goto fail;
  114. return acl;
  115. fail:
  116. posix_acl_release(acl);
  117. return ERR_PTR(-EINVAL);
  118. }
  119. /*
  120. * Convert from in-memory to filesystem representation.
  121. */
  122. static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
  123. {
  124. reiserfs_acl_header *ext_acl;
  125. char *e;
  126. int n;
  127. *size = reiserfs_acl_size(acl->a_count);
  128. ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
  129. acl->a_count *
  130. sizeof(reiserfs_acl_entry),
  131. GFP_NOFS);
  132. if (!ext_acl)
  133. return ERR_PTR(-ENOMEM);
  134. ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
  135. e = (char *)ext_acl + sizeof(reiserfs_acl_header);
  136. for (n = 0; n < acl->a_count; n++) {
  137. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  138. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
  139. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  140. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  141. switch (acl->a_entries[n].e_tag) {
  142. case ACL_USER:
  143. entry->e_id = cpu_to_le32(
  144. from_kuid(&init_user_ns, acl_e->e_uid));
  145. e += sizeof(reiserfs_acl_entry);
  146. break;
  147. case ACL_GROUP:
  148. entry->e_id = cpu_to_le32(
  149. from_kgid(&init_user_ns, acl_e->e_gid));
  150. e += sizeof(reiserfs_acl_entry);
  151. break;
  152. case ACL_USER_OBJ:
  153. case ACL_GROUP_OBJ:
  154. case ACL_MASK:
  155. case ACL_OTHER:
  156. e += sizeof(reiserfs_acl_entry_short);
  157. break;
  158. default:
  159. goto fail;
  160. }
  161. }
  162. return (char *)ext_acl;
  163. fail:
  164. kfree(ext_acl);
  165. return ERR_PTR(-EINVAL);
  166. }
  167. /*
  168. * Inode operation get_posix_acl().
  169. *
  170. * inode->i_mutex: down
  171. * BKL held [before 2.5.x]
  172. */
  173. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  174. {
  175. char *name, *value;
  176. struct posix_acl *acl;
  177. int size;
  178. int retval;
  179. switch (type) {
  180. case ACL_TYPE_ACCESS:
  181. name = XATTR_NAME_POSIX_ACL_ACCESS;
  182. break;
  183. case ACL_TYPE_DEFAULT:
  184. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  185. break;
  186. default:
  187. BUG();
  188. }
  189. size = reiserfs_xattr_get(inode, name, NULL, 0);
  190. if (size < 0) {
  191. if (size == -ENODATA || size == -ENOSYS)
  192. return NULL;
  193. return ERR_PTR(size);
  194. }
  195. value = kmalloc(size, GFP_NOFS);
  196. if (!value)
  197. return ERR_PTR(-ENOMEM);
  198. retval = reiserfs_xattr_get(inode, name, value, size);
  199. if (retval == -ENODATA || retval == -ENOSYS) {
  200. /*
  201. * This shouldn't actually happen as it should have
  202. * been caught above.. but just in case
  203. */
  204. acl = NULL;
  205. } else if (retval < 0) {
  206. acl = ERR_PTR(retval);
  207. } else {
  208. acl = reiserfs_posix_acl_from_disk(value, retval);
  209. }
  210. kfree(value);
  211. return acl;
  212. }
  213. /*
  214. * Inode operation set_posix_acl().
  215. *
  216. * inode->i_mutex: down
  217. * BKL held [before 2.5.x]
  218. */
  219. static int
  220. __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
  221. int type, struct posix_acl *acl)
  222. {
  223. char *name;
  224. void *value = NULL;
  225. size_t size = 0;
  226. int error;
  227. switch (type) {
  228. case ACL_TYPE_ACCESS:
  229. name = XATTR_NAME_POSIX_ACL_ACCESS;
  230. break;
  231. case ACL_TYPE_DEFAULT:
  232. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  233. if (!S_ISDIR(inode->i_mode))
  234. return acl ? -EACCES : 0;
  235. break;
  236. default:
  237. return -EINVAL;
  238. }
  239. if (acl) {
  240. value = reiserfs_posix_acl_to_disk(acl, &size);
  241. if (IS_ERR(value))
  242. return (int)PTR_ERR(value);
  243. }
  244. error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
  245. /*
  246. * Ensure that the inode gets dirtied if we're only using
  247. * the mode bits and an old ACL didn't exist. We don't need
  248. * to check if the inode is hashed here since we won't get
  249. * called by reiserfs_inherit_default_acl().
  250. */
  251. if (error == -ENODATA) {
  252. error = 0;
  253. if (type == ACL_TYPE_ACCESS) {
  254. inode->i_ctime = current_time(inode);
  255. mark_inode_dirty(inode);
  256. }
  257. }
  258. kfree(value);
  259. if (!error)
  260. set_cached_acl(inode, type, acl);
  261. return error;
  262. }
  263. /*
  264. * dir->i_mutex: locked,
  265. * inode is new and not released into the wild yet
  266. */
  267. int
  268. reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
  269. struct inode *dir, struct dentry *dentry,
  270. struct inode *inode)
  271. {
  272. struct posix_acl *default_acl, *acl;
  273. int err = 0;
  274. /* ACLs only get applied to files and directories */
  275. if (S_ISLNK(inode->i_mode))
  276. return 0;
  277. /*
  278. * ACLs can only be used on "new" objects, so if it's an old object
  279. * there is nothing to inherit from
  280. */
  281. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  282. goto apply_umask;
  283. /*
  284. * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  285. * would be useless since permissions are ignored, and a pain because
  286. * it introduces locking cycles
  287. */
  288. if (IS_PRIVATE(inode))
  289. goto apply_umask;
  290. err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  291. if (err)
  292. return err;
  293. if (default_acl) {
  294. err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
  295. default_acl);
  296. posix_acl_release(default_acl);
  297. }
  298. if (acl) {
  299. if (!err)
  300. err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
  301. acl);
  302. posix_acl_release(acl);
  303. }
  304. return err;
  305. apply_umask:
  306. /* no ACL, apply umask */
  307. inode->i_mode &= ~current_umask();
  308. return err;
  309. }
  310. /* This is used to cache the default acl before a new object is created.
  311. * The biggest reason for this is to get an idea of how many blocks will
  312. * actually be required for the create operation if we must inherit an ACL.
  313. * An ACL write can add up to 3 object creations and an additional file write
  314. * so we'd prefer not to reserve that many blocks in the journal if we can.
  315. * It also has the advantage of not loading the ACL with a transaction open,
  316. * this may seem silly, but if the owner of the directory is doing the
  317. * creation, the ACL may not be loaded since the permissions wouldn't require
  318. * it.
  319. * We return the number of blocks required for the transaction.
  320. */
  321. int reiserfs_cache_default_acl(struct inode *inode)
  322. {
  323. struct posix_acl *acl;
  324. int nblocks = 0;
  325. if (IS_PRIVATE(inode))
  326. return 0;
  327. acl = get_acl(inode, ACL_TYPE_DEFAULT);
  328. if (acl && !IS_ERR(acl)) {
  329. int size = reiserfs_acl_size(acl->a_count);
  330. /* Other xattrs can be created during inode creation. We don't
  331. * want to claim too many blocks, so we check to see if we
  332. * we need to create the tree to the xattrs, and then we
  333. * just want two files. */
  334. nblocks = reiserfs_xattr_jcreate_nblocks(inode);
  335. nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
  336. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  337. /* We need to account for writes + bitmaps for two files */
  338. nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
  339. posix_acl_release(acl);
  340. }
  341. return nblocks;
  342. }
  343. /*
  344. * Called under i_mutex
  345. */
  346. int reiserfs_acl_chmod(struct inode *inode)
  347. {
  348. if (IS_PRIVATE(inode))
  349. return 0;
  350. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  351. !reiserfs_posixacl(inode->i_sb))
  352. return 0;
  353. return posix_acl_chmod(inode, inode->i_mode);
  354. }