^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2012 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2015 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Mikulas Patocka <mpatocka@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #ifndef DM_VERITY_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define DM_VERITY_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/dm-bufio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/device-mapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define DM_VERITY_MAX_LEVELS 63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) enum verity_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) DM_VERITY_MODE_EIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) DM_VERITY_MODE_LOGGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) DM_VERITY_MODE_RESTART,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) DM_VERITY_MODE_PANIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) enum verity_block_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) DM_VERITY_BLOCK_TYPE_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) DM_VERITY_BLOCK_TYPE_METADATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct dm_verity_fec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct dm_verity {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct dm_dev *data_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct dm_dev *hash_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct dm_target *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct dm_bufio_client *bufio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) char *alg_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct crypto_ahash *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u8 *root_digest; /* digest of the root block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u8 *salt; /* salt: its size is salt_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u8 *zero_digest; /* digest for a zero block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned salt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) sector_t data_start; /* data offset in 512-byte sectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) sector_t hash_start; /* hash start in blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) sector_t data_blocks; /* the number of data blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) sector_t hash_blocks; /* the number of hash blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned char data_dev_block_bits; /* log2(data blocksize) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned char levels; /* the number of tree levels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned char version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned digest_size; /* digest size for the current hash algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned int ahash_reqsize;/* the size of temporary space for crypto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int hash_failed; /* set to 1 if hash of any block failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) enum verity_mode mode; /* mode for handling verification errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned corrupted_errs;/* Number of errors for corrupted blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct workqueue_struct *verify_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* starting blocks for each tree level. 0 is the lowest level. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct dm_verity_fec *fec; /* forward error correction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned long *validated_blocks; /* bitset blocks validated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char *signature_key_desc; /* signature keyring reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct dm_verity_io {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct dm_verity *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* original value of bio->bi_end_io */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) bio_end_io_t *orig_bi_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) sector_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned n_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct bvec_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Three variably-size fields follow this struct:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * u8 hash_req[v->ahash_reqsize];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * u8 real_digest[v->digest_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * u8 want_digest[v->digest_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * To access them use: verity_io_hash_req(), verity_io_real_digest()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * and verity_io_want_digest().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static inline struct ahash_request *verity_io_hash_req(struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct dm_verity_io *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return (struct ahash_request *)(io + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static inline u8 *verity_io_real_digest(struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct dm_verity_io *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return (u8 *)(io + 1) + v->ahash_reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static inline u8 *verity_io_want_digest(struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct dm_verity_io *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return (u8 *)(io + 1) + v->ahash_reqsize + v->digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static inline u8 *verity_io_digest_end(struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct dm_verity_io *io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return verity_io_want_digest(v, io) + v->digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) extern int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct bvec_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int (*process)(struct dm_verity *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) u8 *data, size_t len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) extern int verity_hash(struct dm_verity *v, struct ahash_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) const u8 *data, size_t len, u8 *digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) sector_t block, u8 *digest, bool *is_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #endif /* DM_VERITY_H */