From d5177e33b9eafccbe778fcb02788853c24f776bd Mon Sep 17 00:00:00 2001 From: ANR2ME Date: Sun, 16 May 2021 12:55:55 +0700 Subject: [PATCH 1/2] Fix possible crash issue on blocking socket implementation. --- Core/HLE/sceNetAdhoc.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index 8fa07a050f..aab95bf861 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -397,6 +397,10 @@ int StartGameModeScheduler(int bufSize) { int DoBlockingPdpRecv(int uid, AdhocSocketRequest& req, s64& result) { auto sock = adhocSockets[req.id - 1]; + if (!sock) { + result = ERROR_NET_ADHOC_SOCKET_DELETED; + return 0; + } if (sock->flags & ADHOC_F_ALERTRECV) { result = ERROR_NET_ADHOC_SOCKET_ALERTED; sock->alerted_flags |= ADHOC_F_ALERTRECV; @@ -495,6 +499,10 @@ int DoBlockingPdpRecv(int uid, AdhocSocketRequest& req, s64& result) { int DoBlockingPdpSend(int uid, AdhocSocketRequest& req, s64& result, AdhocSendTargets& targetPeers) { auto sock = adhocSockets[req.id - 1]; + if (!sock) { + result = ERROR_NET_ADHOC_SOCKET_DELETED; + return 0; + } auto& pdpsocket = sock->data.pdp; if (sock->flags & ADHOC_F_ALERTSEND) { result = ERROR_NET_ADHOC_SOCKET_ALERTED; @@ -544,6 +552,10 @@ int DoBlockingPdpSend(int uid, AdhocSocketRequest& req, s64& result, AdhocSendTa int DoBlockingPtpSend(int uid, AdhocSocketRequest& req, s64& result) { auto sock = adhocSockets[req.id - 1]; + if (!sock) { + result = ERROR_NET_ADHOC_SOCKET_DELETED; + return 0; + } auto& ptpsocket = sock->data.ptp; if (sock->flags & ADHOC_F_ALERTSEND) { result = ERROR_NET_ADHOC_SOCKET_ALERTED; @@ -593,6 +605,10 @@ int DoBlockingPtpSend(int uid, AdhocSocketRequest& req, s64& result) { int DoBlockingPtpRecv(int uid, AdhocSocketRequest& req, s64& result) { auto sock = adhocSockets[req.id - 1]; + if (!sock) { + result = ERROR_NET_ADHOC_SOCKET_DELETED; + return 0; + } auto& ptpsocket = sock->data.ptp; if (sock->flags & ADHOC_F_ALERTRECV) { result = ERROR_NET_ADHOC_SOCKET_ALERTED; @@ -645,6 +661,10 @@ int DoBlockingPtpRecv(int uid, AdhocSocketRequest& req, s64& result) { int DoBlockingPtpAccept(int uid, AdhocSocketRequest& req, s64& result) { auto sock = adhocSockets[req.id - 1]; + if (!sock) { + result = ERROR_NET_ADHOC_SOCKET_DELETED; + return 0; + } auto& ptpsocket = sock->data.ptp; if (sock->flags & ADHOC_F_ALERTACCEPT) { result = ERROR_NET_ADHOC_SOCKET_ALERTED; @@ -691,6 +711,10 @@ int DoBlockingPtpAccept(int uid, AdhocSocketRequest& req, s64& result) { int DoBlockingPtpConnect(int uid, AdhocSocketRequest& req, s64& result) { auto sock = adhocSockets[req.id - 1]; + if (!sock) { + result = ERROR_NET_ADHOC_SOCKET_DELETED; + return 0; + } auto& ptpsocket = sock->data.ptp; if (sock->flags & ADHOC_F_ALERTCONNECT) { result = ERROR_NET_ADHOC_SOCKET_ALERTED; @@ -742,6 +766,10 @@ int DoBlockingPtpConnect(int uid, AdhocSocketRequest& req, s64& result) { int DoBlockingPtpFlush(int uid, AdhocSocketRequest& req, s64& result) { auto sock = adhocSockets[req.id - 1]; + if (!sock) { + result = ERROR_NET_ADHOC_SOCKET_DELETED; + return 0; + } auto& ptpsocket = sock->data.ptp; if (sock->flags & ADHOC_F_ALERTFLUSH) { result = ERROR_NET_ADHOC_SOCKET_ALERTED; @@ -1835,6 +1863,9 @@ int PollAdhocSocket(SceNetAdhocPollSd* sds, int count, int timeout, int nonblock // Fill in Socket ID if (sds[i].id > 0 && sds[i].id <= MAX_SOCKET && adhocSockets[sds[i].id - 1] != NULL) { auto sock = adhocSockets[sds[i].id - 1]; + if (!sock) { + return ERROR_NET_ADHOC_SOCKET_DELETED; + } if (sock->type == SOCK_PTP) { fd = sock->data.ptp.id; } From 8dcfed038567eb83d66837849106d8217dc6e15d Mon Sep 17 00:00:00 2001 From: ANR2ME Date: Sun, 16 May 2021 13:16:20 +0700 Subject: [PATCH 2/2] Show Port Offset value too when detecting Data coming from incorrect port. --- Core/HLE/sceNetAdhoc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index aab95bf861..a514e6bff0 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -7261,7 +7261,7 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi } else if (recvresult == 0 && rxbuflen > 0) { WARN_LOG(SCENET, "InputLoop[%d]: Unknown Port[%s:%u] (Recved=%i, Length=%i)", matchingId, mac2str(&sendermac).c_str(), senderport, recvresult, rxbuflen); - host->NotifyUserMessage(std::string(n->T("Data from incorrect Port")) + std::string(" [") + mac2str(&sendermac) + std::string("]:") + std::to_string(senderport) + std::string(" -> ") + std::to_string(context->port), 1.0, 0x0080ff); + host->NotifyUserMessage(std::string(n->T("Data from incorrect Port")) + std::string(" [") + mac2str(&sendermac) + std::string("]:") + std::to_string(senderport) + std::string(" -> ") + std::to_string(context->port) + std::string(" (") + std::to_string(portOffset) + std::string(")"), 1.0, 0x0080ff); } // Handle Peer Timeouts