master.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. */
  10. /* This file implements reading and writing the master node */
  11. #include "ubifs.h"
  12. /**
  13. * ubifs_compare_master_node - compare two UBIFS master nodes
  14. * @c: UBIFS file-system description object
  15. * @m1: the first node
  16. * @m2: the second node
  17. *
  18. * This function compares two UBIFS master nodes. Returns 0 if they are equal
  19. * and nonzero if not.
  20. */
  21. int ubifs_compare_master_node(struct ubifs_info *c, void *m1, void *m2)
  22. {
  23. int ret;
  24. int behind;
  25. int hmac_offs = offsetof(struct ubifs_mst_node, hmac);
  26. /*
  27. * Do not compare the common node header since the sequence number and
  28. * hence the CRC are different.
  29. */
  30. ret = memcmp(m1 + UBIFS_CH_SZ, m2 + UBIFS_CH_SZ,
  31. hmac_offs - UBIFS_CH_SZ);
  32. if (ret)
  33. return ret;
  34. /*
  35. * Do not compare the embedded HMAC aswell which also must be different
  36. * due to the different common node header.
  37. */
  38. behind = hmac_offs + UBIFS_MAX_HMAC_LEN;
  39. if (UBIFS_MST_NODE_SZ > behind)
  40. return memcmp(m1 + behind, m2 + behind, UBIFS_MST_NODE_SZ - behind);
  41. return 0;
  42. }
  43. /* mst_node_check_hash - Check hash of a master node
  44. * @c: UBIFS file-system description object
  45. * @mst: The master node
  46. * @expected: The expected hash of the master node
  47. *
  48. * This checks the hash of a master node against a given expected hash.
  49. * Note that we have two master nodes on a UBIFS image which have different
  50. * sequence numbers and consequently different CRCs. To be able to match
  51. * both master nodes we exclude the common node header containing the sequence
  52. * number and CRC from the hash.
  53. *
  54. * Returns 0 if the hashes are equal, a negative error code otherwise.
  55. */
  56. static int mst_node_check_hash(const struct ubifs_info *c,
  57. const struct ubifs_mst_node *mst,
  58. const u8 *expected)
  59. {
  60. u8 calc[UBIFS_MAX_HASH_LEN];
  61. const void *node = mst;
  62. crypto_shash_tfm_digest(c->hash_tfm, node + sizeof(struct ubifs_ch),
  63. UBIFS_MST_NODE_SZ - sizeof(struct ubifs_ch),
  64. calc);
  65. if (ubifs_check_hash(c, expected, calc))
  66. return -EPERM;
  67. return 0;
  68. }
  69. /**
  70. * scan_for_master - search the valid master node.
  71. * @c: UBIFS file-system description object
  72. *
  73. * This function scans the master node LEBs and search for the latest master
  74. * node. Returns zero in case of success, %-EUCLEAN if there master area is
  75. * corrupted and requires recovery, and a negative error code in case of
  76. * failure.
  77. */
  78. static int scan_for_master(struct ubifs_info *c)
  79. {
  80. struct ubifs_scan_leb *sleb;
  81. struct ubifs_scan_node *snod;
  82. int lnum, offs = 0, nodes_cnt, err;
  83. lnum = UBIFS_MST_LNUM;
  84. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  85. if (IS_ERR(sleb))
  86. return PTR_ERR(sleb);
  87. nodes_cnt = sleb->nodes_cnt;
  88. if (nodes_cnt > 0) {
  89. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  90. list);
  91. if (snod->type != UBIFS_MST_NODE)
  92. goto out_dump;
  93. memcpy(c->mst_node, snod->node, snod->len);
  94. offs = snod->offs;
  95. }
  96. ubifs_scan_destroy(sleb);
  97. lnum += 1;
  98. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
  99. if (IS_ERR(sleb))
  100. return PTR_ERR(sleb);
  101. if (sleb->nodes_cnt != nodes_cnt)
  102. goto out;
  103. if (!sleb->nodes_cnt)
  104. goto out;
  105. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
  106. if (snod->type != UBIFS_MST_NODE)
  107. goto out_dump;
  108. if (snod->offs != offs)
  109. goto out;
  110. if (ubifs_compare_master_node(c, c->mst_node, snod->node))
  111. goto out;
  112. c->mst_offs = offs;
  113. ubifs_scan_destroy(sleb);
  114. if (!ubifs_authenticated(c))
  115. return 0;
  116. if (ubifs_hmac_zero(c, c->mst_node->hmac)) {
  117. err = mst_node_check_hash(c, c->mst_node,
  118. c->sup_node->hash_mst);
  119. if (err)
  120. ubifs_err(c, "Failed to verify master node hash");
  121. } else {
  122. err = ubifs_node_verify_hmac(c, c->mst_node,
  123. sizeof(struct ubifs_mst_node),
  124. offsetof(struct ubifs_mst_node, hmac));
  125. if (err)
  126. ubifs_err(c, "Failed to verify master node HMAC");
  127. }
  128. if (err)
  129. return -EPERM;
  130. return 0;
  131. out:
  132. ubifs_scan_destroy(sleb);
  133. return -EUCLEAN;
  134. out_dump:
  135. ubifs_err(c, "unexpected node type %d master LEB %d:%d",
  136. snod->type, lnum, snod->offs);
  137. ubifs_scan_destroy(sleb);
  138. return -EINVAL;
  139. }
  140. /**
  141. * validate_master - validate master node.
  142. * @c: UBIFS file-system description object
  143. *
  144. * This function validates data which was read from master node. Returns zero
  145. * if the data is all right and %-EINVAL if not.
  146. */
  147. static int validate_master(const struct ubifs_info *c)
  148. {
  149. long long main_sz;
  150. int err;
  151. if (c->max_sqnum >= SQNUM_WATERMARK) {
  152. err = 1;
  153. goto out;
  154. }
  155. if (c->cmt_no >= c->max_sqnum) {
  156. err = 2;
  157. goto out;
  158. }
  159. if (c->highest_inum >= INUM_WATERMARK) {
  160. err = 3;
  161. goto out;
  162. }
  163. if (c->lhead_lnum < UBIFS_LOG_LNUM ||
  164. c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
  165. c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
  166. c->lhead_offs & (c->min_io_size - 1)) {
  167. err = 4;
  168. goto out;
  169. }
  170. if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
  171. c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
  172. err = 5;
  173. goto out;
  174. }
  175. if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
  176. c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
  177. err = 6;
  178. goto out;
  179. }
  180. if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
  181. err = 7;
  182. goto out;
  183. }
  184. if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
  185. c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
  186. c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
  187. err = 8;
  188. goto out;
  189. }
  190. main_sz = (long long)c->main_lebs * c->leb_size;
  191. if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
  192. err = 9;
  193. goto out;
  194. }
  195. if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
  196. c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
  197. err = 10;
  198. goto out;
  199. }
  200. if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
  201. c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
  202. c->nhead_offs > c->leb_size) {
  203. err = 11;
  204. goto out;
  205. }
  206. if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
  207. c->ltab_offs < 0 ||
  208. c->ltab_offs + c->ltab_sz > c->leb_size) {
  209. err = 12;
  210. goto out;
  211. }
  212. if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
  213. c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
  214. c->lsave_offs + c->lsave_sz > c->leb_size)) {
  215. err = 13;
  216. goto out;
  217. }
  218. if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
  219. err = 14;
  220. goto out;
  221. }
  222. if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
  223. err = 15;
  224. goto out;
  225. }
  226. if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
  227. err = 16;
  228. goto out;
  229. }
  230. if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
  231. c->lst.total_free & 7) {
  232. err = 17;
  233. goto out;
  234. }
  235. if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
  236. err = 18;
  237. goto out;
  238. }
  239. if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
  240. err = 19;
  241. goto out;
  242. }
  243. if (c->lst.total_free + c->lst.total_dirty +
  244. c->lst.total_used > main_sz) {
  245. err = 20;
  246. goto out;
  247. }
  248. if (c->lst.total_dead + c->lst.total_dark +
  249. c->lst.total_used + c->bi.old_idx_sz > main_sz) {
  250. err = 21;
  251. goto out;
  252. }
  253. if (c->lst.total_dead < 0 ||
  254. c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
  255. c->lst.total_dead & 7) {
  256. err = 22;
  257. goto out;
  258. }
  259. if (c->lst.total_dark < 0 ||
  260. c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
  261. c->lst.total_dark & 7) {
  262. err = 23;
  263. goto out;
  264. }
  265. return 0;
  266. out:
  267. ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err);
  268. ubifs_dump_node(c, c->mst_node);
  269. return -EINVAL;
  270. }
  271. /**
  272. * ubifs_read_master - read master node.
  273. * @c: UBIFS file-system description object
  274. *
  275. * This function finds and reads the master node during file-system mount. If
  276. * the flash is empty, it creates default master node as well. Returns zero in
  277. * case of success and a negative error code in case of failure.
  278. */
  279. int ubifs_read_master(struct ubifs_info *c)
  280. {
  281. int err, old_leb_cnt;
  282. c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  283. if (!c->mst_node)
  284. return -ENOMEM;
  285. err = scan_for_master(c);
  286. if (err) {
  287. if (err == -EUCLEAN)
  288. err = ubifs_recover_master_node(c);
  289. if (err)
  290. /*
  291. * Note, we do not free 'c->mst_node' here because the
  292. * unmount routine will take care of this.
  293. */
  294. return err;
  295. }
  296. /* Make sure that the recovery flag is clear */
  297. c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
  298. c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
  299. c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
  300. c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
  301. c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
  302. c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
  303. c->zroot.len = le32_to_cpu(c->mst_node->root_len);
  304. c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
  305. c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
  306. c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
  307. c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
  308. c->bi.old_idx_sz = le64_to_cpu(c->mst_node->index_size);
  309. c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
  310. c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
  311. c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
  312. c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
  313. c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
  314. c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
  315. c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
  316. c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
  317. c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
  318. c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
  319. c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
  320. old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
  321. c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
  322. c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
  323. c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
  324. c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
  325. c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
  326. ubifs_copy_hash(c, c->mst_node->hash_root_idx, c->zroot.hash);
  327. c->calc_idx_sz = c->bi.old_idx_sz;
  328. if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
  329. c->no_orphs = 1;
  330. if (old_leb_cnt != c->leb_cnt) {
  331. /* The file system has been resized */
  332. int growth = c->leb_cnt - old_leb_cnt;
  333. if (c->leb_cnt < old_leb_cnt ||
  334. c->leb_cnt < UBIFS_MIN_LEB_CNT) {
  335. ubifs_err(c, "bad leb_cnt on master node");
  336. ubifs_dump_node(c, c->mst_node);
  337. return -EINVAL;
  338. }
  339. dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
  340. old_leb_cnt, c->leb_cnt);
  341. c->lst.empty_lebs += growth;
  342. c->lst.total_free += growth * (long long)c->leb_size;
  343. c->lst.total_dark += growth * (long long)c->dark_wm;
  344. /*
  345. * Reflect changes back onto the master node. N.B. the master
  346. * node gets written immediately whenever mounting (or
  347. * remounting) in read-write mode, so we do not need to write it
  348. * here.
  349. */
  350. c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
  351. c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
  352. c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
  353. c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
  354. }
  355. err = validate_master(c);
  356. if (err)
  357. return err;
  358. err = dbg_old_index_check_init(c, &c->zroot);
  359. return err;
  360. }
  361. /**
  362. * ubifs_write_master - write master node.
  363. * @c: UBIFS file-system description object
  364. *
  365. * This function writes the master node. Returns zero in case of success and a
  366. * negative error code in case of failure. The master node is written twice to
  367. * enable recovery.
  368. */
  369. int ubifs_write_master(struct ubifs_info *c)
  370. {
  371. int err, lnum, offs, len;
  372. ubifs_assert(c, !c->ro_media && !c->ro_mount);
  373. if (c->ro_error)
  374. return -EROFS;
  375. lnum = UBIFS_MST_LNUM;
  376. offs = c->mst_offs + c->mst_node_alsz;
  377. len = UBIFS_MST_NODE_SZ;
  378. if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
  379. err = ubifs_leb_unmap(c, lnum);
  380. if (err)
  381. return err;
  382. offs = 0;
  383. }
  384. c->mst_offs = offs;
  385. c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
  386. ubifs_copy_hash(c, c->zroot.hash, c->mst_node->hash_root_idx);
  387. err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
  388. offsetof(struct ubifs_mst_node, hmac));
  389. if (err)
  390. return err;
  391. lnum += 1;
  392. if (offs == 0) {
  393. err = ubifs_leb_unmap(c, lnum);
  394. if (err)
  395. return err;
  396. }
  397. err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
  398. offsetof(struct ubifs_mst_node, hmac));
  399. return err;
  400. }