1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* dm-verity Forward Error Correction (FEC) support
*
* Copyright (C) 2015 Google, Inc. All rights reserved.
* Copyright (C) 2017-2021 Red Hat, Inc. All rights reserved.
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <errno.h>
#include "verity.h"
#include "internal.h"
#include "rs.h"
/* ecc parameters */
#define FEC_RSM 255
#define FEC_MIN_RSN 231
#define FEC_MAX_RSN 253
#define FEC_INPUT_DEVICES 2
/* parameters to init_rs_char */
#define FEC_PARAMS(roots) \
8, /* symbol size in bits */ \
0x11d, /* field generator polynomial coefficients */ \
0, /* first root of the generator */ \
1, /* primitive element to generate polynomial roots */ \
(roots), /* polynomial degree (number of roots) */ \
0 /* padding bytes at the front of shortened block */
struct fec_input_device {
struct device *device;
int fd;
uint64_t start;
uint64_t count;
};
struct fec_context {
uint32_t rsn;
uint32_t roots;
uint64_t size;
uint64_t blocks;
uint64_t rounds;
uint32_t block_size;
struct fec_input_device *inputs;
size_t ninputs;
};
/* computes ceil(x / y) */
static inline uint64_t FEC_div_round_up(uint64_t x, uint64_t y)
{
return (x / y) + (x % y > 0 ? 1 : 0);
}
/* returns a physical offset for the given RS offset */
static inline uint64_t FEC_interleave(struct fec_context *ctx, uint64_t offset)
{
return (offset / ctx->rsn) +
(offset % ctx->rsn) * ctx->rounds * ctx->block_size;
}
/* returns data for a byte at the specified RS offset */
static int FEC_read_interleaved(struct fec_context *ctx, uint64_t i,
void *output, size_t count)
{
size_t n;
uint64_t offset = FEC_interleave(ctx, i);
/* offsets outside input area are assumed to contain zeros */
if (offset >= ctx->size) {
memset(output, 0, count);
return 0;
}
/* find the correct input device and read from it */
for (n = 0; n < ctx->ninputs; ++n) {
if (offset >= ctx->inputs[n].count) {
offset -= ctx->inputs[n].count;
continue;
}
/* FIXME: read_lseek_blockwise candidate */
if (lseek(ctx->inputs[n].fd, ctx->inputs[n].start + offset, SEEK_SET) < 0)
return -1;
return (read_buffer(ctx->inputs[n].fd, output, count) == (ssize_t)count) ? 0 : -1;
}
/* should never be reached */
return -1;
}
/* encodes/decode inputs to/from fd */
static int FEC_process_inputs(struct crypt_device *cd,
struct crypt_params_verity *params,
struct fec_input_device *inputs,
size_t ninputs, int fd,
int decode, unsigned int *errors)
{
int r = 0;
unsigned int i;
struct fec_context ctx;
uint32_t b;
uint64_t n;
uint8_t rs_block[FEC_RSM];
uint8_t *buf = NULL;
void *rs;
/* initialize parameters */
ctx.roots = params->fec_roots;
ctx.rsn = FEC_RSM - ctx.roots;
ctx.block_size = params->data_block_size;
ctx.inputs = inputs;
ctx.ninputs = ninputs;
rs = init_rs_char(FEC_PARAMS(ctx.roots));
if (!rs) {
log_err(cd, _("Failed to allocate RS context."));
return -ENOMEM;
}
/* calculate the total area covered by error correction codes */
ctx.size = 0;
for (n = 0; n < ctx.ninputs; ++n) {
log_dbg(cd, "FEC input %s, offset %" PRIu64 " [bytes], length %" PRIu64 " [bytes]",
device_path(ctx.inputs[n].device), ctx.inputs[n].start, ctx.inputs[n].count);
ctx.size += ctx.inputs[n].count;
}
/* each byte in a data block is covered by a different code */
ctx.blocks = FEC_div_round_up(ctx.size, ctx.block_size);
ctx.rounds = FEC_div_round_up(ctx.blocks, ctx.rsn);
buf = malloc((size_t)ctx.block_size * ctx.rsn);
if (!buf) {
log_err(cd, _("Failed to allocate buffer."));
r = -ENOMEM;
goto out;
}
/* encode/decode input */
for (n = 0; n < ctx.rounds; ++n) {
for (i = 0; i < ctx.rsn; ++i) {
if (FEC_read_interleaved(&ctx, n * ctx.rsn * ctx.block_size + i,
&buf[i * ctx.block_size], ctx.block_size)) {
log_err(cd, _("Failed to read RS block %" PRIu64 " byte %d."), n, i);
r = -EIO;
goto out;
}
}
for (b = 0; b < ctx.block_size; ++b) {
for (i = 0; i < ctx.rsn; ++i)
rs_block[i] = buf[i * ctx.block_size + b];
/* decoding from parity device */
if (decode) {
if (read_buffer(fd, &rs_block[ctx.rsn], ctx.roots) < 0) {
log_err(cd, _("Failed to read parity for RS block %" PRIu64 "."), n);
r = -EIO;
goto out;
}
/* coverity[tainted_data] */
r = decode_rs_char(rs, rs_block);
if (r < 0) {
log_err(cd, _("Failed to repair parity for block %" PRIu64 "."), n);
goto out;
}
/* return number of detected errors */
if (errors)
*errors += r;
r = 0;
} else {
/* encoding and writing parity data to fec device */
encode_rs_char(rs, rs_block, &rs_block[ctx.rsn]);
if (write_buffer(fd, &rs_block[ctx.rsn], ctx.roots) < 0) {
log_err(cd, _("Failed to write parity for RS block %" PRIu64 "."), n);
r = -EIO;
goto out;
}
}
}
}
out:
free_rs_char(rs);
free(buf);
return r;
}
int VERITY_FEC_process(struct crypt_device *cd,
struct crypt_params_verity *params,
struct device *fec_device, int check_fec,
unsigned int *errors)
{
int r = -EIO, fd = -1;
size_t ninputs = FEC_INPUT_DEVICES;
struct fec_input_device inputs[FEC_INPUT_DEVICES] = {
{
.device = crypt_data_device(cd),
.fd = -1,
.start = 0,
.count = params->data_size * params->data_block_size
},{
.device = crypt_metadata_device(cd),
.fd = -1,
.start = VERITY_hash_offset_block(params) * params->data_block_size,
.count = (VERITY_FEC_blocks(cd, fec_device, params) - params->data_size) * params->data_block_size
}
};
/* validate parameters */
if (params->data_block_size != params->hash_block_size) {
log_err(cd, _("Block sizes must match for FEC."));
return -EINVAL;
}
if (params->fec_roots > FEC_RSM - FEC_MIN_RSN ||
params->fec_roots < FEC_RSM - FEC_MAX_RSN) {
log_err(cd, _("Invalid number of parity bytes."));
return -EINVAL;
}
if (!inputs[0].count) {
log_err(cd, _("Invalid FEC segment length."));
return -EINVAL;
}
if (!inputs[1].count)
ninputs--;
if (check_fec)
fd = open(device_path(fec_device), O_RDONLY);
else
fd = open(device_path(fec_device), O_RDWR);
if (fd == -1) {
log_err(cd, _("Cannot open device %s."), device_path(fec_device));
goto out;
}
if (lseek(fd, params->fec_area_offset, SEEK_SET) < 0) {
log_dbg(cd, "Cannot seek to requested position in FEC device.");
goto out;
}
/* input devices */
inputs[0].fd = open(device_path(inputs[0].device), O_RDONLY);
if (inputs[0].fd == -1) {
log_err(cd, _("Cannot open device %s."), device_path(inputs[0].device));
goto out;
}
inputs[1].fd = open(device_path(inputs[1].device), O_RDONLY);
if (inputs[1].fd == -1) {
log_err(cd, _("Cannot open device %s."), device_path(inputs[1].device));
goto out;
}
r = FEC_process_inputs(cd, params, inputs, ninputs, fd, check_fec, errors);
out:
if (inputs[0].fd != -1)
close(inputs[0].fd);
if (inputs[1].fd != -1)
close(inputs[1].fd);
if (fd != -1)
close(fd);
return r;
}
uint64_t VERITY_FEC_blocks(struct crypt_device *cd,
struct device *fec_device,
struct crypt_params_verity *params)
{
uint64_t blocks = 0;
/*
* FEC covers this data:
* | protected data | hash area | padding (optional foreign metadata) |
*
* If hash device is in a separate image, metadata covers the whole rest of the image after hash area.
* If hash and FEC device is in the image, metadata ends on the FEC area offset.
*/
if (device_is_identical(crypt_metadata_device(cd), fec_device) > 0) {
log_dbg(cd, "FEC and hash device is the same.");
blocks = params->fec_area_offset;
} else {
/* cover the entire hash device starting from hash_offset */
if (device_size(crypt_metadata_device(cd), &blocks)) {
log_err(cd, _("Failed to determine size for device %s."),
device_path(crypt_metadata_device(cd)));
return 0;
}
}
blocks /= params->data_block_size;
if (blocks)
blocks -= VERITY_hash_offset_block(params);
/* Protected data */
blocks += params->data_size;
return blocks;
}
|