Commit 2c8f8d6e authored by Richard Baranyi's avatar Richard Baranyi Committed by Commit Bot

Modify AllocAndGetFullPath to return smart pointer

Change-Id: I26225f54154ea39225d5b6ee8d0b74024a614448
Reviewed-on: https://chromium-review.googlesource.com/c/1275845Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Commit-Queue: Will Harris <wfh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600087}
parent 068286da
...@@ -67,24 +67,26 @@ NTSTATUS WINAPI TargetNtCreateKey(NtCreateKeyFunction orig_CreateKey, ...@@ -67,24 +67,26 @@ NTSTATUS WINAPI TargetNtCreateKey(NtCreateKeyFunction orig_CreateKey,
CountedParameterSet<OpenKey> params; CountedParameterSet<OpenKey> params;
params[OpenKey::ACCESS] = ParamPickerMake(desired_access_uint32); params[OpenKey::ACCESS] = ParamPickerMake(desired_access_uint32);
wchar_t* full_name = nullptr; bool query_broker = false;
const wchar_t* name_ptr = name.get(); {
std::unique_ptr<wchar_t, NtAllocDeleter> full_name;
if (root_directory) { const wchar_t* name_ptr = name.get();
ret = const wchar_t* full_name_ptr = nullptr;
sandbox::AllocAndGetFullPath(root_directory, name.get(), &full_name);
if (!NT_SUCCESS(ret) || !full_name) if (root_directory) {
break; ret = sandbox::AllocAndGetFullPath(root_directory, name.get(),
params[OpenKey::NAME] = ParamPickerMake(full_name); &full_name);
} else { if (!NT_SUCCESS(ret) || !full_name)
params[OpenKey::NAME] = ParamPickerMake(name_ptr); break;
full_name_ptr = full_name.get();
params[OpenKey::NAME] = ParamPickerMake(full_name_ptr);
} else {
params[OpenKey::NAME] = ParamPickerMake(name_ptr);
}
query_broker = QueryBroker(IPC_NTCREATEKEY_TAG, params.GetBase());
} }
bool query_broker = QueryBroker(IPC_NTCREATEKEY_TAG, params.GetBase());
if (full_name)
operator delete(full_name, NT_ALLOC);
if (!query_broker) if (!query_broker)
break; break;
...@@ -150,24 +152,26 @@ NTSTATUS WINAPI CommonNtOpenKey(NTSTATUS status, ...@@ -150,24 +152,26 @@ NTSTATUS WINAPI CommonNtOpenKey(NTSTATUS status,
CountedParameterSet<OpenKey> params; CountedParameterSet<OpenKey> params;
params[OpenKey::ACCESS] = ParamPickerMake(desired_access_uint32); params[OpenKey::ACCESS] = ParamPickerMake(desired_access_uint32);
wchar_t* full_name = nullptr; bool query_broker = false;
const wchar_t* name_ptr = name.get(); {
std::unique_ptr<wchar_t, NtAllocDeleter> full_name;
if (root_directory) { const wchar_t* name_ptr = name.get();
ret = const wchar_t* full_name_ptr = nullptr;
sandbox::AllocAndGetFullPath(root_directory, name.get(), &full_name);
if (!NT_SUCCESS(ret) || !full_name) if (root_directory) {
break; ret = sandbox::AllocAndGetFullPath(root_directory, name.get(),
params[OpenKey::NAME] = ParamPickerMake(full_name); &full_name);
} else { if (!NT_SUCCESS(ret) || !full_name)
params[OpenKey::NAME] = ParamPickerMake(name_ptr); break;
full_name_ptr = full_name.get();
params[OpenKey::NAME] = ParamPickerMake(full_name_ptr);
} else {
params[OpenKey::NAME] = ParamPickerMake(name_ptr);
}
query_broker = QueryBroker(IPC_NTOPENKEY_TAG, params.GetBase());
} }
bool query_broker = QueryBroker(IPC_NTOPENKEY_TAG, params.GetBase());
if (full_name)
operator delete(full_name, NT_ALLOC);
if (!query_broker) if (!query_broker)
break; break;
......
...@@ -228,14 +228,15 @@ NTSTATUS CopyData(void* destination, const void* source, size_t bytes) { ...@@ -228,14 +228,15 @@ NTSTATUS CopyData(void* destination, const void* source, size_t bytes) {
return ret; return ret;
} }
NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path) { NTSTATUS AllocAndGetFullPath(
HANDLE root,
const wchar_t* path,
std::unique_ptr<wchar_t, NtAllocDeleter>* full_path) {
if (!InitHeap()) if (!InitHeap())
return STATUS_NO_MEMORY; return STATUS_NO_MEMORY;
DCHECK_NT(full_path); DCHECK_NT(full_path);
DCHECK_NT(path); DCHECK_NT(path);
*full_path = nullptr;
OBJECT_NAME_INFORMATION* handle_name = nullptr;
NTSTATUS ret = STATUS_UNSUCCESSFUL; NTSTATUS ret = STATUS_UNSUCCESSFUL;
__try { __try {
do { do {
...@@ -247,14 +248,15 @@ NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path) { ...@@ -247,14 +248,15 @@ NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path) {
// Query the name information a first time to get the size of the name. // Query the name information a first time to get the size of the name.
ret = NtQueryObject(root, ObjectNameInformation, nullptr, 0, &size); ret = NtQueryObject(root, ObjectNameInformation, nullptr, 0, &size);
std::unique_ptr<OBJECT_NAME_INFORMATION, NtAllocDeleter> handle_name;
if (size) { if (size) {
handle_name = reinterpret_cast<OBJECT_NAME_INFORMATION*>( handle_name.reset(reinterpret_cast<OBJECT_NAME_INFORMATION*>(
new (NT_ALLOC) BYTE[size]); new (NT_ALLOC) BYTE[size]));
// Query the name information a second time to get the name of the // Query the name information a second time to get the name of the
// object referenced by the handle. // object referenced by the handle.
ret = NtQueryObject(root, ObjectNameInformation, handle_name, size, ret = NtQueryObject(root, ObjectNameInformation, handle_name.get(),
&size); size, &size);
} }
if (STATUS_SUCCESS != ret) if (STATUS_SUCCESS != ret)
...@@ -263,10 +265,10 @@ NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path) { ...@@ -263,10 +265,10 @@ NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path) {
// Space for path + '\' + name + '\0'. // Space for path + '\' + name + '\0'.
size_t name_length = size_t name_length =
handle_name->ObjectName.Length + (wcslen(path) + 2) * sizeof(wchar_t); handle_name->ObjectName.Length + (wcslen(path) + 2) * sizeof(wchar_t);
*full_path = new (NT_ALLOC) wchar_t[name_length / sizeof(wchar_t)]; full_path->reset(new (NT_ALLOC) wchar_t[name_length / sizeof(wchar_t)]);
if (!*full_path) if (!*full_path)
break; break;
wchar_t* off = *full_path; wchar_t* off = full_path->get();
ret = CopyData(off, handle_name->ObjectName.Buffer, ret = CopyData(off, handle_name->ObjectName.Buffer,
handle_name->ObjectName.Length); handle_name->ObjectName.Length);
if (!NT_SUCCESS(ret)) if (!NT_SUCCESS(ret))
...@@ -284,16 +286,8 @@ NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path) { ...@@ -284,16 +286,8 @@ NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path) {
ret = GetExceptionCode(); ret = GetExceptionCode();
} }
if (!NT_SUCCESS(ret)) { if (!NT_SUCCESS(ret) && *full_path)
if (*full_path) { full_path->reset(nullptr);
operator delete(*full_path, NT_ALLOC);
*full_path = nullptr;
}
if (handle_name) {
operator delete(handle_name, NT_ALLOC);
handle_name = nullptr;
}
}
return ret; return ret;
} }
......
...@@ -119,7 +119,10 @@ NTSTATUS AllocAndCopyName(const OBJECT_ATTRIBUTES* in_object, ...@@ -119,7 +119,10 @@ NTSTATUS AllocAndCopyName(const OBJECT_ATTRIBUTES* in_object,
HANDLE* root); HANDLE* root);
// Determine full path name from object root and path. // Determine full path name from object root and path.
NTSTATUS AllocAndGetFullPath(HANDLE root, wchar_t* path, wchar_t** full_path); NTSTATUS AllocAndGetFullPath(
HANDLE root,
const wchar_t* path,
std::unique_ptr<wchar_t, NtAllocDeleter>* full_path);
// Initializes our ntdll level heap // Initializes our ntdll level heap
bool InitHeap(); bool InitHeap();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment