blob: 85d74de0fd964c9c5c912cbf44e262698b9c410b [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +04002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +04004 * 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.
plars865695b2001-08-27 22:15:12 +00008 *
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +04009 * 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.
plars865695b2001-08-27 22:15:12 +000013 *
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040014 * 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
plars865695b2001-08-27 22:15:12 +000017 *
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040018 * Ported by John George
plars865695b2001-08-27 22:15:12 +000019 */
20
21/*
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040022 * Test setregid() when executed by a non-root user.
plars865695b2001-08-27 22:15:12 +000023 */
24
plars865695b2001-08-27 22:15:12 +000025#include <errno.h>
26#include <grp.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080027#include <stdlib.h>
plars865695b2001-08-27 22:15:12 +000028#include <pwd.h>
29#include <string.h>
plars865695b2001-08-27 22:15:12 +000030#include <sys/wait.h>
31
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040032#include "test.h"
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040033#include "compat_16.h"
Wanlong Gao354ebb42012-12-07 10:10:04 +080034
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040035TCID_DEFINE(setregid03);
36static int fail = -1;
37static int pass;
38static gid_t neg_one = -1;
plars865695b2001-08-27 22:15:12 +000039
40/* flag to tell parent if child passed or failed. */
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040041static int flag;
plars865695b2001-08-27 22:15:12 +000042
robbiew3667ecf2001-08-31 16:12:21 +000043struct group users, sys, root, bin;
plars47eadf02001-09-13 16:20:59 +000044struct passwd nobody;
plars865695b2001-08-27 22:15:12 +000045/*
46 * The following structure contains all test data. Each structure in the array
47 * is used for a separate test. The tests are executed in the for loop below.
48 */
49
50struct test_data_t {
subrata_modak56207ce2009-03-23 13:35:39 +000051 gid_t *real_gid;
52 gid_t *eff_gid;
53 int *exp_ret;
54 struct group *exp_real_usr;
55 struct group *exp_eff_usr;
56 char *test_msg;
plars865695b2001-08-27 22:15:12 +000057} test_data[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080058 {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040059 &sys.gr_gid, &bin.gr_gid, &pass, &sys, &bin,
Wanlong Gao354ebb42012-12-07 10:10:04 +080060 "After setregid(sys, bin),"}, {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040061 &neg_one, &sys.gr_gid, &pass, &sys, &sys, "After setregid(-1, sys)"},
Wanlong Gao354ebb42012-12-07 10:10:04 +080062 {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040063 &neg_one, &bin.gr_gid, &pass, &sys, &bin, "After setregid(-1, bin),"},
Wanlong Gao354ebb42012-12-07 10:10:04 +080064 {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040065 &bin.gr_gid, &neg_one, &pass, &bin, &bin, "After setregid(bin, -1),"},
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 {
67 &neg_one, &neg_one, &pass, &bin, &bin, "After setregid(-1, -1),"}, {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040068 &neg_one, &bin.gr_gid, &pass, &bin, &bin, "After setregid(-1, bin),"},
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040070 &bin.gr_gid, &neg_one, &pass, &bin, &bin, "After setregid(bin, -1),"},
Wanlong Gao354ebb42012-12-07 10:10:04 +080071 {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040072 &bin.gr_gid, &bin.gr_gid, &pass, &bin, &bin,
Wanlong Gao354ebb42012-12-07 10:10:04 +080073 "After setregid(bin, bin),"}, {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040074 &sys.gr_gid, &neg_one, &fail, &bin, &bin, "After setregid(sys, -1)"},
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040076 &neg_one, &sys.gr_gid, &fail, &bin, &bin, "After setregid(-1, sys)"},
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040078 &sys.gr_gid, &sys.gr_gid, &fail, &bin, &bin,
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 "After setregid(sys, sys)"},};
plars865695b2001-08-27 22:15:12 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
plars865695b2001-08-27 22:15:12 +000082
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040083static void setup(void);
84static void cleanup(void);
85static void gid_verify(struct group *ru, struct group *eu, char *when);
plars865695b2001-08-27 22:15:12 +000086
robbiewf1ca2382003-03-27 20:28:50 +000087int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000088{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020089 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020090 const char *msg;
plars865695b2001-08-27 22:15:12 +000091
Garrett Cooper60fa8012010-11-22 13:50:58 -080092 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000093 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000094
plars865695b2001-08-27 22:15:12 +000095 setup();
96
plars865695b2001-08-27 22:15:12 +000097 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080098 pid_t pid;
99 int status, i;
plars865695b2001-08-27 22:15:12 +0000100
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400101 pass = 0;
102 flag = 0;
103
Caspar Zhangd59a6592013-03-07 14:59:12 +0800104 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000105
106 /* set the appropriate ownership values */
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400107 if (SETREGID(cleanup, sys.gr_gid, bin.gr_gid) == -1)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800108 tst_brkm(TBROK, NULL, "Initial setregid failed");
plars865695b2001-08-27 22:15:12 +0000109
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400110 if (seteuid(nobody.pw_uid) == -1)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800111 tst_brkm(TBROK, NULL, "Initial seteuid failed");
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400112
robbiewd34d5812005-07-11 22:28:09 +0000113 if ((pid = FORK_OR_VFORK()) == -1) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800114 tst_brkm(TBROK, NULL, "fork failed");
115 } else if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +0000116 for (i = 0; i < TST_TOTAL; i++) {
vapierfa569e42006-07-08 14:51:23 +0000117 gid_t test_ret;
plars865695b2001-08-27 22:15:12 +0000118 /* Set the real or effective group id */
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400119 TEST(SETREGID(NULL, *test_data[i].real_gid,
subrata_modak56207ce2009-03-23 13:35:39 +0000120 *test_data[i].eff_gid));
vapierfa569e42006-07-08 14:51:23 +0000121 test_ret = TEST_RETURN;
plars865695b2001-08-27 22:15:12 +0000122
vapierfa569e42006-07-08 14:51:23 +0000123 if (test_ret == *test_data[i].exp_ret) {
124 if (test_ret == neg_one) {
plars865695b2001-08-27 22:15:12 +0000125 if (TEST_ERRNO != EPERM) {
126 tst_resm(TFAIL,
subrata_modak56207ce2009-03-23 13:35:39 +0000127 "setregid(%d, %d) "
128 "did not set errno "
129 "value as expected.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 *test_data
131 [i].real_gid,
132 *test_data
133 [i].eff_gid);
subrata_modak56207ce2009-03-23 13:35:39 +0000134 fail = -1;
plars865695b2001-08-27 22:15:12 +0000135 continue;
136 } else {
137 tst_resm(TPASS,
subrata_modak56207ce2009-03-23 13:35:39 +0000138 "setregid(%d, %d) "
139 "failed as expected.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 *test_data
141 [i].real_gid,
142 *test_data
143 [i].eff_gid);
plars865695b2001-08-27 22:15:12 +0000144 }
145 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000146 tst_resm(TPASS,
147 "setregid(%d, %d) "
148 "succeeded as expected.",
149 *test_data[i].real_gid,
150 *test_data[i].eff_gid);
plars865695b2001-08-27 22:15:12 +0000151 }
152 } else {
153 tst_resm(TFAIL, "setregid(%d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +0000154 "did not return as expected.",
155 *test_data[i].real_gid,
156 *test_data[i].eff_gid);
157 flag = -1;
plars865695b2001-08-27 22:15:12 +0000158 }
vapierfa569e42006-07-08 14:51:23 +0000159 if (test_ret == -1) {
plars865695b2001-08-27 22:15:12 +0000160 }
161
Cyril Hrubise38b9612014-06-02 17:20:57 +0200162 gid_verify(test_data[i].exp_real_usr,
163 test_data[i].exp_eff_usr,
164 test_data[i].test_msg);
plars865695b2001-08-27 22:15:12 +0000165 }
166 exit(flag);
subrata_modak56207ce2009-03-23 13:35:39 +0000167 } else { /* parent */
plars865695b2001-08-27 22:15:12 +0000168 waitpid(pid, &status, 0);
169 if (WEXITSTATUS(status) != 0) {
170 tst_resm(TFAIL, "test failed within "
subrata_modak56207ce2009-03-23 13:35:39 +0000171 "child process.");
plars865695b2001-08-27 22:15:12 +0000172 }
173 }
174 }
175 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800176 tst_exit();
plars865695b2001-08-27 22:15:12 +0000177}
178
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400179static void setup(void)
plars865695b2001-08-27 22:15:12 +0000180{
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400181 struct group *junk;
182
Garrett Cooper60fa8012010-11-22 13:50:58 -0800183 tst_require_root(NULL);
184
plars865695b2001-08-27 22:15:12 +0000185 tst_sig(FORK, DEF_HANDLER, cleanup);
186
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400187 if (getpwnam("nobody") == NULL)
188 tst_brkm(TBROK, NULL, "nobody must be a valid user.");
plars47eadf02001-09-13 16:20:59 +0000189 nobody = *(getpwnam("nobody"));
plars865695b2001-08-27 22:15:12 +0000190
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400191#define GET_GID(group) do { \
192 junk = getgrnam(#group); \
193 if (junk == NULL) { \
194 tst_brkm(TBROK, NULL, "%s must be a valid group", #group); \
195 } \
196 GID16_CHECK(junk->gr_gid, setregid, NULL); \
197 group = *(junk); \
198} while (0)
plars865695b2001-08-27 22:15:12 +0000199
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400200 GET_GID(users);
201 GET_GID(sys);
202 GET_GID(bin);
plars865695b2001-08-27 22:15:12 +0000203
plars865695b2001-08-27 22:15:12 +0000204 TEST_PAUSE;
205}
206
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400207static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000208{
Garrett Cooper60fa8012010-11-22 13:50:58 -0800209}
plars865695b2001-08-27 22:15:12 +0000210
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400211static void gid_verify(struct group *rg, struct group *eg, char *when)
plars865695b2001-08-27 22:15:12 +0000212{
213 if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400214 tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
plars865695b2001-08-27 22:15:12 +0000215 when, getgid(), getegid());
216 tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d",
217 rg->gr_gid, eg->gr_gid);
218 flag = -1;
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400219 } else {
220 tst_resm(TPASS, "real or effective gid was modified as expected");
plars865695b2001-08-27 22:15:12 +0000221 }
Garrett Cooperbacc8492011-01-14 00:36:17 -0800222}