summaryrefslogtreecommitdiff
path: root/sandbox/mac/policy.cc
blob: 293255adefced7ec9d4cf37e081a51251ff4a0a1 (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
// Copyright 2014 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/mac/policy.h"

namespace sandbox {

Rule::Rule()
    : result(POLICY_DECISION_INVALID),
      substitute_port(MACH_PORT_NULL) {
}

Rule::Rule(PolicyDecision result)
    : result(result),
      substitute_port(MACH_PORT_NULL) {
}

Rule::Rule(mach_port_t override_port)
    : result(POLICY_SUBSTITUTE_PORT),
      substitute_port(override_port) {
}

BootstrapSandboxPolicy::BootstrapSandboxPolicy()
    : default_rule(POLICY_DENY_ERROR) {
}

BootstrapSandboxPolicy::~BootstrapSandboxPolicy() {}

static bool IsRuleValid(const Rule& rule) {
  if (!(rule.result > POLICY_DECISION_INVALID &&
        rule.result < POLICY_DECISION_LAST)) {
    return false;
  }
  if (rule.result == POLICY_SUBSTITUTE_PORT) {
    if (rule.substitute_port == MACH_PORT_NULL)
      return false;
  } else {
    if (rule.substitute_port != MACH_PORT_NULL)
      return false;
  }
  return true;
}

bool IsPolicyValid(const BootstrapSandboxPolicy& policy) {
  if (!IsRuleValid(policy.default_rule))
    return false;

  for (const auto& pair : policy.rules) {
    if (!IsRuleValid(pair.second))
      return false;
  }
  return true;
}

}  // namespace sandbox