Skip to content

Commit

Permalink
Fixes crash and removes unused PortContactStats
Browse files Browse the repository at this point in the history
Class was not used and didn't handle concurrent accesses to strings,
yielding crashes such as

Thread 14 "stats_update.lu" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffb3fff700 (LWP 25268)]
0x00005555556b9183 in IpAddress::intoa (this=0x28c, buf=0x7fffb3ffe030 "", bufLen=64, bitmask=255 '\377') at src/IpAddress.cpp:352
352     src/IpAddress.cpp: No such file or directory.
(gdb)
(gdb) bt
    at src/GenericHash.cpp:222
    #9  0x00005555556c0413 in NetworkInterface::walker (this=0x555556159e70, begin_slot=0x7fffb3ffe454, walk_all=true, wtype=walker_flows, walker=0x5555556c6b79 <host_flow_update_stats(GenericHashEntry*, void*, bool*)>,
        user_data=0x7fffb3ffe458) at src/NetworkInterface.cpp:795
	#10 0x00005555556c6f7c in NetworkInterface::periodicStatsUpdate (this=0x555556159e70, vm=0x7fffac0a5578) at src/NetworkInterface.cpp:2589
	#11 0x0000555555671d62 in ntop_periodic_stats_update (vm=0x7fffac0a5578) at src/LuaEngine.cpp:6304
	#12 0x000055555576c2f6 in luaD_precall ()
	#13 0x0000555555777acd in luaV_execute ()
	#14 0x000055555576c5cf in luaD_call ()
	#15 0x000055555576c621 in luaD_callnoyield ()
	#16 0x000055555576ba42 in luaD_rawrunprotected ()
	#17 0x000055555576c91b in luaD_pcall ()
	#18 0x0000555555769cd4 in lua_pcallk ()
	#19 0x0000555555681e8a in LuaEngine::run_loaded_script (this=0x7fffac01fe10) at src/LuaEngine.cpp:12188
	#20 0x000055555563eca2 in ThreadedActivity::runScript (this=0x555562aee4e0, now=1585309410, script_path=0x7fff88002620 "/usr/share/ntopng/scripts/callbacks/interface/stats_update.lua", iface=0x555556159e70, deadline=1585309420)
	    at src/ThreadedActivity.cpp:418
	    #21 0x00005555556ba04d in ThreadPool::run (this=0x5555624525a0) at src/ThreadPool.cpp:103
	    #22 0x00005555556b9c79 in doRun (ptr=0x5555624525a0) at src/ThreadPool.cpp:31
	    #23 0x00007ffff65aa6db in start_thread (arg=0x7fffb3fff700) at pthread_create.c:463
	    #24 0x00007ffff3ea388f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
  • Loading branch information
simonemainardi committed Mar 27, 2020
1 parent ead1dc0 commit 43284e2
Show file tree
Hide file tree
Showing 9 changed files with 2 additions and 187 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion include/Flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Flow : public GenericHashEntry {
bool detection_completed, extra_dissection_completed,
twh_over, twh_ok, dissect_next_http_packet, passVerdict,
l7_protocol_guessed, flow_dropped_counts_increased,
good_tls_hs, update_flow_port_stats,
good_tls_hs,
quota_exceeded, has_malicious_cli_signature, has_malicious_srv_signature;
#ifdef ALERTED_FLOWS_DEBUG
bool iface_alert_inc, iface_alert_dec;
Expand Down
4 changes: 0 additions & 4 deletions include/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,6 @@ class Host : public GenericHashEntry, public AlertableEntity {
void setResolvedName(const char * const resolved_name);
inline Fingerprint* getJA3Fingerprint() { return(&fingerprints.ja3); }
inline Fingerprint* getHASSHFingerprint() { return(&fingerprints.hassh); }
virtual void setFlowPort(bool as_server, Host *peer, u_int8_t protocol,
u_int16_t port, u_int16_t l7_proto,
const char *info, time_t when) { ; }
virtual void luaPortsDump(lua_State* vm) { lua_pushnil(vm); }

void setPrefsChanged() { prefs_loaded = false; }
virtual void reloadPrefs() {}
Expand Down
8 changes: 0 additions & 8 deletions include/LocalHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class LocalHost : public Host, public SerializableElement {
bool systemHost;
time_t initialization_time;
HostTimeseriesPoint *initial_ts_point;
std::map<u_int16_t,PortContactStats> udp_client_ports, tcp_client_ports, udp_server_ports, tcp_server_ports;

/* LocalHost data: update LocalHost::deleteHostData when adding new fields */
OperatingSystem os;
Expand All @@ -44,10 +43,6 @@ class LocalHost : public Host, public SerializableElement {

char* getMacBasedSerializationKey(char *redis_key, size_t size, char *mac_key);
char* getIpBasedSerializationKey(char *redis_key, size_t size);
void ports2Lua(lua_State* vm, bool proto_udp, bool as_client);
void updateFlowPort(std::map<u_int16_t,PortContactStats> *c, Host *peer,
u_int16_t port, u_int16_t l7_proto,
const char *info, time_t when);

public:
LocalHost(NetworkInterface *_iface, Mac *_mac, u_int16_t _vlanId, IpAddress *_ip);
Expand Down Expand Up @@ -89,9 +84,6 @@ class LocalHost : public Host, public SerializableElement {
virtual void lua(lua_State* vm, AddressTree * ptree, bool host_details,
bool verbose, bool returnHost, bool asListElement);
virtual void tsLua(lua_State* vm);
void luaPortsDump(lua_State* vm);
void setFlowPort(bool as_server, Host *peer, u_int8_t proto, u_int16_t port,
u_int16_t l7_proto, const char *info, time_t when);
};

#endif /* _LOCAL_HOST_H_ */
1 change: 0 additions & 1 deletion include/ntop_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ using namespace std;
#include "TimeseriesStats.h"
#include "HostStats.h"
#include "LocalHostStats.h"
#include "PortContactStats.h"
#include "HostScore.h"
#include "Bin.h"
#include "FlowDurationBin.h"
Expand Down
56 changes: 1 addition & 55 deletions src/Flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Flow::Flow(NetworkInterface *_iface,
peers_score_accounted = false;
status_infos = NULL;

detection_completed = update_flow_port_stats = false;
detection_completed = false;
extra_dissection_completed = false;
ndpiDetectedProtocol = ndpiUnknownProtocol;
doNotExpireBefore = iface->getTimeLastPktRcvd() + DONT_NOT_EXPIRE_BEFORE_SEC;
Expand Down Expand Up @@ -747,7 +747,6 @@ void Flow::setProtocolDetectionCompleted() {
processDetectedProtocol();

detection_completed = true;
update_flow_port_stats = true;

#ifdef BLACKLISTED_FLOWS_DEBUG
if(ndpiDetectedProtocol.category == CUSTOM_CATEGORY_MALWARE) {
Expand Down Expand Up @@ -1384,59 +1383,6 @@ void Flow::periodic_stats_update(void *user_data) {
Mac *cli_mac = get_cli_host() ? get_cli_host()->getMac() : NULL;
Mac *srv_mac = get_srv_host() ? get_srv_host()->getMac() : NULL;

if(update_flow_port_stats) {
bool dump_flow = false;

if(protocol == IPPROTO_TCP) {
/*
update the ports only if the flow has been observed from the beginning
and it has been established
*/
dump_flow = ((src2dst_tcp_flags|dst2src_tcp_flags) & (TH_SYN|TH_PUSH)) == (TH_SYN|TH_PUSH);
} else if(protocol == IPPROTO_UDP) {
if(
(srv_host && srv_host->get_ip()->isBroadMulticastAddress())
|| (get_packets_srv2cli() > 0 /* We see a response, hence we assume this is not a probing attempt */)
)
dump_flow = true;
}

#if 0
char buf[128];

ntop->getTrace()->traceEvent(TRACE_NORMAL, "[%s][%u/%u] %s",
dump_flow ? "DUMP" : "",
get_packets_cli2srv(), get_packets_srv2cli(),
print(buf, sizeof(buf)));
#endif

if(dump_flow && (srv_port != 0)) {
u_int16_t p = ndpiDetectedProtocol.master_protocol;
u_int16_t port = ntohs(srv_port);

if(p == NDPI_PROTOCOL_UNKNOWN)
p = ndpiDetectedProtocol.app_protocol;

if(cli_host && cli_host->isLocalHost())
cli_host->setFlowPort(false /* client */, srv_host, protocol, port, p,
getFlowInfo() ? getFlowInfo() : "",
iface->getTimeLastPktRcvd());

if(srv_host && srv_host->isLocalHost())
srv_host->setFlowPort(true /* server */, cli_host, protocol, port, p,
getFlowInfo() ? getFlowInfo() : "",
iface->getTimeLastPktRcvd());

#if 0
char buf[128];

ntop->getTrace()->traceEvent(TRACE_NORMAL, "%s", print(buf, sizeof(buf)));
#endif
}

update_flow_port_stats = false;
}

hosts_periodic_stats_update(getInterface(), cli_host, srv_host, &partial, first_partial, tv);

if(cli_host && srv_host) {
Expand Down
88 changes: 0 additions & 88 deletions src/LocalHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,6 @@ void LocalHost::lua(lua_State* vm, AddressTree *ptree,

/* *************************************** */

void LocalHost::luaPortsDump(lua_State* vm) {
lua_newtable(vm);

lua_newtable(vm);
ports2Lua(vm, true, true);
ports2Lua(vm, true, false);
lua_pushstring(vm, "udp");
lua_insert(vm, -2);
lua_settable(vm, -3);

lua_newtable(vm);
ports2Lua(vm, false, true);
ports2Lua(vm, false, false);
lua_pushstring(vm, "tcp");
lua_insert(vm, -2);
lua_settable(vm, -3);

}

/* *************************************** */

// TODO move into nDPI
void LocalHost::inlineSetOSDetail(const char *_os_detail) {
if((mac == NULL)
Expand Down Expand Up @@ -351,73 +330,6 @@ char * LocalHost::getIpBasedSerializationKey(char *redis_key, size_t size) {

/* *************************************** */

void LocalHost::ports2Lua(lua_State* vm, bool proto_udp, bool as_client) {
std::map<u_int16_t,PortContactStats> *s = as_client ? (proto_udp ? &udp_client_ports : &tcp_client_ports) : (proto_udp ? &udp_server_ports : &tcp_server_ports);

if(s->size() > 0) {
std::map<u_int16_t,PortContactStats>::iterator it;

lua_newtable(vm);

m.lock(__FILE__, __LINE__);

for(it = s->begin(); it != s->end(); ++it) {
char buf[8];

snprintf(buf, sizeof(buf), "%u", it->first);

lua_newtable(vm);

it->second.lua(vm, iface);

lua_pushstring(vm, buf);
lua_insert(vm, -2);
lua_settable(vm, -3);
}

m.unlock(__FILE__, __LINE__);

lua_pushstring(vm, as_client ? "client_ports" : "server_ports");
lua_insert(vm, -2);
lua_settable(vm, -3);
}
}

/* *************************************** */

void LocalHost::updateFlowPort(std::map<u_int16_t,PortContactStats> *c, Host *peer,
u_int16_t port, u_int16_t l7_proto,
const char *info, time_t when) {
std::map<u_int16_t,PortContactStats>::iterator it = c->find(port);

if(it == c->end())
(*c)[port] = PortContactStats(l7_proto, peer, info, when);
else
it->second.update(peer, info, when);
}

/* *************************************** */

void LocalHost::setFlowPort(bool as_server, Host *peer, u_int8_t protocol,
u_int16_t port, u_int16_t l7_proto,
const char *info, time_t when) {
m.lock(__FILE__, __LINE__);
if(as_server) {
if(protocol == IPPROTO_UDP)
updateFlowPort(&udp_server_ports, peer, port, l7_proto, info, when);
else
updateFlowPort(&tcp_server_ports, peer, port, l7_proto, info, when);
} else {
if(protocol == IPPROTO_UDP)
updateFlowPort(&udp_client_ports, peer, port, l7_proto, info, when);
else
updateFlowPort(&tcp_client_ports, peer, port, l7_proto, info, when);
}
m.unlock(__FILE__, __LINE__);
}

/* *************************************** */

/*
* Reload non-critical host prefs. Such prefs are not reloaded inline to
* avoid slowing down the packet capture. The default value (set into the
Expand Down
30 changes: 0 additions & 30 deletions src/LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3226,35 +3226,6 @@ static int ntop_get_interface_host_info(lua_State* vm) {

/* ****************************************** */

static int ntop_get_interface_host_used_ports(lua_State* vm) {
NetworkInterface *ntop_interface = getCurrentInterface(vm);
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
Host *h;

ntop->getTrace()->traceEvent(TRACE_DEBUG, "%s() called", __FUNCTION__);

if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING) != CONST_LUA_OK) return(CONST_LUA_ERROR);
get_host_vlan_info((char*)lua_tostring(vm, 1), &host_ip, &vlan_id, buf, sizeof(buf));

/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);

if(!ntop_interface) return(CONST_LUA_ERROR);

h = ntop_interface->findHostByIP(get_allowed_nets(vm), host_ip, vlan_id);

if(!h)
return(CONST_LUA_ERROR);
else {
h->luaPortsDump(vm);
return(CONST_LUA_OK);
}
}

/* ****************************************** */

static int ntop_get_interface_host_timeseries(lua_State* vm) {
NetworkInterface *ntop_interface = getCurrentInterface(vm);
char *host_ip;
Expand Down Expand Up @@ -11409,7 +11380,6 @@ static const luaL_Reg ntop_interface_reg[] = {
{ "getBatchedRemoteHostsInfo", ntop_get_batched_interface_remote_hosts_info },
{ "getBatchedLocalHostsTs", ntop_get_batched_interface_local_hosts_ts },
{ "getHostInfo", ntop_get_interface_host_info },
{ "getHostUsedPorts", ntop_get_interface_host_used_ports },
{ "getHostTimeseries", ntop_get_interface_host_timeseries },
{ "getHostCountry", ntop_get_interface_host_country },
{ "getGroupedHosts", ntop_get_grouped_interface_hosts },
Expand Down

0 comments on commit 43284e2

Please sign in to comment.