chroot04.c revision 1e6f5a673655551de5734ff31ef48cd63b604e6d
1 /* 2 * Copyright (C) Bull S.A. 2001 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 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20/* 21 * NAME 22 * chroot04.c 23 * 24 * DESCRIPTION 25 * Testcase to check that chroot sets errno to EACCES. 26 * 27 * ALGORITHM 28 * As a non-root user attempt to perform chroot() to a directory. The 29 * chroot() call should fail with EACCES 30 * 31 * USAGE: <for command-line> 32 * chroot04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 33 * where, -c n : Run n copies concurrently. 34 * -e : Turn on errno logging. 35 * -i n : Execute test n times. 36 * -I x : Execute test for x seconds. 37 * -P x : Pause for x seconds between iterations. 38 * -t : Turn on syscall timing. 39 * 40 * HISTORY 41 * 04/2002 Ported by Jacky Malcles 42 * 43 * RESTRICTIONS 44 * Must be run as non-root user. 45 */ 46 47#include <stdio.h> 48#include <errno.h> 49#include <sys/stat.h> 50#include "test.h" 51#include "usctest.h" 52#include <pwd.h> 53 54char *TCID = "chroot04"; 55int TST_TOTAL = 1; 56 57#define TEST_TMPDIR "chroot04_tmpdir" 58 59int exp_enos[] = { EACCES, 0 }; 60char nobody_uid[] = "nobody"; 61struct passwd *ltpuser; 62 63void setup(void); 64void cleanup(void); 65 66int main(int ac, char **av) 67{ 68 int lc; 69 char *msg; 70 71 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 72 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 73 74 setup(); 75 76 /* set up expected errnos */ 77 TEST_EXP_ENOS(exp_enos); 78 79 /* Check for looping state if -i option is given */ 80 for (lc = 0; TEST_LOOPING(lc); lc++) { 81 82 /* reset Tst_count in case we are looping */ 83 Tst_count = 0; 84 85 TEST(chroot(TEST_TMPDIR)); 86 87 if (TEST_RETURN != -1) 88 tst_resm(TFAIL, "call succeeded unexpectedly"); 89 else if (TEST_ERRNO == EACCES) 90 tst_resm(TPASS, "got EACCESS as expected"); 91 else 92 tst_resm(TFAIL|TTERRNO, "did not get EACCES as expected"); 93 94 } 95 cleanup(); 96 97 tst_exit(); 98 99} 100 101/* 102 * setup() - performs all ONE TIME setup for this test. 103 */ 104void setup() 105{ 106 tst_sig(NOFORK, DEF_HANDLER, cleanup); 107 108 TEST_PAUSE; 109 110 /* make a temporary directory and cd to it */ 111 tst_tmpdir(); 112 113 /* 114 * create a temporary directory 115 */ 116 if (mkdir(TEST_TMPDIR, 0222) != 0) { 117 tst_resm(TBROK, "mkdir(%s) failed", TEST_TMPDIR); 118 } 119 120 ltpuser = getpwnam(nobody_uid); 121 if (seteuid(ltpuser->pw_uid) == -1) { 122 tst_brkm(TBROK, cleanup, "seteuid to nobody failed"); 123 } 124 125} 126 127/* 128 * cleanup() - performs all ONE TIME cleanup for this test at 129 * completion or premature exit. 130 */ 131void cleanup() 132{ 133 /* reset the process ID to the saved ID (root) */ 134 if (setuid(0) == -1) { 135 tst_brkm(TBROK|TERRNO, NULL, "setuid(0) failed"); 136 } 137 if (rmdir(TEST_TMPDIR) != 0) { 138 tst_brkm(TFAIL|TERRNO, NULL, "rmdir(%s) failed", TEST_TMPDIR); 139 } 140 /* 141 * print timing stats if that option was specified. 142 * print errno log if that option was specified. 143 */ 144 TEST_CLEANUP; 145 146 /* delete the test directory created in setup() */ 147 tst_rmdir(); 148 149}