• 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 <pwd.h>
51 
52 char *TCID = "chroot01";
53 int TST_TOTAL = 1;
54 int fail;
55 
56 #ifndef ANDROID
57 char path[] = "/tmp";
58 #else
59 char path[] = "/data/local/tmp";
60 #endif
61 
62 char nobody_uid[] = "nobody";
63 struct passwd *ltpuser;
64 
65 void setup(void);
66 void cleanup(void);
67 
main(int ac,char ** av)68 int main(int ac, char **av)
69 {
70 	int lc;
71 
72 	tst_parse_opts(ac, av, NULL, NULL);
73 
74 	setup();
75 
76 	for (lc = 0; TEST_LOOPING(lc); lc++) {
77 
78 		tst_count = 0;
79 
80 		TEST(chroot(path));
81 
82 		if (TEST_RETURN != -1)
83 			tst_resm(TFAIL, "call succeeded unexpectedly");
84 		else if (errno != EPERM)
85 			tst_resm(TFAIL | TTERRNO, "chroot failed unexpectedly");
86 		else
87 			tst_resm(TPASS, "chroot set errno to EPERM.");
88 	}
89 	cleanup();
90 
91 	tst_exit();
92 
93 }
94 
setup(void)95 void setup(void)
96 {
97 	tst_require_root();
98 
99 	tst_tmpdir();
100 
101 	if ((ltpuser = getpwnam(nobody_uid)) == NULL)
102 		tst_brkm(TBROK | TERRNO, cleanup,
103 			 "getpwnam(\"nobody\") failed");
104 
105 	if (seteuid(ltpuser->pw_uid) == -1)
106 		tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
107 
108 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
109 
110 	TEST_PAUSE;
111 }
112 
cleanup(void)113 void cleanup(void)
114 {
115 	if (seteuid(0) == -1)
116 		tst_brkm(TBROK | TERRNO, NULL, "setuid(0) failed");
117 
118 	tst_rmdir();
119 }
120