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 | /* |
| 21 | * Test Name: mknod03 |
| 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 with set group-ID bit set on a directory with set group-ID bit set. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * The node created should have set group-ID bit set and its gid should be |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 46 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | * 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" |
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 MODE_SGID S_IFIFO | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO |
| 87 | #define DIR_TEMP "testdir_3" |
| 88 | #define TNODE "tnode_%d" |
| 89 | |
| 90 | struct stat buf; /* struct. to hold stat(2) o/p contents */ |
| 91 | struct passwd *user1; /* struct. to hold getpwnam(3) o/p contents */ |
| 92 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 93 | char *TCID = "mknod03"; |
| 94 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | char node_name[PATH_MAX]; /* buffer to hold node name created */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
| 97 | gid_t group1_gid, group2_gid, mygid; /* user and process group id's */ |
| 98 | uid_t save_myuid, user1_uid; /* user and process user id's */ |
| 99 | pid_t mypid; /* process id */ |
| 100 | |
| 101 | void setup(); /* setup function for the test */ |
| 102 | void cleanup(); /* cleanup function for the test */ |
| 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 106 | int lc; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 107 | int fflag; /* functionality flag variable */ |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 108 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 110 | msg = parse_opts(ac, av, NULL, NULL); |
| 111 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | } |
| 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | setup(); |
| 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 119 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 120 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
| 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 125 | * such that, the node created by mknod(2) should have |
| 126 | * group id (sgid) bit set and node's gid should be equal |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | * to that of effective gid of the process. |
| 128 | */ |
| 129 | TEST(mknod(node_name, MODE_SGID, 0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 130 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | /* 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 Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | /* Set the functionality flag */ |
| 139 | fflag = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 141 | /* 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 149 | /* 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 158 | /* 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | node_name, errno, strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
| 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 Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 187 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 188 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
| 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 196 | * Create a test directory on temporary directory and set the ownership |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | * 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 201 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 203 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
| 205 | /* Capture unexpected signals */ |
| 206 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 207 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | TEST_PAUSE; |
| 209 | |
| 210 | /* Make a temp dir and cd to it */ |
| 211 | tst_tmpdir(); |
| 212 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | /* fix permissions on the tmpdir */ |
| 214 | if (chmod(".", 0711) != 0) { |
| 215 | tst_brkm(TBROK, cleanup, "chmod() failed"); |
| 216 | } |
plars | 05a3b8b | 2001-09-05 15:37:06 +0000 | [diff] [blame] | 217 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | /* Save the real user id of the current test process */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | save_myuid = getuid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | /* Save the process id of the current test process */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | mypid = getpid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 234 | group2_gid = getegid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 269 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 270 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 271 | * Set the effective group id and user id of the test process |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 272 | * 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 303 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 304 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 305 | |
| 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 315 | tst_rmdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 316 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 317 | } |