static void setup(void) { struct passwd *ltpuser; const char *fs_type; tst_require_root(NULL); tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; tst_tmpdir(); fs_type = tst_dev_fs_type(); device = tst_acquire_device(cleanup); if (!device) tst_brkm(TCONF, cleanup, "Failed to obtain block device"); SAFE_TOUCH(cleanup, TESTFILE1, FILE_MODE, NULL); ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1); SAFE_CHOWN(cleanup, TESTFILE1, ltpuser->pw_uid, ltpuser->pw_gid); SAFE_TOUCH(cleanup, TESTFILE2, FILE_MODE, NULL); ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER2); SAFE_CHOWN(cleanup, TESTFILE2, ltpuser->pw_uid, ltpuser->pw_gid); tst_mkfs(cleanup, device, fs_type, NULL); SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE); if (mount(device, MNTPOINT, fs_type, 0, NULL) == -1) { tst_brkm(TBROK | TERRNO, cleanup, "mount device:%s failed", device); } mount_flag = 1; SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL); ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1); SAFE_CHOWN(cleanup, TESTFILE3, ltpuser->pw_uid, ltpuser->pw_gid); if (mount(device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY, NULL) == -1) { tst_brkm(TBROK | TERRNO, cleanup, "mount device:%s failed", device); } ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1); SAFE_SETEUID(cleanup, ltpuser->pw_uid); }
/* * void * setup() - performs all ONE TIME setup for this test. * Create a temporary directory and change directory to it. * Create a test file under temporary directory and close it * Change the ownership of test file to that of "ltpuser1" user. */ void setup(void) { struct passwd *ltpuser; /* password struct for ltpuser1 */ struct group *ltpgroup; /* group struct for ltpuser1 */ int fd; /* file descriptor variable */ gid_t group1_gid; /* user and process group id's */ uid_t user1_uid; tst_sig(FORK, DEF_HANDLER, cleanup); TEST_PAUSE; tst_require_root(); tst_tmpdir(); /* Get the uid of guest user - ltpuser1 */ if ((ltpuser = getpwnam(LTPUSER)) == NULL) tst_brkm(TBROK, cleanup, "getpwnam failed"); user1_uid = ltpuser->pw_uid; /* Get the group id of guest user - ltpuser1 */ if ((ltpgroup = getgrnam(LTPGRP)) == NULL) tst_brkm(TBROK, cleanup, "getgrnam failed"); group1_gid = ltpgroup->gr_gid; fd = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE); SAFE_CLOSE(cleanup, fd); SAFE_CHOWN(cleanup, TESTFILE, user1_uid, group1_gid); SAFE_SETGID(cleanup, group1_gid); }
void setup(void) { struct passwd *ltpuser; struct group *ltpgroup; int fd; gid_t group1_gid; uid_t user1_uid; ltpuser = SAFE_GETPWNAM("nobody"); user1_uid = ltpuser->pw_uid; ltpgroup = SAFE_GETGRNAM_FALLBACK("users", "daemon"); group1_gid = ltpgroup->gr_gid; fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE); SAFE_CLOSE(fd); SAFE_CHOWN(TESTFILE, user1_uid, group1_gid); SAFE_SETGID(group1_gid); }
/* * setup(void) - performs all ONE TIME setup for this test. * Exit the test program on receipt of unexpected signals. * Create a temporary directory used to hold test directories created * and change the directory to it. * Verify that pid of process executing the test is root. * Create a test directory on temporary directory and set the ownership * of test directory to guest user and process, change mode permissions * to set group id bit on it. * Set the effective uid/gid of the process to that of guest user. */ void setup(void) { tst_require_root(); /* Capture unexpected signals */ tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; /* Make a temp dir and cd to it */ tst_tmpdir(); /* fix permissions on the tmpdir */ if (chmod(".", 0711) != 0) { tst_brkm(TBROK, cleanup, "chmod() failed"); } /* Save the real user id of the current test process */ save_myuid = getuid(); /* Save the process id of the current test process */ mypid = getpid(); /* Get the node name to be created in the test */ sprintf(node_name, TNODE, mypid); /* Get the uid/gid of ltpuser user */ if ((user1 = getpwnam(LTPUSER)) == NULL) { tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER); } user1_uid = user1->pw_uid; group1_gid = user1->pw_gid; /* Get the effective group id of the test process */ group2_gid = getegid(); /* * Create a test directory under temporary directory with the * specified mode permissions, with uid/gid set to that of guest * user and the test process. */ SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX); SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid); SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID); /* * Verify that test directory created with expected permission modes * and ownerships. */ SAFE_STAT(cleanup, DIR_TEMP, &buf); /* Verify modes of test directory */ if (!(buf.st_mode & S_ISGID)) { tst_brkm(TBROK, cleanup, "%s: Incorrect modes, setgid bit not set", DIR_TEMP); } /* Verify group ID of test directory */ if (buf.st_gid != group2_gid) { tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP); } /* * Set the effective group id and user id of the test process * to that of guest user (nobody) */ SAFE_SETGID(cleanup, group1_gid); if (setreuid(-1, user1_uid) < 0) { tst_brkm(TBROK, cleanup, "Unable to set process uid to that of ltp user"); } /* Save the real group ID of the current process */ mygid = getgid(); /* Change directory to DIR_TEMP */ SAFE_CHDIR(cleanup, DIR_TEMP); }