xattr_acl.c 9.7 KB

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