-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_secure_cmd.c
More file actions
121 lines (106 loc) · 3.81 KB
/
example_secure_cmd.c
File metadata and controls
121 lines (106 loc) · 3.81 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
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
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <seccomp.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
void usage(const char *prog) {
fprintf(stderr, "Usage: %s [-c|-i|-d] <filename>\n", prog);
fprintf(stderr, " -c create file\n");
fprintf(stderr, " -i show file info (size)\n");
fprintf(stderr, " -d delete file\n");
}
int main(int argc, char *argv[]) {
pid_t pid = fork();
if (pid == 0) {
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(set_tid_address), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(ioctl), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(pread64), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getrandom), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(prlimit64), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(newfstatat), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rseq), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(arch_prctl), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(munmap), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fstat), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(unlink), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(set_robust_list), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(access), 0);
if (seccomp_load(ctx) < 0) {
perror("seccomp_load failed");
exit(1);
}
int opt;
char action = 0;
while ((opt = getopt(argc, argv, "cid")) != -1) {
switch (opt) {
case 'c':
case 'i':
case 'd':
action = opt;
break;
default:
usage(argv[0]);
return 1;
}
}
if (optind >= argc) {
fprintf(stderr, "Missing file name.\n");
usage(argv[0]);
return 1;
}
const char *filename = argv[optind];
switch (action) {
case 'c': {
FILE *f = fopen(filename, "w");
if (!f) {
perror("fopen");
return 1;
}
fclose(f);
printf("Created file '%s'.\n", filename);
break;
}
case 'i': {
struct stat st;
if (stat(filename, &st) == 0) {
printf("File %s: %ld Bytes\n", filename, st.st_size);
} else {
perror("stat");
}
break;
}
case 'd': {
if (unlink(filename) == 0) {
printf("File '%s' deleted.\n", filename);
} else {
perror("unlink");
}
break;
}
default:
usage(argv[0]);
return 1;
}
return 0;
} else if (pid > 0) {
if (setuid(1000) < 0) {
perror("setuid failed");
exit(1);
}
int status;
waitpid(pid, &status, 0);
} else {
perror("Fork failed");
}
}