From f250143f8bc6819f0dcee17cbc8a47db5bd9cefc Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sat, 12 Dec 2020 17:20:18 +0100 Subject: [PATCH] Slightly improved cleansing of the command-line on Win32. --- frontend/src/main.c | 3 +++ frontend/src/utils.c | 31 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/frontend/src/main.c b/frontend/src/main.c index e5f1296..c777ec3 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -21,6 +21,7 @@ #include #include #include +#include /* Const */ #define BUFFER_SIZE 4096U @@ -329,6 +330,7 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co } const size_t padding = sizeof(uint64_t) - (file_size % sizeof(uint64_t)); + assert(padding && (padding <= sizeof(uint64_t))); if (slunkcrypt_random_bytes(buffer, padding) < padding) { FPUTS(T("\n\nSlunkCrypt error: Failed to generate random data!\n\n"), stderr); @@ -503,6 +505,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co } const size_t padding = GET_LOWBITS(buffer[sizeof(uint64_t) - 1U]) + 1U; + assert(padding && (padding <= sizeof(uint64_t))); if (padding != sizeof(uint64_t)) { const size_t count = sizeof(uint64_t) - padding; diff --git a/frontend/src/utils.c b/frontend/src/utils.c index c2494c1..0c435f3 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -48,22 +48,39 @@ // Terminal initialization // ========================================================================== -/* CRT imports */ -#if defined(_WIN32) && !defined(_DLL) +#ifdef _WIN32 +#ifdef _DLL +#define _acmdln GetCommandLineA() +#define _wcmdln GetCommandLineW() +#else extern char *const _acmdln; extern wchar_t *const _wcmdln; #endif +static void clear_cmdline_args(char *const acmdln, wchar_t *const wcmdln) +{ + if (acmdln && acmdln[0U]) + { + const size_t len = strlen(acmdln); + slunkcrypt_bzero(acmdln, len * sizeof(char)); + if (len > 5U) strcpy(acmdln, "slunk"); + } + if (wcmdln && wcmdln[0U]) + { + const size_t len = wcslen(wcmdln); + slunkcrypt_bzero(wcmdln, len * sizeof(wchar_t)); + if (len > 5U) wcscpy(wcmdln, L"slunk"); + } +} +#endif void init_terminal(void) { #ifdef _WIN32 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); - _setmode(_fileno(stdin), _O_BINARY); + _setmode(_fileno(stdout), _O_BINARY); + _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stderr), _O_U8TEXT); -#ifndef _DLL - if (_acmdln) slunkcrypt_bzero(_acmdln, strlen(_acmdln) * sizeof(char)); - if (_wcmdln) slunkcrypt_bzero(_wcmdln, wcslen(_wcmdln) * sizeof(wchar_t)); -#endif + clear_cmdline_args(_acmdln, _wcmdln); #endif }