ANDROID: dm: Add android verity target
[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/delay.h>
17 #include <linux/device.h>
18 #include <linux/device-mapper.h>
19 #include <linux/errno.h>
20 #include <linux/fs.h>
21 #include <linux/fcntl.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/key.h>
25 #include <linux/module.h>
26 #include <linux/mount.h>
27 #include <linux/namei.h>
28 #include <linux/of.h>
29 #include <linux/reboot.h>
30 #include <linux/string.h>
31 #include <linux/vmalloc.h>
32
33 #include <asm/setup.h>
34 #include <crypto/hash.h>
35 #include <crypto/public_key.h>
36 #include <crypto/sha.h>
37 #include <keys/asymmetric-type.h>
38 #include <keys/system_keyring.h>
39
40 #include "dm-verity.h"
41 #include "dm-android-verity.h"
42
43 static char verifiedbootstate[VERITY_COMMANDLINE_PARAM_LENGTH];
44 static char veritymode[VERITY_COMMANDLINE_PARAM_LENGTH];
45
46 static int __init verified_boot_state_param(char *line)
47 {
48         strlcpy(verifiedbootstate, line, sizeof(verifiedbootstate));
49         return 1;
50 }
51
52 __setup("androidboot.verifiedbootstate=", verified_boot_state_param);
53
54 static int __init verity_mode_param(char *line)
55 {
56         strlcpy(veritymode, line, sizeof(veritymode));
57         return 1;
58 }
59
60 __setup("androidboot.veritymode=", verity_mode_param);
61
62 static int table_extract_mpi_array(struct public_key_signature *pks,
63                                 const void *data, size_t len)
64 {
65         MPI mpi = mpi_read_raw_data(data, len);
66
67         if (!mpi) {
68                 DMERR("Error while allocating mpi array");
69                 return -ENOMEM;
70         }
71
72         pks->mpi[0] = mpi;
73         pks->nr_mpi = 1;
74         return 0;
75 }
76
77 static struct public_key_signature *table_make_digest(
78                                                 enum pkey_hash_algo hash,
79                                                 const void *table,
80                                                 unsigned long table_len)
81 {
82         struct public_key_signature *pks = NULL;
83         struct crypto_shash *tfm;
84         struct shash_desc *desc;
85         size_t digest_size, desc_size;
86         int ret;
87
88         /* Allocate the hashing algorithm we're going to need and find out how
89          * big the hash operational data will be.
90          */
91         tfm = crypto_alloc_shash(pkey_hash_algo[hash], 0, 0);
92         if (IS_ERR(tfm))
93                 return ERR_CAST(tfm);
94
95         desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
96         digest_size = crypto_shash_digestsize(tfm);
97
98         /* We allocate the hash operational data storage on the end of out
99          * context data and the digest output buffer on the end of that.
100          */
101         ret = -ENOMEM;
102         pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL);
103         if (!pks)
104                 goto error;
105
106         pks->pkey_hash_algo = hash;
107         pks->digest = (u8 *)pks + sizeof(*pks) + desc_size;
108         pks->digest_size = digest_size;
109
110         desc = (struct shash_desc *)(pks + 1);
111         desc->tfm = tfm;
112         desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
113
114         ret = crypto_shash_init(desc);
115         if (ret < 0)
116                 goto error;
117
118         ret = crypto_shash_finup(desc, table, table_len, pks->digest);
119         if (ret < 0)
120                 goto error;
121
122         crypto_free_shash(tfm);
123         return pks;
124
125 error:
126         kfree(pks);
127         crypto_free_shash(tfm);
128         return ERR_PTR(ret);
129 }
130
131 static int read_block_dev(struct bio_read *payload, struct block_device *bdev,
132                 sector_t offset, int length)
133 {
134         struct bio *bio;
135         int err = 0, i;
136
137         payload->number_of_pages = DIV_ROUND_UP(length, PAGE_SIZE);
138
139         bio = bio_alloc(GFP_KERNEL, payload->number_of_pages);
140         if (!bio) {
141                 DMERR("Error while allocating bio");
142                 return -ENOMEM;
143         }
144
145         bio->bi_bdev = bdev;
146         bio->bi_sector = offset;
147
148         payload->page_io = kzalloc(sizeof(struct page *) *
149                 payload->number_of_pages, GFP_KERNEL);
150         if (!payload->page_io) {
151                 DMERR("page_io array alloc failed");
152                 err = -ENOMEM;
153                 goto free_bio;
154         }
155
156         for (i = 0; i < payload->number_of_pages; i++) {
157                 payload->page_io[i] = alloc_page(GFP_KERNEL);
158                 if (!payload->page_io[i]) {
159                         DMERR("alloc_page failed");
160                         err = -ENOMEM;
161                         goto free_pages;
162                 }
163                 if (!bio_add_page(bio, payload->page_io[i], PAGE_SIZE, 0)) {
164                         DMERR("bio_add_page error");
165                         err = -EIO;
166                         goto free_pages;
167                 }
168         }
169
170         if (!submit_bio_wait(READ, bio))
171                 /* success */
172                 goto free_bio;
173         DMERR("bio read failed");
174         err = -EIO;
175
176 free_pages:
177         for (i = 0; i < payload->number_of_pages; i++)
178                 if (payload->page_io[i])
179                         __free_page(payload->page_io[i]);
180         kfree(payload->page_io);
181 free_bio:
182         bio_put(bio);
183         return err;
184 }
185
186 static inline u64 fec_div_round_up(u64 x, u64 y)
187 {
188         u64 remainder;
189
190         return div64_u64_rem(x, y, &remainder) +
191                 (remainder > 0 ? 1 : 0);
192 }
193
194 static inline void populate_fec_metadata(struct fec_header *header,
195                                 struct fec_ecc_metadata *ecc)
196 {
197         ecc->blocks = fec_div_round_up(le64_to_cpu(header->inp_size),
198                         FEC_BLOCK_SIZE);
199         ecc->roots = le32_to_cpu(header->roots);
200         ecc->start = le64_to_cpu(header->inp_size);
201 }
202
203 static inline int validate_fec_header(struct fec_header *header, u64 offset)
204 {
205         /* move offset to make the sanity check work for backup header
206          * as well. */
207         offset -= offset % FEC_BLOCK_SIZE;
208         if (le32_to_cpu(header->magic) != FEC_MAGIC ||
209                 le32_to_cpu(header->version) != FEC_VERSION ||
210                 le32_to_cpu(header->size) != sizeof(struct fec_header) ||
211                 le32_to_cpu(header->roots) == 0 ||
212                 le32_to_cpu(header->roots) >= FEC_RSM ||
213                 offset < le32_to_cpu(header->fec_size) ||
214                 offset - le32_to_cpu(header->fec_size) !=
215                 le64_to_cpu(header->inp_size))
216                 return -EINVAL;
217
218         return 0;
219 }
220
221 static int extract_fec_header(dev_t dev, struct fec_header *fec,
222                                 struct fec_ecc_metadata *ecc)
223 {
224         u64 device_size;
225         struct bio_read payload;
226         int i, err = 0;
227         struct block_device *bdev;
228
229         bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
230
231         if (IS_ERR(bdev)) {
232                 DMERR("bdev get error");
233                 return PTR_ERR(bdev);
234         }
235
236         device_size = i_size_read(bdev->bd_inode);
237
238         /* fec metadata size is a power of 2 and PAGE_SIZE
239          * is a power of 2 as well.
240          */
241         BUG_ON(FEC_BLOCK_SIZE > PAGE_SIZE);
242         /* 512 byte sector alignment */
243         BUG_ON(((device_size - FEC_BLOCK_SIZE) % (1 << SECTOR_SHIFT)) != 0);
244
245         err = read_block_dev(&payload, bdev, (device_size -
246                 FEC_BLOCK_SIZE) / (1 << SECTOR_SHIFT), FEC_BLOCK_SIZE);
247         if (err) {
248                 DMERR("Error while reading verity metadata");
249                 goto error;
250         }
251
252         BUG_ON(sizeof(struct fec_header) > PAGE_SIZE);
253         memcpy(fec, page_address(payload.page_io[0]),
254                         sizeof(*fec));
255
256         ecc->valid = true;
257         if (validate_fec_header(fec, device_size - FEC_BLOCK_SIZE)) {
258                 /* Try the backup header */
259                 memcpy(fec, page_address(payload.page_io[0]) + FEC_BLOCK_SIZE
260                         - sizeof(*fec) ,
261                         sizeof(*fec));
262                 if (validate_fec_header(fec, device_size -
263                         sizeof(struct fec_header)))
264                         ecc->valid = false;
265         }
266
267         if (ecc->valid)
268                 populate_fec_metadata(fec, ecc);
269
270         for (i = 0; i < payload.number_of_pages; i++)
271                 __free_page(payload.page_io[i]);
272         kfree(payload.page_io);
273
274 error:
275         blkdev_put(bdev, FMODE_READ);
276         return err;
277 }
278 static void find_metadata_offset(struct fec_header *fec,
279                 struct block_device *bdev, u64 *metadata_offset)
280 {
281         u64 device_size;
282
283         device_size = i_size_read(bdev->bd_inode);
284
285         if (le32_to_cpu(fec->magic) == FEC_MAGIC)
286                 *metadata_offset = le64_to_cpu(fec->inp_size) -
287                                         VERITY_METADATA_SIZE;
288         else
289                 *metadata_offset = device_size - VERITY_METADATA_SIZE;
290 }
291
292 static struct android_metadata *extract_metadata(dev_t dev,
293                                 struct fec_header *fec)
294 {
295         struct block_device *bdev;
296         struct android_metadata_header *header;
297         struct android_metadata *uninitialized_var(metadata);
298         int i;
299         u32 table_length, copy_length, offset;
300         u64 metadata_offset;
301         struct bio_read payload;
302         int err = 0;
303
304         bdev = blkdev_get_by_dev(dev, FMODE_READ, NULL);
305
306         if (IS_ERR(bdev)) {
307                 DMERR("blkdev_get_by_dev failed");
308                 return ERR_CAST(bdev);
309         }
310
311         find_metadata_offset(fec, bdev, &metadata_offset);
312
313         /* Verity metadata size is a power of 2 and PAGE_SIZE
314          * is a power of 2 as well.
315          * PAGE_SIZE is also a multiple of 512 bytes.
316         */
317         if (VERITY_METADATA_SIZE > PAGE_SIZE)
318                 BUG_ON(VERITY_METADATA_SIZE % PAGE_SIZE != 0);
319         /* 512 byte sector alignment */
320         BUG_ON(metadata_offset % (1 << SECTOR_SHIFT) != 0);
321
322         err = read_block_dev(&payload, bdev, metadata_offset /
323                 (1 << SECTOR_SHIFT), VERITY_METADATA_SIZE);
324         if (err) {
325                 DMERR("Error while reading verity metadata");
326                 metadata = ERR_PTR(err);
327                 goto blkdev_release;
328         }
329
330         header = kzalloc(sizeof(*header), GFP_KERNEL);
331         if (!header) {
332                 DMERR("kzalloc failed for header");
333                 err = -ENOMEM;
334                 goto free_payload;
335         }
336
337         memcpy(header, page_address(payload.page_io[0]),
338                 sizeof(*header));
339
340         DMINFO("bio magic_number:%u protocol_version:%d table_length:%u",
341                 le32_to_cpu(header->magic_number),
342                 le32_to_cpu(header->protocol_version),
343                 le32_to_cpu(header->table_length));
344
345         metadata = kzalloc(sizeof(*metadata), GFP_KERNEL);
346         if (!metadata) {
347                 DMERR("kzalloc for metadata failed");
348                 err = -ENOMEM;
349                 goto free_header;
350         }
351
352         metadata->header = header;
353         table_length = le32_to_cpu(header->table_length);
354
355         if (table_length == 0 ||
356                 table_length > (VERITY_METADATA_SIZE -
357                         sizeof(struct android_metadata_header)))
358                 goto free_metadata;
359
360         metadata->verity_table = kzalloc(table_length + 1, GFP_KERNEL);
361
362         if (!metadata->verity_table) {
363                 DMERR("kzalloc verity_table failed");
364                 err = -ENOMEM;
365                 goto free_metadata;
366         }
367
368         if (sizeof(struct android_metadata_header) +
369                         table_length <= PAGE_SIZE) {
370                 memcpy(metadata->verity_table, page_address(payload.page_io[0])
371                         + sizeof(struct android_metadata_header),
372                         table_length);
373         } else {
374                 copy_length = PAGE_SIZE -
375                         sizeof(struct android_metadata_header);
376                 memcpy(metadata->verity_table, page_address(payload.page_io[0])
377                         + sizeof(struct android_metadata_header),
378                         copy_length);
379                 table_length -= copy_length;
380                 offset = copy_length;
381                 i = 1;
382                 while (table_length != 0) {
383                         if (table_length > PAGE_SIZE) {
384                                 memcpy(metadata->verity_table + offset,
385                                         page_address(payload.page_io[i]),
386                                         PAGE_SIZE);
387                                 offset += PAGE_SIZE;
388                                 table_length -= PAGE_SIZE;
389                         } else {
390                                 memcpy(metadata->verity_table + offset,
391                                         page_address(payload.page_io[i]),
392                                         table_length);
393                                 table_length = 0;
394                         }
395                         i++;
396                 }
397         }
398         metadata->verity_table[table_length] = '\0';
399
400         goto free_payload;
401
402 free_metadata:
403         kfree(metadata);
404 free_header:
405         kfree(header);
406         metadata = ERR_PTR(err);
407 free_payload:
408         for (i = 0; i < payload.number_of_pages; i++)
409                 if (payload.page_io[i])
410                         __free_page(payload.page_io[i]);
411         kfree(payload.page_io);
412
413         DMINFO("verity_table: %s", metadata->verity_table);
414 blkdev_release:
415         blkdev_put(bdev, FMODE_READ);
416         return metadata;
417 }
418
419 /* helper functions to extract properties from dts */
420 const char *find_dt_value(const char *name)
421 {
422         struct device_node *firmware;
423         const char *value;
424
425         firmware = of_find_node_by_path("/firmware/android");
426         if (!firmware)
427                 return NULL;
428         value = of_get_property(firmware, name, NULL);
429         of_node_put(firmware);
430
431         return value;
432 }
433
434 static bool is_unlocked(void)
435 {
436         static const char unlocked[]  = "orange";
437         static const char verified_boot_prop[] = "verifiedbootstate";
438         const char *value;
439
440         value = find_dt_value(verified_boot_prop);
441         if (!value)
442                 value = verifiedbootstate;
443
444         return !strncmp(value, unlocked, sizeof(unlocked) - 1);
445 }
446
447 static int verity_mode(void)
448 {
449         static const char enforcing[] = "enforcing";
450         static const char verified_mode_prop[] = "veritymode";
451         const char *value;
452
453         value = find_dt_value(verified_mode_prop);
454         if (!value)
455                 value = veritymode;
456         if (!strncmp(value, enforcing, sizeof(enforcing) - 1))
457                 return DM_VERITY_MODE_RESTART;
458
459         return DM_VERITY_MODE_EIO;
460 }
461
462 static int verify_header(struct android_metadata_header *header)
463 {
464         int retval = -EINVAL;
465
466         if (is_unlocked() && le32_to_cpu(header->magic_number) ==
467                 VERITY_METADATA_MAGIC_DISABLE) {
468                 retval = VERITY_STATE_DISABLE;
469                 return retval;
470         }
471
472         if (!(le32_to_cpu(header->magic_number) ==
473                 VERITY_METADATA_MAGIC_NUMBER) ||
474                 (le32_to_cpu(header->magic_number) ==
475                 VERITY_METADATA_MAGIC_DISABLE)) {
476                 DMERR("Incorrect magic number");
477                 return retval;
478         }
479
480         if (le32_to_cpu(header->protocol_version) !=
481                 VERITY_METADATA_VERSION) {
482                 DMERR("Unsupported version %u",
483                         le32_to_cpu(header->protocol_version));
484                 return retval;
485         }
486
487         return 0;
488 }
489
490 static int verify_verity_signature(char *key_id,
491                 struct android_metadata *metadata)
492 {
493         key_ref_t key_ref;
494         struct key *key;
495         struct public_key_signature *pks = NULL;
496         int retval = -EINVAL;
497
498         key_ref = keyring_search(make_key_ref(system_trusted_keyring, 1),
499                 &key_type_asymmetric, key_id);
500
501         if (IS_ERR(key_ref)) {
502                 DMERR("keyring: key not found");
503                 return -ENOKEY;
504         }
505
506         key = key_ref_to_ptr(key_ref);
507
508         pks = table_make_digest(PKEY_HASH_SHA256,
509                         (const void *)metadata->verity_table,
510                         le32_to_cpu(metadata->header->table_length));
511
512         if (IS_ERR(pks)) {
513                 DMERR("hashing failed");
514                 goto error;
515         }
516
517         retval = table_extract_mpi_array(pks, &metadata->header->signature[0],
518                                 RSANUMBYTES);
519         if (retval < 0) {
520                 DMERR("Error extracting mpi %d", retval);
521                 goto error;
522         }
523
524         retval = verify_signature(key, pks);
525         mpi_free(pks->rsa.s);
526 error:
527         kfree(pks);
528         key_put(key);
529
530         return retval;
531 }
532
533 static void handle_error(void)
534 {
535         int mode = verity_mode();
536         if (mode == DM_VERITY_MODE_RESTART) {
537                 DMERR("triggering restart");
538                 kernel_restart("dm-verity device corrupted");
539         } else {
540                 DMERR("Mounting verity root failed");
541         }
542 }
543
544 static inline bool test_mult_overflow(sector_t a, u32 b)
545 {
546         sector_t r = (sector_t)~0ULL;
547
548         sector_div(r, b);
549         return a > r;
550 }
551
552 /*
553  * Target parameters:
554  *      <key id>        Key id of the public key in the system keyring.
555  *                      Verity metadata's signature would be verified against
556  *                      this. If the key id contains spaces, replace them
557  *                      with '#'.
558  *      <block device>  The block device for which dm-verity is being setup.
559  */
560 static int android_verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
561 {
562         dev_t uninitialized_var(dev);
563         struct android_metadata *uninitialized_var(metadata);
564         int err = 0, i, mode;
565         char *key_id, *table_ptr, dummy,
566         *verity_table_args[VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS];
567         /* One for specifying number of opt args and one for mode */
568         sector_t data_sectors;
569         u32 data_block_size;
570         unsigned int major, minor,
571         no_of_args = VERITY_TABLE_ARGS + 2 + VERITY_TABLE_OPT_FEC_ARGS;
572         struct fec_header fec;
573         struct fec_ecc_metadata uninitialized_var(ecc);
574         char buf[FEC_ARG_LENGTH], *buf_ptr;
575         unsigned long long tmpll;
576
577         if (argc != 2) {
578                 DMERR("Incorrect number of arguments");
579                 handle_error();
580                 return -EINVAL;
581         }
582
583         /* should come as one of the arguments for the verity target */
584         key_id = argv[0];
585         strreplace(argv[0], '#', ' ');
586
587         if (sscanf(argv[1], "%u:%u%c", &major, &minor, &dummy) == 2) {
588                 dev = MKDEV(major, minor);
589                 if (MAJOR(dev) != major || MINOR(dev) != minor) {
590                         DMERR("Incorrect bdev major minor number");
591                         handle_error();
592                         return -EOVERFLOW;
593                 }
594         }
595
596         DMINFO("key:%s dev:%s", argv[0], argv[1]);
597
598         if (extract_fec_header(dev, &fec, &ecc)) {
599                 DMERR("Error while extracting fec header");
600                 handle_error();
601                 return -EINVAL;
602         }
603
604         metadata = extract_metadata(dev, &fec);
605
606         if (IS_ERR(metadata)) {
607                 DMERR("Error while extracting metadata");
608                 handle_error();
609                 return -EINVAL;
610         }
611
612         err = verify_header(metadata->header);
613
614         if (err == VERITY_STATE_DISABLE) {
615                 DMERR("Mounting root with verity disabled");
616                 return -EINVAL;
617         } else if (err) {
618                 DMERR("Verity header handle error");
619                 handle_error();
620                 goto free_metadata;
621         }
622
623         err = verify_verity_signature(key_id, metadata);
624
625         if (err) {
626                 DMERR("Signature verification failed");
627                 handle_error();
628                 goto free_metadata;
629         } else
630                 DMINFO("Signature verification success");
631
632         table_ptr = metadata->verity_table;
633
634         for (i = 0; i < VERITY_TABLE_ARGS; i++) {
635                 verity_table_args[i] = strsep(&table_ptr, " ");
636                 if (verity_table_args[i] == NULL)
637                         break;
638         }
639
640         if (i != VERITY_TABLE_ARGS) {
641                 DMERR("Verity table not in the expected format");
642                 err = -EINVAL;
643                 handle_error();
644                 goto free_metadata;
645         }
646
647         if (sscanf(verity_table_args[5], "%llu%c", &tmpll, &dummy)
648                                                         != 1) {
649                 DMERR("Verity table not in the expected format");
650                 handle_error();
651                 err = -EINVAL;
652                 goto free_metadata;
653         }
654
655         if (tmpll > ULONG_MAX) {
656                 DMERR("<num_data_blocks> too large. Forgot to turn on CONFIG_LBDAF?");
657                 handle_error();
658                 err = -EINVAL;
659                 goto free_metadata;
660         }
661
662         data_sectors = tmpll;
663
664         if (sscanf(verity_table_args[3], "%u%c", &data_block_size, &dummy)
665                                                                 != 1) {
666                 DMERR("Verity table not in the expected format");
667                 handle_error();
668                 err = -EINVAL;
669                 goto free_metadata;
670         }
671
672         if (test_mult_overflow(data_sectors, data_block_size >>
673                                                         SECTOR_SHIFT)) {
674                 DMERR("data_sectors too large");
675                 handle_error();
676                 err = -EOVERFLOW;
677                 goto free_metadata;
678         }
679
680         data_sectors *= data_block_size >> SECTOR_SHIFT;
681         DMINFO("Data sectors %llu", (unsigned long long)data_sectors);
682
683         /* update target length */
684         ti->len = data_sectors;
685
686         /*substitute data_dev and hash_dev*/
687         verity_table_args[1] = argv[1];
688         verity_table_args[2] = argv[1];
689
690         mode = verity_mode();
691
692         if (ecc.valid && IS_BUILTIN(CONFIG_DM_VERITY_FEC)) {
693                 if (mode) {
694                         err = snprintf(buf, FEC_ARG_LENGTH,
695                         "%u %s " VERITY_TABLE_OPT_FEC_FORMAT,
696                         1 + VERITY_TABLE_OPT_FEC_ARGS,
697                         mode == DM_VERITY_MODE_RESTART ?
698                         VERITY_TABLE_OPT_RESTART : VERITY_TABLE_OPT_LOGGING,
699                         argv[1], ecc.start / FEC_BLOCK_SIZE, ecc.blocks,
700                         ecc.roots);
701                 } else {
702                         err = snprintf(buf, FEC_ARG_LENGTH,
703                         "%u " VERITY_TABLE_OPT_FEC_FORMAT,
704                         VERITY_TABLE_OPT_FEC_ARGS, argv[1],
705                         ecc.start / FEC_BLOCK_SIZE, ecc.blocks, ecc.roots);
706                 }
707         } else if (mode) {
708                 err = snprintf(buf, FEC_ARG_LENGTH,
709                         "2 " VERITY_TABLE_OPT_IGNZERO " %s",
710                         mode == DM_VERITY_MODE_RESTART ?
711                         VERITY_TABLE_OPT_RESTART : VERITY_TABLE_OPT_LOGGING);
712         } else {
713                 err = snprintf(buf, FEC_ARG_LENGTH, "1 %s",
714                                  "ignore_zero_blocks");
715         }
716
717         if (err < 0 || err >= FEC_ARG_LENGTH)
718                 goto free_metadata;
719
720         buf_ptr = buf;
721
722         for (i = VERITY_TABLE_ARGS; i < (VERITY_TABLE_ARGS +
723                 VERITY_TABLE_OPT_FEC_ARGS + 2); i++) {
724                 verity_table_args[i] = strsep(&buf_ptr, " ");
725                 if (verity_table_args[i] == NULL) {
726                         no_of_args = i;
727                         break;
728                 }
729         }
730
731         err = verity_ctr(ti, no_of_args, verity_table_args);
732
733 free_metadata:
734         kfree(metadata->header);
735         kfree(metadata->verity_table);
736         kfree(metadata);
737         return err;
738 }
739
740 static struct target_type android_verity_target = {
741         .name                   = "android-verity",
742         .version                = {1, 0, 0},
743         .module                 = THIS_MODULE,
744         .ctr                    = android_verity_ctr,
745         .dtr                    = verity_dtr,
746         .map                    = verity_map,
747         .status                 = verity_status,
748         .ioctl                  = verity_ioctl,
749         .merge                  = verity_merge,
750         .iterate_devices        = verity_iterate_devices,
751         .io_hints               = verity_io_hints,
752 };
753
754 static int __init dm_android_verity_init(void)
755 {
756         int r;
757
758         r = dm_register_target(&android_verity_target);
759         if (r < 0)
760                 DMERR("register failed %d", r);
761
762         return r;
763 }
764
765 static void __exit dm_android_verity_exit(void)
766 {
767         dm_unregister_target(&android_verity_target);
768 }
769
770 module_init(dm_android_verity_init);
771 module_exit(dm_android_verity_exit);