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: mknod02
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 without set group-ID bit set.
26 * The node created should have set group-ID bit set and its gid should be
27 * equal to that of its parent directory.
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 that of its parent
32 * directory.
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 * mknod02 [-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_2"
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 = "mknod02";
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 bit set
120 * on a directory without group id bit set such that,
121 * the node created by mknod(2) should have group-id (sgid)
122 * bit set and node's gid should be equal to that of its
123 * parent directory.
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,
130 "mknod(%s, %#o, 0) failed, errno=%d : %s",
131 node_name, MODE_SGID, TEST_ERRNO,
132 strerror(TEST_ERRNO));
133 continue;
134 }
135 /* Set the functionality flag */
136 fflag = 1;
137
138 /* Check for node's creation */
139 if (stat(node_name, &buf) < 0) {
140 tst_resm(TFAIL, "stat() of %s failed, errno:%d",
141 node_name, TEST_ERRNO);
142
143 /* unset functionality flag */
144 fflag = 0;
145 }
146
147 /* Verify mode permissions of node */
148 if (!(buf.st_mode & S_ISGID)) {
149 tst_resm(TFAIL, "%s: Incorrect modes, setgid "
150 "bit not set", node_name);
151 /* unset flag as functionality fails */
152 fflag = 0;
153 }
154
155 /* Verify group ID of node */
156 if (buf.st_gid != mygid) {
157 tst_resm(TFAIL, "%s: Incorrect group",
158 node_name);
159 /* unset flag as functionality fails */
160 fflag = 0;
161 }
162 if (fflag) {
163 tst_resm(TPASS, "Functionality of mknod(%s, "
164 "%#o, 0) successful",
165 node_name, MODE_SGID);
166 }
167
168 /* Remove the node for the next go `round */
169 if (unlink(node_name) == -1) {
170 tst_resm(TWARN, "unlink(%s) failed, errno:%d %s",
171 node_name, errno, strerror(errno));
172 }
173 }
174
175 /* Change the directory back to temporary directory */
176 SAFE_CHDIR(cleanup, "..");
177
178 /*
179 * Invoke cleanup() to delete the test directories created
180 * in the setup() and exit main().
181 */
182 cleanup();
183
184 tst_exit();
185 }
186
187 /*
188 * setup(void) - performs all ONE TIME setup for this test.
189 * Exit the test program on receipt of unexpected signals.
190 * Create a temporary directory used to hold test directories created
191 * and change the directory to it.
192 * Verify that pid of process executing the test is root.
193 * Create a test directory on temporary directory and set the ownership
194 * of test directory to ltp user and process.
195 * Set the effective uid/gid of the process to that of ltp user.
196 */
setup(void)197 void setup(void)
198 {
199 tst_require_root();
200
201 /* Capture unexpected signals */
202 tst_sig(NOFORK, DEF_HANDLER, cleanup);
203
204 TEST_PAUSE;
205
206 /* Make a temp dir and cd to it */
207 tst_tmpdir();
208
209 /* fix permissions on the tmpdir */
210 if (chmod(".", 0711) != 0) {
211 tst_brkm(TBROK, cleanup, "chmod() failed");
212 }
213
214 /* Save the real user id of the current test process */
215 save_myuid = getuid();
216
217 /* Save the process id of the current test process */
218 mypid = getpid();
219
220 /* Get the node name to be created in the test */
221 sprintf(node_name, TNODE, mypid);
222
223 /* Get the uid/gid of ltpuser */
224 if ((user1 = getpwnam(LTPUSER)) == NULL) {
225 tst_brkm(TBROK | TERRNO, cleanup,
226 "Couldn't determine if %s was in /etc/passwd",
227 LTPUSER);
228 }
229 user1_uid = user1->pw_uid;
230 group1_gid = user1->pw_gid;
231
232 /* Get the effective group id of the test process */
233 group2_gid = getegid();
234
235 /*
236 * Create a test directory under temporary directory with the
237 * specified mode permissions, with uid/gid set to that of guest
238 * user and the test process.
239 */
240 SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
241 SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid);
242
243 /*
244 * Verify that test directory created with expected permission modes
245 * and ownerships.
246 */
247 SAFE_STAT(cleanup, DIR_TEMP, &buf);
248
249 /* Verify modes of test directory */
250 if (buf.st_mode & S_ISGID) {
251 tst_brkm(TBROK, cleanup,
252 "%s: Incorrect modes, setgid bit set", DIR_TEMP);
253 }
254
255 /* Verify group ID of test directory */
256 if (buf.st_gid != group2_gid) {
257 tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP);
258 }
259
260 /*
261 * Set the effective group id and user id of the test process
262 * to that of guest user.
263 */
264 SAFE_SETGID(cleanup, group1_gid);
265 if (setreuid(-1, user1_uid) < 0) {
266 tst_brkm(TBROK, cleanup,
267 "Unable to set process uid to that of ltp user");
268 }
269
270 /* Save the real group ID of the current process */
271 mygid = getgid();
272
273 /* Change directory to DIR_TEMP */
274 SAFE_CHDIR(cleanup, DIR_TEMP);
275 }
276
277 /*
278 * cleanup() - Performs all ONE TIME cleanup for this test at
279 * completion or premature exit.
280 * Print test timing stats and errno log if test executed with options.
281 * Restore the real/effective user id of the process changed during
282 * setup().
283 * Remove temporary directory and sub-directories/files under it
284 * created during setup().
285 * Exit the test program with normal exit code.
286 */
cleanup(void)287 void cleanup(void)
288 {
289
290 /*
291 * Restore the effective uid of the process changed in the
292 * setup().
293 */
294 if (setreuid(-1, save_myuid) < 0) {
295 tst_brkm(TBROK, NULL,
296 "resetting process real/effective uid failed");
297 }
298
299 tst_rmdir();
300
301 }
302