Files
nDPI/src/lib/protocols/tinc.c
T
Ivan Nardi 06a0abb6fd TINC: fix invalid memory read (#1512)
```
=================================================================
==19324==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60600061be96 at pc 0x55b4a4cb4460 bp 0x7ffc7b461a70 sp 0x7ffc7b461a68
READ of size 1 at 0x60600061be96 thread T0
    #0 0x55b4a4cb445f in ndpi_check_tinc /home/ivan/svnrepos/nDPI/src/lib/protocols/tinc.c:105:9
    #1 0x55b4a4cb1888 in ndpi_search_tinc /home/ivan/svnrepos/nDPI/src/lib/protocols/tinc.c:135:5
    #2 0x55b4a4b4a6e1 in check_ndpi_detection_func /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:5013:6
    #3 0x55b4a4b4c2d4 in check_ndpi_tcp_flow_func /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:5084:12
    #4 0x55b4a4b4bf77 in ndpi_check_flow_func /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:5103:12
    #5 0x55b4a4b5dcca in ndpi_detection_process_packet /home/ivan/svnrepos/nDPI/src/lib/ndpi_main.c:5924:15
    #6 0x55b4a4a87734 in packet_processing /home/ivan/svnrepos/nDPI/example/reader_util.c:1519:31
    #7 0x55b4a4a80761 in ndpi_workflow_process_packet /home/ivan/svnrepos/nDPI/example/reader_util.c:2093:10
    #8 0x55b4a4a39c8d in LLVMFuzzerTestOneInput /home/ivan/svnrepos/nDPI/fuzz/fuzz_ndpi_reader.c:107:7
    #9 0x55b4a4a3a46b in main /home/ivan/svnrepos/nDPI/fuzz/fuzz_ndpi_reader.c:179:17
    #10 0x7f69c63760b2 in __libc_start_main /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:308:16
    #11 0x55b4a497954d in _start (/home/ivan/svnrepos/nDPI/fuzz/fuzz_ndpi_reader_with_main+0x61654d) (BuildId: 705ebc5c412d267294a65cb01f03a1f012aeaf20)

0x60600061be96 is located 0 bytes to the right of 54-byte region [0x60600061be60,0x60600061be96)
allocated by thread T0 here:
[...]
```
Found by oss-fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=46499
2022-04-10 16:59:34 +02:00

152 lines
4.9 KiB
C

/*
* tinc.c
*
* Copyright (C) 2017 - William Guglielmo <william@deselmo.com>
* Copyright (C) 2017-22 - ntop.org
*
* nDPI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nDPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nDPI. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ndpi_protocol_ids.h"
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_TINC
#include "ndpi_api.h"
#include "libcache.h"
static void ndpi_check_tinc(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
{
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
const u_int8_t *packet_payload = packet->payload;
u_int32_t payload_len = packet->payload_packet_len;
if(packet->udp != NULL) {
if(ndpi_struct->tinc_cache != NULL) {
struct tinc_cache_entry tinc_cache_entry1 = {
.src_address = packet->iph->saddr,
.dst_address = packet->iph->daddr,
.dst_port = packet->udp->dest
};
struct tinc_cache_entry tinc_cache_entry2 = {
.src_address = packet->iph->daddr,
.dst_address = packet->iph->saddr,
.dst_port = packet->udp->source
};
if(cache_remove(ndpi_struct->tinc_cache, &tinc_cache_entry1, sizeof(tinc_cache_entry1)) == CACHE_NO_ERROR ||
cache_remove(ndpi_struct->tinc_cache, &tinc_cache_entry2, sizeof(tinc_cache_entry2)) == CACHE_NO_ERROR) {
cache_remove(ndpi_struct->tinc_cache, &tinc_cache_entry1, sizeof(tinc_cache_entry1));
cache_remove(ndpi_struct->tinc_cache, &tinc_cache_entry2, sizeof(tinc_cache_entry2));
/* cache_free(ndpi_struct->tinc_cache); */
NDPI_LOG_INFO(ndpi_struct, "found tinc udp connection\n");
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TINC, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_CACHE);
}
}
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
return;
} else if(packet->tcp != NULL) {
if(payload_len == 0) {
if(packet->tcp->syn == 1 && packet->tcp->ack == 0) {
flow->tinc_cache_entry.src_address = packet->iph->saddr;
flow->tinc_cache_entry.dst_address = packet->iph->daddr;
flow->tinc_cache_entry.dst_port = packet->tcp->dest;
}
return;
}
switch(flow->tinc_state) {
case 0:
case 1:
if(payload_len > 6 && memcmp(packet_payload, "0 ", 2) == 0 && packet_payload[2] != ' ') {
u_int32_t i = 3;
while(i < payload_len && packet_payload[i++] != ' ');
if(i+3 == payload_len && memcmp((packet_payload+i), "17\n", 3) == 0) {
flow->tinc_state++;
return;
}
}
break;
case 2:
case 3:
if(payload_len > 11 && memcmp(packet_payload, "1 ", 2) == 0 && packet_payload[2] != ' ') {
u_int16_t i = 3;
u_int8_t numbers_left = 4;
while(numbers_left) {
while(i < payload_len && packet_payload[i] >= '0' && packet_payload[i] <= '9') {
i++;
}
if(i < payload_len && packet_payload[i++] == ' ') {
numbers_left--;
}
else break;
}
if(numbers_left) break;
while(i < payload_len &&
((packet_payload[i] >= '0' && packet_payload[i] <= '9') ||
(packet_payload[i] >= 'A' && packet_payload[i] <= 'Z'))) {
i++;
}
if(i < payload_len && packet_payload[i] == '\n') {
if(++flow->tinc_state > 3) {
if(ndpi_struct->tinc_cache == NULL)
ndpi_struct->tinc_cache = cache_new(TINC_CACHE_MAX_SIZE);
cache_add(ndpi_struct->tinc_cache, &(flow->tinc_cache_entry), sizeof(flow->tinc_cache_entry));
NDPI_LOG_INFO(ndpi_struct, "found tinc tcp connection\n");
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TINC, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
}
return;
}
}
break;
default: break;
}
}
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
}
void ndpi_search_tinc(struct ndpi_detection_module_struct* ndpi_struct, struct ndpi_flow_struct* flow) {
NDPI_LOG_DBG(ndpi_struct, "tinc detection\n");
if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_TINC) {
ndpi_check_tinc(ndpi_struct, flow);
}
}
void init_tinc_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask)
{
ndpi_set_bitmask_protocol_detection("TINC", ndpi_struct, detection_bitmask, *id,
NDPI_PROTOCOL_TINC,
ndpi_search_tinc,
NDPI_SELECTION_BITMASK_PROTOCOL_TCP_OR_UDP_WITHOUT_RETRANSMISSION, /* TODO: IPv6? */
SAVE_DETECTION_BITMASK_AS_UNKNOWN,
ADD_TO_DETECTION_BITMASK);
*id += 1;
}