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;
  208. rc = posix_acl_update_mode(inode, &mode, &acl);
  209. if (rc)
  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. }
  221. break;
  222. case ACL_TYPE_DEFAULT:
  223. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  224. if (!S_ISDIR(inode->i_mode))
  225. return acl ? -EACCES : 0;
  226. break;
  227. default:
  228. return -EINVAL;
  229. }
  230. rc = __jffs2_set_acl(inode, xprefix, acl);
  231. if (!rc)
  232. set_cached_acl(inode, type, acl);
  233. return rc;
  234. }
  235. int jffs2_init_acl_pre(struct inode *dir_i, struct inode *inode, umode_t *i_mode)
  236. {
  237. struct posix_acl *acl;
  238. int rc;
  239. cache_no_acl(inode);
  240. if (S_ISLNK(*i_mode))
  241. return 0; /* Symlink always has no-ACL */
  242. acl = jffs2_get_acl(dir_i, ACL_TYPE_DEFAULT);
  243. if (IS_ERR(acl))
  244. return PTR_ERR(acl);
  245. if (!acl) {
  246. *i_mode &= ~current_umask();
  247. } else {
  248. if (S_ISDIR(*i_mode))
  249. set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
  250. rc = __posix_acl_create(&acl, GFP_KERNEL, i_mode);
  251. if (rc < 0)
  252. return rc;
  253. if (rc > 0)
  254. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  255. posix_acl_release(acl);
  256. }
  257. return 0;
  258. }
  259. int jffs2_init_acl_post(struct inode *inode)
  260. {
  261. int rc;
  262. if (inode->i_default_acl) {
  263. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_DEFAULT, inode->i_default_acl);
  264. if (rc)
  265. return rc;
  266. }
  267. if (inode->i_acl) {
  268. rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_ACCESS, inode->i_acl);
  269. if (rc)
  270. return rc;
  271. }
  272. return 0;
  273. }
  274. int jffs2_acl_chmod(struct inode *inode)
  275. {
  276. struct posix_acl *acl;
  277. int rc;
  278. if (S_ISLNK(inode->i_mode))
  279. return -EOPNOTSUPP;
  280. acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
  281. if (IS_ERR(acl) || !acl)
  282. return PTR_ERR(acl);
  283. rc = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  284. if (rc)
  285. return rc;
  286. rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, acl);
  287. posix_acl_release(acl);
  288. return rc;
  289. }
  290. static size_t jffs2_acl_access_listxattr(struct dentry *dentry, char *list,
  291. size_t list_size, const char *name, size_t name_len, int type)
  292. {
  293. const int retlen = sizeof(POSIX_ACL_XATTR_ACCESS);
  294. if (list && retlen <= list_size)
  295. strcpy(list, POSIX_ACL_XATTR_ACCESS);
  296. return retlen;
  297. }
  298. static size_t jffs2_acl_default_listxattr(struct dentry *dentry, char *list,
  299. size_t list_size, const char *name, size_t name_len, int type)
  300. {
  301. const int retlen = sizeof(POSIX_ACL_XATTR_DEFAULT);
  302. if (list && retlen <= list_size)
  303. strcpy(list, POSIX_ACL_XATTR_DEFAULT);
  304. return retlen;
  305. }
  306. static int jffs2_acl_getxattr(struct dentry *dentry, const char *name,
  307. void *buffer, size_t size, int type)
  308. {
  309. struct posix_acl *acl;
  310. int rc;
  311. if (name[0] != '\0')
  312. return -EINVAL;
  313. acl = jffs2_get_acl(dentry->d_inode, type);
  314. if (IS_ERR(acl))
  315. return PTR_ERR(acl);
  316. if (!acl)
  317. return -ENODATA;
  318. rc = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  319. posix_acl_release(acl);
  320. return rc;
  321. }
  322. static int jffs2_acl_setxattr(struct dentry *dentry, const char *name,
  323. const void *value, size_t size, int flags, int type)
  324. {
  325. struct posix_acl *acl;
  326. int rc;
  327. if (name[0] != '\0')
  328. return -EINVAL;
  329. if (!inode_owner_or_capable(dentry->d_inode))
  330. return -EPERM;
  331. if (value) {
  332. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  333. if (IS_ERR(acl))
  334. return PTR_ERR(acl);
  335. if (acl) {
  336. rc = posix_acl_valid(acl);
  337. if (rc)
  338. goto out;
  339. }
  340. } else {
  341. acl = NULL;
  342. }
  343. rc = jffs2_set_acl(dentry->d_inode, type, acl);
  344. out:
  345. posix_acl_release(acl);
  346. return rc;
  347. }
  348. const struct xattr_handler jffs2_acl_access_xattr_handler = {
  349. .prefix = POSIX_ACL_XATTR_ACCESS,
  350. .flags = ACL_TYPE_DEFAULT,
  351. .list = jffs2_acl_access_listxattr,
  352. .get = jffs2_acl_getxattr,
  353. .set = jffs2_acl_setxattr,
  354. };
  355. const struct xattr_handler jffs2_acl_default_xattr_handler = {
  356. .prefix = POSIX_ACL_XATTR_DEFAULT,
  357. .flags = ACL_TYPE_DEFAULT,
  358. .list = jffs2_acl_default_listxattr,
  359. .get = jffs2_acl_getxattr,
  360. .set = jffs2_acl_setxattr,
  361. };