From b28fd4e527e28bea98bd1e396f5c617051561d35 Mon Sep 17 00:00:00 2001 From: Louis Scalbert Date: Tue, 23 Jan 2024 11:52:59 +0100 Subject: [PATCH] bgpd: fix res validity in rpki_create_socket Fix coverity scanner issue 1575912 where res pointer is supposed to valid in: > socket = vrf_socket(res->ai_family, ...) but is checked for validity a few lines later. Note that vrf_getaddrinfo returns an error code if getaddrinfo() fails to allocate res and in this case, rpki_create_socket() returns. Fixes: a951752 ("bgpd: create cache server socket in vrf") Signed-off-by: Louis Scalbert --- bgpd/bgp_rpki.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index de5476173d..2c716a0e28 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -25,6 +25,7 @@ #include "memory.h" #include "frrevent.h" #include "filter.h" +#include "lib_errors.h" #include "bgpd/bgpd.h" #include "bgpd/bgp_table.h" #include "bgp_advertise.h" @@ -1332,8 +1333,11 @@ static int rpki_create_socket(void *_cache) frr_with_privs (&bgpd_privs) { ret = vrf_getaddrinfo(host, port, &hints, &res, vrf->vrf_id); } - if (ret != 0) + if (ret != 0) { + flog_err_sys(EC_LIB_SOCKET, "getaddrinfo: %s", + gai_strerror(ret)); return -1; + } frr_with_privs (&bgpd_privs) { socket = vrf_socket(res->ai_family, res->ai_socktype, @@ -1354,15 +1358,13 @@ static int rpki_create_socket(void *_cache) setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); if (connect(socket, res->ai_addr, res->ai_addrlen) == -1) { - if (res) - freeaddrinfo(res); + freeaddrinfo(res); close(socket); pthread_setcancelstate(cancel_state, NULL); return -1; } - if (res) - freeaddrinfo(res); + freeaddrinfo(res); pthread_setcancelstate(cancel_state, NULL); setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &prev_rcv_tmout, -- 2.39.5