acl.c 9.0 KB

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