hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright 2018 Google LLC
 */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
 
#include <openssl/sha.h>
#include <openssl/md5.h>
 
#include "utils.h"
 
#ifndef __S_IFREG
#define __S_IFREG S_IFREG
#endif
 
unsigned int rnd(unsigned int max, unsigned int *seed)
{
   return rand_r(seed) * ((uint64_t)max + 1) / RAND_MAX;
}
 
int remove_dir(const char *dir)
{
   int err = rmdir(dir);
 
   if (err && errno == ENOTEMPTY) {
       err = delete_dir_tree(dir);
       if (err)
           return err;
       return 0;
   }
 
   if (err && errno != ENOENT)
       return -errno;
 
   return 0;
}
 
int drop_caches(void)
{
   int drop_caches =
       open("/proc/sys/vm/drop_caches", O_WRONLY | O_CLOEXEC);
   int i;
 
   if (drop_caches == -1)
       return -errno;
   i = write(drop_caches, "3", 1);
   close(drop_caches);
 
   if (i != 1)
       return -errno;
 
   return 0;
}
 
int mount_fs(const char *mount_dir, const char *backing_dir,
        int read_timeout_ms)
{
   static const char fs_name[] = INCFS_NAME;
   char mount_options[512];
   int result;
 
   snprintf(mount_options, ARRAY_SIZE(mount_options),
        "read_timeout_ms=%u",
         read_timeout_ms);
 
   result = mount(backing_dir, mount_dir, fs_name, 0, mount_options);
   if (result != 0)
       perror("Error mounting fs.");
   return result;
}
 
int mount_fs_opt(const char *mount_dir, const char *backing_dir,
        const char *opt, bool remount)
{
   static const char fs_name[] = INCFS_NAME;
   int result;
 
   result = mount(backing_dir, mount_dir, fs_name,
              remount ? MS_REMOUNT : 0, opt);
   if (result != 0)
       perror("Error mounting fs.");
   return result;
}
 
struct hash_section {
   uint32_t algorithm;
   uint8_t log2_blocksize;
   uint32_t salt_size;
   /* no salt */
   uint32_t hash_size;
   uint8_t hash[SHA256_DIGEST_SIZE];
} __packed;
 
struct signature_blob {
   uint32_t version;
   uint32_t hash_section_size;
   struct hash_section hash_section;
   uint32_t signing_section_size;
   uint8_t signing_section[];
} __packed;
 
size_t format_signature(void **buf, const char *root_hash, const char *add_data)
{
   size_t size = sizeof(struct signature_blob) + strlen(add_data) + 1;
   struct signature_blob *sb = malloc(size);
 
   if (!sb)
       return 0;
 
   *sb = (struct signature_blob){
       .version = INCFS_SIGNATURE_VERSION,
       .hash_section_size = sizeof(struct hash_section),
       .hash_section =
           (struct hash_section){
               .algorithm = INCFS_HASH_TREE_SHA256,
               .log2_blocksize = 12,
               .salt_size = 0,
               .hash_size = SHA256_DIGEST_SIZE,
           },
       .signing_section_size = strlen(add_data) + 1,
   };
 
   memcpy(sb->hash_section.hash, root_hash, SHA256_DIGEST_SIZE);
   memcpy((char *)sb->signing_section, add_data, strlen(add_data) + 1);
   *buf = sb;
   return size;
}
 
int crypto_emit_file(int fd, const char *dir, const char *filename,
            incfs_uuid_t *id_out, size_t size, const char *root_hash,
            const char *add_data)
{
   int mode = __S_IFREG | 0555;
   void *signature;
   int error = 0;
 
   struct incfs_new_file_args args = {
           .size = size,
           .mode = mode,
           .file_name = ptr_to_u64(filename),
           .directory_path = ptr_to_u64(dir),
           .file_attr = 0,
           .file_attr_len = 0
   };
 
   args.signature_size = format_signature(&signature, root_hash, add_data);
   args.signature_info = ptr_to_u64(signature);
 
   md5(filename, strlen(filename), (char *)args.file_id.bytes);
 
   if (ioctl(fd, INCFS_IOC_CREATE_FILE, &args) != 0) {
       error = -errno;
       goto out;
   }
 
   *id_out = args.file_id;
 
out:
   free(signature);
   return error;
}
 
int emit_file(int fd, const char *dir, const char *filename,
         incfs_uuid_t *id_out, size_t size, const char *attr)
{
   int mode = __S_IFREG | 0555;
   struct incfs_new_file_args args = { .size = size,
                       .mode = mode,
                       .file_name = ptr_to_u64(filename),
                       .directory_path = ptr_to_u64(dir),
                       .signature_info = ptr_to_u64(NULL),
                       .signature_size = 0,
                       .file_attr = ptr_to_u64(attr),
                       .file_attr_len =
                           attr ? strlen(attr) : 0 };
 
   md5(filename, strlen(filename), (char *)args.file_id.bytes);
 
   if (ioctl(fd, INCFS_IOC_CREATE_FILE, &args) != 0)
       return -errno;
 
   *id_out = args.file_id;
   return 0;
}
 
int get_file_bmap(int cmd_fd, int ino, unsigned char *buf, int buf_size)
{
   return 0;
}
 
int get_file_signature(int fd, unsigned char *buf, int buf_size)
{
   struct incfs_get_file_sig_args args = {
       .file_signature = ptr_to_u64(buf),
       .file_signature_buf_size = buf_size
   };
 
   if (ioctl(fd, INCFS_IOC_READ_FILE_SIGNATURE, &args) == 0)
       return args.file_signature_len_out;
   return -errno;
}
 
loff_t get_file_size(const char *name)
{
   struct stat st;
 
   if (stat(name, &st) == 0)
       return st.st_size;
   return -ENOENT;
}
 
int open_commands_file(const char *mount_dir)
{
   char cmd_file[255];
   int cmd_fd;
 
   snprintf(cmd_file, ARRAY_SIZE(cmd_file),
           "%s/%s", mount_dir, INCFS_PENDING_READS_FILENAME);
   cmd_fd = open(cmd_file, O_RDONLY | O_CLOEXEC);
 
   if (cmd_fd < 0)
       perror("Can't open commands file");
   return cmd_fd;
}
 
int open_log_file(const char *mount_dir)
{
   char file[255];
   int fd;
 
   snprintf(file, ARRAY_SIZE(file), "%s/.log", mount_dir);
   fd = open(file, O_RDWR | O_CLOEXEC);
   if (fd < 0)
       perror("Can't open log file");
   return fd;
}
 
int open_blocks_written_file(const char *mount_dir)
{
   char file[255];
   int fd;
 
   snprintf(file, ARRAY_SIZE(file),
           "%s/%s", mount_dir, INCFS_BLOCKS_WRITTEN_FILENAME);
   fd = open(file, O_RDONLY | O_CLOEXEC);
 
   if (fd < 0)
       perror("Can't open blocks_written file");
   return fd;
}
 
int wait_for_pending_reads(int fd, int timeout_ms,
   struct incfs_pending_read_info *prs, int prs_count)
{
   ssize_t read_res = 0;
 
   if (timeout_ms > 0) {
       int poll_res = 0;
       struct pollfd pollfd = {
           .fd = fd,
           .events = POLLIN
       };
 
       poll_res = poll(&pollfd, 1, timeout_ms);
       if (poll_res < 0)
           return -errno;
       if (poll_res == 0)
           return 0;
       if (!(pollfd.revents | POLLIN))
           return 0;
   }
 
   read_res = read(fd, prs, prs_count * sizeof(*prs));
   if (read_res < 0)
       return -errno;
 
   return read_res / sizeof(*prs);
}
 
int wait_for_pending_reads2(int fd, int timeout_ms,
   struct incfs_pending_read_info2 *prs, int prs_count)
{
   ssize_t read_res = 0;
 
   if (timeout_ms > 0) {
       int poll_res = 0;
       struct pollfd pollfd = {
           .fd = fd,
           .events = POLLIN
       };
 
       poll_res = poll(&pollfd, 1, timeout_ms);
       if (poll_res < 0)
           return -errno;
       if (poll_res == 0)
           return 0;
       if (!(pollfd.revents | POLLIN))
           return 0;
   }
 
   read_res = read(fd, prs, prs_count * sizeof(*prs));
   if (read_res < 0)
       return -errno;
 
   return read_res / sizeof(*prs);
}
 
char *concat_file_name(const char *dir, const char *file)
{
   char full_name[FILENAME_MAX] = "";
 
   if (snprintf(full_name, ARRAY_SIZE(full_name), "%s/%s", dir, file) < 0)
       return NULL;
   return strdup(full_name);
}
 
int delete_dir_tree(const char *dir_path)
{
   DIR *dir = NULL;
   struct dirent *dp;
   int result = 0;
 
   dir = opendir(dir_path);
   if (!dir) {
       result = -errno;
       goto out;
   }
 
   while ((dp = readdir(dir))) {
       char *full_path;
 
       if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
           continue;
 
       full_path = concat_file_name(dir_path, dp->d_name);
       if (dp->d_type == DT_DIR)
           result = delete_dir_tree(full_path);
       else
           result = unlink(full_path);
       free(full_path);
       if (result)
           goto out;
   }
 
out:
   if (dir)
       closedir(dir);
   if (!result)
       rmdir(dir_path);
   return result;
}
 
void sha256(const char *data, size_t dsize, char *hash)
{
   SHA256_CTX ctx;
 
   SHA256_Init(&ctx);
   SHA256_Update(&ctx, data, dsize);
   SHA256_Final((unsigned char *)hash, &ctx);
}
 
void md5(const char *data, size_t dsize, char *hash)
{
   MD5_CTX ctx;
 
   MD5_Init(&ctx);
   MD5_Update(&ctx, data, dsize);
   MD5_Final((unsigned char *)hash, &ctx);
}