xattr_acl.c 9.8 KB

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