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