blob: 400c06d4c3b48840f4330cf00d4b5e2fc2798f39 [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/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: mknod08
plars865695b2001-08-27 22:15:12 +000022 *
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 without set group-ID bit set. The node created
26 * should not have set group-ID bit set and its gid should be equal to that
27 * of its parent directory.
28 *
29 * Expected Result:
30 * mknod() should return value 0 on success and node created should not
31 * have set group-ID bit set.
32 *
33 * Algorithm:
34 * Setup:
35 * Setup signal handling.
36 * Create temporary directory.
37 * Pause for SIGUSR1 if option specified.
38 *
39 * Test:
40 * Loop if the proper options are given.
41 * Execute system call
42 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000043 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000044 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000046 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000047 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000048 * Otherwise,
49 * Issue Functionality-Fail message.
50 * Cleanup:
51 * Print errno log and/or timing stats if options given
52 * Delete the temporary directory created.
53 *
54 * Usage: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000055 * mknod08 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000056 * where, -c n : Run n copies concurrently.
57 * -e : Turn on errno logging.
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 DIR_TEMP "testdir_1"
87#define TNODE "tnode_%d"
88
89struct stat buf; /* struct. to hold stat(2) o/p contents */
90struct passwd *user1; /* struct. to hold getpwnam(3) o/p contents */
91
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020092char *TCID = "mknod08";
93int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000094char node_name[PATH_MAX]; /* buffer to hold node name created */
plars865695b2001-08-27 22:15:12 +000095
96gid_t group1_gid, group2_gid, mygid; /* user and process group id's */
97uid_t save_myuid, user1_uid; /* user and process user id's */
98pid_t mypid; /* process id */
99
100void setup(); /* setup function for the test */
101void cleanup(); /* cleanup function for the test */
102
subrata_modak56207ce2009-03-23 13:35:39 +0000103int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000104{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200105 int lc;
plars865695b2001-08-27 22:15:12 +0000106 int fflag; /* functionality flag variable */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200107 const char *msg;
plars865695b2001-08-27 22:15:12 +0000108
Garrett Cooper45e285d2010-11-22 12:19:25 -0800109 msg = parse_opts(ac, av, NULL, NULL);
110 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000111 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800112
plars865695b2001-08-27 22:15:12 +0000113 }
114
plars865695b2001-08-27 22:15:12 +0000115 setup();
116
plars865695b2001-08-27 22:15:12 +0000117 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800118
Caspar Zhangd59a6592013-03-07 14:59:12 +0800119 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000120
121 /*
122 * Call mknod() to creat a node on a directory without
123 * set group-ID (sgid) bit set.
124 */
125 TEST(mknod(node_name, MODE_RWX, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000126
plars865695b2001-08-27 22:15:12 +0000127 /* 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_RWX, TEST_ERRNO,
132 strerror(TEST_ERRNO));
133 continue;
134 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200135 /* Set the functionality flag */
136 fflag = 1;
plars865695b2001-08-27 22:15:12 +0000137
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 /* Check for node's creation */
139 if (stat(node_name, &buf) < 0) {
140 tst_resm(TFAIL,
141 "stat() of %s failed, errno:%d",
142 node_name, TEST_ERRNO);
143 /* unset flag as functionality fails */
144 fflag = 0;
145 }
plars865695b2001-08-27 22:15:12 +0000146
Cyril Hrubise38b9612014-06-02 17:20:57 +0200147 /* Verify mode permissions of node */
148 if (buf.st_mode & S_ISGID) {
149 tst_resm(TFAIL, "%s: Incorrect modes, setgid "
150 "bit set", node_name);
151 /* unset flag as functionality fails */
152 fflag = 0;
153 }
plars865695b2001-08-27 22:15:12 +0000154
Cyril Hrubise38b9612014-06-02 17:20:57 +0200155 /* Verify group ID */
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_RWX);
plars865695b2001-08-27 22:15:12 +0000166 }
167
168 /* Remove the node for the next go `round */
169 if (unlink(node_name) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000170 tst_resm(TWARN,
plars865695b2001-08-27 22:15:12 +0000171 "unlink(%s) failed, errno:%d %s",
172 node_name, errno, strerror(errno));
173 }
subrata_modak56207ce2009-03-23 13:35:39 +0000174 }
plars865695b2001-08-27 22:15:12 +0000175
176 /* Change the directory back to temporary directory */
177 chdir("..");
178
179 /*
180 * Invoke cleanup() to delete the test directories created
181 * in the setup() and exit main().
182 */
183 cleanup();
184
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800185 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800186}
plars865695b2001-08-27 22:15:12 +0000187
188/*
189 * setup(void) - performs all ONE TIME setup for this test.
subrata_modak56207ce2009-03-23 13:35:39 +0000190 * Exit the test program on receipt of unexpected signals.
plars865695b2001-08-27 22:15:12 +0000191 * Create a temporary directory used to hold test directories created
192 * and change the directory to it.
193 * Verify that pid of process executing the test is root.
194 * Create a test directory on temporary directory and set the ownership
195 * of test directory to nobody user.
196 * Set the effective uid/gid of the process to that of nobody user.
197 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400198void setup(void)
plars865695b2001-08-27 22:15:12 +0000199{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200200 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000201
202 /* Capture unexpected signals */
203 tst_sig(NOFORK, DEF_HANDLER, cleanup);
204
plars865695b2001-08-27 22:15:12 +0000205 TEST_PAUSE;
206
207 /* Make a temp dir and cd to it */
208 tst_tmpdir();
209
subrata_modak56207ce2009-03-23 13:35:39 +0000210 /* fix permissions on the tmpdir */
211 if (chmod(".", 0711) != 0) {
212 tst_brkm(TBROK, cleanup, "chmod() failed");
213 }
plars05a3b8b2001-09-05 15:37:06 +0000214
plars865695b2001-08-27 22:15:12 +0000215 /* Save the real user id of the test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000216 save_myuid = getuid();
plars865695b2001-08-27 22:15:12 +0000217
218 /* Save the process id of the test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000219 mypid = getpid();
plars865695b2001-08-27 22:15:12 +0000220
221 /* Get the node name to be created in the test */
222 sprintf(node_name, TNODE, mypid);
223
224 /* Get the uid/gid of guest user - nobody */
225 if ((user1 = getpwnam(LTPUSER)) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000226 tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER);
plars865695b2001-08-27 22:15:12 +0000227 }
228 user1_uid = user1->pw_uid;
229 group1_gid = user1->pw_gid;
230
231 /* Get effective group id of the test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000232 group2_gid = getegid();
plars865695b2001-08-27 22:15:12 +0000233
234 /*
235 * Create a test directory under temporary directory with the
236 * specified mode permissions, with uid/gid set to that of guest
237 * user and the test process.
238 */
239 if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
240 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
241 }
242 if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
243 tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
244 }
245
246 /*
247 * Verify that test directory created with expected permission modes
248 * and ownerships.
249 */
250 if (stat(DIR_TEMP, &buf) < 0) {
251 tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
252 }
253
254 /* Verify modes of test directory */
255 if (buf.st_mode & S_ISGID) {
256 tst_brkm(TBROK, cleanup,
257 "%s: Incorrect modes, setgid bit set", DIR_TEMP);
258 }
259
260 /* Verify group ID */
261 if (buf.st_gid != group2_gid) {
262 tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP);
263 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000264
subrata_modak56207ce2009-03-23 13:35:39 +0000265 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000266 * Set the effective group id and user id of the test process
plars865695b2001-08-27 22:15:12 +0000267 * to that of guest user.
268 */
269 if (setgid(group1_gid) < 0) {
270 tst_brkm(TBROK, cleanup,
271 "Unable to set process gid to that of ltp user");
272 }
273 if (setreuid(-1, user1_uid) < 0) {
274 tst_brkm(TBROK, cleanup,
275 "Unable to set process uid to that of ltp user");
276 }
277
278 /* Save the real group ID of the current process */
279 mygid = getgid();
280
281 /* Change directory to DIR_TEMP */
282 if (chdir(DIR_TEMP) < 0) {
283 tst_brkm(TBROK, cleanup,
284 "Unable to change to %s directory", DIR_TEMP);
285 }
286}
287
288/*
289 * cleanup() - Performs all ONE TIME cleanup for this test at
290 * completion or premature exit.
291 * Print test timing stats and errno log if test executed with options.
292 * Restore the real/effective user id of the process changed during
293 * setup().
294 * Remove temporary directory and sub-directories/files under it
295 * created during setup().
296 * Exit the test program with normal exit code.
297 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400298void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000299{
plars865695b2001-08-27 22:15:12 +0000300
301 /*
302 * Restore the effective uid of the process changed in the
303 * setup().
304 */
305 if (setreuid(-1, save_myuid) < 0) {
306 tst_brkm(TBROK, NULL,
307 "resetting process real/effective uid failed");
308 }
309
plars865695b2001-08-27 22:15:12 +0000310 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000311
Chris Dearmanec6edca2012-10-17 19:54:01 -0700312}