securesocketfunneling / ssf

Secure Socket Funneling - Network tool and toolkit - TCP and UDP port forwarding, SOCKS proxy, remote shell, standalone and cross platform

Home Page:https://securesocketfunneling.github.io/ssf/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak in bound cache (stale entries)

pbansalrubrik opened this issue · comments

Problem Description:
When Fiber to Socket create fiber, it add an entry in cache map (fiber id -> fiber object), however later it update the fiber id in fiber object and add another duplicate entry in cache map.
This result in accumulation of stale entries in cache map, since key of cache holds stale fiber id.
Also fiber is shared ptr, which means fiber never get released since a reference still exist in those stale entries.

Fix
Iterate through the cache_map to find entries which have same fiber object and prune them during connection reset handling.

Here is my code to fix the issue in file basic_fiber_demux_impl.hpp

void erase_fiber_and_used_ports(const fiber_id& id) { std::unique_lock<std::recursive_mutex> lock1(bound_mutex); std::unique_lock<std::recursive_mutex> lock2(used_ports_mutex); auto it = bound.begin(); while (it != bound.end()) { if (it->second->id == id) { // bound cache key is reverse of fid, that's why // we have to extract remote port which is local port of fiber fiber_id::local_port_type local_port = it->first.remote_port(); it = bound.erase(it); used_ports.erase(local_port); } else { ++it; } } }

Call this function erase_fiber_and_used_ports from
void basic_fiber_demux_service::unbind(implementation_type impl, const fiber_id& id)