Skip to content

Commit b32fd48

Browse files
committed
Fix String References
1. When looking up a name in the name-ID map, don't bother checking if the name is NULL. 2. The GetString functions should fail if the destination string parameter is NULL. 3. The GetString functions should not bother to copy an empty string found in the data stream. 4. When checking the public key type provided by the callback, do not check it when it is NULL.
1 parent fb54473 commit b32fd48

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/internal.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,6 +2827,9 @@ byte NameToId(const char* name, word32 nameSz)
28272827
byte id = ID_UNKNOWN;
28282828
word32 i;
28292829

2830+
if (name == NULL)
2831+
return id;
2832+
28302833
for (i = 0; i < (sizeof(NameIdMap)/sizeof(NameIdPair)); i++) {
28312834
if (nameSz == (word32)WSTRLEN(NameIdMap[i].name) &&
28322835
XMEMCMP(name, NameIdMap[i].name, nameSz) == 0) {
@@ -3582,14 +3585,20 @@ int GetMpint(word32* mpintSz, const byte** mpint,
35823585
* the provided buffer, and terminates it with a NULL. */
35833586
int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
35843587
{
3585-
int result;
3588+
int result = WS_SUCCESS;
35863589
word32 strSz;
35873590
const byte* str;
35883591

3589-
result = GetStringRef(&strSz, &str, buf, len, idx);
3592+
if (s == NULL)
3593+
result = WS_BAD_ARGUMENT;
3594+
3595+
if (result == WS_SUCCESS)
3596+
result = GetStringRef(&strSz, &str, buf, len, idx);
3597+
35903598
if (result == WS_SUCCESS) {
35913599
*sSz = (strSz >= *sSz) ? *sSz - 1 : strSz; /* -1 for null char */
3592-
WMEMCPY(s, str, *sSz);
3600+
if (strSz && str)
3601+
WMEMCPY(s, str, *sSz);
35933602
s[*sSz] = 0;
35943603
}
35953604

@@ -3602,22 +3611,24 @@ int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
36023611
int GetStringAlloc(void* heap, char** s, word32* sSz,
36033612
const byte* buf, word32 len, word32 *idx)
36043613
{
3605-
int result;
3614+
int result = WS_SUCCESS;
36063615
const byte *str;
3616+
char* newStr;
36073617
word32 strSz;
36083618

3609-
if (s == NULL) {
3610-
return WS_BAD_ARGUMENT;
3611-
}
3619+
if (s == NULL)
3620+
result = WS_BAD_ARGUMENT;
36123621

3613-
result = GetStringRef(&strSz, &str, buf, len, idx);
3614-
if (result == WS_SUCCESS) {
3615-
char* newStr;
3622+
if (result == WS_SUCCESS)
3623+
result = GetStringRef(&strSz, &str, buf, len, idx);
36163624

3625+
if (result == WS_SUCCESS) {
36173626
newStr = (char*)WMALLOC(strSz + 1, heap, DYNTYPE_STRING);
36183627
if (newStr == NULL)
3619-
return WS_MEMORY_E;
3628+
result = WS_MEMORY_E;
3629+
}
36203630

3631+
if (result == WS_SUCCESS) {
36213632
if (strSz > 0 && str)
36223633
WMEMCPY(newStr, str, strSz);
36233634
newStr[strSz] = 0;
@@ -3638,9 +3649,14 @@ int GetStringAlloc(void* heap, char** s, word32* sSz,
36383649
int GetStringRef(word32* strSz, const byte** str,
36393650
const byte* buf, word32 len, word32* idx)
36403651
{
3641-
int result;
3652+
int result = WS_SUCCESS;
3653+
3654+
if (str == NULL)
3655+
result = WS_BAD_ARGUMENT;
3656+
3657+
if (result == WS_SUCCESS)
3658+
result = GetUint32(strSz, buf, len, idx);
36423659

3643-
result = GetUint32(strSz, buf, len, idx);
36443660
if (result == WS_SUCCESS) {
36453661
if (*idx <= len && *strSz <= len - *idx) {
36463662
if (*strSz) {
@@ -6907,8 +6923,9 @@ static int DoUserAuthRequestRsa(WOLFSSH* ssh, WS_UserAuthData_PublicKey* pk,
69076923
}
69086924

69096925
if (ret == WS_SUCCESS) {
6910-
if (publicKeyTypeSz != 7 &&
6911-
WMEMCMP(publicKeyType, "ssh-rsa", 7) != 0) {
6926+
if (publicKeyTypeSz != 7
6927+
&& (publicKeyType == NULL
6928+
|| WMEMCMP(publicKeyType, "ssh-rsa", 7) != 0)) {
69126929

69136930
WLOG(WS_LOG_DEBUG,
69146931
"Public Key's type does not match public key type");
@@ -6947,7 +6964,9 @@ static int DoUserAuthRequestRsa(WOLFSSH* ssh, WS_UserAuthData_PublicKey* pk,
69476964

69486965
if (ret == WS_SUCCESS) {
69496966
if (publicKeyTypeSz != pk->publicKeyTypeSz &&
6950-
WMEMCMP(publicKeyType, pk->publicKeyType, publicKeyTypeSz) != 0) {
6967+
(publicKeyType == NULL
6968+
|| WMEMCMP(publicKeyType, pk->publicKeyType,
6969+
publicKeyTypeSz) != 0)) {
69516970

69526971
WLOG(WS_LOG_DEBUG,
69536972
"Signature's type does not match public key type");

0 commit comments

Comments
 (0)