blob: 9bbaba2107ef6d1d3861e7f43d4b78edd7183138 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: mknod04
22 *
23 * Test Description:
subrata_modak4bb656a2009-02-26 12:02:09 +000024 * Verify that mknod(2) succeeds when used to create a filesystem
plars865695b2001-08-27 22:15:12 +000025 * node on a directory with set group-ID bit set.
subrata_modak4bb656a2009-02-26 12:02:09 +000026 * The node created should not have group-ID bit set and its gid should be
plars865695b2001-08-27 22:15:12 +000027 * equal to the effective gid of the process.
28 *
29 * Expected Result:
30 * mknod() should return value 0 on success and node created should not
31 * have set group-ID bit set and its gid should be equal to the effective
32 * gid of 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)
subrata_modak56207ce2009-03-23 13:35:39 +000044 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000045 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000046 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000047 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000048 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000049 * 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 * mknod04 [-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"
plars865695b2001-08-27 22:15:12 +000083
84#define LTPUSER "nobody"
85#define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
86#define MODE_SGID S_IFIFO | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO
87#define DIR_TEMP "testdir_4"
88#define TNODE "tnode_%d"
89
90struct stat buf; /* struct. to hold stat(2) o/p contents */
91struct passwd *user1; /* struct. to hold getpwnam(3) o/p contents */
92
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020093char *TCID = "mknod04";
94int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000095char node_name[PATH_MAX]; /* buffer to hold node name created */
plars865695b2001-08-27 22:15:12 +000096
97gid_t group1_gid, group2_gid, mygid; /* user and process group id's */
98uid_t save_myuid, user1_uid; /* user and process user id's */
99pid_t mypid; /* process id */
100
101void setup(); /* setup function for the test */
102void cleanup(); /* cleanup function for the test */
103
subrata_modak56207ce2009-03-23 13:35:39 +0000104int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000105{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200106 int lc;
plars865695b2001-08-27 22:15:12 +0000107 int fflag; /* functionality flag variable */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200108 const char *msg;
plars865695b2001-08-27 22:15:12 +0000109
Garrett Cooper45e285d2010-11-22 12:19:25 -0800110 msg = parse_opts(ac, av, NULL, NULL);
111 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000112 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800113
plars865695b2001-08-27 22:15:12 +0000114 }
115
plars865695b2001-08-27 22:15:12 +0000116 setup();
117
plars865695b2001-08-27 22:15:12 +0000118 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800119
Caspar Zhangd59a6592013-03-07 14:59:12 +0800120 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000121
122 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000123 * TEST CASE CONDITION:
plars865695b2001-08-27 22:15:12 +0000124 * Attempt to create a filesystem node on a directory
125 * with group id (sgid) bit set such that,
126 * the node created by mknod(2) should not have group id
127 * (sgid) bit set and node's gid should be equal to the
128 * effective gid of the process.
129 */
130 TEST(mknod(node_name, MODE_RWX, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000131
plars865695b2001-08-27 22:15:12 +0000132 /* Check return code from mknod(2) */
133 if (TEST_RETURN == -1) {
134 tst_resm(TFAIL, "mknod(%s, %#o, 0) failed, errno=%d : "
135 "%s", node_name, MODE_RWX, TEST_ERRNO,
136 strerror(TEST_ERRNO));
137 continue;
138 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200139 /* Set the functionality flag */
140 fflag = 1;
plars865695b2001-08-27 22:15:12 +0000141
Cyril Hrubise38b9612014-06-02 17:20:57 +0200142 /* Check for node's creation */
143 if (stat(node_name, &buf) < 0) {
144 tst_resm(TFAIL, "stat() of %s failed, errno:%d",
145 node_name, TEST_ERRNO);
146 /* unset fflag */
147 fflag = 0;
148 }
plars865695b2001-08-27 22:15:12 +0000149
Cyril Hrubise38b9612014-06-02 17:20:57 +0200150 /* Verify mode permissions of node */
151 if (buf.st_mode & S_ISGID) {
152 tst_resm(TFAIL, "%s: Incorrect modes, setgid "
153 "bit set", node_name);
154 /* unset flag as functionality fails */
155 fflag = 0;
156 }
plars865695b2001-08-27 22:15:12 +0000157
Cyril Hrubise38b9612014-06-02 17:20:57 +0200158 /* Verify group ID of node */
159 if (buf.st_gid != group2_gid) {
160 tst_resm(TFAIL, "%s: Incorrect group",
161 node_name);
162 /* unset flag as functionality fails */
163 fflag = 0;
164 }
165 if (fflag) {
166 tst_resm(TPASS, "Functionality of mknod(%s, "
167 "%#o, 0) successful",
168 node_name, MODE_RWX);
plars865695b2001-08-27 22:15:12 +0000169 }
170
171 /* Remove the node for the next go `round */
172 if (unlink(node_name) == -1) {
173 tst_resm(TWARN, "unlink(%s) failed, errno:%d %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000174 node_name, errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000175 }
subrata_modak56207ce2009-03-23 13:35:39 +0000176 }
plars865695b2001-08-27 22:15:12 +0000177
178 /* Change the directory back to temporary directory */
179 chdir("..");
180
181 /*
182 * Invoke cleanup() to delete the test directories created
183 * in the setup() and exit main().
184 */
185 cleanup();
186
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800187 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800188}
plars865695b2001-08-27 22:15:12 +0000189
190/*
subrata_modak4bb656a2009-02-26 12:02:09 +0000191 * void
plars865695b2001-08-27 22:15:12 +0000192 * setup(void) - performs all ONE TIME setup for this test.
subrata_modak56207ce2009-03-23 13:35:39 +0000193 * Exit the test program on receipt of unexpected signals.
plars865695b2001-08-27 22:15:12 +0000194 * Create a temporary directory used to hold test directories created
195 * and change the directory to it.
196 * Verify that pid of process executing the test is root.
subrata_modak4bb656a2009-02-26 12:02:09 +0000197 * Create a test directory on temporary directory and set the ownership
plars865695b2001-08-27 22:15:12 +0000198 * of test directory to guest user and process, change mode permissions
199 * to set group-id bit on it.
200 * Set the effective uid/gid of the process to that of guest user.
201 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400202void setup(void)
plars865695b2001-08-27 22:15:12 +0000203{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200204 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000205
206 /* Capture unexpected signals */
207 tst_sig(NOFORK, DEF_HANDLER, cleanup);
208
plars865695b2001-08-27 22:15:12 +0000209 TEST_PAUSE;
210
211 /* Make a temp dir and cd to it */
212 tst_tmpdir();
213
subrata_modak56207ce2009-03-23 13:35:39 +0000214 /* fix permissions on the tmpdir */
215 if (chmod(".", 0711) != 0) {
216 tst_brkm(TBROK, cleanup, "chmod() failed");
217 }
plars05a3b8b2001-09-05 15:37:06 +0000218
plars865695b2001-08-27 22:15:12 +0000219 /* Save the real user id of the current test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000220 save_myuid = getuid();
plars865695b2001-08-27 22:15:12 +0000221
222 /* Save the process id of the current test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000223 mypid = getpid();
plars865695b2001-08-27 22:15:12 +0000224
225 /* Get the node name to be created in the test */
226 sprintf(node_name, TNODE, mypid);
227
228 /* Get the uid/gid of ltp user */
229 if ((user1 = getpwnam(LTPUSER)) == NULL) {
230 tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER);
231 }
232 user1_uid = user1->pw_uid;
233 group1_gid = user1->pw_gid;
234
235 /* Get the effective group id of the test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000236 group2_gid = getegid();
plars865695b2001-08-27 22:15:12 +0000237
238 /*
239 * Create a test directory under temporary directory with the
240 * specified mode permissions, with uid/gid set to that of guest
241 * user and the test process.
242 */
243 if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
244 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
245 }
246 if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
247 tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
248 }
249 if (chmod(DIR_TEMP, MODE_SGID) < 0) {
250 tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
251 }
252
253 /*
254 * Verify that test directory created with expected permission modes
255 * and ownerships.
256 */
257 if (stat(DIR_TEMP, &buf) < 0) {
258 tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
259 }
260
261 /* Verify modes of test directory */
262 if (!(buf.st_mode & S_ISGID)) {
263 tst_brkm(TBROK, cleanup,
264 "%s: Incorrect modes, setgid bit not set", DIR_TEMP);
265 }
266
267 /* Verify group ID */
268 if (buf.st_gid != group2_gid) {
269 tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP);
270 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000271
subrata_modak56207ce2009-03-23 13:35:39 +0000272 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000273 * Set the effective group id and user id of the test process
plars865695b2001-08-27 22:15:12 +0000274 * to that of guest user (nobody)
275 */
276 if (setgid(group1_gid) < 0) {
277 tst_brkm(TBROK, cleanup,
278 "Unable to set process gid to that of ltp user");
279 }
280 if (setreuid(-1, user1_uid) < 0) {
281 tst_brkm(TBROK, cleanup,
282 "Unable to set process uid to that of ltp user");
283 }
284
285 /* Save the real group ID of the current process */
286 mygid = getgid();
287
288 /* Change directory to DIR_TEMP */
289 if (chdir(DIR_TEMP) < 0) {
290 tst_brkm(TBROK, cleanup,
291 "Unable to change to %s directory", DIR_TEMP);
292 }
293}
294
295/*
296 * cleanup() - Performs all ONE TIME cleanup for this test at
297 * completion or premature exit.
298 * Print test timing stats and errno log if test executed with options.
299 * Restore the real/effective user id of the process changed during
300 * setup().
301 * Remove temporary directory and sub-directories/files under it
302 * created during setup().
303 * Exit the test program with normal exit code.
304 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400305void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000306{
plars865695b2001-08-27 22:15:12 +0000307
308 /*
309 * Restore the effective uid of the process changed in the
310 * setup().
311 */
312 if (setreuid(-1, save_myuid) < 0) {
313 tst_brkm(TBROK, cleanup,
314 "resetting process real/effective uid failed");
315 }
316
plars865695b2001-08-27 22:15:12 +0000317 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000318
Chris Dearmanec6edca2012-10-17 19:54:01 -0700319}