Skip to content

Commit 8461e9d

Browse files
committed
Coverity: Untrusted divisor
1. The individual bytes of the value read by ato32() are promoted to int values. Added typecasts to word32 for each of the bytes of the 32-bit value so they are treated as unsigned values like the target type. Also shifted each byte separately after masking them and then oring them into a temp. 2. To get the e value from the KexDhInit message, use the GetStringRef() function. Fixes CID: 572837
1 parent 7d48298 commit 8461e9d

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/internal.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4751,7 +4751,7 @@ static int DoKexDhInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
47514751
* in the message isn't of the DH e value. Treat the Q as e. */
47524752
/* DYNTYPE_DH */
47534753

4754-
byte* e;
4754+
const byte* e;
47554755
word32 eSz;
47564756
word32 begin;
47574757
int ret = WS_SUCCESS;
@@ -4770,24 +4770,12 @@ static int DoKexDhInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx)
47704770
*idx += len;
47714771
return WS_SUCCESS;
47724772
}
4773-
}
47744773

4775-
if (ret == WS_SUCCESS) {
47764774
begin = *idx;
4777-
ret = GetUint32(&eSz, buf, len, &begin);
4778-
}
4779-
4780-
if (ret == WS_SUCCESS) {
4781-
/* Validate eSz */
4782-
if ((len < begin) || (eSz > len - begin)) {
4783-
ret = WS_RECV_OVERFLOW_E;
4784-
}
4775+
ret = GetStringRef(&eSz, &e, buf, len, &begin);
47854776
}
47864777

47874778
if (ret == WS_SUCCESS) {
4788-
e = buf + begin;
4789-
begin += eSz;
4790-
47914779
if (eSz <= (word32)sizeof(ssh->handshake->e)) {
47924780
WMEMCPY(ssh->handshake->e, e, eSz);
47934781
ssh->handshake->eSz = eSz;

src/misc.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ STATIC INLINE word32 min(word32 a, word32 b)
7474
/* convert opaque to 32 bit integer */
7575
STATIC INLINE void ato32(const byte* c, word32* u32)
7676
{
77-
*u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
77+
word32 v = 0;
78+
79+
v |= (word32)(c[0] & 0xFF);
80+
v <<= 8;
81+
v |= (word32)(c[1] & 0xFF);
82+
v <<= 8;
83+
v |= (word32)(c[2] & 0xFF);
84+
v <<= 8;
85+
v |= (word32)(c[3] & 0xFF);
86+
87+
*u32 = v;
7888
}
7989

8090

0 commit comments

Comments
 (0)