plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * Testcase to test whether chroot(2) sets errno correctly. |
| 21 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * 1. Test for ENAMETOOLONG: |
| 23 | * Create a bad directory name with length more than |
| 24 | * VFS_MAXNAMELEN (Linux kernel variable), and pass it as the |
| 25 | * path to chroot(2). |
| 26 | * |
| 27 | * 2. Test for ENOENT: |
| 28 | * Attempt to chroot(2) on a non-existent directory |
| 29 | * |
| 30 | * 3. Test for ENOTDIR: |
| 31 | * Attempt to chdir(2) on a file. |
| 32 | * |
| 33 | * 4. Test for EFAULT: |
| 34 | * The pathname parameter to chroot() points to an invalid address, |
| 35 | * chroot(2) fails with EPERM. |
| 36 | * |
Xiaoguang Wang | 2edca2f | 2013-10-31 10:15:05 +0800 | [diff] [blame] | 37 | * 5. Test for ELOOP: |
| 38 | * Too many symbolic links were encountered When resolving the |
| 39 | * pathname parameter. |
| 40 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | */ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include <errno.h> |
| 46 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 47 | #include <sys/mman.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 48 | #include "test.h" |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 49 | #include <fcntl.h> |
Xiaoguang Wang | 2edca2f | 2013-10-31 10:15:05 +0800 | [diff] [blame] | 50 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
| 52 | char *TCID = "chroot03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 54 | static int fd; |
| 55 | static char fname[255]; |
| 56 | static char good_dir[100] = "/tmp/testdir"; |
| 57 | static char bad_dir[] = "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; |
| 58 | static char symbolic_dir[] = "sym_dir1"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | char *dir; |
| 62 | int error; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | } TC[] = { |
| 64 | /* |
| 65 | * to test whether chroot() is setting ENAMETOOLONG if the |
| 66 | * pathname is more than VFS_MAXNAMELEN |
| 67 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | { |
| 69 | bad_dir, ENAMETOOLONG}, |
| 70 | /* |
| 71 | * to test whether chroot() is setting ENOTDIR if the argument |
| 72 | * is not a directory. |
| 73 | */ |
| 74 | { |
| 75 | fname, ENOTDIR}, |
| 76 | /* |
| 77 | * to test whether chroot() is setting ENOENT if the directory |
| 78 | * does not exist. |
| 79 | */ |
| 80 | { |
| 81 | good_dir, ENOENT}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 82 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | /* |
| 84 | * attempt to chroot to a path pointing to an invalid address |
| 85 | * and expect EFAULT as errno |
| 86 | */ |
| 87 | { |
Xiaoguang Wang | 2edca2f | 2013-10-31 10:15:05 +0800 | [diff] [blame] | 88 | (char *)-1, EFAULT}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 89 | #endif |
Xiaoguang Wang | 2edca2f | 2013-10-31 10:15:05 +0800 | [diff] [blame] | 90 | {symbolic_dir, ELOOP} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 93 | int TST_TOTAL = ARRAY_SIZE(TC); |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 94 | |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 95 | static char *bad_addr; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 96 | |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 97 | static void setup(void); |
| 98 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 102 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 104 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 106 | msg = parse_opts(ac, av, NULL, NULL); |
| 107 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
| 110 | setup(); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 113 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | TEST(chroot(TC[i].dir)); |
| 117 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | if (TEST_RETURN != -1) { |
| 119 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 120 | continue; |
| 121 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | if (TEST_ERRNO == TC[i].error) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | tst_resm(TPASS | TTERRNO, "failed as expected"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | tst_resm(TFAIL | TTERRNO, |
| 127 | "didn't fail as expected (expected errno " |
| 128 | "= %d : %s)", |
| 129 | TC[i].error, strerror(TC[i].error)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 134 | cleanup(); |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 135 | tst_exit(); |
| 136 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 138 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | tst_tmpdir(); |
| 143 | |
| 144 | /* |
| 145 | * create a file and use it to test whether chroot() is setting |
| 146 | * ENOTDIR if the argument is not a directory. |
| 147 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | (void)sprintf(fname, "tfile_%d", getpid()); |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 149 | fd = creat(fname, 0777); |
| 150 | if (fd == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tst_brkm(TBROK, cleanup, "Failed to creat a temp file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * set up good_dir to test whether chroot() is setting ENOENT if the |
| 155 | * directory does not exist. |
| 156 | */ |
| 157 | (void)sprintf(good_dir, "%s.%d", good_dir, getpid()); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 158 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 159 | #if !defined(UCLINUX) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 160 | bad_addr = mmap(0, 1, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 162 | if (bad_addr == MAP_FAILED) |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 163 | tst_brkm(TBROK, cleanup, "mmap failed"); |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 164 | |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 165 | TC[3].dir = bad_addr; |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 166 | #endif |
Xiaoguang Wang | 2edca2f | 2013-10-31 10:15:05 +0800 | [diff] [blame] | 167 | /* |
| 168 | * create two symbolic directory who point to each other to |
| 169 | * test ELOOP. |
| 170 | */ |
| 171 | SAFE_SYMLINK(cleanup, "sym_dir1/", "sym_dir2"); |
| 172 | SAFE_SYMLINK(cleanup, "sym_dir2/", "sym_dir1"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Wanlong Gao | 278743a | 2013-10-31 16:37:47 +0800 | [diff] [blame] | 175 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | close(fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 179 | } |