ANDROID: dm: mount as linear target if eng build
[firefly-linux-kernel-4.4.55.git] / drivers / md / dm-android-verity.c
1 /*
2  * Copyright (C) 2015 Google, Inc.
3  *
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.
7  *
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.
12  *
13  */
14
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>
21 #include <linux/fs.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>
29 #include <linux/of.h>
30 #include <linux/reboot.h>
31 #include <linux/string.h>
32 #include <linux/vmalloc.h>
33
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>
40
41 #include "dm-verity.h"
42 #include "dm-android-verity.h"
43
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];
48
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);
53
54 static struct target_type android_verity_target = {
55         .name                   = "android-verity",
56         .version                = {1, 0, 0},
57         .module                 = THIS_MODULE,
58         .ctr                    = android_verity_ctr,
59         .dtr                    = verity_dtr,
60         .map                    = verity_map,
61         .status                 = verity_status,
62         .ioctl                  = verity_ioctl,
63         .merge                  = verity_merge,
64         .iterate_devices        = verity_iterate_devices,
65         .io_hints               = verity_io_hints,
66 };
67
68 static int __init verified_boot_state_param(char *line)
69 {
70         strlcpy(verifiedbootstate, line, sizeof(verifiedbootstate));
71         return 1;
72 }
73
74 __setup("androidboot.verifiedbootstate=", verified_boot_state_param);
75
76 static int __init verity_mode_param(char *line)
77 {
78         strlcpy(veritymode, line, sizeof(veritymode));
79         return 1;
80 }
81
82 __setup("androidboot.veritymode=", verity_mode_param);
83
84 static int __init verity_keyid_param(char *line)
85 {
86         strlcpy(veritykeyid, line, sizeof(veritykeyid));
87         return 1;
88 }
89
90 __setup("veritykeyid=", verity_keyid_param);
91
92 static int __init verity_buildvariant(char *line)
93 {
94         strlcpy(buildvariant, line, sizeof(buildvariant));
95         return 1;
96 }
97
98 __setup("buildvariant=", verity_buildvariant);
99
100 static inline bool default_verity_key_id(void)
101 {
102         return veritykeyid[0] != '\0';
103 }
104
105 static inline bool is_eng(void)
106 {
107         static const char typeeng[]  = "eng";
108
109         return !strncmp(buildvariant, typeeng, sizeof(typeeng));
110 }
111
112 static int table_extract_mpi_array(struct public_key_signature *pks,
113                                 const void *data, size_t len)
114 {
115         MPI mpi = mpi_read_raw_data(data, len);
116
117         if (!mpi) {
118                 DMERR("Error while allocating mpi array");
119                 return -ENOMEM;
120         }
121
122         pks->mpi[0] = mpi;
123         pks->nr_mpi = 1;
124         return 0;
125 }
126
127 static struct public_key_signature *table_make_digest(
128                                                 enum hash_algo hash,
129                                                 const void *table,
130                                                 unsigned long table_len)
131 {
132         struct public_key_signature *pks = NULL;
133         struct crypto_shash *tfm;
134         struct shash_desc *desc;
135         size_t digest_size, desc_size;
136         int ret;
137
138         /* Allocate the hashing algorithm we're going to need and find out how
139          * big the hash operational data will be.
140          */
141         tfm = crypto_alloc_shash(hash_algo_name[hash], 0, 0);
142         if (IS_ERR(tfm))
143                 return ERR_CAST(tfm);
144
145         desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
146         digest_size = crypto_shash_digestsize(tfm);
147
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.
150          */
151         ret = -ENOMEM;
152         pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL);
153         if (!pks)
154                 goto error;
155
156         pks->pkey_hash_algo = hash;
157         pks->digest = (u8 *)pks + sizeof(*pks) + desc_size;
158         pks->digest_size = digest_size;
159
160         desc = (struct shash_desc *)(pks + 1);
161         desc->tfm = tfm;
162         desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
163
164         ret = crypto_shash_init(desc);
165         if (ret < 0)
166                 goto error;
167
168         ret = crypto_shash_finup(desc, table, table_len, pks->digest);
169         if (ret < 0)
170                 goto error;
171
172         crypto_free_shash(tfm);
173         return pks;
174
175 error:
176         kfree(pks);
177         crypto_free_shash(tfm);
178         return ERR_PTR(ret);
179 }
180
181 static int read_block_dev(struct bio_read *payload, struct block_device *bdev,
182                 sector_t offset, int length)
183 {
184         struct bio *bio;
185         int err = 0, i;
186
187         payload->number_of_pages = DIV_ROUND_UP(length, PAGE_SIZE);
188
189         bio = bio_alloc(GFP_KERNEL, payload->number_of_pages);
190         if (!bio) {
191                 DMERR("Error while allocating bio");
192                 return -ENOMEM;
193         }
194
195         bio->bi_bdev = bdev;
196         bio->bi_iter.bi_sector = offset;
197
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");
202                 err = -ENOMEM;
203                 goto free_bio;
204         }
205
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");
210                         err = -ENOMEM;
211                         goto free_pages;
212                 }
213                 if (!bio_add_page(bio, payload->page_io[i], PAGE_SIZE, 0)) {
214                         DMERR("bio_add_page error");
215                         err = -EIO;
216                         goto free_pages;
217                 }
218         }
219
220         if (!submit_bio_wait(READ, bio))
221                 /* success */
222                 goto free_bio;
223         DMERR("bio read failed");
224         err = -EIO;
225
226 free_pages:
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);
231 free_bio:
232         bio_put(bio);
233         return err;
234 }
235
236 static inline u64 fec_div_round_up(u64 x, u64 y)
237 {
238         u64 remainder;
239
240         return div64_u64_rem(x, y, &remainder) +
241                 (remainder > 0 ? 1 : 0);
242 }
243
244 static inline void populate_fec_metadata(struct fec_header *header,
245                                 struct fec_ecc_metadata *ecc)
246 {
247         ecc->blocks = fec_div_round_up(le64_to_cpu(header->inp_size),
248                         FEC_BLOCK_SIZE);
249         ecc->roots = le32_to_cpu(header->roots);
250         ecc->start = le64_to_cpu(header->inp_size);
251 }
252
253 static inline int validate_fec_header(struct fec_header *header, u64 offset)
254 {
255         /* move offset to make the sanity check work for backup header
256          * as well. */
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))
266                 return -EINVAL;
267
268         return 0;
269 }
270
271 static int extract_fec_header(dev_t dev, struct fec_header *fec,
272                                 struct fec_ecc_metadata *ecc)
273 {
274         u64 device_size;
275         struct bio_read payload;
276         int i, err = 0;
277         struct block_device *bdev;
278
279         bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
280
281         if (IS_ERR_OR_NULL(bdev)) {
282                 DMERR("bdev get error");
283                 return PTR_ERR(bdev);
284         }
285
286         device_size = i_size_read(bdev->bd_inode);
287
288         /* fec metadata size is a power of 2 and PAGE_SIZE
289          * is a power of 2 as well.
290          */
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);
294
295         err = read_block_dev(&payload, bdev, (device_size -
296                 FEC_BLOCK_SIZE) / (1 << SECTOR_SHIFT), FEC_BLOCK_SIZE);
297         if (err) {
298                 DMERR("Error while reading verity metadata");
299                 goto error;
300         }
301
302         BUG_ON(sizeof(struct fec_header) > PAGE_SIZE);
303         memcpy(fec, page_address(payload.page_io[0]),
304                         sizeof(*fec));
305
306         ecc->valid = true;
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
310                         - sizeof(*fec) ,
311                         sizeof(*fec));
312                 if (validate_fec_header(fec, device_size -
313                         sizeof(struct fec_header)))
314                         ecc->valid = false;
315         }
316
317         if (ecc->valid)
318                 populate_fec_metadata(fec, ecc);
319
320         for (i = 0; i < payload.number_of_pages; i++)
321                 __free_page(payload.page_io[i]);
322         kfree(payload.page_io);
323
324 error:
325         blkdev_put(bdev, FMODE_READ);
326         return err;
327 }
328 static void find_metadata_offset(struct fec_header *fec,
329                 struct block_device *bdev, u64 *metadata_offset)
330 {
331         u64 device_size;
332
333         device_size = i_size_read(bdev->bd_inode);
334
335         if (le32_to_cpu(fec->magic) == FEC_MAGIC)
336                 *metadata_offset = le64_to_cpu(fec->inp_size) -
337                                         VERITY_METADATA_SIZE;
338         else
339                 *metadata_offset = device_size - VERITY_METADATA_SIZE;
340 }
341
342 static int find_size(dev_t dev, u64 *device_size)
343 {
344         struct block_device *bdev;
345
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);
350         }
351
352         *device_size = i_size_read(bdev->bd_inode);
353         *device_size >>= SECTOR_SHIFT;
354
355         DMINFO("blkdev size in sectors: %llu", *device_size);
356         blkdev_put(bdev, FMODE_READ);
357         return 0;
358 }
359
360 static struct android_metadata *extract_metadata(dev_t dev,
361                                 struct fec_header *fec)
362 {
363         struct block_device *bdev;
364         struct android_metadata_header *header;
365         struct android_metadata *uninitialized_var(metadata);
366         int i;
367         u32 table_length, copy_length, offset;
368         u64 metadata_offset;
369         struct bio_read payload;
370         int err = 0;
371
372         bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
373
374         if (IS_ERR_OR_NULL(bdev)) {
375                 DMERR("blkdev_get_by_dev failed");
376                 return ERR_CAST(bdev);
377         }
378
379         find_metadata_offset(fec, bdev, &metadata_offset);
380
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.
384         */
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);
389
390         err = read_block_dev(&payload, bdev, metadata_offset /
391                 (1 << SECTOR_SHIFT), VERITY_METADATA_SIZE);
392         if (err) {
393                 DMERR("Error while reading verity metadata");
394                 metadata = ERR_PTR(err);
395                 goto blkdev_release;
396         }
397
398         header = kzalloc(sizeof(*header), GFP_KERNEL);
399         if (!header) {
400                 DMERR("kzalloc failed for header");
401                 err = -ENOMEM;
402                 goto free_payload;
403         }
404
405         memcpy(header, page_address(payload.page_io[0]),
406                 sizeof(*header));
407
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));
412
413         metadata = kzalloc(sizeof(*metadata), GFP_KERNEL);
414         if (!metadata) {
415                 DMERR("kzalloc for metadata failed");
416                 err = -ENOMEM;
417                 goto free_header;
418         }
419
420         metadata->header = header;
421         table_length = le32_to_cpu(header->table_length);
422
423         if (table_length == 0 ||
424                 table_length > (VERITY_METADATA_SIZE -
425                         sizeof(struct android_metadata_header)))
426                 goto free_metadata;
427
428         metadata->verity_table = kzalloc(table_length + 1, GFP_KERNEL);
429
430         if (!metadata->verity_table) {
431                 DMERR("kzalloc verity_table failed");
432                 err = -ENOMEM;
433                 goto free_metadata;
434         }
435
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),
440                         table_length);
441         } else {
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),
446                         copy_length);
447                 table_length -= copy_length;
448                 offset = copy_length;
449                 i = 1;
450                 while (table_length != 0) {
451                         if (table_length > PAGE_SIZE) {
452                                 memcpy(metadata->verity_table + offset,
453                                         page_address(payload.page_io[i]),
454                                         PAGE_SIZE);
455                                 offset += PAGE_SIZE;
456                                 table_length -= PAGE_SIZE;
457                         } else {
458                                 memcpy(metadata->verity_table + offset,
459                                         page_address(payload.page_io[i]),
460                                         table_length);
461                                 table_length = 0;
462                         }
463                         i++;
464                 }
465         }
466         metadata->verity_table[table_length] = '\0';
467
468         goto free_payload;
469
470 free_metadata:
471         kfree(metadata);
472 free_header:
473         kfree(header);
474         metadata = ERR_PTR(err);
475 free_payload:
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);
480
481         DMINFO("verity_table: %s", metadata->verity_table);
482 blkdev_release:
483         blkdev_put(bdev, FMODE_READ);
484         return metadata;
485 }
486
487 /* helper functions to extract properties from dts */
488 const char *find_dt_value(const char *name)
489 {
490         struct device_node *firmware;
491         const char *value;
492
493         firmware = of_find_node_by_path("/firmware/android");
494         if (!firmware)
495                 return NULL;
496         value = of_get_property(firmware, name, NULL);
497         of_node_put(firmware);
498
499         return value;
500 }
501
502 static bool is_unlocked(void)
503 {
504         static const char unlocked[]  = "orange";
505         static const char verified_boot_prop[] = "verifiedbootstate";
506         const char *value;
507
508         value = find_dt_value(verified_boot_prop);
509         if (!value)
510                 value = verifiedbootstate;
511
512         return !strncmp(value, unlocked, sizeof(unlocked) - 1);
513 }
514
515 static int verity_mode(void)
516 {
517         static const char enforcing[] = "enforcing";
518         static const char verified_mode_prop[] = "veritymode";
519         const char *value;
520
521         value = find_dt_value(verified_mode_prop);
522         if (!value)
523                 value = veritymode;
524         if (!strncmp(value, enforcing, sizeof(enforcing) - 1))
525                 return DM_VERITY_MODE_RESTART;
526
527         return DM_VERITY_MODE_EIO;
528 }
529
530 static int verify_header(struct android_metadata_header *header)
531 {
532         int retval = -EINVAL;
533
534         if (is_unlocked() && le32_to_cpu(header->magic_number) ==
535                 VERITY_METADATA_MAGIC_DISABLE) {
536                 retval = VERITY_STATE_DISABLE;
537                 return retval;
538         }
539
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");
545                 return retval;
546         }
547
548         if (le32_to_cpu(header->protocol_version) !=
549                 VERITY_METADATA_VERSION) {
550                 DMERR("Unsupported version %u",
551                         le32_to_cpu(header->protocol_version));
552                 return retval;
553         }
554
555         return 0;
556 }
557
558 static int verify_verity_signature(char *key_id,
559                 struct android_metadata *metadata)
560 {
561         key_ref_t key_ref;
562         struct key *key;
563         struct public_key_signature *pks = NULL;
564         int retval = -EINVAL;
565
566         key_ref = keyring_search(make_key_ref(system_trusted_keyring, 1),
567                 &key_type_asymmetric, key_id);
568
569         if (IS_ERR(key_ref)) {
570                 DMERR("keyring: key not found");
571                 return -ENOKEY;
572         }
573
574         key = key_ref_to_ptr(key_ref);
575
576         pks = table_make_digest(HASH_ALGO_SHA256,
577                         (const void *)metadata->verity_table,
578                         le32_to_cpu(metadata->header->table_length));
579
580         if (IS_ERR(pks)) {
581                 DMERR("hashing failed");
582                 goto error;
583         }
584
585         retval = table_extract_mpi_array(pks, &metadata->header->signature[0],
586                                 RSANUMBYTES);
587         if (retval < 0) {
588                 DMERR("Error extracting mpi %d", retval);
589                 goto error;
590         }
591
592         retval = verify_signature(key, pks);
593         mpi_free(pks->rsa.s);
594 error:
595         kfree(pks);
596         key_put(key);
597
598         return retval;
599 }
600
601 static void handle_error(void)
602 {
603         int mode = verity_mode();
604         if (mode == DM_VERITY_MODE_RESTART) {
605                 DMERR("triggering restart");
606                 kernel_restart("dm-verity device corrupted");
607         } else {
608                 DMERR("Mounting verity root failed");
609         }
610 }
611
612 static inline bool test_mult_overflow(sector_t a, u32 b)
613 {
614         sector_t r = (sector_t)~0ULL;
615
616         sector_div(r, b);
617         return a > r;
618 }
619
620 static int add_as_linear_device(struct dm_target *ti, char *dev)
621 {
622         /*Move to linear mapping defines*/
623         char *linear_table_args[DM_LINEAR_ARGS] = {dev,
624                                         DM_LINEAR_TARGET_OFFSET};
625         int err = 0;
626
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;
634
635         err = dm_linear_ctr(ti, DM_LINEAR_ARGS, linear_table_args);
636
637         if (!err) {
638                 DMINFO("Added android-verity as a linear target");
639                 target_added = true;
640         } else
641                 DMERR("Failed to add android-verity as linear target");
642
643         return err;
644 }
645
646 /*
647  * Target parameters:
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
651  *                      with '#'.
652  *      <block device>  The block device for which dm-verity is being setup.
653  */
654 static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
655 {
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;
663         u32 data_block_size;
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;
669         u64 device_size;
670
671         if (argc == 1) {
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");
677                         handle_error();
678                         return -EINVAL;
679                 }
680         } else if (argc == 2)
681                 key_id = argv[1];
682         else {
683                 DMERR("Incorrect number of arguments");
684                 handle_error();
685                 return -EINVAL;
686         }
687
688         target_device = argv[0];
689
690         dev = name_to_dev_t(target_device);
691         if (!dev) {
692                 DMERR("no dev found for %s", target_device);
693                 handle_error();
694                 return -EINVAL;
695         }
696
697         if (is_eng()) {
698                 err = find_size(dev, &device_size);
699                 if (err) {
700                         DMERR("error finding bdev size");
701                         handle_error();
702                         return err;
703                 }
704
705                 ti->len = device_size;
706                 err = add_as_linear_device(ti, target_device);
707                 if (err) {
708                         handle_error();
709                         return err;
710                 }
711                 verity_enabled = false;
712                 return 0;
713         }
714
715         strreplace(key_id, '#', ' ');
716
717         DMINFO("key:%s dev:%s", key_id, target_device);
718
719         if (extract_fec_header(dev, &fec, &ecc)) {
720                 DMERR("Error while extracting fec header");
721                 handle_error();
722                 return -EINVAL;
723         }
724
725         metadata = extract_metadata(dev, &fec);
726
727         if (IS_ERR(metadata)) {
728                 DMERR("Error while extracting metadata");
729                 handle_error();
730                 return -EINVAL;
731         }
732
733         err = verify_header(metadata->header);
734
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.
741                  */
742         } else if (err) {
743                 DMERR("Verity header handle error");
744                 handle_error();
745                 goto free_metadata;
746         }
747
748         if (verity_enabled) {
749                 err = verify_verity_signature(key_id, metadata);
750
751                 if (err) {
752                         DMERR("Signature verification failed");
753                         handle_error();
754                         goto free_metadata;
755                 } else
756                         DMINFO("Signature verification success");
757         }
758
759         table_ptr = metadata->verity_table;
760
761         for (i = 0; i < VERITY_TABLE_ARGS; i++) {
762                 verity_table_args[i] = strsep(&table_ptr, " ");
763                 if (verity_table_args[i] == NULL)
764                         break;
765         }
766
767         if (i != VERITY_TABLE_ARGS) {
768                 DMERR("Verity table not in the expected format");
769                 err = -EINVAL;
770                 handle_error();
771                 goto free_metadata;
772         }
773
774         if (sscanf(verity_table_args[5], "%llu%c", &tmpll, &dummy)
775                                                         != 1) {
776                 DMERR("Verity table not in the expected format");
777                 handle_error();
778                 err = -EINVAL;
779                 goto free_metadata;
780         }
781
782         if (tmpll > ULONG_MAX) {
783                 DMERR("<num_data_blocks> too large. Forgot to turn on CONFIG_LBDAF?");
784                 handle_error();
785                 err = -EINVAL;
786                 goto free_metadata;
787         }
788
789         data_sectors = tmpll;
790
791         if (sscanf(verity_table_args[3], "%u%c", &data_block_size, &dummy)
792                                                                 != 1) {
793                 DMERR("Verity table not in the expected format");
794                 handle_error();
795                 err = -EINVAL;
796                 goto free_metadata;
797         }
798
799         if (test_mult_overflow(data_sectors, data_block_size >>
800                                                         SECTOR_SHIFT)) {
801                 DMERR("data_sectors too large");
802                 handle_error();
803                 err = -EOVERFLOW;
804                 goto free_metadata;
805         }
806
807         data_sectors *= data_block_size >> SECTOR_SHIFT;
808         DMINFO("Data sectors %llu", (unsigned long long)data_sectors);
809
810         /* update target length */
811         ti->len = data_sectors;
812
813         /* Setup linear target and free */
814         if (!verity_enabled) {
815                 err = add_as_linear_device(ti, target_device);
816                 goto free_metadata;
817         }
818
819         /*substitute data_dev and hash_dev*/
820         verity_table_args[1] = target_device;
821         verity_table_args[2] = target_device;
822
823         mode = verity_mode();
824
825         if (ecc.valid && IS_BUILTIN(CONFIG_DM_VERITY_FEC)) {
826                 if (mode) {
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,
833                                 target_device,
834                                 ecc.start / FEC_BLOCK_SIZE, ecc.blocks,
835                                 ecc.roots);
836                 } else {
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,
841                                 ecc.roots);
842                 }
843         } else if (mode) {
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);
848         } else {
849                 err = snprintf(buf, FEC_ARG_LENGTH, "1 %s",
850                                  "ignore_zero_blocks");
851         }
852
853         if (err < 0 || err >= FEC_ARG_LENGTH)
854                 goto free_metadata;
855
856         buf_ptr = buf;
857
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) {
862                         no_of_args = i;
863                         break;
864                 }
865         }
866
867         err = verity_ctr(ti, no_of_args, verity_table_args);
868
869         if (err)
870                 DMERR("android-verity failed to mount as verity target");
871         else {
872                 target_added = true;
873                 DMINFO("android-verity mounted as verity target");
874         }
875
876 free_metadata:
877         kfree(metadata->header);
878         kfree(metadata->verity_table);
879         kfree(metadata);
880         return err;
881 }
882
883 static int __init dm_android_verity_init(void)
884 {
885         int r;
886         struct dentry *file;
887
888         r = dm_register_target(&android_verity_target);
889         if (r < 0)
890                 DMERR("register failed %d", r);
891
892         /* Tracks the status of the last added target */
893         debug_dir = debugfs_create_dir("android_verity", NULL);
894
895         if (IS_ERR_OR_NULL(debug_dir)) {
896                 DMERR("Cannot create android_verity debugfs directory: %ld",
897                         PTR_ERR(debug_dir));
898                 goto end;
899         }
900
901         file = debugfs_create_bool("target_added", S_IRUGO, debug_dir,
902                                 (u32 *)&target_added);
903
904         if (IS_ERR_OR_NULL(file)) {
905                 DMERR("Cannot create android_verity debugfs directory: %ld",
906                         PTR_ERR(debug_dir));
907                 debugfs_remove_recursive(debug_dir);
908                 goto end;
909         }
910
911         file = debugfs_create_bool("verity_enabled", S_IRUGO, debug_dir,
912                                 (u32 *)&verity_enabled);
913
914         if (IS_ERR_OR_NULL(file)) {
915                 DMERR("Cannot create android_verity debugfs directory: %ld",
916                         PTR_ERR(debug_dir));
917                 debugfs_remove_recursive(debug_dir);
918         }
919
920 end:
921         return r;
922 }
923
924 static void __exit dm_android_verity_exit(void)
925 {
926         if (!IS_ERR_OR_NULL(debug_dir))
927                 debugfs_remove_recursive(debug_dir);
928
929         dm_unregister_target(&android_verity_target);
930 }
931
932 module_init(dm_android_verity_init);
933 module_exit(dm_android_verity_exit);