Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2012 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_SRC_HANDLE_CLOSER_H_
      6 #define SANDBOX_SRC_HANDLE_CLOSER_H_
      7 
      8 #include <map>
      9 #include <set>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/strings/string16.h"
     13 #include "sandbox/win/src/interception.h"
     14 #include "sandbox/win/src/sandbox_types.h"
     15 #include "sandbox/win/src/target_process.h"
     16 
     17 namespace sandbox {
     18 
     19 // This is a map of handle-types to names that we need to close in the
     20 // target process. A null set means we need to close all handles of the
     21 // given type.
     22 typedef std::map<const string16, std::set<const string16> > HandleMap;
     23 
     24 // Type and set of corresponding handle names to close.
     25 struct HandleListEntry {
     26   size_t record_bytes;       // Rounded to sizeof(size_t) bytes.
     27   size_t offset_to_names;    // Nul terminated strings of name_count names.
     28   size_t name_count;
     29   char16 handle_type[1];
     30 };
     31 
     32 // Global parameters and a pointer to the list of entries.
     33 struct HandleCloserInfo {
     34   size_t record_bytes;       // Rounded to sizeof(size_t) bytes.
     35   size_t num_handle_types;
     36   struct HandleListEntry handle_entries[1];
     37 };
     38 
     39 SANDBOX_INTERCEPT HandleCloserInfo* g_handle_closer_info;
     40 
     41 // Adds handles to close after lockdown.
     42 class HandleCloser {
     43  public:
     44   HandleCloser();
     45 
     46   // Adds a handle that will be closed in the target process after lockdown.
     47   // A NULL value for handle_name indicates all handles of the specified type.
     48   // An empty string for handle_name indicates the handle is unnamed.
     49   ResultCode AddHandle(const char16* handle_type, const char16* handle_name);
     50 
     51   // Serializes and copies the closer table into the target process.
     52   bool InitializeTargetHandles(TargetProcess* target);
     53 
     54   // Adds any interceptions that may be required due to closed system handles.
     55   bool SetupHandleInterceptions(InterceptionManager* manager);
     56 
     57  private:
     58   // Calculates the memory needed to copy the serialized handles list (rounded
     59   // to the nearest machine-word size).
     60   size_t GetBufferSize();
     61 
     62   // Serializes the handle list into the target process.
     63   bool SetupHandleList(void* buffer, size_t buffer_bytes);
     64 
     65   HandleMap handles_to_close_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(HandleCloser);
     68 };
     69 
     70 // Returns the object manager's name associated with a handle
     71 bool GetHandleName(HANDLE handle, string16* handle_name);
     72 
     73 }  // namespace sandbox
     74 
     75 #endif  // SANDBOX_SRC_HANDLE_CLOSER_H_
     76