lib:krb5_wrap: Fix resource leak in smb_krb5_kt_seek_and_delete_old_entries
authorPavel Filipenský <pfilipensky@samba.org>
Wed, 26 Jul 2023 20:37:51 +0000 (22:37 +0200)
committerStefan Metzmacher <metze@samba.org>
Mon, 31 Jul 2023 10:56:54 +0000 (10:56 +0000)
Reported by Red Hat internal covscan
leaked_storage: Variable "cursor" going out of scope leaks the storage it points to.

Signed-off-by: Pavel Filipenský <pfilipensky@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
lib/krb5_wrap/krb5_samba.c

index 427e4beb91ad3410df5bb80671ded6aaefd2ccca..9488fcde7e286e76f6989dff2350216c6bf07d2d 100644 (file)
@@ -1690,17 +1690,22 @@ krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
        ZERO_STRUCT(cursor);
        ZERO_STRUCT(kt_entry);
 
+       /*
+        * Start with talloc_new() and only then call krb5_kt_start_seq_get().
+        * If any of them fails, the cleanup code is simpler.
+        */
+       tmp_ctx = talloc_new(NULL);
+       if (tmp_ctx == NULL) {
+               return ENOMEM;
+       }
+
        ret = krb5_kt_start_seq_get(context, keytab, &cursor);
        if (ret == KRB5_KT_END || ret == ENOENT ) {
                /* no entries */
+               talloc_free(tmp_ctx);
                return 0;
        }
 
-       tmp_ctx = talloc_new(NULL);
-       if (tmp_ctx == NULL) {
-               return ENOMEM;
-       }
-
        DEBUG(3, (__location__ ": Will try to delete old keytab entries\n"));
        while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
                bool name_ok = false;