acl.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. 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)
  160. {
  161. struct posix_acl *acl;
  162. char *value = NULL;
  163. int rc, xprefix;
  164. switch (type) {
  165. case ACL_TYPE_ACCESS:
  166. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  167. break;
  168. case ACL_TYPE_DEFAULT:
  169. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  170. break;
  171. default:
  172. BUG();
  173. }
  174. rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
  175. if (rc > 0) {
  176. value = kmalloc(rc, GFP_KERNEL);
  177. if (!value)
  178. return ERR_PTR(-ENOMEM);
  179. rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
  180. }
  181. if (rc > 0) {
  182. acl = jffs2_acl_from_medium(value, rc);
  183. } else if (rc == -ENODATA || rc == -ENOSYS) {
  184. acl = NULL;
  185. } else {
  186. acl = ERR_PTR(rc);
  187. }
  188. kfree(value);
  189. return acl;
  190. }
  191. static int __jffs2_set_acl(struct inode *inode, int xprefix, struct posix_acl *acl)
  192. {
  193. char *value = NULL;
  194. size_t size = 0;
  195. int rc;
  196. if (acl) {
  197. value = jffs2_acl_to_medium(acl, &size);
  198. if (IS_ERR(value))
  199. return PTR_ERR(value);
  200. }
  201. rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
  202. if (!value && rc == -ENODATA)
  203. rc = 0;
  204. kfree(value);
  205. return rc;
  206. }
  207. int jffs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  208. {
  209. int rc, xprefix;
  210. switch (type) {
  211. case ACL_TYPE_ACCESS:
  212. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  213. if (acl) {
  214. umode_t mode;
  215. rc = posix_acl_update_mode(inode, &mode, &acl);
  216. if (rc)
  217. return rc;
  218. if (inode->i_mode != mode) {
  219. struct iattr attr;
  220. attr.ia_valid = ATTR_MODE | ATTR_CTIME;
  221. attr.ia_mode = mode;
  222. attr.ia_ctime = current_time(inode);
  223. rc = jffs2_do_setattr(inode, &attr);
  224. if (rc < 0)
  225. return rc;
  226. }
  227. }
  228. break;
  229. case ACL_TYPE_DEFAULT:
  230. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  231. if (!S_ISDIR(inode->i_mode))
  232. return acl ? -EACCES : 0;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. rc = __jffs2_set_acl(inode, xprefix, acl);
  238. if (!rc)
  239. set_cached_acl(inode, type, acl);
  240. return rc;
  241. }
  242. int jffs2_init_acl_pre(struct inode *dir_i, struct inode *inode, umode_t *i_mode)
  243. {
  244. struct posix_acl *default_acl, *acl;
  245. int rc;
  246. cache_no_acl(inode);
  247. rc = posix_acl_create(dir_i, i_mode, &default_acl, &acl);
  248. if (rc)
  249. return rc;
  250. if (default_acl) {
  251. set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  252. posix_acl_release(default_acl);
  253. }
  254. if (acl) {
  255. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  256. posix_acl_release(acl);
  257. }
  258. return 0;
  259. }
  260. int jffs2_init_acl_post(struct inode *inode)
  261. {
  262. int rc;
  263. if (inode->i_default_acl) {
  264. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_DEFAULT, inode->i_default_acl);
  265. if (rc)
  266. return rc;
  267. }
  268. if (inode->i_acl) {
  269. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_ACCESS, inode->i_acl);
  270. if (rc)
  271. return rc;
  272. }
  273. return 0;
  274. }