blob: d216fc76d350280572f341410b6de3cd51b804d7 [file] [log] [blame]
Sami Tolvanenffa39382015-12-03 16:01:51 -05001/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2015 Google, Inc.
4 *
5 * Author: Mikulas Patocka <mpatocka@redhat.com>
6 *
7 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
8 *
9 * This file is released under the GPLv2.
10 */
11
12#ifndef DM_VERITY_H
13#define DM_VERITY_H
14
15#include "dm-bufio.h"
16#include <linux/device-mapper.h>
17#include <crypto/hash.h>
18
19#define DM_VERITY_MAX_LEVELS 63
20
21enum verity_mode {
22 DM_VERITY_MODE_EIO,
23 DM_VERITY_MODE_LOGGING,
24 DM_VERITY_MODE_RESTART
25};
26
27enum verity_block_type {
28 DM_VERITY_BLOCK_TYPE_DATA,
29 DM_VERITY_BLOCK_TYPE_METADATA
30};
31
Sami Tolvanena739ff32015-12-03 14:26:30 +000032struct dm_verity_fec;
33
Sami Tolvanenffa39382015-12-03 16:01:51 -050034struct dm_verity {
35 struct dm_dev *data_dev;
36 struct dm_dev *hash_dev;
37 struct dm_target *ti;
38 struct dm_bufio_client *bufio;
39 char *alg_name;
40 struct crypto_shash *tfm;
41 u8 *root_digest; /* digest of the root block */
42 u8 *salt; /* salt: its size is salt_size */
Sami Tolvanen0cc37c22015-12-03 14:26:31 +000043 u8 *zero_digest; /* digest for a zero block */
Sami Tolvanenffa39382015-12-03 16:01:51 -050044 unsigned salt_size;
45 sector_t data_start; /* data offset in 512-byte sectors */
46 sector_t hash_start; /* hash start in blocks */
47 sector_t data_blocks; /* the number of data blocks */
48 sector_t hash_blocks; /* the number of hash blocks */
49 unsigned char data_dev_block_bits; /* log2(data blocksize) */
50 unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
51 unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
52 unsigned char levels; /* the number of tree levels */
53 unsigned char version;
54 unsigned digest_size; /* digest size for the current hash algorithm */
55 unsigned shash_descsize;/* the size of temporary space for crypto */
56 int hash_failed; /* set to 1 if hash of any block failed */
57 enum verity_mode mode; /* mode for handling verification errors */
58 unsigned corrupted_errs;/* Number of errors for corrupted blocks */
59
60 struct workqueue_struct *verify_wq;
61
62 /* starting blocks for each tree level. 0 is the lowest level. */
63 sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
Sami Tolvanena739ff32015-12-03 14:26:30 +000064
65 struct dm_verity_fec *fec; /* forward error correction */
Patrik Torstensson142d4b52018-03-22 18:18:04 -070066 unsigned long *validated_blocks; /* bitset blocks validated */
Sami Tolvanenffa39382015-12-03 16:01:51 -050067};
68
69struct dm_verity_io {
70 struct dm_verity *v;
71
72 /* original value of bio->bi_end_io */
73 bio_end_io_t *orig_bi_end_io;
74
75 sector_t block;
76 unsigned n_blocks;
77
78 struct bvec_iter iter;
79
80 struct work_struct work;
81
82 /*
83 * Three variably-size fields follow this struct:
84 *
85 * u8 hash_desc[v->shash_descsize];
86 * u8 real_digest[v->digest_size];
87 * u8 want_digest[v->digest_size];
88 *
89 * To access them use: verity_io_hash_desc(), verity_io_real_digest()
90 * and verity_io_want_digest().
91 */
92};
93
94static inline struct shash_desc *verity_io_hash_desc(struct dm_verity *v,
95 struct dm_verity_io *io)
96{
97 return (struct shash_desc *)(io + 1);
98}
99
100static inline u8 *verity_io_real_digest(struct dm_verity *v,
101 struct dm_verity_io *io)
102{
103 return (u8 *)(io + 1) + v->shash_descsize;
104}
105
106static inline u8 *verity_io_want_digest(struct dm_verity *v,
107 struct dm_verity_io *io)
108{
109 return (u8 *)(io + 1) + v->shash_descsize + v->digest_size;
110}
111
Sami Tolvanena739ff32015-12-03 14:26:30 +0000112static inline u8 *verity_io_digest_end(struct dm_verity *v,
113 struct dm_verity_io *io)
114{
115 return verity_io_want_digest(v, io) + v->digest_size;
116}
117
Sami Tolvanenbb4d73a2015-12-03 16:30:36 -0500118extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
119 struct bvec_iter *iter,
120 int (*process)(struct dm_verity *v,
121 struct dm_verity_io *io,
122 u8 *data, size_t len));
123
Sami Tolvanenffa39382015-12-03 16:01:51 -0500124extern int verity_hash(struct dm_verity *v, struct shash_desc *desc,
125 const u8 *data, size_t len, u8 *digest);
126
127extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
Sami Tolvanen0cc37c22015-12-03 14:26:31 +0000128 sector_t block, u8 *digest, bool *is_zero);
Sami Tolvanenffa39382015-12-03 16:01:51 -0500129
Badhri Jagan Sridharan8bb45a52015-12-14 20:09:39 -0800130extern void verity_status(struct dm_target *ti, status_type_t type,
131 unsigned status_flags, char *result, unsigned maxlen);
Badhri Jagan Sridharan424861b2016-08-09 12:47:37 -0700132extern int verity_prepare_ioctl(struct dm_target *ti,
133 struct block_device **bdev, fmode_t *mode);
Badhri Jagan Sridharan8bb45a52015-12-14 20:09:39 -0800134extern int verity_iterate_devices(struct dm_target *ti,
135 iterate_devices_callout_fn fn, void *data);
136extern void verity_io_hints(struct dm_target *ti, struct queue_limits *limits);
137extern void verity_dtr(struct dm_target *ti);
138extern int verity_ctr(struct dm_target *ti, unsigned argc, char **argv);
139extern int verity_map(struct dm_target *ti, struct bio *bio);
David Zeuthen9dc978d2017-01-24 13:17:01 -0500140extern void dm_verity_avb_error_handler(void);
Sami Tolvanenffa39382015-12-03 16:01:51 -0500141#endif /* DM_VERITY_H */