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
|
// Copyright 2019 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 <arpa/inet.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <cstdio>
#include <cstring>
#include <map>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gtest/gtest.h"
#include "test/syscalls/linux/ip_socket_test_util.h"
#include "test/syscalls/linux/socket_bind_to_device_util.h"
#include "test/syscalls/linux/socket_test_util.h"
#include "test/util/capability_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
namespace gvisor {
namespace testing {
using std::string;
// Test fixture for SO_BINDTODEVICE tests.
class BindToDeviceTest : public ::testing::TestWithParam<SocketKind> {
protected:
void SetUp() override {
printf("Testing case: %s\n", GetParam().description.c_str());
ASSERT_TRUE(ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_NET_RAW)))
<< "CAP_NET_RAW is required to use SO_BINDTODEVICE";
interface_name_ = "eth1";
auto interface_names = GetInterfaceNames();
if (interface_names.find(interface_name_) == interface_names.end()) {
// Need a tunnel.
tunnel_ = ASSERT_NO_ERRNO_AND_VALUE(Tunnel::New());
interface_name_ = tunnel_->GetName();
ASSERT_FALSE(interface_name_.empty());
}
socket_ = ASSERT_NO_ERRNO_AND_VALUE(GetParam().Create());
}
string interface_name() const { return interface_name_; }
int socket_fd() const { return socket_->get(); }
private:
std::unique_ptr<Tunnel> tunnel_;
string interface_name_;
std::unique_ptr<FileDescriptor> socket_;
};
constexpr char kIllegalIfnameChar = '/';
// Tests getsockopt of the default value.
TEST_P(BindToDeviceTest, GetsockoptDefault) {
char name_buffer[IFNAMSIZ * 2];
char original_name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
// Read the default SO_BINDTODEVICE.
memset(original_name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
for (size_t i = 0; i <= sizeof(name_buffer); i++) {
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = i;
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE,
name_buffer, &name_buffer_size),
SyscallSucceedsWithValue(0));
EXPECT_EQ(name_buffer_size, 0);
EXPECT_EQ(memcmp(name_buffer, original_name_buffer, sizeof(name_buffer)),
0);
}
}
// Tests setsockopt of invalid device name.
TEST_P(BindToDeviceTest, SetsockoptInvalidDeviceName) {
char name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
// Set an invalid device name.
memset(name_buffer, kIllegalIfnameChar, 5);
name_buffer_size = 5;
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
name_buffer_size),
SyscallFailsWithErrno(ENODEV));
}
// Tests setsockopt of a buffer with a valid device name but not
// null-terminated, with different sizes of buffer.
TEST_P(BindToDeviceTest, SetsockoptValidDeviceNameWithoutNullTermination) {
char name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
strncpy(name_buffer, interface_name().c_str(), interface_name().size() + 1);
// Intentionally overwrite the null at the end.
memset(name_buffer + interface_name().size(), kIllegalIfnameChar,
sizeof(name_buffer) - interface_name().size());
for (size_t i = 1; i <= sizeof(name_buffer); i++) {
name_buffer_size = i;
SCOPED_TRACE(absl::StrCat("Buffer size: ", i));
// It should only work if the size provided is exactly right.
if (name_buffer_size == interface_name().size()) {
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE,
name_buffer, name_buffer_size),
SyscallSucceeds());
} else {
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE,
name_buffer, name_buffer_size),
SyscallFailsWithErrno(ENODEV));
}
}
}
// Tests setsockopt of a buffer with a valid device name and null-terminated,
// with different sizes of buffer.
TEST_P(BindToDeviceTest, SetsockoptValidDeviceNameWithNullTermination) {
char name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
strncpy(name_buffer, interface_name().c_str(), interface_name().size() + 1);
// Don't overwrite the null at the end.
memset(name_buffer + interface_name().size() + 1, kIllegalIfnameChar,
sizeof(name_buffer) - interface_name().size() - 1);
for (size_t i = 1; i <= sizeof(name_buffer); i++) {
name_buffer_size = i;
SCOPED_TRACE(absl::StrCat("Buffer size: ", i));
// It should only work if the size provided is at least the right size.
if (name_buffer_size >= interface_name().size()) {
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE,
name_buffer, name_buffer_size),
SyscallSucceeds());
} else {
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE,
name_buffer, name_buffer_size),
SyscallFailsWithErrno(ENODEV));
}
}
}
// Tests that setsockopt of an invalid device name doesn't unset the previous
// valid setsockopt.
TEST_P(BindToDeviceTest, SetsockoptValidThenInvalid) {
char name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
// Write successfully.
strncpy(name_buffer, interface_name().c_str(), sizeof(name_buffer));
ASSERT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
sizeof(name_buffer)),
SyscallSucceeds());
// Read it back successfully.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = sizeof(name_buffer);
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
&name_buffer_size),
SyscallSucceeds());
EXPECT_EQ(name_buffer_size, interface_name().size() + 1);
EXPECT_STREQ(name_buffer, interface_name().c_str());
// Write unsuccessfully.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = 5;
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
sizeof(name_buffer)),
SyscallFailsWithErrno(ENODEV));
// Read it back successfully, it's unchanged.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = sizeof(name_buffer);
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
&name_buffer_size),
SyscallSucceeds());
EXPECT_EQ(name_buffer_size, interface_name().size() + 1);
EXPECT_STREQ(name_buffer, interface_name().c_str());
}
// Tests that setsockopt of zero-length string correctly unsets the previous
// value.
TEST_P(BindToDeviceTest, SetsockoptValidThenClear) {
char name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
// Write successfully.
strncpy(name_buffer, interface_name().c_str(), sizeof(name_buffer));
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
sizeof(name_buffer)),
SyscallSucceeds());
// Read it back successfully.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = sizeof(name_buffer);
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
&name_buffer_size),
SyscallSucceeds());
EXPECT_EQ(name_buffer_size, interface_name().size() + 1);
EXPECT_STREQ(name_buffer, interface_name().c_str());
// Clear it successfully.
name_buffer_size = 0;
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
name_buffer_size),
SyscallSucceeds());
// Read it back successfully, it's cleared.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = sizeof(name_buffer);
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
&name_buffer_size),
SyscallSucceeds());
EXPECT_EQ(name_buffer_size, 0);
}
// Tests that setsockopt of empty string correctly unsets the previous
// value.
TEST_P(BindToDeviceTest, SetsockoptValidThenClearWithNull) {
char name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
// Write successfully.
strncpy(name_buffer, interface_name().c_str(), sizeof(name_buffer));
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
sizeof(name_buffer)),
SyscallSucceeds());
// Read it back successfully.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = sizeof(name_buffer);
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
&name_buffer_size),
SyscallSucceeds());
EXPECT_EQ(name_buffer_size, interface_name().size() + 1);
EXPECT_STREQ(name_buffer, interface_name().c_str());
// Clear it successfully.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer[0] = 0;
name_buffer_size = sizeof(name_buffer);
EXPECT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
name_buffer_size),
SyscallSucceeds());
// Read it back successfully, it's cleared.
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = sizeof(name_buffer);
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
&name_buffer_size),
SyscallSucceeds());
EXPECT_EQ(name_buffer_size, 0);
}
// Tests getsockopt with different buffer sizes.
TEST_P(BindToDeviceTest, GetsockoptDevice) {
char name_buffer[IFNAMSIZ * 2];
socklen_t name_buffer_size;
// Write successfully.
strncpy(name_buffer, interface_name().c_str(), sizeof(name_buffer));
ASSERT_THAT(setsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE, name_buffer,
sizeof(name_buffer)),
SyscallSucceeds());
// Read it back at various buffer sizes.
for (size_t i = 0; i <= sizeof(name_buffer); i++) {
memset(name_buffer, kIllegalIfnameChar, sizeof(name_buffer));
name_buffer_size = i;
SCOPED_TRACE(absl::StrCat("Buffer size: ", i));
// Linux only allows a buffer at least IFNAMSIZ, even if less would suffice
// for this interface name.
if (name_buffer_size >= IFNAMSIZ) {
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE,
name_buffer, &name_buffer_size),
SyscallSucceeds());
EXPECT_EQ(name_buffer_size, interface_name().size() + 1);
EXPECT_STREQ(name_buffer, interface_name().c_str());
} else {
EXPECT_THAT(getsockopt(socket_fd(), SOL_SOCKET, SO_BINDTODEVICE,
name_buffer, &name_buffer_size),
SyscallFailsWithErrno(EINVAL));
EXPECT_EQ(name_buffer_size, i);
}
}
}
INSTANTIATE_TEST_SUITE_P(BindToDeviceTest, BindToDeviceTest,
::testing::Values(IPv4UDPUnboundSocket(0),
IPv4TCPUnboundSocket(0)));
} // namespace testing
} // namespace gvisor
|