• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *     07/2001 Ported by Wayne Boyer
5  * Copyright (c) 2019 SUSE LLC <mdoucha@suse.cz>
6  */
7 
8 /*
9  * Check that the chdir() syscall returns correct value and error code
10  * in various situations when called with root privileges
11  */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <pwd.h>
18 
19 #include "tst_test.h"
20 
21 #define MNTPOINT "mntpoint"
22 
23 #define FILE_NAME "testfile"
24 #define DIR_NAME "subdir"
25 #define BLOCKED_NAME "keep_out"
26 #define LINK_NAME1 "symloop"
27 #define LINK_NAME2 "symloop2"
28 #define TESTUSER "nobody"
29 
30 static char *workdir;
31 static int skip_symlinks, skip_blocked;
32 static struct passwd *ltpuser;
33 
34 static struct test_case {
35 	const char *name;
36 	int root_ret, root_err, nobody_ret, nobody_err;
37 } testcase_list[] = {
38 	{FILE_NAME, -1, ENOTDIR, -1, ENOTDIR},
39 	{BLOCKED_NAME, 0, 0, -1, EACCES},
40 	{DIR_NAME, 0, 0, 0, 0},
41 	{".", 0, 0, 0, 0},
42 	{"..", 0, 0, 0, 0},
43 	{"/", 0, 0, 0, 0},
44 	{"missing", -1, ENOENT, -1, ENOENT},
45 	{LINK_NAME1, -1, ELOOP, -1, ELOOP},
46 };
47 
setup(void)48 static void setup(void)
49 {
50 	char *cwd;
51 	int fd;
52 	struct stat statbuf;
53 
54 	umask(0);
55 
56 	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
57 
58 	cwd = SAFE_GETCWD(NULL, 0);
59 	workdir = SAFE_MALLOC(strlen(cwd) + strlen(MNTPOINT) + 2);
60 	sprintf(workdir, "%s/%s", cwd, MNTPOINT);
61 	free(cwd);
62 	SAFE_CHDIR(workdir);
63 
64 	SAFE_MKDIR(DIR_NAME, 0755);
65 	SAFE_MKDIR(BLOCKED_NAME, 0644);
66 
67 	/* FAT and NTFS override file and directory permissions */
68 	SAFE_STAT(BLOCKED_NAME, &statbuf);
69 	skip_blocked = statbuf.st_mode & 0111;
70 	skip_symlinks = 0;
71 	TEST(symlink(LINK_NAME1, LINK_NAME2));
72 
73 	if (!TST_RET)
74 		SAFE_SYMLINK(LINK_NAME2, LINK_NAME1);
75 	else if (TST_RET == -1 && (TST_ERR == EPERM || TST_ERR == ENOSYS))
76 		skip_symlinks = 1;
77 	else
78 		tst_brk(TBROK | TTERRNO, "Cannot create symlinks");
79 
80 	fd = SAFE_CREAT(FILE_NAME, 0644);
81 	SAFE_CLOSE(fd);
82 
83 	if (!ltpuser)
84 		ltpuser = SAFE_GETPWNAM(TESTUSER);
85 }
86 
check_result(const char * user,const char * name,int retval,int experr)87 static void check_result(const char *user, const char *name, int retval,
88 	int experr)
89 {
90 	if (TST_RET != retval) {
91 		tst_res(TFAIL | TTERRNO,
92 			"%s: chdir(\"%s\") returned unexpected value %ld",
93 			user, name, TST_RET);
94 		return;
95 	}
96 
97 	if (TST_RET != 0 && TST_ERR != experr) {
98 		tst_res(TFAIL | TTERRNO,
99 			"%s: chdir(\"%s\") returned unexpected error", user,
100 			name);
101 		return;
102 	}
103 
104 	tst_res(TPASS | TTERRNO, "%s: chdir(\"%s\") returned correct value",
105 		user, name);
106 }
107 
run(unsigned int n)108 static void run(unsigned int n)
109 {
110 	struct test_case *tc = testcase_list + n;
111 
112 	tst_res(TINFO, "Testing '%s'", tc->name);
113 
114 	if (tc->root_err == ELOOP && skip_symlinks) {
115 		tst_res(TCONF, "Skipping symlink loop test, not supported");
116 		return;
117 	}
118 
119 	/* Reset current directory to mountpoint */
120 	SAFE_CHDIR(workdir);
121 
122 	TEST(chdir(tc->name));
123 	check_result("root", tc->name, tc->root_ret, tc->root_err);
124 
125 	if (tc->nobody_err == EACCES && skip_blocked) {
126 		tst_res(TCONF, "Skipping unprivileged permission test, "
127 			"FS mangles dir mode");
128 		return;
129 	}
130 
131 	SAFE_CHDIR(workdir);
132 	SAFE_SETEUID(ltpuser->pw_uid);
133 	TEST(chdir(tc->name));
134 	SAFE_SETEUID(0);
135 	check_result(TESTUSER, tc->name, tc->nobody_ret, tc->nobody_err);
136 }
137 
cleanup(void)138 static void cleanup(void)
139 {
140 	SAFE_CHDIR("..");
141 	tst_umount(workdir);
142 	free(workdir);
143 }
144 
145 static struct tst_test test = {
146 	.needs_root = 1,
147 	.format_device = 1,
148 	.mntpoint = MNTPOINT,
149 	.all_filesystems = 1,
150 	.test = run,
151 	.tcnt = ARRAY_SIZE(testcase_list),
152 	.setup = setup,
153 	.cleanup = cleanup
154 };
155