Skip to content

Commit 768b151

Browse files
committed
interceptor: check for invalid fd
1 parent 5705ac4 commit 768b151

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

include/libwebsockets/lws-network-helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ lws_canonical_hostname(struct lws_context *context);
103103
* truncated if there is not enough room. If either cannot be
104104
* determined, they will be returned as valid zero-length strings.
105105
*/
106-
LWS_VISIBLE LWS_EXTERN void
106+
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
107107
lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
108108
int name_len, char *rip, int rip_len);
109109

lib/core-net/network.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
105105
struct addrinfo ai, *res;
106106
struct sockaddr_in addr4;
107107

108+
if (!rip)
109+
return -1;
110+
108111
rip[0] = '\0';
109-
name[0] = '\0';
112+
if (name)
113+
name[0] = '\0';
110114
addr4.sin_family = AF_UNSPEC;
111115

112116
#ifdef LWS_WITH_IPV6
@@ -212,7 +216,7 @@ lws_get_peer_simple(struct lws *wsi, char *name, size_t namelen)
212216
}
213217
#endif
214218

215-
void
219+
int
216220
lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
217221
int name_len, char *rip, int rip_len)
218222
{
@@ -224,8 +228,13 @@ lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
224228
struct sockaddr_in sin4;
225229
void *p;
226230

227-
rip[0] = '\0';
228-
name[0] = '\0';
231+
if (!lws_socket_is_valid(fd))
232+
return 1;
233+
234+
if (rip)
235+
rip[0] = '\0';
236+
if (name)
237+
name[0] = '\0';
229238

230239
#ifdef LWS_WITH_IPV6
231240
if (LWS_IPV6_ENABLED(wsi->a.vhost)) {
@@ -245,19 +254,20 @@ lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
245254
lwsl_wsi_warn(wsi, "getpeername: %s",
246255
lws_errno_describe(LWS_ERRNO, t16, sizeof(t16)));
247256
#endif
248-
goto bail;
257+
return 1;
249258
}
250259

251-
lws_get_addresses(wsi->a.vhost, p, name, name_len, rip, rip_len);
260+
return lws_get_addresses(wsi->a.vhost, p, name, name_len, rip, rip_len);
252261

253-
bail:
254-
#endif
262+
#else
255263
(void)wsi;
256264
(void)fd;
257265
(void)name;
258266
(void)name_len;
259267
(void)rip;
260268
(void)rip_len;
269+
return 0;
270+
#endif
261271
}
262272

263273

lib/roles/http/server/interceptor.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,13 @@ lws_interceptor_check(struct lws *wsi, const struct lws_protocols *prot)
285285
lws_sockaddr46 sa46;
286286
int allow = 0;
287287

288-
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi),
289-
NULL, 0, ip, sizeof(ip));
288+
lws_sockfd_type fd = lws_get_socket_fd(wsi);
289+
290+
if (lws_get_peer_addresses(wsi, fd,
291+
NULL, 0, ip, sizeof(ip))) {
292+
lwsl_err("%s: get peer ads fail\n", __func__);
293+
return 1;
294+
}
290295

291296
if (!lws_sa46_parse_numeric_address(ip, &sa46)) {
292297
while (cidr) {

0 commit comments

Comments
 (0)