xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/execveat/execveat02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  * Authors: Jinhui huang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /* Test Description:
7*49cdfc7eSAndroid Build Coastguard Worker  *  Check various errnos for execveat(2):
8*49cdfc7eSAndroid Build Coastguard Worker  *    1) execveat() fails and returns EBADF if dirfd is a invalid file
9*49cdfc7eSAndroid Build Coastguard Worker  *	 descriptor.
10*49cdfc7eSAndroid Build Coastguard Worker  *    2) execveat() fails and returns EINVAL if flag specified is invalid.
11*49cdfc7eSAndroid Build Coastguard Worker  *    3) execveat() fails and returns ELOOP if the file identified by dirfd and
12*49cdfc7eSAndroid Build Coastguard Worker  *       pathname is a symbolic link and flag includes AT_SYMLINK_NOFOLLOW.
13*49cdfc7eSAndroid Build Coastguard Worker  *    4) execveat() fails and returns ENOTDIR if pathname is relative and dirfd
14*49cdfc7eSAndroid Build Coastguard Worker  *       is a file descriptor referring to a file other than a directory.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
18*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/execveat.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "execveat.h"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR "testdir"
29*49cdfc7eSAndroid Build Coastguard Worker #define TEST_APP "execveat_errno"
30*49cdfc7eSAndroid Build Coastguard Worker #define TEST_SYMLINK "execveat_symlink"
31*49cdfc7eSAndroid Build Coastguard Worker #define TEST_REL_APP TESTDIR"/"TEST_APP
32*49cdfc7eSAndroid Build Coastguard Worker #define TEST_ERL_SYMLINK TESTDIR"/"TEST_SYMLINK
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static int bad_fd = -1, fd;
35*49cdfc7eSAndroid Build Coastguard Worker static char app_abs_path[512], app_sym_path[512];
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
38*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
39*49cdfc7eSAndroid Build Coastguard Worker 	char *pathname;
40*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
41*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
42*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
43*49cdfc7eSAndroid Build Coastguard Worker 	{&bad_fd, "", AT_EMPTY_PATH, EBADF},
44*49cdfc7eSAndroid Build Coastguard Worker 	{&fd, app_abs_path, -1, EINVAL},
45*49cdfc7eSAndroid Build Coastguard Worker 	{&fd, app_sym_path, AT_SYMLINK_NOFOLLOW, ELOOP},
46*49cdfc7eSAndroid Build Coastguard Worker 	{&fd, TEST_REL_APP, 0, ENOTDIR},
47*49cdfc7eSAndroid Build Coastguard Worker };
48*49cdfc7eSAndroid Build Coastguard Worker 
verify_execveat(unsigned int i)49*49cdfc7eSAndroid Build Coastguard Worker static void verify_execveat(unsigned int i)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
52*49cdfc7eSAndroid Build Coastguard Worker 	char *argv[2] = {TEST_APP, NULL};
53*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
56*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0) {
57*49cdfc7eSAndroid Build Coastguard Worker 		TEST(execveat(*tc->fd, tc->pathname, argv, environ, tc->flag));
58*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->exp_err != TST_ERR) {
59*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO,
60*49cdfc7eSAndroid Build Coastguard Worker 				"execveat() fails unexpectedly, expected: %s",
61*49cdfc7eSAndroid Build Coastguard Worker 				tst_strerrno(tc->exp_err));
62*49cdfc7eSAndroid Build Coastguard Worker 		} else {
63*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS | TTERRNO,
64*49cdfc7eSAndroid Build Coastguard Worker 				"execveat() fails as expected");
65*49cdfc7eSAndroid Build Coastguard Worker 		}
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)69*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker 	char cur_dir_path[512];
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	check_execveat();
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TESTDIR, 0777);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CP(TEST_APP, TEST_REL_APP);
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETCWD(cur_dir_path, sizeof(cur_dir_path));
80*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(app_abs_path, "%s/%s", cur_dir_path, TEST_REL_APP);
81*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(app_sym_path, "%s/%s", cur_dir_path, TEST_ERL_SYMLINK);
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(TEST_REL_APP, TEST_ERL_SYMLINK);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TEST_REL_APP, O_PATH);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)88*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
91*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
95*49cdfc7eSAndroid Build Coastguard Worker 	.resource_files = (const char *const []) {
96*49cdfc7eSAndroid Build Coastguard Worker 		TEST_APP,
97*49cdfc7eSAndroid Build Coastguard Worker 		NULL
98*49cdfc7eSAndroid Build Coastguard Worker 	},
99*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
100*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_execveat,
101*49cdfc7eSAndroid Build Coastguard Worker 	.child_needs_reinit = 1,
102*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
103*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
104*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
105*49cdfc7eSAndroid Build Coastguard Worker };
106