chown02.c revision 0b9589f3f9c0345b29cfcf7da5a1253c708303eb
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: chown02
22 *
23 * Test Description:
24 *  Verify that, when chown(2) invoked by super-user to change the owner and
25 *  group of a file specified by path to any numeric owner(uid)/group(gid)
26 *  values,
27 *	- clears setuid and setgid bits set on an executable file.
28 *	- preserves setgid bit set on a non-group-executable file.
29 *
30 * Expected Result:
31 *  chown(2) should return 0 and the ownership set on the file should match
32 *  the numeric values contained in owner and group respectively.
33 *
34 * Algorithm:
35 *  Setup:
36 *   Setup signal handling.
37 *   Create temporary directory.
38 *   Pause for SIGUSR1 if option specified.
39 *
40 *  Test:
41 *   Loop if the proper options are given.
42 *   Execute system call
43 *   Check return code, if system call failed (return=-1)
44 *   	Log the errno and Issue a FAIL message.
45 *   Otherwise,
46 *   	Verify the Functionality of system call
47 *      if successful,
48 *      	Issue Functionality-Pass message.
49 *      Otherwise,
50 *		Issue Functionality-Fail message.
51 *  Cleanup:
52 *   Print errno log and/or timing stats if options given
53 *   Delete the temporary directory created.
54 *
55 * Usage:  <for command-line>
56 *  chown02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
57 *     where,  -c n : Run n copies concurrently.
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 <sys/types.h>
74#include <sys/stat.h>
75#include <sys/fcntl.h>
76#include <errno.h>
77#include <string.h>
78#include <signal.h>
79
80#include "test.h"
81#include "usctest.h"
82
83#define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
84#define NEW_PERMS1	(S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID)
85#define NEW_PERMS2	(S_IFREG|S_IRWXU|S_ISGID)
86#define EXP_PERMS	(S_IFREG|S_IRWXU|S_IRWXG)
87#define TESTFILE1	"testfile1"
88#define TESTFILE2	"testfile2"
89
90char *TCID = "chown02";
91
92int setup1();			/* Test specific setup functions */
93int setup2();
94
95struct test_case_t {
96	char *pathname;
97	uid_t user_id;
98	gid_t group_id;
99	int test_flag;
100	int (*setupfunc) ();
101} test_cases[] = {
102	/* setuid/setgid bits cleared */
103	{
104	TESTFILE1, 700, 701, 1, setup1},
105	    /* setgid bit not cleared */
106	{
107TESTFILE2, 700, 701, 2, setup2},};
108
109int TST_TOTAL = sizeof(test_cases) / sizeof(*test_cases);
110
111void setup();			/* setup function for the test */
112void cleanup();			/* cleanup function for the test */
113
114int main(int ac, char **av)
115{
116	struct stat stat_buf;	/* stat(2) struct contents */
117	int lc;
118	const char *msg;
119	int i;
120	uid_t user_id;		/* user id of the user set for testfile */
121	gid_t group_id;		/* group id of the user set for testfile */
122	int test_flag;		/* test condition specific flag variable */
123	char *file_name;	/* ptr. for test file name */
124
125	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
126		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
127
128	setup();
129
130	for (lc = 0; TEST_LOOPING(lc); lc++) {
131
132		tst_count = 0;
133
134		for (i = 0; i < TST_TOTAL; i++) {
135
136			file_name = test_cases[i].pathname;
137			user_id = test_cases[i].user_id;
138			group_id = test_cases[i].group_id;
139			test_flag = test_cases[i].test_flag;
140
141			/*
142			 * Call chown(2) with different user id and
143			 * group id (numeric values) to set it on testfile.
144			 */
145			TEST(chown(file_name, user_id, group_id));
146
147			if (TEST_RETURN == -1) {
148				tst_resm(TFAIL | TTERRNO,
149					 "chown(%s, ..) failed", file_name);
150				continue;
151			}
152
153			/*
154			 * Perform functional verification if test
155			 * executed without (-f) option.
156			 */
157			if (STD_FUNCTIONAL_TEST) {
158				/*
159				 * Get the testfile information using stat(2).
160				 */
161				if (stat(file_name, &stat_buf) < 0) {
162					tst_brkm(TFAIL, cleanup, "stat(2) of "
163						 "%s failed, errno:%d",
164						 file_name, TEST_ERRNO);
165				}
166
167				/*
168				 * Check for expected Ownership ids
169				 * set on testfile.
170				 */
171				if (stat_buf.st_uid != user_id ||
172				    stat_buf.st_gid != group_id) {
173					tst_brkm(TFAIL, cleanup, "%s: incorrect"
174						 " ownership set, Expected %d "
175						 "%d", file_name,
176						 user_id, group_id);
177				}
178
179				/*
180				 * Verify that S_ISUID/S_ISGID bits set on the
181				 * testfile(s) in setup()s are cleared by
182				 * chown().
183				 */
184				if (test_flag == 1 &&
185				    (stat_buf.st_mode & (S_ISUID | S_ISGID)) !=
186				    0)
187					tst_resm(TFAIL,
188						 "%s: incorrect mode "
189						 "permissions %#o, Expected "
190						 "%#o", file_name, NEW_PERMS1,
191						 EXP_PERMS);
192				else if (test_flag == 2
193					 && (stat_buf.st_mode & S_ISGID) == 0)
194					tst_resm(TFAIL,
195						 "%s: Incorrect mode "
196						 "permissions %#o, Expected "
197						 "%#o", file_name,
198						 stat_buf.st_mode, NEW_PERMS2);
199				else
200					tst_resm(TPASS,
201						 "chown(%s, ..) succeeded",
202						 file_name);
203			} else
204				tst_resm(TPASS, "call succeeded");
205		}
206	}
207
208	cleanup();
209	tst_exit();
210
211}
212
213/*
214 * void
215 * setup() - performs all ONE TIME setup for this test.
216 *  Create a temporary directory and change directory to it.
217 *  Create a test file under temporary directory and close it
218 */
219void setup(void)
220{
221	int i;
222
223	tst_sig(NOFORK, DEF_HANDLER, cleanup);
224
225	tst_require_root(NULL);
226
227	TEST_PAUSE;
228
229	tst_tmpdir();
230
231	/* call iividual setup functions */
232	for (i = 0; i < TST_TOTAL; i++)
233		test_cases[i].setupfunc();
234}
235
236/*
237 * int
238 * setup1() - Setup function for chown(2) to verify setuid/setgid bits
239 *	      set on an executable file will not be cleared.
240 *  Creat a testfile and set setuid/setgid bits on the mode of file.$
241 */
242int setup1(void)
243{
244	int fd;			/* File descriptor for testfile1 */
245
246	/* Creat a testfile and close it */
247	if ((fd = open(TESTFILE1, O_RDWR | O_CREAT, FILE_MODE)) == -1)
248		tst_brkm(TBROK | TERRNO, cleanup,
249			 "open(%s, O_RDWR|O_CREAT, %o) failed",
250			 TESTFILE1, FILE_MODE);
251	if (close(fd) == -1)
252		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
253			 TESTFILE1);
254
255	/* Set setuid/setgid bits on the test file created */
256	if (chmod(TESTFILE1, NEW_PERMS1) == -1)
257		tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, ..) failed",
258			 TESTFILE1);
259	return 0;
260}
261
262/*
263 * int
264 * setup2() - Setup function for chown(2) to verify setgid bit set
265 *	      set on non-group executable file will not be cleared.
266 *  Creat a testfile and set setgid bit on the mode of file.
267 */
268int setup2(void)
269{
270	int fd;			/* File descriptor for testfile2 */
271
272	/* Creat a testfile and close it */
273	if ((fd = open(TESTFILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
274		tst_brkm(TBROK | TERRNO, cleanup,
275			 "open(%s, O_RDWR|O_CREAT, %o) failed",
276			 TESTFILE2, FILE_MODE);
277	}
278	/* Set setgid bit on the test file created */
279	if (fchmod(fd, NEW_PERMS2) != 0)
280		tst_brkm(TBROK | TERRNO, cleanup, "fchmod failed");
281	if (close(fd) == -1)
282		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
283			 TESTFILE2);
284	return 0;
285}
286
287/*
288 * void
289 * cleanup() - performs all ONE TIME cleanup for this test at
290 *	       completion or premature exit.
291 *  Remove the test directory and testfile created in the setup.
292 */
293void cleanup(void)
294{
295	/*
296	 * print timing stats if that option was specified.
297	 */
298	TEST_CLEANUP;
299
300	tst_rmdir();
301
302}
303