Fix some strings, fix sb_offset.
[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                    uint64_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         uint64_t sb_data_blocks;
43
44         log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
45                 sizeof(struct verity_sb), device, 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 = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
79                                 ntohl(sb.data_blocks_lo);
80
81         params->hash_name = strdup((const char*)sb.algorithm);
82         if (!params->hash_name)
83                 return -ENOMEM;
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);
89         if (!params->salt)
90                 return -ENOMEM;
91         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
92         params->hash_area_offset = sb_offset;
93         params->version = sb.version;
94
95         return 0;
96 }
97
98 /* Write verity superblock to disk */
99 int VERITY_write_sb(struct crypt_device *cd,
100                    const char *device,
101                    uint64_t sb_offset,
102                    struct crypt_params_verity *params)
103 {
104         struct verity_sb sb = {};
105         ssize_t hdr_size = sizeof(struct verity_sb);
106         int r, devfd = 0;
107
108         log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
109                 sizeof(struct verity_sb), device, sb_offset);
110
111         devfd = open(device, O_RDWR | O_DIRECT);
112         if(devfd == -1) {
113                 log_err(cd, _("Cannot open device %s.\n"), device);
114                 return -EINVAL;
115         }
116
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);
126
127         r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
128         if (r)
129                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
130         close(devfd);
131
132         return r;
133 }
134
135 /* Calculate hash offset in hash blocks */
136 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
137 {
138         uint64_t hash_offset = params->hash_area_offset;
139
140         if (params->flags & CRYPT_VERITY_NO_HEADER)
141                 return hash_offset / params->hash_block_size;
142
143         hash_offset += sizeof(struct verity_sb);
144         hash_offset += params->hash_block_size - 1;
145
146         return hash_offset / params->hash_block_size;
147 }
148
149 /* Activate verity device in kernel device-mapper */
150 int VERITY_activate(struct crypt_device *cd,
151                      const char *name,
152                      const char *hash_device,
153                      const char *root_hash,
154                      size_t root_hash_size,
155                      struct crypt_params_verity *verity_hdr,
156                      uint32_t flags)
157 {
158         struct crypt_dm_active_verity dmd;
159         uint64_t offset = 0;
160         int r;
161
162         log_dbg("Trying to activate VERITY device %s using hash %s.",
163                 name ?: "[none]", verity_hdr->hash_name);
164
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);
169                 if (r < 0)
170                         return r;
171         }
172
173         if (!name)
174                 return 0;
175
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;
183
184         r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
185                                     &dmd.size, &offset, &dmd.flags);
186         if (r)
187                 return r;
188
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"));
192                 return -ENOTSUP;
193         }
194         if (r < 0)
195                 return r;
196
197         r = dm_status_verity_ok(name);
198         if (r < 0)
199                 return r;
200
201         if (!r)
202                 log_err(cd, _("Verity device detected corruption after activation.\n"));
203         return 0;
204 }