-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace_dyn_mem.c
More file actions
138 lines (117 loc) · 3.47 KB
/
namespace_dyn_mem.c
File metadata and controls
138 lines (117 loc) · 3.47 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <string.h>
#define CGROUP_PATH "/sys/fs/cgroup/demo_cgroup"
#define MEMORY_LIMIT "67108864" // 64MB
#define TRIGGER_FILE "/tmp/mem_trigger"
void write_to_file(const char *path, const char *value) {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
perror("open cgroup file");
exit(EXIT_FAILURE);
}
write(fd, value, strlen(value));
close(fd);
}
void setup_cgroup(pid_t pid) {
if (mkdir(CGROUP_PATH, 0755) == -1 && errno != EEXIST) {
perror("mkdir cgroup");
exit(EXIT_FAILURE);
}
write_to_file("/sys/fs/cgroup/cgroup.subtree_control", "+memory");
write_to_file(CGROUP_PATH "/memory.max", MEMORY_LIMIT);
char pid_str[16];
sprintf(pid_str, "%d", pid);
write_to_file(CGROUP_PATH "/cgroup.procs", pid_str);
}
void cleanup() {
printf("Cleaning up...\n");
unlink(TRIGGER_FILE);
rmdir(CGROUP_PATH);
}
void accumulate_memory() {
size_t allocated_size = 0;
const size_t chunk_size = 10 * 1024 * 1024; // 10MB
void *mem_blocks[10] = {NULL};
printf("Memory accumulation started...\n");
for (int i = 0; i < 10; i++) {
sleep(10);
mem_blocks[i] = malloc(chunk_size);
if (!mem_blocks[i]) {
perror("malloc failed (OOM reached)");
cleanup(); // Clean up and exit on failure
exit(EXIT_FAILURE);
}
memset(mem_blocks[i], 0, chunk_size);
allocated_size += chunk_size;
printf("Allocated %ld MB\n", allocated_size / (1024 * 1024));
}
}
void monitor_trigger() {
printf("Monitoring trigger file: %s\n", TRIGGER_FILE);
while (1) {
sleep(2);
FILE *file = fopen(TRIGGER_FILE, "r");
if (file) {
char buffer[16];
fgets(buffer, sizeof(buffer), file);
fclose(file);
if (strncmp(buffer, "start", 5) == 0) {
printf("Trigger received! Starting memory accumulation...\n");
accumulate_memory();
break;
}
}
}
}
void reduce_memory_limit() {
sleep(5);
printf("Reducing memory limit to 32MB...\n");
write_to_file(CGROUP_PATH "/memory.max", "33554432");
sleep(5);
printf("Reducing memory limit to 16MB...\n");
write_to_file(CGROUP_PATH "/memory.max", "16777216");
}
int main() {
if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET | CLONE_NEWUTS) == -1) {
perror("unshare");
exit(EXIT_FAILURE);
}
printf("Namespaces created successfully.\n");
pid_t child = fork();
if (child == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (child == 0) {
printf("Namespace isolated. PID: %d\n", getpid());
sethostname("container", 10);
setup_cgroup(getpid());
if (fork() == 0) {
monitor_trigger();
exit(0);
}
if (fork() == 0) {
reduce_memory_limit();
exit(0);
}
printf("Starting shell inside isolated namespace...\n");
execlp("/bin/bash", "/bin/bash", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}
waitpid(child, NULL, 0);
cleanup(); // Ensure cleanup after child exits
return 0;
}