s3:winbind: Fix memory leak in nss_init()
authorAndreas Schneider <asn@samba.org>
Thu, 9 Aug 2018 14:38:49 +0000 (16:38 +0200)
committerJeremy Allison <jra@samba.org>
Mon, 13 Aug 2018 17:46:08 +0000 (19:46 +0200)
Found by covscan.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13567

Pair-Programmed-With: Justin Stephenson <jstephen@redhat.com>
Signed-off-by: Andreas Schneider <asn@samba.org>
Signed-off-by: Justin Stephenson <jstephen@redhat.com>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/winbindd/nss_info.c

index 473b1a3b66e49deaa20ea5c0c7ee9ee263d8a091..1a8325ce7dc6168b22a504674e69de546ba5e51e 100644 (file)
@@ -84,7 +84,10 @@ static struct nss_function_entry *nss_get_backend(const char *name )
 /********************************************************************
  *******************************************************************/
 
-static bool parse_nss_parm( const char *config, char **backend, char **domain )
+static bool parse_nss_parm(TALLOC_CTX *mem_ctx,
+                          const char *config,
+                          char **backend,
+                          char **domain)
 {
        char *p;
 
@@ -98,17 +101,17 @@ static bool parse_nss_parm( const char *config, char **backend, char **domain )
        /* if no : then the string must be the backend name only */
 
        if ( !p ) {
-               *backend = SMB_STRDUP( config );
+               *backend = talloc_strdup(mem_ctx, config);
                return (*backend != NULL);
        }
 
        /* split the string and return the two parts */
 
        if ( strlen(p+1) > 0 ) {
-               *domain = SMB_STRDUP( p+1 );
+               *domain = talloc_strdup(mem_ctx, p + 1);
        }
 
-       *backend = SMB_STRNDUP(config, PTR_DIFF(p, config));
+       *backend = talloc_strndup(mem_ctx, config, PTR_DIFF(p, config));
        return (*backend != NULL);
 }
 
@@ -158,8 +161,9 @@ static NTSTATUS nss_init(const char **nss_list)
        NTSTATUS status;
        static bool nss_initialized = false;
        int i;
-       char *backend, *domain;
+       char *backend = NULL, *domain = NULL;
        struct nss_function_entry *nss_backend;
+       TALLOC_CTX *frame;
 
        /* check for previous successful initializations */
 
@@ -167,6 +171,8 @@ static NTSTATUS nss_init(const char **nss_list)
                return NT_STATUS_OK;
        }
 
+       frame = talloc_stackframe();
+
        /* The "template" backend should always be registered as it
           is a static module */
 
@@ -179,8 +185,10 @@ static NTSTATUS nss_init(const char **nss_list)
           as necessary) */
 
        for ( i=0; nss_list && nss_list[i]; i++ ) {
+               bool ok;
 
-               if ( !parse_nss_parm(nss_list[i], &backend, &domain) ) {
+               ok = parse_nss_parm(frame, nss_list[i], &backend, &domain);
+               if (!ok) {
                        DEBUG(0,("nss_init: failed to parse \"%s\"!\n",
                                 nss_list[i]));
                        continue;
@@ -238,10 +246,11 @@ static NTSTATUS nss_init(const char **nss_list)
 
                /* cleanup */
 
-               SAFE_FREE( backend );
-               SAFE_FREE( domain );
+               TALLOC_FREE(domain);
+               TALLOC_FREE(backend);
        }
 
+
        if ( !nss_domain_list ) {
                DEBUG(3,("nss_init: no nss backends configured.  "
                         "Defaulting to \"template\".\n"));
@@ -252,6 +261,7 @@ static NTSTATUS nss_init(const char **nss_list)
 
        nss_initialized = true;
 
+       TALLOC_FREE(frame);
        return NT_STATUS_OK;
 }