summaryrefslogtreecommitdiff
path: root/minix/tests/test89.c
blob: 516f86f2be49689f77ba787ac8aa1152cbd185be (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
/* Tests for set[ug]id, sete[ug]id, and saved IDs - by D.C. van Moolenbroek */
/* This test must be run as root, as it tests privileged operations. */
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/sysctl.h>
#include <unistd.h>

#include "common.h"

#define ITERATIONS	2

/* These are in a specific order. */
enum {
	SUB_REAL,	/* test set[ug]id(2) */
	SUB_EFF,	/* test sete[ug]id(2) */
	SUB_REAL_E0,	/* test setgid(2) with euid=0 */
	SUB_EFF_E0,	/* test setegid(2) with euid=0 */
	SUB_RETAIN,	/* test r/e/s preservation across fork(2), exec(2) */
};

static const char *executable;

/*
 * The table below is exhaustive in terms of different combinations of real,
 * effective, and saved user IDs (with 0 being a special value, but 1 and 2
 * being interchangeable), but not all these combinations can actually be
 * established in practice.  The results for which there is no way to create
 * the initial condition are set to -1.  If we ever implement setresuid(2),
 * these results can be filled in and tested as well.
 */
static const struct uid_set {
	uid_t ruid;
	uid_t euid;
	uid_t suid;
	uid_t uid;
	int res;
	int eres;
} uid_sets[] = {
	{ 0, 0, 0, 0,  1,  1 },
	{ 0, 0, 0, 1,  1,  1 },
	{ 0, 0, 1, 0,  1,  1 },
	{ 0, 0, 1, 1,  1,  1 },
	{ 0, 0, 1, 2,  1,  1 },
	{ 0, 1, 0, 0,  1,  1 },
	{ 0, 1, 0, 1,  0,  0 },
	{ 0, 1, 0, 2,  0,  0 },
	{ 0, 1, 1, 0,  1,  1 },
	{ 0, 1, 1, 1,  0,  1 },
	{ 0, 1, 1, 2,  0,  0 },
	{ 0, 1, 2, 0, -1, -1 },
	{ 0, 1, 2, 1, -1, -1 },
	{ 0, 1, 2, 2, -1, -1 },
	{ 1, 0, 0, 0,  1,  1 },
	{ 1, 0, 0, 1,  1,  1 },
	{ 1, 0, 0, 2,  1,  1 },
	{ 1, 0, 1, 0, -1, -1 },
	{ 1, 0, 1, 1, -1, -1 },
	{ 1, 0, 1, 2, -1, -1 },
	{ 1, 0, 2, 0, -1, -1 },
	{ 1, 0, 2, 1, -1, -1 },
	{ 1, 0, 2, 2, -1, -1 },
	{ 1, 1, 0, 0,  0,  1 },
	{ 1, 1, 0, 1,  1,  1 },
	{ 1, 1, 0, 2,  0,  0 },
	{ 1, 1, 1, 0,  0,  0 },
	{ 1, 1, 1, 1,  1,  1 },
	{ 1, 1, 1, 2,  0,  0 },
	{ 1, 1, 2, 0,  0,  0 },
	{ 1, 1, 2, 1,  1,  1 },
	{ 1, 1, 2, 2,  0,  1 },
	{ 1, 2, 0, 0,  0,  1 },
	{ 1, 2, 0, 1,  1,  1 },
	{ 1, 2, 0, 2,  0,  0 },
	{ 1, 2, 1, 0, -1, -1 },
	{ 1, 2, 1, 1, -1, -1 },
	{ 1, 2, 1, 2, -1, -1 },
	{ 1, 2, 2, 0,  0,  0 },
	{ 1, 2, 2, 1,  1,  1 },
	{ 1, 2, 2, 2,  0,  1 },
};

/*
 * The same type of table but now for group identifiers.  In this case, all
 * combinations are possible to establish in practice, because the effective
 * UID, not the GID, is used for the privilege check.  GID 0 does not have any
 * special meaning, but we still test it as though it does, in order to ensure
 * that it in fact does not.
 */
static const struct gid_set {
	gid_t rgid;
	gid_t egid;
	gid_t sgid;
	gid_t gid;
	int res;
	int eres;
} gid_sets[] = {
	{ 0, 0, 0, 0, 1, 1 },
	{ 0, 0, 0, 1, 0, 0 },
	{ 0, 0, 1, 0, 1, 1 },
	{ 0, 0, 1, 1, 0, 1 },
	{ 0, 0, 1, 2, 0, 0 },
	{ 0, 1, 0, 0, 1, 1 },
	{ 0, 1, 0, 1, 0, 0 },
	{ 0, 1, 0, 2, 0, 0 },
	{ 0, 1, 1, 0, 1, 1 },
	{ 0, 1, 1, 1, 0, 1 },
	{ 0, 1, 1, 2, 0, 0 },
	{ 0, 1, 2, 0, 1, 1 },
	{ 0, 1, 2, 1, 0, 0 },
	{ 0, 1, 2, 2, 0, 1 },
	{ 1, 0, 0, 0, 0, 1 },
	{ 1, 0, 0, 1, 1, 1 },
	{ 1, 0, 0, 2, 0, 0 },
	{ 1, 0, 1, 0, 0, 0 },
	{ 1, 0, 1, 1, 1, 1 },
	{ 1, 0, 1, 2, 0, 0 },
	{ 1, 0, 2, 0, 0, 0 },
	{ 1, 0, 2, 1, 1, 1 },
	{ 1, 0, 2, 2, 0, 1 },
	{ 1, 1, 0, 0, 0, 1 },
	{ 1, 1, 0, 1, 1, 1 },
	{ 1, 1, 0, 2, 0, 0 },
	{ 1, 1, 1, 0, 0, 0 },
	{ 1, 1, 1, 1, 1, 1 },
	{ 1, 1, 1, 2, 0, 0 },
	{ 1, 1, 2, 0, 0, 0 },
	{ 1, 1, 2, 1, 1, 1 },
	{ 1, 1, 2, 2, 0, 1 },
	{ 1, 2, 0, 0, 0, 1 },
	{ 1, 2, 0, 1, 1, 1 },
	{ 1, 2, 0, 2, 0, 0 },
	{ 1, 2, 1, 0, 0, 0 },
	{ 1, 2, 1, 1, 1, 1 },
	{ 1, 2, 1, 2, 0, 0 },
	{ 1, 2, 2, 0, 0, 0 },
	{ 1, 2, 2, 1, 1, 1 },
	{ 1, 2, 2, 2, 0, 1 },
};

/*
 * Obtain the kinfo_proc2 data for the given process ID.  Return 0 on success,
 * or -1 with errno set appropriately on failure.
 */
static int
get_proc2(pid_t pid, struct kinfo_proc2 * proc2)
{
	int mib[6];
	size_t oldlen;

	/*
	 * FIXME: for performance reasons, the MIB service updates it process
	 * tables only every clock tick.  As a result, we may not be able to
	 * obtain accurate process details right away, and we need to wait.
	 * Eventually, the MIB service should retrieve more targeted subsets of
	 * the process tables, and this problem should go away at least for
	 * specific queries such as this one, which queries only a single PID.
	 */
	usleep((2000000 + sysconf(_SC_CLK_TCK)) / sysconf(_SC_CLK_TCK));

	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC2;
	mib[2] = KERN_PROC_PID;
	mib[3] = pid;
	mib[4] = sizeof(*proc2);
	mib[5] = 1;

	oldlen = sizeof(*proc2);
	if (sysctl(mib, __arraycount(mib), proc2, &oldlen, NULL, 0) == -1)
		return -1;
	if (oldlen != sizeof(*proc2)) {
		errno = ESRCH;
		return -1;
	}
	return 0;
}

/*
 * Verify that the current process's real, effective, and saved user IDs are
 * set to the given respective value.
 */
static void
test_uids(uid_t ruid, uid_t euid, uid_t suid)
{
	struct kinfo_proc2 proc2;

	if (getuid() != ruid) e(0);
	if (geteuid() != euid) e(0);

	/*
	 * There is no system call specifically to retrieve the saved user ID,
	 * so we use sysctl(2) to obtain process information.  This allows us
	 * to verify the real and effective user IDs once more, too.
	 */
	if (get_proc2(getpid(), &proc2) != 0) e(0);

	if (proc2.p_ruid != ruid) e(0);
	if (proc2.p_uid != euid) e(0);
	if (proc2.p_svuid != suid) e(0);
}

/*
 * Verify that the real and effective user IDs are kept as is after an exec(2)
 * call on a non-setuid binary, and that the saved user ID is set to the
 * effective user ID.
 */
static void
exec89b(const char * param1, const char * param2 __unused)
{
	const struct uid_set *set;
	int setnum;

	setnum = atoi(param1);
	if (setnum < 0 || setnum >= __arraycount(uid_sets)) {
		e(setnum);
		return;
	}
	set = &uid_sets[setnum];

	test_uids(set->ruid, set->euid, set->euid);
}

/*
 * The real, effective, and saved user IDs have been set up as indicated by the
 * current set.  Verify that fork(2) and exec(2) do not change the real and
 * effective UIDs, and that only exec(2) sets the saved UID to the effective
 * UID.
 */
static void
sub89b(int setnum)
{
	const struct uid_set *set;
	char param1[32];
	pid_t pid;
	int status;

	set = &uid_sets[setnum];

	pid = fork();

	switch (pid) {
	case -1:
		e(setnum);
		break;

	case 0:
		/*
		 * Verify that all the UIDs were retained across the fork(2)
		 * call.
		 */
		test_uids(set->ruid, set->euid, set->suid);

		snprintf(param1, sizeof(param1), "%d", setnum);

		(void)execl(executable, executable, "DO CHECK", "b", param1,
		    "", NULL);

		e(setnum);
		break;

	default:
		if (waitpid(pid, &status, 0) != pid) e(setnum);
		if (!WIFEXITED(status)) e(setnum);
		if (WEXITSTATUS(status) != 0) e(setnum);
	}
}

/*
 * The real, effective, and saved user IDs have been set up as indicated by the
 * current set.  Test one particular case for test A or B, and verify the
 * result.
 */
static void
test_one_uid(int setnum, int sub)
{
	const struct uid_set *set;
	int res, exp;

	set = &uid_sets[setnum];

	/* Verify that the pre-call process state is as expected. */
	test_uids(set->ruid, set->euid, set->suid);

	/* Perform the call, and check whether the result is as expected. */
	switch (sub) {
	case SUB_REAL:
		res = setuid(set->uid);
		exp = set->res - 1;
		break;

	case SUB_EFF:
		res = seteuid(set->uid);
		exp = set->eres - 1;
		break;

	case SUB_RETAIN:
		sub89b(setnum);

		return;

	default:
		abort();
	}

	if (res != 0 && (res != -1 || errno != EPERM)) e(setnum);

	if (res != exp) e(setnum);

	/* Verify that the post-call process state is as expected as well. */
	if (res == 0) {
		if (sub == SUB_EFF)
			test_uids(set->ruid, set->uid, set->suid);
		else
			test_uids(set->uid, set->uid, set->uid);
	} else
		test_uids(set->ruid, set->euid, set->suid);
}

/*
 * Test setuid(2) or seteuid(2) after a successful execve(2) call, which should
 * have set the process's effective and saved user ID.
 */
static void
exec89a(const char * param1, const char * param2)
{
	const struct uid_set *set;
	int setnum, sub;

	setnum = atoi(param1);
	if (setnum < 0 || setnum >= __arraycount(uid_sets)) {
		e(setnum);
		return;
	}
	set = &uid_sets[setnum];

	sub = atoi(param2);

	if (sub == SUB_RETAIN) {
		/* Clear the set-uid bit before dropping more privileges. */
		if (chmod(executable, S_IXUSR | S_IXGRP | S_IXOTH) != 0)
			e(setnum);
	}

	/* Finish setting up the initial condition. */
	if (set->euid != set->suid) {
		if (set->euid != set->ruid && set->suid != 0) {
			test_uids(set->ruid, set->suid, set->suid);

			return; /* skip test */
		}

		if (seteuid(set->euid) != 0) e(setnum);
	}

	/* Perform the actual test. */
	test_one_uid(setnum, sub);
}

/*
 * Test setuid(2) or seteuid(2) with a certain value starting from a certain
 * initial condition, as identified by the given uid_sets[] array element.  As
 * a side effect, test that in particular exec(2) properly sets the effective
 * and saved user ID.
 */
static void
sub89a(int setnum, int sub)
{
	const struct uid_set *set;
	char param1[32], param2[32];

	set = &uid_sets[setnum];

	/*
	 * Figure out how to set the real, effective, and saved UIDs to those
	 * of the set structure.  Without setresuid(2), not all combinations
	 * are possible to achieve.  We silently skip the tests for which we
	 * cannot create the requested initial condition.
	 */
	if (set->ruid != set->suid) {
		/*
		 * In order to set the saved UID to something other than the
		 * real UID, we must exec(2) a set-uid binary.
		 */
		if (chown(executable, set->suid, 0 /*anything*/) != 0) e(0);
		if (chmod(executable,
		    S_ISUID | S_IXUSR | S_IXGRP | S_IXOTH) != 0) e(0);

		if (setuid(set->ruid) != 0) e(setnum);

		snprintf(param1, sizeof(param1), "%d", setnum);
		snprintf(param2, sizeof(param2), "%d", sub);

		(void)execl(executable, executable, "DO CHECK", "a", param1,
		    param2, NULL);

		e(0);
	} else {
		/*
		 * If the real and saved user ID are to be set to the same
		 * value, we need not use exec(2).  Still, we cannot achieve
		 * all combinations here either.
		 */
		if (set->ruid != 0 && set->ruid != set->euid)
			return; /* skip test */

		if (sub == SUB_RETAIN) {
			/* Clear the set-uid bit before dropping privileges. */
			if (chmod(executable,
			    S_IXUSR | S_IXGRP | S_IXOTH) != 0) e(setnum);
		}

		if (setuid(set->ruid) != 0) e(setnum);
		if (seteuid(set->euid) != 0) e(setnum);

		/* Perform the actual test. */
		test_one_uid(setnum, sub);
	}
}

/*
 * Test setuid(2) and seteuid(2) calls with various initial conditions, by
 * setting the real, effective, and saved UIDs to different values before
 * performing the setuid(2) or seteuid(2) call.
 */
static void
test89a(void)
{
	unsigned int setnum;
	int sub, status;
	pid_t pid;

	subtest = 1;

	for (setnum = 0; setnum < __arraycount(uid_sets); setnum++) {
		for (sub = SUB_REAL; sub <= SUB_EFF; sub++) {
			pid = fork();

			switch (pid) {
			case -1:
				e(setnum);

				break;

			case 0:
				errct = 0;

				sub89a((int)setnum, sub);

				exit(errct);
				/* NOTREACHED */

			default:
				if (waitpid(pid, &status, 0) != pid) e(setnum);
				if (!WIFEXITED(status)) e(setnum);
				if (WEXITSTATUS(status) != 0) e(setnum);
			}
		}
	}
}

/*
 * Ensure that the real, effective, and saved UIDs are fully preserved across
 * fork(2) and non-setuid-binary exec(2) calls.
 */
static void
test89b(void)
{
	unsigned int setnum;
	int status;
	pid_t pid;

	subtest = 2;

	for (setnum = 0; setnum < __arraycount(uid_sets); setnum++) {
		if (uid_sets[setnum].uid != 0)
			continue; /* no need to do the same test >1 times */

		pid = fork();

		switch (pid) {
		case -1:
			e(setnum);

			break;

		case 0:
			errct = 0;

			/*
			 * Test B uses some of the A-test code.  While rather
			 * ugly, this avoids duplication of some of test A's
			 * important UID logic.
			 */
			sub89a((int)setnum, SUB_RETAIN);

			exit(errct);
			/* NOTREACHED */

		default:
			if (waitpid(pid, &status, 0) != pid) e(setnum);
			if (!WIFEXITED(status)) e(setnum);
			if (WEXITSTATUS(status) != 0) e(setnum);
		}
	}
}

/*
 * Verify that the current process's real, effective, and saved group IDs are
 * set to the given respective value.
 */
static void
test_gids(gid_t rgid, gid_t egid, gid_t sgid)
{
	struct kinfo_proc2 proc2;

	if (getgid() != rgid) e(0);
	if (getegid() != egid) e(0);

	/* As above. */
	if (get_proc2(getpid(), &proc2) != 0) e(0);

	if (proc2.p_rgid != rgid) e(0);
	if (proc2.p_gid != egid) e(0);
	if (proc2.p_svgid != sgid) e(0);
}

/*
 * Verify that the real and effective group IDs are kept as is after an exec(2)
 * call on a non-setgid binary, and that the saved group ID is set to the
 * effective group ID.
 */
static void
exec89d(const char * param1, const char * param2 __unused)
{
	const struct gid_set *set;
	int setnum;

	setnum = atoi(param1);
	if (setnum < 0 || setnum >= __arraycount(gid_sets)) {
		e(setnum);
		return;
	}
	set = &gid_sets[setnum];

	test_gids(set->rgid, set->egid, set->egid);
}

/*
 * The real, effective, and saved group IDs have been set up as indicated by
 * the current set.  Verify that fork(2) and exec(2) do not change the real and
 * effective GID, and that only exec(2) sets the saved GID to the effective
 * GID.
 */
static void
sub89d(int setnum)
{
	const struct gid_set *set;
	char param1[32];
	pid_t pid;
	int status;

	set = &gid_sets[setnum];

	pid = fork();

	switch (pid) {
	case -1:
		e(setnum);
		break;

	case 0:
		/*
		 * Verify that all the GIDs were retained across the fork(2)
		 * call.
		 */
		test_gids(set->rgid, set->egid, set->sgid);

		/* Clear the set-gid bit. */
		if (chmod(executable, S_IXUSR | S_IXGRP | S_IXOTH) != 0)
			e(setnum);

		/* Alternate between preserving and dropping user IDs. */
		if (set->gid != 0) {
			if (setuid(3) != 0) e(setnum);
		}

		snprintf(param1, sizeof(param1), "%d", setnum);

		(void)execl(executable, executable, "DO CHECK", "d", param1,
		    "", NULL);

		e(setnum);
		break;

	default:
		if (waitpid(pid, &status, 0) != pid) e(setnum);
		if (!WIFEXITED(status)) e(setnum);
		if (WEXITSTATUS(status) != 0) e(setnum);
	}
}

/*
 * The real, effective, and saved group IDs have been set up as indicated by
 * the current set.  Test one particular case for test C or D, and verify the
 * result.
 */
static void
test_one_gid(int setnum, int sub)
{
	const struct gid_set *set;
	int res, exp;

	set = &gid_sets[setnum];

	/* Verify that the pre-call process state is as expected. */
	test_gids(set->rgid, set->egid, set->sgid);

	/* Perform the call, and check whether the result is as expected. */
	switch (sub) {
	case SUB_REAL:
	case SUB_REAL_E0:
		if (sub != SUB_REAL_E0 && seteuid(1) != 0) e(0);

		res = setgid(set->gid);
		exp = (sub != SUB_REAL_E0) ? (set->res - 1) : 0;
		break;

	case SUB_EFF:
	case SUB_EFF_E0:
		if (sub != SUB_EFF_E0 && seteuid(1) != 0) e(0);

		res = setegid(set->gid);
		exp = (sub != SUB_EFF_E0) ? (set->eres - 1) : 0;
		break;

	case SUB_RETAIN:
		sub89d(setnum);

		return;

	default:
		abort();
	}

	if (res != 0 && (res != -1 || errno != EPERM)) e(setnum);

	if (res != exp) e(setnum);

	/* Verify that the post-call process state is as expected as well. */
	if (res == 0) {
		if (sub == SUB_EFF || sub == SUB_EFF_E0)
			test_gids(set->rgid, set->gid, set->sgid);
		else
			test_gids(set->gid, set->gid, set->gid);
	} else
		test_gids(set->rgid, set->egid, set->sgid);
}

/*
 * Test setgid(2) or setegid(2) after a successful execve(2) call, which should
 * have set the process's effective and saved group ID.
 */
static void
exec89c(const char * param1, const char * param2)
{
	const struct gid_set *set;
	int setnum, sub;

	setnum = atoi(param1);
	if (setnum < 0 || setnum >= __arraycount(gid_sets)) {
		e(setnum);
		return;
	}
	set = &gid_sets[setnum];

	sub = atoi(param2);

	/* Finish setting up the initial condition. */
	if (set->egid != set->sgid && setegid(set->egid) != 0) e(setnum);

	/* Perform the actual test. */
	test_one_gid(setnum, sub);
}

/*
 * Test setgid(2) or setegid(2) with a certain value starting from a certain
 * initial condition, as identified by the given gid_sets[] array element.  As
 * a side effect, test that in particular exec(2) properly sets the effective
 * and saved group ID.
 */
static void
sub89c(int setnum, int sub)
{
	const struct gid_set *set;
	char param1[32], param2[32];

	set = &gid_sets[setnum];

	/*
	 * Figure out how to set the real, effective, and saved GIDs to those
	 * of the set structure.  In this case, all combinations are possible.
	 */
	if (set->rgid != set->sgid) {
		/*
		 * In order to set the saved GID to something other than the
		 * real GID, we must exec(2) a set-gid binary.
		 */
		if (chown(executable, 0 /*anything*/, set->sgid) != 0) e(0);
		if (chmod(executable,
		    S_ISGID | S_IXUSR | S_IXGRP | S_IXOTH) != 0) e(0);

		if (setgid(set->rgid) != 0) e(setnum);

		snprintf(param1, sizeof(param1), "%d", setnum);
		snprintf(param2, sizeof(param2), "%d", sub);

		(void)execl(executable, executable, "DO CHECK", "c", param1,
		    param2, NULL);

		e(0);
	} else {
		/*
		 * If the real and saved group ID are to be set to the same
		 * value, we need not use exec(2).
		 */
		if (setgid(set->rgid) != 0) e(setnum);
		if (setegid(set->egid) != 0) e(setnum);

		/* Perform the actual test. */
		test_one_gid(setnum, sub);
	}
}

/*
 * Test setgid(2) and setegid(2) calls with various initial conditions, by
 * setting the real, effective, and saved GIDs to different values before
 * performing the setgid(2) or setegid(2) call.  At the same time, verify that
 * if the caller has an effective UID of 0, all set(e)gid calls are allowed.
 */
static void
test89c(void)
{
	unsigned int setnum;
	int sub, status;
	pid_t pid;

	subtest = 3;

	for (setnum = 0; setnum < __arraycount(gid_sets); setnum++) {
		for (sub = SUB_REAL; sub <= SUB_EFF_E0; sub++) {
			pid = fork();

			switch (pid) {
			case -1:
				e(setnum);

				break;

			case 0:
				errct = 0;

				sub89c((int)setnum, sub);

				exit(errct);
				/* NOTREACHED */

			default:
				if (waitpid(pid, &status, 0) != pid) e(setnum);
				if (!WIFEXITED(status)) e(setnum);
				if (WEXITSTATUS(status) != 0) e(setnum);
			}
		}
	}
}

/*
 * Ensure that the real, effective, and saved GIDs are fully preserved across
 * fork(2) and non-setgid-binary exec(2) calls.
 */
static void
test89d(void)
{
	unsigned int setnum;
	int status;
	pid_t pid;

	subtest = 4;

	for (setnum = 0; setnum < __arraycount(gid_sets); setnum++) {
		if (gid_sets[setnum].gid == 2)
			continue; /* no need to do the same test >1 times */

		pid = fork();

		switch (pid) {
		case -1:
			e(setnum);

			break;

		case 0:
			errct = 0;

			/* Similarly, test D uses some of the C-test code. */
			sub89c((int)setnum, SUB_RETAIN);

			exit(errct);
			/* NOTREACHED */

		default:
			if (waitpid(pid, &status, 0) != pid) e(setnum);
			if (!WIFEXITED(status)) e(setnum);
			if (WEXITSTATUS(status) != 0) e(setnum);
		}
	}
}

/*
 * Either perform the second step of setting up user and group IDs, or check
 * whether the user and/or group IDs have indeed been changed appropriately as
 * the result of the second exec(2).
 */
static void
exec89e(const char * param1, const char * param2)
{
	int mask, step;
	mode_t mode;

	mask = atoi(param1);
	step = atoi(param2);

	if (step == 0) {
		mode = S_IXUSR | S_IXGRP | S_IXOTH;
		if (mask & 1) mode |= S_ISUID;
		if (mask & 2) mode |= S_ISGID;

		if (chown(executable, 6, 7) != 0) e(0);
		if (chmod(executable, mode) != 0) e(0);

		if (setegid(4) != 0) e(0);
		if (seteuid(2) != 0) e(0);

		test_uids(1, 2, 0);
		test_gids(3, 4, 5);

		(void)execl(executable, executable, "DO CHECK", "e", param1,
		    "1", NULL);

		e(0);
	} else {
		if (mask & 1)
			test_uids(1, 6, 6);
		else
			test_uids(1, 2, 2);

		if (mask & 2)
			test_gids(3, 7, 7);
		else
			test_gids(3, 4, 4);
	}
}

/*
 * Set up for the set-uid/set-gid execution test by initializing to different
 * real and effective user IDs.
 */
static void
sub89e(int mask)
{
	char param1[32];

	if (chown(executable, 0, 5) != 0) e(0);
	if (chmod(executable,
	    S_ISUID | S_ISGID | S_IXUSR | S_IXGRP | S_IXOTH) != 0) e(0);

	if (setgid(3) != 0) e(0);
	if (setuid(1) != 0) e(0);

	snprintf(param1, sizeof(param1), "%d", mask);
	(void)execl(executable, executable, "DO CHECK", "e", param1, "0",
	    NULL);
}

/*
 * Perform basic verification that the set-uid and set-gid bits on binaries are
 * fully independent from each other.
 */
static void
test89e(void)
{
	int mask, status;
	pid_t pid;

	subtest = 5;

	for (mask = 0; mask <= 3; mask++) {
		pid = fork();

		switch (pid) {
		case -1:
			e(0);

			break;

		case 0:
			errct = 0;

			sub89e(mask);

			exit(errct);
			/* NOTREACHED */

		default:
			if (waitpid(pid, &status, 0) != pid) e(mask);
			if (!WIFEXITED(status)) e(mask);
			if (WEXITSTATUS(status) != 0) e(mask);
		}
	}
}

/*
 * Call the right function after having executed myself.
 */
static void
exec89(const char * param0, const char * param1, const char * param2)
{

	switch (param0[0]) {
	case 'a':
		exec89a(param1, param2);
		break;

	case 'b':
		exec89b(param1, param2);
		break;

	case 'c':
		exec89c(param1, param2);
		break;

	case 'd':
		exec89d(param1, param2);
		break;

	case 'e':
		exec89e(param1, param2);
		break;

	default:
		e(0);
	}

	exit(errct);
}

/*
 * Initialize the test.
 */
static void
test89_init(void)
{
	char cp_cmd[PATH_MAX + 9];
	int status;

	subtest = 0;

	/* Reset all user and group IDs to known values. */
	if (setuid(0) != 0) e(0);
	if (setgid(0) != 0) e(0);
	if (setgroups(0, NULL) != 0) e(0);

	test_uids(0, 0, 0);
	test_gids(0, 0, 0);

	/* Make a copy of the binary, which as of start() is one level up. */
	snprintf(cp_cmd, sizeof(cp_cmd), "cp ../%s .", executable);

	status = system(cp_cmd);
	if (status < 0 || !WIFEXITED(status) ||
	    WEXITSTATUS(status) != EXIT_SUCCESS) e(0);
}

/*
 * Test program for set[ug]id, sete[ug]id, and saved IDs.
 */
int
main(int argc, char ** argv)
{
	int i, m;

	executable = argv[0];

	/* This test executes itself.  Handle that case first. */
	if (argc == 5 && !strcmp(argv[1], "DO CHECK"))
		exec89(argv[2], argv[3], argv[4]);

	start(89);

	test89_init();

	if (argc == 2)
		m = atoi(argv[1]);
	else
		m = 0xFF;

	for (i = 0; i < ITERATIONS; i++) {
		if (m & 0x01) test89a();
		if (m & 0x02) test89b();
		if (m & 0x04) test89c();
		if (m & 0x08) test89d();
		if (m & 0x10) test89e();
	}

	quit();
	/* NOTREACHED */
}