-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathrestrict_bind_loopback.cc
More file actions
181 lines (157 loc) · 6.05 KB
/
Copy pathrestrict_bind_loopback.cc
File metadata and controls
181 lines (157 loc) · 6.05 KB
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
// Copyright 2025 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.
// Tests for the --restrict-bind-to-loopback runsc flag. When the flag is
// enabled, bind(2) must return EACCES for any address that is not a loopback
// address (127.x.x.x or ::1). These tests skip automatically when the flag is
// not active.
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "gtest/gtest.h"
#include "test/util/file_descriptor.h"
#include "test/util/socket_util.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
// Detect whether --restrict-bind-to-loopback is active by probing a bind to
// INADDR_ANY. On a normal kernel or a gVisor instance without the flag, this
// bind will succeed (or fail with something other than EACCES).
bool RestrictBindToLoopbackEnabled() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) return false;
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
int ret =
bind(sock, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr));
int saved_errno = errno;
close(sock);
return ret < 0 && saved_errno == EACCES;
}
// IPv4: binding to INADDR_ANY (0.0.0.0) must fail with EACCES.
TEST(RestrictBindLoopback, IPv4_INADDR_ANY_Rejected) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, 0));
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallFailsWithErrno(EACCES));
}
// IPv4: binding to a non-loopback unicast address must fail with EACCES.
TEST(RestrictBindLoopback, IPv4_NonLoopback_Rejected) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, 0));
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
inet_pton(AF_INET, "192.0.2.1", &addr.sin_addr); // TEST-NET-1, RFC 5737
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallFailsWithErrno(EACCES));
}
// IPv4: binding to 127.0.0.1 must succeed.
TEST(RestrictBindLoopback, IPv4_Loopback_Allowed) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, 0));
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = 0;
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallSucceeds());
}
// IPv4: binding to 127.x.x.x (any loopback block address) must succeed.
TEST(RestrictBindLoopback, IPv4_LoopbackBlock_Allowed) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, 0));
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = 0;
inet_pton(AF_INET, "127.0.0.2", &addr.sin_addr);
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallSucceeds());
}
// IPv6: binding to IN6ADDR_ANY (::) must fail with EACCES.
TEST(RestrictBindLoopback, IPv6_IN6ADDR_ANY_Rejected) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET6, SOCK_STREAM, 0));
struct sockaddr_in6 addr = {};
addr.sin6_family = AF_INET6;
addr.sin6_addr = in6addr_any;
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallFailsWithErrno(EACCES));
}
// IPv6: binding to ::1 must succeed.
TEST(RestrictBindLoopback, IPv6_Loopback_Allowed) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET6, SOCK_STREAM, 0));
struct sockaddr_in6 addr = {};
addr.sin6_family = AF_INET6;
addr.sin6_port = 0;
addr.sin6_addr = in6addr_loopback;
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallSucceeds());
}
// IPv6: binding to ::ffff:127.0.0.1 (IPv4-mapped loopback) must succeed.
// Dual-stack sockets can bind to IPv4 addresses in mapped form; rejecting this
// would break applications that use IPv6 sockets for both IPv4 and IPv6.
TEST(RestrictBindLoopback, IPv6_IPv4MappedLoopback_Allowed) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET6, SOCK_STREAM, 0));
struct sockaddr_in6 addr = {};
addr.sin6_family = AF_INET6;
addr.sin6_port = 0;
// ::ffff:127.0.0.1
inet_pton(AF_INET6, "::ffff:127.0.0.1", &addr.sin6_addr);
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallSucceeds());
}
// IPv6: binding to ::ffff:0.0.0.0 (IPv4-mapped INADDR_ANY) must fail.
TEST(RestrictBindLoopback, IPv6_IPv4MappedAny_Rejected) {
SKIP_IF(!RestrictBindToLoopbackEnabled());
FileDescriptor sock =
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET6, SOCK_STREAM, 0));
struct sockaddr_in6 addr = {};
addr.sin6_family = AF_INET6;
// ::ffff:0.0.0.0
inet_pton(AF_INET6, "::ffff:0.0.0.0", &addr.sin6_addr);
EXPECT_THAT(
bind(sock.get(), reinterpret_cast<const struct sockaddr*>(&addr),
sizeof(addr)),
SyscallFailsWithErrno(EACCES));
}
} // namespace
} // namespace testing
} // namespace gvisor