• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018 Linux Test Project
3  * Copyright (c) 2015 Cyril Hrubis <chrubis@suse.cz>
4  * Copyright (c) International Business Machines  Corp., 2001
5  *
6  *  07/2001 Ported by Wayne Boyer
7  *  21/04/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /*
24  * Attempt to execve(2) an executable owned by root with no execute permissions
25  * for the other users, fails when execve(2) is used as a non-root user, the
26  * errno should be EACCES.
27  */
28 
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE
31 #endif
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include <pwd.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "tst_test.h"
42 
43 #define TEST_APP "execve_child"
44 #define USER_NAME "nobody"
45 
46 static uid_t nobody_uid;
47 
do_child(void)48 static void do_child(void)
49 {
50 	char *argv[2] = {TEST_APP, NULL};
51 
52 	SAFE_SETEUID(nobody_uid);
53 
54 	/* Use environ:
55 	 *     Inherit a copy of parent's environment
56 	 *     for tst_reinit() in execve_child.c
57 	 */
58 	TEST(execve(TEST_APP, argv, environ));
59 
60 	if (!TST_RET)
61 		tst_brk(TFAIL, "execve() passed unexpectedly");
62 
63 	if (TST_ERR != EACCES)
64 		tst_brk(TFAIL | TERRNO, "execve() failed unexpectedly");
65 
66 	tst_res(TPASS | TERRNO, "execve() failed expectedly");
67 
68 	exit(0);
69 }
70 
verify_execve(void)71 static void verify_execve(void)
72 {
73 	pid_t pid = SAFE_FORK();
74 
75 	if (pid == 0)
76 		do_child();
77 }
78 
setup(void)79 static void setup(void)
80 {
81 	struct passwd *pwd;
82 
83 	SAFE_CHMOD(TEST_APP, 0700);
84 
85 	pwd = SAFE_GETPWNAM(USER_NAME);
86 	nobody_uid = pwd->pw_uid;
87 }
88 
89 static const char *const resource_files[] = {
90 	TEST_APP,
91 	NULL,
92 };
93 
94 static struct tst_test test = {
95 	.needs_root = 1,
96 	.forks_child = 1,
97 	.child_needs_reinit = 1,
98 	.setup = setup,
99 	.resource_files = resource_files,
100 	.test_all = verify_execve,
101 };
102