summaryrefslogtreecommitdiffstats
path: root/security/sandbox/chromium/sandbox/win/src/handle_policy.cc
blob: fa3295ae3f3b08a8be96804d2d5bfbf644f65f51 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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.

#include "sandbox/win/src/handle_policy.h"

#include <string>

#include "base/win/scoped_handle.h"
#include "sandbox/win/src/broker_services.h"
#include "sandbox/win/src/ipc_tags.h"
#include "sandbox/win/src/policy_engine_opcodes.h"
#include "sandbox/win/src/policy_params.h"
#include "sandbox/win/src/sandbox_types.h"
#include "sandbox/win/src/sandbox_utils.h"

namespace sandbox {

bool HandlePolicy::GenerateRules(const wchar_t* type_name,
                                 TargetPolicy::Semantics semantics,
                                 LowLevelPolicy* policy) {
  PolicyRule duplicate_rule(ASK_BROKER);

  switch (semantics) {
    case TargetPolicy::HANDLES_DUP_ANY: {
      if (!duplicate_rule.AddNumberMatch(IF_NOT, HandleTarget::TARGET,
                                         ::GetCurrentProcessId(), EQUAL)) {
        return false;
      }
      break;
    }

    case TargetPolicy::HANDLES_DUP_BROKER: {
      if (!duplicate_rule.AddNumberMatch(IF, HandleTarget::TARGET,
                                         ::GetCurrentProcessId(), EQUAL)) {
        return false;
      }
      break;
    }

    default:
     return false;
  }
  if (!duplicate_rule.AddStringMatch(IF, HandleTarget::NAME, type_name,
                                     CASE_INSENSITIVE)) {
    return false;
  }
  if (!policy->AddRule(IpcTag::DUPLICATEHANDLEPROXY, &duplicate_rule)) {
    return false;
  }
  return true;
}

DWORD HandlePolicy::DuplicateHandleProxyAction(EvalResult eval_result,
                                               HANDLE source_handle,
                                               DWORD target_process_id,
                                               HANDLE* target_handle,
                                               DWORD desired_access,
                                               DWORD options) {
  // The only action supported is ASK_BROKER which means duplicate the handle.
  if (ASK_BROKER != eval_result) {
    return ERROR_ACCESS_DENIED;
  }

  base::win::ScopedHandle remote_target_process;
  if (target_process_id != ::GetCurrentProcessId()) {
    // Sandboxed children are dynamic, so we check that manually.
    if (!BrokerServicesBase::GetInstance()->IsSafeDuplicationTarget(
            target_process_id)) {
      return ERROR_ACCESS_DENIED;
    }

    remote_target_process.Set(::OpenProcess(PROCESS_DUP_HANDLE, FALSE,
                                            target_process_id));
    if (!remote_target_process.IsValid())
      return ::GetLastError();
  }

  // If the policy didn't block us and we have no valid target, then the broker
  // (this process) is the valid target.
  HANDLE target_process = remote_target_process.IsValid() ?
                          remote_target_process.Get() : ::GetCurrentProcess();
  if (!::DuplicateHandle(::GetCurrentProcess(), source_handle, target_process,
                         target_handle, desired_access, FALSE,
                         options)) {
    return ::GetLastError();
  }

  return ERROR_SUCCESS;
}

}  // namespace sandbox