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 | * 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_modak | 4374a71 | 2008-04-22 06:57:32 +0000 | [diff] [blame] | 45 | * 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 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | * RESTRICTIONS |
| 51 | * Must run test with the -F <test file> option. |
| 52 | */ |
| 53 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 54 | #ifndef _GNU_SOURCE |
| 55 | #define _GNU_SOURCE |
| 56 | #endif |
robbiew | f7dc1be | 2003-03-14 16:20:37 +0000 | [diff] [blame] | 57 | #include <sys/types.h> |
| 58 | #include <sys/stat.h> |
| 59 | #include <sys/wait.h> |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 60 | #include <errno.h> |
| 61 | #include <libgen.h> |
| 62 | #include <pwd.h> |
| 63 | #include <stdio.h> |
| 64 | #include <string.h> |
| 65 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | char *TCID = "execve02"; |
| 69 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 71 | void setup(char *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | void cleanup(void); |
| 73 | void help(void); |
| 74 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | int Fflag = 0; |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 76 | char *test_app; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 78 | /* for test specific parse_opts options - in this case "-F" */ |
| 79 | option_t options[] = { |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 80 | {"F:", &Fflag, &test_app}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | {NULL, NULL, NULL} |
| 82 | }; |
| 83 | |
| 84 | char user1name[] = "nobody"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | extern struct passwd *my_getpwnam(char *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | struct passwd *ltpuser1; |
| 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 90 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 91 | const char *msg; |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 92 | int status, retval = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | pid_t pid; |
| 94 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 95 | if ((msg = parse_opts(ac, av, options, &help)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 96 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 98 | if (!Fflag) |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "You must specify a test executable with " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | "the -F option."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 102 | setup(av[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 105 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 108 | if (chmod(test_app, 0700) != 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | tst_resm(TFAIL | TERRNO, "chmod failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 111 | if ((pid = FORK_OR_VFORK()) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 112 | tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 114 | if (pid == 0) { |
| 115 | if (seteuid(ltpuser1->pw_uid) == -1) |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 116 | perror("setuid failed"); |
| 117 | char *argv[2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 119 | argv[0] = basename(test_app); |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 120 | argv[1] = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 122 | if (argv[0] == NULL) |
| 123 | perror("basename failed"); |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 124 | TEST(execve(test_app, argv, NULL)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
| 126 | if (TEST_ERRNO != EACCES) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | retval = 1; |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 128 | perror("execve failed unexpectedly"); |
| 129 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | printf |
| 131 | ("execve failed with EACCES as expected\n"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 133 | if (seteuid(0) == -1) |
| 134 | perror("setuid(0) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 136 | if (chmod(test_app, 0755) == -1) |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 137 | perror("chmod #2 failed"); |
plars | da85874 | 2002-09-25 17:34:14 +0000 | [diff] [blame] | 138 | exit(retval); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | } |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 140 | if (wait(&status) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | tst_brkm(TBROK | TERRNO, cleanup, "wait failed"); |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 142 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 143 | tst_resm(TFAIL, "child exited abnormally"); |
| 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 146 | cleanup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 148 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * help() - Prints out the help message for the -F option defined |
| 153 | * by this test. |
| 154 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 155 | void help(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | { |
| 157 | printf(" -F <test file> : for example, 'execve02 -F test3'\n"); |
| 158 | } |
| 159 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 160 | void setup(char *argv0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 162 | char *cmd, *pwd = NULL; |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 163 | char test_path[MAXPATHLEN]; |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 164 | |
| 165 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 167 | if (test_app[0] == '/') |
| 168 | strncpy(test_path, test_app, sizeof(test_path)); |
| 169 | else { |
| 170 | if ((pwd = get_current_dir_name()) == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 171 | tst_brkm(TBROK | TERRNO, NULL, "getcwd failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 173 | snprintf(test_path, sizeof(test_path), "%s/%s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | pwd, basename(test_app)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 176 | free(pwd); |
| 177 | } |
| 178 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 179 | cmd = malloc(strlen(test_path) + strlen("cp -p \"") + strlen("\" .") + 1); |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 180 | if (cmd == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | tst_brkm(TBROK | TERRNO, NULL, "Cannot alloc command string"); |
subrata_modak | 4374a71 | 2008-04-22 06:57:32 +0000 | [diff] [blame] | 182 | |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 183 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 184 | |
| 185 | tst_tmpdir(); |
| 186 | |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 187 | sprintf(cmd, "cp -p \"%s\" .", test_path); |
Garrett Cooper | 775c33d | 2010-12-20 01:57:37 -0800 | [diff] [blame] | 188 | if (system(cmd) != 0) |
Garrett Cooper | c585a27 | 2010-12-20 03:04:07 -0800 | [diff] [blame] | 189 | tst_brkm(TBROK, NULL, "command failed: %s", cmd); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | free(cmd); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 191 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | umask(0); |
| 193 | |
| 194 | ltpuser1 = my_getpwnam(user1name); |
| 195 | } |
| 196 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 197 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | { |
subrata_modak | 4374a71 | 2008-04-22 06:57:32 +0000 | [diff] [blame] | 199 | tst_rmdir(); |
Garrett Cooper | 5cf94a2 | 2010-12-19 10:02:02 -0800 | [diff] [blame] | 200 | } |