acl.c 11 KB

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