blob: e94894d0a2ddab5f5c5850f2dad07f9676ebdb60 [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 * setresuid01.c
23 *
24 * DESCRIPTION
25 * Test setresuid() when executed by root.
26 *
27 * ALGORITHM
28 *
29 * Setup:
30 * Setup signal handling
31 * Get user information.
32 * Pause for SIGUSER1 if option specified.
33 * Setup test values.
34 * Loop if the proper options are given.
35 * For each test execute the system call
36 * Check return code, if system call failed (return=-1)
37 * Log the errno and Issue a FAIL message.
38 * Otherwise,
39 * Verify the Functionality of system call
40 * if successful,
41 * Issue Functionality-Pass message.
42 * Otherwise,
43 * Issue Functionality-Fail message.
44 * Cleanup:
45 * Print errno log and/or timing stats if options given.
46 *
47 * USAGE: <for command-line>
48 * setresuid01 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
49 * where, -c n : Run n copies concurrently.
50 * -e : Turn on errno logging.
51 * -f : Turn off functionality Testing.
52 * -i n : Execute test n times.
53 * -I x : Execute test for x seconds.
54 * -P x : Pause for x seconds between iterations.
55 * -t : Turn on syscall timing.
56 * History
57 * 07/2001 John George
58 * -Ported
59 *
60 * Restrictions
61 * This test must be ran as root.
plars47eadf02001-09-13 16:20:59 +000062 * nobody and bin must be valid users.
plars865695b2001-08-27 22:15:12 +000063 */
64
vapierbed036a2006-05-26 06:48:55 +000065#define _GNU_SOURCE 1
plars865695b2001-08-27 22:15:12 +000066#include <pwd.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080067#include <stdlib.h>
plars865695b2001-08-27 22:15:12 +000068#include <string.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080069#include "test.h"
plars865695b2001-08-27 22:15:12 +000070#include <errno.h>
Han Pingtian75fb0572014-11-28 16:33:41 +080071#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000072
Han Pingtian75fb0572014-11-28 16:33:41 +080073TCID_DEFINE(setresuid01);
robbiewfa451a12003-03-27 20:52:36 +000074
plars47eadf02001-09-13 16:20:59 +000075uid_t nobody_pw_uid, root_pw_uid, bin_pw_uid;
vapierc4e1c242006-05-26 06:39:42 +000076uid_t neg_one = -1;
plars865695b2001-08-27 22:15:12 +000077
plars47eadf02001-09-13 16:20:59 +000078struct passwd nobody, bin, root;
plars865695b2001-08-27 22:15:12 +000079
80/*
81 * The following structure contains all test data. Each structure in the array
82 * is used for a separate test. The tests are executed in the for loop below.
83 */
84
85struct test_data_t {
subrata_modak56207ce2009-03-23 13:35:39 +000086 uid_t *real_uid;
87 uid_t *eff_uid;
88 uid_t *sav_uid;
89 struct passwd *exp_real_usr;
90 struct passwd *exp_eff_usr;
91 struct passwd *exp_sav_usr;
92 char *test_msg;
plars865695b2001-08-27 22:15:12 +000093} test_data[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000094 {
95 &neg_one, &neg_one, &neg_one, &root, &root, &root,
96 "After setresuid(-1, -1, -1),"}, {
97 &neg_one, &neg_one, &nobody_pw_uid, &root, &root, &nobody,
98 "After setresuid(-1, -1, nobody),"}, {
99 &neg_one, &bin_pw_uid, &neg_one, &root, &bin, &nobody,
100 "After setresuid(-1, bin, -1),"}, {
101 &neg_one, &neg_one, &root_pw_uid, &root, &bin, &root,
102 "After setresuid(-1, -1, root),"}, {
103 &neg_one, &neg_one, &bin_pw_uid, &root, &bin, &bin,
104 "After setresuid(-1, -1, bin),"}, {
105 &neg_one, &root_pw_uid, &neg_one, &root, &root, &bin,
106 "After setresuid(-1, root, -1),"}, {
107 &nobody_pw_uid, &neg_one, &neg_one, &nobody, &root, &bin,
108 "After setresuid(nobody, -1, -1)"}, {
109 &neg_one, &root_pw_uid, &neg_one, &nobody, &root, &bin,
110 "After setresuid(-1, root, -1),"}, {
111&root_pw_uid, &neg_one, &root_pw_uid, &root, &root, &root,
112 "After setresuid(root, -1, -1),"},};
plars865695b2001-08-27 22:15:12 +0000113
subrata_modak56207ce2009-03-23 13:35:39 +0000114int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
plars865695b2001-08-27 22:15:12 +0000115
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200116void setup(void);
117void cleanup(void);
plars865695b2001-08-27 22:15:12 +0000118
119void
120uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *when);
121
robbiewfa451a12003-03-27 20:52:36 +0000122int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000123{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200124 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200125 const char *msg;
plars865695b2001-08-27 22:15:12 +0000126
Garrett Cooper45e285d2010-11-22 12:19:25 -0800127 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +0000128 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800129
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 }
plars865695b2001-08-27 22:15:12 +0000131
plars865695b2001-08-27 22:15:12 +0000132 setup();
133
plars865695b2001-08-27 22:15:12 +0000134 for (lc = 0; TEST_LOOPING(lc); lc++) {
135 int i;
136
Caspar Zhangd59a6592013-03-07 14:59:12 +0800137 /* reset tst_count in case we are looping */
138 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000139
140 for (i = 0; i < TST_TOTAL; i++) {
141 /* Set the real, effective or user id */
Han Pingtian75fb0572014-11-28 16:33:41 +0800142 TEST(SETRESUID(cleanup, *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) {
plars865695b2001-08-27 22:15:12 +0000147 tst_resm(TFAIL, "setresuid(%d, %d, %d) failed",
subrata_modak56207ce2009-03-23 13:35:39 +0000148 *test_data[i].real_uid,
149 *test_data[i].eff_uid,
150 *test_data[i].sav_uid);
plars865695b2001-08-27 22:15:12 +0000151 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200152 uid_verify(test_data[i].exp_real_usr,
153 test_data[i].exp_eff_usr,
154 test_data[i].exp_sav_usr,
155 test_data[i].test_msg);
plars865695b2001-08-27 22:15:12 +0000156 }
157 }
158 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200159
plars865695b2001-08-27 22:15:12 +0000160 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800161 tst_exit();
plars865695b2001-08-27 22:15:12 +0000162}
163
164/*
165 * setup()
166 * performs all ONE TIME setup for this test
167 */
subrata_modak56207ce2009-03-23 13:35:39 +0000168void setup(void)
plars865695b2001-08-27 22:15:12 +0000169{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200170 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800171
plars865695b2001-08-27 22:15:12 +0000172 tst_sig(FORK, DEF_HANDLER, cleanup);
173
plars865695b2001-08-27 22:15:12 +0000174 if (getpwnam("nobody") == NULL) {
175 tst_brkm(TBROK, NULL, "nobody must be a valid user.");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800176 }
plars865695b2001-08-27 22:15:12 +0000177
plars47eadf02001-09-13 16:20:59 +0000178 if (getpwnam("bin") == NULL) {
179 tst_brkm(TBROK, NULL, "bin must be a valid user.");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 }
plars865695b2001-08-27 22:15:12 +0000181
plars865695b2001-08-27 22:15:12 +0000182 root = *(getpwnam("root"));
Han Pingtian75fb0572014-11-28 16:33:41 +0800183 UID16_CHECK((root_pw_uid = root.pw_uid), "setresuid", cleanup)
plars865695b2001-08-27 22:15:12 +0000184
185 nobody = *(getpwnam("nobody"));
Han Pingtian75fb0572014-11-28 16:33:41 +0800186 UID16_CHECK((nobody_pw_uid = nobody.pw_uid), "setresuid", cleanup)
plars865695b2001-08-27 22:15:12 +0000187
plars47eadf02001-09-13 16:20:59 +0000188 bin = *(getpwnam("bin"));
Han Pingtian75fb0572014-11-28 16:33:41 +0800189 UID16_CHECK((bin_pw_uid = bin.pw_uid), "setresuid", cleanup)
plars865695b2001-08-27 22:15:12 +0000190
191 /* Pause if that option was specified
192 * TEST_PAUSE contains the code to fork the test with the -c option.
193 */
194 TEST_PAUSE;
195}
196
197/*
198 * cleanup()
199 * performs all ONE TIME cleanup for this test at
200 * completion or premature exit
201 */
subrata_modak56207ce2009-03-23 13:35:39 +0000202void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000203{
plars865695b2001-08-27 22:15:12 +0000204
Wanlong Gao354ebb42012-12-07 10:10:04 +0800205}
plars865695b2001-08-27 22:15:12 +0000206
207void
208uid_verify(struct passwd *ru, struct passwd *eu, struct passwd *su, char *when)
209{
210 uid_t cur_ru, cur_eu, cur_su;
211 if (getresuid(&cur_ru, &cur_eu, &cur_su) != 0) {
212 tst_brkm(TBROK, cleanup, "Set getresuid() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 }
plars865695b2001-08-27 22:15:12 +0000214 if ((cur_ru != ru->pw_uid) || (cur_eu != eu->pw_uid) || (cur_su !=
subrata_modak56207ce2009-03-23 13:35:39 +0000215 su->pw_uid)) {
plars865695b2001-08-27 22:15:12 +0000216 tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d; "
subrata_modak56207ce2009-03-23 13:35:39 +0000217 "saved uid = %d", when, cur_ru, cur_eu, cur_su);
plars865695b2001-08-27 22:15:12 +0000218 tst_resm(TINFO, "Expected: real uid = %d, effective uid = %d "
subrata_modak56207ce2009-03-23 13:35:39 +0000219 "saved uid = %d", ru->pw_uid, eu->pw_uid, su->pw_uid);
plars865695b2001-08-27 22:15:12 +0000220 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000221 tst_resm(TPASS,
222 "real uid = %d, effective uid = %d, and saved uid = "
223 "%d as expected", cur_ru, cur_eu, cur_su);
plars865695b2001-08-27 22:15:12 +0000224 }
Garrett Cooperbacc8492011-01-14 00:36:17 -0800225}