Home | History | Annotate | Download | only in chroot
      1 /*
      2  *
      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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * NAME
     22  * 	chroot01.c
     23  *
     24  * DESCRIPTION
     25  *	Testcase to check the whether chroot sets errno to EPERM.
     26  *
     27  * ALGORITHM
     28  *	As a non-root user attempt to perform chroot() to a directory. The
     29  *	chroot() call should fail with EPERM
     30  *
     31  * USAGE:  <for command-line>
     32  *  chroot01 [-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  *	07/2001 Ported by Wayne Boyer
     42  *
     43  * RESTRICTIONS
     44  * 	Must be run as non-root user.
     45  */
     46 
     47 #include <stdio.h>
     48 #include <errno.h>
     49 #include "test.h"
     50 #include "safe_macros.h"
     51 #include <pwd.h>
     52 
     53 char *TCID = "chroot01";
     54 int TST_TOTAL = 1;
     55 int fail;
     56 
     57 #ifndef ANDROID
     58 char path[] = "/tmp";
     59 #else
     60 char path[] = "/data/local/tmp";
     61 #endif
     62 
     63 char nobody_uid[] = "nobody";
     64 struct passwd *ltpuser;
     65 
     66 void setup(void);
     67 void cleanup(void);
     68 
     69 int main(int ac, char **av)
     70 {
     71 	int lc;
     72 
     73 	tst_parse_opts(ac, av, NULL, NULL);
     74 
     75 	setup();
     76 
     77 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     78 
     79 		tst_count = 0;
     80 
     81 		TEST(chroot(path));
     82 
     83 		if (TEST_RETURN != -1)
     84 			tst_resm(TFAIL, "call succeeded unexpectedly");
     85 		else if (errno != EPERM)
     86 			tst_resm(TFAIL | TTERRNO, "chroot failed unexpectedly");
     87 		else
     88 			tst_resm(TPASS, "chroot set errno to EPERM.");
     89 	}
     90 	cleanup();
     91 
     92 	tst_exit();
     93 
     94 }
     95 
     96 void setup(void)
     97 {
     98 	tst_require_root();
     99 
    100 	tst_tmpdir();
    101 
    102 	if ((ltpuser = getpwnam(nobody_uid)) == NULL)
    103 		tst_brkm(TBROK | TERRNO, cleanup,
    104 			 "getpwnam(\"nobody\") failed");
    105 
    106 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
    107 
    108 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    109 
    110 	TEST_PAUSE;
    111 }
    112 
    113 void cleanup(void)
    114 {
    115 	SAFE_SETEUID(NULL, 0);
    116 
    117 	tst_rmdir();
    118 }
    119