Commit 6ff44ae Ticket 49226 - Memory leak in ldap-agent-bin

1 file Authored and Committed by firstyear 4 days ago
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  (<unknown module>)

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!)

    
 1 @@ -47,6 +47,16 @@
 2       pid_t               child_pid = 0;
 3       FILE                *pid_fp;
 4   
 5 +     /* Pause for the debugger if DEBUG_SLEEP is set in the environment */
 6 +     {
 7 +         char *s = getenv( "DEBUG_SLEEP" );
 8 +         if ( (s != NULL) && isdigit(*s) ) {
 9 +             int secs = atoi(s);
10 +             printf("%s pid is %d\n", argv[0], getpid());
11 +             sleep(secs);
12 +         }
13 +     }
14 + 
15       /* Load options */
16       while ((--argc > 0) && ((*++argv)[0] == '-')) {
17           while ((c = *++argv[0])) {
18 @@ -61,8 +71,9 @@
19           }
20       }
21   
22 -     if (argc != 1)
23 +     if (argc != 1) {
24           exit_usage();
25 +     }
26   
27       /* load config file */
28       if ((config_file = strdup(*argv)) == NULL) {
29 @@ -416,15 +427,20 @@
30                       printf("ldap-agent: error parsing ldif line from [%s]\n", serv_p->dse_ldif);
31                   }
32   
33 -                 if ((strcmp(attr, "dn") == 0) &&
34 -                     (strcmp(val, "cn=config") == 0)) {
35 +                 if ((strcmp(attr, "dn") == 0) && (strcmp(val, "cn=config") == 0)) {
36                       char *dse_line = NULL;
37 -                     
38 -                     
39 +                     /* Free both outer values and attr */
40 +                     free(attr);
41 +                     free(val);
42 +                     attr = NULL;
43 +                     val = NULL;
44 + 
45                       /* Look for port and rundir attributes */
46                       while ((dse_line = ldif_getline(&entryp)) != NULL) {
47 -                         ldif_parse_line(dse_line, &attr, &val, &vlen);
48 -                         if (strcmp(attr, "nsslapd-snmp-index") == 0) {
49 +                         if (ldif_parse_line(dse_line, &attr, &val, &vlen) != 0) {
50 +                             /* can't parse these, try next line instead */
51 +                             continue;
52 +                         } else if (strcmp(attr, "nsslapd-snmp-index") == 0) {
53                               snmp_index = atol(val);
54                               got_snmp_index = 1;
55                           } else if (strcmp(attr, "nsslapd-port") == 0) {
56 @@ -447,6 +463,10 @@
57                               }
58                               got_rundir = 1;
59                           }
60 +                         free(attr);
61 +                         free(val);
62 +                         attr = NULL;
63 +                         val = NULL;
64   
65                           /* Stop processing this entry if we found the
66                            *  port and rundir and snmp_index settings */
67 @@ -458,6 +478,11 @@
68                        * cn=config entry, so we can stop reading through
69                        * the dse.ldif now. */
70                       break;
71 +                 } else {
72 +                     free(attr);
73 +                     free(val);
74 +                     attr = NULL;
75 +                     val = NULL;
76                   }
77               }
78