huangcm
2025-09-01 53d8e046ac1bf2ebe94f671983e3d3be059df91a
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Elide and patch handling for 'fsverity setup'
 *
 * Copyright (C) 2018 Google LLC
 *
 * Written by Eric Biggers.
 */
 
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include "fsverity_uapi.h"
#include "fsveritysetup.h"
 
/* An elision or a patch */
struct fsverity_elide_patch {
   u64 offset;    /* byte offset within the original data */
   u64 length;    /* length in bytes */
   bool patch;    /* false if elision, true if patch */
   u8 data[];    /* replacement data (if patch=true) */
};
 
/* Maximum supported patch size, in bytes */
#define FS_VERITY_MAX_PATCH_SIZE    255
 
/* Parse an --elide=OFFSET,LENGTH option */
static struct fsverity_elide_patch *parse_elide_option(const char *optarg)
{
   struct fsverity_elide_patch *ext = NULL;
   char *sep, *end;
   unsigned long long offset;
   unsigned long long length;
 
   sep = strchr(optarg, ',');
   if (!sep || sep == optarg)
       goto invalid;
   errno = 0;
   *sep = '\0';
   offset = strtoull(optarg, &end, 10);
   *sep = ',';
   if (errno || end != sep)
       goto invalid;
   length = strtoull(sep + 1, &end, 10);
   if (errno || *end)
       goto invalid;
   if (length <= 0 || length > UINT64_MAX - offset) {
       error_msg("Invalid length in '--elide=%s'", optarg);
       return NULL;
   }
   ext = xzalloc(sizeof(*ext));
   ext->offset = offset;
   ext->length = length;
   ext->patch = false;
   return ext;
 
invalid:
   error_msg("Invalid --elide option: '%s'.  Must be formatted as OFFSET,LENGTH",
         optarg);
   return NULL;
}
 
/* Parse a --patch=OFFSET,PATCHFILE option */
static struct fsverity_elide_patch *parse_patch_option(const char *optarg)
{
   struct fsverity_elide_patch *ext = NULL;
   struct filedes patchfile = { .fd = -1 };
   char *sep, *end;
   unsigned long long offset;
   u64 length;
 
   sep = strchr(optarg, ',');
   if (!sep || sep == optarg)
       goto invalid;
   errno = 0;
   *sep = '\0';
   offset = strtoull(optarg, &end, 10);
   *sep = ',';
   if (errno || end != sep)
       goto invalid;
   if (!open_file(&patchfile, sep + 1, O_RDONLY, 0))
       goto out;
   if (!get_file_size(&patchfile, &length))
       goto out;
   if (length <= 0) {
       error_msg("patch file '%s' is empty", patchfile.name);
       goto out;
   }
   if (length > FS_VERITY_MAX_PATCH_SIZE) {
       error_msg("Patch file '%s' is too long.  Max patch size is %d bytes.",
             patchfile.name, FS_VERITY_MAX_PATCH_SIZE);
       goto out;
   }
   ext = xzalloc(sizeof(*ext) + length);
   ext->offset = offset;
   ext->length = length;
   ext->patch = true;
   if (!full_read(&patchfile, ext->data, length)) {
       free(ext);
       ext = NULL;
   }
out:
   filedes_close(&patchfile);
   return ext;
 
invalid:
   error_msg("Invalid --patch option: '%s'.  Must be formatted as OFFSET,PATCHFILE",
         optarg);
   goto out;
}
 
/* Sort by increasing offset */
static int cmp_elide_patch_exts(const void *_p1, const void *_p2)
{
   const struct fsverity_elide_patch *ext1, *ext2;
 
   ext1 = *(const struct fsverity_elide_patch **)_p1;
   ext2 = *(const struct fsverity_elide_patch **)_p2;
 
   if (ext1->offset > ext2->offset)
       return 1;
   if (ext1->offset < ext2->offset)
       return -1;
   return 0;
}
 
/*
 * Given the lists of --elide and --patch options, validate and load the
 * elisions and patches into @params.
 */
bool load_elisions_and_patches(const struct string_list *elide_opts,
                  const struct string_list *patch_opts,
                  struct fsveritysetup_params *params)
{
   const size_t num_exts = elide_opts->length + patch_opts->length;
   struct fsverity_elide_patch **exts;
   size_t i, j;
 
   if (num_exts == 0)    /* Normal case: no elisions or patches */
       return true;
   params->num_elisions_and_patches = num_exts;
   exts = xzalloc(num_exts * sizeof(exts[0]));
   params->elisions_and_patches = exts;
   j = 0;
 
   /* Parse the --elide options */
   for (i = 0; i < elide_opts->length; i++) {
       exts[j] = parse_elide_option(elide_opts->strings[i]);
       if (!exts[j++])
           return false;
   }
 
   /* Parse the --patch options */
   for (i = 0; i < patch_opts->length; i++) {
       exts[j] = parse_patch_option(patch_opts->strings[i]);
       if (!exts[j++])
           return false;
   }
 
   /* Sort the elisions and patches by increasing offset */
   qsort(exts, num_exts, sizeof(exts[0]), cmp_elide_patch_exts);
 
   /* Verify that no elisions or patches overlap */
   for (j = 1; j < num_exts; j++) {
       if (exts[j]->offset <
           exts[j - 1]->offset + exts[j - 1]->length) {
           error_msg("%s at [%"PRIu64", %"PRIu64") overlaps "
                 "%s at [%"PRIu64", %"PRIu64")",
                 exts[j - 1]->patch ? "Patch" : "Elision",
                 exts[j - 1]->offset,
                 exts[j - 1]->offset + exts[j - 1]->length,
                 exts[j]->patch ? "patch" : "elision",
                 exts[j]->offset,
                 exts[j]->offset + exts[j]->length);
           return false;
       }
   }
   return true;
}
 
void free_elisions_and_patches(struct fsveritysetup_params *params)
{
   size_t i;
 
   for (i = 0; i < params->num_elisions_and_patches; i++)
       free(params->elisions_and_patches[i]);
   free(params->elisions_and_patches);
}
 
/*
 * Given the original file @in of length @in_length bytes, create a temporary
 * file @out_ret and write to it the data with the elisions and patches applied,
 * with the end zero-padded to the next block boundary.  Returns in
 * @out_length_ret the length of the elided/patched file in bytes.
 */
bool apply_elisions_and_patches(const struct fsveritysetup_params *params,
               struct filedes *in, u64 in_length,
               struct filedes *out_ret, u64 *out_length_ret)
{
   struct fsverity_elide_patch **exts = params->elisions_and_patches;
   struct filedes *out = out_ret;
   size_t i;
 
   for (i = 0; i < params->num_elisions_and_patches; i++) {
       if (exts[i]->offset + exts[i]->length > in_length) {
           error_msg("%s at [%"PRIu64", %"PRIu64") extends beyond end of input file",
                 exts[i]->patch ? "Patch" : "Elision",
                 exts[i]->offset,
                 exts[i]->offset + exts[i]->length);
           return false;
       }
   }
 
   if (!filedes_seek(in, 0, SEEK_SET))
       return false;
 
   if (!open_tempfile(out))
       return false;
 
   for (i = 0; i < params->num_elisions_and_patches; i++) {
       printf("Applying %s: offset=%"PRIu64", length=%"PRIu64"\n",
              exts[i]->patch ? "patch" : "elision",
              exts[i]->offset, exts[i]->length);
 
       if (!copy_file_data(in, out, exts[i]->offset - in->pos))
           return false;
 
       if (exts[i]->patch &&
           !full_write(out, exts[i]->data, exts[i]->length))
           return false;
 
       if (!filedes_seek(in, exts[i]->length, SEEK_CUR))
           return false;
   }
   if (!copy_file_data(in, out, in_length - in->pos))
       return false;
   if (!write_zeroes(out, ALIGN(out->pos, params->blocksize) - out->pos))
       return false;
   *out_length_ret = out->pos;
   return true;
}
 
/* Calculate the size the elisions and patches will take up when serialized */
size_t total_elide_patch_ext_length(const struct fsveritysetup_params *params)
{
   size_t total = 0;
   size_t i;
 
   for (i = 0; i < params->num_elisions_and_patches; i++) {
       const struct fsverity_elide_patch *ext =
           params->elisions_and_patches[i];
       size_t inner_len;
 
       if (ext->patch) {
           inner_len = sizeof(struct fsverity_extension_patch) +
                   ext->length;
       } else {
           inner_len = sizeof(struct fsverity_extension_elide);
       }
       total += FSVERITY_EXTLEN(inner_len);
   }
   return total;
}
 
/*
 * Append the elide and patch extensions (if any) to the given buffer.
 * The buffer must have enough space; call total_elide_patch_ext_length() first.
 */
void append_elide_patch_exts(void **buf_p,
                const struct fsveritysetup_params *params)
{
   void *buf = *buf_p;
   size_t i;
   union {
       struct {
           struct fsverity_extension_patch hdr;
           u8 data[FS_VERITY_MAX_PATCH_SIZE];
       } patch;
       struct fsverity_extension_elide elide;
   } u;
 
   for (i = 0; i < params->num_elisions_and_patches; i++) {
       const struct fsverity_elide_patch *ext =
           params->elisions_and_patches[i];
       int type;
       size_t extlen;
 
       if (ext->patch) {
           type = FS_VERITY_EXT_PATCH;
           u.patch.hdr.offset = cpu_to_le64(ext->offset);
           ASSERT(ext->length <= sizeof(u.patch.data));
           memcpy(u.patch.data, ext->data, ext->length);
           extlen = sizeof(u.patch.hdr) + ext->length;
       } else {
           type = FS_VERITY_EXT_ELIDE;
           u.elide.offset = cpu_to_le64(ext->offset),
           u.elide.length = cpu_to_le64(ext->length);
           extlen = sizeof(u.elide);
       }
       fsverity_append_extension(&buf, type, &u, extlen);
   }
 
   *buf_p = buf;
}