1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2018 Linux Test Project
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Renaud Lottiaux <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker * Ported to LTP: Wayne Boyer
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker /*
10*49cdfc7eSAndroid Build Coastguard Worker * Testcase to check execve sets the following errnos correctly:
11*49cdfc7eSAndroid Build Coastguard Worker * 1. ENAMETOOLONG
12*49cdfc7eSAndroid Build Coastguard Worker * 2. ENOENT
13*49cdfc7eSAndroid Build Coastguard Worker * 3. ENOTDIR
14*49cdfc7eSAndroid Build Coastguard Worker * 4. EFAULT
15*49cdfc7eSAndroid Build Coastguard Worker * 5. EACCES
16*49cdfc7eSAndroid Build Coastguard Worker * 6. ENOEXEC
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * ALGORITHM
19*49cdfc7eSAndroid Build Coastguard Worker * 1. Attempt to execve(2) a file whose name is more than
20*49cdfc7eSAndroid Build Coastguard Worker * VFS_MAXNAMLEN fails with ENAMETOOLONG.
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * 2. Attempt to execve(2) a file which doesn't exist fails with
23*49cdfc7eSAndroid Build Coastguard Worker * ENOENT.
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * 3. Attempt to execve(2) a pathname (executabl) comprising of a
26*49cdfc7eSAndroid Build Coastguard Worker * directory, which doesn't exist fails with ENOTDIR.
27*49cdfc7eSAndroid Build Coastguard Worker *
28*49cdfc7eSAndroid Build Coastguard Worker * 4. Attempt to execve(2) a filename not within the address space
29*49cdfc7eSAndroid Build Coastguard Worker * of the process fails with EFAULT.
30*49cdfc7eSAndroid Build Coastguard Worker *
31*49cdfc7eSAndroid Build Coastguard Worker * 5. Attempt to execve(2) a filename that does not have executable
32*49cdfc7eSAndroid Build Coastguard Worker * permission - fails with EACCES.
33*49cdfc7eSAndroid Build Coastguard Worker *
34*49cdfc7eSAndroid Build Coastguard Worker * 6. Attempt to execve(2) a zero length file with executable
35*49cdfc7eSAndroid Build Coastguard Worker * permissions - fails with ENOEXEC.
36*49cdfc7eSAndroid Build Coastguard Worker */
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
39*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
40*49cdfc7eSAndroid Build Coastguard Worker #endif
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker static char nobody_uid[] = "nobody";
53*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
54*49cdfc7eSAndroid Build Coastguard Worker static char long_fname[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
55*49cdfc7eSAndroid Build Coastguard Worker static char no_dir[] = "testdir";
56*49cdfc7eSAndroid Build Coastguard Worker static char test_name3[1024];
57*49cdfc7eSAndroid Build Coastguard Worker static char test_name5[1024];
58*49cdfc7eSAndroid Build Coastguard Worker static char test_name6[1024];
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
61*49cdfc7eSAndroid Build Coastguard Worker char *tname;
62*49cdfc7eSAndroid Build Coastguard Worker int error;
63*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
64*49cdfc7eSAndroid Build Coastguard Worker /* the file name is greater than VFS_MAXNAMELEN - ENAMTOOLONG */
65*49cdfc7eSAndroid Build Coastguard Worker {long_fname, ENAMETOOLONG},
66*49cdfc7eSAndroid Build Coastguard Worker /* the filename does not exist - ENOENT */
67*49cdfc7eSAndroid Build Coastguard Worker {no_dir, ENOENT},
68*49cdfc7eSAndroid Build Coastguard Worker /* the path contains a directory name which doesn't exist - ENOTDIR */
69*49cdfc7eSAndroid Build Coastguard Worker {test_name3, ENOTDIR},
70*49cdfc7eSAndroid Build Coastguard Worker /* the filename isn't part of the process address space - EFAULT */
71*49cdfc7eSAndroid Build Coastguard Worker {NULL, EFAULT},
72*49cdfc7eSAndroid Build Coastguard Worker /* the filename does not have execute permission - EACCES */
73*49cdfc7eSAndroid Build Coastguard Worker {test_name5, EACCES},
74*49cdfc7eSAndroid Build Coastguard Worker /* the file is zero length with execute permissions - ENOEXEC */
75*49cdfc7eSAndroid Build Coastguard Worker {test_name6, ENOEXEC}
76*49cdfc7eSAndroid Build Coastguard Worker };
77*49cdfc7eSAndroid Build Coastguard Worker
setup(void)78*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker char *cwdname = NULL;
81*49cdfc7eSAndroid Build Coastguard Worker unsigned i;
82*49cdfc7eSAndroid Build Coastguard Worker int fd;
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker umask(0);
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker ltpuser = SAFE_GETPWNAM(nobody_uid);
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETGID(ltpuser->pw_gid);
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker cwdname = SAFE_GETCWD(cwdname, 0);
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker sprintf(test_name5, "%s/fake", cwdname);
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_CREAT(test_name5, 0444);
95*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker sprintf(test_name3, "%s/fake", test_name5);
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker /* creat() and close a zero length file with executeable permission */
100*49cdfc7eSAndroid Build Coastguard Worker sprintf(test_name6, "%s/execve03", cwdname);
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_CREAT(test_name6, 0755);
103*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(tcases); i++) {
106*49cdfc7eSAndroid Build Coastguard Worker if (!tcases[i].tname)
107*49cdfc7eSAndroid Build Coastguard Worker tcases[i].tname = tst_get_bad_addr(NULL);
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
verify_execve(unsigned int i)111*49cdfc7eSAndroid Build Coastguard Worker static void verify_execve(unsigned int i)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[i];
114*49cdfc7eSAndroid Build Coastguard Worker char *argv[2] = {tc->tname, NULL};
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker TEST(execve(tc->tname, argv, NULL));
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
119*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "call succeeded unexpectedly");
120*49cdfc7eSAndroid Build Coastguard Worker return;
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc->error) {
124*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "execve failed as expected");
125*49cdfc7eSAndroid Build Coastguard Worker return;
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "execve failed unexpectedly; expected %s",
129*49cdfc7eSAndroid Build Coastguard Worker strerror(tc->error));
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
133*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
134*49cdfc7eSAndroid Build Coastguard Worker .test = verify_execve,
135*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
136*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
137*49cdfc7eSAndroid Build Coastguard Worker .child_needs_reinit = 1,
138*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
139*49cdfc7eSAndroid Build Coastguard Worker };
140