1 /* $OpenBSD: t_getgroups.c,v 1.2 2020/11/09 23:18:51 bluhm 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/cdefs.h> 36 __RCSID("$NetBSD: t_getgroups.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $"); 37 38 #include <sys/wait.h> 39 40 #include "atf-c.h" 41 #include <errno.h> 42 #include <limits.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 ATF_TC(getgroups_err); 48 ATF_TC_HEAD(getgroups_err, tc) 49 { 50 atf_tc_set_md_var(tc, "descr", "Test errors in getgroups(2)"); 51 } 52 53 ATF_TC_BODY(getgroups_err, tc) 54 { 55 gid_t gidset[NGROUPS_MAX]; 56 57 errno = 0; 58 59 #if __OpenBSD__ 60 ATF_REQUIRE(getgroups(NGROUPS_MAX, (gid_t *)-1) == -1); 61 #else 62 ATF_REQUIRE(getgroups(10, (gid_t *)-1) == -1); 63 #endif 64 ATF_REQUIRE(errno == EFAULT); 65 66 errno = 0; 67 68 ATF_REQUIRE(getgroups(-1, gidset) == -1); 69 ATF_REQUIRE(errno == EINVAL); 70 } 71 72 ATF_TC(getgroups_getgid); 73 ATF_TC_HEAD(getgroups_getgid, tc) 74 { 75 atf_tc_set_md_var(tc, "descr", "Test getgid(2) from getgroups(2)"); 76 } 77 78 ATF_TC_BODY(getgroups_getgid, tc) 79 { 80 gid_t gidset[NGROUPS_MAX]; 81 gid_t gid = getgid(); 82 int i, n; 83 84 /* 85 * Check that getgid(2) is found from 86 * the GIDs returned by getgroups(2). 87 */ 88 n = getgroups(NGROUPS_MAX, gidset); 89 90 for (i = 0; i < n; i++) { 91 92 if (gidset[i] == gid) 93 return; 94 } 95 96 atf_tc_fail("getgid(2) not found from getgroups(2)"); 97 } 98 99 ATF_TC(getgroups_setgid); 100 ATF_TC_HEAD(getgroups_setgid, tc) 101 { 102 atf_tc_set_md_var(tc, "descr", "Test setgid(2) from getgroups(2)"); 103 atf_tc_set_md_var(tc, "require.user", "root"); 104 } 105 106 ATF_TC_BODY(getgroups_setgid, tc) 107 { 108 gid_t gidset[NGROUPS_MAX]; 109 int i, n, rv, sta; 110 pid_t pid; 111 112 /* 113 * Check that we can setgid(2) 114 * to the returned group IDs. 115 */ 116 n = getgroups(NGROUPS_MAX, gidset); 117 ATF_REQUIRE(n >= 0); 118 119 for (i = 0; i < n; i++) { 120 121 pid = fork(); 122 ATF_REQUIRE(pid >= 0); 123 124 if (pid == 0) { 125 126 rv = setgid(gidset[i]); 127 128 if (rv != 0) 129 _exit(EXIT_FAILURE); 130 131 _exit(EXIT_SUCCESS); 132 } 133 134 (void)wait(&sta); 135 136 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 137 atf_tc_fail("getgroups(2) is inconsistent"); 138 } 139 } 140 141 ATF_TC(getgroups_zero); 142 ATF_TC_HEAD(getgroups_zero, tc) 143 { 144 atf_tc_set_md_var(tc, "descr", "Test getgroups(2) with zero param"); 145 } 146 147 ATF_TC_BODY(getgroups_zero, tc) 148 { 149 const gid_t val = 123456789; 150 gid_t gidset[NGROUPS_MAX]; 151 size_t i; 152 153 /* 154 * If the first parameter is zero, the number 155 * of groups should be returned but the supplied 156 * buffer should remain intact. 157 */ 158 for (i = 0; i < __arraycount(gidset); i++) 159 gidset[i] = val; 160 161 ATF_REQUIRE(getgroups(0, gidset) >= 0); 162 163 for (i = 0; i < __arraycount(gidset); i++) { 164 165 if (gidset[i] != val) 166 atf_tc_fail("getgroups(2) modified the buffer"); 167 } 168 } 169 170 ATF_TP_ADD_TCS(tp) 171 { 172 173 ATF_TP_ADD_TC(tp, getgroups_err); 174 ATF_TP_ADD_TC(tp, getgroups_getgid); 175 ATF_TP_ADD_TC(tp, getgroups_setgid); 176 ATF_TP_ADD_TC(tp, getgroups_zero); 177 178 return atf_no_error(); 179 } 180