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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2011-2017 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/stat.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include "sudo_compat.h"
#include "sudo_conf.h"
#include "sudo_debug.h"
#include "sudo_fatal.h"
#include "sudo_gettext.h"
#include "sudo_plugin.h"
#include "sudo_util.h"
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
/*
* The debug priorities and subsystems are currently hard-coded.
* In the future we might consider allowing plugins to register their
* own subsystems and provide direct access to the debugging API.
*/
/* Note: this must match the order in sudo_debug.h */
static const char *const sudo_debug_priorities[] = {
"crit",
"err",
"warn",
"notice",
"diag",
"info",
"trace",
"debug",
NULL
};
/* Note: this must match the order in sudo_debug.h */
static const char *const sudo_debug_default_subsystems[] = {
"args",
"conv",
"edit",
"event",
"exec",
"hooks",
"main",
"netif",
"pcomm",
"plugin",
"pty",
"selinux",
"util",
"utmp",
NULL
};
#define NUM_DEF_SUBSYSTEMS (nitems(sudo_debug_default_subsystems) - 1)
/*
* For multiple programs/plugins there is a per-program instance
* and one or more outputs (files).
*/
struct sudo_debug_output {
SLIST_ENTRY(sudo_debug_output) entries;
char *filename;
int *settings;
int fd;
};
SLIST_HEAD(sudo_debug_output_list, sudo_debug_output);
struct sudo_debug_instance {
char *program;
const char *const *subsystems;
const unsigned int *subsystem_ids;
unsigned int max_subsystem;
unsigned int refcnt;
struct sudo_debug_output_list outputs;
};
/* Support up to 10 instances. */
#define SUDO_DEBUG_INSTANCE_MAX 10
static struct sudo_debug_instance *sudo_debug_instances[SUDO_DEBUG_INSTANCE_MAX];
static int sudo_debug_last_instance = -1;
static char sudo_debug_pidstr[(((sizeof(int) * 8) + 2) / 3) + 3];
static size_t sudo_debug_pidlen;
#define round_nfds(_n) (((_n) + (4 * NBBY) - 1) & ~((4 * NBBY) - 1))
static int sudo_debug_fds_size;
static unsigned char *sudo_debug_fds;
static int sudo_debug_max_fd = -1;
/* Default instance index to use for common utility functions. */
static int sudo_debug_active_instance = -1;
/*
* Free the specified output structure.
*/
static void
sudo_debug_free_output(struct sudo_debug_output *output)
{
free(output->filename);
free(output->settings);
if (output->fd != -1)
close(output->fd);
free(output);
}
/*
* Create a new output file for the specified debug instance.
* Returns NULL if the file cannot be opened or memory cannot be allocated.
* XXX - check for duplicates
*/
static struct sudo_debug_output *
sudo_debug_new_output(struct sudo_debug_instance *instance,
struct sudo_debug_file *debug_file, int minfd)
{
char *buf, *cp, *last, *subsys, *pri;
struct sudo_debug_output *output;
unsigned int j;
int i;
/* Create new output for the instance. */
/* XXX - reuse fd for existing filename? */
output = calloc(1, sizeof(*output));
if (output == NULL)
goto oom;
output->fd = -1;
output->settings = reallocarray(NULL, instance->max_subsystem + 1,
sizeof(int));
if (output->settings == NULL)
goto oom;
output->filename = strdup(debug_file->debug_file);
if (output->filename == NULL)
goto oom;
/* Init per-subsystems settings to -1 since 0 is a valid priority. */
for (j = 0; j <= instance->max_subsystem; j++)
output->settings[j] = -1;
/* Open debug file. */
output->fd = open(output->filename, O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
if (output->fd == -1) {
/* Create debug file as needed and set group ownership. */
if (errno == ENOENT) {
output->fd = open(output->filename, O_WRONLY|O_APPEND|O_CREAT,
S_IRUSR|S_IWUSR);
}
if (output->fd == -1) {
sudo_warn_nodebug("%s", output->filename);
goto bad;
}
ignore_result(fchown(output->fd, (uid_t)-1, 0));
}
if (output->fd < minfd) {
int newfd = fcntl(output->fd, F_DUPFD, minfd);
if (newfd == -1) {
sudo_warn_nodebug("%s", output->filename);
goto bad;
}
close(output->fd);
output->fd = newfd;
}
if (fcntl(output->fd, F_SETFD, FD_CLOEXEC) == -1) {
sudo_warn_nodebug("%s", output->filename);
goto bad;
}
if (sudo_debug_fds_size < output->fd) {
/* Bump fds size to the next multiple of 4 * NBBY. */
const int old_size = sudo_debug_fds_size / NBBY;
const int new_size = round_nfds(output->fd + 1) / NBBY;
unsigned char *new_fds;
new_fds = realloc(sudo_debug_fds, new_size);
if (new_fds == NULL)
goto oom;
memset(new_fds + old_size, 0, new_size - old_size);
sudo_debug_fds = new_fds;
sudo_debug_fds_size = new_size * NBBY;
}
sudo_setbit(sudo_debug_fds, output->fd);
if (output->fd > sudo_debug_max_fd)
sudo_debug_max_fd = output->fd;
/* Parse Debug conf string. */
buf = strdup(debug_file->debug_flags);
if (buf == NULL)
goto oom;
for ((cp = strtok_r(buf, ",", &last)); cp != NULL; (cp = strtok_r(NULL, ",", &last))) {
/* Should be in the form subsys@pri. */
subsys = cp;
if ((pri = strchr(cp, '@')) == NULL)
continue;
*pri++ = '\0';
/* Look up priority and subsystem, fill in sudo_debug_settings[]. */
for (i = 0; sudo_debug_priorities[i] != NULL; i++) {
if (strcasecmp(pri, sudo_debug_priorities[i]) == 0) {
for (j = 0; instance->subsystems[j] != NULL; j++) {
if (strcasecmp(subsys, "all") == 0) {
const unsigned int idx = instance->subsystem_ids ?
SUDO_DEBUG_SUBSYS(instance->subsystem_ids[j]) : j;
if (i > output->settings[idx])
output->settings[idx] = i;
continue;
}
if (strcasecmp(subsys, instance->subsystems[j]) == 0) {
const unsigned int idx = instance->subsystem_ids ?
SUDO_DEBUG_SUBSYS(instance->subsystem_ids[j]) : j;
if (i > output->settings[idx])
output->settings[idx] = i;
break;
}
}
break;
}
}
}
free(buf);
return output;
oom:
// -V:sudo_warn_nodebug:575, 618
sudo_warn_nodebug(NULL);
bad:
if (output != NULL)
sudo_debug_free_output(output);
return NULL;
}
/*
* Register a program/plugin with the debug framework,
* parses settings string from sudo.conf and opens debug_files.
* If subsystem names are specified they override the default values.
* NOTE: subsystems must not be freed by caller unless deregistered.
* Sets the active instance to the newly registered instance.
* Returns instance index on success, SUDO_DEBUG_INSTANCE_INITIALIZER
* if no debug files are specified and SUDO_DEBUG_INSTANCE_ERROR
* on error.
*/
int
sudo_debug_register_v2(const char *program, const char *const subsystems[],
unsigned int ids[], struct sudo_conf_debug_file_list *debug_files,
int minfd)
{
struct sudo_debug_instance *instance = NULL;
struct sudo_debug_output *output;
struct sudo_debug_file *debug_file;
int idx, free_idx = -1;
debug_decl_func(sudo_debug_register);
if (debug_files == NULL)
return SUDO_DEBUG_INSTANCE_INITIALIZER;
/* Use default subsystem names if none are provided. */
if (subsystems == NULL) {
subsystems = sudo_debug_default_subsystems;
} else if (ids == NULL) {
/* If subsystems are specified we must have ids[] too. */
return SUDO_DEBUG_INSTANCE_ERROR;
}
/* Search for existing instance. */
for (idx = 0; idx <= sudo_debug_last_instance; idx++) {
if (sudo_debug_instances[idx] == NULL) {
free_idx = idx;
continue;
}
if (sudo_debug_instances[idx]->subsystems == subsystems &&
strcmp(sudo_debug_instances[idx]->program, program) == 0) {
instance = sudo_debug_instances[idx];
break;
}
}
if (instance == NULL) {
unsigned int i, j, max_id = NUM_DEF_SUBSYSTEMS - 1;
/* Fill in subsystem name -> id mapping as needed. */
if (ids != NULL) {
for (i = 0; subsystems[i] != NULL; i++) {
/* Check default subsystems. */
for (j = 0; j < NUM_DEF_SUBSYSTEMS; j++) {
if (strcmp(subsystems[i], sudo_debug_default_subsystems[j]) == 0)
break;
}
if (j == NUM_DEF_SUBSYSTEMS)
j = ++max_id;
ids[i] = ((j + 1) << 6);
}
}
if (free_idx != -1)
idx = free_idx;
if (idx == SUDO_DEBUG_INSTANCE_MAX) {
/* XXX - realloc? */
sudo_warnx_nodebug("too many debug instances (max %d)", SUDO_DEBUG_INSTANCE_MAX);
return SUDO_DEBUG_INSTANCE_ERROR;
}
if (idx != sudo_debug_last_instance + 1 && idx != free_idx) {
sudo_warnx_nodebug("%s: instance number mismatch: expected %d or %d, got %d", __func__, sudo_debug_last_instance + 1, free_idx, idx);
return SUDO_DEBUG_INSTANCE_ERROR;
}
if ((instance = malloc(sizeof(*instance))) == NULL)
return SUDO_DEBUG_INSTANCE_ERROR;
if ((instance->program = strdup(program)) == NULL) {
free(instance);
return SUDO_DEBUG_INSTANCE_ERROR;
}
instance->subsystems = subsystems;
instance->subsystem_ids = ids;
instance->max_subsystem = max_id;
instance->refcnt = 1;
SLIST_INIT(&instance->outputs);
sudo_debug_instances[idx] = instance;
if (idx != free_idx)
sudo_debug_last_instance++;
} else {
/* Check for matching instance but different ids[]. */
if (ids != NULL && instance->subsystem_ids != ids) {
unsigned int i;
for (i = 0; subsystems[i] != NULL; i++)
ids[i] = instance->subsystem_ids[i];
}
instance->refcnt++;
}
TAILQ_FOREACH(debug_file, debug_files, entries) {
output = sudo_debug_new_output(instance, debug_file, minfd);
if (output != NULL)
SLIST_INSERT_HEAD(&instance->outputs, output, entries);
}
/* Set active instance. */
sudo_debug_active_instance = idx;
/* Stash the pid string so we only have to format it once. */
if (sudo_debug_pidlen == 0) {
(void)snprintf(sudo_debug_pidstr, sizeof(sudo_debug_pidstr), "[%d] ",
(int)getpid());
sudo_debug_pidlen = strlen(sudo_debug_pidstr);
}
return idx;
}
int
sudo_debug_register_v1(const char *program, const char *const subsystems[],
unsigned int ids[], struct sudo_conf_debug_file_list *debug_files)
{
return sudo_debug_register_v2(program, subsystems, ids, debug_files, -1);
}
/*
* De-register the specified instance from the debug subsystem
* and free up any associated data structures.
* Returns the number of remaining references for the instance or -1 on error.
*/
int
sudo_debug_deregister_v1(int idx)
{
struct sudo_debug_instance *instance;
struct sudo_debug_output *output, *next;
debug_decl_func(sudo_debug_deregister);
if (idx < 0 || idx > sudo_debug_last_instance) {
sudo_warnx_nodebug("%s: invalid instance ID %d, max %d",
__func__, idx, sudo_debug_last_instance);
return -1;
}
/* Reset active instance as needed. */
if (sudo_debug_active_instance == idx)
sudo_debug_active_instance = -1;
instance = sudo_debug_instances[idx];
if (instance == NULL)
return -1; /* already deregistered */
if (--instance->refcnt != 0)
return instance->refcnt; /* ref held by other caller */
/* Free up instance data, note that subsystems[] is owned by caller. */
sudo_debug_instances[idx] = NULL;
SLIST_FOREACH_SAFE(output, &instance->outputs, entries, next) {
close(output->fd);
free(output->filename);
free(output->settings);
free(output);
}
free(instance->program);
free(instance);
if (idx == sudo_debug_last_instance)
sudo_debug_last_instance--;
return 0;
}
/*
* Parse the "filename flags,..." debug_flags entry from sudo.conf
* and insert a new sudo_debug_file struct into the list.
* Returns 0 on success, 1 on parse error or -1 on malloc failure.
*/
int
sudo_debug_parse_flags_v1(struct sudo_conf_debug_file_list *debug_files,
const char *entry)
{
struct sudo_debug_file *debug_file;
const char *filename, *flags;
size_t namelen;
/* Only process new-style debug flags: filename flags,... */
filename = entry;
if (*filename != '/' || (flags = strpbrk(filename, " \t")) == NULL)
return 1;
namelen = (size_t)(flags - filename);
while (isblank((unsigned char)*flags))
flags++;
if (*flags != '\0') {
if ((debug_file = calloc(1, sizeof(*debug_file))) == NULL)
goto oom;
if ((debug_file->debug_file = strndup(filename, namelen)) == NULL)
goto oom;
if ((debug_file->debug_flags = strdup(flags)) == NULL)
goto oom;
TAILQ_INSERT_TAIL(debug_files, debug_file, entries);
}
return 0;
oom:
if (debug_file != NULL) {
free(debug_file->debug_file);
free(debug_file->debug_flags);
free(debug_file);
}
return -1;
}
int
sudo_debug_get_instance_v1(const char *program)
{
int idx;
for (idx = 0; idx <= sudo_debug_last_instance; idx++) {
if (sudo_debug_instances[idx] == NULL)
continue;
if (strcmp(sudo_debug_instances[idx]->program, program) == 0)
return idx;
}
return SUDO_DEBUG_INSTANCE_INITIALIZER;
}
pid_t
sudo_debug_fork_v1(void)
{
pid_t pid;
if ((pid = fork()) == 0) {
(void)snprintf(sudo_debug_pidstr, sizeof(sudo_debug_pidstr), "[%d] ",
(int)getpid());
sudo_debug_pidlen = strlen(sudo_debug_pidstr);
}
return pid;
}
void
sudo_debug_enter_v1(const char *func, const char *file, int line,
int subsys)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"-> %s @ %s:%d", func, file, line);
}
void
sudo_debug_exit_v1(const char *func, const char *file, int line,
int subsys)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d", func, file, line);
}
void
sudo_debug_exit_int_v1(const char *func, const char *file, int line,
int subsys, int ret)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %d", func, file, line, ret);
}
void
sudo_debug_exit_long_v1(const char *func, const char *file, int line,
int subsys, long ret)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %ld", func, file, line, ret);
}
void
sudo_debug_exit_id_t_v1(const char *func, const char *file, int line,
int subsys, id_t ret)
{
#if SIZEOF_ID_T == 8
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %lld", func, file, line, (long long)ret);
#else
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %d", func, file, line, (int)ret);
#endif
}
void
sudo_debug_exit_size_t_v1(const char *func, const char *file, int line,
int subsys, size_t ret)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %zu", func, file, line, ret);
}
void
sudo_debug_exit_ssize_t_v1(const char *func, const char *file, int line,
int subsys, ssize_t ret)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %zd", func, file, line, ret);
}
void
sudo_debug_exit_time_t_v1(const char *func, const char *file, int line,
int subsys, time_t ret)
{
#if SIZEOF_TIME_T == 8
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %lld", func, file, line, (long long)ret);
#else
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %d", func, file, line, (int)ret);
#endif
}
void
sudo_debug_exit_bool_v1(const char *func, const char *file, int line,
int subsys, bool ret)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %s", func, file, line, ret ? "true" : "false");
}
void
sudo_debug_exit_str_v1(const char *func, const char *file, int line,
int subsys, const char *ret)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %s", func, file, line, ret ? ret : "(null)");
}
void
sudo_debug_exit_str_masked_v1(const char *func, const char *file, int line,
int subsys, const char *ret)
{
static const char stars[] = "********************************************************************************";
int len = ret ? strlen(ret) : sizeof("(null)") - 1;
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %.*s", func, file, line, len, ret ? stars : "(null)");
}
void
sudo_debug_exit_ptr_v1(const char *func, const char *file, int line,
int subsys, const void *ret)
{
sudo_debug_printf2(NULL, NULL, 0, subsys | SUDO_DEBUG_TRACE,
"<- %s @ %s:%d := %p", func, file, line, ret);
}
void
sudo_debug_write2_v1(int fd, const char *func, const char *file, int lineno,
const char *str, int len, int errnum)
{
char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2];
char timebuf[64];
struct timeval tv;
struct iovec iov[12];
int iovcnt = 3;
/* Cannot use sudo_gettime_real() here since it calls sudo_debug. */
timebuf[0] = '\0';
if (gettimeofday(&tv, NULL) != -1) {
time_t now = tv.tv_sec;
struct tm tm;
size_t tlen;
if (localtime_r(&now, &tm) != NULL) {
timebuf[sizeof(timebuf) - 1] = '\0';
tlen = strftime(timebuf, sizeof(timebuf), "%b %e %H:%M:%S", &tm);
if (tlen == 0 || timebuf[sizeof(timebuf) - 1] != '\0') {
/* contents are undefined on error */
timebuf[0] = '\0';
} else {
(void)snprintf(timebuf + tlen, sizeof(timebuf) - tlen,
".%03d ", (int)tv.tv_usec / 1000);
}
}
}
iov[0].iov_base = timebuf;
iov[0].iov_len = strlen(timebuf);
/* Prepend program name and pid with a trailing space. */
iov[1].iov_base = (char *)getprogname();
iov[1].iov_len = strlen(iov[1].iov_base);
iov[2].iov_base = sudo_debug_pidstr;
iov[2].iov_len = sudo_debug_pidlen;
/* Add string, trimming any trailing newlines. */
while (len > 0 && str[len - 1] == '\n')
len--;
if (len > 0) {
iov[iovcnt].iov_base = (char *)str;
iov[iovcnt].iov_len = len;
iovcnt++;
}
/* Append error string if errno is specified. */
if (errnum) {
if (len > 0) {
iov[iovcnt].iov_base = (char *)": ";
iov[iovcnt].iov_len = 2;
iovcnt++;
}
iov[iovcnt].iov_base = strerror(errnum);
iov[iovcnt].iov_len = strlen(iov[iovcnt].iov_base);
iovcnt++;
}
/* If function, file and lineno are specified, append them. */
if (func != NULL && file != NULL && lineno != 0) {
iov[iovcnt].iov_base = (char *)" @ ";
iov[iovcnt].iov_len = 3;
iovcnt++;
iov[iovcnt].iov_base = (char *)func;
iov[iovcnt].iov_len = strlen(func);
iovcnt++;
iov[iovcnt].iov_base = (char *)"() ";
iov[iovcnt].iov_len = 3;
iovcnt++;
iov[iovcnt].iov_base = (char *)file;
iov[iovcnt].iov_len = strlen(file);
iovcnt++;
(void)snprintf(numbuf, sizeof(numbuf), ":%d", lineno);
iov[iovcnt].iov_base = numbuf;
iov[iovcnt].iov_len = strlen(numbuf);
iovcnt++;
}
/* Append newline. */
iov[iovcnt].iov_base = (char *)"\n";
iov[iovcnt].iov_len = 1;
iovcnt++;
/* Write message in a single syscall */
ignore_result(writev(fd, iov, iovcnt));
}
bool
sudo_debug_needed_v1(int level)
{
unsigned int subsys;
int pri;
struct sudo_debug_instance *instance;
struct sudo_debug_output *output;
bool result = false;
if (sudo_debug_active_instance == -1)
goto out;
/* Extract priority and subsystem from level. */
pri = SUDO_DEBUG_PRI(level);
subsys = (unsigned int)SUDO_DEBUG_SUBSYS(level);
if (sudo_debug_active_instance > sudo_debug_last_instance)
goto out;
instance = sudo_debug_instances[sudo_debug_active_instance];
if (instance == NULL)
goto out;
if (subsys <= instance->max_subsystem) {
SLIST_FOREACH(output, &instance->outputs, entries) {
if (output->settings[subsys] >= pri) {
result = true;
break;
}
}
}
out:
return result;
}
void
sudo_debug_vprintf2_v1(const char *func, const char *file, int lineno, int level,
const char *fmt, va_list ap)
{
int buflen, pri, saved_errno = errno;
unsigned int subsys;
char static_buf[1024], *buf = static_buf;
struct sudo_debug_instance *instance;
struct sudo_debug_output *output;
debug_decl_func(sudo_debug_vprintf2);
if (sudo_debug_active_instance == -1)
goto out;
/* Extract priority and subsystem from level. */
pri = SUDO_DEBUG_PRI(level);
subsys = SUDO_DEBUG_SUBSYS(level);
/* Find matching instance. */
if (sudo_debug_active_instance > sudo_debug_last_instance) {
sudo_warnx_nodebug("%s: invalid instance ID %d, max %d",
__func__, sudo_debug_active_instance, sudo_debug_last_instance);
goto out;
}
instance = sudo_debug_instances[sudo_debug_active_instance];
if (instance == NULL) {
sudo_warnx_nodebug("%s: unregistered instance index %d", __func__,
sudo_debug_active_instance);
goto out;
}
SLIST_FOREACH(output, &instance->outputs, entries) {
/* Make sure we want debug info at this level. */
if (subsys <= instance->max_subsystem && output->settings[subsys] >= pri) {
va_list ap2;
/* Operate on a copy of ap to support multiple outputs. */
va_copy(ap2, ap);
buflen = fmt ? vsnprintf(static_buf, sizeof(static_buf), fmt, ap2) : 0;
va_end(ap2);
if (buflen >= ssizeof(static_buf)) {
va_list ap3;
/* Not enough room in static buf, allocate dynamically. */
va_copy(ap3, ap);
buflen = vasprintf(&buf, fmt, ap3);
va_end(ap3);
}
if (buflen != -1) {
int errcode = ISSET(level, SUDO_DEBUG_ERRNO) ? saved_errno : 0;
if (ISSET(level, SUDO_DEBUG_LINENO))
sudo_debug_write2(output->fd, func, file, lineno, buf, buflen, errcode);
else
sudo_debug_write2(output->fd, NULL, NULL, 0, buf, buflen, errcode);
if (buf != static_buf) {
free(buf);
buf = static_buf;
}
}
}
}
out:
errno = saved_errno;
}
#ifdef NO_VARIADIC_MACROS
void
sudo_debug_printf_nvm_v1(int pri, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sudo_debug_vprintf2(NULL, NULL, 0, pri, fmt, ap);
va_end(ap);
}
#endif /* NO_VARIADIC_MACROS */
void
sudo_debug_printf2_v1(const char *func, const char *file, int lineno, int level,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sudo_debug_vprintf2(func, file, lineno, level, fmt, ap);
va_end(ap);
}
#define EXEC_PREFIX "exec "
void
sudo_debug_execve2_v1(int level, const char *path, char *const argv[], char *const envp[])
{
int buflen, pri, saved_errno = errno;
unsigned int subsys;
struct sudo_debug_instance *instance;
struct sudo_debug_output *output;
char * const *av;
char *cp, static_buf[4096], *buf = static_buf;
size_t plen;
debug_decl_func(sudo_debug_execve2);
if (sudo_debug_active_instance == -1 || path == NULL)
goto out;
/* Extract priority and subsystem from level. */
pri = SUDO_DEBUG_PRI(level);
subsys = SUDO_DEBUG_SUBSYS(level);
/* Find matching instance. */
if (sudo_debug_active_instance > sudo_debug_last_instance) {
sudo_warnx_nodebug("%s: invalid instance ID %d, max %d",
__func__, sudo_debug_active_instance, sudo_debug_last_instance);
goto out;
}
instance = sudo_debug_instances[sudo_debug_active_instance];
if (instance == NULL) {
sudo_warnx_nodebug("%s: unregistered instance index %d", __func__,
sudo_debug_active_instance);
goto out;
}
if (subsys > instance->max_subsystem)
goto out;
SLIST_FOREACH(output, &instance->outputs, entries) {
bool log_envp = false;
/* Make sure we want debug info at this level. */
if (output->settings[subsys] < pri)
continue;
/* Log envp for debug level "debug". */
if (output->settings[subsys] >= SUDO_DEBUG_DEBUG - 1 && envp != NULL)
log_envp = true;
/* Alloc and build up buffer. */
plen = strlen(path);
buflen = sizeof(EXEC_PREFIX) -1 + plen;
if (argv != NULL && argv[0] != NULL) {
buflen += sizeof(" []") - 1;
for (av = argv; *av; av++)
buflen += strlen(*av) + 1;
buflen--;
}
if (log_envp && envp[0] != NULL) {
buflen += sizeof(" []") - 1;
for (av = envp; *av; av++)
buflen += strlen(*av) + 1;
buflen--;
}
if (buflen >= ssizeof(static_buf)) {
buf = malloc(buflen + 1);
if (buf == NULL)
goto out;
}
/* Copy prefix and command. */
memcpy(buf, EXEC_PREFIX, sizeof(EXEC_PREFIX) - 1);
cp = buf + sizeof(EXEC_PREFIX) - 1;
memcpy(cp, path, plen);
cp += plen;
/* Copy argv. */
if (argv != NULL && argv[0] != NULL) {
*cp++ = ' ';
*cp++ = '[';
for (av = argv; *av; av++) {
size_t avlen = strlen(*av);
memcpy(cp, *av, avlen);
cp += avlen;
*cp++ = ' ';
}
cp[-1] = ']';
}
if (log_envp && envp[0] != NULL) {
*cp++ = ' ';
*cp++ = '[';
for (av = envp; *av; av++) {
size_t avlen = strlen(*av);
memcpy(cp, *av, avlen);
cp += avlen;
*cp++ = ' ';
}
cp[-1] = ']';
}
*cp = '\0';
sudo_debug_write(output->fd, buf, buflen, 0);
if (buf != static_buf) {
free(buf);
buf = static_buf;
}
}
out:
errno = saved_errno;
}
/*
* Returns the active instance or SUDO_DEBUG_INSTANCE_INITIALIZER
* if no instance is active.
*/
int
sudo_debug_get_active_instance_v1(void)
{
return sudo_debug_active_instance;
}
/*
* Sets a new active instance, returning the old one.
* Note that the old instance may be SUDO_DEBUG_INSTANCE_INITIALIZER
* if this is the only instance.
*/
int
sudo_debug_set_active_instance_v1(int idx)
{
const int old_idx = sudo_debug_active_instance;
if (idx >= -1 && idx <= sudo_debug_last_instance)
sudo_debug_active_instance = idx;
return old_idx;
}
/*
* Replace the ofd with nfd in all outputs if present.
* Also updates sudo_debug_fds.
*/
void
sudo_debug_update_fd_v1(int ofd, int nfd)
{
int idx;
if (ofd <= sudo_debug_max_fd && sudo_isset(sudo_debug_fds, ofd)) {
/* Update sudo_debug_fds. */
sudo_clrbit(sudo_debug_fds, ofd);
sudo_setbit(sudo_debug_fds, nfd);
/* Update the outputs. */
for (idx = 0; idx <= sudo_debug_last_instance; idx++) {
struct sudo_debug_instance *instance;
struct sudo_debug_output *output;
instance = sudo_debug_instances[idx];
if (instance == NULL)
continue;
SLIST_FOREACH(output, &instance->outputs, entries) {
if (output->fd == ofd)
output->fd = nfd;
}
}
}
}
/*
* Returns the highest debug output fd or -1 if no debug files open.
* Fills in fds with the value of sudo_debug_fds.
*/
int
sudo_debug_get_fds_v1(unsigned char **fds)
{
*fds = sudo_debug_fds;
return sudo_debug_max_fd;
}
#else /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
int
sudo_debug_register_v2(const char *program, const char *const subsystems[],
unsigned int ids[], struct sudo_conf_debug_file_list *debug_files,
int minfd)
{
return SUDO_DEBUG_INSTANCE_INITIALIZER;
}
int
sudo_debug_register_v1(const char *program, const char *const subsystems[],
unsigned int ids[], struct sudo_conf_debug_file_list *debug_files)
{
return SUDO_DEBUG_INSTANCE_INITIALIZER;
}
int
sudo_debug_deregister_v1(int idx)
{
return -1;
}
int
sudo_debug_parse_flags_v1(struct sudo_conf_debug_file_list *debug_files,
const char *entry)
{
return 0;
}
int
sudo_debug_get_instance_v1(const char *program)
{
return SUDO_DEBUG_INSTANCE_INITIALIZER;
}
pid_t
sudo_debug_fork_v1(void)
{
return fork();
}
void
sudo_debug_enter_v1(const char *func, const char *file, int line,
int subsys)
{
}
void
sudo_debug_exit_v1(const char *func, const char *file, int line,
int subsys)
{
}
void
sudo_debug_exit_int_v1(const char *func, const char *file, int line,
int subsys, int ret)
{
}
void
sudo_debug_exit_long_v1(const char *func, const char *file, int line,
int subsys, long ret)
{
}
void
sudo_debug_exit_id_t_v1(const char *func, const char *file, int line,
int subsys, id_t ret)
{
}
void
sudo_debug_exit_size_t_v1(const char *func, const char *file, int line,
int subsys, size_t ret)
{
}
void
sudo_debug_exit_ssize_t_v1(const char *func, const char *file, int line,
int subsys, ssize_t ret)
{
}
void
sudo_debug_exit_time_t_v1(const char *func, const char *file, int line,
int subsys, time_t ret)
{
}
void
sudo_debug_exit_bool_v1(const char *func, const char *file, int line,
int subsys, bool ret)
{
}
void
sudo_debug_exit_str_v1(const char *func, const char *file, int line,
int subsys, const char *ret)
{
}
void
sudo_debug_exit_str_masked_v1(const char *func, const char *file, int line,
int subsys, const char *ret)
{
}
void
sudo_debug_exit_ptr_v1(const char *func, const char *file, int line,
int subsys, const void *ret)
{
}
void
sudo_debug_write2_v1(int fd, const char *func, const char *file, int lineno,
const char *str, int len, int errnum)
{
}
bool
sudo_debug_needed_v1(int level)
{
return false;
}
void
sudo_debug_vprintf2_v1(const char *func, const char *file, int lineno, int level,
const char *fmt, va_list ap)
{
}
#ifdef NO_VARIADIC_MACROS
void
sudo_debug_printf_nvm_v1(int pri, const char *fmt, ...)
{
}
#endif /* NO_VARIADIC_MACROS */
void
sudo_debug_printf2_v1(const char *func, const char *file, int lineno, int level,
const char *fmt, ...)
{
}
void
sudo_debug_execve2_v1(int level, const char *path, char *const argv[], char *const envp[])
{
}
int
sudo_debug_get_active_instance_v1(void)
{
return SUDO_DEBUG_INSTANCE_INITIALIZER;
}
int
sudo_debug_set_active_instance_v1(int idx)
{
return SUDO_DEBUG_INSTANCE_INITIALIZER;
}
void
sudo_debug_update_fd_v1(int ofd, int nfd)
{
}
int
sudo_debug_get_fds_v1(unsigned char **fds)
{
return -1;
}
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|