From 27d753ac9ecc5afa4e49214e486d5dfbb50ce588 Mon Sep 17 00:00:00 2001
From: "nsylvain@chromium.org"
 <nsylvain@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Tue, 23 Mar 2010 22:42:25 +0000
Subject: Fix the POC and some of unit tests to be build and run correctly in
 64 bit.

BUG=27218

Review URL: http://codereview.chromium.org/1168002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42392 0039d316-1c4b-4281-b951-d872f2087c98
---
 sandbox/sandbox_poc/main_ui_window.cc | 12 ++++++------
 sandbox/sandbox_poc/pocdll/handles.cc | 15 +++++----------
 sandbox/sandbox_poc/sandbox.cc        |  8 ++++----
 sandbox/src/policy_target_test.cc     |  3 ---
 sandbox/src/registry_policy_test.cc   | 14 ++++++++------
 sandbox/tools/finder/ntundoc.h        | 12 +++++++-----
 6 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/sandbox/sandbox_poc/main_ui_window.cc b/sandbox/sandbox_poc/main_ui_window.cc
index 9669c4b..ef4d550 100644
--- a/sandbox/sandbox_poc/main_ui_window.cc
+++ b/sandbox/sandbox_poc/main_ui_window.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -96,9 +96,9 @@ unsigned int MainUIWindow::CreateMainWindowAndLoop(
   if (NULL == window)
     return ::GetLastError();
 
-  ::SetWindowLong(window,
-                  GWL_USERDATA,
-                  static_cast<LONG>(reinterpret_cast<LONG_PTR>(this)));
+  ::SetWindowLongPtr(window,
+                     GWLP_USERDATA,
+                     reinterpret_cast<LONG_PTR>(this));
 
   ::SetWindowText(window, L"Sandbox Proof of Concept");
 
@@ -233,9 +233,9 @@ MainUIWindow* MainUIWindow::FromWindow(HWND main_window) {
   // so that we can retrieve it with this function later. This prevents us
   // from having to define all the message handling functions (that we refer to
   // in the window proc) as static
-  ::GetWindowLong(main_window, GWL_USERDATA);
+  ::GetWindowLongPtr(main_window, GWLP_USERDATA);
   return reinterpret_cast<MainUIWindow*>(
-      static_cast<LONG_PTR>(::GetWindowLong(main_window, GWL_USERDATA)));
+      ::GetWindowLongPtr(main_window, GWLP_USERDATA));
 }
 
 BOOL MainUIWindow::OnCreate(HWND parent_window, LPCREATESTRUCT) {
diff --git a/sandbox/sandbox_poc/pocdll/handles.cc b/sandbox/sandbox_poc/pocdll/handles.cc
index 410e977..05a57b7 100644
--- a/sandbox/sandbox_poc/pocdll/handles.cc
+++ b/sandbox/sandbox_poc/pocdll/handles.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -18,7 +18,7 @@ void POCDLL_API TestGetHandle(HANDLE log) {
   FILE *output = handle2file.Translate(log, "w");
 
   // Initialize the NTAPI functions we need
-  HMODULE ntdll_handle = ::LoadLibraryA("ntdll.dll");
+  HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll");
   if (!ntdll_handle) {
     fprintf(output, "[ERROR] Cannot load ntdll.dll. Error %d\r\n",
             ::GetLastError());
@@ -35,7 +35,6 @@ void POCDLL_API TestGetHandle(HANDLE log) {
   if (!NtQueryObject || !NtQueryInformationFile || !NtQuerySystemInformation) {
     fprintf(output, "[ERROR] Cannot load all NT functions. Error %d\r\n",
                     ::GetLastError());
-    ::FreeLibrary(ntdll_handle);
     return;
   }
 
@@ -48,7 +47,6 @@ void POCDLL_API TestGetHandle(HANDLE log) {
   if (!buffer_size) {
     fprintf(output, "[ERROR] Get the number of handles. Error 0x%X\r\n",
                     status);
-    ::FreeLibrary(ntdll_handle);
     return;
   }
 
@@ -60,12 +58,11 @@ void POCDLL_API TestGetHandle(HANDLE log) {
   if (STATUS_SUCCESS != status) {
     fprintf(output, "[ERROR] Failed to get the handle list. Error 0x%X\r\n",
                     status);
-    ::FreeLibrary(ntdll_handle);
     delete [] system_handles;
     return;
   }
 
-  for (unsigned int  i = 0; i <  system_handles->NumberOfHandles; ++i) {
+  for (ULONG i = 0; i < system_handles->NumberOfHandles; ++i) {
     USHORT h = system_handles->Information[i].Handle;
     if (system_handles->Information[i].ProcessId != ::GetCurrentProcessId())
       continue;
@@ -122,8 +119,8 @@ void POCDLL_API TestGetHandle(HANDLE log) {
       // This function does not return the size of the buffer. We need to
       // iterate and always increase the buffer size until the function
       // succeeds. (Or at least does not fail with STATUS_BUFFER_OVERFLOW)
-      DWORD size_file = MAX_PATH;
-      IO_STATUS_BLOCK status_block;
+      ULONG size_file = MAX_PATH;
+      IO_STATUS_BLOCK status_block = {0};
       do {
         // Delete the previous buffer create. The buffer was too small
         if (file_name) {
@@ -186,6 +183,4 @@ void POCDLL_API TestGetHandle(HANDLE log) {
   if (system_handles) {
     delete [] system_handles;
   }
-
-  ::FreeLibrary(ntdll_handle);
 }
diff --git a/sandbox/sandbox_poc/sandbox.cc b/sandbox/sandbox_poc/sandbox.cc
index 53a9f5a..4dc0882 100644
--- a/sandbox/sandbox_poc/sandbox.cc
+++ b/sandbox/sandbox_poc/sandbox.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -148,9 +148,6 @@ int APIENTRY _tWinMain(HINSTANCE instance, HINSTANCE, wchar_t* command_line,
       return -4;
     }
 
-    // Initialization is finished, so we can enter lock-down mode
-    target_service->LowerToken();
-
     // We now know what we should load, so load it
     HMODULE dll_module = ::LoadLibraryA(dll_name.c_str());
     if (dll_module == NULL) {
@@ -158,6 +155,9 @@ int APIENTRY _tWinMain(HINSTANCE instance, HINSTANCE, wchar_t* command_line,
       return -5;
     }
 
+    // Initialization is finished, so we can enter lock-down mode
+    target_service->LowerToken();
+
     lpfnInit init_function =
         (lpfnInit) ::GetProcAddress(dll_module, entry_point.c_str());
 
diff --git a/sandbox/src/policy_target_test.cc b/sandbox/src/policy_target_test.cc
index d4ffb40..98f2418 100644
--- a/sandbox/src/policy_target_test.cc
+++ b/sandbox/src/policy_target_test.cc
@@ -209,8 +209,6 @@ TEST(PolicyTargetTest, OpenProcess) {
       "Opens a process";
 }
 
-#if !defined(_WIN64)
-
 // Launches the app in the sandbox and ask it to wait in an
 // infinite loop. Waits for 2 seconds and then check if the
 // desktop associated with the app thread is not the same as the
@@ -338,6 +336,5 @@ TEST(PolicyTargetTest, WinstaPolicy) {
   temp_policy->DestroyAlternateDesktop();
   temp_policy->Release();
 }
-#endif  // _WIN64
 
 }  // namespace sandbox
diff --git a/sandbox/src/registry_policy_test.cc b/sandbox/src/registry_policy_test.cc
index 1cc9b89..cdc1577 100644
--- a/sandbox/src/registry_policy_test.cc
+++ b/sandbox/src/registry_policy_test.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -102,12 +102,14 @@ TEST(RegistryPolicyTest, TestKeyAnyAccess) {
   EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(
       L"Reg_OpenKey open read HKEY_LOCAL_MACHINE software\\microsoft"));
 
-  // Tests write access on key allowed for read-write.
-  EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(
-      L"Reg_OpenKey create write HKEY_LOCAL_MACHINE software\\microsoft"));
+  if (::IsUserAnAdmin()) {
+    // Tests write access on key allowed for read-write.
+    EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(
+        L"Reg_OpenKey create write HKEY_LOCAL_MACHINE software\\microsoft"));
 
-  EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(
-      L"Reg_OpenKey open write HKEY_LOCAL_MACHINE software\\microsoft"));
+    EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(
+        L"Reg_OpenKey open write HKEY_LOCAL_MACHINE software\\microsoft"));
+  }
 
   // Tests subdirectory access on keys where we don't have subdirectory acess.
   EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Reg_OpenKey create read "
diff --git a/sandbox/tools/finder/ntundoc.h b/sandbox/tools/finder/ntundoc.h
index 7b5bdff..19dd386 100644
--- a/sandbox/tools/finder/ntundoc.h
+++ b/sandbox/tools/finder/ntundoc.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -108,10 +108,12 @@ typedef enum _SYSTEM_INFORMATION_CLASS {
   SystemHandleInformation = 16
 } SYSTEM_INFORMATION_CLASS;
 
-typedef struct
-{
-  NTSTATUS Status;
-  ULONG Information;
+typedef struct _IO_STATUS_BLOCK {
+    union {
+        NTSTATUS Status;
+        PVOID Pointer;
+    };
+    ULONG_PTR Information;
 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
 
 #define InitializeObjectAttributes( p, n, a, r, s ) { \
-- 
cgit v1.1