From f61aaf83f6e63a53b66b7b8496b18d197b44f598 Mon Sep 17 00:00:00 2001 From: RipleyTom Date: Tue, 21 Oct 2025 14:37:25 +0200 Subject: [PATCH] More communication id validation changes --- rpcs3/Emu/Cell/Modules/sceNp.cpp | 2 +- rpcs3/Emu/Cell/Modules/sceNp2.cpp | 2 +- rpcs3/Emu/NP/np_contexts.cpp | 11 ++++++----- rpcs3/Emu/NP/np_helpers.cpp | 8 +++++++- rpcs3/Emu/NP/np_helpers.h | 1 + rpcs3/Emu/NP/rpcn_client.cpp | 2 +- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/rpcs3/Emu/Cell/Modules/sceNp.cpp b/rpcs3/Emu/Cell/Modules/sceNp.cpp index ee365b54ae..beb6c2da91 100644 --- a/rpcs3/Emu/Cell/Modules/sceNp.cpp +++ b/rpcs3/Emu/Cell/Modules/sceNp.cpp @@ -3198,7 +3198,7 @@ error_code sceNpLookupTerm() error_code sceNpLookupCreateTitleCtx(vm::cptr communicationId, vm::cptr selfNpId) { - sceNp.warning("sceNpLookupCreateTitleCtx(communicationId=*0x%x(%s), selfNpId=0x%x)", communicationId, communicationId ? communicationId->data : "", selfNpId); + sceNp.warning("sceNpLookupCreateTitleCtx(communicationId=*0x%x(%s), selfNpId=0x%x)", communicationId, communicationId ? std::string_view(communicationId->data, 9) : "", selfNpId); auto& nph = g_fxo->get>(); diff --git a/rpcs3/Emu/Cell/Modules/sceNp2.cpp b/rpcs3/Emu/Cell/Modules/sceNp2.cpp index ed4655ccd4..7a2ee50bd8 100644 --- a/rpcs3/Emu/Cell/Modules/sceNp2.cpp +++ b/rpcs3/Emu/Cell/Modules/sceNp2.cpp @@ -1300,7 +1300,7 @@ error_code sceNpMatching2GrantRoomOwner( error_code sceNpMatching2CreateContext( vm::cptr npId, vm::cptr commId, vm::cptr passPhrase, vm::ptr ctxId, s32 option) { - sceNp2.warning("sceNpMatching2CreateContext(npId=*0x%x, commId=*0x%x(%s), passPhrase=*0x%x, ctxId=*0x%x, option=%d)", npId, commId, commId ? commId->data : "", passPhrase, ctxId, option); + sceNp2.warning("sceNpMatching2CreateContext(npId=*0x%x, commId=*0x%x(%s), passPhrase=*0x%x, ctxId=*0x%x, option=%d)", npId, commId, commId ? std::string_view(commId->data, 9) : "", passPhrase, ctxId, option); auto& nph = g_fxo->get>(); diff --git a/rpcs3/Emu/NP/np_contexts.cpp b/rpcs3/Emu/NP/np_contexts.cpp index acc934c8b9..8bcfd5f01c 100644 --- a/rpcs3/Emu/NP/np_contexts.cpp +++ b/rpcs3/Emu/NP/np_contexts.cpp @@ -4,6 +4,7 @@ #include "Emu/Cell/PPUCallback.h" #include "Emu/IdManager.h" #include "Emu/Cell/Modules/cellSysutil.h" +#include "np_helpers.h" LOG_CHANNEL(sceNp2); @@ -53,7 +54,7 @@ void generic_async_transaction_context::set_result_and_wake(error_code err) tus_ctx::tus_ctx(vm::cptr communicationId, vm::cptr passphrase) { - ensure(!communicationId->data[9] && strlen(communicationId->data) == 9); + ensure(communicationId && np::validate_communication_id(*communicationId), "tus_ctx::tus_ctx: Invalid SceNpCommunicationId"); memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId)); memcpy(&this->passphrase, passphrase.get_ptr(), sizeof(SceNpCommunicationPassphrase)); } @@ -96,7 +97,7 @@ bool destroy_tus_transaction_context(s32 ctx_id) score_ctx::score_ctx(vm::cptr communicationId, vm::cptr passphrase) { - ensure(!communicationId->data[9] && strlen(communicationId->data) == 9); + ensure(communicationId && np::validate_communication_id(*communicationId), "score_ctx::score_ctx: Invalid SceNpCommunicationId"); memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId)); memcpy(&this->passphrase, passphrase.get_ptr(), sizeof(SceNpCommunicationPassphrase)); } @@ -140,7 +141,7 @@ bool destroy_score_transaction_context(s32 ctx_id) match2_ctx::match2_ctx(vm::cptr communicationId, vm::cptr passphrase, s32 option) { - ensure(!communicationId->data[9] && strlen(communicationId->data) == 9); + ensure(communicationId && np::validate_communication_id(*communicationId), "match2_ctx::match2_ctx: Invalid SceNpCommunicationId"); memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId)); memcpy(&this->passphrase, passphrase.get_ptr(), sizeof(SceNpCommunicationPassphrase)); @@ -149,7 +150,7 @@ match2_ctx::match2_ctx(vm::cptr communicationId, vm::cptr< } u16 create_match2_context(vm::cptr communicationId, vm::cptr passphrase, s32 option) { - sceNp2.notice("Creating match2 context with communicationId: <%s>", static_cast(communicationId->data)); + sceNp2.notice("Creating match2 context with communicationId: <%s>", std::string_view(communicationId->data, 9)); return static_cast(idm::make(communicationId, passphrase, option)); } bool destroy_match2_context(u16 ctx_id) @@ -167,7 +168,7 @@ shared_ptr get_match2_context(u16 ctx_id) lookup_title_ctx::lookup_title_ctx(vm::cptr communicationId) { - ensure(!communicationId->data[9] && strlen(communicationId->data) == 9); + ensure(communicationId && np::validate_communication_id(*communicationId), "lookup_title_ctx::lookup_title_ctx: Invalid SceNpCommunicationId"); memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId)); } s32 create_lookup_title_context(vm::cptr communicationId) diff --git a/rpcs3/Emu/NP/np_helpers.cpp b/rpcs3/Emu/NP/np_helpers.cpp index 5167a30fb5..b6e49b97b2 100644 --- a/rpcs3/Emu/NP/np_helpers.cpp +++ b/rpcs3/Emu/NP/np_helpers.cpp @@ -1,3 +1,4 @@ +#include "Emu/Cell/Modules/sceNp.h" #include "stdafx.h" #include "util/types.hpp" #include "Utilities/StrUtil.h" @@ -22,9 +23,14 @@ namespace np return fmt::format("%02X:%02X:%02X:%02X:%02X:%02X", ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]); } + bool validate_communication_id(const SceNpCommunicationId& com_id) + { + return std::all_of(com_id.data, com_id.data + 9, [](char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z'); }) && com_id.num <= 99; + } + std::string communication_id_to_string(const SceNpCommunicationId& communicationId) { - std::string_view com_id_data(communicationId.data, communicationId.data + 9); + std::string_view com_id_data(communicationId.data, 9); return fmt::format("%s_%02d", com_id_data, communicationId.num); } diff --git a/rpcs3/Emu/NP/np_helpers.h b/rpcs3/Emu/NP/np_helpers.h index a90f5d144c..054e45388d 100644 --- a/rpcs3/Emu/NP/np_helpers.h +++ b/rpcs3/Emu/NP/np_helpers.h @@ -8,6 +8,7 @@ namespace np { std::string ip_to_string(u32 addr); std::string ether_to_string(std::array& ether); + bool validate_communication_id(const SceNpCommunicationId& com_id); std::string communication_id_to_string(const SceNpCommunicationId& communicationId); void string_to_npid(std::string_view str, SceNpId& npid); diff --git a/rpcs3/Emu/NP/rpcn_client.cpp b/rpcs3/Emu/NP/rpcn_client.cpp index bdf0b37b00..c1dce01cdb 100644 --- a/rpcs3/Emu/NP/rpcn_client.cpp +++ b/rpcs3/Emu/NP/rpcn_client.cpp @@ -2752,7 +2752,7 @@ namespace rpcn void rpcn_client::write_communication_id(const SceNpCommunicationId& com_id, std::vector& data) { - ensure(std::all_of(com_id.data, com_id.data + 9, [](char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z'); }) && com_id.num <= 99, "rpcn_client::write_communication_id: Invalid SceNpCommunicationId"); + ensure(np::validate_communication_id(com_id), "rpcn_client::write_communication_id: Invalid SceNpCommunicationId"); const std::string com_id_str = np::communication_id_to_string(com_id); ensure(com_id_str.size() == 12, "rpcn_client::write_communication_id: Error formatting SceNpCommunicationId"); memcpy(data.data(), com_id_str.data(), COMMUNICATION_ID_SIZE);