Skip to content

Commit

Permalink
Fix: memory leak due to openssl lib used by net-snmp
Browse files Browse the repository at this point in the history
  • Loading branch information
k402xxxcenxxx committed Jul 11, 2023
1 parent 7182681 commit bb89c23
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions plugins/in_snmp/in_snmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <openssl/crypto.h>
#include <msgpack.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_time.h>
Expand Down Expand Up @@ -131,10 +132,13 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co

char *err;


init_snmp(PLUGIN_NAME);

init_snmp(PLUGIN_NAME);
snmp_sess_init(&session);

session.peername = strdup(ctx->target_host);
session.peername = ctx->target_host;

if (strcmp(ctx->version, "1") == 0) {
session.version = SNMP_VERSION_1;
Expand All @@ -154,10 +158,9 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
return -1;
}

session.community = (u_char *) strdup(ctx->community);
session.community = (u_char *) ctx->community;
session.community_len = strlen(ctx->community);

SOCK_STARTUP;
ss = snmp_open(&session);
if (!ss) {
snmp_error(ss, NULL, NULL, &err);
Expand All @@ -167,7 +170,7 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
}

anOID_len = MAX_OID_LEN;
if (snmp_parse_oid(strdup(ctx->oid), anOID, &anOID_len) == NULL) {
if (snmp_parse_oid(ctx->oid, anOID, &anOID_len) == NULL) {
flb_plg_error(ctx->ins, "Fail to parse oid");
SOCK_CLEANUP;
return -1;
Expand Down Expand Up @@ -304,10 +307,8 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co

snmp_close(ss);

flb_free(buf);
flb_free(oid_buf);

SOCK_CLEANUP;
if (buf) flb_free(buf);
if (oid_buf) flb_free(oid_buf);

return ret;
}
Expand Down Expand Up @@ -367,6 +368,9 @@ static int in_snmp_init(struct flb_input_instance *in,
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS, 1);
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 1);

SOCK_STARTUP;
init_snmp(PLUGIN_NAME);

return 0;
}

Expand All @@ -392,6 +396,12 @@ static int in_snmp_exit(void *data, struct flb_config *config)
flb_log_event_encoder_destroy(&ctx->log_encoder);

flb_free(ctx);
snmp_shutdown(PLUGIN_NAME);

unload_all_mibs();
SOCK_CLEANUP;

OPENSSL_cleanup();

return 0;
}
Expand Down

0 comments on commit bb89c23

Please sign in to comment.