]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: Free Memory for SRv6 Functions and Locator Chunks
authorKeelan10 <keelan.cannoo@icloud.com>
Wed, 29 Nov 2023 11:49:58 +0000 (15:49 +0400)
committerMergify <37929162+mergify[bot]@users.noreply.github.com>
Thu, 30 Nov 2023 07:24:17 +0000 (07:24 +0000)
Implement proper memory cleanup for SRv6 functions and locator chunks to prevent potential memory leaks.
The list callback deletion functions have been set.

The ASan leak log for reference:

```
***********************************************************************************
Address Sanitizer Error detected in bgp_srv6l3vpn_to_bgp_vrf.test_bgp_srv6l3vpn_to_bgp_vrf/r2.asan.bgpd.4180

=================================================================
==4180==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 544 byte(s) in 2 object(s) allocated from:
    #0 0x7f8d176a0d28 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded28)
    #1 0x7f8d1709f238 in qcalloc lib/memory.c:105
    #2 0x55d5dba6ee75 in sid_register bgpd/bgp_mplsvpn.c:591
    #3 0x55d5dba6ee75 in alloc_new_sid bgpd/bgp_mplsvpn.c:712
    #4 0x55d5dba6f3ce in ensure_vrf_tovpn_sid_per_af bgpd/bgp_mplsvpn.c:758
    #5 0x55d5dba6fb94 in ensure_vrf_tovpn_sid bgpd/bgp_mplsvpn.c:849
    #6 0x55d5dba7f975 in vpn_leak_postchange bgpd/bgp_mplsvpn.h:299
    #7 0x55d5dba7f975 in vpn_leak_postchange_all bgpd/bgp_mplsvpn.c:3704
    #8 0x55d5dbbb6c66 in bgp_zebra_process_srv6_locator_chunk bgpd/bgp_zebra.c:3164
    #9 0x7f8d1716f08a in zclient_read lib/zclient.c:4459
    #10 0x7f8d1713f034 in event_call lib/event.c:1974
    #11 0x7f8d1708242b in frr_run lib/libfrr.c:1214
    #12 0x55d5db99d19d in main bgpd/bgp_main.c:510
    #13 0x7f8d160c5c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c86)

Direct leak of 296 byte(s) in 1 object(s) allocated from:
    #0 0x7f8d176a0d28 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded28)
    #1 0x7f8d1709f238 in qcalloc lib/memory.c:105
    #2 0x7f8d170b1d5f in srv6_locator_chunk_alloc lib/srv6.c:135
    #3 0x55d5dbbb6a19 in bgp_zebra_process_srv6_locator_chunk bgpd/bgp_zebra.c:3144
    #4 0x7f8d1716f08a in zclient_read lib/zclient.c:4459
    #5 0x7f8d1713f034 in event_call lib/event.c:1974
    #6 0x7f8d1708242b in frr_run lib/libfrr.c:1214
    #7 0x55d5db99d19d in main bgpd/bgp_main.c:510
    #8 0x7f8d160c5c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c86)
***********************************************************************************

```

Signed-off-by: Keelan Cannoo <keelan.cannoo@icloud.com>
(cherry picked from commit 8e7044ba3b806d286ed964746f4e88004035be09)

bgpd/bgp_mplsvpn.c
bgpd/bgp_vty.c
bgpd/bgp_zebra.c
bgpd/bgpd.c
bgpd/bgpd.h
lib/srv6.c
lib/srv6.h

index 18d9f64d1eb87a0d69390d59ce197253d07bb00a..ed74e0f08bcd32f205b413acb6bac5f6e9ee08af 100644 (file)
@@ -596,6 +596,11 @@ static void sid_register(struct bgp *bgp, const struct in6_addr *sid,
        listnode_add(bgp->srv6_functions, func);
 }
 
+void srv6_function_free(struct bgp_srv6_function *func)
+{
+       XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
+}
+
 void sid_unregister(struct bgp *bgp, const struct in6_addr *sid)
 {
        struct listnode *node, *nnode;
@@ -604,7 +609,7 @@ void sid_unregister(struct bgp *bgp, const struct in6_addr *sid)
        for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func))
                if (sid_same(&func->sid, sid)) {
                        listnode_delete(bgp->srv6_functions, func);
-                       XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
+                       srv6_function_free(func);
                }
 }
 
index e49bafffb8f4e7fa39858354430c7b4cf3a0ab91..ddef332d6071c8cb84e11aa27221be43bbdb687d 100644 (file)
@@ -310,7 +310,7 @@ static int bgp_srv6_locator_unset(struct bgp *bgp)
        /* refresh functions */
        for (ALL_LIST_ELEMENTS(bgp->srv6_functions, node, nnode, func)) {
                listnode_delete(bgp->srv6_functions, func);
-               XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
+               srv6_function_free(func);
        }
 
        /* refresh tovpn_sid */
index 5b69de08c2e98df9370dfddb888a8ba33c71e436..d38e7a349ba1df94ee1115726c1933c9e997975f 100644 (file)
@@ -3235,7 +3235,7 @@ static int bgp_zebra_process_srv6_locator_delete(ZAPI_CALLBACK_ARGS)
                if (prefix_match((struct prefix *)&loc.prefix,
                                 (struct prefix *)&tmp_prefi)) {
                        listnode_delete(bgp->srv6_functions, func);
-                       XFREE(MTYPE_BGP_SRV6_FUNCTION, func);
+                       srv6_function_free(func);
                }
        }
 
index 4b72798bd820802f696477eb85f0e76ee53d0cf8..2d499b89ae792138940270ba01c943610d180f8f 100644 (file)
@@ -1428,7 +1428,9 @@ static void bgp_srv6_init(struct bgp *bgp)
        bgp->srv6_enabled = false;
        memset(bgp->srv6_locator_name, 0, sizeof(bgp->srv6_locator_name));
        bgp->srv6_locator_chunks = list_new();
+       bgp->srv6_locator_chunks->del = srv6_locator_chunk_list_free;
        bgp->srv6_functions = list_new();
+       bgp->srv6_functions->del = (void (*)(void *))srv6_function_free;
 }
 
 static void bgp_srv6_cleanup(struct bgp *bgp)
index ca1411a3b17951e8f86c814993bce9935f848e24..ce02bf5ce702840dbc523cc7f430e277fe1eef63 100644 (file)
@@ -2721,6 +2721,9 @@ extern bool bgp_path_attribute_discard(struct peer *peer, char *buf,
                                       size_t size);
 extern bool bgp_path_attribute_treat_as_withdraw(struct peer *peer, char *buf,
                                                 size_t size);
+
+extern void srv6_function_free(struct bgp_srv6_function *func);
+
 #ifdef _FRR_ATTRIBUTE_PRINTFRR
 /* clang-format off */
 #pragma FRR printfrr_ext "%pBP" (struct peer *)
index 09835f3ea8b47606b0631d04a0da565cbaa92bff..dceb6ab48bb91440332e763e11ec583030f32b30 100644 (file)
@@ -108,7 +108,7 @@ const char *seg6local_context2str(char *str, size_t size,
        }
 }
 
-static void srv6_locator_chunk_list_free(void *data)
+void srv6_locator_chunk_list_free(void *data)
 {
        struct srv6_locator_chunk *chunk = data;
 
index fb34f861c8c2f82386af5f0652d19aa4d409db45..433c5c14fdcab8962a880cafd5b965c0078d81bd 100644 (file)
@@ -252,6 +252,7 @@ int snprintf_seg6_segs(char *str,
 extern struct srv6_locator *srv6_locator_alloc(const char *name);
 extern struct srv6_locator_chunk *srv6_locator_chunk_alloc(void);
 extern void srv6_locator_free(struct srv6_locator *locator);
+extern void srv6_locator_chunk_list_free(void *data);
 extern void srv6_locator_chunk_free(struct srv6_locator_chunk **chunk);
 json_object *srv6_locator_chunk_json(const struct srv6_locator_chunk *chunk);
 json_object *srv6_locator_json(const struct srv6_locator *loc);