Skip to content

Commit

Permalink
Windows: Fix C/Rust FFI compat issue + Windows compile warnings
Browse files Browse the repository at this point in the history
Primarily this commit fixes an issue with the size of the parameters
passed to cli_checklimits(). The parameters were "unsigned long", which
varies in size depending on platform.
I've switched them to uint64_t / u64.

While working on this, I observed some concerning warnigns on Windows,
and some less serious ones, primarily regarding inconsistencies with
`const` parameters.

Finally, in `scanmem.c`, there is a warning regarding use of `wchar_t *`
with `GetModuleFileNameEx()` instead of `GetModuleFileNameExW()`.
This made me realize this code assumes we're not defining `UNICODE`,
which would have such macros use the 'A' variant.
I have fixed it the best I can, although I'm still a little
uncomfortable with some of this code that uses `char` or `wchar_t`
instead of TCHAR.

I also remove the `if (GetModuleFileNameEx) {` conditional, because this
macro/function will always be defined. The original code was checking a
function pointer, and so this was a bug when integrating into ClamAV.

Regarding the changes to `rijndael.c`, I found that this module assumes
`unsigned long` == 32bits. It does not.
I have corrected it to use `uint32_t`.
  • Loading branch information
micahsnyder committed Apr 9, 2024
1 parent 1cb7ab4 commit e48dfad
Show file tree
Hide file tree
Showing 30 changed files with 172 additions and 170 deletions.
3 changes: 2 additions & 1 deletion clamd/tcpserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ int tcpserver(int **lsockets, unsigned int *nlsockets, char *ipaddr, const struc
int *sockets;
int sockfd = 0, backlog;
int *t;
char *estr, port[10];
const char *estr = NULL;
char port[10];
int yes = 1;
int res;
unsigned int i = 0;
Expand Down
4 changes: 2 additions & 2 deletions clamd/thrmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ int thrmgr_printstats(int f, char term)
const struct cl_engine **s;
/* new engine */
++seen_cnt;
s = realloc(seen, seen_cnt * sizeof(*seen));
s = realloc((void *)seen, seen_cnt * sizeof(*seen));
if (!s) {
error_flag = 1;
break;
Expand All @@ -285,7 +285,7 @@ int thrmgr_printstats(int f, char term)
}
mdprintf(f, "\n");
}
free(seen);
free((void *)seen);
#ifdef HAVE_MALLINFO
{
struct mallinfo inf = mallinfo();
Expand Down
2 changes: 1 addition & 1 deletion clamdtop/clamdtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static WINDOW *stats_window = NULL;
static WINDOW *status_bar_window = NULL;
static WINDOW *mem_window = NULL;

static const char *status_bar_keys[10];
static char *status_bar_keys[10];
static unsigned maxy = 0, maxx = 0;
static char *queue_header = NULL;
static char *multi_queue_header = NULL;
Expand Down
94 changes: 47 additions & 47 deletions common/scanmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,47 +227,47 @@ int walkmodules_th(proc_callback callback, void *data, struct mem_info *info)
}

/* Check and transform non ANSI filenames to ANSI using altnames */
if (GetModuleFileNameEx) {
HANDLE hFile = CreateFile(
me32.szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);

if (hFile == INVALID_HANDLE_VALUE) {
DWORD err = GetLastError();
wchar_t name[MAX_PATH + 1];
char *converted = NULL;
HANDLE p;

if (err == ERROR_BAD_NETPATH) {
logg(LOGG_WARNING, "Warning scanning files on non-ansi network paths is not "
"supported\n");
logg(LOGG_WARNING, "File: %s\n", me32.szExePath);
continue;
}

if ((err != ERROR_INVALID_NAME) && (err != ERROR_PATH_NOT_FOUND)) {
logg(LOGG_WARNING, "Expected ERROR_INVALID_NAME/ERROR_PATH_NOT_FOUND but got %d\n",
err);
continue;
}

p = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
ps.th32ProcessID);
if (!GetModuleFileNameEx(p, NULL, name, MAX_PATH)) {
logg(LOGG_WARNING, "GetModuleFileNameExW() failed %d\n", GetLastError());
CloseHandle(p);
continue;
}
HANDLE hFile = CreateFileA(
me32.szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);

if (hFile == INVALID_HANDLE_VALUE) {
DWORD err = GetLastError();
wchar_t nameW[MAX_PATH + 1];
char *converted = NULL;
HANDLE p;

if (err == ERROR_BAD_NETPATH) {
logg(LOGG_WARNING, "Warning scanning files on non-ansi network paths is not "
"supported\n");
logg(LOGG_WARNING, "File: %s\n", me32.szExePath);
continue;
}

if ((err != ERROR_INVALID_NAME) && (err != ERROR_PATH_NOT_FOUND)) {
logg(LOGG_WARNING, "Expected ERROR_INVALID_NAME/ERROR_PATH_NOT_FOUND but got %d\n",
err);
continue;
}

p = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,
ps.th32ProcessID);

if (!GetModuleFileNameExW(p, NULL, nameW, MAX_PATH)) {
logg(LOGG_WARNING, "GetModuleFileNameExW() failed %d\n", GetLastError());
CloseHandle(p);
continue;
}
CloseHandle(p);

if (!(converted = getaltpath(name))) {
logg(LOGG_WARNING, "Cannot map filename to ANSI codepage\n");
continue;
}
strcpy(me32.szExePath, converted);
free(converted);
} else
CloseHandle(hFile);
if (!(converted = getaltpath(nameW))) {
logg(LOGG_WARNING, "Cannot map filename to ANSI codepage\n");
continue;
}
strcpy(me32.szExePath, converted);
free(converted);
} else {
CloseHandle(hFile);
}

do
Expand Down Expand Up @@ -319,8 +319,8 @@ int walkmodules_psapi(proc_callback callback, void *data, struct mem_info *info)
continue;
}

if (!GetModuleBaseName(hProc, mods[0], ps.szExeFile,
MAX_PATH - 1)) {
if (!GetModuleBaseNameA(hProc, mods[0], ps.szExeFile,
MAX_PATH - 1)) {
CloseHandle(hProc);
continue;
}
Expand Down Expand Up @@ -515,8 +515,8 @@ int dump_pe(const char *filename, PROCESSENTRY32 ProcStruct,
/* PE Realignment */
align_pe(buffer, me32.modBaseSize);

hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
logg(LOGG_INFO, "Error creating %s\n", filename);
free(buffer);
Expand Down Expand Up @@ -598,7 +598,7 @@ int scanmem_cb(PROCESSENTRY32 ProcStruct, MODULEENTRY32 me32, void *data, struct
snprintf(expandmodule, MAX_PATH - 1, "%%SystemRoot%%\\%s",
&me32.szExePath[12]);
expandmodule[MAX_PATH - 1] = 0;
ExpandEnvironmentStrings(expandmodule, modulename, MAX_PATH - 1);
ExpandEnvironmentStringsA(expandmodule, modulename, MAX_PATH - 1);
modulename[MAX_PATH - 1] = 0;
}

Expand Down Expand Up @@ -630,7 +630,7 @@ int scanmem_cb(PROCESSENTRY32 ProcStruct, MODULEENTRY32 me32, void *data, struct
if ((fd = dump_pe(dumped, ProcStruct, me32)) > 0) {
close(fd);
scan_data->res = scanfile(dumped, scan_data, info);
DeleteFile(dumped);
DeleteFileA(dumped);
}
free(dumped);
}
Expand Down Expand Up @@ -667,8 +667,8 @@ int scanmem(struct mem_info *info)
data.processes = 0;
data.modules = 0;

HMODULE psapi_ok = LoadLibrary("psapi.dll");
HMODULE k32_ok = LoadLibrary("kernel32.dll");
HMODULE psapi_ok = LoadLibraryA("psapi.dll");
HMODULE k32_ok = LoadLibraryA("kernel32.dll");

if (!(psapi_ok || k32_ok)) {
logg(LOGG_INFO, " *** Memory Scanning is not supported on this OS ***\n\n");
Expand Down
2 changes: 1 addition & 1 deletion common/win/cert_util_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ cl_error_t cert_store_load(X509 **trusted_certs, size_t trusted_cert_count)
cert_store_t *store = NULL;
bool locked = false;

hStore = CertOpenSystemStoreA(NULL, "ROOT");
hStore = CertOpenSystemStoreA(0, "ROOT");
if (NULL == hStore) {
mprintf(LOGG_ERROR, "Failed to open system certificate store.\n");
goto done;
Expand Down
4 changes: 2 additions & 2 deletions libclamav/dmg.c
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ static int dmg_stripe_bzip(cli_ctx *ctx, int fd, uint32_t index, struct dmg_mish
break;
}

ret = cli_checklimits("dmg_stripe_bzip", ctx, (unsigned long)(size_so_far + sizeof(obuf)), 0, 0);
ret = cli_checklimits("dmg_stripe_bzip", ctx, size_so_far + sizeof(obuf), 0, 0);
if (ret != CL_CLEAN) {
break;
}
Expand Down Expand Up @@ -953,7 +953,7 @@ static int dmg_stripe_bzip(cli_ctx *ctx, int fd, uint32_t index, struct dmg_mish
size_so_far += next_write;
dmg_bzipmsg("dmg_stripe_bzip: size_so_far: " STDu64 " next_write: %zu\n", size_so_far, next_write);

ret = cli_checklimits("dmg_stripe_bzip", ctx, (unsigned long)(size_so_far + sizeof(obuf)), 0, 0);
ret = cli_checklimits("dmg_stripe_bzip", ctx, size_so_far + sizeof(obuf), 0, 0);
if (ret != CL_CLEAN) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion libclamav/hfsplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ static cl_error_t hfsplus_scanfile(cli_ctx *ctx, hfsPlusVolumeHeader *volHeader,
goto done;
}
#endif
status = cli_checklimits("hfsplus_scanfile", ctx, (unsigned long)targetSize, 0, 0);
status = cli_checklimits("hfsplus_scanfile", ctx, targetSize, 0, 0);
if (status != CL_SUCCESS) {
goto done;
}
Expand Down
2 changes: 1 addition & 1 deletion libclamav/jsparse/js-norm.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ static void decode_de(yystype *params[], struct text_buffer *txtbuf)
else
textbuffer_append(txtbuf, tokens[val]);
} while (*p);
free(tokens);
free((void *)tokens);
textbuffer_append(txtbuf, "\0");
}

Expand Down
8 changes: 4 additions & 4 deletions libclamav/matcher-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ int hm_addhash_bin(struct cli_matcher *root, const void *binhash, cli_hash_type_
if (!szh->hash_array) {
cli_errmsg("hm_addhash_bin: failed to grow hash array to %u entries\n", szh->items);
szh->items = 0;
MPOOL_FREE(root->mempool, szh->virusnames);
MPOOL_FREE(root->mempool, (void *)szh->virusnames);
szh->virusnames = NULL;
return CL_EMEM;
}

szh->virusnames = MPOOL_REALLOC2(root->mempool, szh->virusnames, sizeof(*szh->virusnames) * szh->items);
szh->virusnames = MPOOL_REALLOC2(root->mempool, (void *)szh->virusnames, sizeof(*szh->virusnames) * szh->items);
if (!szh->virusnames) {
cli_errmsg("hm_addhash_bin: failed to grow virusname array to %u entries\n", szh->items);
szh->items = 0;
Expand Down Expand Up @@ -319,7 +319,7 @@ void hm_free(struct cli_matcher *root)
MPOOL_FREE(root->mempool, szh->hash_array);
while (szh->items)
MPOOL_FREE(root->mempool, (void *)szh->virusnames[--szh->items]);
MPOOL_FREE(root->mempool, szh->virusnames);
MPOOL_FREE(root->mempool, (void *)szh->virusnames);
MPOOL_FREE(root->mempool, szh);
}
CLI_HTU32_FREE(ht, root->mempool);
Expand All @@ -334,6 +334,6 @@ void hm_free(struct cli_matcher *root)
MPOOL_FREE(root->mempool, szh->hash_array);
while (szh->items)
MPOOL_FREE(root->mempool, (void *)szh->virusnames[--szh->items]);
MPOOL_FREE(root->mempool, szh->virusnames);
MPOOL_FREE(root->mempool, (void *)szh->virusnames);
}
}
6 changes: 3 additions & 3 deletions libclamav/ole2_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ static cl_error_t handler_otf_encrypted(ole2_header_t *hdr, property_t *prop, co
int nrounds = 0;
uint8_t *decryptDst = NULL;
encryption_key_t *key = (encryption_key_t *)handler_ctx;
uint64_t *rk = NULL;
uint32_t *rk = NULL;
uint32_t bytesRead = 0;
uint64_t actualFileLength;
uint64_t bytesWritten = 0;
Expand All @@ -1746,7 +1746,7 @@ static cl_error_t handler_otf_encrypted(ole2_header_t *hdr, property_t *prop, co
goto done;
}

CLI_MAX_MALLOC_OR_GOTO_DONE(rk, RKLENGTH(key->key_length_bits) * sizeof(uint64_t), ret = CL_EMEM);
CLI_MAX_MALLOC_OR_GOTO_DONE(rk, RKLENGTH(key->key_length_bits) * sizeof(uint32_t), ret = CL_EMEM);

print_ole2_property(prop);

Expand Down Expand Up @@ -2143,7 +2143,7 @@ static cl_error_t generate_key_aes(const char *const password, encryption_key_t

static bool aes_128ecb_decrypt(const unsigned char *in, size_t length, unsigned char *out, const encryption_key_t *const key)
{
uint64_t rk[RKLENGTH(128)];
uint32_t rk[RKLENGTH(128)];
int nrounds;
size_t i;
bool bRet = false;
Expand Down
10 changes: 5 additions & 5 deletions libclamav/others.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ static void *get_module_function(HMODULE handle, const char *name)
NULL,
lasterr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPCSTR)&err,
(LPSTR)&err,
0,
NULL);
}
Expand Down Expand Up @@ -1132,10 +1132,10 @@ void cli_append_potentially_unwanted_if_heur_exceedsmax(cli_ctx *ctx, char *vnam
}
}

cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, unsigned long need2, unsigned long need3)
cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, uint64_t need1, uint64_t need2, uint64_t need3)
{
cl_error_t ret = CL_SUCCESS;
unsigned long needed;
uint64_t needed;

if (!ctx) {
/* if called without limits, go on, unpack, scan */
Expand All @@ -1156,7 +1156,7 @@ cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, u
/* Enforce global scan-size limit, if limit enabled */
if (needed && (ctx->engine->maxscansize != 0) && (ctx->engine->maxscansize - ctx->scansize < needed)) {
/* The size needed is greater than the remaining scansize ... Skip this file. */
cli_dbgmsg("%s: scansize exceeded (initial: %lu, consumed: %lu, needed: %lu)\n", who, (unsigned long int)ctx->engine->maxscansize, (unsigned long int)ctx->scansize, needed);
cli_dbgmsg("%s: scansize exceeded (initial: %lu, consumed: %lu, needed: %lu)\n", who, ctx->engine->maxscansize, ctx->scansize, needed);
ret = CL_EMAXSIZE;
cli_append_potentially_unwanted_if_heur_exceedsmax(ctx, "Heuristics.Limits.Exceeded.MaxScanSize");
goto done;
Expand All @@ -1165,7 +1165,7 @@ cl_error_t cli_checklimits(const char *who, cli_ctx *ctx, unsigned long need1, u
/* Enforce per-file file-size limit, if limit enabled */
if (needed && (ctx->engine->maxfilesize != 0) && (ctx->engine->maxfilesize < needed)) {
/* The size needed is greater than that limit ... Skip this file. */
cli_dbgmsg("%s: filesize exceeded (allowed: %lu, needed: %lu)\n", who, (unsigned long int)ctx->engine->maxfilesize, needed);
cli_dbgmsg("%s: filesize exceeded (allowed: %lu, needed: %lu)\n", who, ctx->engine->maxfilesize, needed);
ret = CL_EMAXSIZE;
cli_append_potentially_unwanted_if_heur_exceedsmax(ctx, "Heuristics.Limits.Exceeded.MaxFileSize");
goto done;
Expand Down
Loading

0 comments on commit e48dfad

Please sign in to comment.