plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "usctest.h" |
robbiew | 096b081 | 2001-08-31 15:32:25 +0000 | [diff] [blame] | 51 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
| 53 | char *TCID = "chroot01"; |
| 54 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | int fail; |
| 56 | |
| 57 | char path[] = "/tmp"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 58 | int exp_enos[] = { EPERM, 0 }; |
robbiew | 096b081 | 2001-08-31 15:32:25 +0000 | [diff] [blame] | 59 | char nobody_uid[] = "nobody"; |
| 60 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | void setup(void); |
| 63 | void cleanup(void); |
| 64 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 65 | int |
| 66 | main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | { |
| 68 | int lc; |
| 69 | char *msg; |
| 70 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 71 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | setup(); |
| 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | TEST_EXP_ENOS(exp_enos); |
| 77 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 79 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | Tst_count = 0; |
| 81 | |
| 82 | TEST(chroot(path)); |
| 83 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 84 | if (TEST_RETURN != -1) |
| 85 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 86 | else if (errno != EPERM) |
| 87 | tst_resm(TFAIL|TTERRNO, "chroot failed unexpectedly"); |
| 88 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | tst_resm(TPASS, "chroot set errno to EPERM."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | } |
| 91 | cleanup(); |
| 92 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 93 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
| 98 | setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | { |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 100 | tst_require_root(NULL); |
robbiew | 096b081 | 2001-08-31 15:32:25 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 102 | tst_tmpdir(); |
| 103 | |
| 104 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) |
| 105 | tst_brkm(TBROK|TERRNO, cleanup, "getpwnam(\"nobody\") failed"); |
| 106 | |
| 107 | if (seteuid(ltpuser->pw_uid) == -1) |
| 108 | tst_brkm(TBROK|TERRNO, cleanup, "seteuid to nobody failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 115 | void |
| 116 | cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | TEST_CLEANUP; |
| 119 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 120 | if (seteuid(0) == -1) |
| 121 | tst_brkm(TBROK|TERRNO, NULL, "setuid(0) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
Garrett Cooper | dc2793f | 2010-12-16 18:43:08 -0800 | [diff] [blame] | 123 | tst_rmdir(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame^] | 124 | } |