-
Notifications
You must be signed in to change notification settings - Fork 3
/
exe.c
208 lines (183 loc) · 5.36 KB
/
exe.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
/*
* This file is part of papaw.
*
* Copyright (c) 2019 Dima Krasner
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <stdbool.h>
#ifdef HAVE_PRCTL
# include <sys/prctl.h>
#endif
#ifndef PAPAW_SHARED_LIBRARY
# include <papaw.h>
#endif
static void papaw_do_hide_exe(const char *self, const bool should_truncate)
{
static char buf[192], path[128];
const char *selfbase = NULL, *pathbase;
void *copy;
FILE *fp;
unsigned long start, end;
size_t len;
uid_t uid;
int prot;
#ifndef HAVE_TRUNCATE
int fd;
#endif
char r, w, x, p;
bool found = false, remapped = true;
/*
* when the tmpfs is unmounted, /proc/self/exe points to /x for /tmp/x, so
* we compare only the file name when we look for mapped regions of the
* executable
*/
uid = geteuid();
if (uid == 0) {
selfbase = strrchr(self, '/');
if (!selfbase)
return;
++selfbase;
/*
* restore the process name: when we run /proc/self/exe/3, the process
* name is changed from basename to "3"
*/
#ifdef HAVE_PRCTL
if (strncmp(selfbase, "memfd:", sizeof("memfd:") - 1) == 0)
prctl(PR_SET_NAME, &selfbase[sizeof("memfd:") - 1]);
else
prctl(PR_SET_NAME, selfbase);
#endif
}
fp = fopen("/proc/self/maps", "r");
if (!fp)
return;
while (fgets(buf, sizeof(buf), fp)) {
if ((sscanf(buf,
"%lx-%lx %c%c%c%c %*x %*x:%*x %*u %128s",
&start, &end,
&r, &w, &x, &p,
path) != 7) ||
(p != 'p'))
continue;
if (uid == 0) {
pathbase = strrchr(path, '/');
if (!pathbase)
continue;
++pathbase;
if (strcmp(pathbase, selfbase))
continue;
}
else {
/* if non-root, /proc/self/exe does not change */
if (strcmp(path, self))
continue;
}
found = true;
len = (size_t)(end - start);
#ifdef HAVE_MPROTECT
prot = PROT_WRITE;
if (r == 'r')
prot |= PROT_READ;
if (x == 'x')
prot |= PROT_EXEC;
#else
/* we have no choice, because we can't change permissions later */
prot = PROT_READ | PROT_WRITE | PROT_EXEC;
#endif
/* allocate a memory region */
copy = mmap(NULL, len, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (copy == MAP_FAILED) {
remapped = false;
continue;
}
memcpy(copy, (void *)start, len);
/*
* if the original memory region is read-only, make the copy read-only
* once we're done writing to it
*/
#ifdef HAVE_MPROTECT
if (w != 'w') {
prot &= ~PROT_WRITE;
if (mprotect(copy, len, prot) < 0) {
munmap(copy, len);
remapped = false;
continue;
}
}
#endif
/* replace the original memory region with the copy */
if (mremap(copy,
len,
len,
MREMAP_FIXED | MREMAP_MAYMOVE,
(void *)start) != (void *)start) {
munmap(copy, len);
remapped = false;
}
/*
* ask the kernel not to swap out the copy, so it cannot be read from
* disk by running the packed executable with very little free RAM
*/
#ifdef HAVE_MLOCK
if (mlock((void *)start, len) < 0) {
munmap(copy, len);
remapped = false;
}
#endif
}
fclose(fp);
if (should_truncate && found && remapped) {
#ifdef HAVE_TRUNCATE
truncate("/proc/self/exe", 0);
#else
fd = open("/proc/self/exe", O_TRUNC | O_RDONLY);
if (fd >= 0)
close(fd);
#endif
}
}
#ifdef PAPAW_SHARED_LIBRARY
static
#endif
void papaw_hide_exe(void)
{
const char *self;
self = getenv(" ");
if (!self)
return;
/* try only once */
unsetenv(" ");
papaw_do_hide_exe(self, true);
}
#ifdef PAPAW_SHARED_LIBRARY
__attribute__((constructor))
static void init(void)
{
papaw_hide_exe();
}
#endif