From 86b9d3af98501820ecc6d99780843bf09941516f Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Fri, 11 Feb 2022 12:33:07 +0100 Subject: [PATCH] src: don't print interface if sin6_scope_id is 0 An interface with index 0 doesn't make sense and makes `if_indextoname()` to return `ENXIO` which crashes the process. Fixes: https://github.com/nodejs/node/issues/41500 PR-URL: https://github.com/nodejs/node/pull/41547 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Antoine du Hamel --- src/tcp_wrap.cc | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 84b18a1592d93c..669206fc6bf94b 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -360,16 +360,20 @@ Local AddressToJS(Environment* env, a6 = reinterpret_cast(addr); uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip); // Add an interface identifier to a link local address. - if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr)) { - const size_t addrlen = strlen(ip); - CHECK_LT(addrlen, sizeof(ip)); - ip[addrlen] = '%'; - size_t scopeidlen = sizeof(ip) - addrlen - 1; - CHECK_GE(scopeidlen, UV_IF_NAMESIZE); - const int r = uv_if_indextoiid(a6->sin6_scope_id, - ip + addrlen + 1, - &scopeidlen); - CHECK_EQ(r, 0); + if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr) && a6->sin6_scope_id > 0) { + const size_t addrlen = strlen(ip); + CHECK_LT(addrlen, sizeof(ip)); + ip[addrlen] = '%'; + size_t scopeidlen = sizeof(ip) - addrlen - 1; + CHECK_GE(scopeidlen, UV_IF_NAMESIZE); + const int r = uv_if_indextoiid(a6->sin6_scope_id, + ip + addrlen + 1, + &scopeidlen); + if (r) { + env->ThrowUVException(r, "uv_if_indextoiid"); + // TODO(addaleax): Do proper MaybeLocal handling here + return scope.Escape(info); + } } port = ntohs(a6->sin6_port); info->Set(env->context(),