1 /* 2 * Copyright (c) International Business Machines Corp., 2001 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12 * the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 * 18 * Ported by John George 19 */ 20 21 /* 22 * Test setregid() when executed by root. 23 */ 24 25 #include <errno.h> 26 #include <pwd.h> 27 #include <grp.h> 28 #include <stdlib.h> 29 #include <string.h> 30 31 #include "test.h" 32 #include "compat_16.h" 33 34 TCID_DEFINE(setregid04); 35 36 static gid_t neg_one = -1; 37 38 static struct group users_gr, daemon_gr, root_gr, bin_gr; 39 40 /* 41 * The following structure contains all test data. Each structure in the array 42 * is used for a separate test. The tests are executed in the for loop below. 43 */ 44 45 struct test_data_t { 46 gid_t *real_gid; 47 gid_t *eff_gid; 48 struct group *exp_real_usr; 49 struct group *exp_eff_usr; 50 const char *test_msg; 51 } test_data[] = { 52 { 53 &root_gr.gr_gid, &root_gr.gr_gid, &root_gr, &root_gr, 54 "After setregid(root, root),"}, { 55 &users_gr.gr_gid, &neg_one, &users_gr, &root_gr, 56 "After setregid(users, -1)"}, { 57 &root_gr.gr_gid, &neg_one, &root_gr, &root_gr, 58 "After setregid(root,-1),"}, { 59 &neg_one, &neg_one, &root_gr, &root_gr, 60 "After setregid(-1, -1),"}, { 61 &neg_one, &root_gr.gr_gid, &root_gr, &root_gr, 62 "After setregid(-1, root)"}, { 63 &root_gr.gr_gid, &neg_one, &root_gr, &root_gr, 64 "After setregid(root, -1),"}, { 65 &daemon_gr.gr_gid, &users_gr.gr_gid, &daemon_gr, &users_gr, 66 "After setregid(daemon, users)"}, { 67 &neg_one, &neg_one, &daemon_gr, &users_gr, 68 "After setregid(-1, -1)"}, { 69 &neg_one, &users_gr.gr_gid, &daemon_gr, &users_gr, 70 "After setregid(-1, users)"} 71 }; 72 73 int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]); 74 75 static void setup(void); 76 static void cleanup(void); 77 static void gid_verify(struct group *ru, struct group *eu, const char *when); 78 79 int main(int ac, char **av) 80 { 81 int lc; 82 83 tst_parse_opts(ac, av, NULL, NULL); 84 85 setup(); 86 87 for (lc = 0; TEST_LOOPING(lc); lc++) { 88 int i; 89 90 tst_count = 0; 91 92 for (i = 0; i < TST_TOTAL; i++) { 93 /* Set the real or effective group id */ 94 TEST(SETREGID(cleanup, *test_data[i].real_gid, 95 *test_data[i].eff_gid)); 96 97 if (TEST_RETURN == -1) { 98 tst_resm(TBROK, "setregid(%d, %d) failed", 99 *test_data[i].real_gid, 100 *test_data[i].eff_gid); 101 } else { 102 gid_verify(test_data[i].exp_real_usr, 103 test_data[i].exp_eff_usr, 104 test_data[i].test_msg); 105 } 106 } 107 } 108 109 cleanup(); 110 tst_exit(); 111 } 112 113 #define SAFE_GETGROUP(GROUPNAME) \ 114 if (getgrnam(#GROUPNAME) == NULL) { \ 115 tst_brkm(TBROK, NULL, "Couldn't find the `" #GROUPNAME "' group"); \ 116 } \ 117 GROUPNAME ## _gr = *(getgrnam(#GROUPNAME)); 118 119 static void setup(void) 120 { 121 tst_require_root(); 122 123 tst_sig(FORK, DEF_HANDLER, cleanup); 124 125 SAFE_GETGROUP(root); 126 SAFE_GETGROUP(users); 127 SAFE_GETGROUP(daemon); 128 SAFE_GETGROUP(bin); 129 130 TEST_PAUSE; 131 } 132 133 static void cleanup(void) 134 { 135 } 136 137 static void gid_verify(struct group *rg, struct group *eg, const char *when) 138 { 139 if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) { 140 tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d", 141 when, getgid(), getegid()); 142 tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d", 143 rg->gr_gid, eg->gr_gid); 144 } else { 145 tst_resm(TPASS, "real or effective gid was modified as " 146 "expected"); 147 } 148 } 149