1 /*
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
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.
27 * $
28 * 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,
44 * Verify the Functionality of system call
45 * 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"
79 #include "compat_16.h"
80
81 char *TCID = "getresuid03";
82 int TST_TOTAL = 1;
83 UID_T pr_uid, pe_uid, ps_uid; /* calling process real/effective/saved uid */
84
85 void setup(); /* Main setup function of test */
86 void cleanup(); /* cleanup function for the test */
87
main(int ac,char ** av)88 int main(int ac, char **av)
89 {
90 int lc;
91 UID_T real_uid, /* real/eff./saved user id from getresuid() */
92 eff_uid, sav_uid;
93
94 tst_parse_opts(ac, av, NULL, NULL);
95
96 setup();
97
98 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
100 tst_count = 0;
101
102 /*
103 * Call getresuid() to get the real/effective/saved
104 * user id's of the calling process after
105 * setreuid() in setup.
106 */
107 TEST(GETRESUID(cleanup, &real_uid, &eff_uid, &sav_uid));
108
109 if (TEST_RETURN == -1) {
110 tst_resm(TFAIL, "getresuid() Failed, errno=%d : %s",
111 TEST_ERRNO, strerror(TEST_ERRNO));
112 continue;
113 }
114
115 if ((real_uid != pr_uid) || (eff_uid != pe_uid) ||
116 (sav_uid != ps_uid)) {
117 tst_resm(TFAIL, "real:%d, effective:%d, "
118 "saved-user:%d ids differ",
119 real_uid, eff_uid, sav_uid);
120 } else {
121 tst_resm(TPASS, "Functionality of getresuid() "
122 "successful");
123 }
124 }
125
126 cleanup();
127 tst_exit();
128 }
129
130 /*
131 * setup() - performs all ONE TIME setup for this test.
132 * Make sure test process uid is root.
133 * Get the real/effective/saved user id of the calling process.
134 * Get the user info. of test user "ltpuser1" from /etc/passwd file.
135 * Set the eff. user id of test process to that of "ltpuser1" user.
136 */
setup(void)137 void setup(void)
138 {
139 struct passwd *user_id; /* passwd struct for test user */
140
141 tst_require_root();
142
143 tst_sig(NOFORK, DEF_HANDLER, cleanup);
144
145 TEST_PAUSE;
146
147 /* Real user-id of the calling process */
148 pr_uid = getuid();
149
150 /* Saved user-id of the calling process. */
151 ps_uid = geteuid();
152
153 /* Get effective uid of "ltpuser1" user from passwd file */
154 if ((user_id = getpwnam("nobody")) == NULL) {
155 tst_brkm(TBROK, cleanup,
156 "getpwnam(nobody) Failed, errno=%d", errno);
157 }
158
159 /* Effective user-id of the test-user "ltpuser1" */
160 pe_uid = user_id->pw_uid;
161
162 /*
163 * Set the effective user-id of the process to that of
164 * test user "ltpuser1".
165 * The real/saved user id remains same as of caller.
166 */
167 if (setresuid(-1, pe_uid, -1) < 0) {
168 tst_brkm(TBROK, cleanup,
169 "setresuid(-1, %d, -1) Fails, errno:%d : %s",
170 ps_uid, errno, strerror(errno));
171 }
172 }
173
174 /*
175 * cleanup() - performs all ONE TIME cleanup for this test at
176 * completion or premature exit.
177 */
cleanup(void)178 void cleanup(void)
179 {
180
181 /* Reset the effective/saved uid of the calling process */
182 if (setreuid(-1, pr_uid) < 0) {
183 tst_brkm(TBROK, NULL, "resetting process effective uid failed");
184 }
185
186 }
187