xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/lstat/lstat02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *   07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  *   06/2019 Ported to new library: Christian Amann <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * This test verifies that:
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   1) lstat(2) returns -1 and sets errno to EACCES if search permission is
11*49cdfc7eSAndroid Build Coastguard Worker  *	denied on a component of the path prefix.
12*49cdfc7eSAndroid Build Coastguard Worker  *   2) lstat(2) returns -1 and sets errno to ENOENT if the specified file
13*49cdfc7eSAndroid Build Coastguard Worker  *	does not exists or empty string.
14*49cdfc7eSAndroid Build Coastguard Worker  *   3) lstat(2) returns -1 and sets errno to EFAULT if pathname points
15*49cdfc7eSAndroid Build Coastguard Worker  *	outside user's accessible address space.
16*49cdfc7eSAndroid Build Coastguard Worker  *   4) lstat(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
17*49cdfc7eSAndroid Build Coastguard Worker  *	component is too long.
18*49cdfc7eSAndroid Build Coastguard Worker  *   5) lstat(2) returns -1 and sets errno to ENOTDIR if the directory
19*49cdfc7eSAndroid Build Coastguard Worker  *	component in pathname is not a directory.
20*49cdfc7eSAndroid Build Coastguard Worker  *   6) lstat(2) returns -1 and sets errno to ELOOP if the pathname has too
21*49cdfc7eSAndroid Build Coastguard Worker  *	many symbolic links encountered while traversing.
22*49cdfc7eSAndroid Build Coastguard Worker  */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #define MODE_RWX	0777
34*49cdfc7eSAndroid Build Coastguard Worker #define MODE_RW0	0666
35*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR	"test_dir"
36*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE	"test_file"
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker #define TEST_ELOOP	"/test_eloop"
39*49cdfc7eSAndroid Build Coastguard Worker #define TEST_ENOENT	""
40*49cdfc7eSAndroid Build Coastguard Worker #define TEST_EACCES	TEST_DIR"/test_eacces"
41*49cdfc7eSAndroid Build Coastguard Worker #define TEST_ENOTDIR	TEST_FILE"/test_enotdir"
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker static char longpathname[PATH_MAX + 2];
44*49cdfc7eSAndroid Build Coastguard Worker static char elooppathname[sizeof(TEST_ELOOP) * 43];
45*49cdfc7eSAndroid Build Coastguard Worker static struct stat stat_buf;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
48*49cdfc7eSAndroid Build Coastguard Worker 	char *pathname;
49*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
50*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
51*49cdfc7eSAndroid Build Coastguard Worker 	{TEST_EACCES, EACCES},
52*49cdfc7eSAndroid Build Coastguard Worker 	{TEST_ENOENT, ENOENT},
53*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, EFAULT},
54*49cdfc7eSAndroid Build Coastguard Worker 	{longpathname, ENAMETOOLONG},
55*49cdfc7eSAndroid Build Coastguard Worker 	{TEST_ENOTDIR, ENOTDIR},
56*49cdfc7eSAndroid Build Coastguard Worker 	{elooppathname, ELOOP},
57*49cdfc7eSAndroid Build Coastguard Worker };
58*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)59*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case_t *tc = &test_cases[n];
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	TEST(lstat(tc->pathname, &stat_buf));
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "lstat() returned %ld, expected -1",
67*49cdfc7eSAndroid Build Coastguard Worker 			TST_RET);
68*49cdfc7eSAndroid Build Coastguard Worker 		return;
69*49cdfc7eSAndroid Build Coastguard Worker 	}
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == TST_ERR) {
72*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "lstat() failed as expected");
73*49cdfc7eSAndroid Build Coastguard Worker 	} else {
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
75*49cdfc7eSAndroid Build Coastguard Worker 			"lstat() failed unexpectedly; expected: %s - got",
76*49cdfc7eSAndroid Build Coastguard Worker 			tst_strerrno(tc->exp_errno));
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)80*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	int i;
83*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser;
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	/* Drop privileges for EACCES test */
86*49cdfc7eSAndroid Build Coastguard Worker 	if (geteuid() == 0) {
87*49cdfc7eSAndroid Build Coastguard Worker 		ltpuser = SAFE_GETPWNAM("nobody");
88*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETEUID(ltpuser->pw_uid);
89*49cdfc7eSAndroid Build Coastguard Worker 	}
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	memset(longpathname, 'a', PATH_MAX+1);
92*49cdfc7eSAndroid Build Coastguard Worker 	longpathname[PATH_MAX+1] = '\0';
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEST_DIR, MODE_RWX);
95*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEST_EACCES, MODE_RWX, NULL);
96*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEST_FILE, MODE_RWX, NULL);
97*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(TEST_DIR, MODE_RW0);
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR("test_eloop", MODE_RWX);
100*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
101*49cdfc7eSAndroid Build Coastguard Worker 	/*
102*49cdfc7eSAndroid Build Coastguard Worker 	 * NOTE: The ELOOP test is written based on the fact that the
103*49cdfc7eSAndroid Build Coastguard Worker 	 * consecutive symlinks limit in the kernel is hardwired to 40.
104*49cdfc7eSAndroid Build Coastguard Worker 	 */
105*49cdfc7eSAndroid Build Coastguard Worker 	elooppathname[0] = '.';
106*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 43; i++)
107*49cdfc7eSAndroid Build Coastguard Worker 		strcat(elooppathname, TEST_ELOOP);
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)110*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
111*49cdfc7eSAndroid Build Coastguard Worker {
112*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(TEST_DIR, MODE_RWX);
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
116*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
117*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(test_cases),
118*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
119*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
120*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
121*49cdfc7eSAndroid Build Coastguard Worker };
122