blob: 4140f8013360cbd0e750a9df5267c1edb39d0f9b [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * setresuid02.c
23 *
24 * DESCRIPTION
25 * Test that a non-root user can change the real, effective and saved
26 * uid values through the setresuid system call.
27 *
28 * ALGORITHM
29 *
30 * Setup:
31 * Setup signal handling
32 * Get user information.
33 * Pause for SIGUSER1 if option specified.
34 *
35 * Setup test values.
36 * Loop if the proper options are given.
37 * For each test set execute the system call
38 * Check that we received the expected result.
39 * Verify that the uid, euid and suid values are still correct.
40 * Cleanup:
41 * Print errno log and/or timing stats if option given.
42 *
43 * USAGE: <for command-line>
44 * setresuid02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
45 * where, -c n : Run n copies concurrently.
46 * -f : Turn off functionality Testing.
47 * -i n : Execute test n times.
48 * -I x : Execute test for x seconds.
49 * -P x : Pause for x seconds between iterations.
50 * -t : Turn on syscall timing.
51 *
52 * History
53 * 07/2001 John George
54 * -Ported
55 *
56 * Restrictions
57 * This test must be run by root.
plars47eadf02001-09-13 16:20:59 +000058 * nobody and bin must be a valid users.
plars865695b2001-08-27 22:15:12 +000059 */
60
vapierbed036a2006-05-26 06:48:55 +000061#define _GNU_SOURCE 1
plars865695b2001-08-27 22:15:12 +000062#include <pwd.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080063#include <stdlib.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080064#include "test.h"
plars865695b2001-08-27 22:15:12 +000065#include <errno.h>
66#include <sys/wait.h>
Han Pingtian75fb0572014-11-28 16:33:41 +080067#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000068
Han Pingtian75fb0572014-11-28 16:33:41 +080069TCID_DEFINE(setresuid02);
plars865695b2001-08-27 22:15:12 +000070
vapierc4e1c242006-05-26 06:39:42 +000071uid_t neg_one = -1;
plars865695b2001-08-27 22:15:12 +000072
73/* flag to tell parent if child passed or failed. */
74int flag = 0;
75
plars47eadf02001-09-13 16:20:59 +000076uid_t nobody_pw_uid, bin_pw_uid;
plars865695b2001-08-27 22:15:12 +000077char user1name[] = "nobody";
plars47eadf02001-09-13 16:20:59 +000078char user2name[] = "bin";
plars865695b2001-08-27 22:15:12 +000079
plars47eadf02001-09-13 16:20:59 +000080struct passwd nobody, bin;
plars865695b2001-08-27 22:15:12 +000081
82/*
83 * The following structure contains all test data. Each structure in the array
84 * is used for a separate test. The tests are executed in the for loop below.
85 */
86
87struct test_data_t {
subrata_modak56207ce2009-03-23 13:35:39 +000088 uid_t *real_uid;
89 uid_t *eff_uid;
90 uid_t *sav_uid;
91 struct passwd *exp_real_usr;
92 struct passwd *exp_eff_usr;
93 struct passwd *exp_sav_usr;
94 char *test_msg;
plars865695b2001-08-27 22:15:12 +000095} test_data[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000096 {
97 &neg_one, &neg_one, &bin_pw_uid, &nobody, &bin, &bin,
98 "After setresuid(-1, -1, bin),"}, {
99 &neg_one, &nobody_pw_uid, &neg_one, &nobody, &nobody, &bin,
100 "After setresuid(-1, nobody -1),"}, {
101&bin_pw_uid, &neg_one, &neg_one, &bin, &nobody, &bin,
102 "After setresuid(bin, -1 -1),"},};
plars865695b2001-08-27 22:15:12 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
plars865695b2001-08-27 22:15:12 +0000105
106void setup(void);
107void cleanup(void);
108
109void
110uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *);
111
robbiewfa451a12003-03-27 20:52:36 +0000112int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000113{
114 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200115 const char *msg;
plars865695b2001-08-27 22:15:12 +0000116
Garrett Cooper45e285d2010-11-22 12:19:25 -0800117 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +0000118 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800119
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 }
plars865695b2001-08-27 22:15:12 +0000121
plars865695b2001-08-27 22:15:12 +0000122 setup();
123
plars865695b2001-08-27 22:15:12 +0000124 for (lc = 0; TEST_LOOPING(lc); lc++) {
125 int i, pid;
126
Caspar Zhangd59a6592013-03-07 14:59:12 +0800127 /* reset tst_count in case we are looping */
128 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000129
130 /* set the appropriate ownership values */
plars47eadf02001-09-13 16:20:59 +0000131 if (setresuid(nobody_pw_uid, bin_pw_uid, nobody_pw_uid) == -1) {
plars865695b2001-08-27 22:15:12 +0000132 tst_brkm(TFAIL, cleanup, "Initial setresuid failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 }
plars865695b2001-08-27 22:15:12 +0000134
robbiewd34d5812005-07-11 22:28:09 +0000135 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000136 tst_brkm(TBROK, cleanup, "fork failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 } else if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +0000138
139 for (i = 0; i < TST_TOTAL; i++) {
140
141 /* Set the real, effective or saved user id */
Stanislav Kholmanskikh0a451532014-11-28 17:36:21 +0300142 TEST(SETRESUID(NULL, *test_data[i].real_uid,
subrata_modak56207ce2009-03-23 13:35:39 +0000143 *test_data[i].eff_uid,
144 *test_data[i].sav_uid));
plars865695b2001-08-27 22:15:12 +0000145
146 if (TEST_RETURN != -1) {
147 tst_resm(TPASS, "setresuid(%d, %d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +0000148 "succeeded as expected.",
149 *test_data[i].real_uid,
150 *test_data[i].eff_uid,
151 *test_data[i].sav_uid);
plars865695b2001-08-27 22:15:12 +0000152 } else {
153 tst_resm(TFAIL, "setresuid(%d, %d, %d) "
subrata_modak56207ce2009-03-23 13:35:39 +0000154 "did not return as expected.",
155 *test_data[i].real_uid,
156 *test_data[i].eff_uid,
157 *test_data[i].sav_uid);
158 flag = -1;
plars865695b2001-08-27 22:15:12 +0000159 }
160
Cyril Hrubise38b9612014-06-02 17:20:57 +0200161 uid_verify(test_data[i].exp_real_usr,
162 test_data[i].exp_eff_usr,
163 test_data[i].exp_sav_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 */
Stanislav Kholmanskikh0a451532014-11-28 17:36:21 +0300168 tst_record_childstatus(cleanup, pid);
plars865695b2001-08-27 22:15:12 +0000169 }
170 }
171 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800172 tst_exit();
plars865695b2001-08-27 22:15:12 +0000173}
174
175/*
176 * setup()
177 * performs all ONE TIME setup for this test
178 */
subrata_modak56207ce2009-03-23 13:35:39 +0000179void setup(void)
plars865695b2001-08-27 22:15:12 +0000180{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200181 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800182
plars865695b2001-08-27 22:15:12 +0000183 tst_sig(FORK, DEF_HANDLER, cleanup);
184
185 if (getpwnam("nobody") == NULL) {
186 tst_brkm(TBROK, NULL, "nobody must be a valid user.");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 }
plars865695b2001-08-27 22:15:12 +0000188
plars47eadf02001-09-13 16:20:59 +0000189 if (getpwnam("bin") == NULL) {
190 tst_brkm(TBROK, NULL, "bin must be a valid user.");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800191 }
plars865695b2001-08-27 22:15:12 +0000192
subrata_modak56207ce2009-03-23 13:35:39 +0000193 nobody = *(getpwnam("nobody"));
Han Pingtian75fb0572014-11-28 16:33:41 +0800194 UID16_CHECK((nobody_pw_uid = nobody.pw_uid), "setresuid", cleanup)
plars865695b2001-08-27 22:15:12 +0000195
subrata_modak56207ce2009-03-23 13:35:39 +0000196 bin = *(getpwnam("bin"));
Han Pingtian75fb0572014-11-28 16:33:41 +0800197 UID16_CHECK((bin_pw_uid = bin.pw_uid), "setresuid", cleanup)
plars865695b2001-08-27 22:15:12 +0000198
199 /* Pause if that option was specified
200 * TEST_PAUSE contains the code to fork the test with the -i option.
201 * You want to make sure you do this before you create your temporary
202 * directory.
203 */
204 TEST_PAUSE;
205}
206
207/*
208 * cleanup()
209 * performs all ONE TIME cleanup for this test at
210 * completion or premature exit
211 */
subrata_modak56207ce2009-03-23 13:35:39 +0000212void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000213{
plars865695b2001-08-27 22:15:12 +0000214
Wanlong Gao354ebb42012-12-07 10:10:04 +0800215}
plars865695b2001-08-27 22:15:12 +0000216
217void
218uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *when)
219{
220 uid_t cur_ru, cur_eu, cur_su;
221 if (getresuid(&cur_ru, &cur_eu, &cur_su) != 0) {
222 flag = -1;
223 tst_brkm(TBROK, cleanup, "Set getresuid() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800224 }
plars865695b2001-08-27 22:15:12 +0000225 if ((cur_ru != ru->pw_uid) || (cur_eu != eu->pw_uid) || (cur_su !=
subrata_modak56207ce2009-03-23 13:35:39 +0000226 su->pw_uid)) {
plars865695b2001-08-27 22:15:12 +0000227 tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d; "
subrata_modak56207ce2009-03-23 13:35:39 +0000228 "saved uid = %d", when, cur_ru, cur_eu, cur_su);
plars865695b2001-08-27 22:15:12 +0000229 tst_resm(TINFO, "Expected: real uid = %d, effective uid = %d "
subrata_modak56207ce2009-03-23 13:35:39 +0000230 "saved uid = %d", ru->pw_uid, eu->pw_uid, su->pw_uid);
plars865695b2001-08-27 22:15:12 +0000231 flag = -1;
232 } else {
233 tst_resm(TINFO, "real uid = %d, effective uid = %d, and "
subrata_modak56207ce2009-03-23 13:35:39 +0000234 "saved uid = %d as expected", cur_ru, cur_eu, cur_su);
plars865695b2001-08-27 22:15:12 +0000235 }
Garrett Cooperbacc8492011-01-14 00:36:17 -0800236}