Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / base / process / process_info_win.cc
blob2b9c40653fd75d3b1197cecd97ea2d93d384201e
1 // Copyright (c) 2012 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_info.h"
7 #include <windows.h>
9 #include "base/basictypes.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h"
12 #include "base/win/scoped_handle.h"
13 #include "base/win/windows_version.h"
15 namespace base {
17 // static
18 const Time CurrentProcessInfo::CreationTime() {
19 FILETIME creation_time = {};
20 FILETIME ignore = {};
21 if (::GetProcessTimes(::GetCurrentProcess(), &creation_time, &ignore,
22 &ignore, &ignore) == false)
23 return Time();
25 return Time::FromFileTime(creation_time);
28 IntegrityLevel GetCurrentProcessIntegrityLevel() {
29 if (win::GetVersion() < base::win::VERSION_VISTA)
30 return INTEGRITY_UNKNOWN;
32 HANDLE process_token;
33 if (!::OpenProcessToken(::GetCurrentProcess(),
34 TOKEN_QUERY | TOKEN_QUERY_SOURCE, &process_token)) {
35 return INTEGRITY_UNKNOWN;
37 win::ScopedHandle scoped_process_token(process_token);
39 DWORD token_info_length = 0;
40 if (::GetTokenInformation(process_token, TokenIntegrityLevel, NULL, 0,
41 &token_info_length) ||
42 ::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
43 return INTEGRITY_UNKNOWN;
46 scoped_ptr<char[]> token_label_bytes(new char[token_info_length]);
47 if (!token_label_bytes.get())
48 return INTEGRITY_UNKNOWN;
50 TOKEN_MANDATORY_LABEL* token_label =
51 reinterpret_cast<TOKEN_MANDATORY_LABEL*>(token_label_bytes.get());
52 if (!token_label)
53 return INTEGRITY_UNKNOWN;
55 if (!::GetTokenInformation(process_token, TokenIntegrityLevel, token_label,
56 token_info_length, &token_info_length)) {
57 return INTEGRITY_UNKNOWN;
60 DWORD integrity_level = *::GetSidSubAuthority(
61 token_label->Label.Sid,
62 static_cast<DWORD>(*::GetSidSubAuthorityCount(token_label->Label.Sid)-1));
64 if (integrity_level < SECURITY_MANDATORY_MEDIUM_RID)
65 return LOW_INTEGRITY;
67 if (integrity_level >= SECURITY_MANDATORY_MEDIUM_RID &&
68 integrity_level < SECURITY_MANDATORY_HIGH_RID) {
69 return MEDIUM_INTEGRITY;
72 if (integrity_level >= SECURITY_MANDATORY_HIGH_RID)
73 return HIGH_INTEGRITY;
75 NOTREACHED();
76 return INTEGRITY_UNKNOWN;
79 } // namespace base