Merge pull request #2601 from nfedera/fix-2015-05-07-01

Fix some unchecked create calls
This commit is contained in:
Hardening 2015-05-07 19:06:59 +02:00
commit a88adcbb7e
11 changed files with 142 additions and 84 deletions

View File

@ -454,7 +454,11 @@ int main(int argc, char* argv[])
setlocale(LC_ALL, "");
g_sem = CreateSemaphore(NULL, 0, 1, NULL);
if (!(g_sem = CreateSemaphore(NULL, 0, 1, NULL)))
{
WLog_ERR(TAG, "Failed to create semaphore");
exit(1);
}
instance = freerdp_new();
instance->PreConnect = df_pre_connect;

View File

@ -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;

View File

@ -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;
}

View File

@ -142,11 +142,14 @@ int wf_rdpsnd_unlock()
BOOL wf_peer_rdpsnd_init(wfPeerContext* context)
{
wfInfo* wfi;
wfInfo* wfi = wf_info_get_instance();
wfi = wf_info_get_instance();
if (!wfi)
return FALSE;
if (!(wfi->snd_mutex = CreateMutex(NULL, FALSE, NULL)))
return FALSE;
wfi->snd_mutex = CreateMutex(NULL, FALSE, NULL);
context->rdpsnd = rdpsnd_server_context_new(context->vcm);
context->rdpsnd->data = context;

View File

@ -434,16 +434,26 @@ int shadow_server_init_config_path(rdpShadowServer* server)
if (userLibraryPath)
{
if (!PathFileExistsA(userLibraryPath))
CreateDirectoryA(userLibraryPath, 0);
if (!PathFileExistsA(userLibraryPath) &&
!CreateDirectoryA(userLibraryPath, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", userLibraryPath);
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 +471,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 +505,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");

View File

@ -253,31 +253,36 @@ PVOID InterlockedCompareExchangePointer(PVOID volatile *Destination, PVOID Excha
static volatile HANDLE mutex = NULL;
int static_mutex_lock(volatile HANDLE* static_mutex)
BOOL static_mutex_lock(volatile HANDLE* static_mutex)
{
if (*static_mutex == NULL)
{
HANDLE handle = CreateMutex(NULL, FALSE, NULL);
HANDLE handle;
if (!(handle = CreateMutex(NULL, FALSE, NULL)))
return FALSE;
if (InterlockedCompareExchangePointer((PVOID*) static_mutex, (PVOID) handle, NULL) != NULL)
CloseHandle(handle);
}
return (WaitForSingleObject(*static_mutex, INFINITE) == WAIT_FAILED);
return (WaitForSingleObject(*static_mutex, INFINITE) == WAIT_OBJECT_0);
}
LONGLONG InterlockedCompareExchange64(LONGLONG volatile *Destination, LONGLONG Exchange, LONGLONG Comperand)
{
LONGLONG previousValue = 0;
static_mutex_lock(&mutex);
BOOL locked = static_mutex_lock(&mutex);
previousValue = *Destination;
if (*Destination == Comperand)
*Destination = Exchange;
ReleaseMutex(mutex);
if (locked)
ReleaseMutex(mutex);
else
fprintf(stderr, "WARNING: InterlockedCompareExchange64 operation might have failed\n");
return previousValue;
}

View File

@ -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);
}

View File

@ -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);

View File

@ -55,6 +55,12 @@ int TestThreadCreateProcess(int argc, char* argv[])
&StartupInfo,
&ProcessInformation);
if (!status)
{
printf("CreateProcess failed. error=%d\n", GetLastError());
return 1;
}
FreeEnvironmentStrings(lpszEnvironmentBlock);
WaitForSingleObject(ProcessInformation.hProcess, INFINITE);

View File

@ -60,12 +60,15 @@ static void _winpr_openssl_locking(int mode, int type, const char* file, int lin
static struct CRYPTO_dynlock_value* _winpr_openssl_dynlock_create(const char* file, int line)
{
struct CRYPTO_dynlock_value* dynlock = (struct CRYPTO_dynlock_value*)
malloc(sizeof(struct CRYPTO_dynlock_value));
struct CRYPTO_dynlock_value* dynlock;
if (dynlock)
if (!(dynlock = (struct CRYPTO_dynlock_value*) malloc(sizeof(struct CRYPTO_dynlock_value))))
return NULL;
if (!(dynlock->mutex = CreateMutex(NULL, FALSE, NULL)))
{
dynlock->mutex = CreateMutex(NULL, FALSE, NULL);
free(dynlock);
return NULL;
}
return dynlock;

View File

@ -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);
}