acl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 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. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include <linux/time.h>
  15. #include <linux/crc32.h>
  16. #include <linux/jffs2.h>
  17. #include <linux/xattr.h>
  18. #include <linux/posix_acl_xattr.h>
  19. #include <linux/mtd/mtd.h>
  20. #include "nodelist.h"
  21. static size_t jffs2_acl_size(int count)
  22. {
  23. if (count <= 4) {
  24. return sizeof(struct jffs2_acl_header)
  25. + count * sizeof(struct jffs2_acl_entry_short);
  26. } else {
  27. return sizeof(struct jffs2_acl_header)
  28. + 4 * sizeof(struct jffs2_acl_entry_short)
  29. + (count - 4) * sizeof(struct jffs2_acl_entry);
  30. }
  31. }
  32. static int jffs2_acl_count(size_t size)
  33. {
  34. size_t s;
  35. size -= sizeof(struct jffs2_acl_header);
  36. s = size - 4 * sizeof(struct jffs2_acl_entry_short);
  37. if (s < 0) {
  38. if (size % sizeof(struct jffs2_acl_entry_short))
  39. return -1;
  40. return size / sizeof(struct jffs2_acl_entry_short);
  41. } else {
  42. if (s % sizeof(struct jffs2_acl_entry))
  43. return -1;
  44. return s / sizeof(struct jffs2_acl_entry) + 4;
  45. }
  46. }
  47. static struct posix_acl *jffs2_acl_from_medium(void *value, size_t size)
  48. {
  49. void *end = value + size;
  50. struct jffs2_acl_header *header = value;
  51. struct jffs2_acl_entry *entry;
  52. struct posix_acl *acl;
  53. uint32_t ver;
  54. int i, count;
  55. if (!value)
  56. return NULL;
  57. if (size < sizeof(struct jffs2_acl_header))
  58. return ERR_PTR(-EINVAL);
  59. ver = je32_to_cpu(header->a_version);
  60. if (ver != JFFS2_ACL_VERSION) {
  61. JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver);
  62. return ERR_PTR(-EINVAL);
  63. }
  64. value += sizeof(struct jffs2_acl_header);
  65. count = jffs2_acl_count(size);
  66. if (count < 0)
  67. return ERR_PTR(-EINVAL);
  68. if (count == 0)
  69. return NULL;
  70. acl = posix_acl_alloc(count, GFP_KERNEL);
  71. if (!acl)
  72. return ERR_PTR(-ENOMEM);
  73. for (i=0; i < count; i++) {
  74. entry = value;
  75. if (value + sizeof(struct jffs2_acl_entry_short) > end)
  76. goto fail;
  77. acl->a_entries[i].e_tag = je16_to_cpu(entry->e_tag);
  78. acl->a_entries[i].e_perm = je16_to_cpu(entry->e_perm);
  79. switch (acl->a_entries[i].e_tag) {
  80. case ACL_USER_OBJ:
  81. case ACL_GROUP_OBJ:
  82. case ACL_MASK:
  83. case ACL_OTHER:
  84. value += sizeof(struct jffs2_acl_entry_short);
  85. acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
  86. break;
  87. case ACL_USER:
  88. case ACL_GROUP:
  89. value += sizeof(struct jffs2_acl_entry);
  90. if (value > end)
  91. goto fail;
  92. acl->a_entries[i].e_id = je32_to_cpu(entry->e_id);
  93. break;
  94. default:
  95. goto fail;
  96. }
  97. }
  98. if (value != end)
  99. goto fail;
  100. return acl;
  101. fail:
  102. posix_acl_release(acl);
  103. return ERR_PTR(-EINVAL);
  104. }
  105. static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
  106. {
  107. struct jffs2_acl_header *header;
  108. struct jffs2_acl_entry *entry;
  109. void *e;
  110. size_t i;
  111. *size = jffs2_acl_size(acl->a_count);
  112. header = kmalloc(sizeof(*header) + acl->a_count * sizeof(*entry), GFP_KERNEL);
  113. if (!header)
  114. return ERR_PTR(-ENOMEM);
  115. header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
  116. e = header + 1;
  117. for (i=0; i < acl->a_count; i++) {
  118. entry = e;
  119. entry->e_tag = cpu_to_je16(acl->a_entries[i].e_tag);
  120. entry->e_perm = cpu_to_je16(acl->a_entries[i].e_perm);
  121. switch(acl->a_entries[i].e_tag) {
  122. case ACL_USER:
  123. case ACL_GROUP:
  124. entry->e_id = cpu_to_je32(acl->a_entries[i].e_id);
  125. e += sizeof(struct jffs2_acl_entry);
  126. break;
  127. case ACL_USER_OBJ:
  128. case ACL_GROUP_OBJ:
  129. case ACL_MASK:
  130. case ACL_OTHER:
  131. e += sizeof(struct jffs2_acl_entry_short);
  132. break;
  133. default:
  134. goto fail;
  135. }
  136. }
  137. return header;
  138. fail:
  139. kfree(header);
  140. return ERR_PTR(-EINVAL);
  141. }
  142. static struct posix_acl *jffs2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
  143. {
  144. struct posix_acl *acl = JFFS2_ACL_NOT_CACHED;
  145. spin_lock(&inode->i_lock);
  146. if (*i_acl != JFFS2_ACL_NOT_CACHED)
  147. acl = posix_acl_dup(*i_acl);
  148. spin_unlock(&inode->i_lock);
  149. return acl;
  150. }
  151. static void jffs2_iset_acl(struct inode *inode, struct posix_acl **i_acl, struct posix_acl *acl)
  152. {
  153. spin_lock(&inode->i_lock);
  154. if (*i_acl != JFFS2_ACL_NOT_CACHED)
  155. posix_acl_release(*i_acl);
  156. *i_acl = posix_acl_dup(acl);
  157. spin_unlock(&inode->i_lock);
  158. }
  159. static struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
  160. {
  161. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  162. struct posix_acl *acl;
  163. char *value = NULL;
  164. int rc, xprefix;
  165. switch (type) {
  166. case ACL_TYPE_ACCESS:
  167. acl = jffs2_iget_acl(inode, &f->i_acl_access);
  168. if (acl != JFFS2_ACL_NOT_CACHED)
  169. return acl;
  170. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  171. break;
  172. case ACL_TYPE_DEFAULT:
  173. acl = jffs2_iget_acl(inode, &f->i_acl_default);
  174. if (acl != JFFS2_ACL_NOT_CACHED)
  175. return acl;
  176. xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
  177. break;
  178. default:
  179. return ERR_PTR(-EINVAL);
  180. }
  181. rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
  182. if (rc > 0) {
  183. value = kmalloc(rc, GFP_KERNEL);
  184. if (!value)
  185. return ERR_PTR(-ENOMEM);
  186. rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
  187. }
  188. if (rc > 0) {
  189. acl = jffs2_acl_from_medium(value, rc);
  190. } else if (rc == -ENODATA || rc == -ENOSYS) {
  191. acl = NULL;
  192. } else {
  193. acl = ERR_PTR(rc);
  194. }
  195. if (value)
  196. kfree(value);
  197. if (!IS_ERR(acl)) {
  198. switch (type) {
  199. case ACL_TYPE_ACCESS:
  200. jffs2_iset_acl(inode, &f->i_acl_access, acl);
  201. break;
  202. case ACL_TYPE_DEFAULT:
  203. jffs2_iset_acl(inode, &f->i_acl_default, acl);
  204. break;
  205. }
  206. }
  207. return acl;
  208. }
  209. static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  210. {
  211. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  212. size_t size = 0;
  213. char *value = NULL;
  214. int rc, xprefix;
  215. if (S_ISLNK(inode->i_mode))
  216. return -EOPNOTSUPP;
  217. switch (type) {
  218. case ACL_TYPE_ACCESS:
  219. xprefix = JFFS2_XPREFIX_ACL_ACCESS;
  220. if (acl) {
  221. mode_t mode = inode->i_mode;
  222. rc = posix_acl_equiv_mode(acl, &mode);
  223. if (rc < 0)
  224. return rc;
  225. if (inode->i_mode != mode) {
  226. inode->i_mode = mode;
  227. jffs2_dirty_inode(inode);
  228. }
  229. if (rc == 0)
  230. acl = NULL;
  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. if (acl) {
  242. value = jffs2_acl_to_medium(acl, &size);
  243. if (IS_ERR(value))
  244. return PTR_ERR(value);
  245. }
  246. rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
  247. if (!value && rc == -ENODATA)
  248. rc = 0;
  249. if (value)
  250. kfree(value);
  251. if (!rc) {
  252. switch(type) {
  253. case ACL_TYPE_ACCESS:
  254. jffs2_iset_acl(inode, &f->i_acl_access, acl);
  255. break;
  256. case ACL_TYPE_DEFAULT:
  257. jffs2_iset_acl(inode, &f->i_acl_default, acl);
  258. break;
  259. }
  260. }
  261. return rc;
  262. }
  263. static int jffs2_check_acl(struct inode *inode, int mask)
  264. {
  265. struct posix_acl *acl;
  266. int rc;
  267. acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
  268. if (IS_ERR(acl))
  269. return PTR_ERR(acl);
  270. if (acl) {
  271. rc = posix_acl_permission(inode, acl, mask);
  272. posix_acl_release(acl);
  273. return rc;
  274. }
  275. return -EAGAIN;
  276. }
  277. int jffs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  278. {
  279. return generic_permission(inode, mask, jffs2_check_acl);
  280. }
  281. int jffs2_init_acl(struct inode *inode, struct inode *dir)
  282. {
  283. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  284. struct posix_acl *acl = NULL, *clone;
  285. mode_t mode;
  286. int rc = 0;
  287. f->i_acl_access = JFFS2_ACL_NOT_CACHED;
  288. f->i_acl_default = JFFS2_ACL_NOT_CACHED;
  289. if (!S_ISLNK(inode->i_mode)) {
  290. acl = jffs2_get_acl(dir, ACL_TYPE_DEFAULT);
  291. if (IS_ERR(acl))
  292. return PTR_ERR(acl);
  293. if (!acl)
  294. inode->i_mode &= ~current->fs->umask;
  295. }
  296. if (acl) {
  297. if (S_ISDIR(inode->i_mode)) {
  298. rc = jffs2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  299. if (rc)
  300. goto cleanup;
  301. }
  302. clone = posix_acl_clone(acl, GFP_KERNEL);
  303. rc = -ENOMEM;
  304. if (!clone)
  305. goto cleanup;
  306. mode = inode->i_mode;
  307. rc = posix_acl_create_masq(clone, &mode);
  308. if (rc >= 0) {
  309. inode->i_mode = mode;
  310. if (rc > 0)
  311. rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
  312. }
  313. posix_acl_release(clone);
  314. }
  315. cleanup:
  316. posix_acl_release(acl);
  317. return rc;
  318. }
  319. void jffs2_clear_acl(struct jffs2_inode_info *f)
  320. {
  321. if (f->i_acl_access && f->i_acl_access != JFFS2_ACL_NOT_CACHED) {
  322. posix_acl_release(f->i_acl_access);
  323. f->i_acl_access = JFFS2_ACL_NOT_CACHED;
  324. }
  325. if (f->i_acl_default && f->i_acl_default != JFFS2_ACL_NOT_CACHED) {
  326. posix_acl_release(f->i_acl_default);
  327. f->i_acl_default = JFFS2_ACL_NOT_CACHED;
  328. }
  329. }
  330. int jffs2_acl_chmod(struct inode *inode)
  331. {
  332. struct posix_acl *acl, *clone;
  333. int rc;
  334. if (S_ISLNK(inode->i_mode))
  335. return -EOPNOTSUPP;
  336. acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
  337. if (IS_ERR(acl) || !acl)
  338. return PTR_ERR(acl);
  339. clone = posix_acl_clone(acl, GFP_KERNEL);
  340. posix_acl_release(acl);
  341. if (!clone)
  342. return -ENOMEM;
  343. rc = posix_acl_chmod_masq(clone, inode->i_mode);
  344. if (!rc)
  345. rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
  346. posix_acl_release(clone);
  347. return rc;
  348. }
  349. static size_t jffs2_acl_access_listxattr(struct inode *inode, char *list, size_t list_size,
  350. const char *name, size_t name_len)
  351. {
  352. const int retlen = sizeof(POSIX_ACL_XATTR_ACCESS);
  353. if (list && retlen <= list_size)
  354. strcpy(list, POSIX_ACL_XATTR_ACCESS);
  355. return retlen;
  356. }
  357. static size_t jffs2_acl_default_listxattr(struct inode *inode, char *list, size_t list_size,
  358. const char *name, size_t name_len)
  359. {
  360. const int retlen = sizeof(POSIX_ACL_XATTR_DEFAULT);
  361. if (list && retlen <= list_size)
  362. strcpy(list, POSIX_ACL_XATTR_DEFAULT);
  363. return retlen;
  364. }
  365. static int jffs2_acl_getxattr(struct inode *inode, int type, void *buffer, size_t size)
  366. {
  367. struct posix_acl *acl;
  368. int rc;
  369. acl = jffs2_get_acl(inode, type);
  370. if (IS_ERR(acl))
  371. return PTR_ERR(acl);
  372. if (!acl)
  373. return -ENODATA;
  374. rc = posix_acl_to_xattr(acl, buffer, size);
  375. posix_acl_release(acl);
  376. return rc;
  377. }
  378. static int jffs2_acl_access_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
  379. {
  380. if (name[0] != '\0')
  381. return -EINVAL;
  382. return jffs2_acl_getxattr(inode, ACL_TYPE_ACCESS, buffer, size);
  383. }
  384. static int jffs2_acl_default_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
  385. {
  386. if (name[0] != '\0')
  387. return -EINVAL;
  388. return jffs2_acl_getxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
  389. }
  390. static int jffs2_acl_setxattr(struct inode *inode, int type, const void *value, size_t size)
  391. {
  392. struct posix_acl *acl;
  393. int rc;
  394. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  395. return -EPERM;
  396. if (value) {
  397. acl = posix_acl_from_xattr(value, size);
  398. if (IS_ERR(acl))
  399. return PTR_ERR(acl);
  400. if (acl) {
  401. rc = posix_acl_valid(acl);
  402. if (rc)
  403. goto out;
  404. }
  405. } else {
  406. acl = NULL;
  407. }
  408. rc = jffs2_set_acl(inode, type, acl);
  409. out:
  410. posix_acl_release(acl);
  411. return rc;
  412. }
  413. static int jffs2_acl_access_setxattr(struct inode *inode, const char *name,
  414. const void *buffer, size_t size, int flags)
  415. {
  416. if (name[0] != '\0')
  417. return -EINVAL;
  418. return jffs2_acl_setxattr(inode, ACL_TYPE_ACCESS, buffer, size);
  419. }
  420. static int jffs2_acl_default_setxattr(struct inode *inode, const char *name,
  421. const void *buffer, size_t size, int flags)
  422. {
  423. if (name[0] != '\0')
  424. return -EINVAL;
  425. return jffs2_acl_setxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
  426. }
  427. struct xattr_handler jffs2_acl_access_xattr_handler = {
  428. .prefix = POSIX_ACL_XATTR_ACCESS,
  429. .list = jffs2_acl_access_listxattr,
  430. .get = jffs2_acl_access_getxattr,
  431. .set = jffs2_acl_access_setxattr,
  432. };
  433. struct xattr_handler jffs2_acl_default_xattr_handler = {
  434. .prefix = POSIX_ACL_XATTR_DEFAULT,
  435. .list = jffs2_acl_default_listxattr,
  436. .get = jffs2_acl_default_getxattr,
  437. .set = jffs2_acl_default_setxattr,
  438. };