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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
// 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 <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "gtest/gtest.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/temp_umask.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
TEST(CreateTest, TmpFile) {
int fd;
EXPECT_THAT(fd = open(JoinPath(GetAbsoluteTestTmpdir(), "a").c_str(),
O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
}
TEST(CreateTest, ExistingFile) {
int fd;
EXPECT_THAT(
fd = open(JoinPath(GetAbsoluteTestTmpdir(), "ExistingFile").c_str(),
O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
EXPECT_THAT(
fd = open(JoinPath(GetAbsoluteTestTmpdir(), "ExistingFile").c_str(),
O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
}
TEST(CreateTest, CreateAtFile) {
int dirfd;
EXPECT_THAT(dirfd = open(GetAbsoluteTestTmpdir().c_str(), O_DIRECTORY, 0666),
SyscallSucceeds());
EXPECT_THAT(openat(dirfd, "CreateAtFile", O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
EXPECT_THAT(close(dirfd), SyscallSucceeds());
}
TEST(CreateTest, HonorsUmask_NoRandomSave) {
const DisableSave ds; // file cannot be re-opened as writable.
TempUmask mask(0222);
int fd;
ASSERT_THAT(
fd = open(JoinPath(GetAbsoluteTestTmpdir(), "UmaskedFile").c_str(),
O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
struct stat statbuf;
ASSERT_THAT(fstat(fd, &statbuf), SyscallSucceeds());
EXPECT_EQ(0444, statbuf.st_mode & 0777);
EXPECT_THAT(close(fd), SyscallSucceeds());
}
TEST(CreateTest, CreateExclusively) {
std::string filename = NewTempAbsPath();
int fd;
ASSERT_THAT(fd = open(filename.c_str(), O_CREAT | O_RDWR, 0644),
SyscallSucceeds());
EXPECT_THAT(close(fd), SyscallSucceeds());
EXPECT_THAT(open(filename.c_str(), O_CREAT | O_EXCL | O_RDWR, 0644),
SyscallFailsWithErrno(EEXIST));
}
TEST(CreateTest, CreatWithOTrunc) {
std::string dirpath = JoinPath(GetAbsoluteTestTmpdir(), "truncd");
ASSERT_THAT(mkdir(dirpath.c_str(), 0777), SyscallSucceeds());
ASSERT_THAT(open(dirpath.c_str(), O_CREAT | O_TRUNC, 0666),
SyscallFailsWithErrno(EISDIR));
}
TEST(CreateTest, CreatDirWithOTruncAndReadOnly) {
std::string dirpath = JoinPath(GetAbsoluteTestTmpdir(), "truncd");
ASSERT_THAT(mkdir(dirpath.c_str(), 0777), SyscallSucceeds());
ASSERT_THAT(open(dirpath.c_str(), O_CREAT | O_TRUNC | O_RDONLY, 0666),
SyscallFailsWithErrno(EISDIR));
}
TEST(CreateTest, CreatFileWithOTruncAndReadOnly) {
std::string dirpath = JoinPath(GetAbsoluteTestTmpdir(), "truncfile");
int dirfd;
ASSERT_THAT(dirfd = open(dirpath.c_str(), O_RDWR | O_CREAT, 0666),
SyscallSucceeds());
ASSERT_THAT(open(dirpath.c_str(), O_CREAT | O_TRUNC | O_RDONLY, 0666),
SyscallSucceeds());
ASSERT_THAT(close(dirfd), SyscallSucceeds());
}
TEST(CreateTest, CreateFailsOnDirWithoutWritePerms) {
// Make sure we don't have CAP_DAC_OVERRIDE, since that allows the user to
// always override directory permissions.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
auto parent = ASSERT_NO_ERRNO_AND_VALUE(
TempPath::CreateDirWith(GetAbsoluteTestTmpdir(), 0555));
auto file = JoinPath(parent.path(), "foo");
ASSERT_THAT(open(file.c_str(), O_CREAT | O_RDWR, 0644),
SyscallFailsWithErrno(EACCES));
}
// A file originally created RW, but opened RO can later be opened RW.
// Regression test for b/65385065.
TEST(CreateTest, OpenCreateROThenRW) {
TempPath file(NewTempAbsPath());
// Create a RW file, but only open it RO.
FileDescriptor fd1 = ASSERT_NO_ERRNO_AND_VALUE(
Open(file.path(), O_CREAT | O_EXCL | O_RDONLY, 0644));
// Now get a RW FD.
FileDescriptor fd2 = ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDWR));
// fd1 is not writable, but fd2 is.
char c = 'a';
EXPECT_THAT(WriteFd(fd1.get(), &c, 1), SyscallFailsWithErrno(EBADF));
EXPECT_THAT(WriteFd(fd2.get(), &c, 1), SyscallSucceedsWithValue(1));
}
TEST(CreateTest, ChmodReadToWriteBetweenOpens_NoRandomSave) {
// Make sure we don't have CAP_DAC_OVERRIDE, since that allows the user to
// override file read/write permissions. CAP_DAC_READ_SEARCH needs to be
// cleared for the same reason.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_READ_SEARCH, false));
const TempPath file =
ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileMode(0400));
const FileDescriptor rfd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
// Cannot restore after making permissions more restrictive.
const DisableSave ds;
ASSERT_THAT(fchmod(rfd.get(), 0200), SyscallSucceeds());
EXPECT_THAT(open(file.path().c_str(), O_RDONLY),
SyscallFailsWithErrno(EACCES));
const FileDescriptor wfd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY));
char c = 'x';
EXPECT_THAT(write(wfd.get(), &c, 1), SyscallSucceedsWithValue(1));
c = 0;
EXPECT_THAT(read(rfd.get(), &c, 1), SyscallSucceedsWithValue(1));
EXPECT_EQ(c, 'x');
}
TEST(CreateTest, ChmodWriteToReadBetweenOpens_NoRandomSave) {
// Make sure we don't have CAP_DAC_OVERRIDE, since that allows the user to
// override file read/write permissions.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
const TempPath file =
ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileMode(0200));
const FileDescriptor wfd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY));
// Cannot restore after making permissions more restrictive.
const DisableSave ds;
ASSERT_THAT(fchmod(wfd.get(), 0400), SyscallSucceeds());
EXPECT_THAT(open(file.path().c_str(), O_WRONLY),
SyscallFailsWithErrno(EACCES));
const FileDescriptor rfd =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
char c = 'x';
EXPECT_THAT(write(wfd.get(), &c, 1), SyscallSucceedsWithValue(1));
c = 0;
EXPECT_THAT(read(rfd.get(), &c, 1), SyscallSucceedsWithValue(1));
EXPECT_EQ(c, 'x');
}
TEST(CreateTest, CreateWithReadFlagNotAllowedByMode_NoRandomSave) {
// The only time we can open a file with flags forbidden by its permissions
// is when we are creating the file. We cannot re-open with the same flags,
// so we cannot restore an fd obtained from such an operation.
const DisableSave ds;
// Make sure we don't have CAP_DAC_OVERRIDE, since that allows the user to
// override file read/write permissions. CAP_DAC_READ_SEARCH needs to be
// cleared for the same reason.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_READ_SEARCH, false));
// Create and open a file with read flag but without read permissions.
const std::string path = NewTempAbsPath();
const FileDescriptor rfd =
ASSERT_NO_ERRNO_AND_VALUE(Open(path, O_CREAT | O_RDONLY, 0222));
EXPECT_THAT(open(path.c_str(), O_RDONLY), SyscallFailsWithErrno(EACCES));
const FileDescriptor wfd = ASSERT_NO_ERRNO_AND_VALUE(Open(path, O_WRONLY));
char c = 'x';
EXPECT_THAT(write(wfd.get(), &c, 1), SyscallSucceedsWithValue(1));
c = 0;
EXPECT_THAT(read(rfd.get(), &c, 1), SyscallSucceedsWithValue(1));
EXPECT_EQ(c, 'x');
}
TEST(CreateTest, CreateWithWriteFlagNotAllowedByMode_NoRandomSave) {
// The only time we can open a file with flags forbidden by its permissions
// is when we are creating the file. We cannot re-open with the same flags,
// so we cannot restore an fd obtained from such an operation.
const DisableSave ds;
// Make sure we don't have CAP_DAC_OVERRIDE, since that allows the user to
// override file read/write permissions.
ASSERT_NO_ERRNO(SetCapability(CAP_DAC_OVERRIDE, false));
// Create and open a file with write flag but without write permissions.
const std::string path = NewTempAbsPath();
const FileDescriptor wfd =
ASSERT_NO_ERRNO_AND_VALUE(Open(path, O_CREAT | O_WRONLY, 0444));
EXPECT_THAT(open(path.c_str(), O_WRONLY), SyscallFailsWithErrno(EACCES));
const FileDescriptor rfd = ASSERT_NO_ERRNO_AND_VALUE(Open(path, O_RDONLY));
char c = 'x';
EXPECT_THAT(write(wfd.get(), &c, 1), SyscallSucceedsWithValue(1));
c = 0;
EXPECT_THAT(read(rfd.get(), &c, 1), SyscallSucceedsWithValue(1));
EXPECT_EQ(c, 'x');
}
} // namespace
} // namespace testing
} // namespace gvisor
|