Xbox: Try to fix buffer overflow when parsing socket addresses

This commit is contained in:
UnknownShadow200 2023-12-06 19:55:04 +11:00
parent 42e6719241
commit cf90a60117

View File

@ -34,8 +34,7 @@ void Platform_Log(const char* msg, int len) {
Mem_Copy(tmp, msg, len); tmp[len] = '\0'; Mem_Copy(tmp, msg, len); tmp[len] = '\0';
// log to on-screen display // log to on-screen display
debugPrint(tmp); debugPrint("%s\n", tmp);
debugPrint("\n");
// log to cxbx-reloaded console // log to cxbx-reloaded console
OutputDebugStringA(tmp); OutputDebugStringA(tmp);
} }
@ -300,6 +299,7 @@ void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
union SocketAddress { union SocketAddress {
struct sockaddr raw; struct sockaddr raw;
struct sockaddr_in v4; struct sockaddr_in v4;
struct sockaddr_storage total; // needed for lwip_getaddrinfo
}; };
static int ParseHost(union SocketAddress* addr, const char* host) { static int ParseHost(union SocketAddress* addr, const char* host) {
@ -315,10 +315,13 @@ static int ParseHost(union SocketAddress* addr, const char* host) {
res = lwip_getaddrinfo(host, NULL, &hints, &result); res = lwip_getaddrinfo(host, NULL, &hints, &result);
if (res) return res; if (res) return res;
for (cur = result; cur; cur = cur->ai_next) { for (cur = result; cur; cur = cur->ai_next)
{
if (cur->ai_family != AF_INET) continue; if (cur->ai_family != AF_INET) continue;
found = true; found = true;
// NOTE: cur->ai_addrlen will always be set to sizeof(struct sockaddr_storage) by lwip
// https://github.com/m-labs/lwip/blob/0178d1d2ee35fb82ab0a13256425f9aa33b08f60/src/api/netdb.c#L402C20-L402C52
Mem_Copy(addr, cur->ai_addr, cur->ai_addrlen); Mem_Copy(addr, cur->ai_addr, cur->ai_addrlen);
break; break;
} }
@ -341,12 +344,8 @@ int Socket_ValidAddress(const cc_string* address) {
} }
cc_result Socket_Connect(cc_socket* s, const cc_string* address, int port, cc_bool nonblocking) { cc_result Socket_Connect(cc_socket* s, const cc_string* address, int port, cc_bool nonblocking) {
int family, addrSize = 0;
union SocketAddress addr; union SocketAddress addr;
int res; int res;
// TODO TODO TODO TODO TODO
// if 'addrSize = 0' is removed, then the game never gets past 'Fetching cdn.classicube.net...'
// so probably relying on undefined behaviour somewhere...
*s = -1; *s = -1;
res = ParseAddress(&addr, address); res = ParseAddress(&addr, address);
@ -417,7 +416,12 @@ cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) {
*--------------------------------------------------------Platform---------------------------------------------------------* *--------------------------------------------------------Platform---------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static void InitHDD(void) { static void InitHDD(void) {
if (nxIsDriveMounted('E')) {
hdd_mounted = true;
} else {
hdd_mounted = nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\"); hdd_mounted = nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\");
}
if (!hdd_mounted) { if (!hdd_mounted) {
Platform_LogConst("Failed to mount E:/ from Data partition"); Platform_LogConst("Failed to mount E:/ from Data partition");
return; return;