-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample4-detached-mount-exec.c
More file actions
62 lines (48 loc) · 1.96 KB
/
Copy pathexample4-detached-mount-exec.c
File metadata and controls
62 lines (48 loc) · 1.96 KB
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
#include "common.h"
#include <stdlib.h>
int main(void) {
char dummy_lower_layer[255] = "dummy-lower-layer/";
addcurpath(dummy_lower_layer);
char lower_layer[255] = "lower-layer/";
addcurpath(lower_layer);
char lowerdir_str[512];
snprintf(lowerdir_str, sizeof(lowerdir_str), "%s:%s", lower_layer, dummy_lower_layer);
const char * binary_name = "hello-world";
printpid();
printf("Creating overlayfs with hello-world binary...\n");
wait("* Step 1: Will create the overlayfs context");
int overlay_fd = fsopen("overlay", FSOPEN_CLOEXEC);
if (overlay_fd < 0) {
perror("fsopen overlay failed");
return 1;
}
printf("The overlayfs context was successfully created. The fd handle is %d\n", overlay_fd);
wait("* Step 2: Will configure with the lowerdirs - the true one, and a dummy one: %s", lowerdir_str);
if (fsconfig(overlay_fd, FSCONFIG_SET_STRING, "lowerdir", lowerdir_str, 0) < 0) {
perror("fsconfig set lowerdir failed");
return 1;
}
printf("lowerdir set\n");
wait("* Step 3: Will validate the configuration and create the superblock");
if (fsconfig(overlay_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) {
perror("fsconfig create failed");
return 1;
}
printf("Superblock created\n");
wait("* Step 4: Mount the overlayfs (read-only, no devices, no suid)");
int mount_fd = fsmount(overlay_fd, FSMOUNT_CLOEXEC, MS_RDONLY | MS_NODEV | MS_NOSUID);
if (mount_fd < 0) {
perror("fsmount failed");
return 1;
}
printf("Overlayfs created successfully!\n");
wait("* Step 5: Will open() the file int he detached filesystem");
int binary_fd = openat(mount_fd, binary_name, O_RDONLY | O_CLOEXEC);
if (binary_fd < 0) {
perror("openat hello-world failed");
return 1;
}
printf("Opened hello-world binary from overlayfs (fd=%d)\n", binary_fd);
wait("You can now execute it with: /proc/%d/fd/%d\n", getpid(), binary_fd);
return 0;
}