xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getresgid/getresgid03.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: getresgid03
22  *
23  * Test Description:
24  *  Verify that getresgid() will be successful to get the real, effective
25  *  and saved user ids after calling process invokes setresgid() to change
26  *  the effective gid to that of specified user.
27  *
28  * Expected Result:
29  *  getresgid() should return with 0 value and the effective user id
30  *  should match the egid 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  *  getresgid03 [-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 #define _GNU_SOURCE 1
69 
70 #include <stdio.h>
71 #include <unistd.h>
72 #include <sys/types.h>
73 #include <errno.h>
74 #include <fcntl.h>
75 #include <string.h>
76 #include <signal.h>
77 #include <pwd.h>
78 
79 #include "test.h"
80 
81 /*
82  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
83  * rewriting all tests to the new API.
84  */
85 #include "compat_16.h"
86 
87 char *TCID = "getresgid03";
88 int TST_TOTAL = 1;
89 GID_T pr_gid, pe_gid, ps_gid;	/* calling process real/effective/saved gid */
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 	GID_T real_gid,		/* real/eff./saved user id from getresgid() */
98 	 eff_gid, sav_gid;
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 getresgid() to get the real/effective/saved
110 		 * user id's of the calling process after
111 		 * setregid() in setup.
112 		 */
113 		TEST(GETRESGID(cleanup, &real_gid, &eff_gid, &sav_gid));
114 
115 		if (TEST_RETURN == -1) {
116 			tst_resm(TFAIL, "getresgid() Failed, errno=%d : %s",
117 				 TEST_ERRNO, strerror(TEST_ERRNO));
118 			continue;
119 		}
120 		/*
121 		 * Verify the real/effective/saved gid
122 		 * values returned by getresgid with the
123 		 * expected values.
124 		 */
125 		if ((real_gid != pr_gid) || (eff_gid != pe_gid) ||
126 		    (sav_gid != ps_gid)) {
127 			tst_resm(TFAIL, "real:%d, effective:%d, "
128 				 "saved-user:%d ids differ",
129 				 real_gid, eff_gid, sav_gid);
130 		} else {
131 			tst_resm(TPASS, "Functionality of getresgid() "
132 				 "successful");
133 		}
134 	}
135 
136 	cleanup();
137 	tst_exit();
138 }
139 
140 /*
141  * setup() - performs all ONE TIME setup for this test.
142  *	     Make sure test process gid is root.
143  *	     Get the real/effective/saved user id of the calling process.
144  *	     Get the user info. of test user "ltpuser1" from /etc/passwd file.
145  *	     Set the eff. user id of test process to that of "ltpuser1" user.
146  */
setup(void)147 void setup(void)
148 {
149 	struct passwd *user_id;	/* passwd struct for test user */
150 
151 	tst_require_root();
152 
153 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
154 
155 	TEST_PAUSE;
156 
157 	/* Real user-id of the calling process */
158 	pr_gid = getgid();
159 
160 	/* Saved user-id of the calling process. */
161 	ps_gid = getegid();
162 
163 	/* Get effective gid of "ltpuser1" user from passwd file */
164 	if ((user_id = getpwnam("nobody")) == NULL) {
165 		tst_brkm(TBROK, cleanup,
166 			 "getpwnam(nobody) Failed, errno=%d", errno);
167 	}
168 
169 	/* Effective user-id of the test-user "ltpuser1" */
170 	pe_gid = user_id->pw_gid;
171 
172 	/*
173 	 * Set the effective user-id of the process to that of
174 	 * test user "ltpuser1".
175 	 * The real/saved  user id remains same as of caller.
176 	 */
177 	if (setresgid(-1, pe_gid, -1) < 0) {
178 		tst_brkm(TBROK, cleanup,
179 			 "setresgid(-1, %d, -1) Fails, errno:%d : %s",
180 			 ps_gid, errno, strerror(errno));
181 	}
182 }
183 
184 /*
185  * cleanup() - performs all ONE TIME cleanup for this test at
186  *             completion or premature exit.
187  */
cleanup(void)188 void cleanup(void)
189 {
190 
191 	/* Reset the effective/saved gid of the calling process */
192 	if (setregid(-1, pr_gid) < 0) {
193 		tst_brkm(TBROK, NULL, "resetting process effective gid failed");
194 	}
195 
196 }
197