blob: 93b7a4571ed6b5ea32529d2e9c10ec55fcca5743 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +04003 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00004 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +04005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040015 * 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
plars865695b2001-08-27 22:15:12 +000020 */
21
22/*
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040023 * Test setreuid() when executed by an unpriviledged user.
plars865695b2001-08-27 22:15:12 +000024 */
25
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040026#include <errno.h>
plars865695b2001-08-27 22:15:12 +000027#include <pwd.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080028#include <stdlib.h>
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040029
Garrett Coopere8530df2010-12-21 11:37:57 -080030#include "test.h"
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040031#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000032
33#define FAILED 1
34
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040035TCID_DEFINE(setreuid03);
plars865695b2001-08-27 22:15:12 +000036
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040037static int fail = -1;
38static int pass;
39static uid_t neg_one = -1;
plars865695b2001-08-27 22:15:12 +000040
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040041static struct passwd nobody, bin, root;
plars865695b2001-08-27 22:15:12 +000042
43/*
44 * The following structure contains all test data. Each structure in the array
45 * is used for a separate test. The tests are executed in the for loop below.
46 */
47
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040048static struct test_data_t {
subrata_modak56207ce2009-03-23 13:35:39 +000049 uid_t *real_uid;
50 uid_t *eff_uid;
51 int *exp_ret;
52 struct passwd *exp_real_usr;
53 struct passwd *exp_eff_usr;
54 char *test_msg;
plars865695b2001-08-27 22:15:12 +000055} test_data[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000056 {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040057 &nobody.pw_uid, &nobody.pw_uid, &pass, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000058 "After setreuid(nobody, nobody),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040059 &neg_one, &nobody.pw_uid, &pass, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000060 "After setreuid(-1, nobody),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040061 &nobody.pw_uid, &neg_one, &pass, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000062 "After setreuid(nobody, -1),"}, {
63 &neg_one, &neg_one, &pass, &nobody, &nobody, "After setreuid(-1, -1),"},
64 {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040065 &neg_one, &root.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000066 "After setreuid(-1, root),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040067 &root.pw_uid, &neg_one, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000068 "After setreuid(root, -1),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040069 &root.pw_uid, &root.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000070 "After setreuid(root, root),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040071 &root.pw_uid, &nobody.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000072 "After setreuid(root, nobody),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040073 &root.pw_uid, &bin.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000074 "After setreuid(root, nobody),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040075 &bin.pw_uid, &root.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000076 "After setreuid(bin, root),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040077 &bin.pw_uid, &neg_one, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000078 "After setreuid(bin, -1),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040079 &bin.pw_uid, &bin.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000080 "After setreuid(bin, bin,),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040081 &bin.pw_uid, &nobody.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000082 "After setreuid(bin, nobody),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040083 &nobody.pw_uid, &bin.pw_uid, &fail, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000084 "After setreuid(nobody, bin),"},};
plars865695b2001-08-27 22:15:12 +000085
subrata_modak56207ce2009-03-23 13:35:39 +000086int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
plars865695b2001-08-27 22:15:12 +000087
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040088static void setup(void);
89static void cleanup(void);
90static void uid_verify(struct passwd *, struct passwd *, char *);
plars865695b2001-08-27 22:15:12 +000091
robbiewfa451a12003-03-27 20:52:36 +000092int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000093{
94 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020095 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000096
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040097 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080099
plars865695b2001-08-27 22:15:12 +0000100 setup();
101
plars865695b2001-08-27 22:15:12 +0000102 for (lc = 0; TEST_LOOPING(lc); lc++) {
103 int i;
104
Caspar Zhangd59a6592013-03-07 14:59:12 +0800105 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000106
107 for (i = 0; i < TST_TOTAL; i++) {
108
109 /* Set the real or effective user id */
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400110 TEST(SETREUID(cleanup, *test_data[i].real_uid,
subrata_modak56207ce2009-03-23 13:35:39 +0000111 *test_data[i].eff_uid));
plars865695b2001-08-27 22:15:12 +0000112
113 if (TEST_RETURN == *test_data[i].exp_ret) {
114 if (TEST_RETURN == neg_one) {
115 if (TEST_ERRNO != EPERM) {
116 tst_resm(TFAIL,
subrata_modak56207ce2009-03-23 13:35:39 +0000117 "setreuid(%d, %d) "
118 "did not set errno "
119 "value as expected.",
120 *test_data[i].real_uid,
121 *test_data[i].eff_uid);
plars865695b2001-08-27 22:15:12 +0000122 continue;
123 }
124 tst_resm(TPASS, "setreuid(%d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +0000125 "failed as expected.",
126 *test_data[i].real_uid,
127 *test_data[i].eff_uid);
plars865695b2001-08-27 22:15:12 +0000128 } else {
129 tst_resm(TPASS, "setreuid(%d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +0000130 "succeeded as expected.",
131 *test_data[i].real_uid,
132 *test_data[i].eff_uid);
plars865695b2001-08-27 22:15:12 +0000133 }
134 } else {
135 tst_resm(TFAIL, "setreuid(%d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +0000136 "did not return as expected.",
137 *test_data[i].real_uid,
138 *test_data[i].eff_uid);
plars865695b2001-08-27 22:15:12 +0000139 }
140
141 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000142 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200143 uid_verify(test_data[i].exp_real_usr,
144 test_data[i].exp_eff_usr,
145 test_data[i].test_msg);
plars865695b2001-08-27 22:15:12 +0000146 }
147 }
148
149 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800150 tst_exit();
plars865695b2001-08-27 22:15:12 +0000151}
152
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400153static void setup(void)
plars865695b2001-08-27 22:15:12 +0000154{
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400155 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800156
plars865695b2001-08-27 22:15:12 +0000157 tst_sig(FORK, DEF_HANDLER, cleanup);
158
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400159 if (getpwnam("nobody") == NULL)
plars865695b2001-08-27 22:15:12 +0000160 tst_brkm(TBROK, NULL, "nobody must be a valid user.");
plars865695b2001-08-27 22:15:12 +0000161
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400162 if (getpwnam("bin") == NULL)
plars38b1bc42001-09-05 17:15:30 +0000163 tst_brkm(TBROK, NULL, "bin must be a valid user.");
plars865695b2001-08-27 22:15:12 +0000164
165 root = *(getpwnam("root"));
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400166 UID16_CHECK(root.pw_uid, setreuid, cleanup);
plars865695b2001-08-27 22:15:12 +0000167
subrata_modak56207ce2009-03-23 13:35:39 +0000168 nobody = *(getpwnam("nobody"));
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400169 UID16_CHECK(nobody.pw_uid, setreuid, cleanup);
plars865695b2001-08-27 22:15:12 +0000170
plars38b1bc42001-09-05 17:15:30 +0000171 bin = *(getpwnam("bin"));
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400172 UID16_CHECK(bin.pw_uid, setreuid, cleanup);
plars865695b2001-08-27 22:15:12 +0000173
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400174 if (setuid(nobody.pw_uid) < 0)
175 tst_brkm(TBROK | TERRNO, NULL, "setuid() to nobody failed");
plars865695b2001-08-27 22:15:12 +0000176
plars865695b2001-08-27 22:15:12 +0000177 TEST_PAUSE;
178}
179
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400180static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000181{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182}
plars865695b2001-08-27 22:15:12 +0000183
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400184static void uid_verify(struct passwd *ru, struct passwd *eu, char *when)
plars865695b2001-08-27 22:15:12 +0000185{
186 if ((getuid() != ru->pw_uid) || (geteuid() != eu->pw_uid)) {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400187 tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d",
plars865695b2001-08-27 22:15:12 +0000188 when, getuid(), geteuid());
189 tst_resm(TINFO, "Expected: real uid = %d; effective uid = %d",
190 ru->pw_uid, eu->pw_uid);
191 }
Garrett Cooperbacc8492011-01-14 00:36:17 -0800192}