xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getresuid/getresuid02.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: getresuid02
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 setreuid() to change
26  *  the effective/saved uids to that of specified user.
27  * $
28  * Expected Result:
29  *  getresuid() should return with 0 value and the real/effective/saved
30  *  user ids should match the expected values.
31  *
32  * Algorithm:
33  *  Setup:
34  *   Setup signal handling.
35  *   Pause for SIGUSR1 if option specified.
36  *
37  *  Test:
38  *   Loop if the proper options are given.
39  *   Execute system call
40  *   Check return code, if system call failed (return=-1)
41  *   	Log the errno and Issue a FAIL message.
42  *   Otherwise,
43  *   	Verify the Functionality of system call
44  *      if successful,
45  *      	Issue Functionality-Pass message.
46  *      Otherwise,
47  *		Issue Functionality-Fail message.
48  *  Cleanup:
49  *   Print errno log and/or timing stats if options given
50  *
51  * Usage:  <for command-line>
52  *  getresuid02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
53  *     where,  -c n : Run n copies concurrently.
54  *             -f   : Turn off functionality Testing.
55  *	       -i n : Execute test n times.
56  *	       -I x : Execute test for x seconds.
57  *	       -P x : Pause for x seconds between iterations.
58  *	       -t   : Turn on syscall timing.
59  *
60  * HISTORY
61  *	07/2001 Ported by Wayne Boyer
62  *
63  * RESTRICTIONS:
64  *  This test should be run by 'super-user' (root) only.
65  *
66  */
67 
68 #include <stdio.h>
69 #include <unistd.h>
70 #include <sys/types.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #include <string.h>
74 #include <signal.h>
75 #include <pwd.h>
76 
77 #include "test.h"
78 
79 /*
80  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
81  * rewriting all tests to the new API.
82  */
83 #include "compat_16.h"
84 
85 #define LTPUSER		"nobody"
86 
87 char *TCID = "getresuid02";
88 int TST_TOTAL = 1;
89 UID_T pr_uid, pe_uid, ps_uid;	/* calling process real/effective/saved uid */
90 
91 void setup();			/* Main setup function of test */
92 void cleanup();			/* cleanup function for the test */
93 
main(int ac,char ** av)94 int main(int ac, char **av)
95 {
96 	int lc;
97 	UID_T real_uid,		/* real/eff./saved user id from getresuid() */
98 	 eff_uid, sav_uid;
99 
100 	tst_parse_opts(ac, av, NULL, NULL);
101 
102 	setup();
103 
104 	for (lc = 0; TEST_LOOPING(lc); lc++) {
105 
106 		tst_count = 0;
107 
108 		/*
109 		 * Call getresuid() to get the real/effective/saved
110 		 * user id's of the calling process after
111 		 * setreuid() in setup.
112 		 */
113 		TEST(GETRESUID(cleanup, &real_uid, &eff_uid, &sav_uid));
114 
115 		if (TEST_RETURN == -1) {
116 			tst_resm(TFAIL, "getresuid() Failed, errno=%d : %s",
117 				 TEST_ERRNO, strerror(TEST_ERRNO));
118 			continue;
119 		}
120 
121 		if ((real_uid != pr_uid) || (eff_uid != pe_uid) ||
122 		    (sav_uid != ps_uid)) {
123 			tst_resm(TFAIL, "real:%d, effective:%d, "
124 				 "saved-user:%d ids differ",
125 				 real_uid, eff_uid, sav_uid);
126 		} else {
127 			tst_resm(TPASS, "Functionality of getresuid() "
128 				 "successful");
129 		}
130 	}
131 
132 	cleanup();
133 	tst_exit();
134 }
135 
136 /*
137  * void
138  * setup() - performs all ONE TIME setup for this test.
139  *	     Get the real/effective/saved user id of the calling process.
140  */
setup(void)141 void setup(void)
142 {
143 	struct passwd *user_id;	/* passwd struct for test user */
144 
145 	tst_require_root();
146 
147 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
148 
149 	TEST_PAUSE;
150 
151 	/* Real user-id of the calling process */
152 	pr_uid = getuid();
153 
154 	/* Get effective uid of LTPUSER user from passwd file */
155 	if ((user_id = getpwnam(LTPUSER)) == NULL) {
156 		tst_brkm(TBROK, cleanup, "getpwnam(%s) Failed", LTPUSER);
157 	}
158 
159 	/* Effective user-id of the test-user LTPUSER */
160 	pe_uid = user_id->pw_uid;
161 
162 	/* Saved user-id of the test-user LTPUSER */
163 	ps_uid = user_id->pw_uid;
164 
165 	/*
166 	 * Set the effective user-id of the process to that of
167 	 * test user LTPUSER
168 	 * The real user id remains same as of caller.
169 	 */
170 	if (setreuid(-1, ps_uid) < 0) {
171 		tst_brkm(TBROK, cleanup,
172 			 "setreuid(-1, %d) Fails, errno:%d : %s",
173 			 ps_uid, errno, strerror(errno));
174 	}
175 }
176 
177 /*
178  * void
179  * cleanup() - performs all ONE TIME cleanup for this test at
180  *             completion or premature exit.
181  *	       Restore the test process uid to root.
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