• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 char *path;
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 	for (lc = 0; TEST_LOOPING(lc); lc++) {
74 
75 		tst_count = 0;
76 
77 		TEST(chroot(path));
78 
79 		if (TEST_RETURN != -1)
80 			tst_resm(TFAIL, "call succeeded unexpectedly");
81 		else if (errno != EPERM)
82 			tst_resm(TFAIL | TTERRNO, "chroot failed unexpectedly");
83 		else
84 			tst_resm(TPASS, "chroot set errno to EPERM.");
85 	}
86 	cleanup();
87 
88 	tst_exit();
89 
90 }
91 
setup(void)92 void setup(void)
93 {
94 	tst_require_root();
95 
96 	tst_tmpdir();
97 	path = tst_get_tmpdir();
98 
99 	if ((ltpuser = getpwnam(nobody_uid)) == NULL)
100 		tst_brkm(TBROK | TERRNO, cleanup,
101 			 "getpwnam(\"nobody\") failed");
102 
103 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
104 
105 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
106 
107 	TEST_PAUSE;
108 }
109 
cleanup(void)110 void cleanup(void)
111 {
112 	SAFE_SETEUID(NULL, 0);
113 
114 	free(path);
115 	tst_rmdir();
116 }
117