summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/proc_net_tcp.cc
blob: 5b6e3e3cddca917817f1ec3e95a44da924c9614a (plain)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright 2019 Google LLC
//
// 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 <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "gtest/gtest.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "test/syscalls/linux/ip_socket_test_util.h"
#include "test/util/file_descriptor.h"
#include "test/util/test_util.h"

namespace gvisor {
namespace testing {
namespace {

using absl::StrCat;
using absl::StrSplit;

constexpr char kProcNetTCPHeader[] =
    "  sl  local_address rem_address   st tx_queue rx_queue tr tm->when "
    "retrnsmt   uid  timeout inode                                             "
    "        ";

// TCPEntry represents a single entry from /proc/net/tcp.
struct TCPEntry {
  uint32_t local_addr;
  uint16_t local_port;

  uint32_t remote_addr;
  uint16_t remote_port;

  uint64_t state;
  uint64_t uid;
  uint64_t inode;
};

// Finds the first entry in 'entries' for which 'predicate' returns true.
// Returns true on match, and sets 'match' to a copy of the matching entry. If
// 'match' is null, it's ignored.
bool FindBy(const std::vector<TCPEntry>& entries, TCPEntry* match,
            std::function<bool(const TCPEntry&)> predicate) {
  for (const TCPEntry& entry : entries) {
    if (predicate(entry)) {
      if (match != nullptr) {
        *match = entry;
      }
      return true;
    }
  }
  return false;
}

bool FindByLocalAddr(const std::vector<TCPEntry>& entries, TCPEntry* match,
                     const struct sockaddr* addr) {
  uint32_t host = IPFromInetSockaddr(addr);
  uint16_t port = PortFromInetSockaddr(addr);
  return FindBy(entries, match, [host, port](const TCPEntry& e) {
    return (e.local_addr == host && e.local_port == port);
  });
}

bool FindByRemoteAddr(const std::vector<TCPEntry>& entries, TCPEntry* match,
                      const struct sockaddr* addr) {
  uint32_t host = IPFromInetSockaddr(addr);
  uint16_t port = PortFromInetSockaddr(addr);
  return FindBy(entries, match, [host, port](const TCPEntry& e) {
    return (e.remote_addr == host && e.remote_port == port);
  });
}

// Returns a parsed representation of /proc/net/tcp entries.
PosixErrorOr<std::vector<TCPEntry>> ProcNetTCPEntries() {
  std::string content;
  RETURN_IF_ERRNO(GetContents("/proc/net/tcp", &content));

  bool found_header = false;
  std::vector<TCPEntry> entries;
  std::vector<std::string> lines = StrSplit(content, '\n');
  std::cerr << "<contents of /proc/net/tcp>" << std::endl;
  for (const std::string& line : lines) {
    std::cerr << line << std::endl;

    if (!found_header) {
      EXPECT_EQ(line, kProcNetTCPHeader);
      found_header = true;
      continue;
    }
    if (line.empty()) {
      continue;
    }

    // Parse a single entry from /proc/net/tcp.
    //
    // Example entries:
    //
    // clang-format off
    //
    //  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
    //   0: 00000000:006F 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 1968 1 0000000000000000 100 0 0 10 0
    //   1: 0100007F:7533 00000000:0000 0A 00000000:00000000 00:00000000 00000000   120        0 10684 1 0000000000000000 100 0 0 10 0
    //   ^     ^       ^     ^       ^   ^     ^       ^      ^     ^        ^       ^         ^   ^   ^      ^            ^  ^ ^  ^ ^
    //   0     1       2     3       4   5     6       7      8     9       10       11       12  13  14     15           16 17 18 19 20
    //
    // clang-format on

    TCPEntry entry;
    std::vector<std::string> fields =
        StrSplit(line, absl::ByAnyChar(": "), absl::SkipEmpty());

    ASSIGN_OR_RETURN_ERRNO(entry.local_addr, AtoiBase(fields[1], 16));
    ASSIGN_OR_RETURN_ERRNO(entry.local_port, AtoiBase(fields[2], 16));

    ASSIGN_OR_RETURN_ERRNO(entry.remote_addr, AtoiBase(fields[3], 16));
    ASSIGN_OR_RETURN_ERRNO(entry.remote_port, AtoiBase(fields[4], 16));

    ASSIGN_OR_RETURN_ERRNO(entry.state, AtoiBase(fields[5], 16));
    ASSIGN_OR_RETURN_ERRNO(entry.uid, Atoi<uint64_t>(fields[11]));
    ASSIGN_OR_RETURN_ERRNO(entry.inode, Atoi<uint64_t>(fields[13]));

    entries.push_back(entry);
  }
  std::cerr << "<end of /proc/net/tcp>" << std::endl;

  return entries;
}

TEST(ProcNetTCP, Exists) {
  const std::string content =
      ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/net/tcp"));
  const std::string header_line = StrCat(kProcNetTCPHeader, "\n");
  if (IsRunningOnGvisor()) {
    // Should be just the header since we don't have any tcp sockets yet.
    EXPECT_EQ(content, header_line);
  } else {
    // On a general linux machine, we could have abitrary sockets on the system,
    // so just check the header.
    EXPECT_THAT(content, ::testing::StartsWith(header_line));
  }
}

TEST(ProcNetTCP, EntryUID) {
  auto sockets =
      ASSERT_NO_ERRNO_AND_VALUE(IPv4TCPAcceptBindSocketPair(0).Create());
  std::vector<TCPEntry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCPEntries());
  TCPEntry e;
  ASSERT_TRUE(FindByLocalAddr(entries, &e, sockets->first_addr()));
  EXPECT_EQ(e.uid, geteuid());
  ASSERT_TRUE(FindByRemoteAddr(entries, &e, sockets->first_addr()));
  EXPECT_EQ(e.uid, geteuid());
}

TEST(ProcNetTCP, BindAcceptConnect) {
  auto sockets =
      ASSERT_NO_ERRNO_AND_VALUE(IPv4TCPAcceptBindSocketPair(0).Create());
  std::vector<TCPEntry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCPEntries());
  // We can only make assertions about the total number of entries if we control
  // the entire "machine".
  if (IsRunningOnGvisor()) {
    EXPECT_EQ(entries.size(), 2);
  }

  EXPECT_TRUE(FindByLocalAddr(entries, nullptr, sockets->first_addr()));
  EXPECT_TRUE(FindByRemoteAddr(entries, nullptr, sockets->first_addr()));
}

TEST(ProcNetTCP, InodeReasonable) {
  auto sockets =
      ASSERT_NO_ERRNO_AND_VALUE(IPv4TCPAcceptBindSocketPair(0).Create());
  std::vector<TCPEntry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCPEntries());

  TCPEntry accepted_entry;
  ASSERT_TRUE(FindByLocalAddr(entries, &accepted_entry, sockets->first_addr()));
  EXPECT_NE(accepted_entry.inode, 0);

  TCPEntry client_entry;
  ASSERT_TRUE(FindByRemoteAddr(entries, &client_entry, sockets->first_addr()));
  EXPECT_NE(client_entry.inode, 0);
  EXPECT_NE(accepted_entry.inode, client_entry.inode);
}

TEST(ProcNetTCP, State) {
  std::unique_ptr<FileDescriptor> server =
      ASSERT_NO_ERRNO_AND_VALUE(IPv4TCPUnboundSocket(0).Create());

  auto test_addr = V4Loopback();
  ASSERT_THAT(
      bind(server->get(), reinterpret_cast<struct sockaddr*>(&test_addr.addr),
           test_addr.addr_len),
      SyscallSucceeds());

  struct sockaddr addr;
  socklen_t addrlen = sizeof(struct sockaddr);
  ASSERT_THAT(getsockname(server->get(), &addr, &addrlen), SyscallSucceeds());
  ASSERT_EQ(addrlen, sizeof(struct sockaddr));

  ASSERT_THAT(listen(server->get(), 10), SyscallSucceeds());
  std::vector<TCPEntry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCPEntries());
  TCPEntry listen_entry;
  ASSERT_TRUE(FindByLocalAddr(entries, &listen_entry, &addr));
  EXPECT_EQ(listen_entry.state, TCP_LISTEN);

  std::unique_ptr<FileDescriptor> client =
      ASSERT_NO_ERRNO_AND_VALUE(IPv4TCPUnboundSocket(0).Create());
  ASSERT_THAT(RetryEINTR(connect)(client->get(), &addr, addrlen),
              SyscallSucceeds());
  entries = ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCPEntries());
  ASSERT_TRUE(FindByLocalAddr(entries, &listen_entry, &addr));
  EXPECT_EQ(listen_entry.state, TCP_LISTEN);
  TCPEntry client_entry;
  ASSERT_TRUE(FindByRemoteAddr(entries, &client_entry, &addr));
  EXPECT_EQ(client_entry.state, TCP_ESTABLISHED);

  FileDescriptor accepted =
      ASSERT_NO_ERRNO_AND_VALUE(Accept(server->get(), nullptr, nullptr));

  const uint32_t accepted_local_host = IPFromInetSockaddr(&addr);
  const uint16_t accepted_local_port = PortFromInetSockaddr(&addr);

  entries = ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCPEntries());
  TCPEntry accepted_entry;
  ASSERT_TRUE(FindBy(entries, &accepted_entry,
                     [client_entry, accepted_local_host,
                      accepted_local_port](const TCPEntry& e) {
                       return e.local_addr == accepted_local_host &&
                              e.local_port == accepted_local_port &&
                              e.remote_addr == client_entry.local_addr &&
                              e.remote_port == client_entry.local_port;
                     }));
  EXPECT_EQ(accepted_entry.state, TCP_ESTABLISHED);
}

constexpr char kProcNetTCP6Header[] =
    "  sl  local_address                         remote_address"
    "                        st tx_queue rx_queue tr tm->when retrnsmt"
    "   uid  timeout inode";

// TCP6Entry represents a single entry from /proc/net/tcp6.
struct TCP6Entry {
  struct in6_addr local_addr;
  uint16_t local_port;

  struct in6_addr remote_addr;
  uint16_t remote_port;

  uint64_t state;
  uint64_t uid;
  uint64_t inode;
};

bool IPv6AddrEqual(const struct in6_addr* a1, const struct in6_addr* a2) {
  return memcmp(a1, a2, sizeof(struct in6_addr)) == 0;
}

// Finds the first entry in 'entries' for which 'predicate' returns true.
// Returns true on match, and sets 'match' to a copy of the matching entry. If
// 'match' is null, it's ignored.
bool FindBy6(const std::vector<TCP6Entry>& entries, TCP6Entry* match,
             std::function<bool(const TCP6Entry&)> predicate) {
  for (const TCP6Entry& entry : entries) {
    if (predicate(entry)) {
      if (match != nullptr) {
        *match = entry;
      }
      return true;
    }
  }
  return false;
}

const struct in6_addr* IP6FromInetSockaddr(const struct sockaddr* addr) {
  auto* addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr);
  return &addr6->sin6_addr;
}

bool FindByLocalAddr6(const std::vector<TCP6Entry>& entries, TCP6Entry* match,
                      const struct sockaddr* addr) {
  const struct in6_addr* local = IP6FromInetSockaddr(addr);
  uint16_t port = PortFromInetSockaddr(addr);
  return FindBy6(entries, match, [local, port](const TCP6Entry& e) {
    return (IPv6AddrEqual(&e.local_addr, local) && e.local_port == port);
  });
}

bool FindByRemoteAddr6(const std::vector<TCP6Entry>& entries, TCP6Entry* match,
                       const struct sockaddr* addr) {
  const struct in6_addr* remote = IP6FromInetSockaddr(addr);
  uint16_t port = PortFromInetSockaddr(addr);
  return FindBy6(entries, match, [remote, port](const TCP6Entry& e) {
    return (IPv6AddrEqual(&e.remote_addr, remote) && e.remote_port == port);
  });
}

void ReadIPv6Address(std::string s, struct in6_addr* addr) {
  uint32_t a0, a1, a2, a3;
  const char* fmt = "%08X%08X%08X%08X";
  EXPECT_EQ(sscanf(s.c_str(), fmt, &a0, &a1, &a2, &a3), 4);

  uint8_t* b = addr->s6_addr;
  *((uint32_t*)&b[0]) = a0;
  *((uint32_t*)&b[4]) = a1;
  *((uint32_t*)&b[8]) = a2;
  *((uint32_t*)&b[12]) = a3;
}

// Returns a parsed representation of /proc/net/tcp6 entries.
PosixErrorOr<std::vector<TCP6Entry>> ProcNetTCP6Entries() {
  std::string content;
  RETURN_IF_ERRNO(GetContents("/proc/net/tcp6", &content));

  bool found_header = false;
  std::vector<TCP6Entry> entries;
  std::vector<std::string> lines = StrSplit(content, '\n');
  std::cerr << "<contents of /proc/net/tcp6>" << std::endl;
  for (const std::string& line : lines) {
    std::cerr << line << std::endl;

    if (!found_header) {
      EXPECT_EQ(line, kProcNetTCP6Header);
      found_header = true;
      continue;
    }
    if (line.empty()) {
      continue;
    }

    // Parse a single entry from /proc/net/tcp6.
    //
    // Example entries:
    //
    // clang-format off
    //
    //  sl  local_address                         remote_address                        st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
    //   0: 00000000000000000000000000000000:1F90 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 876340 1 ffff8803da9c9380 100 0 0 10 0
    //   1: 00000000000000000000000000000000:C350 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 876987 1 ffff8803ec408000 100 0 0 10 0
    //   ^                  ^                  ^                  ^                  ^   ^     ^       ^      ^     ^        ^       ^         ^    ^   ^      ^            ^  ^ ^  ^ ^
    //   0                  1                  2                  3                  4   5     6       7      8     9       10       11       12   13  14     15           16 17 18 19 20
    //
    // clang-format on

    TCP6Entry entry;
    std::vector<std::string> fields =
        StrSplit(line, absl::ByAnyChar(": "), absl::SkipEmpty());

    ReadIPv6Address(fields[1], &entry.local_addr);
    ASSIGN_OR_RETURN_ERRNO(entry.local_port, AtoiBase(fields[2], 16));
    ReadIPv6Address(fields[3], &entry.remote_addr);
    ASSIGN_OR_RETURN_ERRNO(entry.remote_port, AtoiBase(fields[4], 16));
    ASSIGN_OR_RETURN_ERRNO(entry.state, AtoiBase(fields[5], 16));
    ASSIGN_OR_RETURN_ERRNO(entry.uid, Atoi<uint64_t>(fields[11]));
    ASSIGN_OR_RETURN_ERRNO(entry.inode, Atoi<uint64_t>(fields[13]));

    entries.push_back(entry);
  }
  std::cerr << "<end of /proc/net/tcp6>" << std::endl;

  return entries;
}

TEST(ProcNetTCP6, Exists) {
  const std::string content =
      ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/net/tcp6"));
  const std::string header_line = StrCat(kProcNetTCP6Header, "\n");
  if (IsRunningOnGvisor()) {
    // Should be just the header since we don't have any tcp sockets yet.
    EXPECT_EQ(content, header_line);
  } else {
    // On a general linux machine, we could have abitrary sockets on the system,
    // so just check the header.
    EXPECT_THAT(content, ::testing::StartsWith(header_line));
  }
}

TEST(ProcNetTCP6, EntryUID) {
  auto sockets =
      ASSERT_NO_ERRNO_AND_VALUE(IPv6TCPAcceptBindSocketPair(0).Create());
  std::vector<TCP6Entry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCP6Entries());
  TCP6Entry e;

  ASSERT_TRUE(FindByLocalAddr6(entries, &e, sockets->first_addr()));
  EXPECT_EQ(e.uid, geteuid());
  ASSERT_TRUE(FindByRemoteAddr6(entries, &e, sockets->first_addr()));
  EXPECT_EQ(e.uid, geteuid());
}

TEST(ProcNetTCP6, BindAcceptConnect) {
  auto sockets =
      ASSERT_NO_ERRNO_AND_VALUE(IPv6TCPAcceptBindSocketPair(0).Create());
  std::vector<TCP6Entry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCP6Entries());
  // We can only make assertions about the total number of entries if we control
  // the entire "machine".
  if (IsRunningOnGvisor()) {
    EXPECT_EQ(entries.size(), 2);
  }

  EXPECT_TRUE(FindByLocalAddr6(entries, nullptr, sockets->first_addr()));
  EXPECT_TRUE(FindByRemoteAddr6(entries, nullptr, sockets->first_addr()));
}

TEST(ProcNetTCP6, InodeReasonable) {
  auto sockets =
      ASSERT_NO_ERRNO_AND_VALUE(IPv6TCPAcceptBindSocketPair(0).Create());
  std::vector<TCP6Entry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCP6Entries());

  TCP6Entry accepted_entry;

  ASSERT_TRUE(
      FindByLocalAddr6(entries, &accepted_entry, sockets->first_addr()));
  EXPECT_NE(accepted_entry.inode, 0);

  TCP6Entry client_entry;
  ASSERT_TRUE(FindByRemoteAddr6(entries, &client_entry, sockets->first_addr()));
  EXPECT_NE(client_entry.inode, 0);
  EXPECT_NE(accepted_entry.inode, client_entry.inode);
}

TEST(ProcNetTCP6, State) {
  std::unique_ptr<FileDescriptor> server =
      ASSERT_NO_ERRNO_AND_VALUE(IPv6TCPUnboundSocket(0).Create());

  auto test_addr = V6Loopback();
  ASSERT_THAT(
      bind(server->get(), reinterpret_cast<struct sockaddr*>(&test_addr.addr),
           test_addr.addr_len),
      SyscallSucceeds());

  struct sockaddr_in6 addr6;
  socklen_t addrlen = sizeof(struct sockaddr_in6);
  auto* addr = reinterpret_cast<struct sockaddr*>(&addr6);
  ASSERT_THAT(getsockname(server->get(), addr, &addrlen), SyscallSucceeds());
  ASSERT_EQ(addrlen, sizeof(struct sockaddr_in6));

  ASSERT_THAT(listen(server->get(), 10), SyscallSucceeds());
  std::vector<TCP6Entry> entries =
      ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCP6Entries());
  TCP6Entry listen_entry;

  ASSERT_TRUE(FindByLocalAddr6(entries, &listen_entry, addr));
  EXPECT_EQ(listen_entry.state, TCP_LISTEN);

  std::unique_ptr<FileDescriptor> client =
      ASSERT_NO_ERRNO_AND_VALUE(IPv6TCPUnboundSocket(0).Create());
  ASSERT_THAT(RetryEINTR(connect)(client->get(), addr, addrlen),
              SyscallSucceeds());
  entries = ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCP6Entries());
  ASSERT_TRUE(FindByLocalAddr6(entries, &listen_entry, addr));
  EXPECT_EQ(listen_entry.state, TCP_LISTEN);
  TCP6Entry client_entry;
  ASSERT_TRUE(FindByRemoteAddr6(entries, &client_entry, addr));
  EXPECT_EQ(client_entry.state, TCP_ESTABLISHED);

  FileDescriptor accepted =
      ASSERT_NO_ERRNO_AND_VALUE(Accept(server->get(), nullptr, nullptr));

  const struct in6_addr* local = IP6FromInetSockaddr(addr);
  const uint16_t accepted_local_port = PortFromInetSockaddr(addr);

  entries = ASSERT_NO_ERRNO_AND_VALUE(ProcNetTCP6Entries());
  TCP6Entry accepted_entry;
  ASSERT_TRUE(FindBy6(
      entries, &accepted_entry,
      [client_entry, local, accepted_local_port](const TCP6Entry& e) {
        return IPv6AddrEqual(&e.local_addr, local) &&
               e.local_port == accepted_local_port &&
               IPv6AddrEqual(&e.remote_addr, &client_entry.local_addr) &&
               e.remote_port == client_entry.local_port;
      }));
  EXPECT_EQ(accepted_entry.state, TCP_ESTABLISHED);
}

}  // namespace
}  // namespace testing
}  // namespace gvisor