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