• 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: chown03
22  *
23  * Test Description:
24  *  Verify that, chown(2) succeeds to change the group of a file specified
25  *  by path when called by non-root user with the following constraints,
26  *	- euid of the process is equal to the owner of the file.
27  *	- the intended gid is either egid, or one of the supplementary gids
28  *	  of the process.
29  *  Also, verify that chown() clears the setuid/setgid bits set on the file.
30  *
31  * Expected Result:
32  *  chown(2) should return 0 and the ownership set on the file should match
33  *  the numeric values contained in owner and group respectively.
34  *
35  * Algorithm:
36  *  Setup:
37  *   Setup signal handling.
38  *   Create temporary directory.
39  *   Pause for SIGUSR1 if option specified.
40  *
41  *  Test:
42  *   Loop if the proper options are given.
43  *   Execute system call
44  *   Check return code, if system call failed (return=-1)
45  *	Log the errno and Issue a FAIL message.
46  *   Otherwise,
47  *	Verify the Functionality of system call
48  *      if successful,
49  *		Issue Functionality-Pass message.
50  *      Otherwise,
51  *		Issue Functionality-Fail message.
52  *  Cleanup:
53  *   Print errno log and/or timing stats if options given
54  *   Delete the temporary directory created.
55  *
56  * Usage:  <for command-line>
57  *  chown03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
58  *     where,  -c n : Run n copies concurrently.
59  *             -f   : Turn off functionality Testing.
60  *	       -i n : Execute test n times.
61  *	       -I x : Execute test for x seconds.
62  *	       -P x : Pause for x seconds between iterations.
63  *	       -t   : Turn on syscall timing.
64  *
65  * HISTORY
66  *	07/2001 Ported by Wayne Boyer
67  *
68  * RESTRICTIONS:
69  *
70  */
71 
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <sys/types.h>
75 #include <sys/stat.h>
76 #include <fcntl.h>
77 #include <errno.h>
78 #include <string.h>
79 #include <signal.h>
80 #include <grp.h>
81 #include <pwd.h>
82 
83 #include "test.h"
84 #include "safe_macros.h"
85 #include "compat_16.h"
86 
87 #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
88 #define NEW_PERMS	(S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID)
89 #define TESTFILE	"testfile"
90 
91 TCID_DEFINE(chown03);
92 int TST_TOTAL = 1;		/* Total number of test conditions */
93 char nobody_uid[] = "nobody";
94 struct passwd *ltpuser;
95 
96 void setup();			/* setup function for the test */
97 void cleanup();			/* cleanup function for the test */
98 
main(int ac,char ** av)99 int main(int ac, char **av)
100 {
101 	struct stat stat_buf;	/* stat(2) struct contents */
102 	int lc;
103 	uid_t user_id;		/* Owner id of the test file. */
104 	gid_t group_id;		/* Group id of the test file. */
105 
106 	tst_parse_opts(ac, av, NULL, NULL);
107 
108 	setup();
109 
110 	for (lc = 0; TEST_LOOPING(lc); lc++) {
111 
112 		tst_count = 0;
113 
114 		UID16_CHECK((user_id = geteuid()), "chown", cleanup)
115 		GID16_CHECK((group_id = getegid()), "chown", cleanup)
116 
117 		TEST(CHOWN(cleanup, TESTFILE, -1, group_id));
118 
119 		if (TEST_RETURN == -1) {
120 			tst_resm(TFAIL | TTERRNO, "chown(%s, ..) failed",
121 				 TESTFILE);
122 			continue;
123 		}
124 
125 		if (stat(TESTFILE, &stat_buf) == -1)
126 			tst_brkm(TFAIL | TERRNO, cleanup,
127 				 "stat failed");
128 
129 		if (stat_buf.st_uid != user_id ||
130 		    stat_buf.st_gid != group_id)
131 			tst_resm(TFAIL, "%s: Incorrect ownership"
132 				 "set to %d %d, Expected %d %d",
133 				 TESTFILE, stat_buf.st_uid,
134 				 stat_buf.st_gid, user_id, group_id);
135 
136 		if (stat_buf.st_mode !=
137 		    (NEW_PERMS & ~(S_ISUID | S_ISGID)))
138 			tst_resm(TFAIL, "%s: incorrect mode permissions"
139 				 " %#o, Expected %#o", TESTFILE,
140 				 stat_buf.st_mode,
141 				 NEW_PERMS & ~(S_ISUID | S_ISGID));
142 		else
143 			tst_resm(TPASS, "chown(%s, ..) was successful",
144 				 TESTFILE);
145 	}
146 
147 	cleanup();
148 	tst_exit();
149 }
150 
151 /*
152  * void
153  * setup() - performs all ONE TIME setup for this test.
154  *  Create a temporary directory and change directory to it.
155  *  Create a test file under temporary directory and close it
156  *  Change the group ownership on testfile.
157  */
setup(void)158 void setup(void)
159 {
160 	int fd;			/* file handler for testfile */
161 
162 	TEST_PAUSE;
163 
164 	tst_require_root();
165 
166 	tst_sig(FORK, DEF_HANDLER, cleanup);
167 
168 	tst_tmpdir();
169 
170 	ltpuser = getpwnam(nobody_uid);
171 	if (ltpuser == NULL)
172 		tst_brkm(TBROK | TERRNO, NULL, "getpwnam(\"nobody\") failed");
173 	SAFE_SETEGID(NULL, ltpuser->pw_gid);
174 	SAFE_SETEUID(NULL, ltpuser->pw_uid);
175 
176 	/* Create a test file under temporary directory */
177 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
178 		tst_brkm(TBROK | TERRNO, cleanup,
179 			 "open(%s, O_RDWR|O_CREAT, %o) failed", TESTFILE,
180 			 FILE_MODE);
181 
182 	SAFE_SETEUID(cleanup, 0);
183 
184 	SAFE_FCHOWN(cleanup, fd, -1, 0);
185 
186 	SAFE_FCHMOD(cleanup, fd, NEW_PERMS);
187 
188 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
189 
190 	SAFE_CLOSE(cleanup, fd);
191 }
192 
cleanup(void)193 void cleanup(void)
194 {
195 	if (setegid(0) == -1)
196 		tst_resm(TWARN | TERRNO, "setegid(0) failed");
197 	if (seteuid(0) == -1)
198 		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
199 
200 	tst_rmdir();
201 
202 }
203