Information

File: steamnetworkingsockets\clientlib\steamnetworkingsockets_snp.cpp
Function: CSteamNetworkConnectionBase::SNP_ReceiveUnreliableSegment()

Throughout the code responsible for handling incoming messages there is an assumption that the maximal incoming message size is 1300 bytes:

/// Max size of UDP payload.  Includes API payload and
/// any headers, but does not include IP/UDP headers
/// (IP addresses, ports, checksum, etc.
const int k_cbSteamNetworkingSocketsMaxUDPMsgLen = 1300;
...
/// Use larger limits for what we are willing to receive.
const int k_cbSteamNetworkingSocketsMaxEncryptedPayloadRecv = k_cbSteamNetworkingSocketsMaxUDPMsgLen;
const int k_cbSteamNetworkingSocketsMaxPlaintextPayloadRecv = k_cbSteamNetworkingSocketsMaxUDPMsgLen;

However, when receiving the messages from the UDP socket, they are read with a size limit of 1300 + 1024:

// Recv socket data from any sockets that might have data, and execute the callbacks.
// EI-DBG: Actual recv buffer is 1024 MORE than the max length...
char buf[ k_cbSteamNetworkingSocketsMaxUDPMsgLen + 1024 ];
...

sockaddr_storage from;
socklen_t fromlen = sizeof(from);
int ret = ::recvfrom( pSock->m_socket, buf, sizeof( buf ), 0, (sockaddr *)&from, &fromlen );
...

This size mismatch breaks the incoming MTU assumption made when handling unreliable segments, enabling an attacker to do the following:

  1. Find a game server / client that supports plain-text communication (default setting is encryption-only)
  2. Establish a connection with the target using the sockets protocol
  3. Send a large unreliable segment, of size > 1300
  4. The plain-text unreliable segment will be copied to a heap buffer of size 1300 bytes, leading to an overflow

Crash Trace:

=================================================================
==11139==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61a0000005d0 at pc 0x7efe388a777a bp 0x7efe335fb6a0 sp 0x7efe335fae48
WRITE of size 1800 at 0x61a0000005d0 thread T1
    #0 0x7efe388a7779  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x79779)
    #1 0x7efe384dc44a in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34
    #2 0x7efe384dc44a in SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::SNP_ReceiveUnreliableSegment(long long, int, void const*, int, bool, long long) ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_snp.cpp:2439
    #3 0x7efe384e1cad in SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::ProcessPlainTextDataChunk(int, SteamNetworkingSocketsLib::RecvPacketContext_t&) ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_snp.cpp:636
    #4 0x7efe384fca0e in SteamNetworkingSocketsLib::CConnectionTransportUDPBase::Received_Data(unsigned char const*, int, long long) ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.cpp:873
    #5 0x7efe384fffb4 in SteamNetworkingSocketsLib::CConnectionTransportUDP::PacketReceived(void const*, int, netadr_t const&, SteamNetworkingSocketsLib::CConnectionTransportUDP*) ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.cpp:1464
    #6 0x7efe3849fc4c in SteamNetworkingSocketsLib::CRecvPacketCallback::operator()(void const*, int, netadr_t const&) const ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_lowlevel.h:58
    #7 0x7efe3849fc4c in PollRawUDPSockets ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_lowlevel.cpp:1283
    #8 0x7efe384a3e71 in SteamNetworkingSockets_InternalPoll ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_lowlevel.cpp:1381
    #9 0x7efe384a4236 in SteamNetworkingThreadProc ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_lowlevel.cpp:1495
    #10 0x7efe384a841a in void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) /usr/include/c++/7/bits/invoke.h:60
    #11 0x7efe384a841a in std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) /usr/include/c++/7/bits/invoke.h:95
    #12 0x7efe384a841a in decltype (__invoke((_S_declval<0ul>)())) std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) /usr/include/c++/7/thread:234
    #13 0x7efe384a841a in std::thread::_Invoker<std::tuple<void (*)()> >::operator()() /usr/include/c++/7/thread:243
    #14 0x7efe384a841a in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() /usr/include/c++/7/thread:186
    #15 0x7efe37f28b9e in execute_native_thread_routine ../../../../../gcc-8.2.0/libstdc++-v3/src/c++11/thread.cc:80
    #16 0x7efe381f76da in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x76da)
    #17 0x7efe37987a3e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0x121a3e)

0x61a0000005d0 is located 0 bytes to the right of 1360-byte region [0x61a000000080,0x61a0000005d0)
allocated by thread T1 here:
    #0 0x7efe3890e448 in operator new(unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0448)
    #1 0x7efe384dc06f in __gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > >::allocate(unsigned long, void const*) /usr/include/c++/7/ext/new_allocator.h:111
    #2 0x7efe384dc06f in std::allocator_traits<std::allocator<std::_Rb_tree_node<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > > >::allocate(std::allocator<std::_Rb_tree_node<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > >&, unsigned long) /usr/include/c++/7/bits/alloc_traits.h:436
    #3 0x7efe384dc06f in std::_Rb_tree<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey, std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData>, std::_Select1st<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> >, std::less<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey>, std::allocator<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > >::_M_get_node() /usr/include/c++/7/bits/stl_tree.h:588
    #4 0x7efe384dc06f in std::_Rb_tree_node<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> >* std::_Rb_tree<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey, std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData>, std::_Select1st<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> >, std::less<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey>, std::allocator<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > >::_M_create_node<std::piecewise_construct_t const&, std::tuple<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const&>, std::tuple<> >(std::piecewise_construct_t const&, std::tuple<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const&>&&, std::tuple<>&&) /usr/include/c++/7/bits/stl_tree.h:642
    #5 0x7efe384dc06f in std::_Rb_tree_iterator<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > std::_Rb_tree<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey, std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData>, std::_Select1st<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> >, std::less<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey>, std::allocator<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > >::_M_emplace_hint_unique<std::piecewise_construct_t const&, std::tuple<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const&>, std::tuple<> >(std::_Rb_tree_const_iterator<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> >, std::piecewise_construct_t const&, std::tuple<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const&>&&, std::tuple<>&&) /usr/include/c++/7/bits/stl_tree.h:2398
    #6 0x7efe384dc06f in std::map<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData, std::less<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey>, std::allocator<std::pair<SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const, SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentData> > >::operator[](SteamNetworkingSocketsLib::SSNPRecvUnreliableSegmentKey const&) /usr/include/c++/7/bits/stl_map.h:493
    #7 0x7efe384dc06f in SteamNetworkingSocketsLib::CSteamNetworkConnectionBase::SNP_ReceiveUnreliableSegment(long long, int, void const*, int, bool, long long) ../src/steamnetworkingsockets/clientlib/steamnetworkingsockets_snp.cpp:2416

Thread T1 created by T0 here:
    #0 0x7efe38865d2f in __interceptor_pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x37d2f)
    #1 0x7efe37f28e24 in __gthread_create /home/eyalitki/Documents/Research/Compilers/OptOut/build/x86_64-linux-gnu/libstdc++-v3/include/x86_64-linux-gnu/bits/gthr-default.h:662
    #2 0x7efe37f28e24 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) ../../../../../gcc-8.2.0/libstdc++-v3/src/c++11/thread.cc:135
    #3 0x7efe3885972e  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x2b72e)

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x79779) 
Shadow bytes around the buggy address:
  0x0c347fff8060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c347fff8070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c347fff8080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c347fff8090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c347fff80a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c347fff80b0: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa
  0x0c347fff80c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c347fff80d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c347fff80e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c347fff80f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c347fff8100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==11139==ABORTING

Attachments:
CVE_2020_6017_PoC_clear_text.py
steamnetworkingsockets_messages_certs_pb2.py
steamnetworkingsockets_messages_pb2.py
steamnetworkingsockets_messages_udp_pb2.py
steam_networking_sockets.py

References:
https://research.checkpoint.com/2020/game-on-finding-vulnerabilities-in-valves-steam-sockets
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-6017
https://github.com/ValveSoftware/GameNetworkingSockets/commit/e0c86dcb9139771db3db0cfdb1fb8bef0af19c43