From f9f59cd29b2daaa0d92ada99686e10aee1258f73 Mon Sep 17 00:00:00 2001 From: Norbert Federa Date: Tue, 5 May 2015 19:45:34 +0200 Subject: [PATCH] Fix unchecked CreateDirectory calls --- libfreerdp/core/transport.c | 17 ++-- libfreerdp/crypto/certificate.c | 89 +++++++++++--------- server/shadow/shadow_server.c | 51 +++++++---- winpr/libwinpr/pipe/pipe.c | 6 +- winpr/libwinpr/smartcard/smartcard_inspect.c | 10 ++- winpr/libwinpr/utils/wlog/BinaryAppender.c | 3 +- 6 files changed, 106 insertions(+), 70 deletions(-) diff --git a/libfreerdp/core/transport.c b/libfreerdp/core/transport.c index d345052ed..d2d0d1842 100644 --- a/libfreerdp/core/transport.c +++ b/libfreerdp/core/transport.c @@ -109,19 +109,16 @@ BOOL transport_connect_tls(rdpTransport* transport) rdpContext* context = transport->context; rdpSettings* settings = transport->settings; - if (transport->GatewayEnabled) - { - tls = transport->tls = tls_new(settings); - transport->layer = TRANSPORT_LAYER_TSG_TLS; - } - else - { - tls = transport->tls = tls_new(settings); - transport->layer = TRANSPORT_LAYER_TLS; - } + if (!(tls = tls_new(settings))) + return FALSE; transport->tls = tls; + if (transport->GatewayEnabled) + transport->layer = TRANSPORT_LAYER_TSG_TLS; + else + transport->layer = TRANSPORT_LAYER_TLS; + tls->hostname = settings->ServerHostname; tls->port = settings->ServerPort; diff --git a/libfreerdp/crypto/certificate.c b/libfreerdp/crypto/certificate.c index 945021d7e..fbe804d00 100644 --- a/libfreerdp/crypto/certificate.c +++ b/libfreerdp/crypto/certificate.c @@ -42,66 +42,75 @@ static const char certificate_known_hosts_file[] = "known_hosts"; #define TAG FREERDP_TAG("crypto") -int certificate_store_init(rdpCertificateStore* certificate_store) +BOOL certificate_store_init(rdpCertificateStore* certificate_store) { - char* server_path; + char* server_path = NULL; rdpSettings* settings; settings = certificate_store->settings; if (!PathFileExistsA(settings->ConfigPath)) { - CreateDirectoryA(settings->ConfigPath, 0); + if (!CreateDirectoryA(settings->ConfigPath, 0)) + { + WLog_ERR(TAG, "error creating directory '%s'", settings->ConfigPath); + goto fail; + } WLog_INFO(TAG, "creating directory %s", settings->ConfigPath); } - certificate_store->path = GetCombinedPath(settings->ConfigPath, (char*) certificate_store_dir); - - if (!certificate_store->path) - return -1; + if (!(certificate_store->path = GetCombinedPath(settings->ConfigPath, (char*) certificate_store_dir))) + goto fail; if (!PathFileExistsA(certificate_store->path)) { - CreateDirectoryA(certificate_store->path, 0); - WLog_INFO(TAG, "creating directory %s", certificate_store->path); + if (!CreateDirectoryA(certificate_store->path, 0)) + { + WLog_ERR(TAG, "error creating directory [%s]", certificate_store->path); + goto fail; + } + WLog_INFO(TAG, "creating directory [%s]", certificate_store->path); } - server_path = GetCombinedPath(settings->ConfigPath, (char*) certificate_server_dir); - - if (!server_path) - return -1; + if (!(server_path = GetCombinedPath(settings->ConfigPath, (char*) certificate_server_dir))) + goto fail; if (!PathFileExistsA(server_path)) { - CreateDirectoryA(server_path, 0); - WLog_INFO(TAG, "creating directory %s", server_path); + if (!CreateDirectoryA(server_path, 0)) + { + WLog_ERR(TAG, "error creating directory [%s]", server_path); + goto fail; + } + WLog_INFO(TAG, "created directory [%s]", server_path); + } + + if (!(certificate_store->file = GetCombinedPath(settings->ConfigPath, (char*) certificate_known_hosts_file))) + goto fail; + + if (!PathFileExistsA(certificate_store->file)) + certificate_store->fp = fopen((char*) certificate_store->file, "w+"); + else + certificate_store->fp = fopen((char*) certificate_store->file, "r+"); + + if (!certificate_store->fp) + { + WLog_ERR(TAG, "error opening [%s]", certificate_store->file); + goto fail; } free(server_path); - certificate_store->file = GetCombinedPath(settings->ConfigPath, (char*) certificate_known_hosts_file); + return TRUE; - if (!certificate_store->file) - return -1; - - if (PathFileExistsA(certificate_store->file) == FALSE) - { - certificate_store->fp = fopen((char*) certificate_store->file, "w+"); - - if (!certificate_store->fp) - { - WLog_ERR(TAG, "certificate_store_open: error opening [%s] for writing", certificate_store->file); - return -1; - } - - fflush(certificate_store->fp); - } - else - { - certificate_store->fp = fopen((char*) certificate_store->file, "r+"); - } - - return 1; +fail: + WLog_ERR(TAG, "certificate store initialization failed"); + free(server_path); + free(certificate_store->path); + free(certificate_store->file); + certificate_store->path = NULL; + certificate_store->file = NULL; + return FALSE; } int certificate_data_match(rdpCertificateStore* certificate_store, rdpCertificateData* certificate_data) @@ -285,7 +294,11 @@ rdpCertificateStore* certificate_store_new(rdpSettings* settings) certificate_store->settings = settings; - certificate_store_init(certificate_store); + if (!certificate_store_init(certificate_store)) + { + free(certificate_store); + return NULL; + } return certificate_store; } diff --git a/server/shadow/shadow_server.c b/server/shadow/shadow_server.c index 7867a548e..6c3a6422b 100644 --- a/server/shadow/shadow_server.c +++ b/server/shadow/shadow_server.c @@ -434,16 +434,25 @@ int shadow_server_init_config_path(rdpShadowServer* server) if (userLibraryPath) { - if (!PathFileExistsA(userLibraryPath)) - CreateDirectoryA(userLibraryPath, 0); + if (!PathFileExistsA(userLibraryPath) && + !CreateDirectoryA(userLibraryPath, 0)) + { + free(userLibraryPath); + return -1; + } userApplicationSupportPath = GetCombinedPath(userLibraryPath, "Application Support"); if (userApplicationSupportPath) { - if (!PathFileExistsA(userApplicationSupportPath)) - CreateDirectoryA(userApplicationSupportPath, 0); - + if (!PathFileExistsA(userApplicationSupportPath) && + !CreateDirectoryA(userApplicationSupportPath, 0)) + { + WLog_ERR(TAG, "Failed to create directory '%s'", userApplicationSupportPath); + free(userLibraryPath); + free(userApplicationSupportPath); + return -1; + } server->ConfigPath = GetCombinedPath(userApplicationSupportPath, "freerdp"); } @@ -461,11 +470,14 @@ int shadow_server_init_config_path(rdpShadowServer* server) if (configHome) { - if (!PathFileExistsA(configHome)) - CreateDirectoryA(configHome, 0); - + if (!PathFileExistsA(configHome) && + !CreateDirectoryA(configHome, 0)) + { + WLog_ERR(TAG, "Failed to create directory '%s'", configHome); + free(configHome); + return -1; + } server->ConfigPath = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, "freerdp"); - free(configHome); } } @@ -492,16 +504,23 @@ int shadow_server_init_certificate(rdpShadowServer* server) int makecert_argc = (sizeof(makecert_argv) / sizeof(char*)); - if (!PathFileExistsA(server->ConfigPath)) - CreateDirectoryA(server->ConfigPath, 0); + if (!PathFileExistsA(server->ConfigPath) && + !CreateDirectoryA(server->ConfigPath, 0)) + { + WLog_ERR(TAG, "Failed to create directory '%s'", server->ConfigPath); + return -1; + } - filepath = GetCombinedPath(server->ConfigPath, "shadow"); - - if (!filepath) + if (!(filepath = GetCombinedPath(server->ConfigPath, "shadow"))) return -1; - if (!PathFileExistsA(filepath)) - CreateDirectoryA(filepath, 0); + if (!PathFileExistsA(filepath) && + !CreateDirectoryA(filepath, 0)) + { + WLog_ERR(TAG, "Failed to create directory '%s'", filepath); + free(filepath); + return -1; + } server->CertificateFile = GetCombinedPath(filepath, "shadow.crt"); server->PrivateKeyFile = GetCombinedPath(filepath, "shadow.key"); diff --git a/winpr/libwinpr/pipe/pipe.c b/winpr/libwinpr/pipe/pipe.c index f3dce5da4..98ab3aa0e 100644 --- a/winpr/libwinpr/pipe/pipe.c +++ b/winpr/libwinpr/pipe/pipe.c @@ -584,7 +584,11 @@ HANDLE CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD if (!PathFileExistsA(lpPipePath)) { - CreateDirectoryA(lpPipePath, 0); + if (!CreateDirectoryA(lpPipePath, 0)) + { + free(lpPipePath); + goto out; + } UnixChangeFileMode(lpPipePath, 0xFFFF); } diff --git a/winpr/libwinpr/smartcard/smartcard_inspect.c b/winpr/libwinpr/smartcard/smartcard_inspect.c index ada50b44a..192a48ac1 100644 --- a/winpr/libwinpr/smartcard/smartcard_inspect.c +++ b/winpr/libwinpr/smartcard/smartcard_inspect.c @@ -1275,15 +1275,17 @@ void Inspect_InitLog() if (g_Log) return; - g_Log = WLog_Get("WinSCard"); + if (!PathFileExistsA(filepath)) + if (!CreateDirectoryA(filepath, NULL)) + return; + + if (!(g_Log = WLog_Get("WinSCard"))) + return; WLog_SetLogLevel(g_Log, WLOG_DEBUG); WLog_SetLogAppenderType(g_Log, WLOG_APPENDER_FILE); appender = (wLogFileAppender*) WLog_GetLogAppender(g_Log); - if (!PathFileExistsA(filepath)) - CreateDirectoryA(filepath, NULL); - WLog_FileAppender_SetOutputFileName(g_Log, appender, "WinSCard.txt"); WLog_FileAppender_SetOutputFilePath(g_Log, appender, filepath); diff --git a/winpr/libwinpr/utils/wlog/BinaryAppender.c b/winpr/libwinpr/utils/wlog/BinaryAppender.c index 208891ea9..e37a3dbd7 100644 --- a/winpr/libwinpr/utils/wlog/BinaryAppender.c +++ b/winpr/libwinpr/utils/wlog/BinaryAppender.c @@ -96,7 +96,8 @@ int WLog_BinaryAppender_Open(wLog* log, wLogBinaryAppender* appender) if (!PathFileExistsA(appender->FilePath)) { - CreateDirectoryA(appender->FilePath, 0); + if (!CreateDirectoryA(appender->FilePath, 0)) + return -1; UnixChangeFileMode(appender->FilePath, 0xFFFF); }