1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Ported by John George
20 */
21
22 /*
23 * Test that setregid() fails and sets the proper errno values when a
24 * non-root user attemps to change the real or effective group id to a
25 * value other than the current gid or the current effective gid.
26 */
27
28 #include <errno.h>
29 #include <pwd.h>
30 #include <grp.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "test.h"
35 #include "safe_macros.h"
36 #include "compat_16.h"
37
38 TCID_DEFINE(setregid02);
39
40 static gid_t neg_one = -1;
41
42 static struct passwd *ltpuser;
43
44 static struct group ltpgroup, root, bin;
45
46 /*
47 * The following structure contains all test data. Each structure in the array
48 * is used for a separate test. The tests are executed in the for loop below.
49 */
50
51 struct test_data_t {
52 gid_t *real_gid;
53 gid_t *eff_gid;
54 int exp_errno;
55 struct group *exp_real_usr;
56 struct group *exp_eff_usr;
57 char *test_msg;
58 } test_data[] = {
59 {
60 &neg_one, &root.gr_gid, EPERM, <pgroup, <pgroup,
61 "After setregid(-1, root),"}, {
62 &neg_one, &bin.gr_gid, EPERM, <pgroup, <pgroup,
63 "After setregid(-1, bin)"}, {
64 &root.gr_gid, &neg_one, EPERM, <pgroup, <pgroup,
65 "After setregid(root,-1),"}, {
66 &bin.gr_gid, &neg_one, EPERM, <pgroup, <pgroup,
67 "After setregid(bin, -1),"}, {
68 &root.gr_gid, &bin.gr_gid, EPERM, <pgroup, <pgroup,
69 "After setregid(root, bin)"}, {
70 &bin.gr_gid, &root.gr_gid, EPERM, <pgroup, <pgroup,
71 "After setregid(bin, root),"}
72 };
73
74 int TST_TOTAL = ARRAY_SIZE(test_data);
75
76 static void setup(void);
77 static void gid_verify(struct group *ru, struct group *eu, char *when);
78 static struct group get_group_by_name(const char *name);
79 static struct group get_group_by_gid(gid_t gid);
80
main(int ac,char ** av)81 int main(int ac, char **av)
82 {
83 int lc;
84
85 tst_parse_opts(ac, av, NULL, NULL);
86
87 setup();
88
89 for (lc = 0; TEST_LOOPING(lc); lc++) {
90 int i;
91
92 tst_count = 0;
93
94 for (i = 0; i < TST_TOTAL; i++) {
95 /* Set the real or effective group id */
96 TEST(SETREGID(NULL, *test_data[i].real_gid,
97 *test_data[i].eff_gid));
98
99 if (TEST_RETURN == -1) {
100 if (TEST_ERRNO == test_data[i].exp_errno) {
101 tst_resm(TPASS, "setregid(%d, %d) "
102 "failed as expected.",
103 *test_data[i].real_gid,
104 *test_data[i].eff_gid);
105 } else {
106 tst_resm(TFAIL, "setregid(%d, %d) "
107 "failed (%d) but did not set the "
108 "expected errno (%d).",
109 *test_data[i].real_gid,
110 *test_data[i].eff_gid,
111 TEST_ERRNO,
112 test_data[i].exp_errno);
113 }
114 } else {
115 tst_resm(TFAIL, "setregid(%d, %d) "
116 "did not fail (ret: %ld) as expected (ret: -1).",
117 *test_data[i].real_gid,
118 *test_data[i].eff_gid, TEST_RETURN);
119 }
120 gid_verify(test_data[i].exp_real_usr,
121 test_data[i].exp_eff_usr,
122 test_data[i].test_msg);
123 }
124 }
125
126 tst_exit();
127 }
128
setup(void)129 static void setup(void)
130 {
131 tst_require_root();
132
133 tst_sig(FORK, DEF_HANDLER, NULL);
134
135 ltpuser = getpwnam("nobody");
136 if (ltpuser == NULL)
137 tst_brkm(TBROK, NULL, "getpwnam(\"nobody\") failed");
138
139 SAFE_SETGID(NULL, ltpuser->pw_gid);
140 SAFE_SETUID(NULL, ltpuser->pw_uid);
141
142 root = get_group_by_name("root");
143 ltpgroup = get_group_by_gid(ltpuser->pw_gid);
144 bin = get_group_by_name("bin");
145
146 TEST_PAUSE;
147 }
148
get_group_by_name(const char * name)149 static struct group get_group_by_name(const char *name)
150 {
151 struct group *ret = getgrnam(name);
152
153 if (ret == NULL)
154 tst_brkm(TBROK|TERRNO, NULL, "getgrnam(\"%s\") failed", name);
155
156 GID16_CHECK(ret->gr_gid, setregid, NULL);
157
158 return *ret;
159 }
160
get_group_by_gid(gid_t gid)161 static struct group get_group_by_gid(gid_t gid)
162 {
163 struct group *ret = getgrgid(gid);
164
165 if (ret == NULL)
166 tst_brkm(TBROK|TERRNO, NULL, "getgrgid(\"%d\") failed", gid);
167
168 GID16_CHECK(ret->gr_gid, setregid, NULL);
169
170 return *ret;
171 }
172
gid_verify(struct group * rg,struct group * eg,char * when)173 void gid_verify(struct group *rg, struct group *eg, char *when)
174 {
175 if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
176 tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
177 when, getgid(), getegid());
178 tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d",
179 rg->gr_gid, eg->gr_gid);
180 } else {
181 tst_resm(TPASS, "real or effective gid was modified as expected");
182 }
183 }
184