Home | History | Annotate | Download | only in sandbox_poc
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef SANDBOX_SANDBOX_POC_MAIN_UI_WINDOW_H__
      6 #define SANDBOX_SANDBOX_POC_MAIN_UI_WINDOW_H__
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 
     12 namespace sandbox {
     13 class BrokerServices;
     14 enum ResultCode;
     15 }
     16 
     17 // Header file for the MainUIWindow, a simple window with a menu bar that
     18 // can pop up a dialog (accessible through the menu bar), to specify a path and
     19 // filename of any DLL to load and choose an entry point of his/her choice
     20 // (note: only entry points with no parameters are expected to work).
     21 //
     22 // The purpose of this is to be able to spawn an EXE inside a SandBox, have it
     23 // load a DLL and call the entry point on it to test how it behaves inside the
     24 // sandbox. This is useful for developer debugging and for security testing.
     25 //
     26 // The MainUIWindow also has a listview that displays debugging information to
     27 // the user.
     28 //
     29 // Sample usage:
     30 //
     31 //    MainUIWindow window;
     32 //    unsigned int ret = window.CreateMainWindowAndLoop(
     33 //        handle_to_current_instance,
     34 //        ::GetCommandLineW(),
     35 //        show_command,
     36 //        broker);
     37 //
     38 // The CreateMainWindowAndLoop() contains a message loop that ends when the
     39 // user closes the MainUIWindow.
     40 
     41 // This class encapsulates the Main UI window for the broker application.
     42 // It simply shows a menu that gives the user the ability (through a dialog) to
     43 // specify a DLL and what entry point to call in the DLL.
     44 class MainUIWindow {
     45  public:
     46   MainUIWindow();
     47   ~MainUIWindow();
     48 
     49   // Creates the main window, displays it and starts the message pump. This
     50   // call will not return until user closes the main UI window that appears
     51   // as a result. Arguments 'instance', 'command_line' and 'show_cmd' can be
     52   // passed in directly from winmain. The 'broker' argument is a pointer to a
     53   // BrokerService that will launch a new EXE inside the sandbox and load the
     54   // DLL of the user's choice.
     55   unsigned int CreateMainWindowAndLoop(HINSTANCE instance,
     56                                        wchar_t* command_line,
     57                                        int show_command,
     58                                        sandbox::BrokerServices* broker);
     59 
     60  private:
     61   // The default value DLL name to add to the edit box.
     62   static const wchar_t kDefaultDll_[];
     63 
     64   // The default value to show in the entry point.
     65   static const wchar_t kDefaultEntryPoint_[];
     66 
     67   // The default value to show in the log file.
     68   static const wchar_t kDefaultLogFile_[];
     69 
     70   // Handles the messages sent to the main UI window. The return value is the
     71   // result of the message processing and depends on the message.
     72   static LRESULT CALLBACK WndProc(HWND window,
     73                                   UINT message_id,
     74                                   WPARAM wparam,
     75                                   LPARAM lparam);
     76 
     77   // Handles the messages sent to the SpawnTarget dialog. The return value is
     78   // the result of the message processing and depends on the message.
     79   static INT_PTR CALLBACK SpawnTargetWndProc(HWND dialog,
     80                                              UINT message_id,
     81                                              WPARAM wparam,
     82                                              LPARAM lparam);
     83 
     84   // Retrieves a pointer to the MainWindow from a value stored along with the
     85   // window handle (passed in as hwnd). Return value is a pointer to the
     86   // MainUIWindow previously stored with SetWindowLong() during WM_CREATE.
     87   static MainUIWindow* FromWindow(HWND main_window);
     88 
     89   // Handles the WM_CREATE message for the main UI window. Returns TRUE on
     90   // success.
     91   static BOOL OnCreate(HWND parent_window, LPCREATESTRUCT);
     92 
     93   // Handles the WM_DESTROY message for the main UI window.
     94   void OnDestroy(HWND window);
     95 
     96   // Handles the WM_SIZE message for the main UI window.
     97   void OnSize(HWND window, UINT state, int cx, int cy);
     98 
     99   // Handles the WM_PAINT message for the main UI window.
    100   void OnPaint(HWND window);
    101 
    102   // Handles the menu command File \ Exit for the main UI window.
    103   void OnFileExit();
    104 
    105   // Handles the menu command Commands \ Launch for the main UI window.
    106   void OnCommandsLaunch(HWND window);
    107 
    108   // Handles the Launch button in the SpawnTarget dialog (normally clicked
    109   // after selecting DLL and entry point). OnLaunchDll will retrieve the
    110   // values entered by the user and store it in the members of the class.
    111   // Returns true if user selected a non-zero values for DLL filename
    112   // (possibly including path also) and entry point.
    113   bool OnLaunchDll(HWND dialog);
    114 
    115   // Spawns a target EXE inside the sandbox (with the help of the
    116   // BrokerServices passed in to CreateMainWindowAndLoop), and passes to it
    117   // (as command line arguments) the DLL path and the entry point function
    118   // name. The EXE is expected to parse the command line and load the DLL.
    119   // NOTE: The broker does not know if the target EXE successfully loaded the
    120   // DLL, for that you have to rely on the EXE providing a log.
    121   // Returns true if the broker reports that it was successful in creating
    122   // the target and false if not.
    123   bool SpawnTarget();
    124 
    125   // Shows a standard File Open dialog and returns the DLL filename selected or
    126   // blank string if the user cancelled (or an error occurred).
    127   std::wstring OnShowBrowseForDllDlg(HWND owner);
    128 
    129   // Shows a standard Save As dialog and returns the log filename selected or
    130   // blank string if the user cancelled (or an error occurred).
    131   std::wstring OnShowBrowseForLogFileDlg(HWND owner);
    132 
    133   // Formats a message using the supplied format string and prints it in the
    134   // listview in the main UI window. Passing a NULL param in 'fmt' results in
    135   // no action being performed. Maximum message length is 1K.
    136   void AddDebugMessage(const wchar_t* format, ...);
    137 
    138   // Assists AddDebugMessage in displaying a message in the ListView. It
    139   // simply wraps ListView_InsertItem to insert a debugging message to the
    140   // top of the list view. Passing a NULL param in 'fmt' results in no action
    141   // being performed.
    142   void InsertLineInListView(wchar_t* debug_message);
    143 
    144   // Calls ListenPipe using the class instance received in parameter. This is
    145   // used to create new threads executing ListenPipe
    146   static DWORD WINAPI ListenPipeThunk(void *param);
    147 
    148   // Calls WaitForTargetThunk using the class instance received in parameter
    149   // This is used to create new threads executing WaitForTarget.
    150   static DWORD WINAPI WaitForTargetThunk(void *param);
    151 
    152   // Listens on a pipe and output the data received to a file and to the UI.
    153   DWORD ListenPipe();
    154 
    155   // Waits for the target to dies and display a message in the UI.
    156   DWORD WaitForTarget();
    157 
    158   // The BrokerServices will be used to spawn an EXE in a sandbox and ask
    159   // it to load a DLL.
    160   sandbox::BrokerServices* broker_;
    161 
    162   // Contains the information about the running target.
    163   PROCESS_INFORMATION target_;
    164 
    165   // This is essentially a command line to a target executable that the
    166   // broker will spawn and ask to load the DLL.
    167   std::wstring spawn_target_;
    168 
    169   // A handle to the current instance of the app. Passed in to this class
    170   // through CreateMainWindowAndLoop.
    171   HINSTANCE instance_handle_;
    172 
    173   // A path to the DLL that the target should load once it executes.
    174   std::wstring dll_path_;
    175 
    176   // The name of the entry point the target should call after it loads the DLL.
    177   std::wstring entry_point_;
    178 
    179   // The name of the log file to use.
    180   std::wstring log_file_;
    181 
    182   // This is a static handle to the list view that fills up the entire main
    183   // UI window. The list view is used to display debugging information to the
    184   // user.
    185   static HWND list_view_;
    186 
    187   // Pipe used to communicate the logs between the target and the broker.
    188   HANDLE pipe_handle_;
    189 
    190   DISALLOW_COPY_AND_ASSIGN(MainUIWindow);
    191 };
    192 
    193 #endif  // SANDBOX_SANDBOX_POC_MAIN_UI_WINDOW_H__
    194