-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdived.c
1990 lines (1754 loc) · 68.5 KB
/
dived.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
// dived -- Server for seamless program starter inside unshared namespaces
// License=MIT ; Created by Vitaly "_Vi" Shukela in 2012
#include "config.h"
#define _GNU_SOURCE
#include <sched.h> // clone
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h> // for umask
#include <sys/socket.h>
#include <sys/un.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/select.h>
#ifndef SIGNALFD_WORKAROUND
#include <sys/signalfd.h>
#endif
#include <string.h>
#include <unistd.h>
#ifndef NO_RLIMIT
#include <sys/resource.h>
#endif
#ifndef NO_CAPABILITIES
#include <sys/capability.h>
#ifndef SECUREBITS_WORKAROUND
#include <linux/securebits.h>
#else
// build supporting this feature even if system does not support it yet
#include "hacks/securebits.h"
#endif
#endif
#ifdef CLONE_PARAMETERS_WORKAROUND
#include "hacks/clone_parameters.h"
#endif // CLONE_PARAMETERS_WORKAROUND
#include <sys/prctl.h>
#include "recv_fd.h"
#include "safer.h"
/* Maximum FD number */
#define MAXFD 1024
/* Maximum number of groups specified by --groups */
#define MAXGROUPS 128
#define VERSION 1100
#define VERSION2 "v1.9.0"
#define CLONE_STACK_SIZE (1024*16)
// For use with "--unshare"
// list of FDs that should be closed for non-client process
int saved_fdnums[MAXFD];
extern char **environ;
void sigchild(int arg) {
int status;
wait(&status);
}
#define MAX_SETNS_FILES 8
#define MAX_WRITECONTENT_FILES 8
#define MAX_PIDFILES 12
#define MAX_RLIMIT_SETS 30
struct rlimit_setter {
int resource;
struct rlimit limits;
};
struct dived_options {
int sock;
char* socket_path;
int nochilddaemon;
int nodaemon;
int nofork;
int noprivs;
int nosetsid;
int nocsctty;
char* forceuser;
char* forcegroups;
char* effective_user;
char* pidfiles[MAX_PIDFILES];
char* chmod_;
char* chown_;
char** forced_argv;
int forced_argv_count;
int client_umask;
int client_chdir;
int client_fds;
int client_environment;
int client_argv;
char* chroot_;
char* chdir_;
char* setns_files[MAX_SETNS_FILES];
char** envp;
char* unshare_;
int inetd;
int client_chroot;
int root_to_current;
char* authentication_program;
char* retain_capabilities;
char* remove_capabilities;
char* ambient_capabilities;
char* set_capabilities;
int no_new_privs;
int just_execute;
int lock_securebits;
int signal_processing;
int fork_and_wait_for_exit_code;
struct rlimit_setter rlimits[MAX_RLIMIT_SETS];
char* pivotroot_newroot;
char* pivotroot_putold;
char* writecontent_files[MAX_WRITECONTENT_FILES];
char* writecontent_data[MAX_WRITECONTENT_FILES];
int maximum_fd_count;
int maximum_argv_count;
int maximum_argv_size;
int maximum_envp_count;
int maximum_envp_size;
} options;
#ifndef NO_PIVOTROOT
#ifndef PIVOTROOT_WORKAROUND
int pivot_root(const char* newroot, const char* putold);
#else
int pivot_root(const char* newroot, const char* putold) {
return syscall(SYS_pivot_root, newroot, putold, 0, 0);
}
#endif
#endif // NO_PIVOTROOT
struct serve_client_context {
int fd;
struct ucred cred;
struct passwd *client_cred;
int terminal_fd;
const char* username;
uid_t effective_user;
gid_t effective_group;
uid_t targetuid;
gid_t targetgid;
/* When we receive and apply (dup2) file descriptors, it can happen that we need to replace the fd itself.
* In this postpone applying it until we close "fd" */
int debt_instead_of_fd;
int exit_code_or_signal_processing_enabled;
int initialisation_finished_event[2];
int signal_fd;
int pid2;
char** argv;
char** envp_;
int dive_signal_fd;
pid_t pgid;
};
int serve_client_setns(struct dived_options *opts, struct serve_client_context *ctx) {
#ifndef NO_SETNS
{
int i;
for (i=0; i<MAX_SETNS_FILES; ++i) {
if (opts->setns_files[i]) {
int nsfd = open(opts->setns_files[i], O_RDONLY);
if(nsfd==-1) {
perror("open");
return 23;
}
if(setns(nsfd, 0) == -1) {
perror("setns");
return 23;
}
close(nsfd);
}
}
}
#endif
return 0;
}
int serve_client_write_content(struct dived_options *opts, struct serve_client_context *ctx) {
int i;
for (i=0; i<MAX_WRITECONTENT_FILES; ++i) {
if (opts->writecontent_files[i]) {
const char* n = opts->writecontent_files[i];
const char* c = opts->writecontent_data[i];
size_t cl = strlen(c);
int fd = open(n, O_WRONLY|O_TRUNC|O_CREAT, 0777);
if (fd == -1) {
perror("open");
return 23;
}
int ret = safer_write(fd, c, cl);
if (ret == -1) {
close(fd);
perror("write");
return 23;
}
close(fd);
if (ret < cl) {
fprintf(stderr, "Can't write content file in full. Aborting.\n");
return 23;
}
}
}
return 0;
}
int serve_client_opt_chdir_chroot(struct dived_options *opts, struct serve_client_context *ctx) {
DIR* chdir_here_after_chroot = NULL;
if (opts->chdir_ && !opts->chroot_) {
int ret = chdir(opts->chdir_);
if (ret==-1) {
perror("chdir");
return 23;
}
}
// save away current directory before it is mangled by chroot
if (opts->chdir_ && opts->chroot_) {
chdir_here_after_chroot = opendir(opts->chdir_);
if (!chdir_here_after_chroot) {
perror("opendir for later chdir");
return 23;
}
}
if (opts->chroot_) {
int ret = chroot(opts->chroot_);
if (ret==-1) {
perror("chroot");
return 23;
}
}
if (chdir_here_after_chroot) {
int ret = fchdir(dirfd(chdir_here_after_chroot));
if (ret==-1) {
perror("fchdir");
return 23;
}
closedir(chdir_here_after_chroot);
}
#ifndef NO_PIVOTROOT
if (opts->pivotroot_newroot) {
int ret = pivot_root(opts->pivotroot_newroot, opts->pivotroot_putold);
if (ret==-1) {
perror("pivot_root");
return 23;
}
}
#endif // NO_PIVOTROOT
return 0;
}
int serve_client_getcred(struct dived_options *opts, struct serve_client_context *ctx) {
socklen_t len = sizeof(struct ucred);
if (!opts->just_execute) {
if (getsockopt(ctx->fd, SOL_SOCKET, SO_PEERCRED, &ctx->cred, &len) == -1) {
perror("getsockopt SOL_SOCKET SO_PEERCRED");
if (!opts->noprivs) {
return 23;
}
} else {
ctx->client_cred = getpwuid(ctx->cred.uid);
}
} else {
// --just-execute
ctx->cred.uid = getuid();
ctx->cred.gid = getgid();
ctx->cred.pid = getpid();
}
return 0;
}
int serve_client_set_dive_envvars(struct dived_options *opts, struct serve_client_context *ctx) {
{
char buffer[64];
sprintf(buffer, "%d", ctx->cred.uid); setenv("DIVE_UID", buffer, 1);
sprintf(buffer, "%d", ctx->cred.gid); setenv("DIVE_GID", buffer, 1);
sprintf(buffer, "%d", ctx->cred.pid); setenv("DIVE_PID", buffer, 1);
if (ctx->client_cred) {
setenv("DIVE_USER", ctx->client_cred->pw_name, 1);
} else {
setenv("DIVE_USER", "", 1);
}
}
//printf("pid=%ld, euid=%ld, egid=%ld\n", (long) ctx->cred.pid, (long) ctx->cred.uid, (long) ctx->cred.gid);
return 0;
}
int serve_client_daemon(struct dived_options *opts, struct serve_client_context *ctx) {
if(!opts->nochilddaemon) {
int ret = daemon(0,0);
if (ret==-1) {
perror("daemon");
// considering this error as non-important and not exiting
}
}
return 0;
}
int serve_client_recv_fds_flags(struct dived_options *opts, struct serve_client_context *ctx) {
long int version = VERSION;
safer_write(ctx->fd, (char*)&version, sizeof(version));
/* Send pid */
pid_t mypid = getpid();
safer_write(ctx->fd, (char*)&mypid, sizeof(mypid));
/* Receive file descriptor to be controlling terminal */
ctx->terminal_fd = recv_fd(ctx->fd);
/* Receive flag whether client wants us to stay and wait for exit code */
int waiting_requested;
safer_read(ctx->fd, (char*)&waiting_requested, sizeof(waiting_requested));
if (!waiting_requested) ctx->exit_code_or_signal_processing_enabled=0;
/* Send whether we will wait for the client */
safer_write(ctx->fd, (char*)&ctx->exit_code_or_signal_processing_enabled, sizeof(ctx->exit_code_or_signal_processing_enabled));
/* Receive and apply file descriptors */
int received_fd_count = 0;
memset(saved_fdnums, 0, sizeof saved_fdnums);
for(;;) {
int i;
safer_read(ctx->fd, (char*)&i, sizeof(i));
if(i==-1) {
break;
}
if(i<-1 || i>=MAXFD) {
fprintf(stderr, "dived: Wrong file descriptor number %d\n", i);
return 7;
}
int f = recv_fd(ctx->fd);
if (!opts->client_fds) {
close(f);
continue;
}
if (received_fd_count == opts->maximum_fd_count) {
fprintf(stderr, "dived: Too many FDs, ignoring %d\n", i);
close(f);
continue;
}
++received_fd_count;
if(i==ctx->fd) {
ctx->debt_instead_of_fd=f;
} else if (i==ctx->debt_instead_of_fd) {
// Now "debt_instead_of_fd wants" to become "fd" and
// "i" wants to become "debt_instead_of_fd"
// reroute the debt to new FD.
// Unfortunately this may be "kicked down the street"
// again and again as more FDs are received
int newfd = dup(ctx->debt_instead_of_fd);
if (newfd == -1) { perror("dup"); return 1; }
ctx->debt_instead_of_fd = newfd;
// this also closes "i" aka the former debt_instead_of_fd
if (dup2(f, i) == i) {
saved_fdnums[i]=1;
} else {
perror("dup2");
}
close(f);
} else {
if(f!=i) {
if(dup2(f, i)==i) {
saved_fdnums[i]=1;
} else {
perror("dup2");
}
close(f);
} else {
saved_fdnums[i]=1;
}
}
}
return 0;
}
int serve_client_auth_prog(struct dived_options *opts, struct serve_client_context *ctx) {
if (opts->authentication_program) {
if (system(opts->authentication_program)) {
return 1;
}
}
return 0;
}
int serve_client_umask(struct dived_options *opts, struct serve_client_context *ctx) {
/* Receive and apply umask */
mode_t umask_;
safer_read(ctx->fd, (char*)&umask_, sizeof(umask_));
if (opts->client_umask) umask(umask_);
return 0;
}
int serve_client_rootdir(struct dived_options *opts, struct serve_client_context *ctx) {
/* Receive and apply root directory */
int rootdir = recv_fd(ctx->fd);
if (opts->client_chroot) {
DIR* curdir = NULL;
if (!opts->client_chdir) {
if (opts->root_to_current) {
curdir = opendir("/");
} else {
curdir = opendir(".");
}
}
int ret = fchdir(rootdir);
if (ret==-1) {
perror("fchdir");
/* Inability to do client-managed chroot is not a security error, so continuing */
} else {
ret = chroot(".");
if (ret==-1) {
perror("chroot");
}
}
close(rootdir);
if (!opts->client_chdir) {
ret = fchdir(dirfd(curdir));
if (ret==-1) {
perror("fchdir");
}
closedir(curdir);
}
} else {
close(rootdir);
}
return 0;
}
int serve_client_curdir(struct dived_options *opts, struct serve_client_context *ctx) {
/* Receive and apply current directory */
int curdir = recv_fd(ctx->fd);
if (!opts->root_to_current) {
if (opts->client_chdir) {
int ret = fchdir(curdir);
if (ret==-1) {
perror("fchdir");
/* Not considering preservance of current directory instead
of client-choosed one as a security issue, continuing */
}
}
} else {
if (!opts->client_chroot) {
int ret = chdir("/");
if (ret==-1) {
perror("chdir");
return 23;
}
}
}
close(curdir);
return 0;
}
int serve_client_setsid(struct dived_options *opts, struct serve_client_context *ctx) {
if (!opts->nosetsid) {
setpgid(0, getppid());
setsid();
ctx->pgid = getpgid(0);
}
return 0;
}
int serve_client_csctty(struct dived_options *opts, struct serve_client_context *ctx) {
if (!opts->nocsctty) {
if (ctx->terminal_fd != -1) {
#ifndef TIOCSCTTY
#define TIOCSCTTY 0x540E
#endif
ioctl (ctx->terminal_fd, TIOCSCTTY, 1);
}
}
close(ctx->terminal_fd);
return 0;
}
int serve_client_securebits(struct dived_options *opts, struct serve_client_context *ctx) {
#ifndef NO_CAPABILITIES
if (opts->set_capabilities || opts->lock_securebits) {
if (!opts->lock_securebits) {
prctl(PR_SET_SECUREBITS, SECBIT_KEEP_CAPS, 0, 0);
} else {
if(prctl(PR_SET_SECUREBITS,
SECBIT_KEEP_CAPS |
SECBIT_NO_SETUID_FIXUP | SECBIT_NO_SETUID_FIXUP_LOCKED |
SECBIT_NOROOT | SECBIT_NOROOT_LOCKED
, 0, 0) == -1) {
perror("prctl(PR_SET_SECUREBITS)");
return -1;
}
}
}
#endif
return 0;
}
int serve_client_rlimit(struct dived_options *opts, struct serve_client_context *ctx) {
/* Set resource limits, if needed */
#ifndef NO_RLIMIT
if(opts->rlimits[0].resource!=-1) {
int j;
for(j=0; j<MAX_RLIMIT_SETS; ++j) {
struct rlimit_setter *r = &opts->rlimits[j];
if(r->resource==-1)break;
if(setrlimit(r->resource, &r->limits) == -1) {
perror("setrlimit");
return -1;
}
}
}
#endif
return 0;
}
int serve_client_decide_user(struct dived_options *opts, struct serve_client_context *ctx) {
/* Decide what user shall we change to */
struct passwd *pw = NULL;
ctx->targetuid = ctx->cred.uid;
ctx->targetgid = ctx->cred.gid;
if (opts->forceuser) {
if (!opts->forcegroups) {
pw = getpwnam(opts->forceuser);
if (pw) {
ctx->targetuid = pw->pw_uid;
ctx->targetgid = pw->pw_gid;
} else {
ctx->targetuid = -1;
fprintf(stderr, "Failed to getpwnam the specified user\n");
fprintf(stderr, "If you want to set uid by number then also specify --groups \n");
return 112;
}
} else {
ctx->targetuid = -1;
sscanf(opts->forceuser, "%d", &ctx->targetuid);
if (ctx->targetuid == -1) {
struct passwd *userp = getpwnam(opts->forceuser);
if (!userp) {
fprintf(stderr, "User %s not found (and is not a numeric uid)\n", opts->forceuser);
return 13;
}
ctx->targetuid = userp->pw_uid;
}
ctx->targetgid = -1;
sscanf(opts->forcegroups, "%u", &ctx->targetgid);
if (ctx->targetgid == -1) {
char first_group_name_without_comma[256];
snprintf(first_group_name_without_comma, sizeof first_group_name_without_comma, "%s", opts->forcegroups);
if (strchr(first_group_name_without_comma, ',')) {
*strchr(first_group_name_without_comma, ',') = 0;
}
struct group *groupp = getgrnam(first_group_name_without_comma);
if (!groupp) {
fprintf(stderr, "Group %s not found (and is not a numeric gid)\n", first_group_name_without_comma);
return 13;
}
ctx->targetgid = groupp->gr_gid;
}
}
} else {
/* By default it is user at the other end of the connection */
pw = ctx->client_cred;
}
ctx->effective_user = ctx->targetuid;
ctx->effective_group = ctx->targetgid;
if (opts->effective_user) {
struct passwd *pw_e;
pw_e = getpwnam(opts->effective_user);
if (pw_e) {
ctx->effective_user = pw_e->pw_uid;
ctx->effective_group = pw_e->pw_gid;
} else {
ctx->effective_user=-1;
sscanf(opts->effective_user, "%d", &ctx->effective_user);
}
}
if(pw) {
ctx->username = pw->pw_name;
}
return 0;
}
int serve_client_cap_bset(struct dived_options *opts, struct serve_client_context *ctx) {
#ifndef NO_CAPABILITIES
if (opts->remove_capabilities || opts->retain_capabilities) {
int also_remove_CAP_SETPCAP = 0;
if (opts->remove_capabilities ) {
char* q = strtok(opts->remove_capabilities, ",");
for(; q; q=strtok(NULL, ",")) {
cap_value_t c=-1;
cap_from_name(q, &c);
if(c==-1) {
perror("cap_from_name");
return -1;
}
if (c == CAP_SETPCAP) {
also_remove_CAP_SETPCAP = 1;
continue;
}
if(prctl(PR_CAPBSET_DROP, c, 0, 0, 0)==-1) {
perror("prctl(PR_CAPBSET_DROP)");
return -1;
}
}
} else {
// retain capabilities
#define MAXCAPS 256
unsigned char retain_set[MAXCAPS] = {0};
char* q = strtok(opts->retain_capabilities, ",");
for(; q; q=strtok(NULL, ",")) {
cap_value_t c=-1;
cap_from_name(q, &c);
if(c==-1) {
perror("cap_from_name");
return -1;
}
if (c<MAXCAPS) {
retain_set[c] = 1;
}
}
cap_value_t i;
for(i=0; i<MAXCAPS; ++i) {
if(!retain_set[i]) {
if(i!=CAP_SETPCAP) {
if(prctl(PR_CAPBSET_DROP, i, 0, 0, 0)==-1) {
if(errno==EINVAL) {
continue;
}
perror("prctl(PR_CAPBSET_DROP)");
return -1;
}
} else {
also_remove_CAP_SETPCAP = 1;
}
}
}
}
if (also_remove_CAP_SETPCAP) {
if(prctl(PR_CAPBSET_DROP, CAP_SETPCAP, 0, 0, 0)==-1) {
perror("prctl(PR_CAPBSET_DROP)");
return -1;
}
}
}
#endif
return 0;
}
int serve_client_groups(struct dived_options *opts, struct serve_client_context *ctx) {
if (!opts->forcegroups) {
if (ctx->targetuid != getuid() || ctx->targetgid != getgid()) {
if (initgroups(ctx->username, ctx->targetgid) == -1) {
perror("initgroups");
return -1;
}
}
} else {
gid_t groups[MAXGROUPS];
size_t group_count = 0;
char *saveptr_commas;
char* q = strtok_r(opts->forcegroups, ",", &saveptr_commas);
for(; q && group_count < MAXGROUPS; q=strtok_r(NULL, ",", &saveptr_commas)) {
unsigned int gid = -1;
sscanf(q, "%u", &gid);
if (gid == -1) {
struct group *groupp = getgrnam(q);
if (!groupp) {
fprintf(stderr, "Group %s not found (and is not a numeric uid)\n", q);
return 13;
}
gid = groupp->gr_gid;
}
groups[group_count] = gid;
++group_count;
}
if (setgroups(group_count, groups) == -1) {
perror("setgroups");
return -1;
}
}
return 0;
}
int serve_client_setuid(struct dived_options *opts, struct serve_client_context *ctx) {
if (!opts->effective_user) {
if(setgid(ctx->targetgid) == -1) { perror("setgid"); return -1; }
if(setuid(ctx->targetuid) == -1) { perror("setuid"); return -1; }
} else {
if(setregid(ctx->targetgid, ctx->effective_group) == -1) { perror("setregid"); return -1; }
if(setreuid(ctx->targetuid, ctx->effective_user) == -1) { perror("setregid"); return -1; }
}
return 0;
}
int serve_client_setcap(struct dived_options *opts, struct serve_client_context *ctx) {
#ifndef NO_CAPABILITIES
if (opts->set_capabilities) {
cap_t c = cap_from_text(opts->set_capabilities);
if (c==NULL) {
perror("cap_from_text");
return -1;
}
if (cap_set_proc(c) == -1) {
perror("caps_set_proc");
return -1;
}
cap_free(c);
}
#endif
return 0;
}
int serve_client_ambient_caps(struct dived_options *opts, struct serve_client_context *ctx) {
#ifndef NO_CAPABILITIES
if (opts->ambient_capabilities) {
char* q = strtok(opts->ambient_capabilities, ",");
for(; q; q=strtok(NULL, ",")) {
cap_value_t c=-1;
cap_from_name(q, &c);
if(c==-1) {
perror("cap_from_name");
return -1;
}
#ifndef PR_CAP_AMBIENT
#define PR_CAP_AMBIENT 47
#define PR_CAP_AMBIENT_RAISE 2
#endif
if(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, c, 0, 0)==-1) {
perror("prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE)");
return -1;
}
}
}
#endif
return 0;
}
int serve_client_no_new_privs(struct dived_options *opts, struct serve_client_context *ctx) {
if (opts->no_new_privs) {
#ifndef PR_SET_NO_NEW_PRIVS
#define PR_SET_NO_NEW_PRIVS 38
#endif
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
perror("prctl(PR_SET_NO_NEW_PRIVS)");
return -1;
}
}
return 0;
}
int serve_client_initfinevent(struct dived_options *opts, struct serve_client_context *ctx) {
int ret;
ret = pipe2(ctx->initialisation_finished_event, O_CLOEXEC);
if (ret == -1) {
ctx->initialisation_finished_event[0]=-1;
ctx->initialisation_finished_event[1]=-1;
}
return 0;
}
int serve_client_signalsetup(struct dived_options *opts, struct serve_client_context *ctx) {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
#ifndef SIGNALFD_WORKAROUND
ctx->signal_fd = signalfd(-1, &mask, 0/*SFD_NONBLOCK*/);
#endif
sigprocmask(SIG_BLOCK, &mask, NULL);
return 0;
}
int serve_client_maybe_fork(struct dived_options *opts, struct serve_client_context *ctx) {
if (ctx->exit_code_or_signal_processing_enabled) {
ctx->pid2 = fork();
if (ctx->pid2 == -1) {
perror("fork");
return -1;
}
}
if (!ctx->pid2) {
if (ctx->signal_fd != -1) close(ctx->signal_fd);
}
return 0;
}
int serve_client_recv_argv(struct dived_options *opts, struct serve_client_context *ctx) {
/* Receive argv */
int numargs, totallen;
int i, u;
int forced_argv_count = opts->forced_argv_count;
safer_read(ctx->fd, (char*)&numargs, sizeof(numargs));
safer_read(ctx->fd, (char*)&totallen, sizeof(totallen));
if (numargs < 0 || numargs > opts->maximum_argv_count ||
totallen < 0 || totallen > opts->maximum_argv_size) {
fprintf(stderr, "dived: Exceed maximum argv count or size\n");
return -1;
}
char* args=(char*)malloc(totallen);
if (!args) {
perror("malloc");
return -1;
}
safer_read(ctx->fd, args, totallen);
if(!opts->client_argv) { numargs=0; totallen=0; }
ctx->argv = (char**) malloc((numargs+forced_argv_count+1)*sizeof(char*));
if (!ctx->argv) {
perror("malloc");
return -1;
}
for(u=0; u<forced_argv_count; ++u) {
ctx->argv[u] = opts->forced_argv[u];
}
u=forced_argv_count; /* explicit > implicit */
ctx->argv[u]=args;
for(i=0; i<totallen; ++i) {
if (!args[i]) {
++u;
if ( u-forced_argv_count > numargs) {
fprintf(stderr, "dived: Malformed argv\n");
return -1;
}
ctx->argv[u]=args+i+1;
}
}
ctx->argv[forced_argv_count + numargs]=NULL;
return 0;
}
int serve_client_recv_evnvars(struct dived_options *opts, struct serve_client_context *ctx) {
/* Receive environment */
int numargs, totallen;
int i, u;
safer_read(ctx->fd, (char*)&numargs, sizeof(numargs));
safer_read(ctx->fd, (char*)&totallen, sizeof(totallen));
if (numargs < 0 || numargs > opts->maximum_envp_count ||
totallen < 0 || totallen > opts->maximum_envp_size) {
fprintf(stderr, "dived: Exceed maximum environment count or size\n");
return -1;
}
char* env=(char*)malloc(totallen);
if (!env) {
perror("malloc");
return -1;
}
safer_read(ctx->fd, env, totallen);
if (opts->client_environment) {
ctx->envp_ = (char**) malloc((numargs+4+1)*sizeof(char*));
if (!ctx->envp_) {
perror("malloc");
return -1;
}
int was_zero = 1;
u=0;
for(i=0; i<totallen; ++i) {
if (was_zero) {
was_zero = 0;
if(!strncmp(env+i, "DIVE_", 5)) continue;
ctx->envp_[u]=env+i;
++u;
if(u>=numargs) break;
}
if (!env[i]) {
was_zero = 1;
}
}
char *buffer_uid = (char*)malloc(64);
char *buffer_gid = (char*)malloc(64);
char *buffer_pid = (char*)malloc(64);
char *buffer_user = (char*)malloc(1024);
if (!buffer_uid || !buffer_gid || !buffer_pid || !buffer_user) {
perror("malloc");
// if some buffers are actually allocated, the memory is obviously freed by itself
return -1;
}
snprintf(buffer_uid, 64, "DIVE_UID=%d", ctx->cred.uid);
snprintf(buffer_gid, 64, "DIVE_GID=%d", ctx->cred.gid);
snprintf(buffer_pid, 64, "DIVE_PID=%d", ctx->cred.pid);
if (ctx->client_cred) {
snprintf(buffer_user, 1024, "DIVE_USER=%s", ctx->client_cred->pw_name);
} else {
snprintf(buffer_user, 1024, "DIVE_USER=");
}
ctx->envp_[u+0]=buffer_uid;
ctx->envp_[u+1]=buffer_gid;
ctx->envp_[u+2]=buffer_pid;
ctx->envp_[u+3]=buffer_user;
ctx->envp_[u+4]=NULL;
} else {
ctx->envp_ = environ;
}
return 0;
}
int serve_client_exec(struct dived_options *opts, struct serve_client_context *ctx) {
close(ctx->fd);
if (ctx->debt_instead_of_fd != -1) {
dup2(ctx->debt_instead_of_fd, ctx->fd);
close(ctx->debt_instead_of_fd);
}
close (ctx->initialisation_finished_event[1]);
#ifndef NO_EXECVPE
execvpe(ctx->argv[0], ctx->argv, ctx->envp_);
#else
execve(ctx->argv[0], ctx->argv, ctx->envp_);
#endif
exit(127);
return 127;
}
int serve_client_release_fds(struct dived_options *opts, struct serve_client_context *ctx) {
/* Release client's fds */
int i;
for(i=0; i<MAXFD; ++i) {
if(saved_fdnums[i] && i!=ctx->initialisation_finished_event[0]) close(i);
}
if (ctx->debt_instead_of_fd != -1) {
close(ctx->debt_instead_of_fd);
}
close (ctx->initialisation_finished_event[1]);
return 0;
}
int serve_client_wait_init_event(struct dived_options *opts, struct serve_client_context *ctx) {