• 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: chmod05
22  *
23  * Test Description:
24 //wjh
25 //This test seems to be invalid see comments below
26 //The man page for chmod doesn't seem to indicate that the setgid bit can
27 //only be set by root
28  *  Verify that, chmod(2) will succeed to change the mode of a directory
29  *  but fails to set the setgid bit on it if invoked by non-root (uid != 0)
30  *  process with the following constraints,
31  *	- the process is the owner of the directory.
32  *	- the effective group ID or one of the supplementary group ID's of the
33  *	  process is not equal to the group ID of the directory.
34  *
35  * Expected Result:
36  *  chmod() should return value 0 on success and though succeeds to change
37  *  the mode of a directory but fails to set setgid bit on it.
38  *
39  * Algorithm:
40  *  Setup:
41  *   Setup signal handling.
42  *   Create temporary directory.
43  *   Pause for SIGUSR1 if option specified.
44  *
45  *  Test:
46  *   Loop if the proper options are given.
47  *   Execute system call
48  *   Check return code, if system call failed (return=-1)
49  *	Log the errno and Issue a FAIL message.
50  *   Otherwise,
51  *	Verify the Functionality of system call
52  *      if successful,
53  *		Issue Functionality-Pass message.
54  *      Otherwise,
55  *		Issue Functionality-Fail message.
56  *  Cleanup:
57  *   Print errno log and/or timing stats if options given
58  *   Delete the temporary directory created.
59  *
60  * Usage:  <for command-line>
61  *  chmod05 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
62  *     where,  -c n : Run n copies concurrently.
63  *             -e   : Turn on errno logging.
64  *             -f   : Turn off functionality Testing.
65  *	       -i n : Execute test n times.
66  *	       -I x : Execute test for x seconds.
67  *	       -P x : Pause for x seconds between iterations.
68  *	       -t   : Turn on syscall timing.
69  *
70  * HISTORY
71  *	07/2001 Ported by Wayne Boyer
72  *	05/2002 Fixes by wjhuie and Ferhan Shareef
73  *                  changed conditional to check for valid tests only and
74  *                  in a more logically understandable way
75  *	07/2002 Additional fixes to #defines to allow comparisions to work.
76  *		-Robbie Williamson
77  *
78  * RESTRICTIONS:
79  *  This test should be run by root.
80  *
81  */
82 
83 #ifndef _GNU_SOURCE
84 #define _GNU_SOURCE
85 #endif
86 
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <sys/types.h>
90 #include <sys/stat.h>
91 #include <sys/fcntl.h>
92 #include <errno.h>
93 #include <string.h>
94 #include <signal.h>
95 #include <unistd.h>
96 #include <grp.h>
97 #include <pwd.h>
98 
99 #include "test.h"
100 
101 #define DEBUG 0
102 
103 #define MODE_RWX	(mode_t)(S_IRWXU | S_IRWXG | S_IRWXO)
104 #define DIR_MODE	(mode_t)(S_ISVTX | S_ISGID | S_IFDIR)
105 #define PERMS		(mode_t)(MODE_RWX | DIR_MODE)
106 #define TESTDIR		"testdir"
107 
108 char *TCID = "chmod05";
109 int TST_TOTAL = 1;
110 
111 void setup();			/* Main setup function for test */
112 void cleanup();			/* Main cleanup function for test */
113 
main(int ac,char ** av)114 int main(int ac, char **av)
115 {
116 	struct stat stat_buf;	/* stat struct */
117 	int lc;
118 	mode_t dir_mode;	/* mode permissions set on test directory */
119 
120 	tst_parse_opts(ac, av, NULL, NULL);
121 
122 	setup();
123 
124 	for (lc = 0; TEST_LOOPING(lc); lc++) {
125 
126 		tst_count = 0;
127 
128 		TEST(chmod(TESTDIR, PERMS));
129 
130 		if (TEST_RETURN == -1) {
131 			tst_resm(TFAIL, "chmod(%s, %#o) failed",
132 				 TESTDIR, PERMS);
133 			continue;
134 		}
135 		/*
136 		 * Get the directory information using
137 		 * stat(2).
138 		 */
139 		if (stat(TESTDIR, &stat_buf) < 0) {
140 			tst_brkm(TFAIL | TERRNO, cleanup,
141 				 "stat(%s) failed", TESTDIR);
142 		}
143 		dir_mode = stat_buf.st_mode;
144 #if DEBUG
145 		printf("DIR_MODE = 0%03o\n", DIR_MODE);
146 		printf("MODE_RWX = 0%03o\n", MODE_RWX);
147 		printf("PERMS = 0%03o\n", PERMS);
148 		printf("dir_mode = 0%03o\n", dir_mode);
149 #endif
150 		if ((PERMS & ~S_ISGID) != dir_mode)
151 			tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
152 				 "Expected 0%03o", TESTDIR, dir_mode,
153 				 PERMS & ~S_ISGID);
154 		else
155 			tst_resm(TPASS,
156 				 "Functionality of chmod(%s, %#o) successful",
157 					 TESTDIR, PERMS);
158 	}
159 
160 	cleanup();
161 	tst_exit();
162 }
163 
164 /*
165  * void
166  * setup() - performs all ONE TIME setup for this test.
167  *  Create a temporary directory and cd to it.
168  *  Create a test directory under temporary directory.
169 //wjh we are root so do we really need this kluged helper program?
170  *  Invoke setuid to root program to modify group ownership
171  *  on test directory.
172  */
setup(void)173 void setup(void)
174 {
175 	struct passwd *nobody_u;
176 	struct group *bin_group;
177 
178 	tst_require_root();
179 
180 	TEST_PAUSE;
181 
182 	tst_tmpdir();
183 
184 	nobody_u = getpwnam("nobody");
185 	if (nobody_u == NULL)
186 		tst_brkm(TBROK | TERRNO, cleanup,
187 			 "getpwnam(\"nobody\") failed");
188 #ifdef ANDROID
189 	bin_group = getgrnam("everybody");
190 #else
191 	bin_group = getgrnam("bin");
192 #endif
193 	if (bin_group == NULL)
194 		tst_brkm(TBROK | TERRNO, cleanup, "getgrnam(\"bin\") failed");
195 
196 	/*
197 	 * Create a test directory under temporary directory with specified
198 	 * mode permissions and change the gid of test directory to nobody's
199 	 * gid.
200 	 */
201 	if (mkdir(TESTDIR, MODE_RWX) < 0)
202 		tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s) failed", TESTDIR);
203 
204 	if (setgroups(1, &nobody_u->pw_gid) == -1)
205 		tst_brkm(TBROK | TERRNO, cleanup,
206 			 "setgroups to nobody's gid failed");
207 
208 	if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
209 		tst_brkm(TBROK | TERRNO, cleanup,
210 			 "chowning testdir to nobody:bin failed");
211 
212 	/* change to nobody:nobody */
213 	if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1)
214 		tst_brkm(TBROK | TERRNO, cleanup,
215 			 "Couldn't switch to nobody:nobody");
216 }
217 
cleanup(void)218 void cleanup(void)
219 {
220 	if (setegid(0) == -1)
221 		tst_resm(TWARN | TERRNO, "setegid(0) failed");
222 	if (seteuid(0) == -1)
223 		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
224 
225 	tst_rmdir();
226 }
227