From 2307c3700cc8eea44e33c8ad0a993115a841ae7c Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Sun, 2 Nov 2014 20:21:29 +0100 Subject: [PATCH] nt_printing: don't leak state_path onto talloc tos Also check for allocation failures, and close tdbs in nt_printing_tdb_upgrade error paths. Signed-off-by: David Disseldorp Reviewed-by: Jeremy Allison --- .../printing/nt_printing_migrate_internal.c | 25 ++++-- source3/printing/nt_printing_tdb.c | 89 +++++++++++++------ 2 files changed, 82 insertions(+), 32 deletions(-) diff --git a/source3/printing/nt_printing_migrate_internal.c b/source3/printing/nt_printing_migrate_internal.c index 6bc7ea2e8ec..bbcce8e4e8a 100644 --- a/source3/printing/nt_printing_migrate_internal.c +++ b/source3/printing/nt_printing_migrate_internal.c @@ -186,17 +186,30 @@ static NTSTATUS migrate_internal(TALLOC_CTX *mem_ctx, bool nt_printing_tdb_migrate(struct messaging_context *msg_ctx) { - const char *drivers_path = state_path("ntdrivers.tdb"); - const char *printers_path = state_path("ntprinters.tdb"); - const char *forms_path = state_path("ntforms.tdb"); - bool drivers_exists = file_exist(drivers_path); - bool printers_exists = file_exist(printers_path); - bool forms_exists = file_exist(forms_path); + const char *drivers_path; + const char *printers_path; + const char *forms_path; + bool drivers_exists; + bool printers_exists; + bool forms_exists; struct auth_session_info *session_info; struct rpc_pipe_client *winreg_pipe = NULL; TALLOC_CTX *tmp_ctx = talloc_stackframe(); NTSTATUS status; + /* paths talloced on new stackframe */ + drivers_path = state_path("ntdrivers.tdb"); + printers_path = state_path("ntprinters.tdb"); + forms_path = state_path("ntforms.tdb"); + if ((drivers_path == NULL) || (printers_path == NULL) + || (forms_path == NULL)) { + talloc_free(tmp_ctx); + return false; + } + drivers_exists = file_exist(drivers_path); + printers_exists = file_exist(printers_path); + forms_exists = file_exist(forms_path); + if (!drivers_exists && !printers_exists && !forms_exists) { talloc_free(tmp_ctx); return true; diff --git a/source3/printing/nt_printing_tdb.c b/source3/printing/nt_printing_tdb.c index 870231af8c0..953448d497e 100644 --- a/source3/printing/nt_printing_tdb.c +++ b/source3/printing/nt_printing_tdb.c @@ -339,17 +339,39 @@ static bool upgrade_to_version_5(void) bool nt_printing_tdb_upgrade(void) { - const char *drivers_path = state_path("ntdrivers.tdb"); - const char *printers_path = state_path("ntprinters.tdb"); - const char *forms_path = state_path("ntforms.tdb"); - bool drivers_exists = file_exist(drivers_path); - bool printers_exists = file_exist(printers_path); - bool forms_exists = file_exist(forms_path); + char *drivers_path; + char *printers_path; + char *forms_path; + bool drivers_exists; + bool printers_exists; + bool forms_exists; const char *vstring = "INFO/version"; int32_t vers_id; + bool ret; + + drivers_path = state_path("ntdrivers.tdb"); + if (drivers_path == NULL) { + ret = false; + goto err_out; + } + printers_path = state_path("ntprinters.tdb"); + if (printers_path == NULL) { + ret = false; + goto err_drvdb_free; + } + forms_path = state_path("ntforms.tdb"); + if (forms_path == NULL) { + ret = false; + goto err_prdb_free; + } + + drivers_exists = file_exist(drivers_path); + printers_exists = file_exist(printers_path); + forms_exists = file_exist(forms_path); if (!drivers_exists && !printers_exists && !forms_exists) { - return true; + ret = true; + goto err_formsdb_free; } tdb_drivers = tdb_open_log(drivers_path, @@ -361,7 +383,8 @@ bool nt_printing_tdb_upgrade(void) DEBUG(0,("nt_printing_init: Failed to open nt drivers " "database %s (%s)\n", drivers_path, strerror(errno))); - return false; + ret = false; + goto err_formsdb_free; } tdb_printers = tdb_open_log(printers_path, @@ -373,7 +396,8 @@ bool nt_printing_tdb_upgrade(void) DEBUG(0,("nt_printing_init: Failed to open nt printers " "database %s (%s)\n", printers_path, strerror(errno))); - return false; + ret = false; + goto err_drvdb_close; } tdb_forms = tdb_open_log(forms_path, @@ -385,7 +409,8 @@ bool nt_printing_tdb_upgrade(void) DEBUG(0,("nt_printing_init: Failed to open nt forms " "database %s (%s)\n", forms_path, strerror(errno))); - return false; + ret = false; + goto err_prdb_close; } /* Samba upgrade */ @@ -400,7 +425,8 @@ bool nt_printing_tdb_upgrade(void) if ((vers_id == NTDRIVERS_DATABASE_VERSION_1) || (IREV(vers_id) == NTDRIVERS_DATABASE_VERSION_1)) { if (!upgrade_to_version_3()) { - return false; + ret = false; + goto err_formsdb_close; } tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_3); @@ -420,7 +446,8 @@ bool nt_printing_tdb_upgrade(void) if (vers_id == NTDRIVERS_DATABASE_VERSION_3) { if (!upgrade_to_version_4()) { - return false; + ret = false; + goto err_formsdb_close; } tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_4); vers_id = NTDRIVERS_DATABASE_VERSION_4; @@ -428,7 +455,8 @@ bool nt_printing_tdb_upgrade(void) if (vers_id == NTDRIVERS_DATABASE_VERSION_4 ) { if (!upgrade_to_version_5()) { - return false; + ret = false; + goto err_formsdb_close; } tdb_store_int32(tdb_drivers, vstring, NTDRIVERS_DATABASE_VERSION_5); vers_id = NTDRIVERS_DATABASE_VERSION_5; @@ -436,24 +464,33 @@ bool nt_printing_tdb_upgrade(void) if (vers_id != NTDRIVERS_DATABASE_VERSION_5) { DEBUG(0,("nt_printing_init: Unknown printer database version [%d]\n", vers_id)); - return false; + ret = false; + goto err_formsdb_close; } } + ret = true; - if (tdb_drivers) { - tdb_close(tdb_drivers); - tdb_drivers = NULL; - } - - if (tdb_printers) { - tdb_close(tdb_printers); - tdb_printers = NULL; - } - +err_formsdb_close: if (tdb_forms) { tdb_close(tdb_forms); tdb_forms = NULL; } - - return true; +err_prdb_close: + if (tdb_printers) { + tdb_close(tdb_printers); + tdb_printers = NULL; + } +err_drvdb_close: + if (tdb_drivers) { + tdb_close(tdb_drivers); + tdb_drivers = NULL; + } +err_formsdb_free: + talloc_free(forms_path); +err_prdb_free: + talloc_free(printers_path); +err_drvdb_free: + talloc_free(drivers_path); +err_out: + return ret; }