Skip to content

Commit f61aaf8

Browse files
RipleyTomelad335
authored andcommitted
More communication id validation changes
1 parent 66920b4 commit f61aaf8

File tree

6 files changed

+17
-9
lines changed

6 files changed

+17
-9
lines changed

rpcs3/Emu/Cell/Modules/sceNp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3198,7 +3198,7 @@ error_code sceNpLookupTerm()
31983198

31993199
error_code sceNpLookupCreateTitleCtx(vm::cptr<SceNpCommunicationId> communicationId, vm::cptr<SceNpId> selfNpId)
32003200
{
3201-
sceNp.warning("sceNpLookupCreateTitleCtx(communicationId=*0x%x(%s), selfNpId=0x%x)", communicationId, communicationId ? communicationId->data : "", selfNpId);
3201+
sceNp.warning("sceNpLookupCreateTitleCtx(communicationId=*0x%x(%s), selfNpId=0x%x)", communicationId, communicationId ? std::string_view(communicationId->data, 9) : "", selfNpId);
32023202

32033203
auto& nph = g_fxo->get<named_thread<np::np_handler>>();
32043204

rpcs3/Emu/Cell/Modules/sceNp2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ error_code sceNpMatching2GrantRoomOwner(
13001300
error_code sceNpMatching2CreateContext(
13011301
vm::cptr<SceNpId> npId, vm::cptr<SceNpCommunicationId> commId, vm::cptr<SceNpCommunicationPassphrase> passPhrase, vm::ptr<SceNpMatching2ContextId> ctxId, s32 option)
13021302
{
1303-
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);
1303+
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);
13041304

13051305
auto& nph = g_fxo->get<named_thread<np::np_handler>>();
13061306

rpcs3/Emu/NP/np_contexts.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Emu/Cell/PPUCallback.h"
55
#include "Emu/IdManager.h"
66
#include "Emu/Cell/Modules/cellSysutil.h"
7+
#include "np_helpers.h"
78

89
LOG_CHANNEL(sceNp2);
910

@@ -53,7 +54,7 @@ void generic_async_transaction_context::set_result_and_wake(error_code err)
5354

5455
tus_ctx::tus_ctx(vm::cptr<SceNpCommunicationId> communicationId, vm::cptr<SceNpCommunicationPassphrase> passphrase)
5556
{
56-
ensure(!communicationId->data[9] && strlen(communicationId->data) == 9);
57+
ensure(communicationId && np::validate_communication_id(*communicationId), "tus_ctx::tus_ctx: Invalid SceNpCommunicationId");
5758
memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId));
5859
memcpy(&this->passphrase, passphrase.get_ptr(), sizeof(SceNpCommunicationPassphrase));
5960
}
@@ -96,7 +97,7 @@ bool destroy_tus_transaction_context(s32 ctx_id)
9697

9798
score_ctx::score_ctx(vm::cptr<SceNpCommunicationId> communicationId, vm::cptr<SceNpCommunicationPassphrase> passphrase)
9899
{
99-
ensure(!communicationId->data[9] && strlen(communicationId->data) == 9);
100+
ensure(communicationId && np::validate_communication_id(*communicationId), "score_ctx::score_ctx: Invalid SceNpCommunicationId");
100101
memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId));
101102
memcpy(&this->passphrase, passphrase.get_ptr(), sizeof(SceNpCommunicationPassphrase));
102103
}
@@ -140,7 +141,7 @@ bool destroy_score_transaction_context(s32 ctx_id)
140141

141142
match2_ctx::match2_ctx(vm::cptr<SceNpCommunicationId> communicationId, vm::cptr<SceNpCommunicationPassphrase> passphrase, s32 option)
142143
{
143-
ensure(!communicationId->data[9] && strlen(communicationId->data) == 9);
144+
ensure(communicationId && np::validate_communication_id(*communicationId), "match2_ctx::match2_ctx: Invalid SceNpCommunicationId");
144145
memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId));
145146
memcpy(&this->passphrase, passphrase.get_ptr(), sizeof(SceNpCommunicationPassphrase));
146147

@@ -149,7 +150,7 @@ match2_ctx::match2_ctx(vm::cptr<SceNpCommunicationId> communicationId, vm::cptr<
149150
}
150151
u16 create_match2_context(vm::cptr<SceNpCommunicationId> communicationId, vm::cptr<SceNpCommunicationPassphrase> passphrase, s32 option)
151152
{
152-
sceNp2.notice("Creating match2 context with communicationId: <%s>", static_cast<const char*>(communicationId->data));
153+
sceNp2.notice("Creating match2 context with communicationId: <%s>", std::string_view(communicationId->data, 9));
153154
return static_cast<u16>(idm::make<match2_ctx>(communicationId, passphrase, option));
154155
}
155156
bool destroy_match2_context(u16 ctx_id)
@@ -167,7 +168,7 @@ shared_ptr<match2_ctx> get_match2_context(u16 ctx_id)
167168

168169
lookup_title_ctx::lookup_title_ctx(vm::cptr<SceNpCommunicationId> communicationId)
169170
{
170-
ensure(!communicationId->data[9] && strlen(communicationId->data) == 9);
171+
ensure(communicationId && np::validate_communication_id(*communicationId), "lookup_title_ctx::lookup_title_ctx: Invalid SceNpCommunicationId");
171172
memcpy(&this->communicationId, communicationId.get_ptr(), sizeof(SceNpCommunicationId));
172173
}
173174
s32 create_lookup_title_context(vm::cptr<SceNpCommunicationId> communicationId)

rpcs3/Emu/NP/np_helpers.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "Emu/Cell/Modules/sceNp.h"
12
#include "stdafx.h"
23
#include "util/types.hpp"
34
#include "Utilities/StrUtil.h"
@@ -22,9 +23,14 @@ namespace np
2223
return fmt::format("%02X:%02X:%02X:%02X:%02X:%02X", ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
2324
}
2425

26+
bool validate_communication_id(const SceNpCommunicationId& com_id)
27+
{
28+
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;
29+
}
30+
2531
std::string communication_id_to_string(const SceNpCommunicationId& communicationId)
2632
{
27-
std::string_view com_id_data(communicationId.data, communicationId.data + 9);
33+
std::string_view com_id_data(communicationId.data, 9);
2834
return fmt::format("%s_%02d", com_id_data, communicationId.num);
2935
}
3036

rpcs3/Emu/NP/np_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace np
88
{
99
std::string ip_to_string(u32 addr);
1010
std::string ether_to_string(std::array<u8, 6>& ether);
11+
bool validate_communication_id(const SceNpCommunicationId& com_id);
1112
std::string communication_id_to_string(const SceNpCommunicationId& communicationId);
1213

1314
void string_to_npid(std::string_view str, SceNpId& npid);

rpcs3/Emu/NP/rpcn_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,7 @@ namespace rpcn
27522752

27532753
void rpcn_client::write_communication_id(const SceNpCommunicationId& com_id, std::vector<u8>& data)
27542754
{
2755-
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");
2755+
ensure(np::validate_communication_id(com_id), "rpcn_client::write_communication_id: Invalid SceNpCommunicationId");
27562756
const std::string com_id_str = np::communication_id_to_string(com_id);
27572757
ensure(com_id_str.size() == 12, "rpcn_client::write_communication_id: Error formatting SceNpCommunicationId");
27582758
memcpy(data.data(), com_id_str.data(), COMMUNICATION_ID_SIZE);

0 commit comments

Comments
 (0)