summaryrefslogtreecommitdiffstats
path: root/security/sandbox/chromium/sandbox/win/src/handle_closer.h
blob: 4f023b27b66111d04bb5295a100e8d30c7a254ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2012 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.

#ifndef SANDBOX_SRC_HANDLE_CLOSER_H_
#define SANDBOX_SRC_HANDLE_CLOSER_H_

#include <stddef.h>

#include <map>
#include <set>

#include <string>

#include "base/macros.h"
#include "sandbox/win/src/interception.h"
#include "sandbox/win/src/sandbox_types.h"
#include "sandbox/win/src/target_process.h"

namespace sandbox {

// This is a map of handle-types to names that we need to close in the
// target process. A null set means we need to close all handles of the
// given type.
typedef std::map<const std::wstring, std::set<std::wstring>> HandleMap;

// Type and set of corresponding handle names to close.
struct HandleListEntry {
  size_t record_bytes;     // Rounded to sizeof(size_t) bytes.
  size_t offset_to_names;  // Nul terminated strings of name_count names.
  size_t name_count;
  wchar_t handle_type[1];
};

// Global parameters and a pointer to the list of entries.
struct HandleCloserInfo {
  size_t record_bytes;  // Rounded to sizeof(size_t) bytes.
  size_t num_handle_types;
  struct HandleListEntry handle_entries[1];
};

SANDBOX_INTERCEPT HandleCloserInfo* g_handle_closer_info;

// Adds handles to close after lockdown.
class HandleCloser {
 public:
  HandleCloser();
  ~HandleCloser();

  // Adds a handle that will be closed in the target process after lockdown.
  // A nullptr value for handle_name indicates all handles of the specified
  // type. An empty string for handle_name indicates the handle is unnamed.
  ResultCode AddHandle(const wchar_t* handle_type, const wchar_t* handle_name);

  // Serializes and copies the closer table into the target process.
  bool InitializeTargetHandles(TargetProcess* target);

 private:
  // Calculates the memory needed to copy the serialized handles list (rounded
  // to the nearest machine-word size).
  size_t GetBufferSize();

  // Serializes the handle list into the target process.
  bool SetupHandleList(void* buffer, size_t buffer_bytes);

  HandleMap handles_to_close_;

  DISALLOW_COPY_AND_ASSIGN(HandleCloser);
};

// Returns the object manager's name associated with a handle
bool GetHandleName(HANDLE handle, std::wstring* handle_name);

}  // namespace sandbox

#endif  // SANDBOX_SRC_HANDLE_CLOSER_H_