1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* 21f08c3bdfSopenharmony_ci * Test Name: mknod08 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * Test Description: 24f08c3bdfSopenharmony_ci * Verify that mknod(2) succeeds when used to create a filesystem 25f08c3bdfSopenharmony_ci * node on a directory without set group-ID bit set. The node created 26f08c3bdfSopenharmony_ci * should not have set group-ID bit set and its gid should be equal to that 27f08c3bdfSopenharmony_ci * of its parent directory. 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * Expected Result: 30f08c3bdfSopenharmony_ci * mknod() should return value 0 on success and node created should not 31f08c3bdfSopenharmony_ci * have set group-ID bit set. 32f08c3bdfSopenharmony_ci * 33f08c3bdfSopenharmony_ci * Algorithm: 34f08c3bdfSopenharmony_ci * Setup: 35f08c3bdfSopenharmony_ci * Setup signal handling. 36f08c3bdfSopenharmony_ci * Create temporary directory. 37f08c3bdfSopenharmony_ci * Pause for SIGUSR1 if option specified. 38f08c3bdfSopenharmony_ci * 39f08c3bdfSopenharmony_ci * Test: 40f08c3bdfSopenharmony_ci * Loop if the proper options are given. 41f08c3bdfSopenharmony_ci * Execute system call 42f08c3bdfSopenharmony_ci * Check return code, if system call failed (return=-1) 43f08c3bdfSopenharmony_ci * Log the errno and Issue a FAIL message. 44f08c3bdfSopenharmony_ci * Otherwise, 45f08c3bdfSopenharmony_ci * Verify the Functionality of system call 46f08c3bdfSopenharmony_ci * if successful, 47f08c3bdfSopenharmony_ci * Issue Functionality-Pass message. 48f08c3bdfSopenharmony_ci * Otherwise, 49f08c3bdfSopenharmony_ci * Issue Functionality-Fail message. 50f08c3bdfSopenharmony_ci * Cleanup: 51f08c3bdfSopenharmony_ci * Print errno log and/or timing stats if options given 52f08c3bdfSopenharmony_ci * Delete the temporary directory created. 53f08c3bdfSopenharmony_ci * 54f08c3bdfSopenharmony_ci * Usage: <for command-line> 55f08c3bdfSopenharmony_ci * mknod08 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] 56f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 57f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 58f08c3bdfSopenharmony_ci * -f : Turn off functionality Testing. 59f08c3bdfSopenharmony_ci * -i n : Execute test n times. 60f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 61f08c3bdfSopenharmony_ci * -P x : Pause for x seconds between iterations. 62f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 63f08c3bdfSopenharmony_ci * 64f08c3bdfSopenharmony_ci * HISTORY 65f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 66f08c3bdfSopenharmony_ci * 67f08c3bdfSopenharmony_ci * RESTRICTIONS: 68f08c3bdfSopenharmony_ci * This test should be run by 'super-user' (root) only. 69f08c3bdfSopenharmony_ci * 70f08c3bdfSopenharmony_ci */ 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci#include <stdio.h> 73f08c3bdfSopenharmony_ci#include <stdlib.h> 74f08c3bdfSopenharmony_ci#include <unistd.h> 75f08c3bdfSopenharmony_ci#include <errno.h> 76f08c3bdfSopenharmony_ci#include <string.h> 77f08c3bdfSopenharmony_ci#include <signal.h> 78f08c3bdfSopenharmony_ci#include <pwd.h> 79f08c3bdfSopenharmony_ci#include <sys/types.h> 80f08c3bdfSopenharmony_ci#include <sys/stat.h> 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci#include "test.h" 83f08c3bdfSopenharmony_ci#include "safe_macros.h" 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci#define LTPUSER "nobody" 86f08c3bdfSopenharmony_ci#define MODE_RWX S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO 87f08c3bdfSopenharmony_ci#define DIR_TEMP "testdir_1" 88f08c3bdfSopenharmony_ci#define TNODE "tnode_%d" 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_cistruct stat buf; /* struct. to hold stat(2) o/p contents */ 91f08c3bdfSopenharmony_cistruct passwd *user1; /* struct. to hold getpwnam(3) o/p contents */ 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_cichar *TCID = "mknod08"; 94f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 95f08c3bdfSopenharmony_cichar node_name[PATH_MAX]; /* buffer to hold node name created */ 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_cigid_t group1_gid, group2_gid, mygid; /* user and process group id's */ 98f08c3bdfSopenharmony_ciuid_t save_myuid, user1_uid; /* user and process user id's */ 99f08c3bdfSopenharmony_cipid_t mypid; /* process id */ 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_civoid setup(); /* setup function for the test */ 102f08c3bdfSopenharmony_civoid cleanup(); /* cleanup function for the test */ 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ciint main(int ac, char **av) 105f08c3bdfSopenharmony_ci{ 106f08c3bdfSopenharmony_ci int lc; 107f08c3bdfSopenharmony_ci int fflag; 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci setup(); 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci tst_count = 0; 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci /* 118f08c3bdfSopenharmony_ci * Call mknod() to creat a node on a directory without 119f08c3bdfSopenharmony_ci * set group-ID (sgid) bit set. 120f08c3bdfSopenharmony_ci */ 121f08c3bdfSopenharmony_ci TEST(mknod(node_name, MODE_RWX, 0)); 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci /* Check return code from mknod(2) */ 124f08c3bdfSopenharmony_ci if (TEST_RETURN == -1) { 125f08c3bdfSopenharmony_ci tst_resm(TFAIL, 126f08c3bdfSopenharmony_ci "mknod(%s, %#o, 0) failed, errno=%d : %s", 127f08c3bdfSopenharmony_ci node_name, MODE_RWX, TEST_ERRNO, 128f08c3bdfSopenharmony_ci strerror(TEST_ERRNO)); 129f08c3bdfSopenharmony_ci continue; 130f08c3bdfSopenharmony_ci } 131f08c3bdfSopenharmony_ci /* Set the functionality flag */ 132f08c3bdfSopenharmony_ci fflag = 1; 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_ci /* Check for node's creation */ 135f08c3bdfSopenharmony_ci if (stat(node_name, &buf) < 0) { 136f08c3bdfSopenharmony_ci tst_resm(TFAIL, 137f08c3bdfSopenharmony_ci "stat() of %s failed, errno:%d", 138f08c3bdfSopenharmony_ci node_name, TEST_ERRNO); 139f08c3bdfSopenharmony_ci /* unset flag as functionality fails */ 140f08c3bdfSopenharmony_ci fflag = 0; 141f08c3bdfSopenharmony_ci } 142f08c3bdfSopenharmony_ci 143f08c3bdfSopenharmony_ci /* Verify mode permissions of node */ 144f08c3bdfSopenharmony_ci if (buf.st_mode & S_ISGID) { 145f08c3bdfSopenharmony_ci tst_resm(TFAIL, "%s: Incorrect modes, setgid " 146f08c3bdfSopenharmony_ci "bit set", node_name); 147f08c3bdfSopenharmony_ci /* unset flag as functionality fails */ 148f08c3bdfSopenharmony_ci fflag = 0; 149f08c3bdfSopenharmony_ci } 150f08c3bdfSopenharmony_ci 151f08c3bdfSopenharmony_ci /* Verify group ID */ 152f08c3bdfSopenharmony_ci if (buf.st_gid != mygid) { 153f08c3bdfSopenharmony_ci tst_resm(TFAIL, "%s: Incorrect group", 154f08c3bdfSopenharmony_ci node_name); 155f08c3bdfSopenharmony_ci /* unset flag as functionality fails */ 156f08c3bdfSopenharmony_ci fflag = 0; 157f08c3bdfSopenharmony_ci } 158f08c3bdfSopenharmony_ci if (fflag) { 159f08c3bdfSopenharmony_ci tst_resm(TPASS, "Functionality of mknod(%s, " 160f08c3bdfSopenharmony_ci "%#o, 0) successful", 161f08c3bdfSopenharmony_ci node_name, MODE_RWX); 162f08c3bdfSopenharmony_ci } 163f08c3bdfSopenharmony_ci 164f08c3bdfSopenharmony_ci /* Remove the node for the next go `round */ 165f08c3bdfSopenharmony_ci if (unlink(node_name) == -1) { 166f08c3bdfSopenharmony_ci tst_resm(TWARN, 167f08c3bdfSopenharmony_ci "unlink(%s) failed, errno:%d %s", 168f08c3bdfSopenharmony_ci node_name, errno, strerror(errno)); 169f08c3bdfSopenharmony_ci } 170f08c3bdfSopenharmony_ci } 171f08c3bdfSopenharmony_ci 172f08c3bdfSopenharmony_ci /* Change the directory back to temporary directory */ 173f08c3bdfSopenharmony_ci SAFE_CHDIR(cleanup, ".."); 174f08c3bdfSopenharmony_ci 175f08c3bdfSopenharmony_ci /* 176f08c3bdfSopenharmony_ci * Invoke cleanup() to delete the test directories created 177f08c3bdfSopenharmony_ci * in the setup() and exit main(). 178f08c3bdfSopenharmony_ci */ 179f08c3bdfSopenharmony_ci cleanup(); 180f08c3bdfSopenharmony_ci 181f08c3bdfSopenharmony_ci tst_exit(); 182f08c3bdfSopenharmony_ci} 183f08c3bdfSopenharmony_ci 184f08c3bdfSopenharmony_ci/* 185f08c3bdfSopenharmony_ci * setup(void) - performs all ONE TIME setup for this test. 186f08c3bdfSopenharmony_ci * Exit the test program on receipt of unexpected signals. 187f08c3bdfSopenharmony_ci * Create a temporary directory used to hold test directories created 188f08c3bdfSopenharmony_ci * and change the directory to it. 189f08c3bdfSopenharmony_ci * Verify that pid of process executing the test is root. 190f08c3bdfSopenharmony_ci * Create a test directory on temporary directory and set the ownership 191f08c3bdfSopenharmony_ci * of test directory to nobody user. 192f08c3bdfSopenharmony_ci * Set the effective uid/gid of the process to that of nobody user. 193f08c3bdfSopenharmony_ci */ 194f08c3bdfSopenharmony_civoid setup(void) 195f08c3bdfSopenharmony_ci{ 196f08c3bdfSopenharmony_ci tst_require_root(); 197f08c3bdfSopenharmony_ci 198f08c3bdfSopenharmony_ci /* Capture unexpected signals */ 199f08c3bdfSopenharmony_ci tst_sig(NOFORK, DEF_HANDLER, cleanup); 200f08c3bdfSopenharmony_ci 201f08c3bdfSopenharmony_ci TEST_PAUSE; 202f08c3bdfSopenharmony_ci 203f08c3bdfSopenharmony_ci /* Make a temp dir and cd to it */ 204f08c3bdfSopenharmony_ci tst_tmpdir(); 205f08c3bdfSopenharmony_ci 206f08c3bdfSopenharmony_ci /* fix permissions on the tmpdir */ 207f08c3bdfSopenharmony_ci if (chmod(".", 0711) != 0) { 208f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "chmod() failed"); 209f08c3bdfSopenharmony_ci } 210f08c3bdfSopenharmony_ci 211f08c3bdfSopenharmony_ci /* Save the real user id of the test process */ 212f08c3bdfSopenharmony_ci save_myuid = getuid(); 213f08c3bdfSopenharmony_ci 214f08c3bdfSopenharmony_ci /* Save the process id of the test process */ 215f08c3bdfSopenharmony_ci mypid = getpid(); 216f08c3bdfSopenharmony_ci 217f08c3bdfSopenharmony_ci /* Get the node name to be created in the test */ 218f08c3bdfSopenharmony_ci sprintf(node_name, TNODE, mypid); 219f08c3bdfSopenharmony_ci 220f08c3bdfSopenharmony_ci /* Get the uid/gid of guest user - nobody */ 221f08c3bdfSopenharmony_ci if ((user1 = getpwnam(LTPUSER)) == NULL) { 222f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER); 223f08c3bdfSopenharmony_ci } 224f08c3bdfSopenharmony_ci user1_uid = user1->pw_uid; 225f08c3bdfSopenharmony_ci group1_gid = user1->pw_gid; 226f08c3bdfSopenharmony_ci 227f08c3bdfSopenharmony_ci /* Get effective group id of the test process */ 228f08c3bdfSopenharmony_ci group2_gid = getegid(); 229f08c3bdfSopenharmony_ci 230f08c3bdfSopenharmony_ci /* 231f08c3bdfSopenharmony_ci * Create a test directory under temporary directory with the 232f08c3bdfSopenharmony_ci * specified mode permissions, with uid/gid set to that of guest 233f08c3bdfSopenharmony_ci * user and the test process. 234f08c3bdfSopenharmony_ci */ 235f08c3bdfSopenharmony_ci SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX); 236f08c3bdfSopenharmony_ci SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid); 237f08c3bdfSopenharmony_ci 238f08c3bdfSopenharmony_ci /* 239f08c3bdfSopenharmony_ci * Verify that test directory created with expected permission modes 240f08c3bdfSopenharmony_ci * and ownerships. 241f08c3bdfSopenharmony_ci */ 242f08c3bdfSopenharmony_ci SAFE_STAT(cleanup, DIR_TEMP, &buf); 243f08c3bdfSopenharmony_ci 244f08c3bdfSopenharmony_ci /* Verify modes of test directory */ 245f08c3bdfSopenharmony_ci if (buf.st_mode & S_ISGID) { 246f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, 247f08c3bdfSopenharmony_ci "%s: Incorrect modes, setgid bit set", DIR_TEMP); 248f08c3bdfSopenharmony_ci } 249f08c3bdfSopenharmony_ci 250f08c3bdfSopenharmony_ci /* Verify group ID */ 251f08c3bdfSopenharmony_ci if (buf.st_gid != group2_gid) { 252f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP); 253f08c3bdfSopenharmony_ci } 254f08c3bdfSopenharmony_ci 255f08c3bdfSopenharmony_ci /* 256f08c3bdfSopenharmony_ci * Set the effective group id and user id of the test process 257f08c3bdfSopenharmony_ci * to that of guest user. 258f08c3bdfSopenharmony_ci */ 259f08c3bdfSopenharmony_ci SAFE_SETGID(cleanup, group1_gid); 260f08c3bdfSopenharmony_ci if (setreuid(-1, user1_uid) < 0) { 261f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, 262f08c3bdfSopenharmony_ci "Unable to set process uid to that of ltp user"); 263f08c3bdfSopenharmony_ci } 264f08c3bdfSopenharmony_ci 265f08c3bdfSopenharmony_ci /* Save the real group ID of the current process */ 266f08c3bdfSopenharmony_ci mygid = getgid(); 267f08c3bdfSopenharmony_ci 268f08c3bdfSopenharmony_ci /* Change directory to DIR_TEMP */ 269f08c3bdfSopenharmony_ci SAFE_CHDIR(cleanup, DIR_TEMP); 270f08c3bdfSopenharmony_ci} 271f08c3bdfSopenharmony_ci 272f08c3bdfSopenharmony_ci/* 273f08c3bdfSopenharmony_ci * cleanup() - Performs all ONE TIME cleanup for this test at 274f08c3bdfSopenharmony_ci * completion or premature exit. 275f08c3bdfSopenharmony_ci * Print test timing stats and errno log if test executed with options. 276f08c3bdfSopenharmony_ci * Restore the real/effective user id of the process changed during 277f08c3bdfSopenharmony_ci * setup(). 278f08c3bdfSopenharmony_ci * Remove temporary directory and sub-directories/files under it 279f08c3bdfSopenharmony_ci * created during setup(). 280f08c3bdfSopenharmony_ci * Exit the test program with normal exit code. 281f08c3bdfSopenharmony_ci */ 282f08c3bdfSopenharmony_civoid cleanup(void) 283f08c3bdfSopenharmony_ci{ 284f08c3bdfSopenharmony_ci 285f08c3bdfSopenharmony_ci /* 286f08c3bdfSopenharmony_ci * Restore the effective uid of the process changed in the 287f08c3bdfSopenharmony_ci * setup(). 288f08c3bdfSopenharmony_ci */ 289f08c3bdfSopenharmony_ci if (setreuid(-1, save_myuid) < 0) { 290f08c3bdfSopenharmony_ci tst_brkm(TBROK, NULL, 291f08c3bdfSopenharmony_ci "resetting process real/effective uid failed"); 292f08c3bdfSopenharmony_ci } 293f08c3bdfSopenharmony_ci 294f08c3bdfSopenharmony_ci tst_rmdir(); 295f08c3bdfSopenharmony_ci 296f08c3bdfSopenharmony_ci} 297