Rewrite veritysetup to use libcryptsetup.
[platform/upstream/cryptsetup.git] / lib / verity / verity.c
1 /*
2  * dm-verity volume handling
3  *
4  * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
5  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <netinet/in.h>
28
29 #include "libcryptsetup.h"
30 #include "verity.h"
31 #include "internal.h"
32
33 /* Read verity superblock from disk */
34 int VERITY_read_sb(struct crypt_device *cd,
35                    const char *device,
36                    size_t sb_offset,
37                    struct crypt_params_verity *params)
38 {
39         struct verity_sb sb = {};
40         ssize_t hdr_size = sizeof(struct verity_sb);
41         int devfd = 0;
42         long long sb_data_blocks;
43
44         log_dbg("Reading VERITY header of size %d on device %s, offset %u.",
45                 sizeof(struct verity_sb), device, (unsigned)sb_offset);
46
47         devfd = open(device ,O_RDONLY | O_DIRECT);
48         if(devfd == -1) {
49                 log_err(cd, _("Cannot open device %s.\n"), device);
50                 return -EINVAL;
51         }
52
53         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
54            read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
55                 close(devfd);
56                 return -EIO;
57         }
58         close(devfd);
59
60         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
61                 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
62                 return -EINVAL;
63         }
64
65         if (sb.version > 1) {
66                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
67                 return -EINVAL;
68         }
69
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"));
75                 return -EINVAL;
76         }
77
78         sb_data_blocks = ((unsigned long long)ntohl(sb.data_blocks_hi) << 31 << 1) |
79                                 ntohl(sb.data_blocks_lo);
80         if (sb_data_blocks < 0 ||
81             (off_t)sb_data_blocks < 0 ||
82             (off_t)sb_data_blocks != sb_data_blocks) {
83                 log_err(cd, _("VERITY header data block size mismatch.\n"));
84                 return -EINVAL;
85         }
86
87         // FIXME alloc error
88         params->hash_name = strdup((const char*)sb.algorithm);
89         params->data_block_size = 1 << sb.data_block_bits;
90         params->hash_block_size = 1 << sb.hash_block_bits;
91         params->data_size = sb_data_blocks;
92         params->salt_size = ntohs(sb.salt_size);
93         params->salt = malloc(params->salt_size);
94         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
95         params->hash_area_offset = sb_offset;
96         params->version = sb.version;
97
98         return 0;
99 }
100
101 /* Write verity superblock to disk */
102 int VERITY_write_sb(struct crypt_device *cd,
103                    const char *device,
104                    size_t sb_offset,
105                    struct crypt_params_verity *params)
106 {
107         struct verity_sb sb = {};
108         ssize_t hdr_size = sizeof(struct verity_sb);
109         int r, devfd = 0;
110
111         log_dbg("Updating VERITY header of size %d on device %s, offset %u.",
112                 sizeof(struct verity_sb), device, (unsigned)sb_offset);
113
114         devfd = open(device, O_RDWR | O_DIRECT);
115         if(devfd == -1) {
116                 log_err(cd, _("Cannot open device %s.\n"), device);
117                 return -EINVAL;
118         }
119
120         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
121         sb.version = params->version;
122         sb.data_block_bits = ffs(params->data_block_size) - 1;
123         sb.hash_block_bits = ffs(params->hash_block_size) - 1;
124         sb.salt_size = htons(params->salt_size);
125         sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
126         sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
127         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
128         memcpy(sb.salt, params->salt, params->salt_size);
129
130         r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
131         if (r)
132                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
133         close(devfd);
134
135         return r;
136 }
137
138 /* Calculate hash offset in hash blocks */
139 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
140 {
141         uint64_t hash_offset = params->hash_area_offset;
142
143         if (params->flags & CRYPT_VERITY_NO_HEADER)
144                 return hash_offset / params->hash_block_size;
145
146         hash_offset += sizeof(struct verity_sb);
147         hash_offset += params->hash_block_size - 1;
148
149         return hash_offset / params->hash_block_size;
150 }
151
152 /* Activate verity device in kernel device-mapper */
153 int VERITY_activate(struct crypt_device *cd,
154                      const char *name,
155                      const char *hash_device,
156                      const char *root_hash,
157                      size_t root_hash_size,
158                      struct crypt_params_verity *verity_hdr,
159                      uint32_t flags)
160 {
161         struct crypt_dm_active_verity dmd;
162         uint64_t offset = 0;
163         int r;
164
165         log_dbg("Trying to activate VERITY device %s using hash %s.",
166                 name ?: "[none]", verity_hdr->hash_name);
167
168         if (flags & CRYPT_VERITY_CHECK_HASH) {
169                 r = VERITY_verify(cd, verity_hdr,
170                                   crypt_get_device_name(cd), hash_device,
171                                   root_hash, root_hash_size);
172                 if (r < 0)
173                         return r;
174         }
175
176         if (!name)
177                 return 0;
178
179         dmd.data_device = crypt_get_device_name(cd);
180         dmd.hash_device = hash_device;
181         dmd.root_hash = root_hash;
182         dmd.root_hash_size = root_hash_size;
183         dmd.hash_offset = VERITY_hash_offset_block(verity_hdr),
184         dmd.flags = CRYPT_ACTIVATE_READONLY;
185         dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
186
187         r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
188                                     &dmd.size, &offset, &dmd.flags);
189         if (r)
190                 return r;
191
192         r = dm_create_verity(name, verity_hdr, &dmd);
193         if (r < 0)
194                 return r;
195
196         r = dm_status_verity_ok(name);
197         if (r < 0)
198                 return r;
199
200         if (!r)
201                 log_err(cd, _("Verity device detected corruption after activation.\n"));
202         return 0;
203 }