• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: mknod08
22  *
23  * Test Description:
24  *  Verify that mknod(2) succeeds when used to create a filesystem
25  *  node on a directory without set group-ID bit set. The node created
26  *  should not have set group-ID bit set and its gid should be equal to that
27  *  of its parent directory.
28  *
29  * Expected Result:
30  *  mknod() should return value 0 on success and node created should not
31  *  have set group-ID bit set.
32  *
33  * Algorithm:
34  *  Setup:
35  *   Setup signal handling.
36  *   Create temporary directory.
37  *   Pause for SIGUSR1 if option specified.
38  *
39  *  Test:
40  *   Loop if the proper options are given.
41  *   Execute system call
42  *   Check return code, if system call failed (return=-1)
43  *	Log the errno and Issue a FAIL message.
44  *   Otherwise,
45  *	Verify the Functionality of system call
46  *      if successful,
47  *		Issue Functionality-Pass message.
48  *      Otherwise,
49  *		Issue Functionality-Fail message.
50  *  Cleanup:
51  *   Print errno log and/or timing stats if options given
52  *   Delete the temporary directory created.
53  *
54  * Usage:  <for command-line>
55  *  mknod08 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
56  *     where,  -c n : Run n copies concurrently.
57  *             -e   : Turn on errno logging.
58  *             -f   : Turn off functionality Testing.
59  *	       -i n : Execute test n times.
60  *	       -I x : Execute test for x seconds.
61  *	       -P x : Pause for x seconds between iterations.
62  *	       -t   : Turn on syscall timing.
63  *
64  * HISTORY
65  *	07/2001 Ported by Wayne Boyer
66  *
67  * RESTRICTIONS:
68  *  This test should be run by 'super-user' (root) only.
69  *
70  */
71 
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75 #include <errno.h>
76 #include <string.h>
77 #include <signal.h>
78 #include <pwd.h>
79 #include <sys/types.h>
80 #include <sys/stat.h>
81 
82 #include "test.h"
83 
84 #define LTPUSER		"nobody"
85 #define MODE_RWX	S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
86 #define DIR_TEMP	"testdir_1"
87 #define TNODE		"tnode_%d"
88 
89 struct stat buf;		/* struct. to hold stat(2) o/p contents */
90 struct passwd *user1;		/* struct. to hold getpwnam(3) o/p contents */
91 
92 char *TCID = "mknod08";
93 int TST_TOTAL = 1;
94 char node_name[PATH_MAX];	/* buffer to hold node name created */
95 
96 gid_t group1_gid, group2_gid, mygid;	/* user and process group id's */
97 uid_t save_myuid, user1_uid;	/* user and process user id's */
98 pid_t mypid;			/* process id */
99 
100 void setup();			/* setup function for the test */
101 void cleanup();			/* cleanup function for the test */
102 
main(int ac,char ** av)103 int main(int ac, char **av)
104 {
105 	int lc;
106 	int fflag;
107 
108 	tst_parse_opts(ac, av, NULL, NULL);
109 
110 	setup();
111 
112 	for (lc = 0; TEST_LOOPING(lc); lc++) {
113 
114 		tst_count = 0;
115 
116 		/*
117 		 * Call mknod() to creat a node on a directory without
118 		 * set group-ID (sgid) bit set.
119 		 */
120 		TEST(mknod(node_name, MODE_RWX, 0));
121 
122 		/* Check return code from mknod(2) */
123 		if (TEST_RETURN == -1) {
124 			tst_resm(TFAIL,
125 				 "mknod(%s, %#o, 0)  failed, errno=%d : %s",
126 				 node_name, MODE_RWX, TEST_ERRNO,
127 				 strerror(TEST_ERRNO));
128 			continue;
129 		}
130 		/* Set the functionality flag */
131 		fflag = 1;
132 
133 		/* Check for node's creation */
134 		if (stat(node_name, &buf) < 0) {
135 			tst_resm(TFAIL,
136 				 "stat() of %s failed, errno:%d",
137 				 node_name, TEST_ERRNO);
138 			/* unset flag as functionality fails */
139 			fflag = 0;
140 		}
141 
142 		/* Verify mode permissions of node */
143 		if (buf.st_mode & S_ISGID) {
144 			tst_resm(TFAIL, "%s: Incorrect modes, setgid "
145 				 "bit set", node_name);
146 			/* unset flag as functionality fails */
147 			fflag = 0;
148 		}
149 
150 		/* Verify group ID */
151 		if (buf.st_gid != mygid) {
152 			tst_resm(TFAIL, "%s: Incorrect group",
153 				 node_name);
154 			/* unset flag as functionality fails */
155 			fflag = 0;
156 		}
157 		if (fflag) {
158 			tst_resm(TPASS, "Functionality of mknod(%s, "
159 				 "%#o, 0) successful",
160 				 node_name, MODE_RWX);
161 		}
162 
163 		/* Remove the node for the next go `round */
164 		if (unlink(node_name) == -1) {
165 			tst_resm(TWARN,
166 				 "unlink(%s) failed, errno:%d %s",
167 				 node_name, errno, strerror(errno));
168 		}
169 	}
170 
171 	/* Change the directory back to temporary directory */
172 	chdir("..");
173 
174 	/*
175 	 * Invoke cleanup() to delete the test directories created
176 	 * in the setup() and exit main().
177 	 */
178 	cleanup();
179 
180 	tst_exit();
181 }
182 
183 /*
184  * setup(void) - performs all ONE TIME setup for this test.
185  *	Exit the test program on receipt of unexpected signals.
186  *	Create a temporary directory used to hold test directories created
187  *	and change the directory to it.
188  *	Verify that pid of process executing the test is root.
189  *	Create a test directory on temporary directory and set the ownership
190  *	of test directory to nobody user.
191  *	Set the effective uid/gid of the process to that of nobody user.
192  */
setup(void)193 void setup(void)
194 {
195 	tst_require_root();
196 
197 	/* Capture unexpected signals */
198 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
199 
200 	TEST_PAUSE;
201 
202 	/* Make a temp dir and cd to it */
203 	tst_tmpdir();
204 
205 	/* fix permissions on the tmpdir */
206 	if (chmod(".", 0711) != 0) {
207 		tst_brkm(TBROK, cleanup, "chmod() failed");
208 	}
209 
210 	/* Save the real user id of the test process */
211 	save_myuid = getuid();
212 
213 	/* Save the process id of the test process */
214 	mypid = getpid();
215 
216 	/* Get the node name to be created in the test */
217 	sprintf(node_name, TNODE, mypid);
218 
219 	/* Get the uid/gid of guest user - nobody */
220 	if ((user1 = getpwnam(LTPUSER)) == NULL) {
221 		tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER);
222 	}
223 	user1_uid = user1->pw_uid;
224 	group1_gid = user1->pw_gid;
225 
226 	/* Get effective group id of the test process */
227 	group2_gid = getegid();
228 
229 	/*
230 	 * Create a test directory under temporary directory with the
231 	 * specified mode permissions, with uid/gid set to that of guest
232 	 * user and the test process.
233 	 */
234 	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
235 		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
236 	}
237 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
238 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
239 	}
240 
241 	/*
242 	 * Verify that test directory created with expected permission modes
243 	 * and ownerships.
244 	 */
245 	if (stat(DIR_TEMP, &buf) < 0) {
246 		tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
247 	}
248 
249 	/* Verify modes of test directory */
250 	if (buf.st_mode & S_ISGID) {
251 		tst_brkm(TBROK, cleanup,
252 			 "%s: Incorrect modes, setgid bit set", DIR_TEMP);
253 	}
254 
255 	/* Verify group ID */
256 	if (buf.st_gid != group2_gid) {
257 		tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP);
258 	}
259 
260 	/*
261 	 * Set the effective group id and user id of the test process
262 	 * to that of guest user.
263 	 */
264 	if (setgid(group1_gid) < 0) {
265 		tst_brkm(TBROK, cleanup,
266 			 "Unable to set process gid to that of ltp user");
267 	}
268 	if (setreuid(-1, user1_uid) < 0) {
269 		tst_brkm(TBROK, cleanup,
270 			 "Unable to set process uid to that of ltp user");
271 	}
272 
273 	/* Save the real group ID of the current process */
274 	mygid = getgid();
275 
276 	/* Change directory to DIR_TEMP */
277 	if (chdir(DIR_TEMP) < 0) {
278 		tst_brkm(TBROK, cleanup,
279 			 "Unable to change to %s directory", DIR_TEMP);
280 	}
281 }
282 
283 /*
284  * cleanup() - Performs all ONE TIME cleanup for this test at
285  *             completion or premature exit.
286  *	Print test timing stats and errno log if test executed with options.
287  *	Restore the real/effective user id of the process changed during
288  *	setup().
289  *	Remove temporary directory and sub-directories/files under it
290  *	created during setup().
291  *	Exit the test program with normal exit code.
292  */
cleanup(void)293 void cleanup(void)
294 {
295 
296 	/*
297 	 * Restore the effective uid of the process changed in the
298 	 * setup().
299 	 */
300 	if (setreuid(-1, save_myuid) < 0) {
301 		tst_brkm(TBROK, NULL,
302 			 "resetting process real/effective uid failed");
303 	}
304 
305 	tst_rmdir();
306 
307 }
308