• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "safe_macros.h"
52 #include <pwd.h>
53 
54 char *TCID = "chroot04";
55 int TST_TOTAL = 1;
56 
57 #define TEST_TMPDIR	"chroot04_tmpdir"
58 
59 char nobody_uid[] = "nobody";
60 struct passwd *ltpuser;
61 
62 void setup(void);
63 void cleanup(void);
64 
main(int ac,char ** av)65 int main(int ac, char **av)
66 {
67 	int lc;
68 
69 	tst_parse_opts(ac, av, NULL, NULL);
70 
71 	setup();
72 
73 	/* Check for looping state if -i option is given */
74 	for (lc = 0; TEST_LOOPING(lc); lc++) {
75 
76 		/* reset tst_count in case we are looping */
77 		tst_count = 0;
78 
79 		TEST(chroot(TEST_TMPDIR));
80 
81 		if (TEST_RETURN != -1)
82 			tst_resm(TFAIL, "call succeeded unexpectedly");
83 		else if (TEST_ERRNO == EACCES)
84 			tst_resm(TPASS, "got EACCESS as expected");
85 		else
86 			tst_resm(TFAIL | TTERRNO,
87 				 "did not get EACCES as expected");
88 
89 	}
90 	cleanup();
91 
92 	tst_exit();
93 
94 }
95 
96 /*
97  * setup() - performs all ONE TIME setup for this test.
98  */
setup(void)99 void setup(void)
100 {
101 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
102 
103 	tst_require_root();
104 
105 	TEST_PAUSE;
106 
107 	/* make a temporary directory and cd to it */
108 	tst_tmpdir();
109 
110 	/*
111 	 * create a temporary directory
112 	 */
113 	if (mkdir(TEST_TMPDIR, 0222) != 0) {
114 		tst_resm(TBROK, "mkdir(%s) failed", TEST_TMPDIR);
115 	}
116 
117 	ltpuser = getpwnam(nobody_uid);
118 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
119 
120 }
121 
122 /*
123  * cleanup() - performs all ONE TIME cleanup for this test at
124  *		        completion or premature exit.
125  */
cleanup(void)126 void cleanup(void)
127 {
128 	/* reset the process ID to the saved ID (root) */
129 	SAFE_SETUID(NULL, 0);
130 	if (rmdir(TEST_TMPDIR) != 0) {
131 		tst_brkm(TFAIL | TERRNO, NULL, "rmdir(%s) failed", TEST_TMPDIR);
132 	}
133 
134 	/* delete the test directory created in setup() */
135 	tst_rmdir();
136 
137 }
138