Set(const uid_t newuid, const gid_t newgid) { if(!initialized) { currentuid = GETEUID(); currentgid = GETEGID(); initialized = true; } if(newuid == currentuid && newgid == currentgid) return; if(currentuid != 0) { SETREUID(-1,0); SETREGID(-1,0); } if(newgid) { SETREGID(-1,newgid); initgroups(newuid,newgid); } if(newuid) SETREUID(-1,newuid); currentuid = newuid; currentgid = newgid; }
int main(int ac, char **av) { int lc; const char *msg; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); flag = 0; for (lc = 0; TEST_LOOPING(lc); lc++) { int i, pid, status; tst_count = 0; if ((pid = FORK_OR_VFORK()) == -1) { tst_brkm(TBROK, cleanup, "fork failed"); } else if (pid == 0) { /* child */ for (i = 0; i < TST_TOTAL; i++) { /* Set the real or effective user id */ TEST(SETREUID(cleanup, *test_data[i].real_uid, *test_data[i].eff_uid)); if (TEST_RETURN != -1) { tst_resm(TPASS, "setreuid(%d, %d) " "succeeded as expected.", *test_data[i].real_uid, *test_data[i].eff_uid); } else { tst_resm(TFAIL, "setreuid(%d, %d) " "did not return as expected.", *test_data[i].real_uid, *test_data[i].eff_uid); flag = -1; } if (STD_FUNCTIONAL_TEST) { uid_verify(test_data[i].exp_real_usr, test_data[i].exp_eff_usr, test_data[i].test_msg); } else { tst_resm(TINFO, "Call succeeded."); } } exit(flag); } else { /* parent */ waitpid(pid, &status, 0); if (WEXITSTATUS(status) != 0) { tst_resm(TFAIL, "test failed within " "child process."); } } } cleanup(); tst_exit(); }
int main(int argc, char **argv) { int lc; tst_parse_opts(argc, argv, NULL, NULL); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; TEST(SETREUID(cleanup, -1, INVAL_USER)); if (TEST_RETURN != -1) { tst_resm(TFAIL, "%s did not fail as expected", TCID); } else if (TEST_ERRNO == EPERM) { tst_resm(TPASS, "setreuid set errno to EPERM as " "expected"); } else { tst_resm(TFAIL, "setreuid FAILED, expected 1 but " "returned %d", TEST_ERRNO); } } cleanup(); tst_exit(); }
int main(int ac, char **av) { int lc; char *msg; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { int i; tst_count = 0; for (i = 0; i < TST_TOTAL; i++) { /* Set the real or effective user id */ TEST(SETREUID(cleanup, *test_data[i].real_uid, *test_data[i].eff_uid)); if (TEST_RETURN == -1) { TEST_ERROR_LOG(TEST_ERRNO); tst_resm(TBROK, "setreuid(%d, %d) failed", *test_data[i].real_uid, *test_data[i].eff_uid); } else { if (STD_FUNCTIONAL_TEST) { uid_verify(test_data[i].exp_real_usr, test_data[i].exp_eff_usr, test_data[i].test_msg); } else { tst_resm(TPASS, "Call succeeded."); } } } } cleanup(); tst_exit(); }
int main(int argc, char **argv) { int lc; const char *msg; if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); pass = 0; for (lc = 0; TEST_LOOPING(lc); lc++) { int i, pid; tst_count = 0; if ((pid = FORK_OR_VFORK()) == -1) { tst_brkm(TBROK, cleanup, "fork failed"); } else if (pid == 0) { /* child */ for (i = 0; i < TST_TOTAL; i++) { /* Set the real or effective user id */ TEST(SETREUID(cleanup, *test_data[i].real_uid, *test_data[i].eff_uid)); if (TEST_RETURN == *test_data[i].exp_ret) { if (TEST_RETURN == neg_one) { if (TEST_ERRNO != EPERM) { tst_resm(TFAIL, "setreuid(%d, %d) " "did not set errno " "value as expected.", *test_data [i].real_uid, *test_data [i].eff_uid); continue; } tst_resm(TPASS, "setreuid(%d, %d) " "failed as expected.", *test_data[i].real_uid, *test_data[i].eff_uid); } else { tst_resm(TPASS, "setreuid(%d, %d) " "succeeded as expected.", *test_data[i].real_uid, *test_data[i].eff_uid); } } else { tst_resm(TFAIL, "setreuid(%d, %d) " "did not return as expected.", *test_data[i].real_uid, *test_data[i].eff_uid); } if (TEST_RETURN == -1) { TEST_ERROR_LOG(TEST_ERRNO); } uid_verify(test_data[i].exp_real_usr, test_data[i].exp_eff_usr, test_data[i].test_msg); } tst_exit(); } else { /* parent */ tst_record_childstatus(cleanup, pid); } } cleanup(); tst_exit(); }
static void do_master_child(void) { int lc; int pid; int status; for (lc = 0; TEST_LOOPING(lc); lc++) { int tst_fd; tst_count = 0; if (SETREUID(NULL, 0, ltpuser->pw_uid) == -1) { perror("setreuid failed"); exit(TFAIL); } /* Test 1: Check the process with new uid cannot open the file * with RDWR permissions. */ TEST(tst_fd = open(testfile, O_RDWR)); if (TEST_RETURN != -1) { printf("open succeeded unexpectedly\n"); close(tst_fd); exit(TFAIL); } if (TEST_ERRNO == EACCES) { printf("open failed with EACCES as expected\n"); } else { perror("open failed unexpectedly"); exit(TFAIL); } /* Test 2: Check a son process cannot open the file * with RDWR permissions. */ pid = FORK_OR_VFORK(); if (pid < 0) tst_brkm(TBROK, cleanup, "Fork failed"); if (pid == 0) { int tst_fd2; /* Test to open the file in son process */ TEST(tst_fd2 = open(testfile, O_RDWR)); if (TEST_RETURN != -1) { printf("call succeeded unexpectedly\n"); close(tst_fd2); exit(TFAIL); } if (TEST_ERRNO == EACCES) { printf("open failed with EACCES as expected\n"); exit(TPASS); } else { printf("open failed unexpectedly\n"); exit(TFAIL); } } else { /* Wait for son completion */ if (waitpid(pid, &status, 0) == -1) { perror("waitpid failed"); exit(TFAIL); } if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) exit(WEXITSTATUS(status)); } /* Test 3: Fallback to initial uid and check we can again open * the file with RDWR permissions. */ tst_count++; if (SETREUID(NULL, 0, 0) == -1) { perror("setreuid failed"); exit(TFAIL); } TEST(tst_fd = open(testfile, O_RDWR)); if (TEST_RETURN == -1) { perror("open failed unexpectedly"); exit(TFAIL); } else { printf("open call succeeded\n"); close(tst_fd); } } exit(TPASS); }