blob: 0d6cd4c5d7c3ad1a8f5ae5bc54239c1de32bb15c [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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_modak4bb656a2009-02-26 12:02:09 +000032 *
plars865695b2001-08-27 22:15:12 +000033 * 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
robbiewf7dc1be2003-03-14 16:20:37 +000068#include <sys/types.h>
Garrett Cooper58dc8e62010-12-20 12:25:34 -080069#include <sys/mman.h>
robbiewf7dc1be2003-03-14 16:20:37 +000070#include <sys/stat.h>
Garrett Cooper58dc8e62010-12-20 12:25:34 -080071#include <errno.h>
robbiewf7dc1be2003-03-14 16:20:37 +000072#include <fcntl.h>
Garrett Cooper58dc8e62010-12-20 12:25:34 -080073#include <pwd.h>
74#include <stdio.h>
75
76#include "test.h"
Garrett Cooper58dc8e62010-12-20 12:25:34 -080077#include "safe_macros.h"
robbiewf7dc1be2003-03-14 16:20:37 +000078
plars865695b2001-08-27 22:15:12 +000079char *TCID = "execve03";
mridge3578f4a2004-08-25 15:20:35 +000080int fileHandle = 0;
plars865695b2001-08-27 22:15:12 +000081
robbiewc5f2e372001-09-10 19:52:03 +000082char nobody_uid[] = "nobody";
83struct passwd *ltpuser;
84
Garrett Coopere653fac2010-12-20 12:31:19 -080085#ifndef UCLINUX
86void *bad_addr = NULL;
87#endif
plars1ad84512002-07-23 13:11:18 +000088
plars865695b2001-08-27 22:15:12 +000089void setup(void);
90void cleanup(void);
91
subrata_modak56207ce2009-03-23 13:35:39 +000092char long_file[] =
93 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
plars865695b2001-08-27 22:15:12 +000094char no_dir[] = "testdir";
robbiew066dbd42004-10-28 18:53:39 +000095char test_name3[1000];
96char test_name5[1000];
97char test_name6[1000];
plars865695b2001-08-27 22:15:12 +000098
99struct 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 Gao354ebb42012-12-07 10:10:04 +0800104 {
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},
vapier62b16cf2007-02-09 20:48:23 +0000112#if !defined(UCLINUX)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 /* the filename isn't part of the process address space - EFAULT */
114 {
115 (char *)-1, EFAULT},
vapier62b16cf2007-02-09 20:48:23 +0000116#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 /* 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}
plars865695b2001-08-27 22:15:12 +0000123};
124
Cyril Hrubisb863a0b2014-09-24 13:15:29 +0200125int TST_TOTAL = ARRAY_SIZE(TC);
vapier62b16cf2007-02-09 20:48:23 +0000126
subrata_modak56207ce2009-03-23 13:35:39 +0000127int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000128{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200129 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200130 const char *msg;
plars865695b2001-08-27 22:15:12 +0000131 int i;
132
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800133 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800134 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000135
136 setup();
137
plars865695b2001-08-27 22:15:12 +0000138 for (lc = 0; TEST_LOOPING(lc); lc++) {
139
Caspar Zhangd59a6592013-03-07 14:59:12 +0800140 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000141
plars865695b2001-08-27 22:15:12 +0000142 for (i = 0; i < TST_TOTAL; i++) {
143
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800144 TEST(execve(TC[i].tname, av, NULL));
plars865695b2001-08-27 22:15:12 +0000145
146 if (TEST_RETURN != -1) {
147 tst_resm(TFAIL, "call succeeded unexpectedly");
148 continue;
149 }
150
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800151 if (TEST_ERRNO == TC[i].error)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 tst_resm(TPASS | TTERRNO,
153 "execve failed as expected");
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800154 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 tst_resm(TFAIL | TTERRNO,
156 "execve failed unexpectedly; expected "
157 "%d - %s",
158 TC[i].error, strerror(TC[i].error));
plars865695b2001-08-27 22:15:12 +0000159 }
160 }
161 cleanup();
162
Garrett Cooper53740502010-12-16 00:04:01 -0800163 tst_exit();
plars865695b2001-08-27 22:15:12 +0000164}
165
Mike Frysingerc57fba52014-04-09 18:56:30 -0400166void setup(void)
plars865695b2001-08-27 22:15:12 +0000167{
168 char *cwdname = NULL;
169 int fd;
170
171 umask(0);
172
plars865695b2001-08-27 22:15:12 +0000173 tst_sig(NOFORK, DEF_HANDLER, cleanup);
174
plars865695b2001-08-27 22:15:12 +0000175 TEST_PAUSE;
176
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800177 ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);
robbiewc5f2e372001-09-10 19:52:03 +0000178
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800179 SAFE_SETGID(NULL, ltpuser->pw_gid);
180
plars865695b2001-08-27 22:15:12 +0000181 tst_tmpdir();
182
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800183 cwdname = SAFE_GETCWD(cleanup, cwdname, 0);
plars865695b2001-08-27 22:15:12 +0000184
185 sprintf(test_name5, "%s/fake.%d", cwdname, getpid());
186
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800187 fileHandle = SAFE_CREAT(cleanup, test_name5, 0444);
plars865695b2001-08-27 22:15:12 +0000188
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 Cooper58dc8e62010-12-20 12:25:34 -0800194 fd = SAFE_CREAT(cleanup, test_name6, 0755);
195 fd = SAFE_CLOSE(cleanup, fd);
Garrett Coopere653fac2010-12-20 12:31:19 -0800196#ifndef UCLINUX
197 bad_addr = SAFE_MMAP(cleanup, NULL, 1, PROT_NONE,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
plars1ad84512002-07-23 13:11:18 +0000199 TC[3].tname = bad_addr;
vapier62b16cf2007-02-09 20:48:23 +0000200#endif
plars865695b2001-08-27 22:15:12 +0000201}
202
Mike Frysingerc57fba52014-04-09 18:56:30 -0400203void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000204{
Garrett Coopere653fac2010-12-20 12:31:19 -0800205#ifndef UCLINUX
206 SAFE_MUNMAP(NULL, bad_addr, 1);
207#endif
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800208 SAFE_CLOSE(NULL, fileHandle);
mridge3578f4a2004-08-25 15:20:35 +0000209
plars865695b2001-08-27 22:15:12 +0000210 tst_rmdir();
211
Garrett Cooper58dc8e62010-12-20 12:25:34 -0800212}