xref: /openbsd-src/regress/lib/libc/sys/t_getgroups.c (revision 49a6e16f2c2c8e509184b1f777366d1a6f337e1c)
1 /*	$OpenBSD: t_getgroups.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $	*/
2 /* $NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
3 
4 /*-
5  * Copyright (c) 2011 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jukka Ruohonen.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "macros.h"
34 
35 #include <sys/wait.h>
36 
37 #include "atf-c.h"
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 ATF_TC(getgroups_err);
ATF_TC_HEAD(getgroups_err,tc)45 ATF_TC_HEAD(getgroups_err, tc)
46 {
47 	atf_tc_set_md_var(tc, "descr", "Test errors in getgroups(2)");
48 }
49 
ATF_TC_BODY(getgroups_err,tc)50 ATF_TC_BODY(getgroups_err, tc)
51 {
52 	gid_t gidset[NGROUPS_MAX];
53 
54 	errno = 0;
55 
56 #if __OpenBSD__
57 	ATF_REQUIRE(getgroups(NGROUPS_MAX, (gid_t *)-1) == -1);
58 #else
59 	ATF_REQUIRE(getgroups(10, (gid_t *)-1) == -1);
60 #endif
61 	ATF_REQUIRE(errno == EFAULT);
62 
63 	errno = 0;
64 
65 	ATF_REQUIRE(getgroups(-1, gidset) == -1);
66 	ATF_REQUIRE(errno == EINVAL);
67 }
68 
69 ATF_TC(getgroups_getgid);
ATF_TC_HEAD(getgroups_getgid,tc)70 ATF_TC_HEAD(getgroups_getgid, tc)
71 {
72 	atf_tc_set_md_var(tc, "descr", "Test getgid(2) from getgroups(2)");
73 }
74 
ATF_TC_BODY(getgroups_getgid,tc)75 ATF_TC_BODY(getgroups_getgid, tc)
76 {
77 	gid_t gidset[NGROUPS_MAX];
78 	gid_t gid = getgid();
79 	int i, n;
80 
81 	/*
82 	 * Check that getgid(2) is found from
83 	 * the GIDs returned by getgroups(2).
84 	 */
85 	n = getgroups(NGROUPS_MAX, gidset);
86 
87 	for (i = 0; i < n; i++) {
88 
89 		if (gidset[i] == gid)
90 			return;
91 	}
92 
93 	atf_tc_fail("getgid(2) not found from getgroups(2)");
94 }
95 
96 ATF_TC(getgroups_setgid);
ATF_TC_HEAD(getgroups_setgid,tc)97 ATF_TC_HEAD(getgroups_setgid, tc)
98 {
99 	atf_tc_set_md_var(tc, "descr", "Test setgid(2) from getgroups(2)");
100 	atf_tc_set_md_var(tc, "require.user", "root");
101 }
102 
ATF_TC_BODY(getgroups_setgid,tc)103 ATF_TC_BODY(getgroups_setgid, tc)
104 {
105 	gid_t gidset[NGROUPS_MAX];
106 	int i, n, rv, sta;
107 	pid_t pid;
108 
109 	/*
110 	 * Check that we can setgid(2)
111 	 * to the returned group IDs.
112 	 */
113 	n = getgroups(NGROUPS_MAX, gidset);
114 	ATF_REQUIRE(n >= 0);
115 
116 	for (i = 0; i < n; i++) {
117 
118 		pid = fork();
119 		ATF_REQUIRE(pid >= 0);
120 
121 		if (pid == 0) {
122 
123 			rv = setgid(gidset[i]);
124 
125 			if (rv != 0)
126 				_exit(EXIT_FAILURE);
127 
128 			_exit(EXIT_SUCCESS);
129 		}
130 
131 		(void)wait(&sta);
132 
133 		if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
134 			atf_tc_fail("getgroups(2) is inconsistent");
135 	}
136 }
137 
138 ATF_TC(getgroups_zero);
ATF_TC_HEAD(getgroups_zero,tc)139 ATF_TC_HEAD(getgroups_zero, tc)
140 {
141 	atf_tc_set_md_var(tc, "descr", "Test getgroups(2) with zero param");
142 }
143 
ATF_TC_BODY(getgroups_zero,tc)144 ATF_TC_BODY(getgroups_zero, tc)
145 {
146 	const gid_t val = 123456789;
147 	gid_t gidset[NGROUPS_MAX];
148 	size_t i;
149 
150 	/*
151 	 * If the first parameter is zero, the number
152 	 * of groups should be returned but the supplied
153 	 * buffer should remain intact.
154 	 */
155 	for (i = 0; i < __arraycount(gidset); i++)
156 		gidset[i] = val;
157 
158 	ATF_REQUIRE(getgroups(0, gidset) >= 0);
159 
160 	for (i = 0; i < __arraycount(gidset); i++) {
161 
162 		if (gidset[i] != val)
163 			atf_tc_fail("getgroups(2) modified the buffer");
164 	}
165 }
166 
ATF_TP_ADD_TCS(tp)167 ATF_TP_ADD_TCS(tp)
168 {
169 
170 	ATF_TP_ADD_TC(tp, getgroups_err);
171 	ATF_TP_ADD_TC(tp, getgroups_getgid);
172 	ATF_TP_ADD_TC(tp, getgroups_setgid);
173 	ATF_TP_ADD_TC(tp, getgroups_zero);
174 
175 	return atf_no_error();
176 }
177