acl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2006 NEC Corporation
  5. *
  6. * Created by KaiGai Kohei <[email protected]>
  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. break;
  88. case ACL_USER:
  89. value += sizeof(struct jffs2_acl_entry);
  90. if (value > end)
  91. goto fail;
  92. acl->a_entries[i].e_uid =
  93. make_kuid(&init_user_ns,
  94. je32_to_cpu(entry->e_id));
  95. break;
  96. case ACL_GROUP:
  97. value += sizeof(struct jffs2_acl_entry);
  98. if (value > end)
  99. goto fail;
  100. acl->a_entries[i].e_gid =
  101. make_kgid(&init_user_ns,
  102. je32_to_cpu(entry->e_id));
  103. break;
  104. default:
  105. goto fail;
  106. }
  107. }
  108. if (value != end)
  109. goto fail;
  110. return acl;
  111. fail:
  112. posix_acl_release(acl);
  113. return ERR_PTR(-EINVAL);
  114. }
  115. static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
  116. {
  117. struct jffs2_acl_header *header;
  118. struct jffs2_acl_entry *entry;
  119. void *e;
  120. size_t i;
  121. *size = jffs2_acl_size(acl->a_count);
  122. header = kmalloc(struct_size(header, a_entries, acl->a_count),
  123. GFP_KERNEL);
  124. if (!header)
  125. return ERR_PTR(-ENOMEM);
  126. header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
  127. e = header + 1;
  128. for (i=0; i < acl->a_count; i++) {
  129. const struct posix_acl_entry *acl_e = &acl->a_entries[i];
  130. entry = e;
  131. entry->e_tag = cpu_to_je16(acl_e->e_tag);
  132. entry->e_perm = cpu_to_je16(acl_e->e_perm);
  133. switch(acl_e->e_tag) {
  134. case ACL_USER:
  135. entry->e_id = cpu_to_je32(
  136. from_kuid(&init_user_ns, acl_e->e_uid));
  137. e += sizeof(struct jffs2_acl_entry);
  138. break;
  139. case ACL_GROUP:
  140. entry->e_id = cpu_to_je32(
  141. from_kgid(&init_user_ns, acl_e->e_gid));
  142. e += sizeof(struct jffs2_acl_entry);
  143. break;
  144. case ACL_USER_OBJ:
  145. case ACL_GROUP_OBJ:
  146. case ACL_MASK:
  147. case ACL_OTHER:
  148. e += sizeof(struct jffs2_acl_entry_short);
  149. break;
  150. default:
  151. goto fail;
  152. }
  153. }
  154. return header;
  155. fail:
  156. kfree(header);
  157. return ERR_PTR(-EINVAL);
  158. }
  159. struct posix_acl *jffs2_get_acl(struct inode *inode, int type, bool rcu)
  160. {
  161. struct posix_acl *acl;
  162. char *value = NULL;
  163. int rc, xprefix;
  164. if (rcu)
  165. return ERR_PTR(-ECHILD);
  166. switch (type) {
  167. case ACL_TYPE_ACCESS:
  168. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  169. break;
  170. case ACL_TYPE_DEFAULT:
  171. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  172. break;
  173. default:
  174. BUG();
  175. }
  176. rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
  177. if (rc > 0) {
  178. value = kmalloc(rc, GFP_KERNEL);
  179. if (!value)
  180. return ERR_PTR(-ENOMEM);
  181. rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
  182. }
  183. if (rc > 0) {
  184. acl = jffs2_acl_from_medium(value, rc);
  185. } else if (rc == -ENODATA || rc == -ENOSYS) {
  186. acl = NULL;
  187. } else {
  188. acl = ERR_PTR(rc);
  189. }
  190. kfree(value);
  191. return acl;
  192. }
  193. static int __jffs2_set_acl(struct inode *inode, int xprefix, struct posix_acl *acl)
  194. {
  195. char *value = NULL;
  196. size_t size = 0;
  197. int rc;
  198. if (acl) {
  199. value = jffs2_acl_to_medium(acl, &size);
  200. if (IS_ERR(value))
  201. return PTR_ERR(value);
  202. }
  203. rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
  204. if (!value && rc == -ENODATA)
  205. rc = 0;
  206. kfree(value);
  207. return rc;
  208. }
  209. int jffs2_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
  210. struct posix_acl *acl, int type)
  211. {
  212. int rc, xprefix;
  213. switch (type) {
  214. case ACL_TYPE_ACCESS:
  215. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  216. if (acl) {
  217. umode_t mode;
  218. rc = posix_acl_update_mode(&init_user_ns, inode, &mode,
  219. &acl);
  220. if (rc)
  221. return rc;
  222. if (inode->i_mode != mode) {
  223. struct iattr attr;
  224. attr.ia_valid = ATTR_MODE | ATTR_CTIME;
  225. attr.ia_mode = mode;
  226. attr.ia_ctime = current_time(inode);
  227. rc = jffs2_do_setattr(inode, &attr);
  228. if (rc < 0)
  229. return rc;
  230. }
  231. }
  232. break;
  233. case ACL_TYPE_DEFAULT:
  234. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  235. if (!S_ISDIR(inode->i_mode))
  236. return acl ? -EACCES : 0;
  237. break;
  238. default:
  239. return -EINVAL;
  240. }
  241. rc = __jffs2_set_acl(inode, xprefix, acl);
  242. if (!rc)
  243. set_cached_acl(inode, type, acl);
  244. return rc;
  245. }
  246. int jffs2_init_acl_pre(struct inode *dir_i, struct inode *inode, umode_t *i_mode)
  247. {
  248. struct posix_acl *default_acl, *acl;
  249. int rc;
  250. cache_no_acl(inode);
  251. rc = posix_acl_create(dir_i, i_mode, &default_acl, &acl);
  252. if (rc)
  253. return rc;
  254. if (default_acl) {
  255. set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  256. posix_acl_release(default_acl);
  257. }
  258. if (acl) {
  259. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  260. posix_acl_release(acl);
  261. }
  262. return 0;
  263. }
  264. int jffs2_init_acl_post(struct inode *inode)
  265. {
  266. int rc;
  267. if (inode->i_default_acl) {
  268. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_DEFAULT, inode->i_default_acl);
  269. if (rc)
  270. return rc;
  271. }
  272. if (inode->i_acl) {
  273. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_ACCESS, inode->i_acl);
  274. if (rc)
  275. return rc;
  276. }
  277. return 0;
  278. }