From 065dfd4afa9ad20a7b31bfefa5062724c985a514 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:44:24 +0200 Subject: [PATCH 01/14] bgpd: Fix dereference of rpki_vrf for rpki_expire_interval_cmd CID 1575926 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 219cb299834e..25839b2ca54e 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1769,6 +1769,9 @@ DEFPY (rpki_expire_interval, else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + if ((unsigned int)tmp >= rpki_vrf->polling_period) { rpki_vrf->expire_interval = tmp; return CMD_SUCCESS; From 3ffc9bc6b3589938d66f6dcb6a116ee8e519b44d Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:45:20 +0200 Subject: [PATCH 02/14] bgpd: Fix dereference for no_rpki_retry_interval_cmd CID 1575925 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 25839b2ca54e..56e0ef99ba03 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1833,6 +1833,9 @@ DEFUN (no_rpki_retry_interval, else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + rpki_vrf->retry_interval = RETRY_INTERVAL_DEFAULT; return CMD_SUCCESS; } From fe0c2880d837f116445e5d47c2ed3e1e781adaf3 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:53:24 +0200 Subject: [PATCH 03/14] bgpd: Check getsockopt/setsockopt return values CID 1575924 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 56e0ef99ba03..a196be833949 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1345,11 +1345,29 @@ static int rpki_create_socket(void *_cache) timeout.tv_usec = 0; optlen = sizeof(prev_rcv_tmout); - getsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &prev_rcv_tmout, &optlen); - getsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &prev_snd_tmout, &optlen); - - setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); + ret = getsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &prev_rcv_tmout, + &optlen); + if (ret < 0) + zlog_warn("%s: failed to getsockopt SO_RCVTIMEO for socket %d", + __func__, socket); + + ret = getsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &prev_snd_tmout, + &optlen); + if (ret < 0) + zlog_warn("%s: failed to getsockopt SO_SNDTIMEO for socket %d", + __func__, socket); + + ret = setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, + sizeof(timeout)); + if (ret < 0) + zlog_warn("%s: failed to setsockopt SO_RCVTIMEO for socket %d", + __func__, socket); + + ret = setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, + sizeof(timeout)); + if (ret < 0) + zlog_warn("%s: failed to setsockopt SO_SNDTIMEO for socket %d", + __func__, socket); if (connect(socket, res->ai_addr, res->ai_addrlen) == -1) { if (res) @@ -1363,10 +1381,17 @@ static int rpki_create_socket(void *_cache) freeaddrinfo(res); pthread_setcancelstate(cancel_state, NULL); - setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &prev_rcv_tmout, - sizeof(prev_rcv_tmout)); - setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &prev_snd_tmout, - sizeof(prev_snd_tmout)); + ret = setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &prev_rcv_tmout, + sizeof(prev_rcv_tmout)); + if (ret < 0) + zlog_warn("%s: failed to setsockopt SO_RCVTIMEO for socket %d", + __func__, socket); + + ret = setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &prev_snd_tmout, + sizeof(prev_snd_tmout)); + if (ret < 0) + zlog_warn("%s: failed to setsockopt SO_SNDTIMEO for socket %d", + __func__, socket); return socket; } From 2cf71e2baaf50009d8e44a0a076e7f248d089221 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:54:12 +0200 Subject: [PATCH 04/14] bgpd: Fix dereference for rpki_polling_period_cmd CID 1575923 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index a196be833949..e8f0609a7c59 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1757,6 +1757,9 @@ DEFPY (rpki_polling_period, else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + rpki_vrf->polling_period = pp; return CMD_SUCCESS; } From 1cae97e19a5a490d3b2bbd5b0170ff84bae51d11 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:54:59 +0200 Subject: [PATCH 05/14] bgpd: Fix dereference for no_rpki_polling_period_cmd CID 1575922 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index e8f0609a7c59..59f74dbda268 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1779,6 +1779,9 @@ DEFUN (no_rpki_polling_period, else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + rpki_vrf->polling_period = POLLING_PERIOD_DEFAULT; return CMD_SUCCESS; } From d7afcfd4351a4b8cc47364980ef7c5466ecf7d89 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:55:29 +0200 Subject: [PATCH 06/14] bgpd: Fix dereference for no_rpki_expire_interval_cmd CID 1575922 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 59f74dbda268..ade1a68576c7 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1827,6 +1827,9 @@ DEFUN (no_rpki_expire_interval, else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + rpki_vrf->expire_interval = rpki_vrf->polling_period * 2; return CMD_SUCCESS; } From df67510b96c0cadd4617d8678d2e50d585baa03e Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:57:17 +0200 Subject: [PATCH 07/14] bgpd: Return warning if can't find the VRF for RPKI Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index ade1a68576c7..28eec4e09d38 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -2416,7 +2416,7 @@ DEFPY(show_rpki_configuration, show_rpki_configuration_cmd, if (!rpki_vrf) { if (uj) vty_json(vty, json); - return CMD_SUCCESS; + return CMD_WARNING_CONFIG_FAILED; } if (uj) { From b2ee83c92e21dded84574974e69e268945a59fc7 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:57:54 +0200 Subject: [PATCH 08/14] bgpd: Fix dereference for config_on_exit CID 1575920 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 28eec4e09d38..8add4629e76f 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -2462,6 +2462,10 @@ static int config_on_exit(struct vty *vty) rpki_vrf = VTY_GET_CONTEXT_SUB(rpki_vrf); else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + reset(false, rpki_vrf); return 1; } From fbee8dfdd59ec22ce459c881e3a8b53aae1624b0 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 09:59:06 +0200 Subject: [PATCH 09/14] bgpd: Fix dereference for no_rpki_cache_cmd CID 1575919 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 8add4629e76f..f3c50a58164f 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1970,9 +1970,12 @@ DEFPY (no_rpki_cache, else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + if (!rpki_vrf) + return CMD_WARNING; + cache_list = rpki_vrf->cache_list; cache_p = find_cache(preference, cache_list); - if (!rpki_vrf || !cache_p) { + if (!cache_p) { vty_out(vty, "Could not find cache with preference %ld\n", preference); return CMD_WARNING; From f623e99395581a5210c754bcc533664fdfc77d80 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 10:03:37 +0200 Subject: [PATCH 10/14] bgpd: Fix dereference for rpki_cmd CID 1575917 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index f3c50a58164f..1116c645da22 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1652,6 +1652,9 @@ DEFUN_NOSH (rpki, else { struct vrf *vrf = VTY_GET_CONTEXT(vrf); + if (!vrf) + return CMD_WARNING_CONFIG_FAILED; + vty->node = RPKI_VRF_NODE; if (vrf->vrf_id != VRF_DEFAULT) vrfname = vrf->name; From 5f8b040bb88d4e6bd87ecdbdee8921af0792980c Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 10:04:28 +0200 Subject: [PATCH 11/14] bgpd: Fix dereference for rpki_retry_interval_cmd CID 1575915 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 1116c645da22..121eca3d5bc6 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1851,6 +1851,9 @@ DEFPY (rpki_retry_interval, else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + rpki_vrf->retry_interval = tmp; return CMD_SUCCESS; } From cde1a8ff4077196d252f1faa830909f4ddf2876a Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 10:08:10 +0200 Subject: [PATCH 12/14] bgpd: Fix memory leak for rpki_create_socket() CID 1575914 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 121eca3d5bc6..0700e923392f 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1330,8 +1330,10 @@ 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) { + freeaddrinfo(res); return -1; + } frr_with_privs (&bgpd_privs) { socket = vrf_socket(res->ai_family, res->ai_socktype, From ba9f60559176c43fe4fb5bbef368eca7867a73de Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 10:09:16 +0200 Subject: [PATCH 13/14] bgpd: Fix dereference for rpki_reset_config_mode CID 1575913 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index 0700e923392f..be6204344fdb 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -2509,6 +2509,10 @@ DEFPY (rpki_reset_config_mode, rpki_vrf = VTY_GET_CONTEXT_SUB(rpki_vrf); else rpki_vrf = VTY_GET_CONTEXT(rpki_vrf); + + if (!rpki_vrf) + return CMD_WARNING_CONFIG_FAILED; + return reset(true, rpki_vrf) == SUCCESS ? CMD_SUCCESS : CMD_WARNING; } From 3653cc78317a39a367da3b9b32be2180a517469e Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Tue, 23 Jan 2024 10:12:10 +0200 Subject: [PATCH 14/14] bgpd: Fix memory leak for rpki_create_socket If vrf_socket fails, need to free resources allocated by vrf_getaddrinfo(). CID 1575912 Signed-off-by: Donatas Abraitis --- bgpd/bgp_rpki.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bgpd/bgp_rpki.c b/bgpd/bgp_rpki.c index be6204344fdb..038f71ed0de0 100644 --- a/bgpd/bgp_rpki.c +++ b/bgpd/bgp_rpki.c @@ -1331,7 +1331,8 @@ static int rpki_create_socket(void *_cache) ret = vrf_getaddrinfo(host, port, &hints, &res, vrf->vrf_id); } if (ret != 0) { - freeaddrinfo(res); + if (res) + freeaddrinfo(res); return -1; } @@ -1339,8 +1340,11 @@ static int rpki_create_socket(void *_cache) socket = vrf_socket(res->ai_family, res->ai_socktype, res->ai_protocol, vrf->vrf_id, NULL); } - if (socket < 0) + if (socket < 0) { + if (res) + freeaddrinfo(res); return -1; + } pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancel_state); timeout.tv_sec = 30;