Skip to content

Commit 11e2996

Browse files
authored
address codeQL warnings (#598)
* codeql fixes * fix type mismatches * fix pointers in w32_time methods * fixes for codeQL warnings * modify checks for codeql warnings * add comments for codeql suppressions * additional codeql fixes and suppressions * add codeql fixes * add comments for codeql * add comments for codeql * switch from debug to error log messages * fix another merge conflict fix line endings in gss-sspi.c * add null check in channels.c * address PR feedback * address additional review feedback * add CodeQL comments to common code * fix unittest-win32compat * fix unit test * address review feedback * remove suppression
1 parent ed6ba5a commit 11e2996

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+278
-126
lines changed

addr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
396396
if ((mp = strchr(addrbuf, '/')) != NULL) {
397397
*mp = '\0';
398398
mp++;
399-
masklen = strtoul(mp, &cp, 10);
399+
masklen = strtoul(mp, &cp, 10); // CodeQL [SM02313]: strtoul will initialize cp
400400
if (*mp < '0' || *mp > '9' || *cp != '\0' || masklen > 128)
401401
return -1;
402402
}

auth2-passwd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ userauth_passwd(struct ssh *ssh, const char *method)
6666

6767
if (change)
6868
logit("password change not supported");
69-
else if (PRIVSEP(auth_password(ssh, password)) == 1)
69+
else if (PRIVSEP(auth_password(ssh, password)) == 1) // CodeQL [SM01714] false positive: password is null terminated
7070
authenticated = 1;
7171
freezero(password, len);
7272
return authenticated;

channels.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,9 @@ x11_open_helper(struct ssh *ssh, struct sshbuf *b)
11751175
return 0;
11761176

11771177
/* Parse the lengths of variable-length fields. */
1178-
ucp = sshbuf_mutable_ptr(b);
1179-
if (ucp[0] == 0x42) { /* Byte order MSB first. */
1178+
if ((ucp = sshbuf_mutable_ptr(b)) == NULL) // fix CodeQL SM02311
1179+
return 0;
1180+
if (ucp[0] == 0x42) { /* Byte order MSB first. */
11801181
proto_len = 256 * ucp[6] + ucp[7];
11811182
data_len = 256 * ucp[8] + ucp[9];
11821183
} else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
@@ -1291,6 +1292,10 @@ channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
12911292
if (have < len)
12921293
return 0;
12931294
p = sshbuf_ptr(input);
1295+
if (p == NULL) { // fix CodeQL SM02311
1296+
error("channel %d: invalid input", c->self);
1297+
return -1;
1298+
}
12941299

12951300
need = 1;
12961301
/* SOCKS4A uses an invalid IP address 0.0.0.x */
@@ -1324,7 +1329,7 @@ channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
13241329
}
13251330
have = sshbuf_len(input);
13261331
p = sshbuf_ptr(input);
1327-
if (memchr(p, '\0', have) == NULL) {
1332+
if (p == NULL || memchr(p, '\0', have) == NULL) { // fix CodeQL SM02311
13281333
error("channel %d: decode socks4: unterminated user", c->self);
13291334
return -1;
13301335
}
@@ -1342,7 +1347,7 @@ channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
13421347
} else { /* SOCKS4A: two strings */
13431348
have = sshbuf_len(input);
13441349
p = sshbuf_ptr(input);
1345-
if (memchr(p, '\0', have) == NULL) {
1350+
if (p == NULL || memchr(p, '\0', have) == NULL) { // fix CodeQL SM02311
13461351
error("channel %d: decode socks4a: host not nul "
13471352
"terminated", c->self);
13481353
return -1;
@@ -1406,7 +1411,7 @@ channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
14061411

14071412
debug2("channel %d: decode socks5", c->self);
14081413
p = sshbuf_ptr(input);
1409-
if (p[0] != 0x05)
1414+
if (p == NULL || p[0] != 0x05) // fix CodeQL SM02311
14101415
return -1;
14111416
have = sshbuf_len(input);
14121417
if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
@@ -1559,6 +1564,8 @@ channel_pre_dynamic(struct ssh *ssh, Channel *c)
15591564
}
15601565
/* try to guess the protocol */
15611566
p = sshbuf_ptr(c->input);
1567+
if (p == NULL) // fix CodeQL SM02311
1568+
return;
15621569
/* XXX sshbuf_peek_u8? */
15631570
switch (p[0]) {
15641571
case 0x04:
@@ -1621,6 +1628,8 @@ channel_before_prepare_io_rdynamic(struct ssh *ssh, Channel *c)
16211628
return;
16221629
/* try to guess the protocol */
16231630
p = sshbuf_ptr(c->output);
1631+
if (p == NULL) // fix CodeQL SM02311
1632+
return;
16241633
switch (p[0]) {
16251634
case 0x04:
16261635
/* switch input/output for reverse forwarding */

contrib/win32/win32compat/ansiprsr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ ParseANSI(unsigned char * pszBuffer, unsigned char * pszBufferEnd, unsigned char
670670
}
671671
} else if (iParam[0] == 6) {
672672
char * szStatus = GetCursorPositionReport();
673-
if (respbuf != NULL) {
673+
if (szStatus != NULL && respbuf != NULL) {
674674
*respbuf = szStatus;
675675
if (resplen != NULL)
676676
*resplen = strlen(szStatus);

contrib/win32/win32compat/console.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ ConEnterRawMode()
182182
if (FALSE == isConHostParserEnabled || !SetConsoleMode(GetConsoleOutputHandle(), dwAttributes)) /* Windows NT */
183183
isAnsiParsingRequired = TRUE;
184184

185-
GetConsoleScreenBufferInfo(GetConsoleOutputHandle(), &csbi);
185+
BOOL gcsbRet = GetConsoleScreenBufferInfo(GetConsoleOutputHandle(), &csbi);
186186

187187
/* We track the view port, if conpty is not supported */
188188
if (!is_conpty_supported())
@@ -192,6 +192,12 @@ ConEnterRawMode()
192192
* so that the clearscreen will not erase any lines.
193193
*/
194194
if (TRUE == isAnsiParsingRequired) {
195+
if (gcsbRet == 0)
196+
{
197+
dwRet = GetLastError();
198+
error("GetConsoleScreenBufferInfo on GetConsoleOutputHandle() failed with %d", dwRet);
199+
return;
200+
}
195201
SavedViewRect = csbi.srWindow;
196202
debug("console doesn't support the ansi parsing");
197203
} else {
@@ -621,12 +627,17 @@ ConWriteString(char* pszString, int cbString)
621627
if ((needed = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, NULL, 0)) == 0 ||
622628
(utf16 = malloc(needed * sizeof(wchar_t))) == NULL ||
623629
(cnt = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, utf16, needed)) == 0) {
624-
Result = (DWORD)printf_s(pszString);
625-
} else {
630+
const char* pszStringConst = pszString;
631+
Result = (DWORD)printf_s(pszStringConst);
632+
}
633+
else {
626634
if (GetConsoleOutputHandle())
627635
WriteConsoleW(GetConsoleOutputHandle(), utf16, cnt, &Result, 0);
628636
else
629-
Result = (DWORD)wprintf_s(utf16);
637+
{
638+
const wchar_t* utf16Const = utf16;
639+
Result = (DWORD)wprintf_s(utf16Const);
640+
}
630641
}
631642

632643
if (utf16)

contrib/win32/win32compat/fileio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ fileio_readlink(const char *path, char *buf, size_t bufsiz)
11891189
}
11901190

11911191
/* allocate the maximum possible size the reparse buffer size could be */
1192-
reparse_buffer = (PREPARSE_DATA_BUFFER_SYMLINK)malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
1192+
reparse_buffer = (PREPARSE_DATA_BUFFER_SYMLINK)malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); // CodeQL [SM02320]: DeviceIoControl will set reparse_buffer
11931193
if (reparse_buffer == NULL) {
11941194
errno = ENOMEM;
11951195
goto cleanup;

contrib/win32/win32compat/gss-sspi.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,10 @@ gss_acquire_cred(_Out_ OM_uint32 *minor_status, _In_opt_ gss_name_t desired_name
444444
/* determine expiration if requested */
445445
if (time_rec != NULL) {
446446
FILETIME current_time;
447-
SystemTimeToFileTime(&current_time_system, &current_time);
448-
*time_rec = (OM_uint32) (expiry.QuadPart - ((PLARGE_INTEGER)&current_time)->QuadPart) / 10000;
447+
if (SystemTimeToFileTime(&current_time_system, &current_time) != 0)
448+
*time_rec = (OM_uint32)(expiry.QuadPart - ((PLARGE_INTEGER)&current_time)->QuadPart) / 10000;
449+
else
450+
error("SystemTimeToFileTime failed with %d", GetLastError());
449451
}
450452

451453
/* set actual supported mechs if requested */
@@ -597,8 +599,10 @@ gss_init_sec_context(
597599
/* if requested, translate the expiration time to number of second */
598600
if (time_rec != NULL) {
599601
FILETIME current_time;
600-
SystemTimeToFileTime(&current_time_system, &current_time);
601-
*time_rec = (OM_uint32)(expiry.QuadPart - ((PLARGE_INTEGER)&current_time)->QuadPart) / 10000;
602+
if (SystemTimeToFileTime(&current_time_system, &current_time) != 0)
603+
*time_rec = (OM_uint32)(expiry.QuadPart - ((PLARGE_INTEGER)&current_time)->QuadPart) / 10000;
604+
else
605+
error("SystemTimeToFileTime failed with %d", GetLastError());
602606
}
603607

604608
/* if requested, return the supported mechanism oid */
@@ -907,8 +911,10 @@ gss_accept_sec_context(_Out_ OM_uint32 * minor_status, _Inout_opt_ gss_ctx_id_t
907911
/* if requested, translate the expiration time to number of second */
908912
if (time_rec != NULL) {
909913
FILETIME current_time;
910-
SystemTimeToFileTime(&current_time_system, &current_time);
911-
*time_rec = (OM_uint32)(expiry.QuadPart - ((PLARGE_INTEGER)&current_time)->QuadPart) / 10000;
914+
if (SystemTimeToFileTime(&current_time_system, &current_time) != 0)
915+
*time_rec = (OM_uint32)(expiry.QuadPart - ((PLARGE_INTEGER)&current_time)->QuadPart) / 10000;
916+
else
917+
error("SystemTimeToFileTime failed with %d", GetLastError());
912918
}
913919

914920
/* only do checks on the finalized context (no continue needed) */
@@ -1072,6 +1078,11 @@ ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name)
10721078
* onto the next available method.
10731079
*/
10741080
struct passwd * user = getpwnam(name);
1081+
if (user == NULL)
1082+
{
1083+
error("sspi getpwnam failed to get user from user-provided, resolved user '%s'", name);
1084+
return 0;
1085+
}
10751086
if (_stricmp(client->displayname.value, user->pw_name) != 0) {
10761087
/* check failed */
10771088
debug("sspi user '%s' did not match user-provided, resolved user '%s'",

contrib/win32/win32compat/inc/time.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include "crtheaders.h"
22
#include TIME_H
33

4+
#define localtime w32_localtime
5+
#define ctime w32_ctime
6+
47
struct tm *localtime_r(const time_t *, struct tm *);
8+
struct tm *w32_localtime(const time_t* sourceTime);
9+
char *w32_ctime(const time_t* sourceTime);

contrib/win32/win32compat/misc.c

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,10 @@ char*
410410
* stop reading until reach '\n' or the converted utf8 string length is n-1
411411
*/
412412
do {
413-
if (str_tmp)
414-
free(str_tmp);
413+
if (str_tmp) {
414+
free(str_tmp);
415+
str_tmp = NULL;
416+
}
415417
if (fgetws(str_w, 2, stream) == NULL)
416418
goto cleanup;
417419
if ((str_tmp = utf16_to_utf8(str_w)) == NULL) {
@@ -1485,6 +1487,28 @@ localtime_r(const time_t *timep, struct tm *result)
14851487
return localtime_s(result, timep) == 0 ? result : NULL;
14861488
}
14871489

1490+
struct tm *
1491+
w32_localtime(const time_t* sourceTime)
1492+
{
1493+
struct tm* destTime = (struct tm*)malloc(sizeof(struct tm));
1494+
if (destTime == NULL)
1495+
{
1496+
return NULL;
1497+
}
1498+
return localtime_s(destTime, sourceTime) == 0 ? destTime : NULL;
1499+
}
1500+
1501+
char*
1502+
w32_ctime(const time_t* sourceTime)
1503+
{
1504+
char *destTime = malloc(26);
1505+
if (destTime == NULL)
1506+
{
1507+
return NULL;
1508+
}
1509+
return ctime_s(destTime, 26, sourceTime) == 0 ? destTime : NULL;
1510+
}
1511+
14881512
void
14891513
freezero(void *ptr, size_t sz)
14901514
{
@@ -1578,8 +1602,11 @@ am_system()
15781602

15791603
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &proc_token) == FALSE ||
15801604
GetTokenInformation(proc_token, TokenUser, NULL, 0, &info_len) == TRUE ||
1581-
(info = (TOKEN_USER*)malloc(info_len)) == NULL ||
1582-
GetTokenInformation(proc_token, TokenUser, info, info_len, &info_len) == FALSE)
1605+
(info = (TOKEN_USER*)malloc(info_len)) == NULL) {
1606+
fatal("unable to know if I am running as system");
1607+
}
1608+
1609+
if (GetTokenInformation(proc_token, TokenUser, info, info_len, &info_len) == FALSE)
15831610
fatal("unable to know if I am running as system");
15841611

15851612
if (IsWellKnownSid(info->User.Sid, WinLocalSystemSid))
@@ -1609,7 +1636,7 @@ lookup_sid(const wchar_t* name_utf16, PSID psid, DWORD * psid_len)
16091636
wchar_t* name_utf16_modified = NULL;
16101637
BOOL resolveAsAdminsSid = 0, r;
16111638

1612-
LookupAccountNameW(NULL, name_utf16, NULL, &sid_len, dom, &dom_len, &n_use);
1639+
LookupAccountNameW(NULL, name_utf16, NULL, &sid_len, dom, &dom_len, &n_use); // CodeQL [SM02313]: false positive n_use will not be uninitialized
16131640

16141641
if (sid_len == 0 && _wcsicmp(name_utf16, L"administrators") == 0) {
16151642
CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, NULL, &sid_len);
@@ -1663,6 +1690,12 @@ lookup_sid(const wchar_t* name_utf16, PSID psid, DWORD * psid_len)
16631690
debug3_f("local user name is same as machine name");
16641691
size_t name_size = wcslen(name_utf16) * 2U + 2U;
16651692
name_utf16_modified = malloc(name_size * sizeof(wchar_t));
1693+
if (name_utf16_modified == NULL)
1694+
{
1695+
errno = ENOMEM;
1696+
error_f("Failed to allocate memory");
1697+
goto cleanup;
1698+
}
16661699
name_utf16_modified[0] = L'\0';
16671700
wcscat_s(name_utf16_modified, name_size, name_utf16);
16681701
wcscat_s(name_utf16_modified, name_size, L"\\");
@@ -1709,15 +1742,15 @@ get_sid(const char* name)
17091742
}
17101743
else {
17111744
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) == FALSE ||
1712-
GetTokenInformation(token, TokenUser, NULL, 0, &info_len) == TRUE) {
1745+
GetTokenInformation(token, TokenUser, NULL, 0, &info_len) == TRUE) { // CodeQL [SM02320]: GetTokenInformation will initialize info
17131746
errno = EOTHER;
17141747
goto cleanup;
17151748
}
17161749

1717-
if ((info = (TOKEN_USER*)malloc(info_len)) == NULL) {
1750+
if ((info = (TOKEN_USER*)malloc(info_len)) == NULL) {
17181751
errno = ENOMEM;
17191752
goto cleanup;
1720-
}
1753+
};
17211754

17221755
if (GetTokenInformation(token, TokenUser, info, info_len, &info_len) == FALSE) {
17231756
errno = errno_from_Win32LastError();

contrib/win32/win32compat/shell-host.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ ProcessEvent(void *p)
891891
if (child_out == INVALID_HANDLE_VALUE || child_out == NULL)
892892
return ERROR_INVALID_PARAMETER;
893893

894-
GetWindowThreadProcessId(hwnd, &dwProcessId);
894+
GetWindowThreadProcessId(hwnd, &dwProcessId); // CodeQL [SM02313]: false positive dwProcessId will not be uninitialized
895895

896896
if (childProcessId != dwProcessId)
897897
return ERROR_SUCCESS;
@@ -1114,7 +1114,7 @@ QueueEvent(DWORD event, HWND hwnd, LONG idObject, LONG idChild)
11141114
consoleEvent* current = NULL;
11151115

11161116
EnterCriticalSection(&criticalSection);
1117-
current = malloc(sizeof(consoleEvent));
1117+
current = malloc(sizeof(consoleEvent)); // CodeQL [SM02320]: current struct fields initialized below
11181118
if (current) {
11191119
if (!head) {
11201120
current->event = event;
@@ -1128,7 +1128,8 @@ QueueEvent(DWORD event, HWND hwnd, LONG idObject, LONG idChild)
11281128

11291129
head = current;
11301130
tail = current;
1131-
} else {
1131+
}
1132+
else {
11321133
current->event = event;
11331134
current->hwnd = hwnd;
11341135
current->idChild = idChild;

0 commit comments

Comments
 (0)