2 * Copyright (C) 2015 Google, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/buffer_head.h>
16 #include <linux/debugfs.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/device-mapper.h>
20 #include <linux/errno.h>
22 #include <linux/fcntl.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/key.h>
26 #include <linux/module.h>
27 #include <linux/mount.h>
28 #include <linux/namei.h>
30 #include <linux/reboot.h>
31 #include <linux/string.h>
32 #include <linux/vmalloc.h>
34 #include <asm/setup.h>
35 #include <crypto/hash.h>
36 #include <crypto/public_key.h>
37 #include <crypto/sha.h>
38 #include <keys/asymmetric-type.h>
39 #include <keys/system_keyring.h>
41 #include "dm-verity.h"
42 #include "dm-android-verity.h"
44 static char verifiedbootstate[VERITY_COMMANDLINE_PARAM_LENGTH];
45 static char veritymode[VERITY_COMMANDLINE_PARAM_LENGTH];
47 static bool target_added;
48 static bool verity_enabled = true;
49 struct dentry *debug_dir;
50 static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv);
52 static struct target_type android_verity_target = {
53 .name = "android-verity",
55 .module = THIS_MODULE,
56 .ctr = android_verity_ctr,
59 .status = verity_status,
60 .ioctl = verity_ioctl,
61 .merge = verity_merge,
62 .iterate_devices = verity_iterate_devices,
63 .io_hints = verity_io_hints,
66 static int __init verified_boot_state_param(char *line)
68 strlcpy(verifiedbootstate, line, sizeof(verifiedbootstate));
72 __setup("androidboot.verifiedbootstate=", verified_boot_state_param);
74 static int __init verity_mode_param(char *line)
76 strlcpy(veritymode, line, sizeof(veritymode));
80 __setup("androidboot.veritymode=", verity_mode_param);
82 static int table_extract_mpi_array(struct public_key_signature *pks,
83 const void *data, size_t len)
85 MPI mpi = mpi_read_raw_data(data, len);
88 DMERR("Error while allocating mpi array");
97 static struct public_key_signature *table_make_digest(
100 unsigned long table_len)
102 struct public_key_signature *pks = NULL;
103 struct crypto_shash *tfm;
104 struct shash_desc *desc;
105 size_t digest_size, desc_size;
108 /* Allocate the hashing algorithm we're going to need and find out how
109 * big the hash operational data will be.
111 tfm = crypto_alloc_shash(hash_algo_name[hash], 0, 0);
113 return ERR_CAST(tfm);
115 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
116 digest_size = crypto_shash_digestsize(tfm);
118 /* We allocate the hash operational data storage on the end of out
119 * context data and the digest output buffer on the end of that.
122 pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL);
126 pks->pkey_hash_algo = hash;
127 pks->digest = (u8 *)pks + sizeof(*pks) + desc_size;
128 pks->digest_size = digest_size;
130 desc = (struct shash_desc *)(pks + 1);
132 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
134 ret = crypto_shash_init(desc);
138 ret = crypto_shash_finup(desc, table, table_len, pks->digest);
142 crypto_free_shash(tfm);
147 crypto_free_shash(tfm);
151 static int read_block_dev(struct bio_read *payload, struct block_device *bdev,
152 sector_t offset, int length)
157 payload->number_of_pages = DIV_ROUND_UP(length, PAGE_SIZE);
159 bio = bio_alloc(GFP_KERNEL, payload->number_of_pages);
161 DMERR("Error while allocating bio");
166 bio->bi_iter.bi_sector = offset;
168 payload->page_io = kzalloc(sizeof(struct page *) *
169 payload->number_of_pages, GFP_KERNEL);
170 if (!payload->page_io) {
171 DMERR("page_io array alloc failed");
176 for (i = 0; i < payload->number_of_pages; i++) {
177 payload->page_io[i] = alloc_page(GFP_KERNEL);
178 if (!payload->page_io[i]) {
179 DMERR("alloc_page failed");
183 if (!bio_add_page(bio, payload->page_io[i], PAGE_SIZE, 0)) {
184 DMERR("bio_add_page error");
190 if (!submit_bio_wait(READ, bio))
193 DMERR("bio read failed");
197 for (i = 0; i < payload->number_of_pages; i++)
198 if (payload->page_io[i])
199 __free_page(payload->page_io[i]);
200 kfree(payload->page_io);
206 static inline u64 fec_div_round_up(u64 x, u64 y)
210 return div64_u64_rem(x, y, &remainder) +
211 (remainder > 0 ? 1 : 0);
214 static inline void populate_fec_metadata(struct fec_header *header,
215 struct fec_ecc_metadata *ecc)
217 ecc->blocks = fec_div_round_up(le64_to_cpu(header->inp_size),
219 ecc->roots = le32_to_cpu(header->roots);
220 ecc->start = le64_to_cpu(header->inp_size);
223 static inline int validate_fec_header(struct fec_header *header, u64 offset)
225 /* move offset to make the sanity check work for backup header
227 offset -= offset % FEC_BLOCK_SIZE;
228 if (le32_to_cpu(header->magic) != FEC_MAGIC ||
229 le32_to_cpu(header->version) != FEC_VERSION ||
230 le32_to_cpu(header->size) != sizeof(struct fec_header) ||
231 le32_to_cpu(header->roots) == 0 ||
232 le32_to_cpu(header->roots) >= FEC_RSM ||
233 offset < le32_to_cpu(header->fec_size) ||
234 offset - le32_to_cpu(header->fec_size) !=
235 le64_to_cpu(header->inp_size))
241 static int extract_fec_header(dev_t dev, struct fec_header *fec,
242 struct fec_ecc_metadata *ecc)
245 struct bio_read payload;
247 struct block_device *bdev;
249 bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
252 DMERR("bdev get error");
253 return PTR_ERR(bdev);
256 device_size = i_size_read(bdev->bd_inode);
258 /* fec metadata size is a power of 2 and PAGE_SIZE
259 * is a power of 2 as well.
261 BUG_ON(FEC_BLOCK_SIZE > PAGE_SIZE);
262 /* 512 byte sector alignment */
263 BUG_ON(((device_size - FEC_BLOCK_SIZE) % (1 << SECTOR_SHIFT)) != 0);
265 err = read_block_dev(&payload, bdev, (device_size -
266 FEC_BLOCK_SIZE) / (1 << SECTOR_SHIFT), FEC_BLOCK_SIZE);
268 DMERR("Error while reading verity metadata");
272 BUG_ON(sizeof(struct fec_header) > PAGE_SIZE);
273 memcpy(fec, page_address(payload.page_io[0]),
277 if (validate_fec_header(fec, device_size - FEC_BLOCK_SIZE)) {
278 /* Try the backup header */
279 memcpy(fec, page_address(payload.page_io[0]) + FEC_BLOCK_SIZE
282 if (validate_fec_header(fec, device_size -
283 sizeof(struct fec_header)))
288 populate_fec_metadata(fec, ecc);
290 for (i = 0; i < payload.number_of_pages; i++)
291 __free_page(payload.page_io[i]);
292 kfree(payload.page_io);
295 blkdev_put(bdev, FMODE_READ);
298 static void find_metadata_offset(struct fec_header *fec,
299 struct block_device *bdev, u64 *metadata_offset)
303 device_size = i_size_read(bdev->bd_inode);
305 if (le32_to_cpu(fec->magic) == FEC_MAGIC)
306 *metadata_offset = le64_to_cpu(fec->inp_size) -
307 VERITY_METADATA_SIZE;
309 *metadata_offset = device_size - VERITY_METADATA_SIZE;
312 static struct android_metadata *extract_metadata(dev_t dev,
313 struct fec_header *fec)
315 struct block_device *bdev;
316 struct android_metadata_header *header;
317 struct android_metadata *uninitialized_var(metadata);
319 u32 table_length, copy_length, offset;
321 struct bio_read payload;
324 bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
327 DMERR("blkdev_get_by_dev failed");
328 return ERR_CAST(bdev);
331 find_metadata_offset(fec, bdev, &metadata_offset);
333 /* Verity metadata size is a power of 2 and PAGE_SIZE
334 * is a power of 2 as well.
335 * PAGE_SIZE is also a multiple of 512 bytes.
337 if (VERITY_METADATA_SIZE > PAGE_SIZE)
338 BUG_ON(VERITY_METADATA_SIZE % PAGE_SIZE != 0);
339 /* 512 byte sector alignment */
340 BUG_ON(metadata_offset % (1 << SECTOR_SHIFT) != 0);
342 err = read_block_dev(&payload, bdev, metadata_offset /
343 (1 << SECTOR_SHIFT), VERITY_METADATA_SIZE);
345 DMERR("Error while reading verity metadata");
346 metadata = ERR_PTR(err);
350 header = kzalloc(sizeof(*header), GFP_KERNEL);
352 DMERR("kzalloc failed for header");
357 memcpy(header, page_address(payload.page_io[0]),
360 DMINFO("bio magic_number:%u protocol_version:%d table_length:%u",
361 le32_to_cpu(header->magic_number),
362 le32_to_cpu(header->protocol_version),
363 le32_to_cpu(header->table_length));
365 metadata = kzalloc(sizeof(*metadata), GFP_KERNEL);
367 DMERR("kzalloc for metadata failed");
372 metadata->header = header;
373 table_length = le32_to_cpu(header->table_length);
375 if (table_length == 0 ||
376 table_length > (VERITY_METADATA_SIZE -
377 sizeof(struct android_metadata_header)))
380 metadata->verity_table = kzalloc(table_length + 1, GFP_KERNEL);
382 if (!metadata->verity_table) {
383 DMERR("kzalloc verity_table failed");
388 if (sizeof(struct android_metadata_header) +
389 table_length <= PAGE_SIZE) {
390 memcpy(metadata->verity_table, page_address(payload.page_io[0])
391 + sizeof(struct android_metadata_header),
394 copy_length = PAGE_SIZE -
395 sizeof(struct android_metadata_header);
396 memcpy(metadata->verity_table, page_address(payload.page_io[0])
397 + sizeof(struct android_metadata_header),
399 table_length -= copy_length;
400 offset = copy_length;
402 while (table_length != 0) {
403 if (table_length > PAGE_SIZE) {
404 memcpy(metadata->verity_table + offset,
405 page_address(payload.page_io[i]),
408 table_length -= PAGE_SIZE;
410 memcpy(metadata->verity_table + offset,
411 page_address(payload.page_io[i]),
418 metadata->verity_table[table_length] = '\0';
426 metadata = ERR_PTR(err);
428 for (i = 0; i < payload.number_of_pages; i++)
429 if (payload.page_io[i])
430 __free_page(payload.page_io[i]);
431 kfree(payload.page_io);
433 DMINFO("verity_table: %s", metadata->verity_table);
435 blkdev_put(bdev, FMODE_READ);
439 /* helper functions to extract properties from dts */
440 const char *find_dt_value(const char *name)
442 struct device_node *firmware;
445 firmware = of_find_node_by_path("/firmware/android");
448 value = of_get_property(firmware, name, NULL);
449 of_node_put(firmware);
454 static bool is_unlocked(void)
456 static const char unlocked[] = "orange";
457 static const char verified_boot_prop[] = "verifiedbootstate";
460 value = find_dt_value(verified_boot_prop);
462 value = verifiedbootstate;
464 return !strncmp(value, unlocked, sizeof(unlocked) - 1);
467 static int verity_mode(void)
469 static const char enforcing[] = "enforcing";
470 static const char verified_mode_prop[] = "veritymode";
473 value = find_dt_value(verified_mode_prop);
476 if (!strncmp(value, enforcing, sizeof(enforcing) - 1))
477 return DM_VERITY_MODE_RESTART;
479 return DM_VERITY_MODE_EIO;
482 static int verify_header(struct android_metadata_header *header)
484 int retval = -EINVAL;
486 if (is_unlocked() && le32_to_cpu(header->magic_number) ==
487 VERITY_METADATA_MAGIC_DISABLE) {
488 retval = VERITY_STATE_DISABLE;
492 if (!(le32_to_cpu(header->magic_number) ==
493 VERITY_METADATA_MAGIC_NUMBER) ||
494 (le32_to_cpu(header->magic_number) ==
495 VERITY_METADATA_MAGIC_DISABLE)) {
496 DMERR("Incorrect magic number");
500 if (le32_to_cpu(header->protocol_version) !=
501 VERITY_METADATA_VERSION) {
502 DMERR("Unsupported version %u",
503 le32_to_cpu(header->protocol_version));
510 static int verify_verity_signature(char *key_id,
511 struct android_metadata *metadata)
515 struct public_key_signature *pks = NULL;
516 int retval = -EINVAL;
518 key_ref = keyring_search(make_key_ref(system_trusted_keyring, 1),
519 &key_type_asymmetric, key_id);
521 if (IS_ERR(key_ref)) {
522 DMERR("keyring: key not found");
526 key = key_ref_to_ptr(key_ref);
528 pks = table_make_digest(HASH_ALGO_SHA256,
529 (const void *)metadata->verity_table,
530 le32_to_cpu(metadata->header->table_length));
533 DMERR("hashing failed");
537 retval = table_extract_mpi_array(pks, &metadata->header->signature[0],
540 DMERR("Error extracting mpi %d", retval);
544 retval = verify_signature(key, pks);
545 mpi_free(pks->rsa.s);
553 static void handle_error(void)
555 int mode = verity_mode();
556 if (mode == DM_VERITY_MODE_RESTART) {
557 DMERR("triggering restart");
558 kernel_restart("dm-verity device corrupted");
560 DMERR("Mounting verity root failed");
564 static inline bool test_mult_overflow(sector_t a, u32 b)
566 sector_t r = (sector_t)~0ULL;
572 static int add_as_linear_device(struct dm_target *ti, char *dev)
574 /*Move to linear mapping defines*/
575 char *linear_table_args[DM_LINEAR_ARGS] = {dev,
576 DM_LINEAR_TARGET_OFFSET};
579 android_verity_target.dtr = dm_linear_dtr,
580 android_verity_target.map = dm_linear_map,
581 android_verity_target.status = dm_linear_status,
582 android_verity_target.ioctl = dm_linear_ioctl,
583 android_verity_target.merge = dm_linear_merge,
584 android_verity_target.iterate_devices = dm_linear_iterate_devices,
585 android_verity_target.io_hints = NULL;
587 err = dm_linear_ctr(ti, DM_LINEAR_ARGS, linear_table_args);
590 DMINFO("Added android-verity as a linear target");
593 DMERR("Failed to add android-verity as linear target");
600 * <key id> Key id of the public key in the system keyring.
601 * Verity metadata's signature would be verified against
602 * this. If the key id contains spaces, replace them
604 * <block device> The block device for which dm-verity is being setup.
606 static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
608 dev_t uninitialized_var(dev);
609 struct android_metadata *uninitialized_var(metadata);
610 int err = 0, i, mode;
611 char *key_id, *table_ptr, dummy,
612 *verity_table_args[VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS];
613 /* One for specifying number of opt args and one for mode */
614 sector_t data_sectors;
616 unsigned int no_of_args = VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS;
617 struct fec_header uninitialized_var(fec);
618 struct fec_ecc_metadata uninitialized_var(ecc);
619 char buf[FEC_ARG_LENGTH], *buf_ptr;
620 unsigned long long tmpll;
623 DMERR("Incorrect number of arguments");
628 /* should come as one of the arguments for the verity target */
630 strreplace(argv[0], '#', ' ');
632 dev = name_to_dev_t(argv[1]);
634 DMERR("no dev found for %s", argv[1]);
639 DMINFO("key:%s dev:%s", argv[0], argv[1]);
641 if (extract_fec_header(dev, &fec, &ecc)) {
642 DMERR("Error while extracting fec header");
647 metadata = extract_metadata(dev, &fec);
649 if (IS_ERR(metadata)) {
650 DMERR("Error while extracting metadata");
655 err = verify_header(metadata->header);
657 if (err == VERITY_STATE_DISABLE) {
658 DMERR("Mounting root with verity disabled");
659 verity_enabled = false;
660 /* we would still have to parse the args to figure out
661 * the data blocks size. Or may be could map the entire
662 * partition similar to mounting the device.
665 DMERR("Verity header handle error");
670 if (verity_enabled) {
671 err = verify_verity_signature(key_id, metadata);
674 DMERR("Signature verification failed");
678 DMINFO("Signature verification success");
681 table_ptr = metadata->verity_table;
683 for (i = 0; i < VERITY_TABLE_ARGS; i++) {
684 verity_table_args[i] = strsep(&table_ptr, " ");
685 if (verity_table_args[i] == NULL)
689 if (i != VERITY_TABLE_ARGS) {
690 DMERR("Verity table not in the expected format");
696 if (sscanf(verity_table_args[5], "%llu%c", &tmpll, &dummy)
698 DMERR("Verity table not in the expected format");
704 if (tmpll > ULONG_MAX) {
705 DMERR("<num_data_blocks> too large. Forgot to turn on CONFIG_LBDAF?");
711 data_sectors = tmpll;
713 if (sscanf(verity_table_args[3], "%u%c", &data_block_size, &dummy)
715 DMERR("Verity table not in the expected format");
721 if (test_mult_overflow(data_sectors, data_block_size >>
723 DMERR("data_sectors too large");
729 data_sectors *= data_block_size >> SECTOR_SHIFT;
730 DMINFO("Data sectors %llu", (unsigned long long)data_sectors);
732 /* update target length */
733 ti->len = data_sectors;
735 /* Setup linear target and free */
736 if (!verity_enabled) {
737 err = add_as_linear_device(ti, argv[1]);
741 /*substitute data_dev and hash_dev*/
742 verity_table_args[1] = argv[1];
743 verity_table_args[2] = argv[1];
745 mode = verity_mode();
747 if (ecc.valid && IS_BUILTIN(CONFIG_DM_VERITY_FEC)) {
749 err = snprintf(buf, FEC_ARG_LENGTH,
750 "%u %s " VERITY_TABLE_OPT_FEC_FORMAT,
751 1 + VERITY_TABLE_OPT_FEC_ARGS,
752 mode == DM_VERITY_MODE_RESTART ?
753 VERITY_TABLE_OPT_RESTART : VERITY_TABLE_OPT_LOGGING,
754 argv[1], ecc.start / FEC_BLOCK_SIZE, ecc.blocks,
757 err = snprintf(buf, FEC_ARG_LENGTH,
758 "%u " VERITY_TABLE_OPT_FEC_FORMAT,
759 VERITY_TABLE_OPT_FEC_ARGS, argv[1],
760 ecc.start / FEC_BLOCK_SIZE, ecc.blocks, ecc.roots);
763 err = snprintf(buf, FEC_ARG_LENGTH,
764 "2 " VERITY_TABLE_OPT_IGNZERO " %s",
765 mode == DM_VERITY_MODE_RESTART ?
766 VERITY_TABLE_OPT_RESTART : VERITY_TABLE_OPT_LOGGING);
768 err = snprintf(buf, FEC_ARG_LENGTH, "1 %s",
769 "ignore_zero_blocks");
772 if (err < 0 || err >= FEC_ARG_LENGTH)
777 for (i = VERITY_TABLE_ARGS; i < (VERITY_TABLE_ARGS +
778 VERITY_TABLE_OPT_FEC_ARGS + 2); i++) {
779 verity_table_args[i] = strsep(&buf_ptr, " ");
780 if (verity_table_args[i] == NULL) {
786 err = verity_ctr(ti, no_of_args, verity_table_args);
789 DMERR("android-verity failed to mount as verity target");
792 DMINFO("android-verity mounted as verity target");
796 kfree(metadata->header);
797 kfree(metadata->verity_table);
802 static int __init dm_android_verity_init(void)
807 r = dm_register_target(&android_verity_target);
809 DMERR("register failed %d", r);
811 /* Tracks the status of the last added target */
812 debug_dir = debugfs_create_dir("android_verity", NULL);
814 if (IS_ERR_OR_NULL(debug_dir)) {
815 DMERR("Cannot create android_verity debugfs directory: %ld",
820 file = debugfs_create_bool("target_added", S_IRUGO, debug_dir,
821 (u32 *)&target_added);
823 if (IS_ERR_OR_NULL(file)) {
824 DMERR("Cannot create android_verity debugfs directory: %ld",
826 debugfs_remove_recursive(debug_dir);
830 file = debugfs_create_bool("verity_enabled", S_IRUGO, debug_dir,
831 (u32 *)&verity_enabled);
833 if (IS_ERR_OR_NULL(file)) {
834 DMERR("Cannot create android_verity debugfs directory: %ld",
836 debugfs_remove_recursive(debug_dir);
843 static void __exit dm_android_verity_exit(void)
845 if (!IS_ERR_OR_NULL(debug_dir))
846 debugfs_remove_recursive(debug_dir);
848 dm_unregister_target(&android_verity_target);
851 module_init(dm_android_verity_init);
852 module_exit(dm_android_verity_exit);