forked from CAIDA/mper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscamper_privsep.c
1012 lines (874 loc) · 23.4 KB
/
scamper_privsep.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* scamper_privsep.c: code that does root-required tasks
*
* $Id: scamper_privsep.c,v 1.53 2009/03/10 00:35:31 mjl Exp $
*
* Copyright (C) 2004-2009 The University of Waikato
* Author: Matthew Luckie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef WITHOUT_PRIVSEP
#include "internal.h"
#include "scamper_privsep.h"
#include "scamper_debug.h"
#include "utils.h"
#include "scamper_dl.h"
#include "scamper_rtsock.h"
typedef struct privsep_msg
{
uint16_t plen;
uint16_t type;
} privsep_msg_t;
struct privsep_handler
{
uint8_t type;
int (*handler)(const uint16_t len, const uint8_t *param);
};
static pid_t root_pid = -1; /* the process id of the root code */
static int root_fd = -1; /* the fd the root code send/recv on */
static int lame_fd = -1; /* the fd that the lame code uses */
/*
* the privilege separation code works by allowing the lame process to send
* request messages to the root process. these define the messages that
* the root process understands.
*/
#define SCAMPER_PRIVSEP_EXIT 0x00U
#define SCAMPER_PRIVSEP_OPEN_DATALINK 0x01U
#define SCAMPER_PRIVSEP_OPEN_FILE 0x02U
#define SCAMPER_PRIVSEP_OPEN_RTSOCK 0x03U
#define SCAMPER_PRIVSEP_OPEN_ICMP 0x04U
#define SCAMPER_PRIVSEP_OPEN_DIVERT 0x05U
#define SCAMPER_PRIVSEP_OPEN_SOCK 0x06U
#define SCAMPER_PRIVSEP_OPEN_RAWUDP 0x07U
/*
* the privilege separation code permits both the privileged opening of file
* descriptors and the execution of privileged operations. the first half
* of the operations are for opening file descriptors; the second half are
* for doing a task. At the moment, there are no tasks defined so the next
* two #defines resolve to the same value.
*/
#define SCAMPER_PRIVSEP_MAXTYPE (SCAMPER_PRIVSEP_OPEN_RAWUDP)
/*
* privsep_open_rawsock
*
* open a raw icmp socket. one integer parameter corresponding to the 'type'
* is supplied in param.
*
*/
static int privsep_open_icmp(const uint16_t plen, const uint8_t *param)
{
int type, protocol;
if(plen != sizeof(type))
{
scamper_debug(__func__, "plen %d != %d", plen, sizeof(type));
return -1;
}
memcpy(&type, param, sizeof(type));
if(type == AF_INET)
{
protocol = IPPROTO_ICMP;
}
else if(type == AF_INET6)
{
protocol = IPPROTO_ICMPV6;
}
else
{
scamper_debug(__func__, "type %d != AF_INET || AF_INET6", type);
errno = EINVAL;
return -1;
}
return socket(type, SOCK_RAW, protocol);
}
/*
* privsep_open_rtsock
*
* open a routing socket. there are no parameters permitted to this
* method call.
*/
static int privsep_open_rtsock(const uint16_t plen, const uint8_t *param)
{
if(plen != 0)
{
scamper_debug(__func__, "plen %d != 0", plen, 0);
errno = EINVAL;
return -1;
}
return scamper_rtsock_open_fd();
}
/*
* privsep_open_datalink
*
* open a BPF or PF_PACKET socket to the datalink. the param has a single
* field: the ifindex of the device to monitor.
*/
static int privsep_open_datalink(const uint16_t plen, const uint8_t *param)
{
int ifindex;
/* the payload should have an integer field - no more, no less. */
if(plen != sizeof(ifindex))
{
scamper_debug(__func__, "plen %d != %d", plen, sizeof(ifindex));
errno = EINVAL;
return -1;
}
memcpy(&ifindex, param, sizeof(ifindex));
return scamper_dl_open_fd(ifindex);
}
/*
* privsep_open_divert
*
* open a divert socket. bind it to the port supplied.
*/
static int privsep_open_divert(const uint16_t plen, const uint8_t *param)
{
int fd = -1;
#if defined(IPPROTO_DIVERT)
struct sockaddr_in sin;
int port;
if(plen != sizeof(port))
{
scamper_debug(__func__, "plen %d != %d", plen, sizeof(port));
errno = EINVAL;
return -1;
}
memcpy(&port, param, sizeof(port));
if((fd = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT)) == -1)
{
printerror(errno, strerror, __func__, "could not open socket");
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_len = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
if(bind(fd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
printerror(errno, strerror, __func__, "could not bind socket");
return -1;
}
#else
scamper_debug(__func__, "divert sockets not supported");
errno = EINVAL;
#endif
return fd;
}
static int privsep_open_sock(const uint16_t plen, const uint8_t *param)
{
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
int domain, type, protocol, port;
const size_t size = sizeof(domain) + sizeof(protocol) + sizeof(port);
size_t off = 0;
int fd = -1;
if(plen != size)
{
scamper_debug(__func__, "plen %d != %d", plen, size);
errno = EINVAL;
goto err;
}
off = 0;
memcpy(&domain, param+off, sizeof(domain)); off += sizeof(domain);
memcpy(&protocol, param+off, sizeof(protocol)); off += sizeof(protocol);
memcpy(&port, param+off, sizeof(port));
if(port < 1 || port > 65535)
{
scamper_debug(__func__, "refusing to bind to port %d", port);
errno = EINVAL;
goto err;
}
if(protocol == IPPROTO_TCP) type = SOCK_STREAM;
else if(protocol == IPPROTO_UDP) type = SOCK_DGRAM;
else
{
scamper_debug(__func__, "unhandled IPv4 protocol %d", protocol);
errno = EINVAL;
goto err;
}
if(domain == AF_INET)
{
if((fd = socket(AF_INET, type, protocol)) == -1)
{
printerror(errno, strerror, __func__, "could not open IPv4 socket");
goto err;
}
sockaddr_compose((struct sockaddr *)&sin4, AF_INET, NULL, port);
if(bind(fd, (struct sockaddr *)&sin4, sizeof(sin4)) == -1)
{
printerror(errno, strerror, __func__,
"could not bind to IPv4 protocol %d port %d",
protocol, port);
goto err;
}
}
else if(domain == AF_INET6)
{
if((fd = socket(AF_INET6, type, protocol)) == -1)
{
printerror(errno, strerror, __func__, "could not open IPv6 socket");
goto err;
}
sockaddr_compose((struct sockaddr *)&sin6, AF_INET6, NULL, port);
if(bind(fd, (struct sockaddr *)&sin6, sizeof(sin6)) == -1)
{
printerror(errno, strerror, __func__,
"could not bind to IPv6 protocol %d port %d",
protocol, port);
goto err;
}
}
else return -1;
return fd;
err:
if(fd != -1) close(fd);
return -1;
}
static int privsep_open_rawudp(const uint16_t plen, const uint8_t *param)
{
struct in_addr in;
struct sockaddr_in sin4;
int port, fd;
char tmp[32];
if(plen != 4 + sizeof(port))
{
scamper_debug(__func__, "plen %d != %d", plen, 4 + sizeof(port));
errno = EINVAL;
return -1;
}
memcpy(&in, param+0, sizeof(in));
memcpy(&port, param+4, sizeof(port));
if(port < 1 || port > 65535)
{
scamper_debug(__func__, "refusing to bind to port %d", port);
errno = EINVAL;
return -1;
}
if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
{
printerror(errno, strerror, __func__, "could not open socket");
return -1;
}
sockaddr_compose((struct sockaddr *)&sin4, AF_INET, &in, port);
if(bind(fd, (struct sockaddr *)&sin4, sizeof(sin4)) == -1)
{
printerror(errno, strerror, __func__, "could not bind %s:%d",
inet_ntop(AF_INET, &in, tmp, sizeof(tmp)), port);
close(fd);
return -1;
}
return fd;
}
/*
* privsep_open_file
*
* switch to the user running the process and open the file specified.
* the param has two fields in it: the mode of open, and the file to open.
*/
static int privsep_open_file(const uint16_t plen, const uint8_t *param)
{
const char *file;
uid_t uid, euid;
int flags;
mode_t mode;
uint16_t off;
int fd;
/*
* if the payload of param is not large enough to hold the flags and a
* filename, then don't go any further
*/
if(plen < sizeof(int) + 2)
{
return -1;
}
memcpy(&flags, param, sizeof(int));
off = sizeof(int);
/* if the O_CREAT flag is set, we need to fetch the mode parameter too */
if(flags & O_CREAT)
{
/*
* the payload length of the parameter must be large enough to hold
* the flags, mode, and a filename
*/
if(plen < off + sizeof(mode_t) + 2)
{
return -1;
}
memcpy(&mode, param+off, sizeof(mode));
off += sizeof(mode_t);
}
file = (const char *)(param + off);
/*
* make sure the length of the file to open checks out.
* the last byte of the string must be a null character.
*/
if(file[plen-off-1] != '\0')
{
scamper_debug(__func__, "filename not terminated with a null");
return -1;
}
uid = getuid();
euid = geteuid();
/* set our effective uid to be the user who started scamper */
if(seteuid(uid) == -1)
{
return -1;
}
if(flags & O_CREAT)
{
fd = open(file, flags, mode);
}
else
{
fd = open(file, flags);
}
/*
* ask for our root permissions back. if we can't get them back, then
* this process is crippled and it might as well exit now.
*/
if(seteuid(euid) == -1)
{
if(fd != -1) close(fd);
exit(-errno);
}
return fd;
}
/*
* privsep_send_fd
*
* send the fd created using the priviledged code. if the fd was not
* successfully created, we send the errno back in the payload of the
* message.
*/
static int privsep_send_fd(const int fd,const int error,const uint8_t msg_type)
{
uint8_t buf[sizeof(int)];
struct msghdr msg;
struct iovec vec;
#if !defined(HAVE_ACCRIGHTS)
struct cmsghdr *cmsg;
uint8_t tmp[CMSG_LEN(sizeof(int))];
#endif
scamper_debug(__func__, "fd: %d error: %d msg_type: 0x%02x",
fd, error, msg_type);
memset(&vec, 0, sizeof(vec));
memset(&msg, 0, sizeof(msg));
memcpy(buf, &error, sizeof(int));
vec.iov_base = buf;
vec.iov_len = sizeof(buf);
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
if(fd != -1)
{
#if defined(HAVE_ACCRIGHTS)
msg.msg_accrights = (caddr_t)&fd;
msg.msg_accrightslen = sizeof(fd);
#else
msg.msg_control = (caddr_t)tmp;
msg.msg_controllen = CMSG_LEN(sizeof(int));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = fd;
#endif
}
if(sendmsg(root_fd, &msg, 0) == -1)
{
return -1;
}
return 0;
}
static int privsep_recv_fd(void)
{
struct msghdr msg;
struct iovec vec;
ssize_t rc;
int fd = -1, error;
#if !defined(HAVE_ACCRIGHTS)
struct cmsghdr *cmsg;
uint8_t tmp[CMSG_LEN(sizeof(int))];
#endif
memset(&vec, 0, sizeof(vec));
memset(&msg, 0, sizeof(msg));
vec.iov_base = (char *)&error;
vec.iov_len = sizeof(error);
msg.msg_iov = &vec;
msg.msg_iovlen = 1;
#if defined(HAVE_ACCRIGHTS)
msg.msg_accrights = (caddr_t)&fd;
msg.msg_accrightslen = sizeof(fd);
#else
msg.msg_control = tmp;
msg.msg_controllen = sizeof(tmp);
#endif
if((rc = recvmsg(lame_fd, &msg, 0)) == -1)
{
printerror(errno, strerror, __func__, "recvmsg failed");
return -1;
}
else if(rc != sizeof(error))
{
return -1;
}
if(error == 0)
{
#if defined(HAVE_ACCRIGHTS)
if(msg.msg_accrightslen != sizeof(fd))
{
fd = -1;
}
#else
cmsg = CMSG_FIRSTHDR(&msg);
if(cmsg != NULL && cmsg->cmsg_type == SCM_RIGHTS)
{
fd = (*(int *)CMSG_DATA(cmsg));
}
#endif
}
else
{
errno = error;
}
return fd;
}
/*
* privsep_do
*
* this is the only piece of code with root priviledges. we use it to
* create raw sockets, routing/netlink sockets, BPF/PF_PACKET sockets, and
* ordinary files that scamper itself cannot do by itself.
*/
static int privsep_do(void)
{
static int (* const func[])(const uint16_t plen, const uint8_t *param) = {
NULL, /* SCAMPER_PRIVSEP_EXIT */
privsep_open_datalink, /* SCAMPER_PRIVSEP_OPEN_DATALINK */
privsep_open_file, /* SCAMPER_PRIVSEP_OPEN_FILE */
privsep_open_rtsock, /* SCAMPER_PRIVSEP_OPEN_RTSOCK */
privsep_open_icmp, /* SCAMPER_PRIVSEP_OPEN_ICMP */
privsep_open_divert, /* SCAMPER_PRIVSEP_OPEN_DIVERT */
privsep_open_sock, /* SCAMPER_PRIVSEP_OPEN_SOCK */
privsep_open_rawudp, /* SCAMPER_PRIVSEP_OPEN_RAWUDP */
};
privsep_msg_t msg;
void *data = NULL;
int ret = 0, error;
int fd;
#if defined(HAVE_SETPROCTITLE)
setproctitle("%s", "[priv]");
#endif
/* might as well set our copy of the root_pid to something useful */
root_pid = getpid();
/*
* the priviledged process does not need the lame file descriptor for
* anything, so get rid of it
*/
close(lame_fd);
lame_fd = -1;
for(;;)
{
/* read the msg header */
if((ret = read_wrap(root_fd, (uint8_t *)&msg, NULL, sizeof(msg))) != 0)
{
if(ret == -1)
{
printerror(errno, strerror, __func__, "could not read msg hdr");
}
break;
}
/* if we've been told to exit, then do so now */
if(msg.type == SCAMPER_PRIVSEP_EXIT)
{
break;
}
if(msg.type > SCAMPER_PRIVSEP_MAXTYPE)
{
scamper_debug(__func__, "msg %d > maxtype", msg.type);
ret = -EINVAL;
break;
}
/* if there is more data to read, read it now */
if(msg.plen != 0)
{
if((data = malloc(msg.plen)) == NULL)
{
printerror(errno, strerror, __func__, "couldnt malloc data");
ret = (-errno);
break;
}
if((ret = read_wrap(root_fd, data, NULL, msg.plen)) != 0)
{
printerror(errno, strerror, __func__, "couldnt read data");
free(data);
break;
}
}
else data = NULL;
if((fd = func[msg.type](msg.plen, data)) == -1)
{
error = errno;
}
else
{
error = 0;
}
/* we don't need the data we read anymore */
if(data != NULL)
{
free(data);
}
/* send the file descriptor back to the lame process */
if(privsep_send_fd(fd, error, msg.type) == -1)
{
printerror(errno, strerror, __func__, "couldnt send fd");
break;
}
/*
* if we have a file descriptor that has been passed to the parent
* process, we don't need our copy any longer
*/
if(fd != -1)
{
close(fd);
}
}
close(root_fd);
return ret;
}
/*
* privsep_lame_send
*
* compose and send the messages necessary to communicate with the root
* process.
*/
static int privsep_lame_send(const uint16_t type, const uint16_t len,
const uint8_t *param)
{
privsep_msg_t msg;
assert(type != SCAMPER_PRIVSEP_EXIT);
assert(type <= SCAMPER_PRIVSEP_MAXTYPE);
/* send the header first */
msg.type = type;
msg.plen = len;
if(write_wrap(lame_fd, &msg, NULL, sizeof(msg)) == -1)
{
printerror(errno, strerror, __func__, "could not send msg header");
return -1;
}
/* if there is a parameter data to send, send it now */
if(len != 0 && write_wrap(lame_fd, param, NULL, len) == -1)
{
printerror(errno, strerror, __func__, "could not send msg param");
return -1;
}
return 0;
}
/*
* privsep_getfd
*
* send a request to the piece of code running as root to do open a file
* descriptor that requires priviledge to do. return the file descriptor.
*/
static int privsep_getfd(const uint16_t type, const uint16_t len,
const uint8_t *param)
{
if(privsep_lame_send(type, len, param) == -1)
{
return -1;
}
return privsep_recv_fd();
}
static int privsep_getfd_1int(const uint16_t type, const int p1)
{
uint8_t param[sizeof(p1)];
memcpy(param, &p1, sizeof(p1));
return privsep_getfd(type, sizeof(param), param);
}
static int privsep_getfd_3int(const uint16_t type,
const int p1, const int p2, const int p3)
{
uint8_t param[sizeof(p1)+sizeof(p2)+sizeof(p3)];
size_t off = 0;
memcpy(param+off, &p1, sizeof(p1)); off += sizeof(p1);
memcpy(param+off, &p2, sizeof(p2)); off += sizeof(p2);
memcpy(param+off, &p3, sizeof(p3)); off += sizeof(p3);
assert(off == sizeof(param));
return privsep_getfd(type, sizeof(param), param);
}
int scamper_privsep_open_datalink(const int ifindex)
{
return privsep_getfd_1int(SCAMPER_PRIVSEP_OPEN_DATALINK, ifindex);
}
int scamper_privsep_open_file(const char *file,
const int flags, const mode_t mode)
{
uint8_t *param;
int off, len, fd;
/*
* decide how big the message is going to be. don't pass it if the message
* length parameter constrains us
*/
len = sizeof(flags) + strlen(file) + 1;
if(flags & O_CREAT)
{
len += sizeof(mode);
}
/*
* the len is fixed because the length parameter used in the privsep
* header is a 16-bit unsigned integer.
*/
if(len > 65535)
{
return -1;
}
/* allocate the parameter */
if((param = malloc(len)) == NULL)
{
return -1;
}
/* copy in the flags parameter, and the mode parameter if necessary */
memcpy(param, &flags, sizeof(flags)); off = sizeof(flags);
if(flags & O_CREAT)
{
memcpy(param+off, &mode, sizeof(mode));
off += sizeof(mode);
}
/* finally copy in the name of the file to open */
memcpy(param+off, file, len-off);
/* get the file descriptor and return it */
fd = privsep_getfd(SCAMPER_PRIVSEP_OPEN_FILE, len, param);
free(param);
return fd;
}
int scamper_privsep_open_rtsock(void)
{
return privsep_getfd(SCAMPER_PRIVSEP_OPEN_RTSOCK, 0, NULL);
}
int scamper_privsep_open_icmp(const int domain)
{
return privsep_getfd_1int(SCAMPER_PRIVSEP_OPEN_ICMP, domain);
}
int scamper_privsep_open_divert(const int port)
{
return privsep_getfd_1int(SCAMPER_PRIVSEP_OPEN_DIVERT, port);
}
int scamper_privsep_open_tcp(const int domain, const int port)
{
return privsep_getfd_3int(SCAMPER_PRIVSEP_OPEN_SOCK,domain,IPPROTO_TCP,port);
}
int scamper_privsep_open_udp(const int domain, const int port)
{
return privsep_getfd_3int(SCAMPER_PRIVSEP_OPEN_SOCK,domain,IPPROTO_UDP,port);
}
int scamper_privsep_open_rawudp(const void *addr, const int port)
{
uint8_t param[4+sizeof(port)];
size_t off = 0;
if(addr == NULL)
memset(param+off, 0, 4);
else
memcpy(param+off, addr, 4);
off += 4;
memcpy(param+off, &port, sizeof(port)); off += sizeof(port);
assert(off == sizeof(param));
return privsep_getfd(SCAMPER_PRIVSEP_OPEN_RAWUDP, sizeof(param), param);
}
/*
* scamper_privsep
*
* start a child process that has the root priviledges that scamper starts
* with. then, revoke scamper's priviledges to the minimum scamper can
* obtain
*/
int scamper_privsep_init()
{
struct addrinfo hints, *res0;
struct timeval tv;
struct passwd *pw;
struct stat sb;
mode_t mode;
uid_t uid;
gid_t gid;
int sockets[2];
pid_t pid;
int ret;
time_t t;
/* check to see if the PRIVSEP_DIR exists */
if(stat(PRIVSEP_DIR, &sb) == -1)
{
/* if the directory does not exist, try and create it now */
if(errno == ENOENT)
{
/*
* get the uid of the user who will get ownership of the directory.
* by default, this will be root.
*/
if((pw = getpwnam(PRIVSEP_DIR_USER)) == NULL)
{
printerror(errno, strerror, __func__,
"could not getpwnam " PRIVSEP_DIR_USER);
endpwent();
return -1;
}
uid = pw->pw_uid;
endpwent();
gid = 0;
/* create the directory as 555 : no one can write to it */
mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
if(mkdir(PRIVSEP_DIR, mode) == -1)
{
printerror(errno, strerror, __func__,
"could not mkdir " PRIVSEP_DIR);
return -1;
}
/* assign ownership appropriately */
if(chown(PRIVSEP_DIR, uid, gid) == -1)
{
printerror(errno, strerror, __func__,
"could not chown " PRIVSEP_DIR);
rmdir(PRIVSEP_DIR);
return -1;
}
}
else
{
printerror(errno, strerror, __func__, "could not stat " PRIVSEP_DIR);
return -1;
}
}
/*
* open up the unix domain sockets that will allow the prober to talk
* with the priviledged process
*/
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1)
{
printerror(errno, strerror, __func__, "could not socketpair");
return -1;
}
lame_fd = sockets[0];
root_fd = sockets[1];
if((pid = fork()) == -1)
{
printerror(errno, strerror, __func__, "could not fork");
return -1;
}
else if(pid == 0) /* child */
{
/*
* this is the process that will do the root tasks.
* when this function exits, we call exit() on the forked process.
*/
ret = privsep_do();
exit(ret);
}
/* set our copy of the root_pid to the relevant process id */
root_pid = pid;
/*
* we don't need our copy of the file descriptor passed to the priviledged
* process any longer
*/
close(root_fd);
root_fd = -1;
/*
* get the details for the PRIVSEP_USER login, which the rest of scamper
* will use to get things done
*/
if((pw = getpwnam(PRIVSEP_USER)) == NULL)
{
printerror(errno, strerror, __func__,
"could not getpwnam " PRIVSEP_USER);
return -1;
}
uid = pw->pw_uid;
gid = pw->pw_gid;
memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
endpwent();
/*
* call localtime now, as then the unpriviledged process will have the
* local time zone information cached in the process, so localtime will
* actually mean something
*/
gettimeofday_wrap(&tv);
t = tv.tv_sec;
localtime(&t);
/*
* call getaddrinfo now, as then the unpriviledged process will load
* whatever files it needs to to help resolve IP addresses; the need for
* this was first noticed in SunOS
*/
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_family = AF_INET;
getaddrinfo("127.0.0.1", NULL, &hints, &res0);
freeaddrinfo(res0);
/* change the root directory of the unpriviledged directory */
#ifndef NDEBUG
if(chroot(PRIVSEP_DIR) == -1)
{
printerror(errno, strerror, __func__,
"could not chroot to " PRIVSEP_DIR);
return -1;
}
/* go into the chroot environment */
if(chdir("/") == -1)
{
printerror(errno, strerror, __func__, "could not chdir /");
return -1;
}
#endif
/* change the operating group */
if(setgroups(1, &gid) == -1)
{
printerror(errno, strerror, __func__, "could not setgroups");
return -1;
}
if(setgid(gid) == -1)
{
printerror(errno, strerror, __func__, "could not setgid");
return -1;
}
/* change the operating user */
if(setuid(uid) == -1)
{
printerror(errno, strerror, __func__, "could not setuid");
return -1;
}
return lame_fd;
}
void scamper_privsep_cleanup()
{
privsep_msg_t msg;
if(root_pid != -1)
{
msg.plen = 0;
msg.type = SCAMPER_PRIVSEP_EXIT;
write_wrap(lame_fd, (uint8_t *)&msg, NULL, sizeof(msg));
root_pid = -1;