blob: e92d05e2010304bb7db9c3697886ac703937f15d [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +04002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +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 Kholmanskikhe0f85852013-10-21 11:25:15 +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 Kholmanskikhe0f85852013-10-21 11:25:15 +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 Kholmanskikhe0f85852013-10-21 11:25:15 +040018 * Ported by John George
plars865695b2001-08-27 22:15:12 +000019 */
20
21/*
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040022 * Test that root can change the real and effective uid to an
23 * unpriviledged user.
plars865695b2001-08-27 22:15:12 +000024 */
25
26#include <errno.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080027#include <stdlib.h>
plars865695b2001-08-27 22:15:12 +000028#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000029#include <sys/wait.h>
30
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040031#include "test.h"
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040032#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000033
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040034TCID_DEFINE(setreuid04);
35
36static uid_t neg_one = -1;
plars865695b2001-08-27 22:15:12 +000037
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040038static struct passwd nobody, root;
plars865695b2001-08-27 22:15:12 +000039
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
45struct test_data_t {
subrata_modak56207ce2009-03-23 13:35:39 +000046 uid_t *real_uid;
47 uid_t *eff_uid;
48 struct passwd *exp_real_usr;
49 struct passwd *exp_eff_usr;
50 char *test_msg;
plars865695b2001-08-27 22:15:12 +000051} test_data[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000052 {
53 &neg_one, &neg_one, &root, &root, "After setreuid(-1, nobody),"}, {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040054&nobody.pw_uid, &nobody.pw_uid, &nobody, &nobody,
subrata_modak56207ce2009-03-23 13:35:39 +000055 "After setreuid(-1, -1),"},};
plars865695b2001-08-27 22:15:12 +000056
57/*int TST_TOTAL = sizeof(test_data)/sizeof(test_data[0]);*/
58int TST_TOTAL = 2;
59
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040060static void setup(void);
61static void cleanup(void);
62static void uid_verify(struct passwd *, struct passwd *, char *);
plars865695b2001-08-27 22:15:12 +000063
robbiewfa451a12003-03-27 20:52:36 +000064int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000065{
66 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000068
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040069 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000070 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080071
plars865695b2001-08-27 22:15:12 +000072 setup();
73
plars865695b2001-08-27 22:15:12 +000074 for (lc = 0; TEST_LOOPING(lc); lc++) {
Xiaoguang Wangd84d9012014-07-28 19:51:41 +080075 int i, pid;
plars865695b2001-08-27 22:15:12 +000076
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000078
robbiewd34d5812005-07-11 22:28:09 +000079 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +000080 tst_brkm(TBROK, cleanup, "fork failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 } else if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +000082
83 for (i = 0; i < TST_TOTAL; i++) {
84
85 /* Set the real or effective user id */
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040086 TEST(SETREUID(cleanup, *test_data[i].real_uid,
subrata_modak56207ce2009-03-23 13:35:39 +000087 *test_data[i].eff_uid));
plars865695b2001-08-27 22:15:12 +000088
89 if (TEST_RETURN != -1) {
90 tst_resm(TPASS, "setreuid(%d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +000091 "succeeded as expected.",
92 *test_data[i].real_uid,
93 *test_data[i].eff_uid);
plars865695b2001-08-27 22:15:12 +000094 } else {
95 tst_resm(TFAIL, "setreuid(%d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +000096 "did not return as expected.",
97 *test_data[i].real_uid,
98 *test_data[i].eff_uid);
plars865695b2001-08-27 22:15:12 +000099 }
100
Cyril Hrubise38b9612014-06-02 17:20:57 +0200101 uid_verify(test_data[i].exp_real_usr,
102 test_data[i].exp_eff_usr,
103 test_data[i].test_msg);
plars865695b2001-08-27 22:15:12 +0000104 }
Xiaoguang Wangd84d9012014-07-28 19:51:41 +0800105 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000106 } else { /* parent */
Xiaoguang Wangd84d9012014-07-28 19:51:41 +0800107 tst_record_childstatus(cleanup, pid);
plars865695b2001-08-27 22:15:12 +0000108 }
109 }
110 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800111 tst_exit();
plars865695b2001-08-27 22:15:12 +0000112}
113
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400114static void setup(void)
plars865695b2001-08-27 22:15:12 +0000115{
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400116 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800117
plars865695b2001-08-27 22:15:12 +0000118 tst_sig(FORK, DEF_HANDLER, cleanup);
119
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400120 if (getpwnam("nobody") == NULL)
plars865695b2001-08-27 22:15:12 +0000121 tst_brkm(TBROK, NULL, "nobody must be a valid user.");
plars865695b2001-08-27 22:15:12 +0000122
123 root = *(getpwnam("root"));
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400124 UID16_CHECK(root.pw_uid, setreuid, cleanup);
plars865695b2001-08-27 22:15:12 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 nobody = *(getpwnam("nobody"));
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400127 UID16_CHECK(nobody.pw_uid, setreuid, cleanup);
plars865695b2001-08-27 22:15:12 +0000128
plars865695b2001-08-27 22:15:12 +0000129 TEST_PAUSE;
130}
131
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400132static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000133{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134}
plars865695b2001-08-27 22:15:12 +0000135
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400136static void uid_verify(struct passwd *ru, struct passwd *eu, char *when)
plars865695b2001-08-27 22:15:12 +0000137{
138 if ((getuid() != ru->pw_uid) || (geteuid() != eu->pw_uid)) {
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400139 tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d",
plars865695b2001-08-27 22:15:12 +0000140 when, getuid(), geteuid());
141 tst_resm(TINFO, "Expected: real uid = %d; effective uid = %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000142 ru->pw_uid, eu->pw_uid);
plars865695b2001-08-27 22:15:12 +0000143 }
Garrett Cooperbacc8492011-01-14 00:36:17 -0800144}