Skip to content

Commit d6a5d3b

Browse files
committed
Fix: memory leak due to openssl lib used by net-snmp
1 parent 314fee3 commit d6a5d3b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

plugins/in_snmp/in_snmp.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string.h>
44
#include <net-snmp/net-snmp-config.h>
55
#include <net-snmp/net-snmp-includes.h>
6+
#include <openssl/crypto.h>
67
#include <msgpack.h>
78
#include <fluent-bit/flb_input.h>
89
#include <fluent-bit/flb_time.h>
@@ -131,10 +132,13 @@ static int in_snmp_collect(struct flb_input_instance *ins, struct flb_config *co
131132

132133
char *err;
133134

135+
136+
init_snmp(PLUGIN_NAME);
137+
134138
init_snmp(PLUGIN_NAME);
135139
snmp_sess_init(&session);
136140

137-
session.peername = strdup(ctx->target_host);
141+
session.peername = ctx->target_host;
138142

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

157-
session.community = (u_char *) strdup(ctx->community);
161+
session.community = (u_char *) ctx->community;
158162
session.community_len = strlen(ctx->community);
159163

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

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

305308
snmp_close(ss);
306309

307-
flb_free(buf);
308-
flb_free(oid_buf);
309-
310-
SOCK_CLEANUP;
310+
if (buf) flb_free(buf);
311+
if (oid_buf) flb_free(oid_buf);
311312

312313
return ret;
313314
}
@@ -367,6 +368,9 @@ static int in_snmp_init(struct flb_input_instance *in,
367368
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS, 1);
368369
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 1);
369370

371+
SOCK_STARTUP;
372+
init_snmp(PLUGIN_NAME);
373+
370374
return 0;
371375
}
372376

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

394398
flb_free(ctx);
399+
snmp_shutdown(PLUGIN_NAME);
400+
401+
unload_all_mibs();
402+
SOCK_CLEANUP;
403+
404+
OPENSSL_cleanup();
395405

396406
return 0;
397407
}

0 commit comments

Comments
 (0)