blob: 46b060239a53294efe1774361b7d5bc10b30a3d7 [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 * execve02.c
23 *
24 * DESCRIPTION
25 * Testcase to check whether execve(2) sets errno to EACCES correctly
26 *
27 * ALGORITHM
28 * 1. Attempt to execve(2) an executable owned by root with
29 * no execute permissions for the other users, fails when
30 * execve(2) is used as a non-root user (ltpuser1). The errno
31 * should be EACCES.
32 *
33 * USAGE: <for command-line>
34 * execve02 -F <test file> [-c n] [-e] [-i n] [-I x] [-P x] [-t]
35 * where, -c n : Run n copies concurrently.
36 * -e : Turn on errno logging.
37 * -i n : Execute test n times.
38 * -I x : Execute test for x seconds.
39 * -P x : Pause for x seconds between iterations.
40 * -t : Turn on syscall timing.
41 *
42 * HISTORY
43 * 07/2001 Ported by Wayne Boyer
44 *
subrata_modak4374a712008-04-22 06:57:32 +000045 * 21/04/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
46 * - Fix concurrency issue. In case of concurrent executions, all tasks
47 * was using the same file, changing its mode and leading to invalid
48 * mode for some of them.
49 *
plars865695b2001-08-27 22:15:12 +000050 * RESTRICTIONS
51 * Must run test with the -F <test file> option.
52 */
53
Garrett Cooperc585a272010-12-20 03:04:07 -080054#ifndef _GNU_SOURCE
55#define _GNU_SOURCE
56#endif
robbiewf7dc1be2003-03-14 16:20:37 +000057#include <sys/types.h>
58#include <sys/stat.h>
59#include <sys/wait.h>
Garrett Cooper15697992010-12-18 06:31:28 -080060#include <errno.h>
61#include <libgen.h>
62#include <pwd.h>
63#include <stdio.h>
64#include <string.h>
65#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000066#include "test.h"
plars865695b2001-08-27 22:15:12 +000067
68char *TCID = "execve02";
69int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000070
Garrett Cooper15697992010-12-18 06:31:28 -080071void setup(char *);
plars865695b2001-08-27 22:15:12 +000072void cleanup(void);
73void help(void);
74
plars865695b2001-08-27 22:15:12 +000075int Fflag = 0;
Garrett Cooperc585a272010-12-20 03:04:07 -080076char *test_app;
plars865695b2001-08-27 22:15:12 +000077
78/* for test specific parse_opts options - in this case "-F" */
79option_t options[] = {
Garrett Cooperc585a272010-12-20 03:04:07 -080080 {"F:", &Fflag, &test_app},
plars865695b2001-08-27 22:15:12 +000081 {NULL, NULL, NULL}
82};
83
84char user1name[] = "nobody";
subrata_modak56207ce2009-03-23 13:35:39 +000085extern struct passwd *my_getpwnam(char *);
plars865695b2001-08-27 22:15:12 +000086struct passwd *ltpuser1;
87
subrata_modak56207ce2009-03-23 13:35:39 +000088int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000089{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020090 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020091 const char *msg;
Garrett Cooper775c33d2010-12-20 01:57:37 -080092 int status, retval = 0;
plars865695b2001-08-27 22:15:12 +000093 pid_t pid;
94
Garrett Cooper15697992010-12-18 06:31:28 -080095 if ((msg = parse_opts(ac, av, options, &help)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080096 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000097
Garrett Cooper15697992010-12-18 06:31:28 -080098 if (!Fflag)
Garrett Cooperc585a272010-12-20 03:04:07 -080099 tst_brkm(TBROK, NULL, "You must specify a test executable with "
plars865695b2001-08-27 22:15:12 +0000100 "the -F option.");
plars865695b2001-08-27 22:15:12 +0000101
Garrett Cooper15697992010-12-18 06:31:28 -0800102 setup(av[0]);
plars865695b2001-08-27 22:15:12 +0000103
plars865695b2001-08-27 22:15:12 +0000104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000107
Garrett Cooperc585a272010-12-20 03:04:07 -0800108 if (chmod(test_app, 0700) != 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 tst_resm(TFAIL | TERRNO, "chmod failed");
plars865695b2001-08-27 22:15:12 +0000110
Garrett Cooper15697992010-12-18 06:31:28 -0800111 if ((pid = FORK_OR_VFORK()) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
plars865695b2001-08-27 22:15:12 +0000113
Garrett Cooper15697992010-12-18 06:31:28 -0800114 if (pid == 0) {
115 if (seteuid(ltpuser1->pw_uid) == -1)
Garrett Cooper775c33d2010-12-20 01:57:37 -0800116 perror("setuid failed");
117 char *argv[2];
plars865695b2001-08-27 22:15:12 +0000118
Garrett Cooperc585a272010-12-20 03:04:07 -0800119 argv[0] = basename(test_app);
Garrett Cooper775c33d2010-12-20 01:57:37 -0800120 argv[1] = 0;
plars865695b2001-08-27 22:15:12 +0000121
Garrett Cooper775c33d2010-12-20 01:57:37 -0800122 if (argv[0] == NULL)
123 perror("basename failed");
Garrett Cooperc585a272010-12-20 03:04:07 -0800124 TEST(execve(test_app, argv, NULL));
plars865695b2001-08-27 22:15:12 +0000125
126 if (TEST_ERRNO != EACCES) {
subrata_modak56207ce2009-03-23 13:35:39 +0000127 retval = 1;
Garrett Cooper15697992010-12-18 06:31:28 -0800128 perror("execve failed unexpectedly");
129 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 printf
131 ("execve failed with EACCES as expected\n");
plars865695b2001-08-27 22:15:12 +0000132
Garrett Cooper15697992010-12-18 06:31:28 -0800133 if (seteuid(0) == -1)
134 perror("setuid(0) failed");
plars865695b2001-08-27 22:15:12 +0000135
Garrett Cooperc585a272010-12-20 03:04:07 -0800136 if (chmod(test_app, 0755) == -1)
Garrett Cooper775c33d2010-12-20 01:57:37 -0800137 perror("chmod #2 failed");
plarsda858742002-09-25 17:34:14 +0000138 exit(retval);
plars865695b2001-08-27 22:15:12 +0000139 }
Garrett Cooper775c33d2010-12-20 01:57:37 -0800140 if (wait(&status) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_brkm(TBROK | TERRNO, cleanup, "wait failed");
Garrett Cooper775c33d2010-12-20 01:57:37 -0800142 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
143 tst_resm(TFAIL, "child exited abnormally");
144
plars865695b2001-08-27 22:15:12 +0000145 }
Garrett Cooper15697992010-12-18 06:31:28 -0800146 cleanup();
plars865695b2001-08-27 22:15:12 +0000147
Garrett Cooper53740502010-12-16 00:04:01 -0800148 tst_exit();
plars865695b2001-08-27 22:15:12 +0000149}
150
151/*
152 * help() - Prints out the help message for the -F option defined
153 * by this test.
154 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400155void help(void)
plars865695b2001-08-27 22:15:12 +0000156{
157 printf(" -F <test file> : for example, 'execve02 -F test3'\n");
158}
159
Garrett Cooper15697992010-12-18 06:31:28 -0800160void setup(char *argv0)
plars865695b2001-08-27 22:15:12 +0000161{
Garrett Cooper775c33d2010-12-20 01:57:37 -0800162 char *cmd, *pwd = NULL;
Garrett Cooperc585a272010-12-20 03:04:07 -0800163 char test_path[MAXPATHLEN];
Garrett Cooper15697992010-12-18 06:31:28 -0800164
165 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000166
Garrett Cooperc585a272010-12-20 03:04:07 -0800167 if (test_app[0] == '/')
168 strncpy(test_path, test_app, sizeof(test_path));
169 else {
170 if ((pwd = get_current_dir_name()) == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 tst_brkm(TBROK | TERRNO, NULL, "getcwd failed");
plars865695b2001-08-27 22:15:12 +0000172
Garrett Cooperc585a272010-12-20 03:04:07 -0800173 snprintf(test_path, sizeof(test_path), "%s/%s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 pwd, basename(test_app));
plars865695b2001-08-27 22:15:12 +0000175
Garrett Cooperc585a272010-12-20 03:04:07 -0800176 free(pwd);
177 }
178
Cyril Hrubisd218f342014-09-23 13:14:56 +0200179 cmd = malloc(strlen(test_path) + strlen("cp -p \"") + strlen("\" .") + 1);
Garrett Cooper15697992010-12-18 06:31:28 -0800180 if (cmd == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 tst_brkm(TBROK | TERRNO, NULL, "Cannot alloc command string");
subrata_modak4374a712008-04-22 06:57:32 +0000182
Garrett Cooper775c33d2010-12-20 01:57:37 -0800183 tst_sig(FORK, DEF_HANDLER, cleanup);
184
185 tst_tmpdir();
186
Garrett Cooperc585a272010-12-20 03:04:07 -0800187 sprintf(cmd, "cp -p \"%s\" .", test_path);
Garrett Cooper775c33d2010-12-20 01:57:37 -0800188 if (system(cmd) != 0)
Garrett Cooperc585a272010-12-20 03:04:07 -0800189 tst_brkm(TBROK, NULL, "command failed: %s", cmd);
subrata_modak56207ce2009-03-23 13:35:39 +0000190 free(cmd);
subrata_modakbdbaec52009-02-26 12:14:51 +0000191
plars865695b2001-08-27 22:15:12 +0000192 umask(0);
193
194 ltpuser1 = my_getpwnam(user1name);
195}
196
Mike Frysingerc57fba52014-04-09 18:56:30 -0400197void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000198{
subrata_modak4374a712008-04-22 06:57:32 +0000199 tst_rmdir();
Garrett Cooper5cf94a22010-12-19 10:02:02 -0800200}