mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-23 00:36:42 -10:00
* netplay/nsnetservice: fix remotely triggerable heap overflow in mDNS discovery
-[NetplayBonjourMan finishBrowsing:] copied every TXT record value into
a fixed-size struct netplay_host field with
strlcpy(host->nick, [txt[@"nick"] bytes], [txt[@"nick"] length] + 1);
which is wrong in three separate ways, all of them reachable from any
device on the same LAN:
- strlcpy()'s third argument is the size of the *destination*. Passing
the source length instead means a TXT value longer than the field
overruns it: host->nick is 32 bytes, an mDNS TXT string may be 255,
so a crafted advertisement writes up to 223 bytes past the field.
The same applies to frontend/core/core_version/retroarch_version
(32 bytes each); subsystem_name is the last member, so its overflow
runs off the end of the allocation entirely.
- NSData contents are not NUL-terminated. Both the libretro-common
and the platform strlcpy() walk the source to measure it, reading
past the end of the NSData buffer.
- A key absent from the TXT dictionary yields a nil NSData, whose
-bytes is NULL, and strlcpy() then dereferences it.
Replace the whole set with txt_field_copy(), which takes the
destination size, bounds the copy by the NSData length, stops at an
embedded NUL and treats a nil value as an empty string.
The address loop had its own overflow. -[NSNetService addresses]
returns NSData objects sized to the concrete sockaddr - 16 bytes for
sockaddr_in, 28 for sockaddr_in6 - but the buffer was cast straight to
struct sockaddr_storage* and handed to addr_6to4(), which rewrites its
argument with a sizeof(struct sockaddr_storage) memset. For a
v4-mapped v6 address that is a 100-byte write past the end of the
NSData, through a pointer to immutable contents. getnameinfo_retro()
was likewise told the address was 128 bytes long. Copy into a real
sockaddr_storage first and pass the true length.
The duplicate check could never fire: every arm of the test ended in
'continue', so the loop ran to completion and the host was appended
regardless. Give it a break and an actual result.
content_crc was read uninitialised whenever -scanHexInt: failed to
parse - including when the key was missing, where the NSString is nil.
Finally, the browser and its services array live on the main run loop,
where the delegate callbacks mutate them, but finishBrowsing: runs on
the task worker thread: enumerating the live array there races
-didFindService: and mutation during fast enumeration raises
NSGenericException. Stop the browser and snapshot the array on the
main queue, with a same-thread path for the non-threaded task queue,
where the handler already runs on the main thread and dispatch_sync()
would deadlock.
Cap the list at NETPLAY_MAX_DISCOVERED_HOSTS while here; see the
following commit.
* netplay/discovery: bound and de-duplicate the LAN host list
netplay_lan_ad_client_response() appended one entry per accepted
datagram, with no ceiling and no duplicate check. Responses are
unsolicited - nothing ties one to a query we sent - so a single peer
answering in a loop grows the list forever. Measured against the
response loop in isolation: 20000 datagrams from one address produced
20000 entries and 13.6MB of heap, and there is nothing to stop it
there.
Reject a response whose address/port pair is already listed, and stop
accepting new entries at NETPLAY_MAX_DISCOVERED_HOSTS. The same flood
now yields a single entry.
While in here:
- Growth was 1 then +4 forever, which is quadratic: 4096 entries copy
1.4GB through realloc() against 2.9MB for doubling, and 4.4x the
wall time to fill. Make it geometric, clamped to the cap.
- The advertised port is remote input and was carried through
unchecked: ntohl(0xffffffff) reaches struct netplay_room as -1, and
that is what the menu offers to connect to. Require 1..65535.
- struct sockaddr_storage was declared inside the loop with a {0}
initialiser, so every datagram paid a 128-byte memset that
recvfrom() immediately overwrote. Hoist it and reset only the
length, which is the part recvfrom() actually requires.
- getnameinfo_retro() was told the address was sizeof(sockaddr_storage)
bytes long. addr_6to4() has already established it is AF_INET by
that point, so pass sizeof(struct sockaddr_in).
- net_driver_state_t is a zero-initialised static, so lan_ad_client_fd
starts at 0, not -1. Every '>= 0' guard reads that as a live
socket, and deinit_netplay_discovery() would close descriptor 0.
Unreachable today because the scan task always inits first, but only
by luck. Set it explicitly.
- discovered_hosts.hosts is never freed anywhere in the tree; it is
only truncated (size = 0) between scans. LeakSan does not flag it
because the head is reachable from a global, which is presumably why
it has gone unnoticed. Add netplay_discovery_free_hosts() and call
it from the RARCH_CTL_MAIN_DEINIT teardown that already frees
room_list.
- One HAVE_NETPLAYDISCOVERY_NSNET test used #if where the other four
in this file use #ifdef. It works because -D without a value
implies 1, but it breaks silently if the macro is ever defined
empty.
Verified by extracting netplay_lan_ad_client_response() and
netplay_discovery_driver_ctl() verbatim into a harness with a scripted
socket layer: well-formed streams across the growth seam, fully
unterminated string fields, truncated/oversized datagrams, wrong magic,
non-LAN sources, clear-then-refill, and the flood case. ASan, UBSan
and LeakSan clean.
* task_netplay_lan_scan: honour the deadline, and stop spinning
Two problems in the poll branch of the handler.
The timeout was only consulted when the discovery driver reported that
no host had answered:
if (!netplay_discovery_driver_ctl(...GET_RESPONSES, NULL))
if (cpu_features_get_time_usec() >= data->timeout)
goto finished;
netplay_lan_ad_client_response() returns true whenever at least one
host was added this poll, so a peer answering continuously keeps the
scan alive forever - modelled against the real queue, still running
400s into an 800ms scan. The preceding commit stops a single peer from
being counted repeatedly, but the deadline should not depend on that:
test it unconditionally.
The handler also returns immediately after each poll with task->when
left at 0, so the threaded worker re-enters it with no delay at all.
That is a full core spent on back-to-back recvfrom() calls for the
whole 800ms of every scan. Set task->when to poll every 4ms instead;
datagrams that arrive in between are waiting in the socket buffer, so
nothing is missed. (task_cloudsync.c already sets task->when from
inside its handler this way.)
Finally, the handler never looked at RETRO_TASK_FLG_CANCELLED, so
task_queue_cancel_task() on a running scan had no effect and the
discovery socket stayed open until the deadline. Check it up front and
fall through to the normal teardown.
---------
Co-authored-by: libretroadmin <>
138 lines
3.9 KiB
C
138 lines
3.9 KiB
C
/* RetroArch - A frontend for libretro.
|
|
* Copyright (C) 2016-2017 - Jean-André Santoni
|
|
* Copyright (C) 2016-2019 - Andrés Suárez
|
|
*
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* RetroArch 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <features/features_cpu.h>
|
|
|
|
#include "tasks_internal.h"
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "../config.h"
|
|
#endif
|
|
|
|
#ifdef HAVE_NETPLAYDISCOVERY
|
|
|
|
#include "../network/netplay/netplay.h"
|
|
|
|
/* How long to wait between response polls, in microseconds. */
|
|
#define NETPLAY_LAN_SCAN_POLL_INTERVAL 4000
|
|
|
|
struct netplay_lan_scan_data
|
|
{
|
|
retro_time_t timeout;
|
|
void (*cb)(const void*);
|
|
bool query;
|
|
bool busy;
|
|
};
|
|
|
|
static void task_netplay_lan_scan_handler(retro_task_t *task)
|
|
{
|
|
struct netplay_lan_scan_data *data =
|
|
(struct netplay_lan_scan_data*)task->task_data;
|
|
|
|
if ((task_get_flags(task) & RETRO_TASK_FLG_CANCELLED) > 0)
|
|
goto finished;
|
|
|
|
if (data->query)
|
|
{
|
|
netplay_discovery_driver_ctl(
|
|
RARCH_NETPLAY_DISCOVERY_CTL_LAN_CLEAR_RESPONSES, NULL);
|
|
|
|
if (!init_netplay_discovery())
|
|
goto finished;
|
|
|
|
if (!netplay_discovery_driver_ctl(
|
|
RARCH_NETPLAY_DISCOVERY_CTL_LAN_SEND_QUERY, NULL))
|
|
goto finished;
|
|
|
|
data->timeout += cpu_features_get_time_usec();
|
|
data->query = false;
|
|
}
|
|
else
|
|
{
|
|
netplay_discovery_driver_ctl(
|
|
RARCH_NETPLAY_DISCOVERY_CTL_LAN_GET_RESPONSES, NULL);
|
|
|
|
/* The deadline is unconditional. Testing it only when no host
|
|
* answered this poll let a peer that keeps answering hold the
|
|
* scan open indefinitely - observed still running 400s into an
|
|
* 800ms scan - which in turn kept the discovery socket open and,
|
|
* before the list was capped, grew it without bound. */
|
|
if (cpu_features_get_time_usec() >= data->timeout)
|
|
goto finished;
|
|
}
|
|
|
|
/* Yield rather than spin. The handler returns immediately after each
|
|
* poll and the threaded queue re-enters it with no delay, so a scan
|
|
* used to burn a core on back-to-back recvfrom() calls for its whole
|
|
* duration. A poll every few milliseconds loses nothing: the
|
|
* responses are already sitting in the socket buffer. */
|
|
task->when = cpu_features_get_time_usec()
|
|
+ NETPLAY_LAN_SCAN_POLL_INTERVAL;
|
|
|
|
return;
|
|
|
|
finished:
|
|
deinit_netplay_discovery();
|
|
|
|
task_set_progress(task, 100);
|
|
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
|
|
}
|
|
|
|
static void task_netplay_lan_scan_callback(retro_task_t *task,
|
|
void *task_data, void *user_data, const char *error)
|
|
{
|
|
struct netplay_lan_scan_data *data =
|
|
(struct netplay_lan_scan_data*)task_data;
|
|
net_driver_state_t *net_st = networking_state_get_ptr();
|
|
|
|
data->cb(&net_st->discovered_hosts);
|
|
|
|
data->busy = false;
|
|
}
|
|
|
|
bool task_push_netplay_lan_scan(void (*cb)(const void*), unsigned timeout)
|
|
{
|
|
static struct netplay_lan_scan_data data = {0};
|
|
retro_task_t *task;
|
|
|
|
/* Do not run more than one LAN scan task at a time. */
|
|
if (data.busy)
|
|
return false;
|
|
|
|
task = task_init();
|
|
if (!task)
|
|
return false;
|
|
|
|
data.busy = true;
|
|
data.query = true;
|
|
data.timeout = (retro_time_t)timeout * 1000;
|
|
data.cb = cb;
|
|
|
|
task->handler = task_netplay_lan_scan_handler;
|
|
task->callback = task_netplay_lan_scan_callback;
|
|
task->task_data = &data;
|
|
|
|
task_queue_push(task);
|
|
|
|
return true;
|
|
}
|
|
#else
|
|
bool task_push_netplay_lan_scan(void (*cb)(const void*), unsigned timeout)
|
|
{
|
|
return false;
|
|
}
|
|
#endif
|