1 // Copyright (c) 2013 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.
5 #include "base/process/process_handle.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/win/scoped_handle.h"
11 #include "base/win/windows_version.h"
15 ProcessId
GetCurrentProcId() {
16 return ::GetCurrentProcessId();
19 ProcessHandle
GetCurrentProcessHandle() {
20 return ::GetCurrentProcess();
23 bool OpenProcessHandle(ProcessId pid
, ProcessHandle
* handle
) {
24 // We try to limit privileges granted to the handle. If you need this
25 // for test code, consider using OpenPrivilegedProcessHandle instead of
26 // adding more privileges here.
27 ProcessHandle result
= OpenProcess(PROCESS_TERMINATE
|
28 PROCESS_QUERY_INFORMATION
|
39 bool OpenPrivilegedProcessHandle(ProcessId pid
, ProcessHandle
* handle
) {
40 ProcessHandle result
= OpenProcess(PROCESS_DUP_HANDLE
|
42 PROCESS_QUERY_INFORMATION
|
54 bool OpenProcessHandleWithAccess(ProcessId pid
,
56 ProcessHandle
* handle
) {
57 ProcessHandle result
= OpenProcess(access_flags
, FALSE
, pid
);
66 void CloseProcessHandle(ProcessHandle process
) {
70 ProcessId
GetProcId(ProcessHandle process
) {
71 // This returns 0 if we have insufficient rights to query the process handle.
72 return GetProcessId(process
);
75 bool GetProcessIntegrityLevel(ProcessHandle process
, IntegrityLevel
*level
) {
79 if (win::GetVersion() < base::win::VERSION_VISTA
)
83 if (!OpenProcessToken(process
, TOKEN_QUERY
| TOKEN_QUERY_SOURCE
,
87 win::ScopedHandle
scoped_process_token(process_token
);
89 DWORD token_info_length
= 0;
90 if (GetTokenInformation(process_token
, TokenIntegrityLevel
, NULL
, 0,
91 &token_info_length
) ||
92 GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
95 scoped_ptr
<char[]> token_label_bytes(new char[token_info_length
]);
96 if (!token_label_bytes
.get())
99 TOKEN_MANDATORY_LABEL
* token_label
=
100 reinterpret_cast<TOKEN_MANDATORY_LABEL
*>(token_label_bytes
.get());
104 if (!GetTokenInformation(process_token
, TokenIntegrityLevel
, token_label
,
105 token_info_length
, &token_info_length
))
108 DWORD integrity_level
= *GetSidSubAuthority(token_label
->Label
.Sid
,
109 (DWORD
)(UCHAR
)(*GetSidSubAuthorityCount(token_label
->Label
.Sid
)-1));
111 if (integrity_level
< SECURITY_MANDATORY_MEDIUM_RID
) {
112 *level
= LOW_INTEGRITY
;
113 } else if (integrity_level
>= SECURITY_MANDATORY_MEDIUM_RID
&&
114 integrity_level
< SECURITY_MANDATORY_HIGH_RID
) {
115 *level
= MEDIUM_INTEGRITY
;
116 } else if (integrity_level
>= SECURITY_MANDATORY_HIGH_RID
) {
117 *level
= HIGH_INTEGRITY
;