Add POSIX user-mode subsystem, NT syscall wrappers, and console-logon utility; update build files and docs#2
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements a POSIX subsystem scaffold, featuring a user-mode runtime DLL, a kernel-mode driver stub, and comprehensive strategy documentation. It also expands NT syscall wrapper coverage and adds a PowerShell utility for toggling console logon. Key feedback includes a critical error in PosixSpawnProcess where Win32 error codes are incorrectly treated as NTSTATUS, along with suggestions to improve type safety in function declarations, optimize string construction, and eliminate duplicated type definitions and documentation.
| if (!ok) { | ||
| resp->pid = -1; | ||
| resp->ntstatus = (int32_t)GetLastError(); | ||
| return PosixTranslateNtStatusToErrno(resp->ntstatus); | ||
| } |
There was a problem hiding this comment.
When CreateProcessA fails, GetLastError() returns a Win32 error code (DWORD), but it is then passed to PosixTranslateNtStatusToErrno, which expects an NTSTATUS code. This mismatch will lead to incorrect errno values being returned. You should map the Win32 error to errno directly.
if (!ok) {
resp->pid = -1;
DWORD win32Error = GetLastError();
resp->ntstatus = (int32_t)win32Error;
switch (win32Error) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return 2; /* ENOENT */
case ERROR_ACCESS_DENIED:
return 13; /* EACCES */
default:
return 5; /* EIO */
}
}| add_library(posixsubsystem SHARED | ||
| posix/runtime/posixsubsystem.c | ||
| posix/runtime/posixsubsystem.def | ||
| posix/include/posix_abi.h |
There was a problem hiding this comment.
|
|
||
| #include "ntos2nd/syscall.h" | ||
| #include "ntos2nd/kernel.h" | ||
| #include "ntos2nd/apibridge.h" /* For NT type definitions */ |
There was a problem hiding this comment.
The apibridge.h header is removed, but the type definitions it provided (like POBJECT_ATTRIBUTES) are now duplicated in NTCall.c and needed by NTCall.h. To improve maintainability and avoid type definition duplication, consider moving these common NT type definitions into a shared header file that both NTCall.c and consumers of NTCall.h can include.
| NTSTATUS NTAPI NtOpenProcessWrap(PHANDLE ProcessHandle, ACCESS_MASK DesiredAccess, void* ObjectAttributes, void* ClientId); | ||
| NTSTATUS NTAPI NtReadFileWrap(HANDLE FileHandle, HANDLE Event, PVOID ApcRoutine, PVOID ApcContext, void* IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key); | ||
| NTSTATUS NTAPI NtWriteFileWrap(HANDLE FileHandle, HANDLE Event, PVOID ApcRoutine, PVOID ApcContext, void* IoStatusBlock, const VOID* Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key); |
There was a problem hiding this comment.
The function declarations for NtOpenProcessWrap, NtReadFileWrap, and NtWriteFileWrap use void* for parameters that have more specific types in their implementations. Using void* bypasses compiler type-checking and reduces type safety. Please use the specific pointer types as used in NTCall.c to allow for better static analysis and prevent potential bugs. You will need to make the type definitions for POBJECT_ATTRIBUTES, PCLIENT_ID, and PIO_STATUS_BLOCK available to this header's consumers.
NTSTATUS NTAPI NtOpenProcessWrap(PHANDLE ProcessHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PCLIENT_ID ClientId);
NTSTATUS NTAPI NtReadFileWrap(HANDLE FileHandle, HANDLE Event, PVOID ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key);
NTSTATUS NTAPI NtWriteFileWrap(HANDLE FileHandle, HANDLE Event, PVOID ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, const VOID* Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key);| ### NT Native Wrapper Coverage | ||
|
|
||
| `NTCall.c` provides direct wrapper entry points routed through `NTOS2NDHandleSyscall`. | ||
| Current wrappers include: | ||
|
|
||
| - `NtCloseWrap` | ||
| - `NtOpenProcessWrap` | ||
| - `NtReadFileWrap` | ||
| - `NtWriteFileWrap` | ||
| - `NtAllocateVirtualMemoryWrap` | ||
|
|
||
| These wrappers are declared in `NTCall.h` for consumers embedding ntos2nd. | ||
|
|
| char cmdLine[4096]; | ||
| cmdLine[0] = '\0'; | ||
| strncat(cmdLine, req->path, sizeof(cmdLine) - 1); | ||
| if (req->argv_blob[0]) { | ||
| strncat(cmdLine, " ", sizeof(cmdLine) - strlen(cmdLine) - 1); | ||
| strncat(cmdLine, req->argv_blob, sizeof(cmdLine) - strlen(cmdLine) - 1); | ||
| } |
There was a problem hiding this comment.
The command line string is constructed using multiple strncat calls. Each call requires a strlen call to find the end of the string, which is inefficient. This approach can also be error-prone when calculating remaining buffer sizes. Using snprintf is a safer, more efficient, and more readable way to build the string.
char cmdLine[4096];
int ret = snprintf(cmdLine, sizeof(cmdLine), "%s%s%s",
req->path,
req->argv_blob[0] ? " " : "",
req->argv_blob[0] ? req->argv_blob : "");
if (ret < 0 || (size_t)ret >= sizeof(cmdLine)) {
resp->pid = -1;
resp->ntstatus = 0xC000000D; /* STATUS_INVALID_PARAMETER */
return 22; /* EINVAL */
}
Motivation
ntoskrnl.exe.ntos2ndsyscall dispatcher.authui.dllfor online/offline Windows roots and document it.Description
ntos2nd/posix/including a user-mode runtimeposixsubsystem.dll(posixsubsystem.c,posixsubsystem.def), a kernel-mode driver stubdriver/posixsubsys_driver.c, a shared ABI headerinclude/posix_abi.h, aPOSIX_SUBSYSTEM.mddesign doc, and a shortposix/README.mddescribing the approach.ntos2nd/CMakeLists.txtto build/installposixsubsystemand install the shared header, and add brief README notes about the NT wrapper coverage.ntos2nd/NTCall.cand declare them inntos2nd/NTCall.h, providing wrappers forNtCloseWrap,NtOpenProcessWrap,NtReadFileWrap,NtWriteFileWrap, andNtAllocateVirtualMemoryWraprouted throughNTOS2NDHandleSyscalland usingNTOS2NDGetHandlePointerwhere appropriate.utils/Enable-ConsoleLogon.ps1and update top-levelREADME.mdto document the utility and usage examples for online and offline Windows roots.ItemGrouplisting source files frommswindows/shell/LibreNT.Shell.csprojto fall back to SDK/project defaults.Testing
ntos2nd/CMakeLists.txtunderenable_testing()andadd_subdirectory(tests), but no new tests were added or run in this rollout.Codex Task