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 /* Read verity superblock from disk */
34 int VERITY_read_sb(struct crypt_device *cd,
37 struct crypt_params_verity *params)
39 struct verity_sb sb = {};
40 ssize_t hdr_size = sizeof(struct verity_sb);
42 uint64_t sb_data_blocks;
44 log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
45 sizeof(struct verity_sb), device, sb_offset);
47 devfd = open(device ,O_RDONLY | O_DIRECT);
49 log_err(cd, _("Cannot open device %s.\n"), device);
53 if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
54 read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
60 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
61 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
66 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
70 if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
71 sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
72 !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
73 ntohs(sb.salt_size) > VERITY_MAX_SALT_SIZE) {
74 log_err(cd, _("VERITY header corrupted.\n"));
78 sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
79 ntohl(sb.data_blocks_lo);
81 params->hash_name = strdup((const char*)sb.algorithm);
82 if (!params->hash_name)
84 params->data_block_size = 1 << sb.data_block_bits;
85 params->hash_block_size = 1 << sb.hash_block_bits;
86 params->data_size = sb_data_blocks;
87 params->salt_size = ntohs(sb.salt_size);
88 params->salt = malloc(params->salt_size);
91 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
92 params->hash_area_offset = sb_offset;
93 params->version = sb.version;
98 /* Write verity superblock to disk */
99 int VERITY_write_sb(struct crypt_device *cd,
102 struct crypt_params_verity *params)
104 struct verity_sb sb = {};
105 ssize_t hdr_size = sizeof(struct verity_sb);
108 log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
109 sizeof(struct verity_sb), device, sb_offset);
111 devfd = open(device, O_RDWR | O_DIRECT);
113 log_err(cd, _("Cannot open device %s.\n"), device);
117 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
118 sb.version = params->version;
119 sb.data_block_bits = ffs(params->data_block_size) - 1;
120 sb.hash_block_bits = ffs(params->hash_block_size) - 1;
121 sb.salt_size = htons(params->salt_size);
122 sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
123 sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
124 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
125 memcpy(sb.salt, params->salt, params->salt_size);
127 r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
129 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
135 /* Calculate hash offset in hash blocks */
136 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
138 uint64_t hash_offset = params->hash_area_offset;
140 if (params->flags & CRYPT_VERITY_NO_HEADER)
141 return hash_offset / params->hash_block_size;
143 hash_offset += sizeof(struct verity_sb);
144 hash_offset += params->hash_block_size - 1;
146 return hash_offset / params->hash_block_size;
149 /* Activate verity device in kernel device-mapper */
150 int VERITY_activate(struct crypt_device *cd,
152 const char *hash_device,
153 const char *root_hash,
154 size_t root_hash_size,
155 struct crypt_params_verity *verity_hdr,
158 struct crypt_dm_active_verity dmd;
162 log_dbg("Trying to activate VERITY device %s using hash %s.",
163 name ?: "[none]", verity_hdr->hash_name);
165 if (flags & CRYPT_VERITY_CHECK_HASH) {
166 r = VERITY_verify(cd, verity_hdr,
167 crypt_get_device_name(cd), hash_device,
168 root_hash, root_hash_size);
176 dmd.data_device = crypt_get_device_name(cd);
177 dmd.hash_device = hash_device;
178 dmd.root_hash = root_hash;
179 dmd.root_hash_size = root_hash_size;
180 dmd.hash_offset = VERITY_hash_offset_block(verity_hdr),
181 dmd.flags = CRYPT_ACTIVATE_READONLY;
182 dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
184 r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
185 &dmd.size, &offset, &dmd.flags);
189 r = dm_create_verity(name, verity_hdr, &dmd);
190 if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
191 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
197 r = dm_status_verity_ok(name);
202 log_err(cd, _("Verity device detected corruption after activation.\n"));