Added extra TLS memory boundary checks
This commit is contained in:
+63
-57
@@ -390,77 +390,83 @@ static void processCertificateElements(struct ndpi_detection_module_struct *ndpi
|
|||||||
|
|
||||||
i += 3 /* skip the initial patten 55 1D 11 */;
|
i += 3 /* skip the initial patten 55 1D 11 */;
|
||||||
i++; /* skip the first type, 0x04 == BIT STRING, and jump to it's length */
|
i++; /* skip the first type, 0x04 == BIT STRING, and jump to it's length */
|
||||||
i += (packet->payload[i] & 0x80) ? (packet->payload[i] & 0x7F) : 0; /* skip BIT STRING length */
|
if(i < packet->payload_packet_len) {
|
||||||
i += 2; /* skip the second type, 0x30 == SEQUENCE, and jump to it's length */
|
i += (packet->payload[i] & 0x80) ? (packet->payload[i] & 0x7F) : 0; /* skip BIT STRING length */
|
||||||
i += (packet->payload[i] & 0x80) ? (packet->payload[i] & 0x7F) : 0; /* skip SEQUENCE length */
|
if(i < packet->payload_packet_len) {
|
||||||
i++;
|
i += 2; /* skip the second type, 0x30 == SEQUENCE, and jump to it's length */
|
||||||
|
if(i < packet->payload_packet_len) {
|
||||||
|
i += (packet->payload[i] & 0x80) ? (packet->payload[i] & 0x7F) : 0; /* skip SEQUENCE length */
|
||||||
|
i++;
|
||||||
|
|
||||||
while(i < packet->payload_packet_len) {
|
while(i < packet->payload_packet_len) {
|
||||||
if(packet->payload[i] == 0x82) {
|
if(packet->payload[i] == 0x82) {
|
||||||
if((i < (packet->payload_packet_len - 1))
|
if((i < (packet->payload_packet_len - 1))
|
||||||
&& ((i + packet->payload[i + 1] + 2) < packet->payload_packet_len)) {
|
&& ((i + packet->payload[i + 1] + 2) < packet->payload_packet_len)) {
|
||||||
u_int8_t len = packet->payload[i + 1];
|
u_int8_t len = packet->payload[i + 1];
|
||||||
char dNSName[256];
|
char dNSName[256];
|
||||||
|
|
||||||
i += 2;
|
i += 2;
|
||||||
|
|
||||||
/* The check "len > sizeof(dNSName) - 1" will be always false. If we add it,
|
/* The check "len > sizeof(dNSName) - 1" will be always false. If we add it,
|
||||||
the compiler is smart enough to detect it and throws a warning */
|
the compiler is smart enough to detect it and throws a warning */
|
||||||
if(len == 0 /* Looks something went wrong */)
|
if(len == 0 /* Looks something went wrong */)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
strncpy(dNSName, (const char*)&packet->payload[i], len);
|
strncpy(dNSName, (const char*)&packet->payload[i], len);
|
||||||
dNSName[len] = '\0';
|
dNSName[len] = '\0';
|
||||||
|
|
||||||
cleanupServerName(dNSName, len);
|
cleanupServerName(dNSName, len);
|
||||||
|
|
||||||
#if DEBUG_TLS
|
#if DEBUG_TLS
|
||||||
printf("[TLS] dNSName %s [%s]\n", dNSName, flow->protos.stun_ssl.ssl.client_requested_server_name);
|
printf("[TLS] dNSName %s [%s]\n", dNSName, flow->protos.stun_ssl.ssl.client_requested_server_name);
|
||||||
#endif
|
#endif
|
||||||
if(matched_name == 0) {
|
if(matched_name == 0) {
|
||||||
if((dNSName[0] == '*') && strstr(flow->protos.stun_ssl.ssl.client_requested_server_name, &dNSName[1]))
|
if((dNSName[0] == '*') && strstr(flow->protos.stun_ssl.ssl.client_requested_server_name, &dNSName[1]))
|
||||||
matched_name = 1;
|
matched_name = 1;
|
||||||
else if(strcmp(flow->protos.stun_ssl.ssl.client_requested_server_name, dNSName) == 0)
|
else if(strcmp(flow->protos.stun_ssl.ssl.client_requested_server_name, dNSName) == 0)
|
||||||
matched_name = 1;
|
matched_name = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(flow->protos.stun_ssl.ssl.server_names == NULL)
|
if(flow->protos.stun_ssl.ssl.server_names == NULL)
|
||||||
flow->protos.stun_ssl.ssl.server_names = ndpi_strdup(dNSName),
|
flow->protos.stun_ssl.ssl.server_names = ndpi_strdup(dNSName),
|
||||||
flow->protos.stun_ssl.ssl.server_names_len = strlen(dNSName);
|
flow->protos.stun_ssl.ssl.server_names_len = strlen(dNSName);
|
||||||
else {
|
else {
|
||||||
u_int16_t dNSName_len = strlen(dNSName);
|
u_int16_t dNSName_len = strlen(dNSName);
|
||||||
u_int16_t newstr_len = flow->protos.stun_ssl.ssl.server_names_len + dNSName_len + 1;
|
u_int16_t newstr_len = flow->protos.stun_ssl.ssl.server_names_len + dNSName_len + 1;
|
||||||
char *newstr = (char*)ndpi_realloc(flow->protos.stun_ssl.ssl.server_names,
|
char *newstr = (char*)ndpi_realloc(flow->protos.stun_ssl.ssl.server_names,
|
||||||
flow->protos.stun_ssl.ssl.server_names_len+1, newstr_len+1);
|
flow->protos.stun_ssl.ssl.server_names_len+1, newstr_len+1);
|
||||||
|
|
||||||
if(newstr) {
|
if(newstr) {
|
||||||
flow->protos.stun_ssl.ssl.server_names = newstr;
|
flow->protos.stun_ssl.ssl.server_names = newstr;
|
||||||
flow->protos.stun_ssl.ssl.server_names[flow->protos.stun_ssl.ssl.server_names_len] = ',';
|
flow->protos.stun_ssl.ssl.server_names[flow->protos.stun_ssl.ssl.server_names_len] = ',';
|
||||||
strncpy(&flow->protos.stun_ssl.ssl.server_names[flow->protos.stun_ssl.ssl.server_names_len+1],
|
strncpy(&flow->protos.stun_ssl.ssl.server_names[flow->protos.stun_ssl.ssl.server_names_len+1],
|
||||||
dNSName, dNSName_len+1);
|
dNSName, dNSName_len+1);
|
||||||
flow->protos.stun_ssl.ssl.server_names[newstr_len] = '\0';
|
flow->protos.stun_ssl.ssl.server_names[newstr_len] = '\0';
|
||||||
flow->protos.stun_ssl.ssl.server_names_len = newstr_len;
|
flow->protos.stun_ssl.ssl.server_names_len = newstr_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!flow->l4.tcp.tls.subprotocol_detected)
|
||||||
|
if(ndpi_match_hostname_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TLS, dNSName, len))
|
||||||
|
flow->l4.tcp.tls.subprotocol_detected = 1;
|
||||||
|
|
||||||
|
i += len;
|
||||||
|
} else {
|
||||||
|
#if DEBUG_TLS
|
||||||
|
printf("[TLS] Leftover %u bytes", packet->payload_packet_len - i);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
} /* while */
|
||||||
|
|
||||||
if(!flow->l4.tcp.tls.subprotocol_detected)
|
if(!matched_name)
|
||||||
if(ndpi_match_hostname_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TLS, dNSName, len))
|
NDPI_SET_BIT(flow->risk, NDPI_TLS_CERTIFICATE_MISMATCH); /* Certificate mismatch */
|
||||||
flow->l4.tcp.tls.subprotocol_detected = 1;
|
|
||||||
|
|
||||||
i += len;
|
|
||||||
} else {
|
|
||||||
#if DEBUG_TLS
|
|
||||||
printf("[TLS] Leftover %u bytes", packet->payload_packet_len - i);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} /* while */
|
}
|
||||||
|
|
||||||
if(!matched_name)
|
|
||||||
NDPI_SET_BIT(flow->risk, NDPI_TLS_CERTIFICATE_MISMATCH); /* Certificate mismatch */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user