blob: 03559a11b156b35de88803d943d5d99ee1f91263 [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: mknod03
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 with set group-ID bit set on a directory with set group-ID bit set.
subrata_modak4bb656a2009-02-26 12:02:09 +000026 * The node created should have set 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 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000046 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000047 * 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"
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_3"
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 = "mknod03";
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;
subrata_modakbdbaec52009-02-26 12:14:51 +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 /*
123 * Attempt to create a filesystem node with group id (sgid)
124 * bit set on a directory with group id (sgid) bit set
subrata_modak4bb656a2009-02-26 12:02:09 +0000125 * such that, the node created by mknod(2) should have
126 * group id (sgid) bit set and node's gid should be equal
plars865695b2001-08-27 22:15:12 +0000127 * to that of effective gid of the process.
128 */
129 TEST(mknod(node_name, MODE_SGID, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000130
plars865695b2001-08-27 22:15:12 +0000131 /* Check return code from mknod(2) */
132 if (TEST_RETURN == -1) {
133 tst_resm(TFAIL, "mknod(%s, %#o, 0) failed, errno=%d : "
134 "%s", node_name, MODE_SGID, TEST_ERRNO,
135 strerror(TEST_ERRNO));
136 continue;
137 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 /* Set the functionality flag */
139 fflag = 1;
plars865695b2001-08-27 22:15:12 +0000140
Cyril Hrubise38b9612014-06-02 17:20:57 +0200141 /* Check for node's creation */
142 if (stat(node_name, &buf) < 0) {
143 tst_resm(TFAIL, "stat() of %s failed, errno:%d",
144 node_name, TEST_ERRNO);
145 /* unset functionality flag */
146 fflag = 0;
147 }
plars865695b2001-08-27 22:15:12 +0000148
Cyril Hrubise38b9612014-06-02 17:20:57 +0200149 /* Verify mode permissions of node */
150 if (!(buf.st_mode & S_ISGID)) {
151 tst_resm(TFAIL,
152 "%s: Incorrect modes, setgid bit not "
153 "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 */
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_SGID);
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/*
191 * setup(void) - performs all ONE TIME setup for this test.
192 * Exit the test program on receipt of unexpected signals.
193 * Create a temporary directory used to hold test directories created
194 * and change the directory to it.
195 * Verify that pid of process executing the test is root.
subrata_modak4bb656a2009-02-26 12:02:09 +0000196 * Create a test directory on temporary directory and set the ownership
plars865695b2001-08-27 22:15:12 +0000197 * of test directory to guest user and process, change mode permissions
198 * to set group id bit on it.
199 * Set the effective uid/gid of the process to that of guest user.
200 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400201void setup(void)
plars865695b2001-08-27 22:15:12 +0000202{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200203 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000204
205 /* Capture unexpected signals */
206 tst_sig(NOFORK, DEF_HANDLER, cleanup);
207
plars865695b2001-08-27 22:15:12 +0000208 TEST_PAUSE;
209
210 /* Make a temp dir and cd to it */
211 tst_tmpdir();
212
subrata_modak56207ce2009-03-23 13:35:39 +0000213 /* fix permissions on the tmpdir */
214 if (chmod(".", 0711) != 0) {
215 tst_brkm(TBROK, cleanup, "chmod() failed");
216 }
plars05a3b8b2001-09-05 15:37:06 +0000217
plars865695b2001-08-27 22:15:12 +0000218 /* Save the real user id of the current test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000219 save_myuid = getuid();
plars865695b2001-08-27 22:15:12 +0000220 /* Save the process id of the current test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000221 mypid = getpid();
plars865695b2001-08-27 22:15:12 +0000222
223 /* Get the node name to be created in the test */
224 sprintf(node_name, TNODE, mypid);
225
226 /* Get the uid/gid of ltpuser user */
227 if ((user1 = getpwnam(LTPUSER)) == NULL) {
228 tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER);
229 }
230 user1_uid = user1->pw_uid;
231 group1_gid = user1->pw_gid;
232
233 /* Get the effective group id of the test process */
subrata_modak56207ce2009-03-23 13:35:39 +0000234 group2_gid = getegid();
plars865695b2001-08-27 22:15:12 +0000235
236 /*
237 * Create a test directory under temporary directory with the
238 * specified mode permissions, with uid/gid set to that of guest
239 * user and the test process.
240 */
241 if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
242 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
243 }
244 if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
245 tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
246 }
247 if (chmod(DIR_TEMP, MODE_SGID) < 0) {
248 tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
249 }
250
251 /*
252 * Verify that test directory created with expected permission modes
253 * and ownerships.
254 */
255 if (stat(DIR_TEMP, &buf) < 0) {
256 tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
257 }
258
259 /* Verify modes of test directory */
260 if (!(buf.st_mode & S_ISGID)) {
261 tst_brkm(TBROK, cleanup,
262 "%s: Incorrect modes, setgid bit not set", DIR_TEMP);
263 }
264
265 /* Verify group ID of test directory */
266 if (buf.st_gid != group2_gid) {
267 tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP);
268 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000269
subrata_modak56207ce2009-03-23 13:35:39 +0000270 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000271 * Set the effective group id and user id of the test process
plars865695b2001-08-27 22:15:12 +0000272 * to that of guest user (nobody)
273 */
274 if (setgid(group1_gid) < 0) {
275 tst_brkm(TBROK, cleanup,
276 "Unable to set process gid to that of ltp user");
277 }
278 if (setreuid(-1, user1_uid) < 0) {
279 tst_brkm(TBROK, cleanup,
280 "Unable to set process uid to that of ltp user");
281 }
282
283 /* Save the real group ID of the current process */
284 mygid = getgid();
285
286 /* Change directory to DIR_TEMP */
287 if (chdir(DIR_TEMP) < 0) {
288 tst_brkm(TBROK, cleanup,
289 "Unable to change to %s directory", DIR_TEMP);
290 }
291}
292
293/*
294 * cleanup() - Performs all ONE TIME cleanup for this test at
295 * completion or premature exit.
296 * Print test timing stats and errno log if test executed with options.
297 * Restore the real/effective user id of the process changed during
298 * setup().
299 * Remove temporary directory and sub-directories/files under it
300 * created during setup().
301 * Exit the test program with normal exit code.
302 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400303void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000304{
plars865695b2001-08-27 22:15:12 +0000305
306 /*
307 * Restore the effective uid of the process changed in the
308 * setup().
309 */
310 if (setreuid(-1, save_myuid) < 0) {
311 tst_brkm(TBROK, NULL,
312 "resetting process real/effective uid failed");
313 }
314
plars865695b2001-08-27 22:15:12 +0000315 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000316
Chris Dearmanec6edca2012-10-17 19:54:01 -0700317}