]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_prometheus: Clone containers before iterating
authorGeorge Joseph <gjoseph@digium.com>
Thu, 1 Apr 2021 13:39:03 +0000 (07:39 -0600)
committerFriendly Automation <jenkins2@gerrit.asterisk.org>
Fri, 2 Apr 2021 12:38:57 +0000 (07:38 -0500)
The channels, bridges and endpoints scrape functions were
grabbing their respective global containers, getting the
count of entries, allocating metric arrays based on
that count, then iterating over the container.  If the
global container had new objects added after the count
was taken and the metric arrays were allocated, we'd run
out of metric entries and attempt to write past the end
of the arrays.

Now each of the scape functions clone their respective
global containers and all operations are done on the
clone.  Since the clone is stable between getting the
count and iterating over it, we can't run past the end
of the metrics array.

ASTERISK-29130
Reported-By: Francisco Correia
Reported-By: BJ Weschke
Reported-By: Sébastien Duthil
Change-Id: If0c8e40853bc0e9429f2ba9c7f5f358d90c311af

res/prometheus/bridges.c
res/prometheus/channels.c
res/prometheus/endpoints.c

index 47dee9a221a8351c00738cd129088b07bac8bc92..0f09603e655ddf8b4e95e308deee41f1cd912e05 100644 (file)
@@ -77,6 +77,7 @@ struct bridge_metric_defs {
  */
 static void bridges_scrape_cb(struct ast_str **response)
 {
+       struct ao2_container *bridge_cache;
        struct ao2_container *bridges;
        struct ao2_iterator it_bridges;
        struct ast_bridge *bridge;
@@ -92,10 +93,17 @@ static void bridges_scrape_cb(struct ast_str **response)
 
        ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
 
-       bridges = ast_bridges();
+       bridge_cache = ast_bridges();
+       if (!bridge_cache) {
+               return;
+       }
+
+       bridges = ao2_container_clone(bridge_cache, 0);
+       ao2_ref(bridge_cache, -1);
        if (!bridges) {
                return;
        }
+
        num_bridges = ao2_container_count(bridges);
 
        /* Current endpoint count */
@@ -178,4 +186,4 @@ int bridge_metrics_init(void)
        prometheus_callback_register(&bridges_callback);
 
        return 0;
-}
\ No newline at end of file
+}
index 97b7519c0dd56cb296377fc9660742d5ec3c1a62..930ae5485bbb80fc7a0c64fc1f6c29a21235b1f9 100644 (file)
@@ -129,6 +129,7 @@ static struct prometheus_metric global_channel_metrics[] = {
  */
 static void channels_scrape_cb(struct ast_str **response)
 {
+       struct ao2_container *channel_cache;
        struct ao2_container *channels;
        struct ao2_iterator it_chans;
        struct ast_channel_snapshot *snapshot;
@@ -145,7 +146,17 @@ static void channels_scrape_cb(struct ast_str **response)
 
        ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
 
-       channels = ast_channel_cache_all();
+       channel_cache = ast_channel_cache_all();
+       if (!channel_cache) {
+               return;
+       }
+
+       channels = ao2_container_clone(channel_cache, 0);
+       ao2_ref(channel_cache, -1);
+       if (!channels) {
+               return;
+       }
+
        num_channels = ao2_container_count(channels);
 
        /* Channel count */
@@ -233,4 +244,4 @@ int channel_metrics_init(void)
        prometheus_callback_register(&channels_callback);
 
        return 0;
-}
\ No newline at end of file
+}
index 5f758c564166cebc79f97ca3950c08dba9514c3f..a575198431bb8ce5b03a65d7cb0a665dafb60586 100644 (file)
@@ -96,6 +96,7 @@ struct endpoint_metric_defs {
  */
 static void endpoints_scrape_cb(struct ast_str **response)
 {
+       struct ao2_container *endpoint_cache;
        struct ao2_container *endpoints;
        struct ao2_iterator it_endpoints;
        struct stasis_message *message;
@@ -111,10 +112,16 @@ static void endpoints_scrape_cb(struct ast_str **response)
 
        ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
 
-       endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
+       endpoint_cache = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
+       if (!endpoint_cache) {
+               return;
+       }
+       endpoints = ao2_container_clone(endpoint_cache, 0);
+       ao2_ref(endpoint_cache, -1);
        if (!endpoints) {
                return;
        }
+
        num_endpoints = ao2_container_count(endpoints);
 
        /* Current endpoint count */