-
Notifications
You must be signed in to change notification settings - Fork 0
/
frugalify.c
401 lines (321 loc) · 9.79 KB
/
frugalify.c
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <unistd.h>
#include <linux/loop.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <sys/mount.h>
#ifdef HAVE_FSCRYPT
# include <linux/fscrypt.h>
# include <mbedtls/sha512.h>
#endif
#ifndef LOOP_CTL_GET_FREE
# define LOOP_CTL_GET_FREE 0x4C82
#endif
#define MAXSFS 8
#ifdef HAVE_AUFS
# define FS "aufs"
# define FSOPTS_HEAD "br=/save"
#else
# define FS "overlay"
# define FSOPTS_HEAD "upperdir=/save,workdir=/.work,lowerdir="
#endif
#define CLEAR_TTY "\033[2J\033[H"
#define HIDE_KEY "\033[32m\033[102m"
#define RESET_TTY "\033[39m\033[49m"
static inline void do_autoclose(void *fdp)
{
if (*(int *)fdp != -1)
close(*(int *)fdp);
}
#define autoclose __attribute__((cleanup(do_autoclose)))
// use of sprintf() can double the executable size
static char *itoa(char *s, int i)
{
if (i >= 10)
s = itoa(s, i / 10);
*s = '0' + i % 10;
++s;
*s = '\0';
return s;
}
static char *get_lo_path(const int i)
{
static char loop[sizeof("/save/dev/loop0")] = "/save/dev/loop";
itoa(loop + sizeof("/save/dev/loop") - 1, i);
return loop;
}
static const char *losetup(const char *sfs, const int i)
{
struct stat stbuf;
struct loop_info64 info = {.lo_flags = LO_FLAGS_READ_ONLY};
const char *loop;
autoclose int loopfd = -1, sfsfd;
sfsfd = open(sfs, O_RDWR);
if (sfsfd < 0)
return NULL;
if (fstat(sfsfd, &stbuf) < 0)
return NULL;
loop = get_lo_path(i);
loopfd = open(loop, O_RDWR);
if (loopfd < 0)
return NULL;
if (ioctl(loopfd, LOOP_SET_FD, sfsfd) < 0)
return NULL;
#ifdef HAVE_STRLCPY
strlcpy(info.lo_file_name, sfs, sizeof(info.lo_file_name));
#else
strncpy((char *)info.lo_file_name, sfs, sizeof(info.lo_file_name));
info.lo_file_name[sizeof(info.lo_file_name) - 1] = '\0';
#endif
info.lo_sizelimit = (uint64_t)stbuf.st_size;
if (ioctl(loopfd, LOOP_SET_STATUS64, &info) < 0)
return NULL;
return loop;
}
static void losetup_d(const int i)
{
autoclose int loopfd = -1;
loopfd = open(get_lo_path(i), O_RDWR);
if (loopfd >= 0)
ioctl(loopfd, LOOP_CLR_FD, 0);
}
static int sfscmp(const void *a, const void *b)
{
const char *as = *(const char **)a, *bs = *(const char **)b, *abase, *bbase;
int m, n;
abase = strrchr(as, '/');
if (abase)
abase = &abase[1];
else
abase = as;
bbase = strrchr(bs, '/');
if (bbase)
bbase = &bbase[1];
else
bbase = bs;
m = strncmp(abase, "puppy_", sizeof("puppy_") - 1);
n = strncmp(bbase, "puppy_", sizeof("puppy_") - 1);
if ((m == 0) && (n != 0))
return 1;
if ((m != 0) && (n == 0))
return -1;
return 0;
}
#ifdef HAVE_FSCRYPT
static unsigned int read_key(unsigned char key[FSCRYPT_MAX_KEY_SIZE])
{
unsigned char buf[32];
unsigned int len;
ssize_t out;
for (len = 0; len < sizeof(buf); ++len) {
if (read(STDIN_FILENO, &buf[len], sizeof(buf[len]) ) != sizeof(buf[len]))
return 0;
if (buf[len] == '\n')
break;
}
mbedtls_sha512_ret(buf, len, key, 0);
return len;
}
static void fscrypt(const char *path)
{
static unsigned char buf[sizeof(struct fscrypt_add_key_arg) + FSCRYPT_MAX_KEY_SIZE];
struct fscrypt_add_key_arg *arg = (struct fscrypt_add_key_arg *)buf;
struct fscrypt_remove_key_arg remove = {
.key_spec = {
.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER,
},
};
struct fscrypt_policy_v2 policy = {
.version = FSCRYPT_POLICY_V2,
.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS,
.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS,
.flags = FSCRYPT_POLICY_FLAGS_PAD_32,
};
unsigned int len;
int fd;
if (write(STDOUT_FILENO, "Key: ", sizeof("Key: ") - 1) != sizeof("Key: ") - 1)
return;
write(STDOUT_FILENO, HIDE_KEY, sizeof(HIDE_KEY) - 1);
len = read_key(&buf[sizeof(*arg)]);
write(STDOUT_FILENO, RESET_TTY, sizeof(RESET_TTY) - 1);
if (len == 0)
return;
fd = open(path, O_RDONLY);
if (fd < 0)
return;
*arg = (struct fscrypt_add_key_arg){
.key_spec = {
.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER,
},
.raw_size = sizeof(buf) - sizeof(*arg),
};
if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, buf) < 0) {
close(fd);
return;
}
memcpy(policy.master_key_identifier,
arg->key_spec.u.identifier,
sizeof(policy.master_key_identifier));
if (ioctl(fd, FS_IOC_SET_ENCRYPTION_POLICY, &policy) < 0) {
memcpy(remove.key_spec.u.identifier,
arg->key_spec.u.identifier,
sizeof(remove.key_spec.u.identifier));
ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &remove);
}
close(fd);
}
#endif
int main(int argc, char *argv[])
{
static const char *dirs[] = {
#ifndef HAVE_AUFS
"/.work",
#endif
"/save/.pup_new",
"/save/dev",
"/save/initrd",
};
static char sfspath[MAXSFS][128], br[1024] = FSOPTS_HEAD;
struct dirent ent[MAXSFS];
char *sfs[MAXSFS] = {NULL}, sfsmnt[sizeof("/save/.sfs0")] = "/save/.sfs";
const char *loop;
size_t len, brlen = sizeof(FSOPTS_HEAD) - 1;
ssize_t out;
DIR *root;
struct dirent *pent;
unsigned int nsfs = 0, i;
int ro = 0;
// protect against accidental click
if (getpid() != 1)
return EXIT_FAILURE;
// clear firmware and bootloader output on the screen
write(STDOUT_FILENO, CLEAR_TTY, sizeof(CLEAR_TTY) - 1);
mount(NULL, "/", NULL, MS_REMOUNT | MS_NOATIME, NULL);
root = opendir("/");
if (!root)
return EXIT_FAILURE;
// create a list of all SFS files under /
while ((readdir_r(root, &ent[nsfs], &pent) == 0) && (pent != NULL)) {
len = strlen(pent->d_name);
if ((len <= sizeof(".sfs") - 1) ||
(memcmp(&pent->d_name[len - 4], ".sfs", sizeof(".sfs") - 1) != 0))
continue;
sfs[nsfs] = pent->d_name;
switch (pent->d_type) {
case DT_REG:
break;
case DT_LNK:
out = readlink(pent->d_name,
sfspath[nsfs],
sizeof(sfspath[nsfs]) - 1);
if (out <= 0)
continue;
sfspath[nsfs][out] = '\0';
sfs[nsfs] = sfspath[nsfs];
break;
default:
continue;
}
++nsfs;
if (nsfs == MAXSFS)
break;
}
closedir(root);
if (!sfs[0])
return EXIT_FAILURE;
if (mkdir("/save", 0755) < 0) {
switch (errno) {
case EEXIST:
break;
case EROFS:
// we need some writable file system as the upper layer, so we mount
// a tmpfs; we assume that /save and /.work already exist if we're
// booting from optical media or from a corrupt file system mounted
// read-only, and we assume that only the first mkdir() can return
// EROFS
if (mount("save", "/save", "tmpfs", 0, NULL) < 0)
return EXIT_FAILURE;
ro = 1;
break;
default:
return EXIT_FAILURE;
}
}
// TODO: figure out a way to make encryption work with overlayfs
#ifdef HAVE_FSCRYPT
if (!ro)
fscrypt("/save");
#endif
for (i = 0; i < sizeof(dirs) / sizeof(dirs[0]); ++i) {
if ((mkdir(dirs[i], 0755) < 0) && (errno != EEXIST))
return EXIT_FAILURE;
}
// mount a devtmpfs so we have the loop%d device nodes
if (mount("dev", "/save/dev", "devtmpfs", 0, NULL) < 0)
return EXIT_FAILURE;
// make sure adrv, zdrv, etc' come after the main SFS
qsort(sfs, (size_t)nsfs, sizeof(sfs[0]), sfscmp);
for (i = 0; (i < nsfs) && (brlen < sizeof(br)); ++i) {
itoa(sfsmnt + sizeof("/save/.sfs") - 1, i);
if ((mkdir(sfsmnt, 0755) < 0) && (errno != EEXIST))
continue;
// bind the SFS to a loop device
loop = losetup(sfs[i], i);
if (!loop) {
rmdir(sfsmnt);
continue;
}
// mount the loop device
if (mount(loop, sfsmnt, "squashfs", 0, NULL) < 0) {
losetup_d(i);
rmdir(sfsmnt);
continue;
}
#ifndef HAVE_AUFS
if (i == 0)
goto cpy;
#endif
br[brlen] = ':';
++brlen;
cpy:
memcpy(&br[brlen], sfsmnt, sizeof("/save/.sfs0") - 1);
brlen += sizeof("/save/.sfs0") - 1;
}
br[brlen] = '\0';
// we no longer need /dev
umount2("/save/dev", MNT_DETACH);
rmdir("/save/dev");
#ifndef HAVE_AUFS
if (ro && mount("work", "/.work", "tmpfs", 0, NULL) < 0)
return EXIT_FAILURE;
#endif
// mount a union file system with the SFS mount points and /save on top
if (mount(FS, "/save/.pup_new", FS, MS_NOATIME, br) < 0)
return EXIT_FAILURE;
// give processes running with the union file system as / a directory
// outside of the union file system that can be used to add aufs branches
if (mount("/", "/save/.pup_new/initrd", NULL, MS_BIND, NULL) < 0)
return EXIT_FAILURE;
if (chdir("/save/.pup_new") < 0)
return EXIT_FAILURE;
// make the union file system the the file system root, or the file system
// root for the real init and its children
#ifndef HAVE_AUFS
if (mount(".", "/", FS, MS_MOVE, NULL) < 0)
return EXIT_FAILURE;
#endif
if (chroot(".") < 0)
return EXIT_FAILURE;
if (chdir("/") < 0)
return EXIT_FAILURE;
// run the real init under the new root file system
execl("/sbin/init", "init", "initrd_full_install", NULL);
return EXIT_FAILURE;
}