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];
46 static char veritykeyid[VERITY_DEFAULT_KEY_ID_LENGTH];
47 static char buildvariant[BUILD_VARIANT];
49 static bool target_added;
50 static bool verity_enabled = true;
51 struct dentry *debug_dir;
52 static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv);
54 static struct target_type android_verity_target = {
55 .name = "android-verity",
57 .module = THIS_MODULE,
58 .ctr = android_verity_ctr,
61 .status = verity_status,
62 .ioctl = verity_ioctl,
63 .merge = verity_merge,
64 .iterate_devices = verity_iterate_devices,
65 .io_hints = verity_io_hints,
68 static int __init verified_boot_state_param(char *line)
70 strlcpy(verifiedbootstate, line, sizeof(verifiedbootstate));
74 __setup("androidboot.verifiedbootstate=", verified_boot_state_param);
76 static int __init verity_mode_param(char *line)
78 strlcpy(veritymode, line, sizeof(veritymode));
82 __setup("androidboot.veritymode=", verity_mode_param);
84 static int __init verity_keyid_param(char *line)
86 strlcpy(veritykeyid, line, sizeof(veritykeyid));
90 __setup("veritykeyid=", verity_keyid_param);
92 static int __init verity_buildvariant(char *line)
94 strlcpy(buildvariant, line, sizeof(buildvariant));
98 __setup("buildvariant=", verity_buildvariant);
100 static inline bool default_verity_key_id(void)
102 return veritykeyid[0] != '\0';
105 static inline bool is_eng(void)
107 static const char typeeng[] = "eng";
109 return !strncmp(buildvariant, typeeng, sizeof(typeeng));
112 static int table_extract_mpi_array(struct public_key_signature *pks,
113 const void *data, size_t len)
115 MPI mpi = mpi_read_raw_data(data, len);
118 DMERR("Error while allocating mpi array");
127 static struct public_key_signature *table_make_digest(
130 unsigned long table_len)
132 struct public_key_signature *pks = NULL;
133 struct crypto_shash *tfm;
134 struct shash_desc *desc;
135 size_t digest_size, desc_size;
138 /* Allocate the hashing algorithm we're going to need and find out how
139 * big the hash operational data will be.
141 tfm = crypto_alloc_shash(hash_algo_name[hash], 0, 0);
143 return ERR_CAST(tfm);
145 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
146 digest_size = crypto_shash_digestsize(tfm);
148 /* We allocate the hash operational data storage on the end of out
149 * context data and the digest output buffer on the end of that.
152 pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL);
156 pks->pkey_hash_algo = hash;
157 pks->digest = (u8 *)pks + sizeof(*pks) + desc_size;
158 pks->digest_size = digest_size;
160 desc = (struct shash_desc *)(pks + 1);
162 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
164 ret = crypto_shash_init(desc);
168 ret = crypto_shash_finup(desc, table, table_len, pks->digest);
172 crypto_free_shash(tfm);
177 crypto_free_shash(tfm);
181 static int read_block_dev(struct bio_read *payload, struct block_device *bdev,
182 sector_t offset, int length)
187 payload->number_of_pages = DIV_ROUND_UP(length, PAGE_SIZE);
189 bio = bio_alloc(GFP_KERNEL, payload->number_of_pages);
191 DMERR("Error while allocating bio");
196 bio->bi_iter.bi_sector = offset;
198 payload->page_io = kzalloc(sizeof(struct page *) *
199 payload->number_of_pages, GFP_KERNEL);
200 if (!payload->page_io) {
201 DMERR("page_io array alloc failed");
206 for (i = 0; i < payload->number_of_pages; i++) {
207 payload->page_io[i] = alloc_page(GFP_KERNEL);
208 if (!payload->page_io[i]) {
209 DMERR("alloc_page failed");
213 if (!bio_add_page(bio, payload->page_io[i], PAGE_SIZE, 0)) {
214 DMERR("bio_add_page error");
220 if (!submit_bio_wait(READ, bio))
223 DMERR("bio read failed");
227 for (i = 0; i < payload->number_of_pages; i++)
228 if (payload->page_io[i])
229 __free_page(payload->page_io[i]);
230 kfree(payload->page_io);
236 static inline u64 fec_div_round_up(u64 x, u64 y)
240 return div64_u64_rem(x, y, &remainder) +
241 (remainder > 0 ? 1 : 0);
244 static inline void populate_fec_metadata(struct fec_header *header,
245 struct fec_ecc_metadata *ecc)
247 ecc->blocks = fec_div_round_up(le64_to_cpu(header->inp_size),
249 ecc->roots = le32_to_cpu(header->roots);
250 ecc->start = le64_to_cpu(header->inp_size);
253 static inline int validate_fec_header(struct fec_header *header, u64 offset)
255 /* move offset to make the sanity check work for backup header
257 offset -= offset % FEC_BLOCK_SIZE;
258 if (le32_to_cpu(header->magic) != FEC_MAGIC ||
259 le32_to_cpu(header->version) != FEC_VERSION ||
260 le32_to_cpu(header->size) != sizeof(struct fec_header) ||
261 le32_to_cpu(header->roots) == 0 ||
262 le32_to_cpu(header->roots) >= FEC_RSM ||
263 offset < le32_to_cpu(header->fec_size) ||
264 offset - le32_to_cpu(header->fec_size) !=
265 le64_to_cpu(header->inp_size))
271 static int extract_fec_header(dev_t dev, struct fec_header *fec,
272 struct fec_ecc_metadata *ecc)
275 struct bio_read payload;
277 struct block_device *bdev;
279 bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
281 if (IS_ERR_OR_NULL(bdev)) {
282 DMERR("bdev get error");
283 return PTR_ERR(bdev);
286 device_size = i_size_read(bdev->bd_inode);
288 /* fec metadata size is a power of 2 and PAGE_SIZE
289 * is a power of 2 as well.
291 BUG_ON(FEC_BLOCK_SIZE > PAGE_SIZE);
292 /* 512 byte sector alignment */
293 BUG_ON(((device_size - FEC_BLOCK_SIZE) % (1 << SECTOR_SHIFT)) != 0);
295 err = read_block_dev(&payload, bdev, (device_size -
296 FEC_BLOCK_SIZE) / (1 << SECTOR_SHIFT), FEC_BLOCK_SIZE);
298 DMERR("Error while reading verity metadata");
302 BUG_ON(sizeof(struct fec_header) > PAGE_SIZE);
303 memcpy(fec, page_address(payload.page_io[0]),
307 if (validate_fec_header(fec, device_size - FEC_BLOCK_SIZE)) {
308 /* Try the backup header */
309 memcpy(fec, page_address(payload.page_io[0]) + FEC_BLOCK_SIZE
312 if (validate_fec_header(fec, device_size -
313 sizeof(struct fec_header)))
318 populate_fec_metadata(fec, ecc);
320 for (i = 0; i < payload.number_of_pages; i++)
321 __free_page(payload.page_io[i]);
322 kfree(payload.page_io);
325 blkdev_put(bdev, FMODE_READ);
328 static void find_metadata_offset(struct fec_header *fec,
329 struct block_device *bdev, u64 *metadata_offset)
333 device_size = i_size_read(bdev->bd_inode);
335 if (le32_to_cpu(fec->magic) == FEC_MAGIC)
336 *metadata_offset = le64_to_cpu(fec->inp_size) -
337 VERITY_METADATA_SIZE;
339 *metadata_offset = device_size - VERITY_METADATA_SIZE;
342 static int find_size(dev_t dev, u64 *device_size)
344 struct block_device *bdev;
346 bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
347 if (IS_ERR_OR_NULL(bdev)) {
348 DMERR("blkdev_get_by_dev failed");
349 return PTR_ERR(bdev);
352 *device_size = i_size_read(bdev->bd_inode);
353 *device_size >>= SECTOR_SHIFT;
355 DMINFO("blkdev size in sectors: %llu", *device_size);
356 blkdev_put(bdev, FMODE_READ);
360 static struct android_metadata *extract_metadata(dev_t dev,
361 struct fec_header *fec)
363 struct block_device *bdev;
364 struct android_metadata_header *header;
365 struct android_metadata *uninitialized_var(metadata);
367 u32 table_length, copy_length, offset;
369 struct bio_read payload;
372 bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
374 if (IS_ERR_OR_NULL(bdev)) {
375 DMERR("blkdev_get_by_dev failed");
376 return ERR_CAST(bdev);
379 find_metadata_offset(fec, bdev, &metadata_offset);
381 /* Verity metadata size is a power of 2 and PAGE_SIZE
382 * is a power of 2 as well.
383 * PAGE_SIZE is also a multiple of 512 bytes.
385 if (VERITY_METADATA_SIZE > PAGE_SIZE)
386 BUG_ON(VERITY_METADATA_SIZE % PAGE_SIZE != 0);
387 /* 512 byte sector alignment */
388 BUG_ON(metadata_offset % (1 << SECTOR_SHIFT) != 0);
390 err = read_block_dev(&payload, bdev, metadata_offset /
391 (1 << SECTOR_SHIFT), VERITY_METADATA_SIZE);
393 DMERR("Error while reading verity metadata");
394 metadata = ERR_PTR(err);
398 header = kzalloc(sizeof(*header), GFP_KERNEL);
400 DMERR("kzalloc failed for header");
405 memcpy(header, page_address(payload.page_io[0]),
408 DMINFO("bio magic_number:%u protocol_version:%d table_length:%u",
409 le32_to_cpu(header->magic_number),
410 le32_to_cpu(header->protocol_version),
411 le32_to_cpu(header->table_length));
413 metadata = kzalloc(sizeof(*metadata), GFP_KERNEL);
415 DMERR("kzalloc for metadata failed");
420 metadata->header = header;
421 table_length = le32_to_cpu(header->table_length);
423 if (table_length == 0 ||
424 table_length > (VERITY_METADATA_SIZE -
425 sizeof(struct android_metadata_header)))
428 metadata->verity_table = kzalloc(table_length + 1, GFP_KERNEL);
430 if (!metadata->verity_table) {
431 DMERR("kzalloc verity_table failed");
436 if (sizeof(struct android_metadata_header) +
437 table_length <= PAGE_SIZE) {
438 memcpy(metadata->verity_table, page_address(payload.page_io[0])
439 + sizeof(struct android_metadata_header),
442 copy_length = PAGE_SIZE -
443 sizeof(struct android_metadata_header);
444 memcpy(metadata->verity_table, page_address(payload.page_io[0])
445 + sizeof(struct android_metadata_header),
447 table_length -= copy_length;
448 offset = copy_length;
450 while (table_length != 0) {
451 if (table_length > PAGE_SIZE) {
452 memcpy(metadata->verity_table + offset,
453 page_address(payload.page_io[i]),
456 table_length -= PAGE_SIZE;
458 memcpy(metadata->verity_table + offset,
459 page_address(payload.page_io[i]),
466 metadata->verity_table[table_length] = '\0';
474 metadata = ERR_PTR(err);
476 for (i = 0; i < payload.number_of_pages; i++)
477 if (payload.page_io[i])
478 __free_page(payload.page_io[i]);
479 kfree(payload.page_io);
481 DMINFO("verity_table: %s", metadata->verity_table);
483 blkdev_put(bdev, FMODE_READ);
487 /* helper functions to extract properties from dts */
488 const char *find_dt_value(const char *name)
490 struct device_node *firmware;
493 firmware = of_find_node_by_path("/firmware/android");
496 value = of_get_property(firmware, name, NULL);
497 of_node_put(firmware);
502 static bool is_unlocked(void)
504 static const char unlocked[] = "orange";
505 static const char verified_boot_prop[] = "verifiedbootstate";
508 value = find_dt_value(verified_boot_prop);
510 value = verifiedbootstate;
512 return !strncmp(value, unlocked, sizeof(unlocked) - 1);
515 static int verity_mode(void)
517 static const char enforcing[] = "enforcing";
518 static const char verified_mode_prop[] = "veritymode";
521 value = find_dt_value(verified_mode_prop);
524 if (!strncmp(value, enforcing, sizeof(enforcing) - 1))
525 return DM_VERITY_MODE_RESTART;
527 return DM_VERITY_MODE_EIO;
530 static int verify_header(struct android_metadata_header *header)
532 int retval = -EINVAL;
534 if (is_unlocked() && le32_to_cpu(header->magic_number) ==
535 VERITY_METADATA_MAGIC_DISABLE) {
536 retval = VERITY_STATE_DISABLE;
540 if (!(le32_to_cpu(header->magic_number) ==
541 VERITY_METADATA_MAGIC_NUMBER) ||
542 (le32_to_cpu(header->magic_number) ==
543 VERITY_METADATA_MAGIC_DISABLE)) {
544 DMERR("Incorrect magic number");
548 if (le32_to_cpu(header->protocol_version) !=
549 VERITY_METADATA_VERSION) {
550 DMERR("Unsupported version %u",
551 le32_to_cpu(header->protocol_version));
558 static int verify_verity_signature(char *key_id,
559 struct android_metadata *metadata)
563 struct public_key_signature *pks = NULL;
564 int retval = -EINVAL;
566 key_ref = keyring_search(make_key_ref(system_trusted_keyring, 1),
567 &key_type_asymmetric, key_id);
569 if (IS_ERR(key_ref)) {
570 DMERR("keyring: key not found");
574 key = key_ref_to_ptr(key_ref);
576 pks = table_make_digest(HASH_ALGO_SHA256,
577 (const void *)metadata->verity_table,
578 le32_to_cpu(metadata->header->table_length));
581 DMERR("hashing failed");
585 retval = table_extract_mpi_array(pks, &metadata->header->signature[0],
588 DMERR("Error extracting mpi %d", retval);
592 retval = verify_signature(key, pks);
593 mpi_free(pks->rsa.s);
601 static void handle_error(void)
603 int mode = verity_mode();
604 if (mode == DM_VERITY_MODE_RESTART) {
605 DMERR("triggering restart");
606 kernel_restart("dm-verity device corrupted");
608 DMERR("Mounting verity root failed");
612 static inline bool test_mult_overflow(sector_t a, u32 b)
614 sector_t r = (sector_t)~0ULL;
620 static int add_as_linear_device(struct dm_target *ti, char *dev)
622 /*Move to linear mapping defines*/
623 char *linear_table_args[DM_LINEAR_ARGS] = {dev,
624 DM_LINEAR_TARGET_OFFSET};
627 android_verity_target.dtr = dm_linear_dtr,
628 android_verity_target.map = dm_linear_map,
629 android_verity_target.status = dm_linear_status,
630 android_verity_target.ioctl = dm_linear_ioctl,
631 android_verity_target.merge = dm_linear_merge,
632 android_verity_target.iterate_devices = dm_linear_iterate_devices,
633 android_verity_target.io_hints = NULL;
635 err = dm_linear_ctr(ti, DM_LINEAR_ARGS, linear_table_args);
638 DMINFO("Added android-verity as a linear target");
641 DMERR("Failed to add android-verity as linear target");
648 * <key id> Key id of the public key in the system keyring.
649 * Verity metadata's signature would be verified against
650 * this. If the key id contains spaces, replace them
652 * <block device> The block device for which dm-verity is being setup.
654 static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
656 dev_t uninitialized_var(dev);
657 struct android_metadata *uninitialized_var(metadata);
658 int err = 0, i, mode;
659 char *key_id, *table_ptr, dummy, *target_device,
660 *verity_table_args[VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS];
661 /* One for specifying number of opt args and one for mode */
662 sector_t data_sectors;
664 unsigned int no_of_args = VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS;
665 struct fec_header uninitialized_var(fec);
666 struct fec_ecc_metadata uninitialized_var(ecc);
667 char buf[FEC_ARG_LENGTH], *buf_ptr;
668 unsigned long long tmpll;
672 /* Use the default keyid */
673 if (default_verity_key_id())
674 key_id = veritykeyid;
675 else if (!is_eng()) {
676 DMERR("veritykeyid= is not set");
680 } else if (argc == 2)
683 DMERR("Incorrect number of arguments");
688 target_device = argv[0];
690 dev = name_to_dev_t(target_device);
692 DMERR("no dev found for %s", target_device);
698 err = find_size(dev, &device_size);
700 DMERR("error finding bdev size");
705 ti->len = device_size;
706 err = add_as_linear_device(ti, target_device);
711 verity_enabled = false;
715 strreplace(key_id, '#', ' ');
717 DMINFO("key:%s dev:%s", key_id, target_device);
719 if (extract_fec_header(dev, &fec, &ecc)) {
720 DMERR("Error while extracting fec header");
725 metadata = extract_metadata(dev, &fec);
727 if (IS_ERR(metadata)) {
728 DMERR("Error while extracting metadata");
733 err = verify_header(metadata->header);
735 if (err == VERITY_STATE_DISABLE) {
736 DMERR("Mounting root with verity disabled");
737 verity_enabled = false;
738 /* we would still have to parse the args to figure out
739 * the data blocks size. Or may be could map the entire
740 * partition similar to mounting the device.
743 DMERR("Verity header handle error");
748 if (verity_enabled) {
749 err = verify_verity_signature(key_id, metadata);
752 DMERR("Signature verification failed");
756 DMINFO("Signature verification success");
759 table_ptr = metadata->verity_table;
761 for (i = 0; i < VERITY_TABLE_ARGS; i++) {
762 verity_table_args[i] = strsep(&table_ptr, " ");
763 if (verity_table_args[i] == NULL)
767 if (i != VERITY_TABLE_ARGS) {
768 DMERR("Verity table not in the expected format");
774 if (sscanf(verity_table_args[5], "%llu%c", &tmpll, &dummy)
776 DMERR("Verity table not in the expected format");
782 if (tmpll > ULONG_MAX) {
783 DMERR("<num_data_blocks> too large. Forgot to turn on CONFIG_LBDAF?");
789 data_sectors = tmpll;
791 if (sscanf(verity_table_args[3], "%u%c", &data_block_size, &dummy)
793 DMERR("Verity table not in the expected format");
799 if (test_mult_overflow(data_sectors, data_block_size >>
801 DMERR("data_sectors too large");
807 data_sectors *= data_block_size >> SECTOR_SHIFT;
808 DMINFO("Data sectors %llu", (unsigned long long)data_sectors);
810 /* update target length */
811 ti->len = data_sectors;
813 /* Setup linear target and free */
814 if (!verity_enabled) {
815 err = add_as_linear_device(ti, target_device);
819 /*substitute data_dev and hash_dev*/
820 verity_table_args[1] = target_device;
821 verity_table_args[2] = target_device;
823 mode = verity_mode();
825 if (ecc.valid && IS_BUILTIN(CONFIG_DM_VERITY_FEC)) {
827 err = snprintf(buf, FEC_ARG_LENGTH,
828 "%u %s " VERITY_TABLE_OPT_FEC_FORMAT,
829 1 + VERITY_TABLE_OPT_FEC_ARGS,
830 mode == DM_VERITY_MODE_RESTART ?
831 VERITY_TABLE_OPT_RESTART :
832 VERITY_TABLE_OPT_LOGGING,
834 ecc.start / FEC_BLOCK_SIZE, ecc.blocks,
837 err = snprintf(buf, FEC_ARG_LENGTH,
838 "%u " VERITY_TABLE_OPT_FEC_FORMAT,
839 VERITY_TABLE_OPT_FEC_ARGS, target_device,
840 ecc.start / FEC_BLOCK_SIZE, ecc.blocks,
844 err = snprintf(buf, FEC_ARG_LENGTH,
845 "2 " VERITY_TABLE_OPT_IGNZERO " %s",
846 mode == DM_VERITY_MODE_RESTART ?
847 VERITY_TABLE_OPT_RESTART : VERITY_TABLE_OPT_LOGGING);
849 err = snprintf(buf, FEC_ARG_LENGTH, "1 %s",
850 "ignore_zero_blocks");
853 if (err < 0 || err >= FEC_ARG_LENGTH)
858 for (i = VERITY_TABLE_ARGS; i < (VERITY_TABLE_ARGS +
859 VERITY_TABLE_OPT_FEC_ARGS + 2); i++) {
860 verity_table_args[i] = strsep(&buf_ptr, " ");
861 if (verity_table_args[i] == NULL) {
867 err = verity_ctr(ti, no_of_args, verity_table_args);
870 DMERR("android-verity failed to mount as verity target");
873 DMINFO("android-verity mounted as verity target");
877 kfree(metadata->header);
878 kfree(metadata->verity_table);
883 static int __init dm_android_verity_init(void)
888 r = dm_register_target(&android_verity_target);
890 DMERR("register failed %d", r);
892 /* Tracks the status of the last added target */
893 debug_dir = debugfs_create_dir("android_verity", NULL);
895 if (IS_ERR_OR_NULL(debug_dir)) {
896 DMERR("Cannot create android_verity debugfs directory: %ld",
901 file = debugfs_create_bool("target_added", S_IRUGO, debug_dir,
902 (u32 *)&target_added);
904 if (IS_ERR_OR_NULL(file)) {
905 DMERR("Cannot create android_verity debugfs directory: %ld",
907 debugfs_remove_recursive(debug_dir);
911 file = debugfs_create_bool("verity_enabled", S_IRUGO, debug_dir,
912 (u32 *)&verity_enabled);
914 if (IS_ERR_OR_NULL(file)) {
915 DMERR("Cannot create android_verity debugfs directory: %ld",
917 debugfs_remove_recursive(debug_dir);
924 static void __exit dm_android_verity_exit(void)
926 if (!IS_ERR_OR_NULL(debug_dir))
927 debugfs_remove_recursive(debug_dir);
929 dm_unregister_target(&android_verity_target);
932 module_init(dm_android_verity_init);
933 module_exit(dm_android_verity_exit);