1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 Linux Test Project
4 * Copyright (c) 2015 Cyril Hrubis <chrubis@suse.cz>
5 * Copyright (c) International Business Machines Corp., 2001
6 *
7 * Ported to LTP: Wayne Boyer
8 * 21/04/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
9 */
10
11 /*
12 * Attempt to execve(2) an executable owned by root with no execute permissions
13 * for the other users, fails when execve(2) is used as a non-root user, the
14 * errno should be EACCES.
15 */
16
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <errno.h>
23 #include <pwd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "tst_test.h"
30
31 #define TEST_APP "execve_child"
32 #define USER_NAME "nobody"
33
34 static uid_t nobody_uid;
35
do_child(void)36 static void do_child(void)
37 {
38 char *argv[2] = {TEST_APP, NULL};
39
40 SAFE_SETEUID(nobody_uid);
41
42 /* Use environ:
43 * Inherit a copy of parent's environment
44 * for tst_reinit() in execve_child.c
45 */
46 TEST(execve(TEST_APP, argv, environ));
47
48 if (!TST_RET)
49 tst_brk(TFAIL, "execve() passed unexpectedly");
50
51 if (TST_ERR != EACCES)
52 tst_brk(TFAIL | TTERRNO, "execve() failed unexpectedly");
53
54 tst_res(TPASS | TTERRNO, "execve() failed expectedly");
55
56 exit(0);
57 }
58
verify_execve(void)59 static void verify_execve(void)
60 {
61 pid_t pid = SAFE_FORK();
62
63 if (pid == 0)
64 do_child();
65 }
66
setup(void)67 static void setup(void)
68 {
69 struct passwd *pwd;
70
71 SAFE_CHMOD(TEST_APP, 0700);
72
73 pwd = SAFE_GETPWNAM(USER_NAME);
74 nobody_uid = pwd->pw_uid;
75 }
76
77 static const char *const resource_files[] = {
78 TEST_APP,
79 NULL,
80 };
81
82 static struct tst_test test = {
83 .needs_root = 1,
84 .forks_child = 1,
85 .child_needs_reinit = 1,
86 .setup = setup,
87 .resource_files = resource_files,
88 .test_all = verify_execve,
89 };
90