Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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. | |
| 4 | |
| 5 #include "components/browser_watcher/system_session_analyzer_win.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 #include <winevt.h> | |
| 9 | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "base/time/time.h" | |
| 14 | |
| 15 namespace browser_watcher { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // The name of the log channel to query. | |
| 20 const wchar_t kChannelName[] = L"System"; | |
| 21 | |
| 22 // Event ids of system startup / shutdown events. These were obtained from | |
| 23 // inspection of the System log in Event Viewer on Windows 10: | |
| 24 // - id 6005: "The Event log service was started." | |
| 25 // - id 6006: "The Event log service was stopped." | |
| 26 // - id 6008: "The previous system shutdown at <time> on <date> was | |
| 27 // unexpected." | |
| 28 const uint16_t kIdSessionStart = 6005U; | |
| 29 const uint16_t kIdSessionEnd = 6006U; | |
| 30 const uint16_t kIdSessionEndUnclean = 6008U; | |
| 31 | |
| 32 // An XPATH expression to query for system startup / shutdown events. The query | |
| 33 // is expected to retrieve exactly one event for each startup (kIdSessionStart) | |
| 34 // and one event for each shutdown (either kIdSessionEnd or | |
| 35 // kIdSessionEndUnclean). | |
| 36 const wchar_t kSessionEventsQuery[] = | |
| 37 L"*[System[Provider[@Name='eventlog']" | |
| 38 L" and (EventID=6005 or EventID=6006 or EventID=6008)]]"; | |
| 39 | |
| 40 // XPath expressions to attributes of interest. | |
| 41 const wchar_t kEventIdPath[] = L"Event/System/EventID"; | |
| 42 const wchar_t kEventTimePath[] = L"Event/System/TimeCreated/@SystemTime"; | |
| 43 | |
| 44 // The timeout to use for calls to ::EvtNext. | |
| 45 const uint32_t kTimeoutMs = 5000; | |
| 46 | |
| 47 struct EvtHandleCloser { | |
| 48 using pointer = EVT_HANDLE; | |
| 49 void operator()(EVT_HANDLE handle) const { | |
| 50 if (handle) | |
| 51 ::EvtClose(handle); | |
| 52 } | |
| 53 }; | |
| 54 | |
| 55 using EvtHandle = std::unique_ptr<EVT_HANDLE, EvtHandleCloser>; | |
| 56 | |
| 57 base::Time ULLFileTimeToTime(ULONGLONG time_ulonglong) { | |
| 58 // Copy low / high parts as FILETIME is not always 64bit aligned. | |
| 59 ULARGE_INTEGER time; | |
| 60 time.QuadPart = time_ulonglong; | |
| 61 FILETIME ft; | |
| 62 ft.dwLowDateTime = time.LowPart; | |
| 63 ft.dwHighDateTime = time.HighPart; | |
| 64 | |
| 65 return base::Time::FromFileTime(ft); | |
| 66 } | |
| 67 | |
| 68 // Create a render context (i.e. specify attributes of interest). | |
| 69 EvtHandle CreateRenderContext() { | |
| 70 LPCWSTR value_paths[] = {kEventIdPath, kEventTimePath}; | |
| 71 const DWORD kValueCnt = arraysize(value_paths); | |
| 72 | |
| 73 EVT_HANDLE context = NULL; | |
| 74 context = | |
| 75 ::EvtCreateRenderContext(kValueCnt, value_paths, EvtRenderContextValues); | |
| 76 if (!context) | |
| 77 DLOG(ERROR) << "Failed to create render context."; | |
| 78 | |
| 79 return EvtHandle(context); | |
| 80 } | |
| 81 | |
| 82 bool GetEventInfo(EVT_HANDLE context, | |
| 83 EVT_HANDLE event, | |
| 84 SystemSessionAnalyzer::EventInfo* info) { | |
| 85 DCHECK(context); | |
| 86 DCHECK(event); | |
| 87 DCHECK(info); | |
| 88 | |
| 89 // Retrieve attributes of interest from the event. We expect the context to | |
| 90 // specify the retrieval of two attributes (event id and event time), each | |
| 91 // with a specific type. | |
| 92 const DWORD kAttributeCnt = 2U; | |
| 93 std::vector<EVT_VARIANT> buffer(kAttributeCnt); | |
| 94 DWORD buffer_size = kAttributeCnt * sizeof(EVT_VARIANT); | |
| 95 DWORD buffer_used = 0U; | |
| 96 DWORD retrieved_attribute_cnt = 0U; | |
| 97 if (!::EvtRender(context, event, EvtRenderEventValues, buffer_size, | |
| 98 buffer.data(), &buffer_used, &retrieved_attribute_cnt)) { | |
| 99 DLOG(ERROR) << "Failed to render the event."; | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 // Validate the count and types of the retrieved attributes. | |
| 104 if ((retrieved_attribute_cnt != kAttributeCnt) || | |
| 105 (buffer[0].Type != EvtVarTypeUInt16) || | |
| 106 (buffer[1].Type != EvtVarTypeFileTime)) { | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 info->event_id = buffer[0].UInt16Val; | |
| 111 info->event_time = ULLFileTimeToTime(buffer[1].FileTimeVal); | |
| 112 | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 } // namespace | |
| 117 | |
| 118 SystemSessionAnalyzer::SystemSessionAnalyzer(uint32_t session_cnt) | |
| 119 : session_cnt_(session_cnt) {} | |
| 120 | |
| 121 SystemSessionAnalyzer::~SystemSessionAnalyzer() {} | |
| 122 | |
| 123 SystemSessionAnalyzer::Status SystemSessionAnalyzer::IsSessionUnclean( | |
| 124 base::Time timestamp) { | |
| 125 if (!initialized_) { | |
| 126 DCHECK(!init_success_); | |
| 127 init_success_ = Initialize(); | |
| 128 initialized_ = true; | |
| 129 } | |
| 130 if (!init_success_) | |
| 131 return FAILED; | |
| 132 if (timestamp < coverage_start_) | |
| 133 return OUTSIDE_RANGE; | |
| 134 | |
| 135 // Get the first session starting after the timestamp. | |
| 136 std::map<base::Time, base::TimeDelta>::const_iterator it = | |
| 137 unclean_sessions_.upper_bound(timestamp); | |
| 138 if (it == unclean_sessions_.begin()) | |
| 139 return CLEAN; // No prior unclean session. | |
| 140 | |
| 141 // Get the previous session and see if it encompasses the timestamp. | |
| 142 --it; | |
| 143 bool is_spanned = (timestamp - it->first) <= it->second; | |
| 144 return is_spanned ? UNCLEAN : CLEAN; | |
| 145 } | |
| 146 | |
| 147 bool SystemSessionAnalyzer::Initialize() { | |
| 148 std::vector<SystemSessionAnalyzer::EventInfo> events; | |
| 149 if (!FetchEvents(&events)) | |
| 150 return false; | |
| 151 | |
| 152 // Validate the number and ordering of events (newest to oldest). The | |
| 153 // expectation is a (start / [unclean]shutdown) pair of events for each | |
| 154 // session, as well as an additional event for the current session's start. | |
| 155 size_t event_cnt = events.size(); | |
| 156 if ((event_cnt % 2) == 0) | |
| 157 return false; // Even number is unexpected. | |
| 158 if (event_cnt < 3) | |
| 159 return false; // Not enough data for a single session. | |
| 160 for (size_t i = 1; i < event_cnt; ++i) { | |
| 161 if (events[i - 1].event_time < events[i].event_time) | |
| 162 return false; | |
| 163 } | |
| 164 | |
| 165 // Step through (start / shutdown) event pairs, validating the types of events | |
| 166 // and recording unclean sessions. | |
| 167 std::map<base::Time, base::TimeDelta> unclean_sessions; | |
| 168 size_t start = 2U; | |
| 169 size_t end = 1U; | |
| 170 for (; start < event_cnt && end < event_cnt; start += 2, end += 2) { | |
| 171 uint16_t start_id = events[start].event_id; | |
| 172 uint16_t end_id = events[end].event_id; | |
| 173 if (start_id != kIdSessionStart) | |
| 174 return false; // Unexpected event type. | |
| 175 if (end_id != kIdSessionEnd && end_id != kIdSessionEndUnclean) | |
| 176 return false; // Unexpected event type. | |
| 177 | |
| 178 if (end_id == kIdSessionEnd) | |
| 179 continue; // This is a clean session. | |
| 180 | |
| 181 unclean_sessions.insert( | |
| 182 std::make_pair(events[start].event_time, | |
| 183 events[end].event_time - events[start].event_time)); | |
| 184 } | |
| 185 | |
| 186 unclean_sessions_.swap(unclean_sessions); | |
| 187 coverage_start_ = events[event_cnt - 1].event_time; | |
|
Sigurður Ásgeirsson
2017/03/07 14:38:39
DCHECK event_cnt > 0 for defense against maintenan
manzagop (departed)
2017/03/07 15:49:02
Done.
| |
| 188 | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 192 bool SystemSessionAnalyzer::FetchEvents( | |
| 193 std::vector<EventInfo>* event_infos) const { | |
| 194 DCHECK(event_infos); | |
| 195 | |
| 196 // Query for the events. Note: requesting events from newest to oldest. | |
| 197 EVT_HANDLE query_raw = | |
| 198 ::EvtQuery(nullptr, kChannelName, kSessionEventsQuery, | |
| 199 EvtQueryChannelPath | EvtQueryReverseDirection); | |
| 200 if (!query_raw) { | |
| 201 DLOG(ERROR) << "Event query failed."; | |
| 202 return false; | |
| 203 } | |
| 204 EvtHandle query(query_raw); | |
| 205 | |
| 206 // Retrieve events: 2 events per session, plus the current session's start. | |
| 207 DWORD desired_event_cnt = 2U * session_cnt_ + 1U; | |
| 208 std::vector<EVT_HANDLE> events_raw(desired_event_cnt, NULL); | |
| 209 DWORD event_cnt = 0U; | |
| 210 BOOL success = ::EvtNext(query.get(), desired_event_cnt, events_raw.data(), | |
| 211 kTimeoutMs, 0, &event_cnt); | |
| 212 | |
| 213 // Ensure handles get closed. The MSDN sample seems to imply handles may need | |
| 214 // to be closed event in if EvtNext failed. | |
| 215 std::vector<EvtHandle> events(desired_event_cnt); | |
| 216 for (size_t i = 0; i < event_cnt; ++i) | |
| 217 events[i].reset(events_raw[i]); | |
| 218 | |
| 219 if (!success) { | |
| 220 DLOG(ERROR) << "Failed to retrieve events."; | |
| 221 return false; | |
| 222 } | |
| 223 | |
| 224 // Extract information from the events. | |
| 225 EvtHandle render_context = CreateRenderContext(); | |
| 226 if (!render_context.get()) | |
| 227 return false; | |
| 228 | |
| 229 std::vector<EventInfo> event_infos_tmp; | |
| 230 event_infos_tmp.reserve(event_cnt); | |
| 231 | |
| 232 EventInfo info = {}; | |
| 233 for (size_t i = 0; i < event_cnt; ++i) { | |
| 234 if (!GetEventInfo(render_context.get(), events[i].get(), &info)) | |
| 235 return false; | |
| 236 event_infos_tmp.push_back(info); | |
| 237 } | |
| 238 | |
| 239 event_infos->swap(event_infos_tmp); | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 243 } // namespace browser_watcher | |
| OLD | NEW |