fs_mgr_verity.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * Copyright (C) 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <ctype.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <inttypes.h>
  20. #include <libgen.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/mount.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <time.h>
  29. #include <unistd.h>
  30. #include <android-base/file.h>
  31. #include <android-base/properties.h>
  32. #include <android-base/strings.h>
  33. #include <android-base/unique_fd.h>
  34. #include <crypto_utils/android_pubkey.h>
  35. #include <cutils/properties.h>
  36. #include <libdm/dm.h>
  37. #include <logwrap/logwrap.h>
  38. #include <openssl/obj_mac.h>
  39. #include <openssl/rsa.h>
  40. #include <openssl/sha.h>
  41. #include "fec/io.h"
  42. #include "fs_mgr.h"
  43. #include "fs_mgr_dm_linear.h"
  44. #include "fs_mgr_priv.h"
  45. // Realistically, this file should be part of the android::fs_mgr namespace;
  46. using namespace android::fs_mgr;
  47. #define VERITY_TABLE_RSA_KEY "/verity_key"
  48. #define VERITY_TABLE_HASH_IDX 8
  49. #define VERITY_TABLE_SALT_IDX 9
  50. #define VERITY_TABLE_OPT_RESTART "restart_on_corruption"
  51. #define VERITY_TABLE_OPT_LOGGING "ignore_corruption"
  52. #define VERITY_TABLE_OPT_IGNZERO "ignore_zero_blocks"
  53. #define VERITY_TABLE_OPT_FEC_FORMAT \
  54. "use_fec_from_device %s fec_start %" PRIu64 " fec_blocks %" PRIu64 \
  55. " fec_roots %u " VERITY_TABLE_OPT_IGNZERO
  56. #define VERITY_TABLE_OPT_FEC_ARGS 9
  57. #define METADATA_MAGIC 0x01564c54
  58. #define METADATA_TAG_MAX_LENGTH 63
  59. #define METADATA_EOD "eod"
  60. #define VERITY_LASTSIG_TAG "verity_lastsig"
  61. #define VERITY_STATE_TAG "verity_state"
  62. #define VERITY_STATE_HEADER 0x83c0ae9d
  63. #define VERITY_STATE_VERSION 1
  64. #define VERITY_KMSG_RESTART "dm-verity device corrupted"
  65. #define VERITY_KMSG_BUFSIZE 1024
  66. #define READ_BUF_SIZE 4096
  67. #define __STRINGIFY(x) #x
  68. #define STRINGIFY(x) __STRINGIFY(x)
  69. struct verity_state {
  70. uint32_t header;
  71. uint32_t version;
  72. int32_t mode;
  73. };
  74. extern struct fs_info info;
  75. static RSA *load_key(const char *path)
  76. {
  77. uint8_t key_data[ANDROID_PUBKEY_ENCODED_SIZE];
  78. auto f = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path, "re"), fclose};
  79. if (!f) {
  80. LERROR << "Can't open " << path;
  81. return nullptr;
  82. }
  83. if (!fread(key_data, sizeof(key_data), 1, f.get())) {
  84. LERROR << "Could not read key!";
  85. return nullptr;
  86. }
  87. RSA* key = nullptr;
  88. if (!android_pubkey_decode(key_data, sizeof(key_data), &key)) {
  89. LERROR << "Could not parse key!";
  90. return nullptr;
  91. }
  92. return key;
  93. }
  94. static int verify_table(const uint8_t *signature, size_t signature_size,
  95. const char *table, uint32_t table_length)
  96. {
  97. RSA *key;
  98. uint8_t hash_buf[SHA256_DIGEST_LENGTH];
  99. int retval = -1;
  100. // Hash the table
  101. SHA256((uint8_t*)table, table_length, hash_buf);
  102. // Now get the public key from the keyfile
  103. key = load_key(VERITY_TABLE_RSA_KEY);
  104. if (!key) {
  105. LERROR << "Couldn't load verity keys";
  106. goto out;
  107. }
  108. // verify the result
  109. if (!RSA_verify(NID_sha256, hash_buf, sizeof(hash_buf), signature,
  110. signature_size, key)) {
  111. LERROR << "Couldn't verify table";
  112. goto out;
  113. }
  114. retval = 0;
  115. out:
  116. RSA_free(key);
  117. return retval;
  118. }
  119. static int verify_verity_signature(const struct fec_verity_metadata& verity)
  120. {
  121. if (verify_table(verity.signature, sizeof(verity.signature),
  122. verity.table, verity.table_length) == 0 ||
  123. verify_table(verity.ecc_signature, sizeof(verity.ecc_signature),
  124. verity.table, verity.table_length) == 0) {
  125. return 0;
  126. }
  127. return -1;
  128. }
  129. static int invalidate_table(char *table, size_t table_length)
  130. {
  131. size_t n = 0;
  132. size_t idx = 0;
  133. size_t cleared = 0;
  134. while (n < table_length) {
  135. if (table[n++] == ' ') {
  136. ++idx;
  137. }
  138. if (idx != VERITY_TABLE_HASH_IDX && idx != VERITY_TABLE_SALT_IDX) {
  139. continue;
  140. }
  141. while (n < table_length && table[n] != ' ') {
  142. table[n++] = '0';
  143. }
  144. if (++cleared == 2) {
  145. return 0;
  146. }
  147. }
  148. return -1;
  149. }
  150. struct verity_table_params {
  151. char *table;
  152. int mode;
  153. struct fec_ecc_metadata ecc;
  154. const char *ecc_dev;
  155. };
  156. typedef bool (*format_verity_table_func)(char *buf, const size_t bufsize,
  157. const struct verity_table_params *params);
  158. static bool format_verity_table(char *buf, const size_t bufsize,
  159. const struct verity_table_params *params)
  160. {
  161. const char *mode_flag = NULL;
  162. int res = -1;
  163. if (params->mode == VERITY_MODE_RESTART) {
  164. mode_flag = VERITY_TABLE_OPT_RESTART;
  165. } else if (params->mode == VERITY_MODE_LOGGING) {
  166. mode_flag = VERITY_TABLE_OPT_LOGGING;
  167. }
  168. if (params->ecc.valid) {
  169. if (mode_flag) {
  170. res = snprintf(buf, bufsize,
  171. "%s %u %s " VERITY_TABLE_OPT_FEC_FORMAT,
  172. params->table, 1 + VERITY_TABLE_OPT_FEC_ARGS, mode_flag, params->ecc_dev,
  173. params->ecc.start / FEC_BLOCKSIZE, params->ecc.blocks, params->ecc.roots);
  174. } else {
  175. res = snprintf(buf, bufsize,
  176. "%s %u " VERITY_TABLE_OPT_FEC_FORMAT,
  177. params->table, VERITY_TABLE_OPT_FEC_ARGS, params->ecc_dev,
  178. params->ecc.start / FEC_BLOCKSIZE, params->ecc.blocks, params->ecc.roots);
  179. }
  180. } else if (mode_flag) {
  181. res = snprintf(buf, bufsize, "%s 2 " VERITY_TABLE_OPT_IGNZERO " %s", params->table,
  182. mode_flag);
  183. } else {
  184. res = snprintf(buf, bufsize, "%s 1 " VERITY_TABLE_OPT_IGNZERO, params->table);
  185. }
  186. if (res < 0 || (size_t)res >= bufsize) {
  187. LERROR << "Error building verity table; insufficient buffer size?";
  188. return false;
  189. }
  190. return true;
  191. }
  192. static bool format_legacy_verity_table(char *buf, const size_t bufsize,
  193. const struct verity_table_params *params)
  194. {
  195. int res;
  196. if (params->mode == VERITY_MODE_EIO) {
  197. res = strlcpy(buf, params->table, bufsize);
  198. } else {
  199. res = snprintf(buf, bufsize, "%s %d", params->table, params->mode);
  200. }
  201. if (res < 0 || (size_t)res >= bufsize) {
  202. LERROR << "Error building verity table; insufficient buffer size?";
  203. return false;
  204. }
  205. return true;
  206. }
  207. static int load_verity_table(android::dm::DeviceMapper& dm, const std::string& name,
  208. uint64_t device_size, const struct verity_table_params* params,
  209. format_verity_table_func format) {
  210. android::dm::DmTable table;
  211. table.set_readonly(true);
  212. char buffer[DM_BUF_SIZE];
  213. if (!format(buffer, sizeof(buffer), params)) {
  214. LERROR << "Failed to format verity parameters";
  215. return -1;
  216. }
  217. android::dm::DmTargetVerityString target(0, device_size / 512, buffer);
  218. if (!table.AddTarget(std::make_unique<decltype(target)>(target))) {
  219. LERROR << "Failed to add verity target";
  220. return -1;
  221. }
  222. if (!dm.CreateDevice(name, table)) {
  223. LERROR << "Failed to create verity device \"" << name << "\"";
  224. return -1;
  225. }
  226. return 0;
  227. }
  228. static int check_verity_restart(const char *fname)
  229. {
  230. char buffer[VERITY_KMSG_BUFSIZE + 1];
  231. int fd;
  232. int rc = 0;
  233. ssize_t size;
  234. struct stat s;
  235. fd = TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_CLOEXEC));
  236. if (fd == -1) {
  237. if (errno != ENOENT) {
  238. PERROR << "Failed to open " << fname;
  239. }
  240. goto out;
  241. }
  242. if (fstat(fd, &s) == -1) {
  243. PERROR << "Failed to fstat " << fname;
  244. goto out;
  245. }
  246. size = VERITY_KMSG_BUFSIZE;
  247. if (size > s.st_size) {
  248. size = s.st_size;
  249. }
  250. if (lseek(fd, s.st_size - size, SEEK_SET) == -1) {
  251. PERROR << "Failed to lseek " << (intmax_t)(s.st_size - size) << " " << fname;
  252. goto out;
  253. }
  254. if (!android::base::ReadFully(fd, buffer, size)) {
  255. PERROR << "Failed to read " << size << " bytes from " << fname;
  256. goto out;
  257. }
  258. buffer[size] = '\0';
  259. if (strstr(buffer, VERITY_KMSG_RESTART) != NULL) {
  260. rc = 1;
  261. }
  262. out:
  263. if (fd != -1) {
  264. close(fd);
  265. }
  266. return rc;
  267. }
  268. static int was_verity_restart()
  269. {
  270. static const char* files[] = {
  271. // clang-format off
  272. "/sys/fs/pstore/console-ramoops-0",
  273. "/sys/fs/pstore/console-ramoops",
  274. "/proc/last_kmsg",
  275. NULL
  276. // clang-format on
  277. };
  278. int i;
  279. for (i = 0; files[i]; ++i) {
  280. if (check_verity_restart(files[i])) {
  281. return 1;
  282. }
  283. }
  284. return 0;
  285. }
  286. static int metadata_add(FILE *fp, long start, const char *tag,
  287. unsigned int length, off64_t *offset)
  288. {
  289. if (fseek(fp, start, SEEK_SET) < 0 ||
  290. fprintf(fp, "%s %u\n", tag, length) < 0) {
  291. return -1;
  292. }
  293. *offset = ftell(fp);
  294. if (fseek(fp, length, SEEK_CUR) < 0 ||
  295. fprintf(fp, METADATA_EOD " 0\n") < 0) {
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. static int metadata_find(const char *fname, const char *stag,
  301. unsigned int slength, off64_t *offset)
  302. {
  303. char tag[METADATA_TAG_MAX_LENGTH + 1];
  304. int rc = -1;
  305. int n;
  306. long start = 0x4000; /* skip cryptfs metadata area */
  307. uint32_t magic;
  308. unsigned int length = 0;
  309. if (!fname) {
  310. return -1;
  311. }
  312. auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(fname, "re+"), fclose};
  313. if (!fp) {
  314. PERROR << "Failed to open " << fname;
  315. return -1;
  316. }
  317. /* check magic */
  318. if (fseek(fp.get(), start, SEEK_SET) < 0 || fread(&magic, sizeof(magic), 1, fp.get()) != 1) {
  319. PERROR << "Failed to read magic from " << fname;
  320. return -1;
  321. }
  322. if (magic != METADATA_MAGIC) {
  323. magic = METADATA_MAGIC;
  324. if (fseek(fp.get(), start, SEEK_SET) < 0 ||
  325. fwrite(&magic, sizeof(magic), 1, fp.get()) != 1) {
  326. PERROR << "Failed to write magic to " << fname;
  327. return -1;
  328. }
  329. rc = metadata_add(fp.get(), start + sizeof(magic), stag, slength, offset);
  330. if (rc < 0) {
  331. PERROR << "Failed to add metadata to " << fname;
  332. }
  333. return rc;
  334. }
  335. start += sizeof(magic);
  336. while (1) {
  337. n = fscanf(fp.get(), "%" STRINGIFY(METADATA_TAG_MAX_LENGTH) "s %u\n", tag, &length);
  338. if (n == 2 && strcmp(tag, METADATA_EOD)) {
  339. /* found a tag */
  340. start = ftell(fp.get());
  341. if (!strcmp(tag, stag) && length == slength) {
  342. *offset = start;
  343. return 0;
  344. }
  345. start += length;
  346. if (fseek(fp.get(), length, SEEK_CUR) < 0) {
  347. PERROR << "Failed to seek " << fname;
  348. return -1;
  349. }
  350. } else {
  351. rc = metadata_add(fp.get(), start, stag, slength, offset);
  352. if (rc < 0) {
  353. PERROR << "Failed to write metadata to " << fname;
  354. }
  355. return rc;
  356. }
  357. }
  358. }
  359. static int write_verity_state(const char *fname, off64_t offset, int32_t mode)
  360. {
  361. int fd;
  362. int rc = -1;
  363. struct verity_state s = { VERITY_STATE_HEADER, VERITY_STATE_VERSION, mode };
  364. fd = TEMP_FAILURE_RETRY(open(fname, O_WRONLY | O_SYNC | O_CLOEXEC));
  365. if (fd == -1) {
  366. PERROR << "Failed to open " << fname;
  367. goto out;
  368. }
  369. if (TEMP_FAILURE_RETRY(pwrite64(fd, &s, sizeof(s), offset)) != sizeof(s)) {
  370. PERROR << "Failed to write " << sizeof(s) << " bytes to " << fname
  371. << " to offset " << offset;
  372. goto out;
  373. }
  374. rc = 0;
  375. out:
  376. if (fd != -1) {
  377. close(fd);
  378. }
  379. return rc;
  380. }
  381. static int read_verity_state(const char *fname, off64_t offset, int *mode)
  382. {
  383. int fd = -1;
  384. int rc = -1;
  385. struct verity_state s;
  386. fd = TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_CLOEXEC));
  387. if (fd == -1) {
  388. PERROR << "Failed to open " << fname;
  389. goto out;
  390. }
  391. if (TEMP_FAILURE_RETRY(pread64(fd, &s, sizeof(s), offset)) != sizeof(s)) {
  392. PERROR << "Failed to read " << sizeof(s) << " bytes from " << fname
  393. << " offset " << offset;
  394. goto out;
  395. }
  396. if (s.header != VERITY_STATE_HEADER) {
  397. /* space allocated, but no state written. write default state */
  398. *mode = VERITY_MODE_DEFAULT;
  399. rc = write_verity_state(fname, offset, *mode);
  400. goto out;
  401. }
  402. if (s.version != VERITY_STATE_VERSION) {
  403. LERROR << "Unsupported verity state version (" << s.version << ")";
  404. goto out;
  405. }
  406. if (s.mode < VERITY_MODE_EIO ||
  407. s.mode > VERITY_MODE_LAST) {
  408. LERROR << "Unsupported verity mode (" << s.mode << ")";
  409. goto out;
  410. }
  411. *mode = s.mode;
  412. rc = 0;
  413. out:
  414. if (fd != -1) {
  415. close(fd);
  416. }
  417. return rc;
  418. }
  419. static int read_partition(const char *path, uint64_t size)
  420. {
  421. char buf[READ_BUF_SIZE];
  422. ssize_t size_read;
  423. android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)));
  424. if (fd == -1) {
  425. PERROR << "Failed to open " << path;
  426. return -errno;
  427. }
  428. while (size) {
  429. size_read = TEMP_FAILURE_RETRY(read(fd, buf, READ_BUF_SIZE));
  430. if (size_read == -1) {
  431. PERROR << "Error in reading partition " << path;
  432. return -errno;
  433. }
  434. size -= size_read;
  435. }
  436. return 0;
  437. }
  438. static int compare_last_signature(const FstabEntry& entry, int* match) {
  439. char tag[METADATA_TAG_MAX_LENGTH + 1];
  440. int fd = -1;
  441. int rc = -1;
  442. off64_t offset = 0;
  443. struct fec_handle *f = NULL;
  444. struct fec_verity_metadata verity;
  445. uint8_t curr[SHA256_DIGEST_LENGTH];
  446. uint8_t prev[SHA256_DIGEST_LENGTH];
  447. *match = 1;
  448. if (fec_open(&f, entry.blk_device.c_str(), O_RDONLY, FEC_VERITY_DISABLE, FEC_DEFAULT_ROOTS) ==
  449. -1) {
  450. PERROR << "Failed to open '" << entry.blk_device << "'";
  451. return rc;
  452. }
  453. // read verity metadata
  454. if (fec_verity_get_metadata(f, &verity) == -1) {
  455. PERROR << "Failed to get verity metadata '" << entry.blk_device << "'";
  456. goto out;
  457. }
  458. SHA256(verity.signature, sizeof(verity.signature), curr);
  459. if (snprintf(tag, sizeof(tag), VERITY_LASTSIG_TAG "_%s", basename(entry.mount_point.c_str())) >=
  460. (int)sizeof(tag)) {
  461. LERROR << "Metadata tag name too long for " << entry.mount_point;
  462. goto out;
  463. }
  464. if (metadata_find(entry.verity_loc.c_str(), tag, SHA256_DIGEST_LENGTH, &offset) < 0) {
  465. goto out;
  466. }
  467. fd = TEMP_FAILURE_RETRY(open(entry.verity_loc.c_str(), O_RDWR | O_SYNC | O_CLOEXEC));
  468. if (fd == -1) {
  469. PERROR << "Failed to open " << entry.verity_loc;
  470. goto out;
  471. }
  472. if (TEMP_FAILURE_RETRY(pread64(fd, prev, sizeof(prev), offset)) != sizeof(prev)) {
  473. PERROR << "Failed to read " << sizeof(prev) << " bytes from " << entry.verity_loc
  474. << " offset " << offset;
  475. goto out;
  476. }
  477. *match = !memcmp(curr, prev, SHA256_DIGEST_LENGTH);
  478. if (!*match) {
  479. /* update current signature hash */
  480. if (TEMP_FAILURE_RETRY(pwrite64(fd, curr, sizeof(curr),
  481. offset)) != sizeof(curr)) {
  482. PERROR << "Failed to write " << sizeof(curr) << " bytes to " << entry.verity_loc
  483. << " offset " << offset;
  484. goto out;
  485. }
  486. }
  487. rc = 0;
  488. out:
  489. fec_close(f);
  490. return rc;
  491. }
  492. static int get_verity_state_offset(const FstabEntry& entry, off64_t* offset) {
  493. char tag[METADATA_TAG_MAX_LENGTH + 1];
  494. if (snprintf(tag, sizeof(tag), VERITY_STATE_TAG "_%s", basename(entry.mount_point.c_str())) >=
  495. (int)sizeof(tag)) {
  496. LERROR << "Metadata tag name too long for " << entry.mount_point;
  497. return -1;
  498. }
  499. return metadata_find(entry.verity_loc.c_str(), tag, sizeof(struct verity_state), offset);
  500. }
  501. int load_verity_state(const FstabEntry& entry, int* mode) {
  502. // unless otherwise specified, use EIO mode.
  503. *mode = VERITY_MODE_EIO;
  504. // use the kernel parameter if set.
  505. std::string veritymode;
  506. if (fs_mgr_get_boot_config("veritymode", &veritymode)) {
  507. if (veritymode == "enforcing") {
  508. *mode = VERITY_MODE_DEFAULT;
  509. }
  510. return 0;
  511. }
  512. off64_t offset = 0;
  513. if (get_verity_state_offset(entry, &offset) < 0) {
  514. /* fall back to stateless behavior */
  515. return 0;
  516. }
  517. if (was_verity_restart()) {
  518. /* device was restarted after dm-verity detected a corrupted
  519. * block, so use EIO mode */
  520. return write_verity_state(entry.verity_loc.c_str(), offset, *mode);
  521. }
  522. int match = 0;
  523. if (!compare_last_signature(entry, &match) && !match) {
  524. /* partition has been reflashed, reset dm-verity state */
  525. *mode = VERITY_MODE_DEFAULT;
  526. return write_verity_state(entry.verity_loc.c_str(), offset, *mode);
  527. }
  528. return read_verity_state(entry.verity_loc.c_str(), offset, mode);
  529. }
  530. // Update the verity table using the actual block device path.
  531. // Two cases:
  532. // Case-1: verity table is shared for devices with different by-name prefix.
  533. // Example:
  534. // verity table token: /dev/block/bootdevice/by-name/vendor
  535. // blk_device-1 (non-A/B): /dev/block/platform/soc.0/7824900.sdhci/by-name/vendor
  536. // blk_device-2 (A/B): /dev/block/platform/soc.0/f9824900.sdhci/by-name/vendor_a
  537. //
  538. // Case-2: append A/B suffix in the verity table.
  539. // Example:
  540. // verity table token: /dev/block/platform/soc.0/7824900.sdhci/by-name/vendor
  541. // blk_device: /dev/block/platform/soc.0/7824900.sdhci/by-name/vendor_a
  542. static void update_verity_table_blk_device(const std::string& blk_device, char** table,
  543. bool slot_select) {
  544. bool updated = false;
  545. std::string result, ab_suffix;
  546. auto tokens = android::base::Split(*table, " ");
  547. // If slot_select is set, it means blk_device is already updated with ab_suffix.
  548. if (slot_select) ab_suffix = fs_mgr_get_slot_suffix();
  549. for (const auto& token : tokens) {
  550. std::string new_token;
  551. if (android::base::StartsWith(token, "/dev/block/")) {
  552. if (token == blk_device) return; // no need to update if they're already the same.
  553. std::size_t found1 = blk_device.find("by-name");
  554. std::size_t found2 = token.find("by-name");
  555. if (found1 != std::string::npos && found2 != std::string::npos &&
  556. blk_device.substr(found1) == token.substr(found2) + ab_suffix) {
  557. new_token = blk_device;
  558. }
  559. }
  560. if (!new_token.empty()) {
  561. updated = true;
  562. LINFO << "Verity table: updated block device from '" << token << "' to '" << new_token
  563. << "'";
  564. } else {
  565. new_token = token;
  566. }
  567. if (result.empty()) {
  568. result = new_token;
  569. } else {
  570. result += " " + new_token;
  571. }
  572. }
  573. if (!updated) {
  574. return;
  575. }
  576. free(*table);
  577. *table = strdup(result.c_str());
  578. }
  579. // prepares the verity enabled (MF_VERIFY / MF_VERIFYATBOOT) fstab record for
  580. // mount. The 'wait_for_verity_dev' parameter makes this function wait for the
  581. // verity device to get created before return
  582. int fs_mgr_setup_verity(FstabEntry* entry, bool wait_for_verity_dev) {
  583. int retval = FS_MGR_SETUP_VERITY_FAIL;
  584. int fd = -1;
  585. std::string verity_blk_name;
  586. struct fec_handle *f = NULL;
  587. struct fec_verity_metadata verity;
  588. struct verity_table_params params = { .table = NULL };
  589. const std::string mount_point(basename(entry->mount_point.c_str()));
  590. bool verified_at_boot = false;
  591. android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
  592. if (fec_open(&f, entry->blk_device.c_str(), O_RDONLY, FEC_VERITY_DISABLE, FEC_DEFAULT_ROOTS) <
  593. 0) {
  594. PERROR << "Failed to open '" << entry->blk_device << "'";
  595. return retval;
  596. }
  597. // read verity metadata
  598. if (fec_verity_get_metadata(f, &verity) < 0) {
  599. PERROR << "Failed to get verity metadata '" << entry->blk_device << "'";
  600. // Allow verity disabled when the device is unlocked without metadata
  601. if (fs_mgr_is_device_unlocked()) {
  602. retval = FS_MGR_SETUP_VERITY_SKIPPED;
  603. LWARNING << "Allow invalid metadata when the device is unlocked";
  604. }
  605. goto out;
  606. }
  607. #ifdef ALLOW_ADBD_DISABLE_VERITY
  608. if (verity.disabled) {
  609. retval = FS_MGR_SETUP_VERITY_DISABLED;
  610. LINFO << "Attempt to cleanly disable verity - only works in USERDEBUG/ENG";
  611. goto out;
  612. }
  613. #endif
  614. // read ecc metadata
  615. if (fec_ecc_get_metadata(f, &params.ecc) < 0) {
  616. params.ecc.valid = false;
  617. }
  618. params.ecc_dev = entry->blk_device.c_str();
  619. if (load_verity_state(*entry, &params.mode) < 0) {
  620. /* if accessing or updating the state failed, switch to the default
  621. * safe mode. This makes sure the device won't end up in an endless
  622. * restart loop, and no corrupted data will be exposed to userspace
  623. * without a warning. */
  624. params.mode = VERITY_MODE_EIO;
  625. }
  626. if (!verity.table) {
  627. goto out;
  628. }
  629. params.table = strdup(verity.table);
  630. if (!params.table) {
  631. goto out;
  632. }
  633. // verify the signature on the table
  634. if (verify_verity_signature(verity) < 0) {
  635. // Allow signature verification error when the device is unlocked
  636. if (fs_mgr_is_device_unlocked()) {
  637. retval = FS_MGR_SETUP_VERITY_SKIPPED;
  638. LWARNING << "Allow signature verification error when the device is unlocked";
  639. goto out;
  640. }
  641. if (params.mode == VERITY_MODE_LOGGING) {
  642. // the user has been warned, allow mounting without dm-verity
  643. retval = FS_MGR_SETUP_VERITY_SKIPPED;
  644. goto out;
  645. }
  646. // invalidate root hash and salt to trigger device-specific recovery
  647. if (invalidate_table(params.table, verity.table_length) < 0) {
  648. goto out;
  649. }
  650. }
  651. LINFO << "Enabling dm-verity for " << mount_point.c_str()
  652. << " (mode " << params.mode << ")";
  653. // Update the verity params using the actual block device path
  654. update_verity_table_blk_device(entry->blk_device, &params.table,
  655. entry->fs_mgr_flags.slot_select);
  656. // load the verity mapping table
  657. if (load_verity_table(dm, mount_point, verity.data_size, &params, format_verity_table) == 0) {
  658. goto loaded;
  659. }
  660. if (params.ecc.valid) {
  661. // kernel may not support error correction, try without
  662. LINFO << "Disabling error correction for " << mount_point.c_str();
  663. params.ecc.valid = false;
  664. if (load_verity_table(dm, mount_point, verity.data_size, &params, format_verity_table) == 0) {
  665. goto loaded;
  666. }
  667. }
  668. // try the legacy format for backwards compatibility
  669. if (load_verity_table(dm, mount_point, verity.data_size, &params, format_legacy_verity_table) ==
  670. 0) {
  671. goto loaded;
  672. }
  673. if (params.mode != VERITY_MODE_EIO) {
  674. // as a last resort, EIO mode should always be supported
  675. LINFO << "Falling back to EIO mode for " << mount_point.c_str();
  676. params.mode = VERITY_MODE_EIO;
  677. if (load_verity_table(dm, mount_point, verity.data_size, &params,
  678. format_legacy_verity_table) == 0) {
  679. goto loaded;
  680. }
  681. }
  682. LERROR << "Failed to load verity table for " << mount_point.c_str();
  683. goto out;
  684. loaded:
  685. if (!dm.GetDmDevicePathByName(mount_point, &verity_blk_name)) {
  686. LERROR << "Couldn't get verity device number!";
  687. goto out;
  688. }
  689. // mark the underlying block device as read-only
  690. fs_mgr_set_blk_ro(entry->blk_device);
  691. // Verify the entire partition in one go
  692. // If there is an error, allow it to mount as a normal verity partition.
  693. if (entry->fs_mgr_flags.verify_at_boot) {
  694. LINFO << "Verifying partition " << entry->blk_device << " at boot";
  695. int err = read_partition(verity_blk_name.c_str(), verity.data_size);
  696. if (!err) {
  697. LINFO << "Verified verity partition " << entry->blk_device << " at boot";
  698. verified_at_boot = true;
  699. }
  700. }
  701. // assign the new verity block device as the block device
  702. if (!verified_at_boot) {
  703. entry->blk_device = verity_blk_name;
  704. } else if (!dm.DeleteDevice(mount_point)) {
  705. LERROR << "Failed to remove verity device " << mount_point.c_str();
  706. goto out;
  707. }
  708. // make sure we've set everything up properly
  709. if (wait_for_verity_dev && !fs_mgr_wait_for_file(entry->blk_device, 1s)) {
  710. goto out;
  711. }
  712. retval = FS_MGR_SETUP_VERITY_SUCCESS;
  713. out:
  714. if (fd != -1) {
  715. close(fd);
  716. }
  717. fec_close(f);
  718. free(params.table);
  719. return retval;
  720. }
  721. bool fs_mgr_teardown_verity(FstabEntry* entry, bool wait) {
  722. const std::string mount_point(basename(entry->mount_point.c_str()));
  723. if (!android::fs_mgr::UnmapDevice(mount_point, wait ? 1000ms : 0ms)) {
  724. return false;
  725. }
  726. LINFO << "Unmapped verity device " << mount_point;
  727. return true;
  728. }