xattr_acl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #include <linux/capability.h>
  2. #include <linux/fs.h>
  3. #include <linux/posix_acl.h>
  4. #include <linux/reiserfs_fs.h>
  5. #include <linux/errno.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/xattr.h>
  8. #include <linux/posix_acl_xattr.h>
  9. #include <linux/reiserfs_xattr.h>
  10. #include <linux/reiserfs_acl.h>
  11. #include <asm/uaccess.h>
  12. static int reiserfs_set_acl(struct inode *inode, int type,
  13. struct posix_acl *acl);
  14. static int
  15. xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
  16. {
  17. struct posix_acl *acl;
  18. int error;
  19. if (!reiserfs_posixacl(inode->i_sb))
  20. return -EOPNOTSUPP;
  21. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  22. return -EPERM;
  23. if (value) {
  24. acl = posix_acl_from_xattr(value, size);
  25. if (IS_ERR(acl)) {
  26. return PTR_ERR(acl);
  27. } else if (acl) {
  28. error = posix_acl_valid(acl);
  29. if (error)
  30. goto release_and_out;
  31. }
  32. } else
  33. acl = NULL;
  34. error = reiserfs_set_acl(inode, type, acl);
  35. release_and_out:
  36. posix_acl_release(acl);
  37. return error;
  38. }
  39. static int
  40. xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
  41. {
  42. struct posix_acl *acl;
  43. int error;
  44. if (!reiserfs_posixacl(inode->i_sb))
  45. return -EOPNOTSUPP;
  46. acl = reiserfs_get_acl(inode, type);
  47. if (IS_ERR(acl))
  48. return PTR_ERR(acl);
  49. if (acl == NULL)
  50. return -ENODATA;
  51. error = posix_acl_to_xattr(acl, buffer, size);
  52. posix_acl_release(acl);
  53. return error;
  54. }
  55. /*
  56. * Convert from filesystem to in-memory representation.
  57. */
  58. static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
  59. {
  60. const char *end = (char *)value + size;
  61. int n, count;
  62. struct posix_acl *acl;
  63. if (!value)
  64. return NULL;
  65. if (size < sizeof(reiserfs_acl_header))
  66. return ERR_PTR(-EINVAL);
  67. if (((reiserfs_acl_header *) value)->a_version !=
  68. cpu_to_le32(REISERFS_ACL_VERSION))
  69. return ERR_PTR(-EINVAL);
  70. value = (char *)value + sizeof(reiserfs_acl_header);
  71. count = reiserfs_acl_count(size);
  72. if (count < 0)
  73. return ERR_PTR(-EINVAL);
  74. if (count == 0)
  75. return NULL;
  76. acl = posix_acl_alloc(count, GFP_NOFS);
  77. if (!acl)
  78. return ERR_PTR(-ENOMEM);
  79. for (n = 0; n < count; n++) {
  80. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
  81. if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
  82. goto fail;
  83. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  84. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  85. switch (acl->a_entries[n].e_tag) {
  86. case ACL_USER_OBJ:
  87. case ACL_GROUP_OBJ:
  88. case ACL_MASK:
  89. case ACL_OTHER:
  90. value = (char *)value +
  91. sizeof(reiserfs_acl_entry_short);
  92. acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
  93. break;
  94. case ACL_USER:
  95. case ACL_GROUP:
  96. value = (char *)value + sizeof(reiserfs_acl_entry);
  97. if ((char *)value > end)
  98. goto fail;
  99. acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
  100. break;
  101. default:
  102. goto fail;
  103. }
  104. }
  105. if (value != end)
  106. goto fail;
  107. return acl;
  108. fail:
  109. posix_acl_release(acl);
  110. return ERR_PTR(-EINVAL);
  111. }
  112. /*
  113. * Convert from in-memory to filesystem representation.
  114. */
  115. static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
  116. {
  117. reiserfs_acl_header *ext_acl;
  118. char *e;
  119. int n;
  120. *size = reiserfs_acl_size(acl->a_count);
  121. ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
  122. acl->a_count *
  123. sizeof(reiserfs_acl_entry),
  124. GFP_NOFS);
  125. if (!ext_acl)
  126. return ERR_PTR(-ENOMEM);
  127. ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
  128. e = (char *)ext_acl + sizeof(reiserfs_acl_header);
  129. for (n = 0; n < acl->a_count; n++) {
  130. reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
  131. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  132. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  133. switch (acl->a_entries[n].e_tag) {
  134. case ACL_USER:
  135. case ACL_GROUP:
  136. entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  137. e += sizeof(reiserfs_acl_entry);
  138. break;
  139. case ACL_USER_OBJ:
  140. case ACL_GROUP_OBJ:
  141. case ACL_MASK:
  142. case ACL_OTHER:
  143. e += sizeof(reiserfs_acl_entry_short);
  144. break;
  145. default:
  146. goto fail;
  147. }
  148. }
  149. return (char *)ext_acl;
  150. fail:
  151. kfree(ext_acl);
  152. return ERR_PTR(-EINVAL);
  153. }
  154. /*
  155. * Inode operation get_posix_acl().
  156. *
  157. * inode->i_mutex: down
  158. * BKL held [before 2.5.x]
  159. */
  160. struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
  161. {
  162. char *name, *value;
  163. struct posix_acl *acl, **p_acl;
  164. int size;
  165. int retval;
  166. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  167. switch (type) {
  168. case ACL_TYPE_ACCESS:
  169. name = POSIX_ACL_XATTR_ACCESS;
  170. p_acl = &reiserfs_i->i_acl_access;
  171. break;
  172. case ACL_TYPE_DEFAULT:
  173. name = POSIX_ACL_XATTR_DEFAULT;
  174. p_acl = &reiserfs_i->i_acl_default;
  175. break;
  176. default:
  177. return ERR_PTR(-EINVAL);
  178. }
  179. if (IS_ERR(*p_acl)) {
  180. if (PTR_ERR(*p_acl) == -ENODATA)
  181. return NULL;
  182. } else if (*p_acl != NULL)
  183. return posix_acl_dup(*p_acl);
  184. size = reiserfs_xattr_get(inode, name, NULL, 0);
  185. if (size < 0) {
  186. if (size == -ENODATA || size == -ENOSYS) {
  187. *p_acl = ERR_PTR(-ENODATA);
  188. return NULL;
  189. }
  190. return ERR_PTR(size);
  191. }
  192. value = kmalloc(size, GFP_NOFS);
  193. if (!value)
  194. return ERR_PTR(-ENOMEM);
  195. retval = reiserfs_xattr_get(inode, name, value, size);
  196. if (retval == -ENODATA || retval == -ENOSYS) {
  197. /* This shouldn't actually happen as it should have
  198. been caught above.. but just in case */
  199. acl = NULL;
  200. *p_acl = ERR_PTR(-ENODATA);
  201. } else if (retval < 0) {
  202. acl = ERR_PTR(retval);
  203. } else {
  204. acl = posix_acl_from_disk(value, retval);
  205. if (!IS_ERR(acl))
  206. *p_acl = posix_acl_dup(acl);
  207. }
  208. kfree(value);
  209. return acl;
  210. }
  211. /*
  212. * Inode operation set_posix_acl().
  213. *
  214. * inode->i_mutex: down
  215. * BKL held [before 2.5.x]
  216. */
  217. static int
  218. reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  219. {
  220. char *name;
  221. void *value = NULL;
  222. struct posix_acl **p_acl;
  223. size_t size;
  224. int error;
  225. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  226. if (S_ISLNK(inode->i_mode))
  227. return -EOPNOTSUPP;
  228. switch (type) {
  229. case ACL_TYPE_ACCESS:
  230. name = POSIX_ACL_XATTR_ACCESS;
  231. p_acl = &reiserfs_i->i_acl_access;
  232. if (acl) {
  233. mode_t mode = inode->i_mode;
  234. error = posix_acl_equiv_mode(acl, &mode);
  235. if (error < 0)
  236. return error;
  237. else {
  238. inode->i_mode = mode;
  239. if (error == 0)
  240. acl = NULL;
  241. }
  242. }
  243. break;
  244. case ACL_TYPE_DEFAULT:
  245. name = POSIX_ACL_XATTR_DEFAULT;
  246. p_acl = &reiserfs_i->i_acl_default;
  247. if (!S_ISDIR(inode->i_mode))
  248. return acl ? -EACCES : 0;
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. if (acl) {
  254. value = posix_acl_to_disk(acl, &size);
  255. if (IS_ERR(value))
  256. return (int)PTR_ERR(value);
  257. error = reiserfs_xattr_set(inode, name, value, size, 0);
  258. } else {
  259. error = reiserfs_xattr_del(inode, name);
  260. if (error == -ENODATA) {
  261. /* This may seem odd here, but it means that the ACL was set
  262. * with a value representable with mode bits. If there was
  263. * an ACL before, reiserfs_xattr_del already dirtied the inode.
  264. */
  265. mark_inode_dirty(inode);
  266. error = 0;
  267. }
  268. }
  269. kfree(value);
  270. if (!error) {
  271. /* Release the old one */
  272. if (!IS_ERR(*p_acl) && *p_acl)
  273. posix_acl_release(*p_acl);
  274. if (acl == NULL)
  275. *p_acl = ERR_PTR(-ENODATA);
  276. else
  277. *p_acl = posix_acl_dup(acl);
  278. }
  279. return error;
  280. }
  281. /* dir->i_mutex: locked,
  282. * inode is new and not released into the wild yet */
  283. int
  284. reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
  285. struct inode *inode)
  286. {
  287. struct posix_acl *acl;
  288. int err = 0;
  289. /* ACLs only get applied to files and directories */
  290. if (S_ISLNK(inode->i_mode))
  291. return 0;
  292. /* ACLs can only be used on "new" objects, so if it's an old object
  293. * there is nothing to inherit from */
  294. if (get_inode_sd_version(dir) == STAT_DATA_V1)
  295. goto apply_umask;
  296. /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
  297. * would be useless since permissions are ignored, and a pain because
  298. * it introduces locking cycles */
  299. if (is_reiserfs_priv_object(dir)) {
  300. reiserfs_mark_inode_private(inode);
  301. goto apply_umask;
  302. }
  303. acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
  304. if (IS_ERR(acl)) {
  305. if (PTR_ERR(acl) == -ENODATA)
  306. goto apply_umask;
  307. return PTR_ERR(acl);
  308. }
  309. if (acl) {
  310. struct posix_acl *acl_copy;
  311. mode_t mode = inode->i_mode;
  312. int need_acl;
  313. /* Copy the default ACL to the default ACL of a new directory */
  314. if (S_ISDIR(inode->i_mode)) {
  315. err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  316. if (err)
  317. goto cleanup;
  318. }
  319. /* Now we reconcile the new ACL and the mode,
  320. potentially modifying both */
  321. acl_copy = posix_acl_clone(acl, GFP_NOFS);
  322. if (!acl_copy) {
  323. err = -ENOMEM;
  324. goto cleanup;
  325. }
  326. need_acl = posix_acl_create_masq(acl_copy, &mode);
  327. if (need_acl >= 0) {
  328. if (mode != inode->i_mode) {
  329. inode->i_mode = mode;
  330. }
  331. /* If we need an ACL.. */
  332. if (need_acl > 0) {
  333. err =
  334. reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
  335. acl_copy);
  336. if (err)
  337. goto cleanup_copy;
  338. }
  339. }
  340. cleanup_copy:
  341. posix_acl_release(acl_copy);
  342. cleanup:
  343. posix_acl_release(acl);
  344. } else {
  345. apply_umask:
  346. /* no ACL, apply umask */
  347. inode->i_mode &= ~current->fs->umask;
  348. }
  349. return err;
  350. }
  351. /* Looks up and caches the result of the default ACL.
  352. * We do this so that we don't need to carry the xattr_sem into
  353. * reiserfs_new_inode if we don't need to */
  354. int reiserfs_cache_default_acl(struct inode *inode)
  355. {
  356. int ret = 0;
  357. if (reiserfs_posixacl(inode->i_sb) && !is_reiserfs_priv_object(inode)) {
  358. struct posix_acl *acl;
  359. reiserfs_read_lock_xattr_i(inode);
  360. reiserfs_read_lock_xattrs(inode->i_sb);
  361. acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
  362. reiserfs_read_unlock_xattrs(inode->i_sb);
  363. reiserfs_read_unlock_xattr_i(inode);
  364. ret = (acl && !IS_ERR(acl));
  365. if (ret)
  366. posix_acl_release(acl);
  367. }
  368. return ret;
  369. }
  370. int reiserfs_acl_chmod(struct inode *inode)
  371. {
  372. struct posix_acl *acl, *clone;
  373. int error;
  374. if (S_ISLNK(inode->i_mode))
  375. return -EOPNOTSUPP;
  376. if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
  377. !reiserfs_posixacl(inode->i_sb)) {
  378. return 0;
  379. }
  380. reiserfs_read_lock_xattrs(inode->i_sb);
  381. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  382. reiserfs_read_unlock_xattrs(inode->i_sb);
  383. if (!acl)
  384. return 0;
  385. if (IS_ERR(acl))
  386. return PTR_ERR(acl);
  387. clone = posix_acl_clone(acl, GFP_NOFS);
  388. posix_acl_release(acl);
  389. if (!clone)
  390. return -ENOMEM;
  391. error = posix_acl_chmod_masq(clone, inode->i_mode);
  392. if (!error) {
  393. int lock = !has_xattr_dir(inode);
  394. reiserfs_write_lock_xattr_i(inode);
  395. if (lock)
  396. reiserfs_write_lock_xattrs(inode->i_sb);
  397. else
  398. reiserfs_read_lock_xattrs(inode->i_sb);
  399. error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  400. if (lock)
  401. reiserfs_write_unlock_xattrs(inode->i_sb);
  402. else
  403. reiserfs_read_unlock_xattrs(inode->i_sb);
  404. reiserfs_write_unlock_xattr_i(inode);
  405. }
  406. posix_acl_release(clone);
  407. return error;
  408. }
  409. static int
  410. posix_acl_access_get(struct inode *inode, const char *name,
  411. void *buffer, size_t size)
  412. {
  413. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  414. return -EINVAL;
  415. return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  416. }
  417. static int
  418. posix_acl_access_set(struct inode *inode, const char *name,
  419. const void *value, size_t size, int flags)
  420. {
  421. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  422. return -EINVAL;
  423. return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  424. }
  425. static int posix_acl_access_del(struct inode *inode, const char *name)
  426. {
  427. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  428. struct posix_acl **acl = &reiserfs_i->i_acl_access;
  429. if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
  430. return -EINVAL;
  431. if (!IS_ERR(*acl) && *acl) {
  432. posix_acl_release(*acl);
  433. *acl = ERR_PTR(-ENODATA);
  434. }
  435. return 0;
  436. }
  437. static int
  438. posix_acl_access_list(struct inode *inode, const char *name, int namelen,
  439. char *out)
  440. {
  441. int len = namelen;
  442. if (!reiserfs_posixacl(inode->i_sb))
  443. return 0;
  444. if (out)
  445. memcpy(out, name, len);
  446. return len;
  447. }
  448. struct reiserfs_xattr_handler posix_acl_access_handler = {
  449. .prefix = POSIX_ACL_XATTR_ACCESS,
  450. .get = posix_acl_access_get,
  451. .set = posix_acl_access_set,
  452. .del = posix_acl_access_del,
  453. .list = posix_acl_access_list,
  454. };
  455. static int
  456. posix_acl_default_get(struct inode *inode, const char *name,
  457. void *buffer, size_t size)
  458. {
  459. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  460. return -EINVAL;
  461. return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  462. }
  463. static int
  464. posix_acl_default_set(struct inode *inode, const char *name,
  465. const void *value, size_t size, int flags)
  466. {
  467. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  468. return -EINVAL;
  469. return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  470. }
  471. static int posix_acl_default_del(struct inode *inode, const char *name)
  472. {
  473. struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
  474. struct posix_acl **acl = &reiserfs_i->i_acl_default;
  475. if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
  476. return -EINVAL;
  477. if (!IS_ERR(*acl) && *acl) {
  478. posix_acl_release(*acl);
  479. *acl = ERR_PTR(-ENODATA);
  480. }
  481. return 0;
  482. }
  483. static int
  484. posix_acl_default_list(struct inode *inode, const char *name, int namelen,
  485. char *out)
  486. {
  487. int len = namelen;
  488. if (!reiserfs_posixacl(inode->i_sb))
  489. return 0;
  490. if (out)
  491. memcpy(out, name, len);
  492. return len;
  493. }
  494. struct reiserfs_xattr_handler posix_acl_default_handler = {
  495. .prefix = POSIX_ACL_XATTR_DEFAULT,
  496. .get = posix_acl_default_get,
  497. .set = posix_acl_default_set,
  498. .del = posix_acl_default_del,
  499. .list = posix_acl_default_list,
  500. };