Skip to content

Commit a50752e

Browse files
committed
Fix possible heap overflow in SSH_MSG_CHANNEL_REQUEST libssh2#1815
1 parent 02da2ec commit a50752e

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

src/packet.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,21 +1139,38 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
11391139

11401140
case SSH_MSG_CHANNEL_REQUEST:
11411141
if(datalen >= 9) {
1142-
channel = _libssh2_ntohu32(data + 1);
1143-
len = _libssh2_ntohu32(data + 5);
1144-
want_reply = 1;
1142+
unsigned char *request;
1143+
size_t r_len;
1144+
struct string_buf buf;
1145+
buf.data = data;
1146+
buf.dataptr = buf.data;
1147+
buf.len = datalen;
11451148

1146-
if((len + 9) < datalen)
1147-
want_reply = data[len + 9];
1149+
buf.dataptr++; /* Advance past packet type */
1150+
1151+
if(_libssh2_get_u32(&buf, &channel)) {
1152+
return _libssh2_error(session, LIBSSH2_ERROR_PROTO,
1153+
"Unexpected channel value.");
1154+
}
1155+
if(_libssh2_get_string(&buf, &request, &r_len)) {
1156+
return _libssh2_error(session, LIBSSH2_ERROR_PROTO,
1157+
"Unexpected request value.");
1158+
}
1159+
1160+
len = (uint32_t)r_len;
1161+
1162+
if(_libssh2_get_byte(&buf, &want_reply)) {
1163+
return _libssh2_error(session, LIBSSH2_ERROR_PROTO,
1164+
"Unexpected want reply value.");
1165+
}
11481166

11491167
_libssh2_debug((session,
11501168
LIBSSH2_TRACE_CONN,
11511169
"Channel %u received request type %.*s (wr %X)",
1152-
channel, (int)len, data + 9, want_reply));
1170+
channel, (int)len, request, want_reply));
11531171

11541172
if(len == strlen("exit-status") &&
1155-
(strlen("exit-status") + 9) <= datalen &&
1156-
!memcmp("exit-status", data + 9, strlen("exit-status"))) {
1173+
!memcmp("exit-status", request, strlen("exit-status"))) {
11571174

11581175
/* we've got "exit-status" packet. Set the session value */
11591176
if(datalen >= 20)
@@ -1174,8 +1191,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
11741191

11751192
}
11761193
else if(len == strlen("exit-signal") &&
1177-
(strlen("exit-signal") + 9) <= datalen &&
1178-
!memcmp("exit-signal", data + 9,
1194+
!memcmp("exit-signal", request,
11791195
strlen("exit-signal"))) {
11801196
/* command terminated due to signal */
11811197
if(datalen >= 20)

0 commit comments

Comments
 (0)