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: mknod03
22 *
23 * Test Description:
24 * Verify that mknod(2) succeeds when used to create a filesystem
25 * node with set group-ID bit set on a directory with set group-ID bit set.
26 * The node created should have set group-ID bit set and its gid should be
27 * equal to the effective gid of the process.
28 *
29 * Expected Result:
30 * mknod() should return value 0 on success and node created should have
31 * set group-ID bit set, its gid should be equal to the effective gid of
32 * the process.
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 * mknod03 [-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 <stdlib.h>
74 #include <unistd.h>
75 #include <errno.h>
76 #include <string.h>
77 #include <signal.h>
78 #include <pwd.h>
79 #include <sys/types.h>
80 #include <sys/stat.h>
81
82 #include "test.h"
83 #include "safe_macros.h"
84
85 #define LTPUSER "nobody"
86 #define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
87 #define MODE_SGID S_IFIFO | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO
88 #define DIR_TEMP "testdir_3"
89 #define TNODE "tnode_%d"
90
91 struct stat buf; /* struct. to hold stat(2) o/p contents */
92 struct passwd *user1; /* struct. to hold getpwnam(3) o/p contents */
93
94 char *TCID = "mknod03";
95 int TST_TOTAL = 1;
96 char node_name[PATH_MAX]; /* buffer to hold node name created */
97
98 gid_t group1_gid, group2_gid, mygid; /* user and process group id's */
99 uid_t save_myuid, user1_uid; /* user and process user id's */
100 pid_t mypid; /* process id */
101
102 void setup(); /* setup function for the test */
103 void cleanup(); /* cleanup function for the test */
104
main(int ac,char ** av)105 int main(int ac, char **av)
106 {
107 int lc;
108 int fflag;
109
110 tst_parse_opts(ac, av, NULL, NULL);
111
112 setup();
113
114 for (lc = 0; TEST_LOOPING(lc); lc++) {
115
116 tst_count = 0;
117
118 /*
119 * Attempt to create a filesystem node with group id (sgid)
120 * bit set on a directory with group id (sgid) bit set
121 * such that, the node created by mknod(2) should have
122 * group id (sgid) bit set and node's gid should be equal
123 * to that of effective gid of the process.
124 */
125 TEST(mknod(node_name, MODE_SGID, 0));
126
127 /* Check return code from mknod(2) */
128 if (TEST_RETURN == -1) {
129 tst_resm(TFAIL, "mknod(%s, %#o, 0) failed, errno=%d : "
130 "%s", node_name, MODE_SGID, TEST_ERRNO,
131 strerror(TEST_ERRNO));
132 continue;
133 }
134 /* Set the functionality flag */
135 fflag = 1;
136
137 /* Check for node's creation */
138 if (stat(node_name, &buf) < 0) {
139 tst_resm(TFAIL, "stat() of %s failed, errno:%d",
140 node_name, TEST_ERRNO);
141 /* unset functionality flag */
142 fflag = 0;
143 }
144
145 /*
146 * Skip S_ISGID check
147 * 0fa3ecd87848 ("Fix up non-directory creation in SGID directories")
148 * clears S_ISGID for files created by non-group members
149 */
150
151 /* Verify group ID */
152 if (buf.st_gid != group2_gid) {
153 tst_resm(TFAIL, "%s: Incorrect group",
154 node_name);
155 /* unset flag as functionality fails */
156 fflag = 0;
157 }
158 if (fflag) {
159 tst_resm(TPASS, "Functionality of mknod(%s, "
160 "%#o, 0) successful",
161 node_name, MODE_SGID);
162 }
163
164 /* Remove the node for the next go `round */
165 if (unlink(node_name) == -1) {
166 tst_resm(TWARN, "unlink(%s) failed, errno:%d %s",
167 node_name, errno, strerror(errno));
168 }
169 }
170
171 /* Change the directory back to temporary directory */
172 SAFE_CHDIR(cleanup, "..");
173
174 /*
175 * Invoke cleanup() to delete the test directories created
176 * in the setup() and exit main().
177 */
178 cleanup();
179
180 tst_exit();
181 }
182
183 /*
184 * setup(void) - performs all ONE TIME setup for this test.
185 * Exit the test program on receipt of unexpected signals.
186 * Create a temporary directory used to hold test directories created
187 * and change the directory to it.
188 * Verify that pid of process executing the test is root.
189 * Create a test directory on temporary directory and set the ownership
190 * of test directory to guest user and process, change mode permissions
191 * to set group id bit on it.
192 * Set the effective uid/gid of the process to that of guest user.
193 */
setup(void)194 void setup(void)
195 {
196 tst_require_root();
197
198 /* Capture unexpected signals */
199 tst_sig(NOFORK, DEF_HANDLER, cleanup);
200
201 TEST_PAUSE;
202
203 /* Make a temp dir and cd to it */
204 tst_tmpdir();
205
206 /* fix permissions on the tmpdir */
207 if (chmod(".", 0711) != 0) {
208 tst_brkm(TBROK, cleanup, "chmod() failed");
209 }
210
211 /* Save the real user id of the current test process */
212 save_myuid = getuid();
213 /* Save the process id of the current test process */
214 mypid = getpid();
215
216 /* Get the node name to be created in the test */
217 sprintf(node_name, TNODE, mypid);
218
219 /* Get the uid/gid of ltpuser user */
220 if ((user1 = getpwnam(LTPUSER)) == NULL) {
221 tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER);
222 }
223 user1_uid = user1->pw_uid;
224 group1_gid = user1->pw_gid;
225
226 /* Get the effective group id of the test process */
227 group2_gid = getegid();
228
229 /*
230 * Create a test directory under temporary directory with the
231 * specified mode permissions, with uid/gid set to that of guest
232 * user and the test process.
233 */
234 SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
235 SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid);
236 SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID);
237
238 /*
239 * Verify that test directory created with expected permission modes
240 * and ownerships.
241 */
242 SAFE_STAT(cleanup, DIR_TEMP, &buf);
243
244 /* Verify modes of test directory */
245 if (!(buf.st_mode & S_ISGID)) {
246 tst_brkm(TBROK, cleanup,
247 "%s: Incorrect modes, setgid bit not set", DIR_TEMP);
248 }
249
250 /* Verify group ID of test directory */
251 if (buf.st_gid != group2_gid) {
252 tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP);
253 }
254
255 /*
256 * Set the effective group id and user id of the test process
257 * to that of guest user (nobody)
258 */
259 SAFE_SETGID(cleanup, group1_gid);
260 if (setreuid(-1, user1_uid) < 0) {
261 tst_brkm(TBROK, cleanup,
262 "Unable to set process uid to that of ltp user");
263 }
264
265 /* Save the real group ID of the current process */
266 mygid = getgid();
267
268 /* Change directory to DIR_TEMP */
269 SAFE_CHDIR(cleanup, DIR_TEMP);
270 }
271
272 /*
273 * cleanup() - Performs all ONE TIME cleanup for this test at
274 * completion or premature exit.
275 * Print test timing stats and errno log if test executed with options.
276 * Restore the real/effective user id of the process changed during
277 * setup().
278 * Remove temporary directory and sub-directories/files under it
279 * created during setup().
280 * Exit the test program with normal exit code.
281 */
cleanup(void)282 void cleanup(void)
283 {
284
285 /*
286 * Restore the effective uid of the process changed in the
287 * setup().
288 */
289 if (setreuid(-1, save_myuid) < 0) {
290 tst_brkm(TBROK, NULL,
291 "resetting process real/effective uid failed");
292 }
293
294 tst_rmdir();
295
296 }
297