-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
acl.cc
166 lines (139 loc) · 5.64 KB
/
acl.cc
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sandbox/win/src/acl.h"
#include <windows.h>
#include "base/notreached.h"
#include "base/win/access_token.h"
namespace sandbox {
namespace {
absl::optional<DWORD> GetIntegrityLevelValue(IntegrityLevel integrity_level) {
switch (integrity_level) {
case INTEGRITY_LEVEL_SYSTEM:
return DWORD{SECURITY_MANDATORY_SYSTEM_RID};
case INTEGRITY_LEVEL_HIGH:
return DWORD{SECURITY_MANDATORY_HIGH_RID};
case INTEGRITY_LEVEL_MEDIUM:
return DWORD{SECURITY_MANDATORY_MEDIUM_RID};
case INTEGRITY_LEVEL_MEDIUM_LOW:
return DWORD{SECURITY_MANDATORY_MEDIUM_RID - 2048};
case INTEGRITY_LEVEL_LOW:
return DWORD{SECURITY_MANDATORY_LOW_RID};
case INTEGRITY_LEVEL_BELOW_LOW:
return DWORD{SECURITY_MANDATORY_LOW_RID - 2048};
case INTEGRITY_LEVEL_UNTRUSTED:
return DWORD{SECURITY_MANDATORY_UNTRUSTED_RID};
case INTEGRITY_LEVEL_LAST:
return absl::nullopt;
}
NOTREACHED();
return absl::nullopt;
}
bool AddSidToDefaultDacl(base::win::AccessToken& token,
const base::win::Sid& sid,
base::win::SecurityAccessMode access_mode,
ACCESS_MASK access) {
absl::optional<base::win::AccessControlList> dacl = token.DefaultDacl();
if (!dacl || !dacl->SetEntry(sid, access_mode, access, 0)) {
return false;
}
return token.SetDefaultDacl(*dacl);
}
} // namespace
bool AddSidToDefaultDacl(HANDLE token,
const base::win::Sid& sid,
base::win::SecurityAccessMode access_mode,
ACCESS_MASK access) {
absl::optional<base::win::AccessToken> query_token =
base::win::AccessToken::FromToken(token, TOKEN_ADJUST_DEFAULT);
if (!query_token)
return false;
return AddSidToDefaultDacl(*query_token, sid, access_mode, access);
}
bool AddSidToDefaultDacl(HANDLE token,
base::win::WellKnownSid known_sid,
base::win::SecurityAccessMode access_mode,
ACCESS_MASK access) {
return AddSidToDefaultDacl(token, base::win::Sid(known_sid), access_mode,
access);
}
bool RevokeLogonSidFromDefaultDacl(HANDLE token) {
absl::optional<base::win::AccessToken> query_token =
base::win::AccessToken::FromToken(token, TOKEN_ADJUST_DEFAULT);
if (!query_token)
return false;
absl::optional<base::win::Sid> logon_sid = query_token->LogonId();
if (!logon_sid)
return ::GetLastError() == ERROR_NOT_FOUND;
return AddSidToDefaultDacl(*query_token, *logon_sid,
base::win::SecurityAccessMode::kRevoke, 0);
}
bool AddUserSidToDefaultDacl(HANDLE token, ACCESS_MASK access) {
absl::optional<base::win::AccessToken> query_token =
base::win::AccessToken::FromToken(token, TOKEN_ADJUST_DEFAULT);
if (!query_token)
return false;
return AddSidToDefaultDacl(*query_token, query_token->User(),
base::win::SecurityAccessMode::kGrant, access);
}
bool AddKnownSidToObject(HANDLE object,
base::win::SecurityObjectType object_type,
const base::win::Sid& sid,
base::win::SecurityAccessMode access_mode,
ACCESS_MASK access) {
absl::optional<base::win::SecurityDescriptor> sd =
base::win::SecurityDescriptor::FromHandle(object, object_type,
DACL_SECURITY_INFORMATION);
if (!sd) {
return false;
}
if (!sd->SetDaclEntry(sid, access_mode, access, 0)) {
return false;
}
return sd->WriteToHandle(object, object_type, DACL_SECURITY_INFORMATION);
}
bool AddKnownSidToObject(HANDLE object,
base::win::SecurityObjectType object_type,
base::win::WellKnownSid known_sid,
base::win::SecurityAccessMode access_mode,
ACCESS_MASK access) {
return AddKnownSidToObject(object, object_type, base::win::Sid(known_sid),
access_mode, access);
}
bool ReplacePackageSidInDacl(HANDLE object,
base::win::SecurityObjectType object_type,
const base::win::Sid& package_sid,
ACCESS_MASK access) {
if (!AddKnownSidToObject(object, object_type, package_sid,
base::win::SecurityAccessMode::kRevoke, 0)) {
return false;
}
return AddKnownSidToObject(object, object_type,
base::win::WellKnownSid::kAllApplicationPackages,
base::win::SecurityAccessMode::kGrant, access);
}
absl::optional<base::win::Sid> GetIntegrityLevelSid(
IntegrityLevel integrity_level) {
absl::optional<DWORD> value = GetIntegrityLevelValue(integrity_level);
if (!value)
return absl::nullopt;
return base::win::Sid::FromIntegrityLevel(*value);
}
DWORD SetObjectIntegrityLabel(HANDLE handle,
base::win::SecurityObjectType object_type,
DWORD mandatory_policy,
IntegrityLevel integrity_level) {
absl::optional<DWORD> value = GetIntegrityLevelValue(integrity_level);
if (!value) {
return ERROR_INVALID_SID;
}
base::win::SecurityDescriptor sd;
if (!sd.SetMandatoryLabel(*value, 0, mandatory_policy)) {
return ::GetLastError();
}
if (!sd.WriteToHandle(handle, object_type, LABEL_SECURITY_INFORMATION)) {
return ::GetLastError();
}
return ERROR_SUCCESS;
}
} // namespace sandbox