[Kernel] swprintf_s -> swprintf, change how Format*String/Build*ResourceLocator exports copy text

New way of copying the text should make it less likely for any buffer overflows to occur.
This commit is contained in:
emoose 2018-10-24 00:32:49 +01:00 committed by gibbed
parent 958882a3ea
commit 8c6e0b86f9
1 changed files with 25 additions and 33 deletions

View File

@ -28,8 +28,10 @@ namespace xam {
constexpr uint32_t X_LANGUAGE_ENGLISH = 1; constexpr uint32_t X_LANGUAGE_ENGLISH = 1;
constexpr uint32_t X_LANGUAGE_JAPANESE = 2; constexpr uint32_t X_LANGUAGE_JAPANESE = 2;
void XamFormatDateString(dword_t unk, qword_t filetime, lpwstring_t buffer, void XamFormatDateString(dword_t unk, qword_t filetime, lpvoid_t buffer,
dword_t buffer_length) { dword_t buffer_length) {
std::memset(buffer, 0, buffer_length * 2);
// TODO: implement this for other platforms // TODO: implement this for other platforms
#if XE_PLATFORM_WIN32 #if XE_PLATFORM_WIN32
FILETIME t; FILETIME t;
@ -43,21 +45,19 @@ void XamFormatDateString(dword_t unk, qword_t filetime, lpwstring_t buffer,
SystemTimeToTzSpecificLocalTime(NULL, &st, &stLocal); SystemTimeToTzSpecificLocalTime(NULL, &st, &stLocal);
wchar_t buf[256]; wchar_t buf[256];
std::memset(buf, 0, 256 * 2);
size_t size = 256 > buffer_length ? buffer_length : 256;
// TODO: format this depending on users locale? // TODO: format this depending on users locale?
swprintf_s(buf, size, L"%02d/%02d/%d", stLocal.wMonth, stLocal.wDay, swprintf(buf, 256, L"%02d/%02d/%d", stLocal.wMonth, stLocal.wDay,
stLocal.wYear); stLocal.wYear);
#endif
std::memset(buffer, 0, buffer_length * 2); xe::copy_and_swap((wchar_t*)buffer.host_address(), buf, buffer_length);
#if XE_PLATFORM_WIN32
xe::store_and_swap<std::wstring>(buffer, buf);
#endif #endif
} }
DECLARE_XAM_EXPORT(XamFormatDateString, ExportTag::kImplemented); DECLARE_XAM_EXPORT(XamFormatDateString, ExportTag::kImplemented);
void XamFormatTimeString(dword_t unk, qword_t filetime, lpwstring_t buffer, void XamFormatTimeString(dword_t unk, qword_t filetime, lpvoid_t buffer,
dword_t buffer_length) { dword_t buffer_length) {
std::memset(buffer, 0, buffer_length * 2);
// TODO: implement this for other platforms // TODO: implement this for other platforms
#if XE_PLATFORM_WIN32 #if XE_PLATFORM_WIN32
FILETIME t; FILETIME t;
@ -71,13 +71,9 @@ void XamFormatTimeString(dword_t unk, qword_t filetime, lpwstring_t buffer,
SystemTimeToTzSpecificLocalTime(NULL, &st, &stLocal); SystemTimeToTzSpecificLocalTime(NULL, &st, &stLocal);
wchar_t buf[256]; wchar_t buf[256];
std::memset(buf, 0, 256 * 2); swprintf(buf, 256, L"%02d:%02d", stLocal.wHour, stLocal.wMinute);
size_t size = 256 > buffer_length ? buffer_length : 256;
swprintf_s(buf, size, L"%02d:%02d", stLocal.wHour, stLocal.wMinute); xe::copy_and_swap((wchar_t*)buffer.host_address(), buf, buffer_length);
#endif
std::memset(buffer, 0, buffer_length * 2);
#if XE_PLATFORM_WIN32
xe::store_and_swap<std::wstring>(buffer, buf);
#endif #endif
} }
DECLARE_XAM_EXPORT(XamFormatTimeString, ExportTag::kImplemented); DECLARE_XAM_EXPORT(XamFormatTimeString, ExportTag::kImplemented);
@ -85,29 +81,26 @@ DECLARE_XAM_EXPORT(XamFormatTimeString, ExportTag::kImplemented);
dword_result_t keXamBuildResourceLocator(uint64_t module, dword_result_t keXamBuildResourceLocator(uint64_t module,
const wchar_t* container, const wchar_t* container,
const wchar_t* resource, const wchar_t* resource,
lpwstring_t buffer, lpvoid_t buffer,
uint32_t buffer_length) { uint32_t buffer_length) {
wchar_t buf[256]; wchar_t buf[256];
size_t size = 256 > buffer_length ? buffer_length : 256;
if (!module) { if (!module) {
swprintf_s(buf, size, L"file://media:/%s.xzp#%s", container, resource); swprintf(buf, 256, L"file://media:/%s.xzp#%s", container, resource);
XELOGD( XELOGD(
"XamBuildResourceLocator(%ws) returning locator to local file %ws.xzp", "XamBuildResourceLocator(%ws) returning locator to local file %ws.xzp",
container, container); container, container);
} else { } else {
swprintf_s(buf, size, L"section://%X,%s#%s", (uint32_t)module, container, swprintf(buf, 256, L"section://%X,%s#%s", (uint32_t)module, container,
resource); resource);
} }
memset(buffer, 0, buffer_length * 2); xe::copy_and_swap((wchar_t*)buffer.host_address(), buf, buffer_length);
xe::store_and_swap<std::wstring>(buffer, buf);
return 0; return 0;
} }
dword_result_t XamBuildResourceLocator(qword_t module, lpwstring_t container, dword_result_t XamBuildResourceLocator(qword_t module, lpwstring_t container,
lpwstring_t resource, lpwstring_t buffer, lpwstring_t resource, lpvoid_t buffer,
dword_t buffer_length) { dword_t buffer_length) {
return keXamBuildResourceLocator(module, container.value().c_str(), return keXamBuildResourceLocator(module, container.value().c_str(),
resource.value().c_str(), buffer, resource.value().c_str(), buffer,
@ -116,7 +109,7 @@ dword_result_t XamBuildResourceLocator(qword_t module, lpwstring_t container,
DECLARE_XAM_EXPORT(XamBuildResourceLocator, ExportTag::kImplemented); DECLARE_XAM_EXPORT(XamBuildResourceLocator, ExportTag::kImplemented);
dword_result_t XamBuildGamercardResourceLocator(lpwstring_t filename, dword_result_t XamBuildGamercardResourceLocator(lpwstring_t filename,
lpwstring_t buffer, lpvoid_t buffer,
dword_t buffer_length) { dword_t buffer_length) {
// On an actual xbox these funcs would return a locator to xam.xex resources, // On an actual xbox these funcs would return a locator to xam.xex resources,
// but for Xenia we can return a locator to the resources as local files. (big // but for Xenia we can return a locator to the resources as local files. (big
@ -132,7 +125,7 @@ dword_result_t XamBuildGamercardResourceLocator(lpwstring_t filename,
DECLARE_XAM_EXPORT(XamBuildGamercardResourceLocator, ExportTag::kImplemented); DECLARE_XAM_EXPORT(XamBuildGamercardResourceLocator, ExportTag::kImplemented);
dword_result_t XamBuildSharedSystemResourceLocator(lpwstring_t filename, dword_result_t XamBuildSharedSystemResourceLocator(lpwstring_t filename,
lpwstring_t buffer, lpvoid_t buffer,
dword_t buffer_length) { dword_t buffer_length) {
// see notes inside XamBuildGamercardResourceLocator above // see notes inside XamBuildGamercardResourceLocator above
return keXamBuildResourceLocator(0, L"shrdres", filename.value().c_str(), return keXamBuildResourceLocator(0, L"shrdres", filename.value().c_str(),
@ -142,15 +135,14 @@ DECLARE_XAM_EXPORT(XamBuildSharedSystemResourceLocator,
ExportTag::kImplemented); ExportTag::kImplemented);
dword_result_t XamBuildLegacySystemResourceLocator(lpwstring_t filename, dword_result_t XamBuildLegacySystemResourceLocator(lpwstring_t filename,
lpwstring_t buffer, lpvoid_t buffer,
dword_t buffer_len) { dword_t buffer_length) {
return XamBuildSharedSystemResourceLocator(filename, buffer, buffer_len); return XamBuildSharedSystemResourceLocator(filename, buffer, buffer_length);
} }
DECLARE_XAM_EXPORT(XamBuildLegacySystemResourceLocator, DECLARE_XAM_EXPORT(XamBuildLegacySystemResourceLocator,
ExportTag::kImplemented); ExportTag::kImplemented);
dword_result_t XamBuildXamResourceLocator(lpwstring_t filename, dword_result_t XamBuildXamResourceLocator(lpwstring_t filename, lpvoid_t buffer,
lpwstring_t buffer,
dword_t buffer_length) { dword_t buffer_length) {
return keXamBuildResourceLocator(0, L"xam", filename.value().c_str(), buffer, return keXamBuildResourceLocator(0, L"xam", filename.value().c_str(), buffer,
buffer_length); buffer_length);