Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Nov 2, 2023
1 parent adf7bf1 commit ae7fc95
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 147 deletions.
100 changes: 15 additions & 85 deletions src/host/ntprivapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ namespace
};
}

[[nodiscard]] NTSTATUS NtPrivApi::s_GetProcessParentId(_Inout_ PULONG ProcessId)
[[nodiscard]] NTSTATUS NtPrivApi::GetProcessParentId(_Inout_ PULONG ProcessId) const noexcept
{
if (!_complete)
{
return STATUS_UNSUCCESSFUL;
}

// TODO: Get Parent current not really available without winternl + NtQueryInformationProcess. http://osgvsowi/8394495
OBJECT_ATTRIBUTES oa;
#pragma warning(suppress : 26477) // This macro contains a bare NULL
Expand All @@ -30,13 +35,13 @@ namespace
ClientId.UniqueThread = nullptr;

HANDLE ProcessHandle;
auto Status = s_NtOpenProcess(&ProcessHandle, PROCESS_QUERY_LIMITED_INFORMATION, &oa, &ClientId);
auto Status = _fnNtOpenProcess(&ProcessHandle, PROCESS_QUERY_LIMITED_INFORMATION, &oa, &ClientId);

PROCESS_BASIC_INFORMATION_EXPANDED BasicInfo = { 0 };
if (SUCCEEDED_NTSTATUS(Status))
{
Status = s_NtQueryInformationProcess(ProcessHandle, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), nullptr);
LOG_IF_FAILED(s_NtClose(ProcessHandle));
Status = _fnNtQueryInformationProcess(ProcessHandle, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), nullptr);
LOG_IF_FAILED(_fnNtClose(ProcessHandle));
}

if (FAILED_NTSTATUS(Status))
Expand All @@ -49,88 +54,13 @@ namespace
return STATUS_SUCCESS;
}

[[nodiscard]] NTSTATUS NtPrivApi::s_NtOpenProcess(_Out_ PHANDLE ProcessHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ POBJECT_ATTRIBUTES ObjectAttributes,
_In_opt_ CLIENT_ID* ClientId)
{
auto hNtDll = _Instance()._hNtDll;

if (hNtDll != nullptr)
{
typedef NTSTATUS (*PfnNtOpenProcess)(HANDLE ProcessHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, CLIENT_ID * ClientId);

static auto pfn = (PfnNtOpenProcess)GetProcAddress(hNtDll, "NtOpenProcess");

if (pfn != nullptr)
{
return pfn(ProcessHandle, DesiredAccess, ObjectAttributes, ClientId);
}
}

return STATUS_UNSUCCESSFUL;
}

[[nodiscard]] NTSTATUS NtPrivApi::s_NtQueryInformationProcess(_In_ HANDLE ProcessHandle,
_In_ PROCESSINFOCLASS ProcessInformationClass,
_Out_ PVOID ProcessInformation,
_In_ ULONG ProcessInformationLength,
_Out_opt_ PULONG ReturnLength)
{
auto hNtDll = _Instance()._hNtDll;

if (hNtDll != nullptr)
{
typedef NTSTATUS (*PfnNtQueryInformationProcess)(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength);

static auto pfn = (PfnNtQueryInformationProcess)GetProcAddress(hNtDll, "NtQueryInformationProcess");

if (pfn != nullptr)
{
return pfn(ProcessHandle, ProcessInformationClass, ProcessInformation, ProcessInformationLength, ReturnLength);
}
}

return STATUS_UNSUCCESSFUL;
}

[[nodiscard]] NTSTATUS NtPrivApi::s_NtClose(_In_ HANDLE Handle)
{
auto hNtDll = _Instance()._hNtDll;

if (hNtDll != nullptr)
{
typedef NTSTATUS (*PfnNtClose)(HANDLE Handle);

static auto pfn = (PfnNtClose)GetProcAddress(hNtDll, "NtClose");

if (pfn != nullptr)
{
return pfn(Handle);
}
}

return STATUS_UNSUCCESSFUL;
}

NtPrivApi::NtPrivApi()
NtPrivApi::NtPrivApi() noexcept
{
// NOTE: Use LoadLibraryExW with LOAD_LIBRARY_SEARCH_SYSTEM32 flag below to avoid unneeded directory traversal.
// This has triggered CPG boot IO warnings in the past.
_hNtDll = LoadLibraryExW(L"ntdll.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
}

NtPrivApi::~NtPrivApi()
{
if (_hNtDll != nullptr)
if (const auto hNtDll = GetModuleHandleW(L"ntdll.dll"))
{
FreeLibrary(_hNtDll);
_hNtDll = nullptr;
_fnNtOpenProcess = reinterpret_cast<decltype(_fnNtOpenProcess)>(GetProcAddress(hNtDll, "NtOpenProcess"));
_fnNtQueryInformationProcess = reinterpret_cast<decltype(_fnNtQueryInformationProcess)>(GetProcAddress(hNtDll, "NtQueryInformationProcess"));
_fnNtClose = reinterpret_cast<decltype(_fnNtClose)>(GetProcAddress(hNtDll, "NtClose"));
}
}

NtPrivApi& NtPrivApi::_Instance()
{
static NtPrivApi ntapi;
return ntapi;
_complete = _fnNtOpenProcess && _fnNtQueryInformationProcess && _fnNtClose;
}
27 changes: 8 additions & 19 deletions src/host/ntprivapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,15 @@ Author(s):
class NtPrivApi sealed
{
public:
[[nodiscard]] static NTSTATUS s_GetProcessParentId(_Inout_ PULONG ProcessId);
NtPrivApi() noexcept;

~NtPrivApi();
[[nodiscard]] NTSTATUS GetProcessParentId(_Inout_ PULONG ProcessId) const noexcept;

private:
[[nodiscard]] static NTSTATUS s_NtOpenProcess(_Out_ PHANDLE ProcessHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ POBJECT_ATTRIBUTES ObjectAttributes,
_In_opt_ CLIENT_ID* ClientId);

[[nodiscard]] static NTSTATUS s_NtQueryInformationProcess(_In_ HANDLE ProcessHandle,
_In_ PROCESSINFOCLASS ProcessInformationClass,
_Out_ PVOID ProcessInformation,
_In_ ULONG ProcessInformationLength,
_Out_opt_ PULONG ReturnLength);

[[nodiscard]] static NTSTATUS s_NtClose(_In_ HANDLE Handle);

static NtPrivApi& _Instance();
HMODULE _hNtDll;

NtPrivApi();
// clang-format off
NTSTATUS(*_fnNtOpenProcess)(HANDLE ProcessHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, CLIENT_ID* ClientId) = nullptr;
NTSTATUS(*_fnNtQueryInformationProcess)(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength) = nullptr;
NTSTATUS(*_fnNtClose)(HANDLE Handle) = nullptr;
// clang-format no
bool _complete = false;
};
6 changes: 0 additions & 6 deletions src/interactivity/win32/Clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ void Clipboard::Paste()
CloseClipboard();
}

Clipboard& Clipboard::Instance()
{
static Clipboard clipboard;
return clipboard;
}

// Routine Description:
// - This routine pastes given Unicode string into the console window.
// Arguments:
Expand Down
6 changes: 0 additions & 6 deletions src/interactivity/win32/find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ using namespace Microsoft::Console::Interactivity;
INT_PTR CALLBACK FindDialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
// This bool is used to track which option - up or down - was used to perform the last search. That way, the next time the
// find dialog is opened, it will default to the last used option.
static auto reverse = true;
static auto caseInsensitive = true;
static std::wstring lastFindString;
static Search searcher;

switch (Message)
{
Expand Down
12 changes: 0 additions & 12 deletions src/interactivity/win32/icon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ Icon::~Icon()
_DestroyNonDefaultIcons();
}

// Routine Description:
// - Returns the singleton instance of the Icon
// Arguments:
// - <none>
// Return Value:
// - Reference to the singleton.
Icon& Icon::Instance()
{
static Icon i;
return i;
}

// Routine Description:
// - Gets the requested icons. Will return default icons if no icons have been set.
// Arguments:
Expand Down
2 changes: 1 addition & 1 deletion src/interactivity/win32/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace Microsoft::Console::Interactivity::Win32
_In_ SCREEN_INFORMATION* const pScreen);
void _CloseWindow() const;

static ATOM s_atomWindowClass;
ATOM s_atomWindowClass;
Settings* _pSettings;

HWND _hWnd;
Expand Down
3 changes: 0 additions & 3 deletions src/propsheet/ColorsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#undef MapVirtualKeyW
#undef GetKeyState

static BYTE ColorArray[4];
static int iColor;

// Routine Description:
// - Window proc for the color buttons
[[nodiscard]] LRESULT CALLBACK ColorTableControlProc(HWND hColor, UINT wMsg, WPARAM wParam, LPARAM lParam)
Expand Down
1 change: 0 additions & 1 deletion src/propsheet/PropSheetHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <shlguid.h>

#define PEMAGIC ((WORD)'P' + ((WORD)'E' << 8))
static CONSOLE_STATE_INFO g_csi;

using namespace Microsoft::WRL;

Expand Down
2 changes: 0 additions & 2 deletions src/propslib/TrueTypeFontList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Author(s):
class TrueTypeFontList
{
public:
static SINGLE_LIST_ENTRY s_ttFontList;

[[nodiscard]] static NTSTATUS s_Initialize();
[[nodiscard]] static NTSTATUS s_Destroy();

Expand Down
12 changes: 0 additions & 12 deletions src/server/WinNTControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ WinNTControl::WinNTControl() :
{
}

// Routine Description:
// - Provides the singleton pattern for WinNT control. Stores the single instance and returns it.
// Arguments:
// - <none>
// Return Value:
// - Reference to the single instance of NTDLL.dll wrapped methods.
WinNTControl& WinNTControl::GetInstance()
{
static WinNTControl Instance;
return Instance;
}

// Routine Description:
// - Provides access to the NtOpenFile method documented at:
// https://msdn.microsoft.com/en-us/library/bb432381(v=vs.85).aspx
Expand Down

1 comment on commit ae7fc95

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log for details.

Unrecognized words (1)

NTAPI

Previously acknowledged words that are now absent CProc NONCONST ntapi rgi rgui spammy traceloggingprovider userbase VProc VRaw wcsnicmp :arrow_right:
To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands

... in a clone of the git@github.com:PKRoma/Terminal.git repository
on the dev/lhecker/conhost-remove-singletons2 branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.21/apply.pl' |
perl - 'https://github.com/PKRoma/Terminal/actions/runs/6955846964/attempts/1'
Pattern suggestions ✂️ (1)

You could add these patterns to .github/actions/spelling/patterns/ae7fc95f1357787aa370ff607645aac2f72eed06.txt:

# Automatically suggested patterns
# hit-count: 1 file-count: 1
# tput arguments -- https://man7.org/linux/man-pages/man5/terminfo.5.html -- technically they can be more than 5 chars long...
\btput\s+(?:(?:-[SV]|-T\s*\w+)\s+)*\w{3,5}\b

Warnings (1)

See the 📜action log for details.

ℹ️ Warnings Count
ℹ️ candidate-pattern 1

See ℹ️ Event descriptions for more information.

✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. If it doesn't work for you, you can manually add (one word per line) / remove items to expect.txt and the excludes.txt files.

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.