Skip to content

Commit

Permalink
Merge pull request hrydgard#19013 from hrydgard/save-data-crashfix
Browse files Browse the repository at this point in the history
Fix a long-standing buffer overflow in savedata encryption
  • Loading branch information
hrydgard authored Apr 7, 2024
2 parents 654c16f + 77133dd commit d75b97f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Core/Dialog/SavedataParam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD
u8 *data_ = param->dataBuf;

int aligned_len = align16(cryptedSize);
cryptedData = new u8[aligned_len + 0x10];
cryptedData = new u8[aligned_len + 0x10]{};
memcpy(cryptedData, data_, cryptedSize);

int decryptMode = DetermineCryptMode(param);
Expand Down
9 changes: 4 additions & 5 deletions Core/ELF/ParamSFO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ int ParamSFOData::GetDataOffset(const u8 *paramsfo, const std::string &dataName)
return -1;
}

bool ParamSFOData::WriteSFO(u8 **paramsfo, size_t *size) const {
void ParamSFOData::WriteSFO(u8 **paramsfo, size_t *size) const {
size_t total_size = 0;
size_t key_size = 0;
size_t data_size = 0;
Expand Down Expand Up @@ -251,9 +251,10 @@ bool ParamSFOData::WriteSFO(u8 **paramsfo, size_t *size) const {
total_size += data_size;
*size = total_size;

u8* data = new u8[total_size];
size_t aligned_size = (total_size + 15) & ~15;
u8* data = new u8[aligned_size];
*paramsfo = data;
memset(data, 0, total_size);
memset(data, 0, aligned_size);
memcpy(data, &header, sizeof(Header));

// Now fill
Expand Down Expand Up @@ -300,8 +301,6 @@ bool ParamSFOData::WriteSFO(u8 **paramsfo, size_t *size) const {
index_ptr++;

}

return true;
}

void ParamSFOData::Clear() {
Expand Down
6 changes: 4 additions & 2 deletions Core/ELF/ParamSFO.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ class ParamSFOData

std::string GetDiscID();

bool ReadSFO(const u8 *paramsfo, size_t size);
bool WriteSFO(u8 **paramsfo, size_t *size) const;
// This allocates a buffer (*paramsfo) using new[], whose size is zero-filled up to a multiple of 16 bytes.
// This is required for SavedataParam::BuildHash.
void WriteSFO(u8 **paramsfo, size_t *size) const;

bool ReadSFO(const u8 *paramsfo, size_t size);
bool ReadSFO(const std::vector<u8> &paramsfo) {
if (!paramsfo.empty()) {
return ReadSFO(&paramsfo[0], paramsfo.size());
Expand Down

0 comments on commit d75b97f

Please sign in to comment.