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