From 6ff44aec5d1652ca5c497c82e17ccda513107418 Mon Sep 17 00:00:00 2001 From: William Brown Date: Apr 21 2017 03:04:10 +0000 Subject: Ticket 49226 - Memory leak in ldap-agent-bin Bug Description: We had a leak "somewhere" in the ldap-agent-bin code. This wasn't able to be easily traced, due to the output of: Direct leak of 29 byte(s) in 1 object(s) allocated from: #0 0x7f7c696e7880 in malloc (/lib64/libasan.so.4+0xde880) #1 0x7f7c691afca4 in ber_memalloc_x (/lib64/liblber-2.4.so.2+0x7ca4) #2 0x60400000034f () Fix Description: The issue was that we were not freeing the attr and val from ldap_parse_line while parsing our entries. https://pagure.io/389-ds-base/issue/49226 Author: wibrown Review by: nhosoi (Thanks for the "coverity" check!) --- diff --git a/ldap/servers/snmp/main.c b/ldap/servers/snmp/main.c index 94207f7..8166136 100644 --- a/ldap/servers/snmp/main.c +++ b/ldap/servers/snmp/main.c @@ -47,6 +47,16 @@ main (int argc, char *argv[]) { pid_t child_pid = 0; FILE *pid_fp; + /* Pause for the debugger if DEBUG_SLEEP is set in the environment */ + { + char *s = getenv( "DEBUG_SLEEP" ); + if ( (s != NULL) && isdigit(*s) ) { + int secs = atoi(s); + printf("%s pid is %d\n", argv[0], getpid()); + sleep(secs); + } + } + /* Load options */ while ((--argc > 0) && ((*++argv)[0] == '-')) { while ((c = *++argv[0])) { @@ -61,8 +71,9 @@ main (int argc, char *argv[]) { } } - if (argc != 1) + if (argc != 1) { exit_usage(); + } /* load config file */ if ((config_file = strdup(*argv)) == NULL) { @@ -416,15 +427,20 @@ load_config(char *conf_path) printf("ldap-agent: error parsing ldif line from [%s]\n", serv_p->dse_ldif); } - if ((strcmp(attr, "dn") == 0) && - (strcmp(val, "cn=config") == 0)) { + if ((strcmp(attr, "dn") == 0) && (strcmp(val, "cn=config") == 0)) { char *dse_line = NULL; - - + /* Free both outer values and attr */ + free(attr); + free(val); + attr = NULL; + val = NULL; + /* Look for port and rundir attributes */ while ((dse_line = ldif_getline(&entryp)) != NULL) { - ldif_parse_line(dse_line, &attr, &val, &vlen); - if (strcmp(attr, "nsslapd-snmp-index") == 0) { + if (ldif_parse_line(dse_line, &attr, &val, &vlen) != 0) { + /* can't parse these, try next line instead */ + continue; + } else if (strcmp(attr, "nsslapd-snmp-index") == 0) { snmp_index = atol(val); got_snmp_index = 1; } else if (strcmp(attr, "nsslapd-port") == 0) { @@ -447,6 +463,10 @@ load_config(char *conf_path) } got_rundir = 1; } + free(attr); + free(val); + attr = NULL; + val = NULL; /* Stop processing this entry if we found the * port and rundir and snmp_index settings */ @@ -458,6 +478,11 @@ load_config(char *conf_path) * cn=config entry, so we can stop reading through * the dse.ldif now. */ break; + } else { + free(attr); + free(val); + attr = NULL; + val = NULL; } }