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(sizeof(*header) + acl->a_count * sizeof(*entry), GFP_KERNEL);
  123. if (!header)
  124. return ERR_PTR(-ENOMEM);
  125. header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
  126. e = header + 1;
  127. for (i=0; i < acl->a_count; i++) {
  128. const struct posix_acl_entry *acl_e = &acl->a_entries[i];
  129. entry = e;
  130. entry->e_tag = cpu_to_je16(acl_e->e_tag);
  131. entry->e_perm = cpu_to_je16(acl_e->e_perm);
  132. switch(acl_e->e_tag) {
  133. case ACL_USER:
  134. entry->e_id = cpu_to_je32(
  135. from_kuid(&init_user_ns, acl_e->e_uid));
  136. e += sizeof(struct jffs2_acl_entry);
  137. break;
  138. case ACL_GROUP:
  139. entry->e_id = cpu_to_je32(
  140. from_kgid(&init_user_ns, acl_e->e_gid));
  141. e += sizeof(struct jffs2_acl_entry);
  142. break;
  143. case ACL_USER_OBJ:
  144. case ACL_GROUP_OBJ:
  145. case ACL_MASK:
  146. case ACL_OTHER:
  147. e += sizeof(struct jffs2_acl_entry_short);
  148. break;
  149. default:
  150. goto fail;
  151. }
  152. }
  153. return header;
  154. fail:
  155. kfree(header);
  156. return ERR_PTR(-EINVAL);
  157. }
  158. struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
  159. {
  160. struct posix_acl *acl;
  161. char *value = NULL;
  162. int rc, xprefix;
  163. switch (type) {
  164. case ACL_TYPE_ACCESS:
  165. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  166. break;
  167. case ACL_TYPE_DEFAULT:
  168. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  169. break;
  170. default:
  171. BUG();
  172. }
  173. rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
  174. if (rc > 0) {
  175. value = kmalloc(rc, GFP_KERNEL);
  176. if (!value)
  177. return ERR_PTR(-ENOMEM);
  178. rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
  179. }
  180. if (rc > 0) {
  181. acl = jffs2_acl_from_medium(value, rc);
  182. } else if (rc == -ENODATA || rc == -ENOSYS) {
  183. acl = NULL;
  184. } else {
  185. acl = ERR_PTR(rc);
  186. }
  187. kfree(value);
  188. if (!IS_ERR(acl))
  189. set_cached_acl(inode, type, acl);
  190. return acl;
  191. }
  192. static int __jffs2_set_acl(struct inode *inode, int xprefix, struct posix_acl *acl)
  193. {
  194. char *value = NULL;
  195. size_t size = 0;
  196. int rc;
  197. if (acl) {
  198. value = jffs2_acl_to_medium(acl, &size);
  199. if (IS_ERR(value))
  200. return PTR_ERR(value);
  201. }
  202. rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
  203. if (!value && rc == -ENODATA)
  204. rc = 0;
  205. kfree(value);
  206. return rc;
  207. }
  208. int jffs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  209. {
  210. int rc, xprefix;
  211. switch (type) {
  212. case ACL_TYPE_ACCESS:
  213. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  214. if (acl) {
  215. umode_t mode;
  216. rc = posix_acl_update_mode(inode, &mode, &acl);
  217. if (rc)
  218. return rc;
  219. if (inode->i_mode != mode) {
  220. struct iattr attr;
  221. attr.ia_valid = ATTR_MODE | ATTR_CTIME;
  222. attr.ia_mode = mode;
  223. attr.ia_ctime = CURRENT_TIME_SEC;
  224. rc = jffs2_do_setattr(inode, &attr);
  225. if (rc < 0)
  226. return rc;
  227. }
  228. }
  229. break;
  230. case ACL_TYPE_DEFAULT:
  231. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  232. if (!S_ISDIR(inode->i_mode))
  233. return acl ? -EACCES : 0;
  234. break;
  235. default:
  236. return -EINVAL;
  237. }
  238. rc = __jffs2_set_acl(inode, xprefix, acl);
  239. if (!rc)
  240. set_cached_acl(inode, type, acl);
  241. return rc;
  242. }
  243. int jffs2_init_acl_pre(struct inode *dir_i, struct inode *inode, umode_t *i_mode)
  244. {
  245. struct posix_acl *default_acl, *acl;
  246. int rc;
  247. cache_no_acl(inode);
  248. rc = posix_acl_create(dir_i, i_mode, &default_acl, &acl);
  249. if (rc)
  250. return rc;
  251. if (default_acl) {
  252. set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  253. posix_acl_release(default_acl);
  254. }
  255. if (acl) {
  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. }