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: mknod05 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * Test Description: 24f08c3bdfSopenharmony_ci * Verify that mknod(2) succeeds when used by root to create a filesystem 25f08c3bdfSopenharmony_ci * node with set group-ID bit set on a directory with set group-ID bit set. 26f08c3bdfSopenharmony_ci * The node created should have set group-ID bit set and its gid should be 27f08c3bdfSopenharmony_ci * equal to that of its parent directory. 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * Expected Result: 30f08c3bdfSopenharmony_ci * mknod() should return value 0 on success and node created should have 31f08c3bdfSopenharmony_ci * set group-ID bit set and its gid should be equal to that of its parent 32f08c3bdfSopenharmony_ci * directory. 33f08c3bdfSopenharmony_ci * 34f08c3bdfSopenharmony_ci * Algorithm: 35f08c3bdfSopenharmony_ci * Setup: 36f08c3bdfSopenharmony_ci * Setup signal handling. 37f08c3bdfSopenharmony_ci * Create temporary directory. 38f08c3bdfSopenharmony_ci * Pause for SIGUSR1 if option specified. 39f08c3bdfSopenharmony_ci * 40f08c3bdfSopenharmony_ci * Test: 41f08c3bdfSopenharmony_ci * Loop if the proper options are given. 42f08c3bdfSopenharmony_ci * Execute system call 43f08c3bdfSopenharmony_ci * Check return code, if system call failed (return=-1) 44f08c3bdfSopenharmony_ci * Log the errno and Issue a FAIL message. 45f08c3bdfSopenharmony_ci * Otherwise, 46f08c3bdfSopenharmony_ci * Verify the Functionality of system call 47f08c3bdfSopenharmony_ci * if successful, 48f08c3bdfSopenharmony_ci * Issue Functionality-Pass message. 49f08c3bdfSopenharmony_ci * Otherwise, 50f08c3bdfSopenharmony_ci * Issue Functionality-Fail message. 51f08c3bdfSopenharmony_ci * Cleanup: 52f08c3bdfSopenharmony_ci * Print errno log and/or timing stats if options given 53f08c3bdfSopenharmony_ci * Delete the temporary directory created. 54f08c3bdfSopenharmony_ci * 55f08c3bdfSopenharmony_ci * Usage: <for command-line> 56f08c3bdfSopenharmony_ci * mknod05 [-c n] [-f] [-i n] [-I x] [-P x] [-t] 57f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 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 MODE_SGID S_IFIFO | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO 88f08c3bdfSopenharmony_ci#define DIR_TEMP "testdir_5" 89f08c3bdfSopenharmony_ci#define TNODE "tnode_%d" 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_cistruct stat buf; /* struct. to hold stat(2) o/p contents */ 92f08c3bdfSopenharmony_cistruct passwd *user1; /* struct. to hold getpwnam(3) o/p contents */ 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_cichar *TCID = "mknod05"; 95f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 96f08c3bdfSopenharmony_cichar node_name[PATH_MAX]; /* buffer to hold node name created */ 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_cigid_t group1_gid, group2_gid, mygid; /* user and process group id's */ 99f08c3bdfSopenharmony_ciuid_t save_myuid, user1_uid; /* user and process user id's */ 100f08c3bdfSopenharmony_cipid_t mypid; /* process id */ 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_civoid setup(); /* setup function for the test */ 103f08c3bdfSopenharmony_civoid cleanup(); /* cleanup function for the test */ 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ciint main(int ac, char **av) 106f08c3bdfSopenharmony_ci{ 107f08c3bdfSopenharmony_ci int lc; 108f08c3bdfSopenharmony_ci int fflag; 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci setup(); 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_ci tst_count = 0; 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci /* 119f08c3bdfSopenharmony_ci * Attempt to create a filesystem node with group id (sgid) 120f08c3bdfSopenharmony_ci * bit set on a directory with group id (sgid) bit set 121f08c3bdfSopenharmony_ci * such that, the node created by mknod(2) should have 122f08c3bdfSopenharmony_ci * group id (sgid) bit set and node's gid should be equal 123f08c3bdfSopenharmony_ci * to that of effective gid of the process. 124f08c3bdfSopenharmony_ci */ 125f08c3bdfSopenharmony_ci TEST(mknod(node_name, MODE_SGID, 0)); 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_ci /* Check return code from mknod(2) */ 128f08c3bdfSopenharmony_ci if (TEST_RETURN == -1) { 129f08c3bdfSopenharmony_ci tst_resm(TFAIL, "mknod(%s, %#o, 0) failed, errno=%d : " 130f08c3bdfSopenharmony_ci "%s", node_name, MODE_SGID, TEST_ERRNO, 131f08c3bdfSopenharmony_ci strerror(TEST_ERRNO)); 132f08c3bdfSopenharmony_ci continue; 133f08c3bdfSopenharmony_ci } 134f08c3bdfSopenharmony_ci /* Set the functionality flag */ 135f08c3bdfSopenharmony_ci fflag = 1; 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci /* Check for node's creation */ 138f08c3bdfSopenharmony_ci if (stat(node_name, &buf) < 0) { 139f08c3bdfSopenharmony_ci tst_resm(TFAIL, "stat() of %s failed, errno:%d", 140f08c3bdfSopenharmony_ci node_name, TEST_ERRNO); 141f08c3bdfSopenharmony_ci /* unset functionality flag */ 142f08c3bdfSopenharmony_ci fflag = 0; 143f08c3bdfSopenharmony_ci } 144f08c3bdfSopenharmony_ci 145f08c3bdfSopenharmony_ci /* Verify mode permissions of node */ 146f08c3bdfSopenharmony_ci if (!(buf.st_mode & S_ISGID)) { 147f08c3bdfSopenharmony_ci tst_resm(TFAIL, "%s: Incorrect modes, " 148f08c3bdfSopenharmony_ci "setgid bit not set", node_name); 149f08c3bdfSopenharmony_ci /* unset flag as functionality fails */ 150f08c3bdfSopenharmony_ci fflag = 0; 151f08c3bdfSopenharmony_ci } 152f08c3bdfSopenharmony_ci 153f08c3bdfSopenharmony_ci /* Verify group ID */ 154f08c3bdfSopenharmony_ci if (buf.st_gid != group2_gid) { 155f08c3bdfSopenharmony_ci tst_resm(TFAIL, "%s: Incorrect group", 156f08c3bdfSopenharmony_ci node_name); 157f08c3bdfSopenharmony_ci /* unset flag as functionality fails */ 158f08c3bdfSopenharmony_ci fflag = 0; 159f08c3bdfSopenharmony_ci } 160f08c3bdfSopenharmony_ci if (fflag) { 161f08c3bdfSopenharmony_ci tst_resm(TPASS, "Functionality of mknod(%s, " 162f08c3bdfSopenharmony_ci "%#o, 0) successful", 163f08c3bdfSopenharmony_ci node_name, MODE_SGID); 164f08c3bdfSopenharmony_ci } 165f08c3bdfSopenharmony_ci 166f08c3bdfSopenharmony_ci /* Remove the node for the next go `round */ 167f08c3bdfSopenharmony_ci if (unlink(node_name) == -1) { 168f08c3bdfSopenharmony_ci tst_resm(TWARN, "unlink(%s) failed, errno:%d %s", 169f08c3bdfSopenharmony_ci node_name, errno, strerror(errno)); 170f08c3bdfSopenharmony_ci } 171f08c3bdfSopenharmony_ci } 172f08c3bdfSopenharmony_ci 173f08c3bdfSopenharmony_ci /* change the directory back to temporary directory */ 174f08c3bdfSopenharmony_ci SAFE_CHDIR(cleanup, ".."); 175f08c3bdfSopenharmony_ci 176f08c3bdfSopenharmony_ci /* 177f08c3bdfSopenharmony_ci * Invoke cleanup() to delete the test directories created 178f08c3bdfSopenharmony_ci * in the setup() and exit main(). 179f08c3bdfSopenharmony_ci */ 180f08c3bdfSopenharmony_ci cleanup(); 181f08c3bdfSopenharmony_ci 182f08c3bdfSopenharmony_ci tst_exit(); 183f08c3bdfSopenharmony_ci} 184f08c3bdfSopenharmony_ci 185f08c3bdfSopenharmony_ci/* 186f08c3bdfSopenharmony_ci * setup(void) - performs all ONE TIME setup for this test. 187f08c3bdfSopenharmony_ci * Exit the test program on receipt of unexpected signals. 188f08c3bdfSopenharmony_ci * Create a temporary directory used to hold test directories created 189f08c3bdfSopenharmony_ci * and change the directory to it. 190f08c3bdfSopenharmony_ci * Verify that pid of process executing the test is root. 191f08c3bdfSopenharmony_ci * Create a test directory on temporary directory and set the ownership 192f08c3bdfSopenharmony_ci * of test directory to guest user and process, change mode permissions 193f08c3bdfSopenharmony_ci * to set group id bit on it. 194f08c3bdfSopenharmony_ci */ 195f08c3bdfSopenharmony_civoid setup(void) 196f08c3bdfSopenharmony_ci{ 197f08c3bdfSopenharmony_ci tst_require_root(); 198f08c3bdfSopenharmony_ci 199f08c3bdfSopenharmony_ci /* Capture unexpected signals */ 200f08c3bdfSopenharmony_ci tst_sig(NOFORK, DEF_HANDLER, cleanup); 201f08c3bdfSopenharmony_ci 202f08c3bdfSopenharmony_ci TEST_PAUSE; 203f08c3bdfSopenharmony_ci 204f08c3bdfSopenharmony_ci /* Make a temp dir and cd to it */ 205f08c3bdfSopenharmony_ci tst_tmpdir(); 206f08c3bdfSopenharmony_ci 207f08c3bdfSopenharmony_ci /* Save the real user id of the current test process */ 208f08c3bdfSopenharmony_ci save_myuid = getuid(); 209f08c3bdfSopenharmony_ci 210f08c3bdfSopenharmony_ci /* Save the process id of the current test process */ 211f08c3bdfSopenharmony_ci mypid = getpid(); 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_ci /* Get the node name to be created in the test */ 214f08c3bdfSopenharmony_ci sprintf(node_name, TNODE, mypid); 215f08c3bdfSopenharmony_ci 216f08c3bdfSopenharmony_ci /* Get the uid/gid of ltp user */ 217f08c3bdfSopenharmony_ci if ((user1 = getpwnam(LTPUSER)) == NULL) { 218f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER); 219f08c3bdfSopenharmony_ci } 220f08c3bdfSopenharmony_ci user1_uid = user1->pw_uid; 221f08c3bdfSopenharmony_ci group1_gid = user1->pw_gid; 222f08c3bdfSopenharmony_ci 223f08c3bdfSopenharmony_ci /* Get the effective group id of the test process */ 224f08c3bdfSopenharmony_ci group2_gid = getegid(); 225f08c3bdfSopenharmony_ci 226f08c3bdfSopenharmony_ci /* 227f08c3bdfSopenharmony_ci * Create a test directory under temporary directory with the 228f08c3bdfSopenharmony_ci * specified mode permissions, with uid/gid set to that of guest 229f08c3bdfSopenharmony_ci * user and the test process. 230f08c3bdfSopenharmony_ci */ 231f08c3bdfSopenharmony_ci SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX); 232f08c3bdfSopenharmony_ci SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid); 233f08c3bdfSopenharmony_ci SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID); 234f08c3bdfSopenharmony_ci 235f08c3bdfSopenharmony_ci /* 236f08c3bdfSopenharmony_ci * Verify that test directory created with expected permission modes 237f08c3bdfSopenharmony_ci * and ownerships. 238f08c3bdfSopenharmony_ci */ 239f08c3bdfSopenharmony_ci SAFE_STAT(cleanup, DIR_TEMP, &buf); 240f08c3bdfSopenharmony_ci /* Verify modes of test directory */ 241f08c3bdfSopenharmony_ci if (!(buf.st_mode & S_ISGID)) { 242f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, 243f08c3bdfSopenharmony_ci "%s: Incorrect modes, setgid bit not set", DIR_TEMP); 244f08c3bdfSopenharmony_ci } 245f08c3bdfSopenharmony_ci 246f08c3bdfSopenharmony_ci /* Verify group ID of test directory */ 247f08c3bdfSopenharmony_ci if (buf.st_gid != group2_gid) { 248f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP); 249f08c3bdfSopenharmony_ci } 250f08c3bdfSopenharmony_ci 251f08c3bdfSopenharmony_ci /* Change directory to DIR_TEMP */ 252f08c3bdfSopenharmony_ci SAFE_CHDIR(cleanup, DIR_TEMP); 253f08c3bdfSopenharmony_ci} 254f08c3bdfSopenharmony_ci 255f08c3bdfSopenharmony_ci/* 256f08c3bdfSopenharmony_ci * cleanup() - Performs all ONE TIME cleanup for this test at 257f08c3bdfSopenharmony_ci * completion or premature exit. 258f08c3bdfSopenharmony_ci * Print test timing stats and errno log if test executed with options. 259f08c3bdfSopenharmony_ci * Remove temporary directory and sub-directories/files under it 260f08c3bdfSopenharmony_ci * created during setup(). 261f08c3bdfSopenharmony_ci * Exit the test program with normal exit code. 262f08c3bdfSopenharmony_ci */ 263f08c3bdfSopenharmony_civoid cleanup(void) 264f08c3bdfSopenharmony_ci{ 265f08c3bdfSopenharmony_ci 266f08c3bdfSopenharmony_ci tst_rmdir(); 267f08c3bdfSopenharmony_ci 268f08c3bdfSopenharmony_ci} 269