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