@@ -55,18 +55,37 @@ SObj network_socket_connect_tcp(Timeout *timeout,
5555 Debug::log (" Host capability does not authorise a TCP connection" );
5656 return nullptr ;
5757 }
58+
59+ NetworkAddress address{NetworkAddress::AddressKindInvalid};
60+ CHERI::Capability addressPtr = &address;
61+ addressPtr.permissions () &= {CHERI::Permission::Store};
5862 firewall_permit_dns ();
59- NetworkAddress address = network_host_resolve (host->hostname , UseIPv6);
63+ int ret = network_host_resolve (host->hostname , UseIPv6, addressPtr );
6064 firewall_permit_dns (false );
61- if (address.kind == NetworkAddress::AddressKindInvalid)
65+ if ((ret < 0 ) || ( address.kind == NetworkAddress::AddressKindInvalid) )
6266 {
6367 Debug::log (" Failed to resolve host" );
6468 return nullptr ;
6569 }
66- bool isIPv6 = address.kind == NetworkAddress::AddressKindIPv6;
67- auto sealedSocket = network_socket_create_and_bind (
70+ bool isIPv6 = address.kind == NetworkAddress::AddressKindIPv6;
71+
72+ CHERI::Capability sealedSocket = network_socket_create_and_bind (
6873 timeout, mallocCapability, isIPv6, ConnectionTypeTCP);
69- auto kind = network_socket_kind (sealedSocket);
74+ if (!sealedSocket.is_valid ())
75+ {
76+ Debug::log (" Failed to create socket" );
77+ return nullptr ;
78+ }
79+
80+ SocketKind kind;
81+ CHERI::Capability kindPtr = &kind;
82+ kindPtr.permissions () &= {CHERI::Permission::Store};
83+ if (network_socket_kind (sealedSocket, kindPtr) < 0 )
84+ {
85+ Debug::log (" Failed to retrieve socket kind" );
86+ return nullptr ;
87+ }
88+
7089 // FIXME: IPv6
7190 if (isIPv6)
7291 {
@@ -78,10 +97,13 @@ SObj network_socket_connect_tcp(Timeout *timeout,
7897 firewall_add_tcpipv4_endpoint (
7998 address.ipv4 , kind.localPort , ntohs (host->port ));
8099 }
100+
81101 if (network_socket_connect_tcp_internal (
82102 timeout, sealedSocket, address, host->port ) != 0 )
83103 {
84104 Timeout t{UnlimitedTimeout};
105+ // We pass an unlimited timeout, so this cannot fail in any
106+ // actionable manner. Don't check the return value.
85107 network_socket_close (&t, mallocCapability, sealedSocket);
86108 timeout->elapse (t.elapsed );
87109 if (isIPv6)
@@ -118,7 +140,14 @@ NetworkAddress network_socket_udp_authorise_host(Timeout *timeout,
118140 Debug::log (" Host capability does not authorise a UDP connection" );
119141 return address;
120142 }
121- auto kind = network_socket_kind (socket);
143+
144+ SocketKind kind;
145+ CHERI::Capability kindPtr = &kind;
146+ kindPtr.permissions () &= {CHERI::Permission::Store};
147+ // No need to check the return value here, potential errors will be
148+ // detected in the switch.
149+ network_socket_kind (socket, kindPtr);
150+
122151 bool isIPv6 = false ;
123152 switch (kind.protocol )
124153 {
@@ -133,10 +162,13 @@ NetworkAddress network_socket_udp_authorise_host(Timeout *timeout,
133162 isIPv6 = true ;
134163 break ;
135164 }
165+
166+ CHERI::Capability addressPtr = &address;
167+ addressPtr.permissions () &= {CHERI::Permission::Store};
136168 firewall_permit_dns ();
137- address = network_host_resolve (host->hostname , UseIPv6);
169+ int ret = network_host_resolve (host->hostname , UseIPv6, addressPtr );
138170 firewall_permit_dns (false );
139- if (address.kind == NetworkAddress::AddressKindInvalid)
171+ if ((ret < 0 ) || ( address.kind == NetworkAddress::AddressKindInvalid) )
140172 {
141173 Debug::log (" Failed to resolve host" );
142174 return address;
@@ -146,6 +178,7 @@ NetworkAddress network_socket_udp_authorise_host(Timeout *timeout,
146178 Debug::log (" Host address does not match socket type" );
147179 return address;
148180 }
181+
149182 if (isIPv6)
150183 {
151184 firewall_add_udpipv6_endpoint (
@@ -161,6 +194,7 @@ NetworkAddress network_socket_udp_authorise_host(Timeout *timeout,
161194 firewall_add_udpipv4_endpoint (
162195 address.ipv4 , kind.localPort , ntohs (host->port ));
163196 }
197+
164198 return address;
165199}
166200
0 commit comments