acl.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2006 NEC Corporation
  5. *
  6. * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include <linux/sched.h>
  15. #include <linux/time.h>
  16. #include <linux/crc32.h>
  17. #include <linux/jffs2.h>
  18. #include <linux/xattr.h>
  19. #include <linux/posix_acl_xattr.h>
  20. #include <linux/mtd/mtd.h>
  21. #include "nodelist.h"
  22. static size_t jffs2_acl_size(int count)
  23. {
  24. if (count <= 4) {
  25. return sizeof(struct jffs2_acl_header)
  26. + count * sizeof(struct jffs2_acl_entry_short);
  27. } else {
  28. return sizeof(struct jffs2_acl_header)
  29. + 4 * sizeof(struct jffs2_acl_entry_short)
  30. + (count - 4) * sizeof(struct jffs2_acl_entry);
  31. }
  32. }
  33. static int jffs2_acl_count(size_t size)
  34. {
  35. size_t s;
  36. size -= sizeof(struct jffs2_acl_header);
  37. if (size < 4 * sizeof(struct jffs2_acl_entry_short)) {
  38. if (size % sizeof(struct jffs2_acl_entry_short))
  39. return -1;
  40. return size / sizeof(struct jffs2_acl_entry_short);
  41. } else {
  42. s = size - 4 * sizeof(struct jffs2_acl_entry_short);
  43. if (s % sizeof(struct jffs2_acl_entry))
  44. return -1;
  45. return s / sizeof(struct jffs2_acl_entry) + 4;
  46. }
  47. }
  48. static struct posix_acl *jffs2_acl_from_medium(void *value, size_t size)
  49. {
  50. void *end = value + size;
  51. struct jffs2_acl_header *header = value;
  52. struct jffs2_acl_entry *entry;
  53. struct posix_acl *acl;
  54. uint32_t ver;
  55. int i, count;
  56. if (!value)
  57. return NULL;
  58. if (size < sizeof(struct jffs2_acl_header))
  59. return ERR_PTR(-EINVAL);
  60. ver = je32_to_cpu(header->a_version);
  61. if (ver != JFFS2_ACL_VERSION) {
  62. JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver);
  63. return ERR_PTR(-EINVAL);
  64. }
  65. value += sizeof(struct jffs2_acl_header);
  66. count = jffs2_acl_count(size);
  67. if (count < 0)
  68. return ERR_PTR(-EINVAL);
  69. if (count == 0)
  70. return NULL;
  71. acl = posix_acl_alloc(count, GFP_KERNEL);
  72. if (!acl)
  73. return ERR_PTR(-ENOMEM);
  74. for (i=0; i < count; i++) {
  75. entry = value;
  76. if (value + sizeof(struct jffs2_acl_entry_short) > end)
  77. goto fail;
  78. acl->a_entries[i].e_tag = je16_to_cpu(entry->e_tag);
  79. acl->a_entries[i].e_perm = je16_to_cpu(entry->e_perm);
  80. switch (acl->a_entries[i].e_tag) {
  81. case ACL_USER_OBJ:
  82. case ACL_GROUP_OBJ:
  83. case ACL_MASK:
  84. case ACL_OTHER:
  85. value += sizeof(struct jffs2_acl_entry_short);
  86. acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
  87. break;
  88. case ACL_USER:
  89. case ACL_GROUP:
  90. value += sizeof(struct jffs2_acl_entry);
  91. if (value > end)
  92. goto fail;
  93. acl->a_entries[i].e_id = je32_to_cpu(entry->e_id);
  94. break;
  95. default:
  96. goto fail;
  97. }
  98. }
  99. if (value != end)
  100. goto fail;
  101. return acl;
  102. fail:
  103. posix_acl_release(acl);
  104. return ERR_PTR(-EINVAL);
  105. }
  106. static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
  107. {
  108. struct jffs2_acl_header *header;
  109. struct jffs2_acl_entry *entry;
  110. void *e;
  111. size_t i;
  112. *size = jffs2_acl_size(acl->a_count);
  113. header = kmalloc(sizeof(*header) + acl->a_count * sizeof(*entry), GFP_KERNEL);
  114. if (!header)
  115. return ERR_PTR(-ENOMEM);
  116. header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
  117. e = header + 1;
  118. for (i=0; i < acl->a_count; i++) {
  119. entry = e;
  120. entry->e_tag = cpu_to_je16(acl->a_entries[i].e_tag);
  121. entry->e_perm = cpu_to_je16(acl->a_entries[i].e_perm);
  122. switch(acl->a_entries[i].e_tag) {
  123. case ACL_USER:
  124. case ACL_GROUP:
  125. entry->e_id = cpu_to_je32(acl->a_entries[i].e_id);
  126. e += sizeof(struct jffs2_acl_entry);
  127. break;
  128. case ACL_USER_OBJ:
  129. case ACL_GROUP_OBJ:
  130. case ACL_MASK:
  131. case ACL_OTHER:
  132. e += sizeof(struct jffs2_acl_entry_short);
  133. break;
  134. default:
  135. goto fail;
  136. }
  137. }
  138. return header;
  139. fail:
  140. kfree(header);
  141. return ERR_PTR(-EINVAL);
  142. }
  143. static struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
  144. {
  145. struct posix_acl *acl;
  146. char *value = NULL;
  147. int rc, xprefix;
  148. acl = get_cached_acl(inode, type);
  149. if (acl != ACL_NOT_CACHED)
  150. return acl;
  151. switch (type) {
  152. case ACL_TYPE_ACCESS:
  153. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  154. break;
  155. case ACL_TYPE_DEFAULT:
  156. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  157. break;
  158. default:
  159. BUG();
  160. }
  161. rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
  162. if (rc > 0) {
  163. value = kmalloc(rc, GFP_KERNEL);
  164. if (!value)
  165. return ERR_PTR(-ENOMEM);
  166. rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
  167. }
  168. if (rc > 0) {
  169. acl = jffs2_acl_from_medium(value, rc);
  170. } else if (rc == -ENODATA || rc == -ENOSYS) {
  171. acl = NULL;
  172. } else {
  173. acl = ERR_PTR(rc);
  174. }
  175. if (value)
  176. kfree(value);
  177. if (!IS_ERR(acl))
  178. set_cached_acl(inode, type, acl);
  179. return acl;
  180. }
  181. static int __jffs2_set_acl(struct inode *inode, int xprefix, struct posix_acl *acl)
  182. {
  183. char *value = NULL;
  184. size_t size = 0;
  185. int rc;
  186. if (acl) {
  187. value = jffs2_acl_to_medium(acl, &size);
  188. if (IS_ERR(value))
  189. return PTR_ERR(value);
  190. }
  191. rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
  192. if (!value && rc == -ENODATA)
  193. rc = 0;
  194. kfree(value);
  195. return rc;
  196. }
  197. static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  198. {
  199. int rc, xprefix;
  200. if (S_ISLNK(inode->i_mode))
  201. return -EOPNOTSUPP;
  202. switch (type) {
  203. case ACL_TYPE_ACCESS:
  204. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  205. if (acl) {
  206. mode_t mode = inode->i_mode;
  207. rc = posix_acl_equiv_mode(acl, &mode);
  208. if (rc < 0)
  209. return rc;
  210. if (inode->i_mode != mode) {
  211. struct iattr attr;
  212. attr.ia_valid = ATTR_MODE | ATTR_CTIME;
  213. attr.ia_mode = mode;
  214. attr.ia_ctime = CURRENT_TIME_SEC;
  215. rc = jffs2_do_setattr(inode, &attr);
  216. if (rc < 0)
  217. return rc;
  218. }
  219. if (rc == 0)
  220. acl = NULL;
  221. }
  222. break;
  223. case ACL_TYPE_DEFAULT:
  224. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  225. if (!S_ISDIR(inode->i_mode))
  226. return acl ? -EACCES : 0;
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. rc = __jffs2_set_acl(inode, xprefix, acl);
  232. if (!rc)
  233. set_cached_acl(inode, type, acl);
  234. return rc;
  235. }
  236. int jffs2_check_acl(struct inode *inode, int mask, unsigned int flags)
  237. {
  238. struct posix_acl *acl;
  239. int rc;
  240. if (flags & IPERM_FLAG_RCU)
  241. return -ECHILD;
  242. acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
  243. if (IS_ERR(acl))
  244. return PTR_ERR(acl);
  245. if (acl) {
  246. rc = posix_acl_permission(inode, acl, mask);
  247. posix_acl_release(acl);
  248. return rc;
  249. }
  250. return -EAGAIN;
  251. }
  252. int jffs2_init_acl_pre(struct inode *dir_i, struct inode *inode, int *i_mode)
  253. {
  254. struct posix_acl *acl, *clone;
  255. int rc;
  256. cache_no_acl(inode);
  257. if (S_ISLNK(*i_mode))
  258. return 0; /* Symlink always has no-ACL */
  259. acl = jffs2_get_acl(dir_i, ACL_TYPE_DEFAULT);
  260. if (IS_ERR(acl))
  261. return PTR_ERR(acl);
  262. if (!acl) {
  263. *i_mode &= ~current_umask();
  264. } else {
  265. if (S_ISDIR(*i_mode))
  266. set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
  267. clone = posix_acl_clone(acl, GFP_KERNEL);
  268. if (!clone)
  269. return -ENOMEM;
  270. rc = posix_acl_create_masq(clone, (mode_t *)i_mode);
  271. if (rc < 0) {
  272. posix_acl_release(clone);
  273. return rc;
  274. }
  275. if (rc > 0)
  276. set_cached_acl(inode, ACL_TYPE_ACCESS, clone);
  277. posix_acl_release(clone);
  278. }
  279. return 0;
  280. }
  281. int jffs2_init_acl_post(struct inode *inode)
  282. {
  283. int rc;
  284. if (inode->i_default_acl) {
  285. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_DEFAULT, inode->i_default_acl);
  286. if (rc)
  287. return rc;
  288. }
  289. if (inode->i_acl) {
  290. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_ACCESS, inode->i_acl);
  291. if (rc)
  292. return rc;
  293. }
  294. return 0;
  295. }
  296. int jffs2_acl_chmod(struct inode *inode)
  297. {
  298. struct posix_acl *acl, *clone;
  299. int rc;
  300. if (S_ISLNK(inode->i_mode))
  301. return -EOPNOTSUPP;
  302. acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
  303. if (IS_ERR(acl) || !acl)
  304. return PTR_ERR(acl);
  305. clone = posix_acl_clone(acl, GFP_KERNEL);
  306. posix_acl_release(acl);
  307. if (!clone)
  308. return -ENOMEM;
  309. rc = posix_acl_chmod_masq(clone, inode->i_mode);
  310. if (!rc)
  311. rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
  312. posix_acl_release(clone);
  313. return rc;
  314. }
  315. static size_t jffs2_acl_access_listxattr(struct dentry *dentry, char *list,
  316. size_t list_size, const char *name, size_t name_len, int type)
  317. {
  318. const int retlen = sizeof(POSIX_ACL_XATTR_ACCESS);
  319. if (list && retlen <= list_size)
  320. strcpy(list, POSIX_ACL_XATTR_ACCESS);
  321. return retlen;
  322. }
  323. static size_t jffs2_acl_default_listxattr(struct dentry *dentry, char *list,
  324. size_t list_size, const char *name, size_t name_len, int type)
  325. {
  326. const int retlen = sizeof(POSIX_ACL_XATTR_DEFAULT);
  327. if (list && retlen <= list_size)
  328. strcpy(list, POSIX_ACL_XATTR_DEFAULT);
  329. return retlen;
  330. }
  331. static int jffs2_acl_getxattr(struct dentry *dentry, const char *name,
  332. void *buffer, size_t size, int type)
  333. {
  334. struct posix_acl *acl;
  335. int rc;
  336. if (name[0] != '\0')
  337. return -EINVAL;
  338. acl = jffs2_get_acl(dentry->d_inode, type);
  339. if (IS_ERR(acl))
  340. return PTR_ERR(acl);
  341. if (!acl)
  342. return -ENODATA;
  343. rc = posix_acl_to_xattr(acl, buffer, size);
  344. posix_acl_release(acl);
  345. return rc;
  346. }
  347. static int jffs2_acl_setxattr(struct dentry *dentry, const char *name,
  348. const void *value, size_t size, int flags, int type)
  349. {
  350. struct posix_acl *acl;
  351. int rc;
  352. if (name[0] != '\0')
  353. return -EINVAL;
  354. if (!inode_owner_or_capable(dentry->d_inode))
  355. return -EPERM;
  356. if (value) {
  357. acl = posix_acl_from_xattr(value, size);
  358. if (IS_ERR(acl))
  359. return PTR_ERR(acl);
  360. if (acl) {
  361. rc = posix_acl_valid(acl);
  362. if (rc)
  363. goto out;
  364. }
  365. } else {
  366. acl = NULL;
  367. }
  368. rc = jffs2_set_acl(dentry->d_inode, type, acl);
  369. out:
  370. posix_acl_release(acl);
  371. return rc;
  372. }
  373. const struct xattr_handler jffs2_acl_access_xattr_handler = {
  374. .prefix = POSIX_ACL_XATTR_ACCESS,
  375. .flags = ACL_TYPE_DEFAULT,
  376. .list = jffs2_acl_access_listxattr,
  377. .get = jffs2_acl_getxattr,
  378. .set = jffs2_acl_setxattr,
  379. };
  380. const struct xattr_handler jffs2_acl_default_xattr_handler = {
  381. .prefix = POSIX_ACL_XATTR_DEFAULT,
  382. .flags = ACL_TYPE_DEFAULT,
  383. .list = jffs2_acl_default_listxattr,
  384. .get = jffs2_acl_getxattr,
  385. .set = jffs2_acl_setxattr,
  386. };