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