2 * dm-verity volume handling
4 * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
27 #include <netinet/in.h>
29 #include "libcryptsetup.h"
33 /* FIXME: not yet final on-disk format! Add UUID etc */
37 uint8_t data_block_bits;
38 uint8_t hash_block_bits;
42 uint32_t data_blocks_hi;
43 uint32_t data_blocks_lo;
44 uint8_t algorithm[16];
45 uint8_t salt[VERITY_MAX_SALT_SIZE];
49 /* Read verity superblock from disk */
50 int VERITY_read_sb(struct crypt_device *cd,
53 struct crypt_params_verity *params)
55 struct verity_sb sb = {};
56 ssize_t hdr_size = sizeof(struct verity_sb);
58 uint64_t sb_data_blocks;
60 log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
61 sizeof(struct verity_sb), device, sb_offset);
63 devfd = open(device ,O_RDONLY | O_DIRECT);
65 log_err(cd, _("Cannot open device %s.\n"), device);
69 if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
70 read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
76 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
77 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
82 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
86 if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
87 sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
88 !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
89 ntohs(sb.salt_size) > VERITY_MAX_SALT_SIZE) {
90 log_err(cd, _("VERITY header corrupted.\n"));
94 sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
95 ntohl(sb.data_blocks_lo);
97 params->hash_name = strdup((const char*)sb.algorithm);
98 if (!params->hash_name)
100 params->data_block_size = 1 << sb.data_block_bits;
101 params->hash_block_size = 1 << sb.hash_block_bits;
102 params->data_size = sb_data_blocks;
103 params->salt_size = ntohs(sb.salt_size);
104 params->salt = malloc(params->salt_size);
107 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
108 params->hash_area_offset = sb_offset;
109 params->version = sb.version;
114 /* Write verity superblock to disk */
115 int VERITY_write_sb(struct crypt_device *cd,
118 struct crypt_params_verity *params)
120 struct verity_sb sb = {};
121 ssize_t hdr_size = sizeof(struct verity_sb);
124 log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
125 sizeof(struct verity_sb), device, sb_offset);
127 devfd = open(device, O_RDWR | O_DIRECT);
129 log_err(cd, _("Cannot open device %s.\n"), device);
133 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
134 sb.version = params->version;
135 sb.data_block_bits = ffs(params->data_block_size) - 1;
136 sb.hash_block_bits = ffs(params->hash_block_size) - 1;
137 sb.salt_size = htons(params->salt_size);
138 sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
139 sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
140 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
141 memcpy(sb.salt, params->salt, params->salt_size);
143 r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
145 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
151 /* Calculate hash offset in hash blocks */
152 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
154 uint64_t hash_offset = params->hash_area_offset;
156 if (params->flags & CRYPT_VERITY_NO_HEADER)
157 return hash_offset / params->hash_block_size;
159 hash_offset += sizeof(struct verity_sb);
160 hash_offset += params->hash_block_size - 1;
162 return hash_offset / params->hash_block_size;
165 /* Activate verity device in kernel device-mapper */
166 int VERITY_activate(struct crypt_device *cd,
168 const char *hash_device,
169 const char *root_hash,
170 size_t root_hash_size,
171 struct crypt_params_verity *verity_hdr,
174 struct crypt_dm_active_device dmd;
178 log_dbg("Trying to activate VERITY device %s using hash %s.",
179 name ?: "[none]", verity_hdr->hash_name);
181 if (flags & CRYPT_VERITY_CHECK_HASH) {
182 r = VERITY_verify(cd, verity_hdr,
183 crypt_get_device_name(cd), hash_device,
184 root_hash, root_hash_size);
192 dmd.target = DM_VERITY;
193 dmd.data_device = crypt_get_device_name(cd);
194 dmd.u.verity.hash_device = hash_device;
195 dmd.u.verity.root_hash = root_hash;
196 dmd.u.verity.root_hash_size = root_hash_size;
197 dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
198 dmd.flags = CRYPT_ACTIVATE_READONLY;
199 dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
201 dmd.u.verity.vp = verity_hdr;
203 r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
204 &dmd.size, &offset, &dmd.flags);
208 r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
209 if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
210 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
216 r = dm_status_verity_ok(name);
221 log_err(cd, _("Verity device detected corruption after activation.\n"));