blob: c23c53fd935e7b9a6a1ded8c0bad036aa30beadb [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 * Test Name: getresuid03
22 *
23 * Test Description:
24 * Verify that getresuid() will be successful to get the real, effective
25 * and saved user ids after calling process invokes setresuid() to change
26 * the effective uid to that of specified user.
subrata_modak56207ce2009-03-23 13:35:39 +000027 * $
plars865695b2001-08-27 22:15:12 +000028 * Expected Result:
29 * getresuid() should return with 0 value and the effective user id
30 * should match the euid of specified user, real/saved user ids should
31 * remain unchanged.
32 *
33 * Algorithm:
34 * Setup:
35 * Setup signal handling.
36 * Pause for SIGUSR1 if option specified.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Execute system call
41 * Check return code, if system call failed (return=-1)
42 * Log the errno and Issue a FAIL message.
43 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * if successful,
46 * Issue Functionality-Pass message.
47 * Otherwise,
48 * Issue Functionality-Fail message.
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 *
52 * Usage: <for command-line>
53 * getresuid03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54 * where, -c n : Run n copies concurrently.
55 * -f : Turn off functionality Testing.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * RESTRICTIONS:
65 * This test should be run by 'super-user' (root) only.
66 *
67 */
68
69#include <stdio.h>
70#include <unistd.h>
71#include <sys/types.h>
72#include <errno.h>
73#include <fcntl.h>
74#include <string.h>
75#include <signal.h>
76#include <pwd.h>
77
78#include "test.h"
plars865695b2001-08-27 22:15:12 +000079
subrata_modak56207ce2009-03-23 13:35:39 +000080extern int getresuid(uid_t *, uid_t *, uid_t *);
robbiew96d23372003-03-26 20:40:04 +000081extern int setresuid(uid_t, uid_t, uid_t);
82
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020083char *TCID = "getresuid03";
84int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000085uid_t pr_uid, pe_uid, ps_uid; /* calling process real/effective/saved uid */
86
87void setup(); /* Main setup function of test */
88void cleanup(); /* cleanup function for the test */
89
subrata_modak56207ce2009-03-23 13:35:39 +000090int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000091{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020092 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020093 const char *msg;
plars865695b2001-08-27 22:15:12 +000094 uid_t real_uid, /* real/eff./saved user id from getresuid() */
subrata_modak56207ce2009-03-23 13:35:39 +000095 eff_uid, sav_uid;
plars865695b2001-08-27 22:15:12 +000096
Garrett Cooper45e285d2010-11-22 12:19:25 -080097 msg = parse_opts(ac, av, NULL, NULL);
98 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +000099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800100
plars865695b2001-08-27 22:15:12 +0000101 }
102
plars865695b2001-08-27 22:15:12 +0000103 setup();
104
plars865695b2001-08-27 22:15:12 +0000105 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800106
Caspar Zhangd59a6592013-03-07 14:59:12 +0800107 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak4bb656a2009-02-26 12:02:09 +0000109 /*
plars865695b2001-08-27 22:15:12 +0000110 * Call getresuid() to get the real/effective/saved
subrata_modak4bb656a2009-02-26 12:02:09 +0000111 * user id's of the calling process after
plars865695b2001-08-27 22:15:12 +0000112 * setreuid() in setup.
113 */
114 TEST(getresuid(&real_uid, &eff_uid, &sav_uid));
115
plars865695b2001-08-27 22:15:12 +0000116 if (TEST_RETURN == -1) {
117 tst_resm(TFAIL, "getresuid() Failed, errno=%d : %s",
118 TEST_ERRNO, strerror(TEST_ERRNO));
119 continue;
120 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121
122 if ((real_uid != pr_uid) || (eff_uid != pe_uid) ||
123 (sav_uid != ps_uid)) {
124 tst_resm(TFAIL, "real:%d, effective:%d, "
125 "saved-user:%d ids differ",
126 real_uid, eff_uid, sav_uid);
plars865695b2001-08-27 22:15:12 +0000127 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200128 tst_resm(TPASS, "Functionality of getresuid() "
129 "successful");
plars865695b2001-08-27 22:15:12 +0000130 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800131 }
plars865695b2001-08-27 22:15:12 +0000132
plars865695b2001-08-27 22:15:12 +0000133 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800134 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800135}
plars865695b2001-08-27 22:15:12 +0000136
137/*
138 * setup() - performs all ONE TIME setup for this test.
139 * Make sure test process uid is root.
140 * Get the real/effective/saved user id of the calling process.
141 * Get the user info. of test user "ltpuser1" from /etc/passwd file.
142 * Set the eff. user id of test process to that of "ltpuser1" user.
143 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400144void setup(void)
plars865695b2001-08-27 22:15:12 +0000145{
subrata_modak56207ce2009-03-23 13:35:39 +0000146 struct passwd *user_id; /* passwd struct for test user */
plars865695b2001-08-27 22:15:12 +0000147
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200148 tst_require_root(NULL);
149
plars865695b2001-08-27 22:15:12 +0000150 tst_sig(NOFORK, DEF_HANDLER, cleanup);
151
plars865695b2001-08-27 22:15:12 +0000152 TEST_PAUSE;
153
plars865695b2001-08-27 22:15:12 +0000154 /* Real user-id of the calling process */
155 pr_uid = getuid();
156
157 /* Saved user-id of the calling process. */
158 ps_uid = geteuid();
159
160 /* Get effective uid of "ltpuser1" user from passwd file */
161 if ((user_id = getpwnam("nobody")) == NULL) {
162 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000163 "getpwnam(nobody) Failed, errno=%d", errno);
plars865695b2001-08-27 22:15:12 +0000164 }
subrata_modak56207ce2009-03-23 13:35:39 +0000165
plars865695b2001-08-27 22:15:12 +0000166 /* Effective user-id of the test-user "ltpuser1" */
167 pe_uid = user_id->pw_uid;
168
169 /*
170 * Set the effective user-id of the process to that of
171 * test user "ltpuser1".
172 * The real/saved user id remains same as of caller.
173 */
174 if (setresuid(-1, pe_uid, -1) < 0) {
175 tst_brkm(TBROK, cleanup,
176 "setresuid(-1, %d, -1) Fails, errno:%d : %s",
177 ps_uid, errno, strerror(errno));
178 }
179}
180
plars865695b2001-08-27 22:15:12 +0000181/*
182 * cleanup() - performs all ONE TIME cleanup for this test at
183 * completion or premature exit.
184 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400185void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000186{
plars865695b2001-08-27 22:15:12 +0000187
188 /* Reset the effective/saved uid of the calling process */
189 if (setreuid(-1, pr_uid) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000190 tst_brkm(TBROK, NULL, "resetting process effective uid failed");
plars865695b2001-08-27 22:15:12 +0000191 }
192
Chris Dearmanec6edca2012-10-17 19:54:01 -0700193}