aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKartik Naik <kartik@bugqore.com>2026-07-30 13:19:47 +0530
committerKartik Naik <kartik@bugqore.com>2026-07-30 13:19:47 +0530
commit6c466fce38cca290c7760306a57ee32fe2b3ac4e (patch)
tree83ba0618320ab8e7454e17aede176ee7956fcd61 /tests
parentd4e35851df280ec5f26c3d0942c39d9d098b43a0 (diff)
downloadportable-6c466fce38cca290c7760306a57ee32fe2b3ac4e.tar.gz
portable-6c466fce38cca290c7760306a57ee32fe2b3ac4e.tar.bz2
portable-6c466fce38cca290c7760306a57ee32fe2b3ac4e.zip
verify the accepted peer in the win32 socketpair emulation
Diffstat (limited to 'tests')
-rw-r--r--tests/compat/pipe2.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/tests/compat/pipe2.c b/tests/compat/pipe2.c
index 9d93a55..c0189db 100644
--- a/tests/compat/pipe2.c
+++ b/tests/compat/pipe2.c
@@ -74,8 +74,15 @@ int socketpair(int domain, int type, int protocol, int socket_vector[2])
74 .sin_port = 0, 74 .sin_port = 0,
75 }; 75 };
76 76
77 struct sockaddr_in self, peer;
77 int yes = 1, e; 78 int yes = 1, e;
78 if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, 79
80 /*
81 * SO_REUSEADDR does not mean here what it means on unix: it lets any
82 * other process bind the same address and port and take over the
83 * rendezvous. SO_EXCLUSIVEADDRUSE is the flag that keeps the port ours.
84 */
85 if (setsockopt(listener, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
79 (void *)&yes, sizeof yes) == -1) 86 (void *)&yes, sizeof yes) == -1)
80 goto err; 87 goto err;
81 88
@@ -104,6 +111,28 @@ int socketpair(int domain, int type, int protocol, int socket_vector[2])
104 if (socket_vector[1] == -1) 111 if (socket_vector[1] == -1)
105 goto err; 112 goto err;
106 113
114 /*
115 * The listening port is enumerable by anything running as the user, so
116 * the connection we just accepted is not necessarily the one we made.
117 * Pair the two halves only if they are each other's peer.
118 */
119 memset(&self, 0, sizeof self);
120 addrlen = sizeof self;
121 if (getsockname(socket_vector[0], (struct sockaddr *)&self, &addrlen) != 0)
122 goto err;
123
124 memset(&peer, 0, sizeof peer);
125 addrlen = sizeof peer;
126 if (getpeername(socket_vector[1], (struct sockaddr *)&peer, &addrlen) != 0)
127 goto err;
128
129 if (self.sin_family != peer.sin_family ||
130 self.sin_addr.s_addr != peer.sin_addr.s_addr ||
131 self.sin_port != peer.sin_port) {
132 WSASetLastError(WSAECONNREFUSED);
133 goto err;
134 }
135
107 closesocket(listener); 136 closesocket(listener);
108 137
109 create_issue_1069_sentinels(socket_vector); 138 create_issue_1069_sentinels(socket_vector);