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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef mozilla_SameBinary_h
#define mozilla_SameBinary_h
#include "mozilla/WinHeaderOnlyUtils.h"
#include "mozilla/NativeNt.h"
#include "nsWindowsHelpers.h"
namespace mozilla {
class ProcessImagePath final {
PathType mType;
LauncherVoidResult mLastError;
// Using a larger buffer because an NT path may exceed MAX_PATH.
WCHAR mPathBuffer[(MAX_PATH * 2) + 1];
public:
// Initialize with an NT path string of a given process handle
explicit ProcessImagePath(const nsAutoHandle& aProcess)
: mType(PathType::eNtPath), mLastError(Ok()) {
DWORD len = mozilla::ArrayLength(mPathBuffer);
if (!::QueryFullProcessImageNameW(aProcess.get(), PROCESS_NAME_NATIVE,
mPathBuffer, &len)) {
mLastError = LAUNCHER_ERROR_FROM_LAST();
return;
}
}
// Initizlize with a DOS path string of a given imagebase address
explicit ProcessImagePath(HMODULE aImageBase)
: mType(PathType::eDosPath), mLastError(Ok()) {
DWORD len = ::GetModuleFileNameW(aImageBase, mPathBuffer,
mozilla::ArrayLength(mPathBuffer));
if (!len || len == mozilla::ArrayLength(mPathBuffer)) {
mLastError = LAUNCHER_ERROR_FROM_LAST();
return;
}
}
bool IsError() const { return mLastError.isErr(); }
const WindowsErrorType& GetError() const { return mLastError.inspectErr(); }
FileUniqueId GetId() const { return FileUniqueId(mPathBuffer, mType); }
bool CompareNtPaths(const ProcessImagePath& aOther) const {
if (mLastError.isErr() || aOther.mLastError.isErr() ||
mType != PathType::eNtPath || aOther.mType != PathType::eNtPath) {
return false;
}
UNICODE_STRING path1, path2;
::RtlInitUnicodeString(&path1, mPathBuffer);
::RtlInitUnicodeString(&path2, aOther.mPathBuffer);
return !!::RtlEqualUnicodeString(&path1, &path2, TRUE);
}
};
enum class ImageFileCompareOption {
Default,
CompareNtPathsOnly,
};
static inline mozilla::LauncherResult<bool> IsSameBinaryAsParentProcess(
ImageFileCompareOption aOption = ImageFileCompareOption::Default) {
mozilla::LauncherResult<DWORD> parentPid = mozilla::nt::GetParentProcessId();
if (parentPid.isErr()) {
return parentPid.propagateErr();
}
nsAutoHandle parentProcess(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, parentPid.unwrap()));
if (!parentProcess.get()) {
DWORD err = ::GetLastError();
if (err == ERROR_INVALID_PARAMETER) {
// The process identified by parentPid has already exited. This is a
// common case when the parent process is not Firefox, thus we should
// return false instead of erroring out.
return false;
}
return LAUNCHER_ERROR_FROM_WIN32(err);
}
ProcessImagePath parentExe(parentProcess);
if (parentExe.IsError()) {
return ::mozilla::Err(parentExe.GetError());
}
if (aOption == ImageFileCompareOption::Default) {
bool skipFileIdComparison = false;
FileUniqueId id1 = parentExe.GetId();
if (id1.IsError()) {
// We saw a number of Win7 users failed to call NtOpenFile with
// STATUS_OBJECT_PATH_NOT_FOUND for an unknown reason. In this
// particular case, we fall back to the logic to compare NT path
// strings instead of a file id which will not fail because we don't
// need to open a file handle.
#if !defined(STATUS_OBJECT_PATH_NOT_FOUND)
constexpr NTSTATUS STATUS_OBJECT_PATH_NOT_FOUND = 0xc000003a;
#endif
const LauncherError& err = id1.GetError();
if (err.mError !=
WindowsError::FromNtStatus(STATUS_OBJECT_PATH_NOT_FOUND)) {
return ::mozilla::Err(err);
}
skipFileIdComparison = true;
}
if (!skipFileIdComparison) {
ProcessImagePath ourExe(nullptr);
if (ourExe.IsError()) {
return ::mozilla::Err(ourExe.GetError());
}
FileUniqueId id2 = ourExe.GetId();
if (id2.IsError()) {
return ::mozilla::Err(id2.GetError());
}
return id1 == id2;
}
}
nsAutoHandle ourProcess(::GetCurrentProcess());
ProcessImagePath ourExeNt(ourProcess);
if (ourExeNt.IsError()) {
return ::mozilla::Err(ourExeNt.GetError());
}
return parentExe.CompareNtPaths(ourExeNt);
}
} // namespace mozilla
#endif // mozilla_SameBinary_h
|