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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
// 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 <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "gtest/gtest.h"
#include "absl/time/clock.h"
#include "test/util/file_descriptor.h"
#include "test/util/fs_util.h"
#include "test/util/posix_error.h"
#include "test/util/socket_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
using ::testing::Gt;
namespace gvisor {
namespace testing {
namespace {
constexpr char kMessage[] = "hello world";
// PartialBadBufferTest checks the result of various IO syscalls when passed a
// buffer that does not have the space specified in the syscall (most of it is
// PROT_NONE). Linux is annoyingly inconsistent among different syscalls, so we
// test all of them.
class PartialBadBufferTest : public ::testing::Test {
protected:
void SetUp() override {
// Create and open a directory for getdents cases.
directory_ = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
ASSERT_THAT(
directory_fd_ = open(directory_.path().c_str(), O_RDONLY | O_DIRECTORY),
SyscallSucceeds());
// Create and open a normal file, placing it in the directory
// so the getdents cases have some dirents.
name_ = JoinPath(directory_.path(), "a");
ASSERT_THAT(fd_ = open(name_.c_str(), O_RDWR | O_CREAT, 0644),
SyscallSucceeds());
// Write some initial data.
size_t size = sizeof(kMessage) - 1;
EXPECT_THAT(WriteFd(fd_, &kMessage, size), SyscallSucceedsWithValue(size));
ASSERT_THAT(lseek(fd_, 0, SEEK_SET), SyscallSucceeds());
// Map a useable buffer.
addr_ = mmap(0, 2 * kPageSize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
ASSERT_NE(addr_, MAP_FAILED);
char* buf = reinterpret_cast<char*>(addr_);
// Guard page for our read to run into.
ASSERT_THAT(mprotect(reinterpret_cast<void*>(buf + kPageSize), kPageSize,
PROT_NONE),
SyscallSucceeds());
// Leave only one free byte in the buffer.
bad_buffer_ = buf + kPageSize - 1;
}
off_t Size() {
struct stat st;
int rc = fstat(fd_, &st);
if (rc < 0) {
return static_cast<off_t>(rc);
}
return st.st_size;
}
void TearDown() override {
EXPECT_THAT(munmap(addr_, 2 * kPageSize), SyscallSucceeds()) << addr_;
EXPECT_THAT(close(fd_), SyscallSucceeds());
EXPECT_THAT(unlink(name_.c_str()), SyscallSucceeds());
EXPECT_THAT(close(directory_fd_), SyscallSucceeds());
}
// Return buffer with n bytes of free space.
// N.B. this is the same buffer used to back bad_buffer_.
char* FreeBytes(size_t n) {
TEST_CHECK(n <= static_cast<size_t>(4096));
return reinterpret_cast<char*>(addr_) + kPageSize - n;
}
std::string name_;
int fd_;
TempPath directory_;
int directory_fd_;
void* addr_;
char* bad_buffer_;
};
// We do both "big" and "small" tests to try to hit the "zero copy" and
// non-"zero copy" paths, which have different code paths for handling faults.
TEST_F(PartialBadBufferTest, ReadBig) {
EXPECT_THAT(RetryEINTR(read)(fd_, bad_buffer_, kPageSize),
SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, ReadSmall) {
EXPECT_THAT(RetryEINTR(read)(fd_, bad_buffer_, 10),
SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, PreadBig) {
EXPECT_THAT(RetryEINTR(pread)(fd_, bad_buffer_, kPageSize, 0),
SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, PreadSmall) {
EXPECT_THAT(RetryEINTR(pread)(fd_, bad_buffer_, 10, 0),
SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, ReadvBig) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = kPageSize;
EXPECT_THAT(RetryEINTR(readv)(fd_, &vec, 1), SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, ReadvSmall) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = 10;
EXPECT_THAT(RetryEINTR(readv)(fd_, &vec, 1), SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, PreadvBig) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = kPageSize;
EXPECT_THAT(RetryEINTR(preadv)(fd_, &vec, 1, 0), SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, PreadvSmall) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = 10;
EXPECT_THAT(RetryEINTR(preadv)(fd_, &vec, 1, 0), SyscallSucceedsWithValue(1));
EXPECT_EQ('h', bad_buffer_[0]);
}
TEST_F(PartialBadBufferTest, WriteBig) {
off_t orig_size = Size();
int n;
ASSERT_THAT(lseek(fd_, orig_size, SEEK_SET), SyscallSucceeds());
EXPECT_THAT(
(n = RetryEINTR(write)(fd_, bad_buffer_, kPageSize)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
TEST_F(PartialBadBufferTest, WriteSmall) {
off_t orig_size = Size();
int n;
ASSERT_THAT(lseek(fd_, orig_size, SEEK_SET), SyscallSucceeds());
EXPECT_THAT(
(n = RetryEINTR(write)(fd_, bad_buffer_, 10)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
TEST_F(PartialBadBufferTest, PwriteBig) {
off_t orig_size = Size();
int n;
EXPECT_THAT(
(n = RetryEINTR(pwrite)(fd_, bad_buffer_, kPageSize, orig_size)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
TEST_F(PartialBadBufferTest, PwriteSmall) {
off_t orig_size = Size();
int n;
EXPECT_THAT(
(n = RetryEINTR(pwrite)(fd_, bad_buffer_, 10, orig_size)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
TEST_F(PartialBadBufferTest, WritevBig) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = kPageSize;
off_t orig_size = Size();
int n;
ASSERT_THAT(lseek(fd_, orig_size, SEEK_SET), SyscallSucceeds());
EXPECT_THAT(
(n = RetryEINTR(writev)(fd_, &vec, 1)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
TEST_F(PartialBadBufferTest, WritevSmall) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = 10;
off_t orig_size = Size();
int n;
ASSERT_THAT(lseek(fd_, orig_size, SEEK_SET), SyscallSucceeds());
EXPECT_THAT(
(n = RetryEINTR(writev)(fd_, &vec, 1)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
TEST_F(PartialBadBufferTest, PwritevBig) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = kPageSize;
off_t orig_size = Size();
int n;
EXPECT_THAT(
(n = RetryEINTR(pwritev)(fd_, &vec, 1, orig_size)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
TEST_F(PartialBadBufferTest, PwritevSmall) {
struct iovec vec;
vec.iov_base = bad_buffer_;
vec.iov_len = 10;
off_t orig_size = Size();
int n;
EXPECT_THAT(
(n = RetryEINTR(pwritev)(fd_, &vec, 1, orig_size)),
AnyOf(SyscallFailsWithErrno(EFAULT), SyscallSucceedsWithValue(1)));
EXPECT_EQ(Size(), orig_size + (n >= 0 ? n : 0));
}
// getdents returns EFAULT when the you claim the buffer is large enough, but
// it actually isn't.
TEST_F(PartialBadBufferTest, GetdentsBig) {
EXPECT_THAT(RetryEINTR(syscall)(SYS_getdents64, directory_fd_, bad_buffer_,
kPageSize),
SyscallFailsWithErrno(EFAULT));
}
// getdents returns EINVAL when the you claim the buffer is too small.
TEST_F(PartialBadBufferTest, GetdentsSmall) {
EXPECT_THAT(
RetryEINTR(syscall)(SYS_getdents64, directory_fd_, bad_buffer_, 10),
SyscallFailsWithErrno(EINVAL));
}
// getdents will write entries into a buffer if there is space before it faults.
TEST_F(PartialBadBufferTest, GetdentsOneEntry) {
// 30 bytes is enough for one (small) entry.
char* buf = FreeBytes(30);
EXPECT_THAT(
RetryEINTR(syscall)(SYS_getdents64, directory_fd_, buf, kPageSize),
SyscallSucceedsWithValue(Gt(0)));
}
PosixErrorOr<sockaddr_storage> InetLoopbackAddr(int family) {
struct sockaddr_storage addr;
memset(&addr, 0, sizeof(addr));
addr.ss_family = family;
switch (family) {
case AF_INET:
reinterpret_cast<struct sockaddr_in*>(&addr)->sin_addr.s_addr =
htonl(INADDR_LOOPBACK);
break;
case AF_INET6:
reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_addr =
in6addr_loopback;
break;
default:
return PosixError(EINVAL,
absl::StrCat("unknown socket family: ", family));
}
return addr;
}
// SendMsgTCP verifies that calling sendmsg with a bad address returns an
// EFAULT. It also verifies that passing a buffer which is made up of 2
// pages one valid and one guard page succeeds as long as the write is
// for exactly the size of 1 page.
TEST_F(PartialBadBufferTest, SendMsgTCP) {
// FIXME(b/171436815): Netstack save/restore is broken.
const DisableSave ds;
auto listen_socket =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
// Initialize address to the loopback one.
sockaddr_storage addr = ASSERT_NO_ERRNO_AND_VALUE(InetLoopbackAddr(AF_INET));
socklen_t addrlen = sizeof(addr);
// Bind to some port then start listening.
ASSERT_THAT(bind(listen_socket.get(),
reinterpret_cast<struct sockaddr*>(&addr), addrlen),
SyscallSucceeds());
ASSERT_THAT(listen(listen_socket.get(), SOMAXCONN), SyscallSucceeds());
// Get the address we're listening on, then connect to it. We need to do this
// because we're allowing the stack to pick a port for us.
ASSERT_THAT(getsockname(listen_socket.get(),
reinterpret_cast<struct sockaddr*>(&addr), &addrlen),
SyscallSucceeds());
auto send_socket =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
ASSERT_THAT(
RetryEINTR(connect)(send_socket.get(),
reinterpret_cast<struct sockaddr*>(&addr), addrlen),
SyscallSucceeds());
// Accept the connection.
auto recv_socket =
ASSERT_NO_ERRNO_AND_VALUE(Accept(listen_socket.get(), nullptr, nullptr));
// TODO(gvisor.dev/issue/674): Update this once Netstack matches linux
// behaviour on a setsockopt of SO_RCVBUF/SO_SNDBUF.
//
// Set SO_SNDBUF for socket to exactly kPageSize+1.
//
// gVisor does not double the value passed in SO_SNDBUF like linux does so we
// just increase it by 1 byte here for gVisor so that we can test writing 1
// byte past the valid page and check that it triggers an EFAULT
// correctly. Otherwise in gVisor the sendmsg call will just return with no
// error with kPageSize bytes written successfully.
const uint32_t buf_size = kPageSize + 1;
ASSERT_THAT(setsockopt(send_socket.get(), SOL_SOCKET, SO_SNDBUF, &buf_size,
sizeof(buf_size)),
SyscallSucceedsWithValue(0));
struct msghdr hdr = {};
struct iovec iov = {};
iov.iov_base = bad_buffer_;
iov.iov_len = kPageSize;
hdr.msg_iov = &iov;
hdr.msg_iovlen = 1;
ASSERT_THAT(RetryEINTR(sendmsg)(send_socket.get(), &hdr, 0),
SyscallFailsWithErrno(EFAULT));
// Now assert that writing kPageSize from addr_ succeeds.
iov.iov_base = addr_;
ASSERT_THAT(RetryEINTR(sendmsg)(send_socket.get(), &hdr, 0),
SyscallSucceedsWithValue(kPageSize));
// Read all the data out so that we drain the socket SND_BUF on the sender.
std::vector<char> buffer(kPageSize);
ASSERT_THAT(RetryEINTR(read)(recv_socket.get(), buffer.data(), kPageSize),
SyscallSucceedsWithValue(kPageSize));
// Sleep for a shortwhile to ensure that we have time to process the
// ACKs. This is not strictly required unless running under gotsan which is a
// lot slower and can result in the next write to write only 1 byte instead of
// our intended kPageSize + 1.
absl::SleepFor(absl::Milliseconds(50));
// Now assert that writing > kPageSize results in EFAULT.
iov.iov_len = kPageSize + 1;
ASSERT_THAT(RetryEINTR(sendmsg)(send_socket.get(), &hdr, 0),
SyscallFailsWithErrno(EFAULT));
}
} // namespace
} // namespace testing
} // namespace gvisor
|