1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "gtest/gtest.h"
#include "absl/strings/str_cat.h"
#include "test/util/capability_util.h"
#include "test/util/file_descriptor.h"
#include "test/util/fs_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
TEST(UnlinkTest, IsDir) {
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
EXPECT_THAT(unlink(dir.path().c_str()), SyscallFailsWithErrno(EISDIR));
}
TEST(UnlinkTest, DirNotEmpty) {
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
int fd;
std::string path = JoinPath(dir.path(), "ExistingFile");
EXPECT_THAT(fd = open(path.c_str(), O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
EXPECT_THAT(rmdir(dir.path().c_str()), SyscallFailsWithErrno(ENOTEMPTY));
}
TEST(UnlinkTest, Rmdir) {
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
EXPECT_THAT(rmdir(dir.path().c_str()), SyscallSucceeds());
}
TEST(UnlinkTest, AtDir) {
int dirfd;
auto tmpdir = GetAbsoluteTestTmpdir();
EXPECT_THAT(dirfd = open(tmpdir.c_str(), O_DIRECTORY, 0), SyscallSucceeds());
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(tmpdir));
auto dir_relpath =
ASSERT_NO_ERRNO_AND_VALUE(GetRelativePath(tmpdir, dir.path()));
EXPECT_THAT(unlinkat(dirfd, dir_relpath.c_str(), AT_REMOVEDIR),
SyscallSucceeds());
ASSERT_THAT(close(dirfd), SyscallSucceeds());
}
TEST(UnlinkTest, AtDirDegradedPermissions_NoRandomSave) {
// Drop capabilities that allow us to override file and directory permissions.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_READ_SEARCH, false));
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
int dirfd;
ASSERT_THAT(dirfd = open(dir.path().c_str(), O_DIRECTORY, 0),
SyscallSucceeds());
std::string sub_dir = JoinPath(dir.path(), "NewDir");
EXPECT_THAT(mkdir(sub_dir.c_str(), 0755), SyscallSucceeds());
EXPECT_THAT(fchmod(dirfd, 0444), SyscallSucceeds());
EXPECT_THAT(unlinkat(dirfd, "NewDir", AT_REMOVEDIR),
SyscallFailsWithErrno(EACCES));
ASSERT_THAT(close(dirfd), SyscallSucceeds());
}
// Files cannot be unlinked if the parent is not writable and executable.
TEST(UnlinkTest, ParentDegradedPermissions) {
// Drop capabilities that allow us to override file and directory permissions.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_READ_SEARCH, false));
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir.path()));
ASSERT_THAT(chmod(dir.path().c_str(), 0000), SyscallSucceeds());
struct stat st;
ASSERT_THAT(stat(file.path().c_str(), &st), SyscallFailsWithErrno(EACCES));
ASSERT_THAT(unlinkat(AT_FDCWD, file.path().c_str(), 0),
SyscallFailsWithErrno(EACCES));
// Non-existent files also return EACCES.
const std::string nonexist = JoinPath(dir.path(), "doesnotexist");
ASSERT_THAT(stat(nonexist.c_str(), &st), SyscallFailsWithErrno(EACCES));
ASSERT_THAT(unlinkat(AT_FDCWD, nonexist.c_str(), 0),
SyscallFailsWithErrno(EACCES));
}
TEST(UnlinkTest, AtBad) {
int dirfd;
EXPECT_THAT(dirfd = open(GetAbsoluteTestTmpdir().c_str(), O_DIRECTORY, 0),
SyscallSucceeds());
// Try removing a directory as a file.
std::string path = JoinPath(GetAbsoluteTestTmpdir(), "NewDir");
EXPECT_THAT(mkdir(path.c_str(), 0755), SyscallSucceeds());
EXPECT_THAT(unlinkat(dirfd, "NewDir", 0), SyscallFailsWithErrno(EISDIR));
EXPECT_THAT(unlinkat(dirfd, "NewDir", AT_REMOVEDIR), SyscallSucceeds());
// Try removing a file as a directory.
int fd;
EXPECT_THAT(fd = openat(dirfd, "UnlinkAtFile", O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(unlinkat(dirfd, "UnlinkAtFile", AT_REMOVEDIR),
SyscallFailsWithErrno(ENOTDIR));
EXPECT_THAT(unlinkat(dirfd, "UnlinkAtFile/", 0),
SyscallFailsWithErrno(ENOTDIR));
ASSERT_THAT(close(fd), SyscallSucceeds());
EXPECT_THAT(unlinkat(dirfd, "UnlinkAtFile", 0), SyscallSucceeds());
// Cleanup.
ASSERT_THAT(close(dirfd), SyscallSucceeds());
}
TEST(UnlinkTest, AbsTmpFile) {
int fd;
std::string path = JoinPath(GetAbsoluteTestTmpdir(), "ExistingFile");
EXPECT_THAT(fd = open(path.c_str(), O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
EXPECT_THAT(unlink(path.c_str()), SyscallSucceeds());
}
TEST(UnlinkTest, TooLongName) {
EXPECT_THAT(unlink(std::vector<char>(16384, '0').data()),
SyscallFailsWithErrno(ENAMETOOLONG));
}
TEST(UnlinkTest, BadNamePtr) {
EXPECT_THAT(unlink(reinterpret_cast<char*>(1)),
SyscallFailsWithErrno(EFAULT));
}
TEST(UnlinkTest, AtFile) {
int dirfd;
EXPECT_THAT(dirfd = open(GetAbsoluteTestTmpdir().c_str(), O_DIRECTORY, 0666),
SyscallSucceeds());
int fd;
EXPECT_THAT(fd = openat(dirfd, "UnlinkAtFile", O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
EXPECT_THAT(unlinkat(dirfd, "UnlinkAtFile", 0), SyscallSucceeds());
}
TEST(UnlinkTest, OpenFile_NoRandomSave) {
// We can't save unlinked file unless they are on tmpfs.
const DisableSave ds;
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
int fd;
EXPECT_THAT(fd = open(file.path().c_str(), O_RDWR, 0666), SyscallSucceeds());
EXPECT_THAT(unlink(file.path().c_str()), SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
}
TEST(UnlinkTest, CannotRemoveDots) {
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const std::string self = JoinPath(file.path(), ".");
ASSERT_THAT(unlink(self.c_str()), SyscallFailsWithErrno(ENOTDIR));
const std::string parent = JoinPath(file.path(), "..");
ASSERT_THAT(unlink(parent.c_str()), SyscallFailsWithErrno(ENOTDIR));
}
TEST(UnlinkTest, CannotRemoveRoot) {
ASSERT_THAT(unlinkat(-1, "/", AT_REMOVEDIR), SyscallFailsWithErrno(EBUSY));
}
TEST(UnlinkTest, CannotRemoveRootWithAtDir) {
const FileDescriptor dirfd = ASSERT_NO_ERRNO_AND_VALUE(
Open(GetAbsoluteTestTmpdir(), O_DIRECTORY, 0666));
ASSERT_THAT(unlinkat(dirfd.get(), "/", AT_REMOVEDIR),
SyscallFailsWithErrno(EBUSY));
}
TEST(RmdirTest, CannotRemoveDots) {
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
const std::string self = JoinPath(dir.path(), ".");
ASSERT_THAT(rmdir(self.c_str()), SyscallFailsWithErrno(EINVAL));
const std::string parent = JoinPath(dir.path(), "..");
ASSERT_THAT(rmdir(parent.c_str()), SyscallFailsWithErrno(ENOTEMPTY));
}
TEST(RmdirTest, CanRemoveWithTrailingSlashes) {
auto dir1 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
const std::string slash = absl::StrCat(dir1.path(), "/");
ASSERT_THAT(rmdir(slash.c_str()), SyscallSucceeds());
auto dir2 = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
const std::string slashslash = absl::StrCat(dir2.path(), "//");
ASSERT_THAT(rmdir(slashslash.c_str()), SyscallSucceeds());
}
TEST(UnlinkTest, UnlinkAtEmptyPath) {
auto dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
auto file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileIn(dir.path()));
auto fd = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR, 0666));
EXPECT_THAT(unlinkat(fd.get(), "", 0), SyscallFailsWithErrno(ENOENT));
auto dirInDir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDirIn(dir.path()));
auto dirFD = ASSERT_NO_ERRNO_AND_VALUE(
Open(dirInDir.path(), O_RDONLY | O_DIRECTORY, 0666));
EXPECT_THAT(unlinkat(dirFD.get(), "", AT_REMOVEDIR),
SyscallFailsWithErrno(ENOENT));
}
} // namespace
} // namespace testing
} // namespace gvisor
|