-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssl_sandbox.c
541 lines (452 loc) · 12 KB
/
ssl_sandbox.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*-
* Copyright (c) 2014 Ilias Marinos
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <err.h>
#include <sys/param.h>
#ifndef NO_SANDBOX
#include <sandbox.h>
/*#include <sandbox_rpc.h>*/
#endif
#ifdef WITH_SSL
#include <openssl/x509v3.h>
#endif
#include "fetch.h"
#include "common.h"
/* DPRINTF */
#ifdef DEBUG
#define DPRINTF(format, ...) \
fprintf(stderr, "%s [%d] " format "\n", \
__FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define DPRINTF(...)
#endif
#define MMIN(a, b) ((a) < (b) ? (a) : (b))
#ifndef NO_SANDBOX
/* Sandbox return value */
int rv;
conn_t sconn; /* Global conn_t control block for the SSL sandbox */
int ssl_initialized = 0;
/* Operations */
typedef enum {
SSL_INIT = 0,
SSL_WRITE,
SSL_READ,
SSL_SHUTDOWN
} operation_t;
/* fetch sandbox control block */
struct sandbox_cb *fscb;
struct ssl_init_args {
/*conn_t conn;*/
struct url URL;
int verbose;
} __packed;
struct ssl_read_args {
size_t len;
} __packed;
struct ssl_write_args {
size_t len;
char wbuf[0];
} __packed;
struct ssl_req {
#define REQ_T_SSL_INIT 0x00
#define REQ_T_SSL_READ 0x01
#define REQ_T_SSL_WRITE 0x02
uint8_t type;
union {
struct ssl_init_args iargs;
struct ssl_read_args rargs;
struct ssl_write_args wargs;
};
} __packed;
struct ssl_rep {
#define REP_T_SSL_INIT 0x00
#define REP_T_SSL_READ 0x01
#define REP_T_SSL_WRITE 0x02
uint8_t type;
ssize_t retval;
ssize_t rbuf_len;
char rbuf[0];
} __packed;
static void ssl_sandbox(void);
void
fetch_sandbox_init(void)
{
fscb = calloc(1, sizeof(struct sandbox_cb));
if(!fscb) {
DPRINTF("[XXX] fscb wasn't initialized!");
exit(-1);
}
sandbox_create(fscb, &ssl_sandbox);
}
void
fetch_sandbox_wait(void)
{
wait(&rv);
DPRINTF("Sandbox's exit status is %d", WEXITSTATUS(rv));
}
static int
fetch_ssl_insandbox(conn_t *conn, const struct url *URL, int verbose)
{
struct ssl_req req;
struct ssl_rep rep;
struct iovec iov_req, iov_rep;
int fdarray[1];
size_t len;
/* Clear out req */
bzero(&req, sizeof(req));
/* Pass needed data */
fdarray[0] = dup(conn->sd); /* Get the file descriptor we need to pass */
req.type = SSL_INIT;
memmove(&req.iargs.URL, URL, sizeof(*URL));
req.iargs.verbose = verbose;
/* Update pointers to request and response structures */
iov_req.iov_base = &req;
iov_req.iov_len = sizeof(req);
iov_rep.iov_base = &rep;
iov_rep.iov_len = sizeof(rep);
/*
* Ask the SSL sandbox to initialize SSL, by sending all the necessary info,
* including the actual fd for the established conn.
*/
if (host_rpc_rights(fscb, SSL_INIT, &iov_req, 1, fdarray, 1, &iov_rep,
1, &len, NULL, NULL) < 0)
err(-1, "host_rpc");
if (len != sizeof(rep))
errx(-1, "host_rpc");
close(fdarray[0]);
/* Upon success set SSL flag on */
if (!rep.retval)
conn->ssl_on = 1;
return (rep.retval);
}
/* Called in parent to proxy the request though the sandbox */
/*
* We do not need to pass the SSL handle as it already exists in the persistent
* sandbox process from the SSL_INIT phase.
*/
static ssize_t
fetch_ssl_read_insandbox(char *buf, size_t rlen)
{
struct ssl_req req;
struct ssl_rep *rep;
struct iovec iov_req, iov_rep;
uint32_t opno, seqno;
u_char *buffer;
size_t len;
/* Clear out req */
bzero(&req, sizeof(req));
/* Update the needed data */
req.type = SSL_READ;
req.rargs.len = rlen;
seqno = 0;
iov_req.iov_base = &req;
iov_req.iov_len = sizeof(req);
/*
* Ask the SSL sandbox to read rlen bytes from the SSL handle.
*/
if (host_sendrpc(fscb, SSL_READ, seqno, &iov_req, 1, NULL, 0) < 0)
err(-1, "host_sendrpc");
if (host_recvrpc(fscb, &opno, &seqno, &buffer, &len) < 0) {
if (errno == EPIPE) {
DPRINTF("[XXX] EPIPE");
exit(-1);
} else if ( (opno != SSL_READ) || (seqno != 0)) {
DPRINTF("[XXX] Wrong operation or sequence number!");
exit(-1);
} else {
DPRINTF("[XXX] sandbox_recvrpc");
err(-1, "sandbox_recvrpc");
}
}
rep = (struct ssl_rep *) buffer;
/* Clone the buffer from the RPC library */
if(rep->rbuf_len > 0)
memmove(buf, &rep->rbuf, rep->rbuf_len);
free(buffer);
return (rep->retval);
}
/* Called in parent to proxy the request though the sandbox */
/*
* We do not need to pass the SSL handle as it already exists in the persistent
* sandbox process from the SSL_INIT phase.
*/
static ssize_t
fetch_ssl_write_insandbox(char *buf, size_t rlen)
{
struct ssl_req *req;
struct ssl_rep *rep;
struct iovec iov_req;
uint32_t opno, seqno;
u_char *buffer;
size_t len, req_msg_size, ret;
/* Clear out req */
req_msg_size = sizeof(*req) + rlen - 1;
req = calloc(1, req_msg_size);
if (!req) {
DPRINTF("[XXX] calloc() failed!");
exit(-1);
}
req->type = SSL_WRITE;
req->wargs.len = rlen;
memmove(req->wargs.wbuf, buf, rlen);
seqno = 0;
iov_req.iov_base = req;
iov_req.iov_len = sizeof(*req);
/*
* Ask the SSL sandbox to read rlen bytes from the SSL handle.
*/
if (host_sendrpc(fscb, SSL_WRITE, seqno, &iov_req, 1, NULL, 0) < 0)
err(-1, "host_sendrpc");
if (host_recvrpc(fscb, &opno, &seqno, &buffer, &len) < 0) {
if (errno == EPIPE) {
DPRINTF("[XXX] EPIPE");
exit(-1);
} else if ( (opno != SSL_WRITE) || (seqno != 0)) {
DPRINTF("[XXX] Wrong operation or sequence number!");
exit(-1);
} else {
DPRINTF("[XXX] sandbox_recvrpc");
err(-1, "sandbox_recvrpc");
}
}
if (len != sizeof(*rep)) {
err(-1, "fetch_ssl_write_insandbox received msg len %zu", len);
return (-1);
}
rep = (struct ssl_rep *) buffer;
ret = rep->retval;
free(buffer);
return (ret);
}
static void
fetch_ssl_shutdown_insandbox(void)
{
uint32_t seqno = 0;
/*
* Ask the SSL sandbox to close the SSL handle and terminate.
* We do not expect anything back.
*/
if (host_sendrpc(fscb, SSL_SHUTDOWN, seqno, NULL, 0, NULL, 0) < 0)
err(-1, "host_sendrpc");
/* XXX IM: Wait for the sandbox to terminate? */
}
/* -------------------------------------------------------------------------- */
/* Called in sandbox and wraps the actual fetch_ssl */
static void
sandbox_fetch_ssl(struct sandbox_cb *scb, uint32_t opno, uint32_t seqno, char
*buffer, size_t len, int *sockfd)
{
struct ssl_req req;
struct ssl_rep rep;
struct iovec iov;
if (len != sizeof(req))
err(-1, "sandbox_fetch: len %zu", len);
if (sockfd)
sconn.sd = dup(*sockfd);
/* Demangle data */
memmove(&req, buffer, sizeof(req));
/* Type should be set correctly */
if (req.type != SSL_INIT)
return;
bzero(&rep, sizeof(rep));
rep.retval = fetch_ssl(&sconn, &req.iargs.URL, req.iargs.verbose);
iov.iov_base = &rep;
iov.iov_len = sizeof(rep);
if (sandbox_sendrpc(scb, opno, seqno, &iov, 1) < 0)
err(-1, "sandbox_sendrpc");
}
/* Called in sandbox and wraps the actual fetch_ssl_read */
static void
sandbox_fetch_ssl_read(struct sandbox_cb *scb, uint32_t opno, uint32_t seqno, char
*buffer, size_t len)
{
struct ssl_req req;
struct ssl_rep *rep;
struct iovec iov;
size_t rep_msg_size;
ssize_t rlen;
if (len != sizeof(req)) {
err(-1, "sandbox_fetch_ssl_read: received msg len %zu", len);
return;
}
/* Demangle data */
memmove(&req, buffer, sizeof(req));
/* Type should be set correctly */
if (req.type != SSL_READ)
return;
rep_msg_size = sizeof(*rep);
if(req.rargs.len)
rep_msg_size += req.rargs.len;
rep = malloc(rep_msg_size);
if(!rep) {
DPRINTF("[XXX] malloc() failed");
exit(-1);
}
rep->type = SSL_READ;
rlen = fetch_ssl_read(sconn.ssl, rep->rbuf, req.rargs.len);
rep->retval = rep->rbuf_len = rlen;
iov.iov_base = rep;
iov.iov_len = rep_msg_size;
if (sandbox_sendrpc(scb, opno, seqno, &iov, 1) < 0)
err(-1, "sandbox_sendrpc");
/* Release resources */
free(rep);
}
/* Called in sandbox and wraps the actual fetch_ssl_read */
static void
sandbox_fetch_ssl_write(struct sandbox_cb *scb, uint32_t opno, uint32_t seqno, char
*buffer, size_t len)
{
struct ssl_req *req;
struct ssl_rep rep;
struct iovec iov;
if (len < sizeof(*req)) {
err(-1, "sandbox_fetch_ssl_read: received msg len %zu", len);
return;
}
req = (struct ssl_req *) buffer;
if(req->type != SSL_WRITE)
return;
rep.retval = fetch_ssl_write(sconn.ssl, req->wargs.wbuf, req->wargs.len);
iov.iov_base = &rep;
iov.iov_len = sizeof(rep);
if (sandbox_sendrpc(scb, opno, seqno, &iov, 1) < 0)
err(-1, "sandbox_sendrpc");
}
static void
ssl_sandbox(void)
{
uint32_t opno, seqno;
u_char *buffer;
size_t len;
int fdarray[1], fdcount; /* We expect a fd for SSL_INIT op */
int *fdp;
int ssl_shutdown = 0;
DPRINTF("===> In ssl_sandbox()");
ssl_initialized = 0;
while (!ssl_shutdown) {
/* No SSL initialized on top of established conn */
if (!ssl_initialized) {
DPRINTF("\tSSL not initialized!");
fdp = fdarray;
fdcount = 1; /* we are waiting for a fd */
}
else {
/*DPRINTF("\tSSL is already initialized!");*/
fdp = NULL;
fdcount = 0; /* SSL is already initialized -- no need for fd */
}
/* Get the output fd and URL from parent */
if (sandbox_recvrpc_rights(fscb, &opno, &seqno, &buffer, &len, fdp,
&fdcount) < 0) {
if (errno == EPIPE) {
DPRINTF("[XXX] EPIPE");
exit(-1);
}
else {
DPRINTF("[XXX] sandbox_recvrpc");
err(-1, "sandbox_recvrpc");
}
}
switch(opno) {
case SSL_INIT:
/* fetch the url and return */
DPRINTF("[OP] SSL INITIALIZATION");
sandbox_fetch_ssl(fscb, opno, seqno, (char *)buffer, len,
&fdarray[0]);
close(fdarray[0]);
ssl_initialized = 1;
break;
case SSL_READ:
/*DPRINTF("[OP] SSL READ");*/
sandbox_fetch_ssl_read(fscb, opno, seqno, (char *) buffer, len);
break;
case SSL_WRITE:
/*DPRINTF("[OP] SSL WRITE");*/
sandbox_fetch_ssl_write(fscb, opno, seqno, (char *) buffer, len);
break;
case SSL_SHUTDOWN:
DPRINTF("[OP] SSL SHUTDOWN");
fetch_ssl_shutdown(&sconn);
ssl_shutdown = 1;
break;
default:
errx(-1, "sandbox_main: unknown operation %d", opno);
}
}
/* Free buffers */
free(buffer);
/* exit */
exit(0);
}
#endif
int
fetch_ssl_wrapper(conn_t *conn, const struct url *URL, int verbose)
{
#ifdef NO_SANDBOX
return (fetch_ssl(conn, URL, verbose));
#else
fetch_sandbox_init();
return (fetch_ssl_insandbox(conn, URL, verbose));
#endif
}
ssize_t
fetch_ssl_read_wrapper(SSL *ssl, char *buf, size_t buflen)
{
#ifdef NO_SANDBOX
return (fetch_ssl_read(ssl, buf, buflen));
#else
return (fetch_ssl_read_insandbox(buf, buflen));
#endif
}
ssize_t
fetch_ssl_write_wrapper(SSL *ssl, char *buf, size_t buflen)
{
#ifdef NO_SANDBOX
return (fetch_ssl_write(ssl, buf, buflen));
#else
return (fetch_ssl_write_insandbox(buf, buflen));
#endif
}
void
fetch_ssl_shutdown_wrapper(conn_t *conn)
{
#ifdef NO_SANDBOX
fetch_ssl_shutdown(conn);
#else
fetch_ssl_shutdown_insandbox();
conn->ssl_on = 0;
#endif
}