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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * execve03.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check execve sets the following errnos correctly: |
| 26 | * 1. ENAMETOOLONG |
| 27 | * 2. ENOENT |
| 28 | * 3. ENOTDIR |
| 29 | * 4. EFAULT |
| 30 | * 5. EACCES |
| 31 | * 6. ENOEXEC |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 32 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * ALGORITHM |
| 34 | * 1. Attempt to execve(2) a file whose name is more than |
| 35 | * VFS_MAXNAMLEN fails with ENAMETOOLONG. |
| 36 | * |
| 37 | * 2. Attempt to execve(2) a file which doesn't exist fails with |
| 38 | * ENOENT. |
| 39 | * |
| 40 | * 3. Attempt to execve(2) a pathname (executabl) comprising of a |
| 41 | * directory, which doesn't exist fails with ENOTDIR. |
| 42 | * |
| 43 | * 4. Attempt to execve(2) a filename not within the address space |
| 44 | * of the process fails with EFAULT. |
| 45 | * |
| 46 | * 5. Attempt to execve(2) a filename that does not have executable |
| 47 | * permission - fails with EACCES. |
| 48 | * |
| 49 | * 6. Attempt to execve(2) a zero length file with executable |
| 50 | * permissions - fails with ENOEXEC. |
| 51 | * |
| 52 | * USAGE: <for command-line> |
| 53 | * execve03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -e : Turn on errno logging. |
| 56 | * -i n : Execute test n times. |
| 57 | * -I x : Execute test for x seconds. |
| 58 | * -P x : Pause for x seconds between iterations. |
| 59 | * -t : Turn on syscall timing. |
| 60 | * |
| 61 | * HISTORY |
| 62 | * 07/2001 Ported by Wayne Boyer |
| 63 | * |
| 64 | * RESTRICTIONS |
| 65 | * test #5 will fail with ETXTBSY not EACCES if the test is run as root |
| 66 | */ |
| 67 | |
robbiew | f7dc1be | 2003-03-14 16:20:37 +0000 | [diff] [blame] | 68 | #include <sys/types.h> |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 69 | #include <sys/mman.h> |
robbiew | f7dc1be | 2003-03-14 16:20:37 +0000 | [diff] [blame] | 70 | #include <sys/stat.h> |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 71 | #include <errno.h> |
robbiew | f7dc1be | 2003-03-14 16:20:37 +0000 | [diff] [blame] | 72 | #include <fcntl.h> |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 73 | #include <pwd.h> |
| 74 | #include <stdio.h> |
| 75 | |
| 76 | #include "test.h" |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 77 | #include "safe_macros.h" |
robbiew | f7dc1be | 2003-03-14 16:20:37 +0000 | [diff] [blame] | 78 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | char *TCID = "execve03"; |
mridge | 3578f4a | 2004-08-25 15:20:35 +0000 | [diff] [blame] | 80 | int fileHandle = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
robbiew | c5f2e37 | 2001-09-10 19:52:03 +0000 | [diff] [blame] | 82 | char nobody_uid[] = "nobody"; |
| 83 | struct passwd *ltpuser; |
| 84 | |
Garrett Cooper | e653fac | 2010-12-20 12:31:19 -0800 | [diff] [blame] | 85 | #ifndef UCLINUX |
| 86 | void *bad_addr = NULL; |
| 87 | #endif |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 88 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | void setup(void); |
| 90 | void cleanup(void); |
| 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | char long_file[] = |
| 93 | "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | char no_dir[] = "testdir"; |
robbiew | 066dbd4 | 2004-10-28 18:53:39 +0000 | [diff] [blame] | 95 | char test_name3[1000]; |
| 96 | char test_name5[1000]; |
| 97 | char test_name6[1000]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
| 99 | struct test_case_t { |
| 100 | char *tname; /* the command name to pass to execve() */ |
| 101 | int error; |
| 102 | } TC[] = { |
| 103 | /* the file name is greater than VFS_MAXNAMELEN - ENAMTOOLONG */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | { |
| 105 | long_file, ENAMETOOLONG}, |
| 106 | /* the filename does not exist - ENOENT */ |
| 107 | { |
| 108 | no_dir, ENOENT}, |
| 109 | /* the path contains a directory name which doesn't exist - ENOTDIR */ |
| 110 | { |
| 111 | test_name3, ENOTDIR}, |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 112 | #if !defined(UCLINUX) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | /* the filename isn't part of the process address space - EFAULT */ |
| 114 | { |
| 115 | (char *)-1, EFAULT}, |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 116 | #endif |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 117 | /* the filename does not have execute permission - EACCES */ |
| 118 | { |
| 119 | test_name5, EACCES}, |
| 120 | /* the file is zero length with execute permissions - ENOEXEC */ |
| 121 | { |
| 122 | test_name6, ENOEXEC} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 125 | int TST_TOTAL = ARRAY_SIZE(TC); |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 126 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 129 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 130 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | int i; |
| 132 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 133 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 134 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
| 136 | setup(); |
| 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 139 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 140 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | for (i = 0; i < TST_TOTAL; i++) { |
| 143 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 144 | TEST(execve(TC[i].tname, av, NULL)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | if (TEST_RETURN != -1) { |
| 147 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 148 | continue; |
| 149 | } |
| 150 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 151 | if (TEST_ERRNO == TC[i].error) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | tst_resm(TPASS | TTERRNO, |
| 153 | "execve failed as expected"); |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 154 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | tst_resm(TFAIL | TTERRNO, |
| 156 | "execve failed unexpectedly; expected " |
| 157 | "%d - %s", |
| 158 | TC[i].error, strerror(TC[i].error)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | cleanup(); |
| 162 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 163 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 166 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | { |
| 168 | char *cwdname = NULL; |
| 169 | int fd; |
| 170 | |
| 171 | umask(0); |
| 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | TEST_PAUSE; |
| 176 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 177 | ltpuser = SAFE_GETPWNAM(NULL, nobody_uid); |
robbiew | c5f2e37 | 2001-09-10 19:52:03 +0000 | [diff] [blame] | 178 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 179 | SAFE_SETGID(NULL, ltpuser->pw_gid); |
| 180 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | tst_tmpdir(); |
| 182 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 183 | cwdname = SAFE_GETCWD(cleanup, cwdname, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
| 185 | sprintf(test_name5, "%s/fake.%d", cwdname, getpid()); |
| 186 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 187 | fileHandle = SAFE_CREAT(cleanup, test_name5, 0444); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | |
| 189 | sprintf(test_name3, "%s/fake.%d", test_name5, getpid()); |
| 190 | |
| 191 | /* creat() and close a zero length file with executeable permission */ |
| 192 | sprintf(test_name6, "%s/execve03.%d", cwdname, getpid()); |
| 193 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 194 | fd = SAFE_CREAT(cleanup, test_name6, 0755); |
| 195 | fd = SAFE_CLOSE(cleanup, fd); |
Garrett Cooper | e653fac | 2010-12-20 12:31:19 -0800 | [diff] [blame] | 196 | #ifndef UCLINUX |
| 197 | bad_addr = SAFE_MMAP(cleanup, NULL, 1, PROT_NONE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 198 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 199 | TC[3].tname = bad_addr; |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 200 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 203 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | { |
Garrett Cooper | e653fac | 2010-12-20 12:31:19 -0800 | [diff] [blame] | 205 | #ifndef UCLINUX |
| 206 | SAFE_MUNMAP(NULL, bad_addr, 1); |
| 207 | #endif |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 208 | SAFE_CLOSE(NULL, fileHandle); |
mridge | 3578f4a | 2004-08-25 15:20:35 +0000 | [diff] [blame] | 209 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | tst_rmdir(); |
| 211 | |
Garrett Cooper | 58dc8e6 | 2010-12-20 12:25:34 -0800 | [diff] [blame] | 212 | } |