-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinInit.c
104 lines (89 loc) · 2.05 KB
/
MinInit.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
/*
* PROJECT: MinInit
* FILE: MinInit.c
* PURPOSE: Implementation for MinInit
*
* LICENSE: The MIT License
*
* DEVELOPER: Mouri_Naruto (Mouri_Naruto AT Outlook.com)
*/
#include <errno.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
void print_message(const char *message)
{
printf("[MinInit] %s\n", message);
}
int main()
{
{
int confd = open("/dev/console", O_RDWR);
if (-1 != confd)
{
dup2(confd, STDIN_FILENO);
dup2(confd, STDOUT_FILENO);
dup2(confd, STDERR_FILENO);
if (confd > STDERR_FILENO)
{
close(confd);
}
}
}
if (-1 == mount(
"udev",
"/dev",
"devtmpfs",
MS_NOSUID | MS_NOEXEC | MS_RELATIME,
"size=10240k,mode=755") && errno != EBUSY)
{
print_message("Failed to mount /dev.\n");
}
if (-1 == mount(
"proc",
"/proc",
"proc",
MS_NOSUID | MS_NOEXEC | MS_RELATIME | MS_NODEV,
NULL) && errno != EBUSY)
{
print_message("Failed to mount /proc.\n");
}
if (-1 == mount(
"sysfs",
"/sys",
"sysfs",
MS_NOSUID | MS_NOEXEC | MS_RELATIME | MS_NODEV,
NULL) && errno != EBUSY)
{
print_message("Failed to mount /sys.\n");
}
if (-1 == mount(
"tmpfs",
"/tmp",
"tmpfs",
0,
NULL))
{
print_message("Failed to mount /tmp to tmpfs.\n");
}
chdir("/root");
const char* envp[] =
{
"PATH=/usr/sbin:/usr/bin:/sbin:/bin",
"TERM=xterm-256color",
"HOME=/root",
NULL
};
const char* argv[] =
{
"/bin/pwsh",
"-NoLogo",
NULL
};
execve(argv[0], (char* const*)argv, (char* const*)envp);
print_message("Failed to start PowerShell.\n");
return 0;
}