[Kernel] Replaced TranslateAnsiString with TranslateAnsiPath for file paths.

This removes paths that starts or ends with whitespace characters
This commit is contained in:
Gliniak 2025-01-14 22:43:20 +01:00
parent e80d4effa1
commit 1688ea5d69
5 changed files with 15 additions and 9 deletions

View File

@ -93,6 +93,12 @@ inline std::string_view TranslateAnsiString(const Memory* memory,
ansi_string->length);
}
inline std::string TranslateAnsiPath(const Memory* memory,
const X_ANSI_STRING* ansi_string) {
return string_util::trim(
std::string(TranslateAnsiString(memory, ansi_string)));
}
inline std::string_view TranslateAnsiStringAddress(const Memory* memory,
uint32_t guest_address) {
if (!guest_address) {

View File

@ -63,7 +63,7 @@ dword_result_t NtCreateFile_entry(lpdword_t handle_out, dword_t desired_access,
vfs::Entry* root_entry = nullptr;
// Compute path, possibly attrs relative.
auto target_path = util::TranslateAnsiString(kernel_memory(), object_name);
auto target_path = util::TranslateAnsiPath(kernel_memory(), object_name);
// Enforce that the path is ASCII.
if (!IsValidPath(target_path, false)) {
@ -462,7 +462,7 @@ dword_result_t NtQueryFullAttributesFile_entry(
assert_always();
}
auto target_path = util::TranslateAnsiString(kernel_memory(), object_name);
auto target_path = util::TranslateAnsiPath(kernel_memory(), object_name);
// Enforce that the path is ASCII.
if (!IsValidPath(target_path, false)) {
@ -501,7 +501,7 @@ dword_result_t NtQueryDirectoryFile_entry(
uint32_t info = 0;
auto file = kernel_state()->object_table()->LookupObject<XFile>(file_handle);
auto name = util::TranslateAnsiString(kernel_memory(), file_name);
auto name = util::TranslateAnsiPath(kernel_memory(), file_name);
// Enforce that the path is ASCII.
if (!IsValidPath(name, true)) {
@ -558,7 +558,7 @@ dword_result_t NtOpenSymbolicLinkObject_entry(
auto object_name =
kernel_memory()->TranslateVirtual<X_ANSI_STRING*>(object_attrs->name_ptr);
auto target_path = util::TranslateAnsiString(kernel_memory(), object_name);
auto target_path = util::TranslateAnsiPath(kernel_memory(), object_name);
// Enforce that the path is ASCII.
if (!IsValidPath(target_path, false)) {

View File

@ -234,7 +234,7 @@ dword_result_t NtSetInformationFile_entry(
auto info = info_ptr.as<X_FILE_RENAME_INFORMATION*>();
// Compute path, possibly attrs relative.
std::filesystem::path target_path =
util::TranslateAnsiString(kernel_memory(), &info->ansi_string);
util::TranslateAnsiPath(kernel_memory(), &info->ansi_string);
// Place IsValidPath in path from where it can be accessed everywhere
if (!IsValidPath(target_path.string(), false)) {

View File

@ -263,7 +263,7 @@ dword_result_t XexLoadImageHeaders_entry(pointer_t<X_ANSI_STRING> path,
return X_STATUS_BUFFER_TOO_SMALL;
}
auto current_kernel = ctx->kernel_state;
auto target_path = util::TranslateAnsiString(current_kernel->memory(), path);
auto target_path = util::TranslateAnsiPath(current_kernel->memory(), path);
vfs::File* vfs_file = nullptr;
vfs::FileAction file_action;

View File

@ -350,9 +350,9 @@ DECLARE_XBOXKRNL_EXPORT1(ObReferenceObject, kNone, kImplemented);
dword_result_t ObCreateSymbolicLink_entry(pointer_t<X_ANSI_STRING> path_ptr,
pointer_t<X_ANSI_STRING> target_ptr) {
auto path = xe::utf8::canonicalize_guest_path(
util::TranslateAnsiString(kernel_memory(), path_ptr));
util::TranslateAnsiPath(kernel_memory(), path_ptr));
auto target = xe::utf8::canonicalize_guest_path(
util::TranslateAnsiString(kernel_memory(), target_ptr));
util::TranslateAnsiPath(kernel_memory(), target_ptr));
if (xe::utf8::starts_with(path, "\\??\\")) {
path = path.substr(4); // Strip the full qualifier
@ -367,7 +367,7 @@ dword_result_t ObCreateSymbolicLink_entry(pointer_t<X_ANSI_STRING> path_ptr,
DECLARE_XBOXKRNL_EXPORT1(ObCreateSymbolicLink, kNone, kImplemented);
dword_result_t ObDeleteSymbolicLink_entry(pointer_t<X_ANSI_STRING> path_ptr) {
auto path = util::TranslateAnsiString(kernel_memory(), path_ptr);
auto path = util::TranslateAnsiPath(kernel_memory(), path_ptr);
if (!kernel_state()->file_system()->UnregisterSymbolicLink(path)) {
return X_STATUS_UNSUCCESSFUL;
}