plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: mknod08 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * Verify that mknod(2) succeeds when used to create a filesystem |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | * 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 55 | * mknod08 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | * 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 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 | |
| 89 | struct stat buf; /* struct. to hold stat(2) o/p contents */ |
| 90 | struct passwd *user1; /* struct. to hold getpwnam(3) o/p contents */ |
| 91 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 92 | char *TCID = "mknod08"; |
| 93 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | char node_name[PATH_MAX]; /* buffer to hold node name created */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | gid_t group1_gid, group2_gid, mygid; /* user and process group id's */ |
| 97 | uid_t save_myuid, user1_uid; /* user and process user id's */ |
| 98 | pid_t mypid; /* process id */ |
| 99 | |
| 100 | void setup(); /* setup function for the test */ |
| 101 | void cleanup(); /* cleanup function for the test */ |
| 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 105 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | int fflag; /* functionality flag variable */ |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 107 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 109 | msg = parse_opts(ac, av, NULL, NULL); |
| 110 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | setup(); |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 118 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 119 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 126 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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_RWX, TEST_ERRNO, |
| 132 | strerror(TEST_ERRNO)); |
| 133 | continue; |
| 134 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 135 | /* Set the functionality flag */ |
| 136 | fflag = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | /* 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 147 | /* 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 155 | /* 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* Remove the node for the next go `round */ |
| 169 | if (unlink(node_name) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | tst_resm(TWARN, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | "unlink(%s) failed, errno:%d %s", |
| 172 | node_name, errno, strerror(errno)); |
| 173 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
| 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 Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 185 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 186 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * setup(void) - performs all ONE TIME setup for this test. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | * Exit the test program on receipt of unexpected signals. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | * 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 198 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 200 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | |
| 202 | /* Capture unexpected signals */ |
| 203 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 204 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | TEST_PAUSE; |
| 206 | |
| 207 | /* Make a temp dir and cd to it */ |
| 208 | tst_tmpdir(); |
| 209 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | /* fix permissions on the tmpdir */ |
| 211 | if (chmod(".", 0711) != 0) { |
| 212 | tst_brkm(TBROK, cleanup, "chmod() failed"); |
| 213 | } |
plars | 05a3b8b | 2001-09-05 15:37:06 +0000 | [diff] [blame] | 214 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | /* Save the real user id of the test process */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | save_myuid = getuid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | |
| 218 | /* Save the process id of the test process */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | mypid = getpid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | } |
| 228 | user1_uid = user1->pw_uid; |
| 229 | group1_gid = user1->pw_gid; |
| 230 | |
| 231 | /* Get effective group id of the test process */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | group2_gid = getegid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 264 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 265 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 266 | * Set the effective group id and user id of the test process |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 267 | * 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 298 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 299 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 300 | |
| 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 310 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 311 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 312 | } |