summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/unix_domain_socket_test_util.cc
blob: 7fb9eed8d864b51c8077150a5f4ec7f073d063d7 (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
// 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 "test/syscalls/linux/unix_domain_socket_test_util.h"

#include <sys/un.h>
#include <vector>

#include "gtest/gtest.h"
#include "absl/strings/str_cat.h"
#include "test/util/test_util.h"

namespace gvisor {
namespace testing {

std::string DescribeUnixDomainSocketType(int type) {
  const char* type_str = nullptr;
  switch (type & ~(SOCK_NONBLOCK | SOCK_CLOEXEC)) {
    case SOCK_STREAM:
      type_str = "SOCK_STREAM";
      break;
    case SOCK_DGRAM:
      type_str = "SOCK_DGRAM";
      break;
    case SOCK_SEQPACKET:
      type_str = "SOCK_SEQPACKET";
      break;
  }
  if (!type_str) {
    return absl::StrCat("Unix domain socket with unknown type ", type);
  } else {
    return absl::StrCat(((type & SOCK_NONBLOCK) != 0) ? "non-blocking " : "",
                        ((type & SOCK_CLOEXEC) != 0) ? "close-on-exec " : "",
                        type_str, " Unix domain socket");
  }
}

SocketPairKind UnixDomainSocketPair(int type) {
  return SocketPairKind{DescribeUnixDomainSocketType(type), AF_UNIX, type, 0,
                        SyscallSocketPairCreator(AF_UNIX, type, 0)};
}

SocketPairKind FilesystemBoundUnixDomainSocketPair(int type) {
  std::string description = absl::StrCat(DescribeUnixDomainSocketType(type),
                                         " created with filesystem binding");
  if ((type & SOCK_DGRAM) == SOCK_DGRAM) {
    return SocketPairKind{
        description, AF_UNIX, type, 0,
        FilesystemBidirectionalBindSocketPairCreator(AF_UNIX, type, 0)};
  }
  return SocketPairKind{
      description, AF_UNIX, type, 0,
      FilesystemAcceptBindSocketPairCreator(AF_UNIX, type, 0)};
}

SocketPairKind AbstractBoundUnixDomainSocketPair(int type) {
  std::string description =
      absl::StrCat(DescribeUnixDomainSocketType(type),
                   " created with abstract namespace binding");
  if ((type & SOCK_DGRAM) == SOCK_DGRAM) {
    return SocketPairKind{
        description, AF_UNIX, type, 0,
        AbstractBidirectionalBindSocketPairCreator(AF_UNIX, type, 0)};
  }
  return SocketPairKind{description, AF_UNIX, type, 0,
                        AbstractAcceptBindSocketPairCreator(AF_UNIX, type, 0)};
}

SocketPairKind SocketpairGoferUnixDomainSocketPair(int type) {
  std::string description = absl::StrCat(DescribeUnixDomainSocketType(type),
                                         " created with the socketpair gofer");
  return SocketPairKind{description, AF_UNIX, type, 0,
                        SocketpairGoferSocketPairCreator(AF_UNIX, type, 0)};
}

SocketPairKind SocketpairGoferFileSocketPair(int type) {
  std::string description =
      absl::StrCat(((type & O_NONBLOCK) != 0) ? "non-blocking " : "",
                   ((type & O_CLOEXEC) != 0) ? "close-on-exec " : "",
                   "file socket created with the socketpair gofer");
  // The socketpair gofer always creates SOCK_STREAM sockets on open(2).
  return SocketPairKind{description, AF_UNIX, SOCK_STREAM, 0,
                        SocketpairGoferFileSocketPairCreator(type)};
}

SocketPairKind FilesystemUnboundUnixDomainSocketPair(int type) {
  return SocketPairKind{absl::StrCat(DescribeUnixDomainSocketType(type),
                                     " unbound with a filesystem address"),
                        AF_UNIX, type, 0,
                        FilesystemUnboundSocketPairCreator(AF_UNIX, type, 0)};
}

SocketPairKind AbstractUnboundUnixDomainSocketPair(int type) {
  return SocketPairKind{
      absl::StrCat(DescribeUnixDomainSocketType(type),
                   " unbound with an abstract namespace address"),
      AF_UNIX, type, 0, AbstractUnboundSocketPairCreator(AF_UNIX, type, 0)};
}

void SendSingleFD(int sock, int fd, char buf[], int buf_size) {
  ASSERT_NO_FATAL_FAILURE(SendFDs(sock, &fd, 1, buf, buf_size));
}

void SendFDs(int sock, int fds[], int fds_size, char buf[], int buf_size) {
  struct msghdr msg = {};
  std::vector<char> control(CMSG_SPACE(fds_size * sizeof(int)));
  msg.msg_control = &control[0];
  msg.msg_controllen = control.size();

  struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_len = CMSG_LEN(fds_size * sizeof(int));
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  for (int i = 0; i < fds_size; i++) {
    memcpy(CMSG_DATA(cmsg) + i * sizeof(int), &fds[i], sizeof(int));
  }

  ASSERT_THAT(SendMsg(sock, &msg, buf, buf_size),
              IsPosixErrorOkAndHolds(buf_size));
}

void RecvSingleFD(int sock, int* fd, char buf[], int buf_size) {
  ASSERT_NO_FATAL_FAILURE(RecvFDs(sock, fd, 1, buf, buf_size, buf_size));
}

void RecvSingleFD(int sock, int* fd, char buf[], int buf_size,
                  int expected_size) {
  ASSERT_NO_FATAL_FAILURE(RecvFDs(sock, fd, 1, buf, buf_size, expected_size));
}

void RecvFDs(int sock, int fds[], int fds_size, char buf[], int buf_size) {
  ASSERT_NO_FATAL_FAILURE(
      RecvFDs(sock, fds, fds_size, buf, buf_size, buf_size));
}

void RecvFDs(int sock, int fds[], int fds_size, char buf[], int buf_size,
             int expected_size, bool peek) {
  struct msghdr msg = {};
  std::vector<char> control(CMSG_SPACE(fds_size * sizeof(int)));
  msg.msg_control = &control[0];
  msg.msg_controllen = control.size();

  struct iovec iov;
  iov.iov_base = buf;
  iov.iov_len = buf_size;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;

  int flags = 0;
  if (peek) {
    flags |= MSG_PEEK;
  }

  ASSERT_THAT(RetryEINTR(recvmsg)(sock, &msg, flags),
              SyscallSucceedsWithValue(expected_size));
  struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  ASSERT_NE(cmsg, nullptr);
  ASSERT_EQ(cmsg->cmsg_len, CMSG_LEN(fds_size * sizeof(int)));
  ASSERT_EQ(cmsg->cmsg_level, SOL_SOCKET);
  ASSERT_EQ(cmsg->cmsg_type, SCM_RIGHTS);

  for (int i = 0; i < fds_size; i++) {
    memcpy(&fds[i], CMSG_DATA(cmsg) + i * sizeof(int), sizeof(int));
  }
}

void RecvFDs(int sock, int fds[], int fds_size, char buf[], int buf_size,
             int expected_size) {
  ASSERT_NO_FATAL_FAILURE(
      RecvFDs(sock, fds, fds_size, buf, buf_size, expected_size, false));
}

void PeekSingleFD(int sock, int* fd, char buf[], int buf_size) {
  ASSERT_NO_FATAL_FAILURE(RecvFDs(sock, fd, 1, buf, buf_size, buf_size, true));
}

void RecvNoCmsg(int sock, char buf[], int buf_size, int expected_size) {
  struct msghdr msg = {};
  char control[CMSG_SPACE(sizeof(int)) + CMSG_SPACE(sizeof(struct ucred))];
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);

  struct iovec iov;
  iov.iov_base = buf;
  iov.iov_len = buf_size;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;

  ASSERT_THAT(RetryEINTR(recvmsg)(sock, &msg, 0),
              SyscallSucceedsWithValue(expected_size));
  struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  EXPECT_EQ(cmsg, nullptr);
}

void SendNullCmsg(int sock, char buf[], int buf_size) {
  struct msghdr msg = {};
  msg.msg_control = nullptr;
  msg.msg_controllen = 0;

  ASSERT_THAT(SendMsg(sock, &msg, buf, buf_size),
              IsPosixErrorOkAndHolds(buf_size));
}

void SendCreds(int sock, ucred creds, char buf[], int buf_size) {
  struct msghdr msg = {};

  char control[CMSG_SPACE(sizeof(struct ucred))];
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);

  struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_CREDENTIALS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
  memcpy(CMSG_DATA(cmsg), &creds, sizeof(struct ucred));

  ASSERT_THAT(SendMsg(sock, &msg, buf, buf_size),
              IsPosixErrorOkAndHolds(buf_size));
}

void SendCredsAndFD(int sock, ucred creds, int fd, char buf[], int buf_size) {
  struct msghdr msg = {};

  char control[CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int))] = {};
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);

  struct cmsghdr* cmsg1 = CMSG_FIRSTHDR(&msg);
  cmsg1->cmsg_level = SOL_SOCKET;
  cmsg1->cmsg_type = SCM_CREDENTIALS;
  cmsg1->cmsg_len = CMSG_LEN(sizeof(struct ucred));
  memcpy(CMSG_DATA(cmsg1), &creds, sizeof(struct ucred));

  struct cmsghdr* cmsg2 = CMSG_NXTHDR(&msg, cmsg1);
  cmsg2->cmsg_level = SOL_SOCKET;
  cmsg2->cmsg_type = SCM_RIGHTS;
  cmsg2->cmsg_len = CMSG_LEN(sizeof(int));
  memcpy(CMSG_DATA(cmsg2), &fd, sizeof(int));

  ASSERT_THAT(SendMsg(sock, &msg, buf, buf_size),
              IsPosixErrorOkAndHolds(buf_size));
}

void RecvCreds(int sock, ucred* creds, char buf[], int buf_size) {
  ASSERT_NO_FATAL_FAILURE(RecvCreds(sock, creds, buf, buf_size, buf_size));
}

void RecvCreds(int sock, ucred* creds, char buf[], int buf_size,
               int expected_size) {
  struct msghdr msg = {};
  char control[CMSG_SPACE(sizeof(struct ucred))];
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);

  struct iovec iov;
  iov.iov_base = buf;
  iov.iov_len = buf_size;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;

  ASSERT_THAT(RetryEINTR(recvmsg)(sock, &msg, 0),
              SyscallSucceedsWithValue(expected_size));
  struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  ASSERT_NE(cmsg, nullptr);
  ASSERT_EQ(cmsg->cmsg_len, CMSG_LEN(sizeof(struct ucred)));
  ASSERT_EQ(cmsg->cmsg_level, SOL_SOCKET);
  ASSERT_EQ(cmsg->cmsg_type, SCM_CREDENTIALS);

  memcpy(creds, CMSG_DATA(cmsg), sizeof(struct ucred));
}

void RecvCredsAndFD(int sock, ucred* creds, int* fd, char buf[], int buf_size) {
  struct msghdr msg = {};
  char control[CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int))];
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);

  struct iovec iov;
  iov.iov_base = buf;
  iov.iov_len = buf_size;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;

  ASSERT_THAT(RetryEINTR(recvmsg)(sock, &msg, 0),
              SyscallSucceedsWithValue(buf_size));

  struct cmsghdr* cmsg1 = CMSG_FIRSTHDR(&msg);
  ASSERT_NE(cmsg1, nullptr);
  ASSERT_EQ(cmsg1->cmsg_len, CMSG_LEN(sizeof(struct ucred)));
  ASSERT_EQ(cmsg1->cmsg_level, SOL_SOCKET);
  ASSERT_EQ(cmsg1->cmsg_type, SCM_CREDENTIALS);
  memcpy(creds, CMSG_DATA(cmsg1), sizeof(struct ucred));

  struct cmsghdr* cmsg2 = CMSG_NXTHDR(&msg, cmsg1);
  ASSERT_NE(cmsg2, nullptr);
  ASSERT_EQ(cmsg2->cmsg_len, CMSG_LEN(sizeof(int)));
  ASSERT_EQ(cmsg2->cmsg_level, SOL_SOCKET);
  ASSERT_EQ(cmsg2->cmsg_type, SCM_RIGHTS);
  memcpy(fd, CMSG_DATA(cmsg2), sizeof(int));
}

void RecvSingleFDUnaligned(int sock, int* fd, char buf[], int buf_size) {
  struct msghdr msg = {};
  char control[CMSG_SPACE(sizeof(int)) - sizeof(int)];
  msg.msg_control = control;
  msg.msg_controllen = sizeof(control);

  struct iovec iov;
  iov.iov_base = buf;
  iov.iov_len = buf_size;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;

  ASSERT_THAT(RetryEINTR(recvmsg)(sock, &msg, 0),
              SyscallSucceedsWithValue(buf_size));

  struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  ASSERT_NE(cmsg, nullptr);
  ASSERT_EQ(cmsg->cmsg_len, CMSG_LEN(sizeof(int)));
  ASSERT_EQ(cmsg->cmsg_level, SOL_SOCKET);
  ASSERT_EQ(cmsg->cmsg_type, SCM_RIGHTS);

  memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
}

void SetSoPassCred(int sock) {
  int one = 1;
  EXPECT_THAT(setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)),
              SyscallSucceeds());
}

void UnsetSoPassCred(int sock) {
  int zero = 0;
  EXPECT_THAT(setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &zero, sizeof(zero)),
              SyscallSucceeds());
}

}  // namespace testing
}  // namespace gvisor