summaryrefslogtreecommitdiffstats
path: root/src/exec_intercept.c
blob: 1846124499fd390044f1cbfe2b8587876a86acc6 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2021-2022 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#if defined(HAVE_STDINT_H)
# include <stdint.h>
#elif defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include "sudo.h"
#include "sudo_exec.h"
#include "sudo_plugin.h"
#include "sudo_plugin_int.h"
#include "sudo_rand.h"
#include "intercept.pb-c.h"
#include "exec_intercept.h"

#ifdef _PATH_SUDO_INTERCEPT
static union sudo_token_un intercept_token;
static in_port_t intercept_listen_port;
static struct intercept_closure *accept_closure;
static void intercept_accept_cb(int fd, int what, void *v);
static void intercept_cb(int fd, int what, void *v);

/*
 * Enable the closure->ev event with the specified events and callback,
 * and set the connection state to new_state if it is valid.
 * Returns true on success, else false.
 */
static bool
intercept_enable_event(int fd, short events, enum intercept_state new_state,
    sudo_ev_callback_t callback, struct intercept_closure *closure)
{
    int rc;
    debug_decl(intercept_enable_event, SUDO_DEBUG_EXEC);

    rc = sudo_ev_set(&closure->ev, fd, events, callback, closure);
    if (rc == -1 || sudo_ev_add(NULL, &closure->ev, NULL, false) == -1) {
	sudo_warn("%s", U_("unable to add event to queue"));
	debug_return_bool(false);
    }
    if (new_state != INVALID_STATE)
	closure->state = new_state;
    debug_return_bool(true);
}

static bool
enable_read_event(int fd, enum intercept_state new_state,
    sudo_ev_callback_t callback, struct intercept_closure *closure)
{
    return intercept_enable_event(fd, SUDO_EV_READ|SUDO_EV_PERSIST,
	new_state, callback, closure);
}

static bool
enable_write_event(int fd, sudo_ev_callback_t callback,
    struct intercept_closure *closure)
{
    return intercept_enable_event(fd, SUDO_EV_WRITE|SUDO_EV_PERSIST,
	INVALID_STATE, callback, closure);
}

/*
 * Create an intercept closure.
 * Returns an opaque pointer to the closure, which is also
 * passed to the event callback when not using ptrace(2).
 */
void *
intercept_setup(int fd, struct sudo_event_base *evbase,
    struct command_details *details)
{
    struct intercept_closure *closure;
    debug_decl(intercept_setup, SUDO_DEBUG_EXEC);

    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
	"intercept fd %d\n", fd);

    closure = calloc(1, sizeof(*closure));
    if (closure == NULL) {
	sudo_warnx("%s", U_("unable to allocate memory"));
	goto bad;
    }
    closure->details = details;
    closure->listen_sock = -1;
    sudo_ev_set_base(&closure->ev, evbase);

    if (ISSET(details->flags, CD_USE_PTRACE)) {
	/*
	 * We can perform a policy check immediately using ptrace(2)
	 * but should ignore the execve(2) of the initial command
	 * (and sesh for SELinux RBAC).
	 */
	closure->state = RECV_POLICY_CHECK;
	closure->initial_command = 1;
	if (ISSET(details->flags, CD_RBAC_ENABLED))
	    closure->initial_command++;
    } else {
	/*
	 * Not using ptrace(2), use LD_PRELOAD (or its equivalent).  If
	 * we've already seen an InterceptHello, expect a policy check first.
	 */
	const int new_state = sudo_token_isset(intercept_token) ?
	    RECV_SECRET : RECV_HELLO_INITIAL;
	if (!enable_read_event(fd, new_state, intercept_cb, closure))
	    goto bad;
    }

    debug_return_ptr(closure);

bad:
    free(closure);
    debug_return_ptr(NULL);
}

/*
 * Reset intercept_closure so it can be re-used.
 */
void
intercept_closure_reset(struct intercept_closure *closure)
{
    size_t n;
    debug_decl(intercept_closure_reset, SUDO_DEBUG_EXEC);

    if (closure->listen_sock != -1) {
	close(closure->listen_sock);
	closure->listen_sock = -1;
    }
    free(closure->buf);
    free(closure->command);
    if (closure->run_argv != NULL) {
	for (n = 0; closure->run_argv[n] != NULL; n++)
	    free(closure->run_argv[n]);
	free(closure->run_argv);
    }
    if (closure->run_envp != NULL) {
	for (n = 0; closure->run_envp[n] != NULL; n++)
	    free(closure->run_envp[n]);
	free(closure->run_envp);
    }
    closure->errstr = NULL;
    closure->command = NULL;
    closure->run_argv = NULL;
    closure->run_envp = NULL;
    closure->buf = NULL;
    closure->len = 0;
    closure->off = 0;
    /* Does not currently reset token. */

    debug_return;
}

/*
 * Close intercept socket and free closure when we are done with
 * the connection.
 */
static void
intercept_connection_close(struct intercept_closure *closure)
{
    const int fd = sudo_ev_get_fd(&closure->ev);
    debug_decl(intercept_connection_close, SUDO_DEBUG_EXEC);

    sudo_ev_del(NULL, &closure->ev);
    close(fd);
    intercept_closure_reset(closure);
    free(closure);

    debug_return;
}

void
intercept_cleanup(void)
{
    debug_decl(intercept_cleanup, SUDO_DEBUG_EXEC);

    if (accept_closure != NULL) {
	intercept_connection_close(accept_closure);
	accept_closure = NULL;
    }

    debug_return;
}

/*
 * Prepare to listen on localhost using an ephemeral port.
 * Sets intercept_token and intercept_listen_port as side effects.
 */
static bool
prepare_listener(struct intercept_closure *closure)
{
    struct sockaddr_in sin4;
    socklen_t sin4_len = sizeof(sin4);
    int sock;
    debug_decl(prepare_listener, SUDO_DEBUG_EXEC);

    /* Generate a random token. */
    do {
	arc4random_buf(&intercept_token, sizeof(intercept_token));
    } while (!sudo_token_isset(intercept_token));

    /* Create localhost listener socket (currently AF_INET only). */
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
	sudo_warn("socket");
	goto bad;
    }
    memset(&sin4, 0, sizeof(sin4));
    sin4.sin_family = AF_INET;
    sin4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    sin4.sin_port = 0;
    if (bind(sock, (struct sockaddr *)&sin4, sizeof(sin4)) == -1) {
	sudo_warn("bind");
	goto bad;
    }
    if (getsockname(sock, (struct sockaddr *)&sin4, &sin4_len) == -1) {
	sudo_warn("getsockname");
	goto bad;
    }
    if (listen(sock, SOMAXCONN) == -1) {
	sudo_warn("listen");
	goto bad;
    }

    closure->listen_sock = sock;
    intercept_listen_port = ntohs(sin4.sin_port);
    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
	"%s: listening on port %hu", __func__, intercept_listen_port);

    debug_return_bool(true);

bad:
    if (sock != -1)
	close(sock);
    debug_return_bool(false);
}

/*
 * Allocate a new command_info[] and update command and runcwd in it.
 * Fills in cmnd_out with a copy of the command if not NULL.
 * Returns the new command_info[] which the caller must free.
 */
static char **
update_command_info(char * const *old_command_info, const char *cmnd,
    const char *runcwd, char **cmnd_out, struct intercept_closure *closure)
{
    char **command_info;
    char * const *oci;
    size_t n;
    debug_decl(update_command_info, SUDO_DEBUG_EXEC);

    /* Rebuild command_info[] with new command and add a runcwd. */
    for (n = 0; old_command_info[n] != NULL; n++)
	continue;
    command_info = reallocarray(NULL, n + 3, sizeof(char *));
    if (command_info == NULL) {
	goto bad;
    }
    for (oci = old_command_info, n = 0; *oci != NULL; oci++) {
	const char *cp = *oci;
	switch (*cp) {
	case 'c':
	    if (strncmp(cp, "command=", sizeof("command=") - 1) == 0) {
		if (cmnd == NULL) {
		    /* No new command specified, use old value. */
		    cmnd = cp + sizeof("command=") - 1;
		}
		/* Filled in at the end. */
		continue;
	    }
	    break;
	case 'r':
	    if (strncmp(cp, "runcwd=", sizeof("runcwd=") - 1) == 0) {
		/* Filled in at the end. */
		continue;
	    }
	    break;
	}
	command_info[n] = strdup(cp);
	if (command_info[n] == NULL) {
	    goto bad;
	}
	n++;
    }

    /* Append new command. */
    if (cmnd == NULL) {
	closure->errstr = N_("command not set by the security policy");
	goto bad;
    }
    command_info[n] = sudo_new_key_val("command", cmnd);
    if (command_info[n] == NULL) {
	goto oom;
    }
    n++;

    /* Append actual runcwd. */
    command_info[n] = sudo_new_key_val("runcwd", runcwd ? runcwd : "unknown");
    if (command_info[n] == NULL) {
	goto oom;
    }
    n++;

    command_info[n] = NULL;

    if (cmnd_out != NULL) {
	*cmnd_out = strdup(cmnd);
	if (*cmnd_out == NULL) {
	    goto oom;
	}
    }
    debug_return_ptr(command_info);

oom:
    closure->errstr = N_("unable to allocate memory");

bad:
    if (command_info != NULL) {
	for (n = 0; command_info[n] != NULL; n++) {
	    free(command_info[n]);
	}
	free(command_info);
    }
    debug_return_ptr(NULL);
}

/*
 * Perform a policy check for the given command.
 * While argv must be NULL-terminated, envp need not be.
 * Sets closure->state to the result of the policy check before returning.
 * Return false on error, else true.
 */
bool
intercept_check_policy(const char *command, int argc, char **argv, int envc,
    char **envp, const char *runcwd, int *oldcwd, void *v)
{
    struct intercept_closure *closure = v;
    char **command_info = NULL;
    char **command_info_copy = NULL;
    char **user_env_out = NULL;
    char **run_argv = NULL;
    int i, rc, saved_dir = -1;
    bool ret = true;
    struct stat sb;
    debug_decl(intercept_check_policy, SUDO_DEBUG_EXEC);

    /* Change to runcwd before the policy check if necessary. */
    if (*command != '/') {
	if (runcwd == NULL || (saved_dir = open(".", O_RDONLY)) == -1 ||
		chdir(runcwd) == -1) {
	    if (runcwd == NULL) {
		sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
		    "relative command path but no runcwd specified");
	    } else if (saved_dir == -1) {
		sudo_debug_printf(
		    SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
		    "unable to open current directory for reading");
	    } else {
		sudo_debug_printf(
		    SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
		    "unable to chdir for %s", runcwd);
	    }
	    if (ISSET(closure->details->flags, CD_INTERCEPT)) {
		/* Inability to change cwd is fatal in intercept mode. */
		if (closure->errstr == NULL)
		    closure->errstr = N_("command rejected by policy");
		audit_reject(policy_plugin.name, SUDO_POLICY_PLUGIN,
		    closure->errstr, closure->details->info);
		closure->state = POLICY_REJECT;
		goto done;
	    }
	}
    }

    /*
     * Short-circuit the policy check if the command doesn't exist.
     * Otherwise, both sudo and the shell will report the error.
     */
    if (stat(command, &sb) == -1) {
	closure->errstr = NULL;
	closure->state = POLICY_ERROR;
	goto done;
    }

    if (ISSET(closure->details->flags, CD_INTERCEPT)) {
	/* We don't currently have a good way to validate the environment. */
	sudo_debug_set_active_instance(policy_plugin.debug_instance);
	rc = policy_plugin.u.policy->check_policy(argc, argv, NULL,
	    &command_info, &run_argv, &user_env_out, &closure->errstr);
	sudo_debug_set_active_instance(sudo_debug_instance);
	sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
	    "check_policy returns %d", rc);

	switch (rc) {
	case 1:
	    /* Rebuild command_info[] with runcwd and extract command. */
	    command_info_copy = update_command_info(command_info, NULL,
		runcwd, &closure->command, closure);
	    if (command_info_copy == NULL)
		goto oom;
	    command_info = command_info_copy;
	    closure->state = POLICY_ACCEPT;
	    break;
	case 0:
	    if (closure->errstr == NULL)
		closure->errstr = N_("command rejected by policy");
	    audit_reject(policy_plugin.name, SUDO_POLICY_PLUGIN,
		closure->errstr, command_info);
	    closure->state = POLICY_REJECT;
	    goto done;
	default:
	    /* Plugin error? */
	    goto bad;
	}
    } else {
	/* No actual policy check, just logging child processes. */
	sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
	    "not checking policy, audit only");
	closure->command = strdup(command);
	if (closure->command == NULL)
	    goto oom;

	/* Rebuild command_info[] with new command and runcwd. */
	command_info = update_command_info(closure->details->info,
	    command, runcwd, NULL, closure);
	if (command_info == NULL)
	    goto oom;
	closure->state = POLICY_ACCEPT;
	run_argv = argv;
    }

    if (sudo_debug_needed(SUDO_DEBUG_INFO)) {
	sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
	    "run_command: %s", closure->command);
	for (i = 0; command_info[i] != NULL; i++) {
	    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
		"command_info[%d]: %s", i, command_info[i]);
	}
	for (i = 0; run_argv[i] != NULL; i++) {
	    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
		"run_argv[%d]: %s", i, run_argv[i]);
	}
    }

    /* Make a copy of run_argv, it may share contents of argv. */
    for (i = 0; run_argv[i] != NULL; i++)
	continue;
    closure->run_argv = reallocarray(NULL, i + 1, sizeof(char *));
    if (closure->run_argv == NULL)
	goto oom;
    for (i = 0; run_argv[i] != NULL; i++) {
	closure->run_argv[i] = strdup(run_argv[i]);
	if (closure->run_argv[i] == NULL)
	    goto oom;
    }
    closure->run_argv[i] = NULL;

    /* Make a copy of envp, which may not be NULL-terminated. */
    closure->run_envp = reallocarray(NULL, envc + 1, sizeof(char *));
    if (closure->run_envp == NULL)
	goto oom;
    for (i = 0; i < envc; i++) {
	closure->run_envp[i] = strdup(envp[i]);
	if (closure->run_envp[i] == NULL)
	    goto oom;
    }
    closure->run_envp[i] = NULL;

    if (ISSET(closure->details->flags, CD_INTERCEPT)) {
	audit_accept(policy_plugin.name, SUDO_POLICY_PLUGIN, command_info,
		closure->run_argv, closure->run_envp);

	/* Call approval plugins and audit the result. */
	if (!approval_check(command_info, closure->run_argv, closure->run_envp)) {
	    if (closure->errstr == NULL)
		closure->errstr = N_("approval plugin error");
	    closure->state = POLICY_REJECT;
	    goto done;
	}
    }

    /* Audit the event again for the sudo front-end. */
    audit_accept("sudo", SUDO_FRONT_END, command_info, closure->run_argv,
	closure->run_envp);

    goto done;

oom:
    closure->errstr = N_("unable to allocate memory");

bad:
    if (saved_dir != -1) {
	if (fchdir(saved_dir) == -1) {
	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		"%s: unable to restore saved cwd", __func__);
	}
	close(saved_dir);
	saved_dir = -1;
    }
    if (closure->errstr == NULL)
	closure->errstr = N_("policy plugin error");
    audit_error(policy_plugin.name, SUDO_POLICY_PLUGIN, closure->errstr,
	command_info ? command_info : closure->details->info);
    closure->state = POLICY_ERROR;
    ret = false;

done:
    if (command_info_copy != NULL) {
	for (i = 0; command_info_copy[i] != NULL; i++) {
	    free(command_info_copy[i]);
	}
	free(command_info_copy);
    }
    *oldcwd = saved_dir;

    debug_return_bool(ret);
}

static bool
intercept_check_policy_req(PolicyCheckRequest *req,
    struct intercept_closure *closure)
{
    char **argv = NULL;
    bool ret = false;
    int oldcwd = -1;
    size_t n;
    debug_decl(intercept_check_policy_req, SUDO_DEBUG_EXEC);

    if (req->command == NULL) {
	closure->errstr = N_("invalid PolicyCheckRequest");
	goto done;
    }

    if (sudo_debug_needed(SUDO_DEBUG_INFO)) {
	sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
	    "req_command: %s", req->command);
	for (n = 0; n < req->n_argv; n++) {
	    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
		"req_argv[%zu]: %s", n, req->argv[n]);
	}
    }

    /* If argv is empty, reserve an extra slot for the command. */
    if (req->n_argv == 0)
	req->n_argv = 1;

    /*
     * Rebuild argv from PolicyCheckReq so it is NULL-terminated.
     * The plugin API requires us to pass the pathname to exec in argv[0].
     */
    argv = reallocarray(NULL, req->n_argv + 1, sizeof(char *));
    if (argv == NULL) {
	closure->errstr = N_("unable to allocate memory");
	goto done;
    }
    argv[0] = req->command;
    for (n = 1; n < req->n_argv; n++) {
	argv[n] = req->argv[n];
    }
    argv[n] = NULL;

    ret = intercept_check_policy(req->command, req->n_argv, argv, req->n_envp,
	req->envp, req->cwd, &oldcwd, closure);

done:
    if (oldcwd != -1) {
	if (fchdir(oldcwd) == -1) {
	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
		"%s: unable to restore saved cwd", __func__);
	}
	close(oldcwd);
    }

    free(argv);

    debug_return_bool(ret);
}

/*
 * Read token from sudo_intercept.so and verify w/ intercept_token.
 * Returns true on success, false on mismatch and -1 on error.
 */
static int
intercept_verify_token(int fd, struct intercept_closure *closure)
{
    ssize_t nread;
    debug_decl(intercept_verify_token, SUDO_DEBUG_EXEC);

    nread = recv(fd, closure->token.u8 + closure->off,
	sizeof(closure->token) - closure->off, 0);
    switch (nread) {
    case 0:
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "EOF reading token");
	debug_return_int(false);
    case -1:
	debug_return_int(-1);
    default:
	if (nread + closure->off == sizeof(closure->token))
	    break;
	/* partial read, update offset and try again */
	closure->off += nread;
	errno = EAGAIN;
	debug_return_int(-1);
    }

    closure->off = 0;
    if (memcmp(&closure->token, &intercept_token, sizeof(closure->token)) != 0) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "token mismatch: got 0x%8x%8x%8x%8x, expected 0x%8x%8x%8x%8x",
	    closure->token.u32[3], closure->token.u32[2],
	    closure->token.u32[1], closure->token.u32[0],
	    intercept_token.u32[3], intercept_token.u32[2],
	    intercept_token.u32[1], intercept_token.u32[0]);
	debug_return_int(false);
    }
    debug_return_int(true);
}

/*
 * Read a message from sudo_intercept.so and act on it.
 */
static bool
intercept_read(int fd, struct intercept_closure *closure)
{
    InterceptRequest *req = NULL;
    bool ret = false;
    ssize_t nread;
    debug_decl(intercept_read, SUDO_DEBUG_EXEC);

    if (closure->state == RECV_SECRET) {
	switch (intercept_verify_token(fd, closure)) {
	case true:
	    closure->state = RECV_POLICY_CHECK;
	    break;
	case false:
	    goto done;
	default:
	    if (errno == EINTR || errno == EAGAIN) {
		sudo_debug_printf(
		    SUDO_DEBUG_WARN|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
		    "reading intercept token");
		debug_return_bool(true);
	    }
	    sudo_warn("recv");
	    goto done;
	}
    }

    if (closure->len == 0) {
	uint32_t req_len;

	/* Read message size (uint32_t in host byte order). */
	nread = recv(fd, &req_len, sizeof(req_len), 0);
	if (nread != sizeof(req_len)) {
	    if (nread == -1) {
		if (errno == EINTR || errno == EAGAIN) {
		    sudo_debug_printf(
			SUDO_DEBUG_WARN|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
			"reading intercept message size");
		    debug_return_bool(true);
		}
		sudo_warn("recv");
	    }
	    goto done;
	}

	if (req_len == 0) {
	    /* zero-length message is possible */
	    goto unpack;
	}
	if (req_len > MESSAGE_SIZE_MAX) {
	    sudo_warnx(U_("client request too large: %zu"), (size_t)req_len);
	    goto done;
	}
	if ((closure->buf = malloc(req_len)) == NULL) {
	    sudo_warnx("%s", U_("unable to allocate memory"));
	    goto done;
	}
	closure->len = req_len;
	sudo_debug_printf(SUDO_DEBUG_INFO, "%s: expecting %u bytes from client",
	    __func__, closure->len);
    }

    nread = recv(fd, closure->buf + closure->off, closure->len - closure->off,
	0);
    switch (nread) {
    case 0:
	/* EOF, other side must have exited. */
	goto done;
    case -1:
	if (errno == EINTR || errno == EAGAIN) {
	    sudo_debug_printf(
		SUDO_DEBUG_WARN|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
		"reading intercept message");
	    debug_return_bool(true);
	}
	sudo_warn("recv");
	goto done;
    default:
	closure->off += nread;
	break;
    }
    sudo_debug_printf(SUDO_DEBUG_INFO, "%s: received %zd bytes from client",
	__func__, nread);

    if (closure->off != closure->len) {
	/* Partial read. */
	debug_return_bool(true);
    }

unpack:
    req = intercept_request__unpack(NULL, closure->len, closure->buf);
    if (req == NULL) {
	sudo_warnx(U_("unable to unpack %s size %zu"), "InterceptRequest",
	    (size_t)closure->len);
	goto done;
    }

    sudo_debug_printf(SUDO_DEBUG_INFO,
	"%s: finished receiving %u bytes from client", __func__, closure->len);
    sudo_ev_del(NULL, &closure->ev);
    free(closure->buf);
    closure->buf = NULL;
    closure->len = 0;
    closure->off = 0;

    switch (req->type_case) {
    case INTERCEPT_REQUEST__TYPE_POLICY_CHECK_REQ:
	if (closure->state != RECV_POLICY_CHECK) {
	    /* Only a single policy check request is allowed. */
	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
		"state mismatch, expected RECV_POLICY_CHECK (%d), got %d",
		RECV_POLICY_CHECK, closure->state);
	    goto done;
	}

	ret = intercept_check_policy_req(req->u.policy_check_req, closure);
	if (!ret)
	    goto done;
	if (!ISSET(closure->details->flags, CD_INTERCEPT)) {
	    /* Just logging, re-use event to read next InterceptHello. */
	    ret = enable_read_event(fd, RECV_HELLO, intercept_cb, closure);
	    goto done;
	}
	break;
    case INTERCEPT_REQUEST__TYPE_HELLO:
	switch (closure->state) {
	case RECV_HELLO_INITIAL:
	    if (!prepare_listener(closure))
		goto done;
	    break;
	case RECV_HELLO:
	    break;
	default:
	    /* Only accept hello on a socket with an accepted command. */
	    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
		"got InterceptHello without an accepted command");
	    goto done;
	}
	break;
    default:
	sudo_warnx(U_("unexpected type_case value %d in %s from %s"),
	    req->type_case, "InterceptRequest", "sudo_intercept.so");
	goto done;
    }

    /* Switch event to write mode for the reply. */
    if (!enable_write_event(fd, intercept_cb, closure))
	goto done;

    ret = true;

done:
    intercept_request__free_unpacked(req, NULL);
    debug_return_bool(ret);
}

static bool
fmt_intercept_response(InterceptResponse *resp,
    struct intercept_closure *closure)
{
    uint32_t resp_len;
    bool ret = false;
    debug_decl(fmt_intercept_response, SUDO_DEBUG_EXEC);

    closure->len = intercept_response__get_packed_size(resp);
    if (closure->len > MESSAGE_SIZE_MAX) {
	sudo_warnx(U_("server message too large: %zu"), (size_t)closure->len);
	goto done;
    }

    /* Wire message size is used for length encoding, precedes message. */
    resp_len = closure->len;
    closure->len += sizeof(resp_len);

    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
	"size + InterceptResponse %zu bytes", (size_t)closure->len);

    if ((closure->buf = malloc(closure->len)) == NULL) {
	sudo_warnx("%s", U_("unable to allocate memory"));
	goto done;
    }
    memcpy(closure->buf, &resp_len, sizeof(resp_len));
    intercept_response__pack(resp, closure->buf + sizeof(resp_len));

    ret = true;

done:
    debug_return_bool(ret);
}

static bool
fmt_hello_response(struct intercept_closure *closure)
{
    HelloResponse hello_resp = HELLO_RESPONSE__INIT;
    InterceptResponse resp = INTERCEPT_RESPONSE__INIT;
    debug_decl(fmt_hello_response, SUDO_DEBUG_EXEC);

    hello_resp.portno = intercept_listen_port;
    hello_resp.token_lo = intercept_token.u64[0];
    hello_resp.token_hi = intercept_token.u64[1];
    hello_resp.log_only = !ISSET(closure->details->flags, CD_INTERCEPT);

    resp.u.hello_resp = &hello_resp;
    resp.type_case = INTERCEPT_RESPONSE__TYPE_HELLO_RESP;

    debug_return_bool(fmt_intercept_response(&resp, closure));
}

static bool
fmt_accept_message(struct intercept_closure *closure)
{
    PolicyAcceptMessage msg = POLICY_ACCEPT_MESSAGE__INIT;
    InterceptResponse resp = INTERCEPT_RESPONSE__INIT;
    size_t n;
    debug_decl(fmt_accept_message, SUDO_DEBUG_EXEC);

    msg.run_command = closure->command;
    msg.run_argv = closure->run_argv;
    for (n = 0; closure->run_argv[n] != NULL; n++)
	continue;
    msg.n_run_argv = n;
    msg.run_envp = closure->run_envp;
    for (n = 0; closure->run_envp[n] != NULL; n++)
	continue;
    msg.n_run_envp = n;

    resp.u.accept_msg = &msg;
    resp.type_case = INTERCEPT_RESPONSE__TYPE_ACCEPT_MSG;

    debug_return_bool(fmt_intercept_response(&resp, closure));
}

static bool
fmt_reject_message(struct intercept_closure *closure)
{
    PolicyRejectMessage msg = POLICY_REJECT_MESSAGE__INIT;
    InterceptResponse resp = INTERCEPT_RESPONSE__INIT;
    debug_decl(fmt_reject_message, SUDO_DEBUG_EXEC);

    msg.reject_message = (char *)closure->errstr;

    resp.u.reject_msg = &msg;
    resp.type_case = INTERCEPT_RESPONSE__TYPE_REJECT_MSG;

    debug_return_bool(fmt_intercept_response(&resp, closure));
}

static bool
fmt_error_message(struct intercept_closure *closure)
{
    PolicyErrorMessage msg = POLICY_ERROR_MESSAGE__INIT;
    InterceptResponse resp = INTERCEPT_RESPONSE__INIT;
    debug_decl(fmt_error_message, SUDO_DEBUG_EXEC);

    msg.error_message = (char *)closure->errstr;

    resp.u.error_msg = &msg;
    resp.type_case = INTERCEPT_RESPONSE__TYPE_ERROR_MSG;

    debug_return_bool(fmt_intercept_response(&resp, closure));
}

/*
 * Write a response to sudo_intercept.so.
 */
static bool
intercept_write(int fd, struct intercept_closure *closure)
{
    bool ret = false;
    ssize_t nwritten;
    debug_decl(intercept_write, SUDO_DEBUG_EXEC);

    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO, "state %d",
	closure->state);

    if (closure->len == 0) {
	/* Format new message. */
	switch (closure->state) {
	    case RECV_HELLO_INITIAL:
	    case RECV_HELLO:
		if (!fmt_hello_response(closure))
		    goto done;
		break;
	    case POLICY_ACCEPT:
		if (!fmt_accept_message(closure))
		    goto done;
		break;
	    case POLICY_REJECT:
		if (!fmt_reject_message(closure))
		    goto done;
		break;
	    default:
		if (!fmt_error_message(closure))
		    goto done;
		break;
	}
    }

    sudo_debug_printf(SUDO_DEBUG_INFO, "%s: sending %u bytes to client",
	__func__, closure->len - closure->off);
    nwritten = send(fd, closure->buf + closure->off,
	closure->len - closure->off, 0);
    if (nwritten == -1) {
	if (errno == EINTR || errno == EAGAIN) {
	    sudo_debug_printf(
		SUDO_DEBUG_WARN|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
		"writing intercept message");
	    debug_return_bool(true);
	}
	sudo_warn("send");
	goto done;
    }
    closure->off += nwritten;

    if (closure->off != closure->len) {
	/* Partial write. */
	debug_return_bool(true);
    }

    sudo_debug_printf(SUDO_DEBUG_INFO,
	"%s: sent %u bytes to client", __func__, closure->len);
    sudo_ev_del(NULL, &closure->ev);
    free(closure->buf);
    closure->buf = NULL;
    closure->len = 0;
    closure->off = 0;

    switch (closure->state) {
    case RECV_HELLO_INITIAL:
	/* Re-use the listener event. */
	close(fd);
	if (!enable_read_event(closure->listen_sock, RECV_CONNECTION,
		intercept_accept_cb, closure))
	    goto done;
	closure->listen_sock = -1;
	closure->state = RECV_CONNECTION;
	accept_closure = closure;
	break;
    case POLICY_ACCEPT:
	/* Re-use event to read InterceptHello from sudo_intercept.so ctor. */
	if (!enable_read_event(fd, RECV_HELLO, intercept_cb, closure))
	    goto done;
	break;
    default:
	/* Done with this connection. */
	intercept_connection_close(closure);
    }

    ret = true;

done:
    debug_return_bool(ret);
}

static void
intercept_cb(int fd, int what, void *v)
{
    struct intercept_closure *closure = v;
    bool success = false;
    debug_decl(intercept_cb, SUDO_DEBUG_EXEC);

    switch (what) {
    case SUDO_EV_READ:
	success = intercept_read(fd, closure);
	break;
    case SUDO_EV_WRITE:
	success = intercept_write(fd, closure);
	break;
    default:
	sudo_warnx("%s: unexpected event type %d", __func__, what);
	break;
    }

    if (!success)
	intercept_connection_close(closure);

    debug_return;
}

/*
 * Accept a new connection from the client register a new event for it.
 */
static void
intercept_accept_cb(int fd, int what, void *v)
{
    struct intercept_closure *closure = v;
    struct sudo_event_base *evbase = sudo_ev_get_base(&closure->ev);
    struct sockaddr_in sin4;
    socklen_t sin4_len = sizeof(sin4);
    int client_sock, flags, on = 1;
    debug_decl(intercept_accept_cb, SUDO_DEBUG_EXEC);

    if (closure->state != RECV_CONNECTION) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "state mismatch, expected RECV_CONNECTION (%d), got %d",
	    RECV_CONNECTION, closure->state);
	intercept_connection_close(closure);
	accept_closure = NULL;
	debug_return;
    }

    client_sock = accept(fd, (struct sockaddr *)&sin4, &sin4_len);
    if (client_sock == -1) {
	sudo_warn("accept");
	goto bad;
    }
    flags = fcntl(client_sock, F_GETFL, 0);
    if (flags != -1)
	(void)fcntl(client_sock, F_SETFL, flags | O_NONBLOCK);

    /* Send data immediately, we need low latency IPC. */
    (void)setsockopt(client_sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));

    /*
     * Create a new intercept closure and register an event for client_sock.
     */
    if (intercept_setup(client_sock, evbase, closure->details) == NULL) {
	goto bad;
    }

    debug_return;

bad:
    if (client_sock != -1)
	close(client_sock);
    debug_return;
}
#else /* _PATH_SUDO_INTERCEPT */
void *
intercept_setup(int fd, struct sudo_event_base *evbase,
    struct command_details *details)
{
    debug_decl(intercept_setup, SUDO_DEBUG_EXEC);

    /* Intercept support not compiled in. */

    debug_return_ptr(NULL);
}

void
intercept_cleanup(void)
{
    debug_decl(intercept_cleanup, SUDO_DEBUG_EXEC);

    /* Intercept support not compiled in. */

    debug_return;
}
#endif /* _PATH_SUDO_INTERCEPT */