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: fchmod05
22 *
23 * Test Description:
24 * Verify that, fchmod(2) will succeed to change the mode of a directory
25 * but fails to set the setgid bit on it if invoked by non-root (uid != 0)
26 * process with the following constraints,
27 * - the process is the owner of the directory.
28 * - the effective group ID or one of the supplementary group ID's of the
29 * process is not equal to the group ID of the directory.
30 *
31 * Expected Result:
32 * fchmod() should return value 0 on success and though succeeds to change
33 * the mode of a directory but fails to set setgid bit on it.
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 * fchmod05 [-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 * This test should be run by 'non-super-user' only.
70 *
71 */
72
73 #ifndef _GNU_SOURCE
74 #define _GNU_SOURCE
75 #endif
76
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <sys/types.h>
80 #include <sys/stat.h>
81 #include <fcntl.h>
82 #include <errno.h>
83 #include <string.h>
84 #include <signal.h>
85 #include <unistd.h>
86 #include <grp.h>
87 #include <pwd.h>
88
89 #include "test.h"
90
91 #define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO)
92 #define PERMS 043777
93 #define TESTDIR "testdir"
94
95 int fd; /* file descriptor for test directory */
96 char *TCID = "fchmod05";
97 int TST_TOTAL = 1;
98
99 void setup(); /* Main setup function for test */
100 void cleanup(); /* Main cleanup function for test */
101
main(int ac,char ** av)102 int main(int ac, char **av)
103 {
104 struct stat stat_buf; /* stat struct */
105 int lc;
106 mode_t dir_mode; /* mode permissions set on test directory */
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 fchmod(2) with mode argument
118 * to set setgid bit on TESTDIR.
119 */
120
121 TEST(fchmod(fd, PERMS));
122
123 if (TEST_RETURN == -1) {
124 tst_resm(TFAIL, "fchmod(%d, %#o) Failed, errno=%d : %s",
125 fd, PERMS, TEST_ERRNO, strerror(TEST_ERRNO));
126 continue;
127 }
128 /*
129 * Get the directory information using
130 * fstat(2).
131 */
132 if (fstat(fd, &stat_buf) < 0) {
133 tst_brkm(TFAIL, cleanup,
134 "fstat(2) of %s failed, errno:%d",
135 TESTDIR, TEST_ERRNO);
136 }
137 dir_mode = stat_buf.st_mode;
138 if ((PERMS & ~S_ISGID) != dir_mode) {
139 tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
140 "Expected 0%03o",
141 TESTDIR, dir_mode, PERMS & ~S_ISGID);
142 } else {
143 tst_resm(TPASS, "Functionality of fchmod(%d, "
144 "%#o) successful", fd,
145 PERMS & ~S_ISGID);
146 }
147 }
148
149 cleanup();
150 tst_exit();
151 }
152
153 /*
154 * void
155 * setup() - performs all ONE TIME setup for this test.
156 * Create a temporary directory and cd to it.
157 * Create a test directory under temporary directory.
158 * Invoke setuid to root program to modify group ownership
159 * on test directory.
160 * Open the test directory for reading.
161 */
setup(void)162 void setup(void)
163 {
164 struct passwd *nobody_u;
165 struct group *bin_group;
166
167 tst_require_root();
168
169 tst_sig(FORK, DEF_HANDLER, cleanup);
170
171 TEST_PAUSE;
172
173 tst_tmpdir();
174
175 nobody_u = getpwnam("nobody");
176 if (!nobody_u)
177 tst_brkm(TBROK, cleanup,
178 "Couldn't find uid of nobody: %s", strerror(errno));
179
180 #ifdef ANDROID
181 bin_group = getgrnam("everybody");
182 #else
183 bin_group = getgrnam("bin");
184 #endif
185 if (!bin_group)
186 tst_brkm(TBROK, cleanup,
187 "Couldn't find gid of bin: %s", strerror(errno));
188
189 /*
190 * Create a test directory under temporary directory with specified
191 * mode permissions and change the gid of test directory to that of
192 * guest user.
193 */
194 if (mkdir(TESTDIR, MODE_RWX) < 0) {
195 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
196 }
197
198 if (setgroups(1, &nobody_u->pw_gid) == -1)
199 tst_brkm(TBROK, cleanup,
200 "Couldn't change supplementary group Id: %s",
201 strerror(errno));
202
203 if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
204 tst_brkm(TBROK, cleanup, "Couldn't change owner of testdir: %s",
205 strerror(errno));
206
207 /* change to nobody:nobody */
208 if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1)
209 tst_brkm(TBROK, cleanup, "Couldn't switch to nobody:nobody: %s",
210 strerror(errno));
211
212 /* Open the test directory for reading */
213 fd = open(TESTDIR, O_RDONLY);
214 if (fd == -1) {
215 tst_brkm(TBROK, cleanup,
216 "open(%s, O_RDONLY) failed, errno=%d : %s",
217 TESTDIR, errno, strerror(errno));
218 }
219 }
220
221 /*
222 * void
223 * cleanup() - performs all ONE TIME cleanup for this test at
224 * completion or premature exit.
225 * Close the test directory opened in the setup().
226 * Remove the test directory and temporary directory created in
227 * in the setup().
228 */
cleanup(void)229 void cleanup(void)
230 {
231
232 /* Close the test directory opened in the setup() */
233 if (close(fd) == -1) {
234 tst_brkm(TBROK, NULL,
235 "close(%s) Failed, errno=%d : %s",
236 TESTDIR, errno, strerror(errno));
237 }
238
239 setegid(0);
240 seteuid(0);
241
242 tst_rmdir();
243
244 }
245