Reworked initial process loader.

Crash the system if the initial process fails within 5 seconds.

svn path=/trunk/; revision=3748
This commit is contained in:
Eric Kohl 2002-11-12 19:12:34 +00:00
parent 2f04a917bb
commit eb1c9c77ee
3 changed files with 364 additions and 304 deletions

View file

@ -21,11 +21,10 @@
extern ULONG_PTR LdrHalBase; extern ULONG_PTR LdrHalBase;
NTSTATUS NTSTATUS
LdrLoadInitialProcess ( LdrLoadInitialProcess(PHANDLE ProcessHandle,
VOID PHANDLE ThreadHandle);
);
VOID VOID
LdrLoadAutoConfigDrivers ( LdrLoadAutoConfigDrivers (
VOID VOID

View file

@ -16,7 +16,7 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
/* $Id: main.c,v 1.139 2002/09/17 23:48:14 dwelch Exp $ /* $Id: main.c,v 1.140 2002/11/12 19:12:13 ekohl Exp $
* *
* PROJECT: ReactOS kernel * PROJECT: ReactOS kernel
* FILE: ntoskrnl/ke/main.c * FILE: ntoskrnl/ke/main.c
@ -282,9 +282,13 @@ InitSystemSharedUserPage (PCSZ ParameterLine)
} }
} }
VOID VOID
ExpInitializeExecutive(VOID) ExpInitializeExecutive(VOID)
{ {
LARGE_INTEGER Timeout;
HANDLE ProcessHandle;
HANDLE ThreadHandle;
ULONG BootDriverCount; ULONG BootDriverCount;
ULONG i; ULONG i;
ULONG start; ULONG start;
@ -575,7 +579,27 @@ ExpInitializeExecutive(VOID)
/* /*
* Launch initial process * Launch initial process
*/ */
LdrLoadInitialProcess(); Status = LdrLoadInitialProcess(&ProcessHandle,
&ThreadHandle);
if (!NT_SUCCESS(Status))
{
KeBugCheckEx(SESSION4_INITIALIZATION_FAILED, Status, 0, 0, 0);
}
/*
* Crash the system if the initial process terminates within 5 seconds.
*/
Timeout.QuadPart = 50000000;
Status = NtWaitForSingleObject(ProcessHandle,
FALSE,
&Timeout);
if (Status != STATUS_TIMEOUT)
{
KeBugCheckEx(SESSION5_INITIALIZATION_FAILED, Status, 0, 0, 0);
}
NtClose(ThreadHandle);
NtClose(ProcessHandle);
PsTerminateSystemThread(STATUS_SUCCESS); PsTerminateSystemThread(STATUS_SUCCESS);
} }

View file

@ -45,157 +45,247 @@
#include <internal/ldr.h> #include <internal/ldr.h>
#include <napi/teb.h> #include <napi/teb.h>
#define NDEBUG //#define NDEBUG
#include <internal/debug.h> #include <internal/debug.h>
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
/*
* TODO: Read the location of the initial process from command line before static NTSTATUS
* trying the registry - embedded setups, like the installation CD-ROM, may not LdrpMapProcessImage(PHANDLE SectionHandle,
* have a SYSTEM hive PUNICODE_STRING ImagePath)
*/
NTSTATUS LdrLoadInitialProcess (VOID)
{ {
NTSTATUS Status;
HANDLE ProcessHandle;
UNICODE_STRING ProcessName = {0, 0, NULL};
OBJECT_ATTRIBUTES ObjectAttributes; OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE FileHandle; HANDLE FileHandle;
HANDLE SectionHandle; NTSTATUS Status;
PIMAGE_NT_HEADERS NTHeaders;
PEPROCESS Process;
CONTEXT Context;
HANDLE ThreadHandle;
INITIAL_TEB InitialTeb;
ULONG OldPageProtection;
SECTION_IMAGE_INFORMATION Sii;
ULONG ResultLength;
PVOID ImageBaseAddress;
ULONG InitialStack[5];
#if 0
/* FIXME: Test this please */
HANDLE RootDir;
PWSTR Environment = L"SystemRoot=\\SystemRoot\0";
RTL_QUERY_REGISTRY_TABLE RegistryValues[] = {
{
NULL,
RTL_QUERY_REGISTRY_DIRECT,
L"Path",
&ProcessName,
REG_SZ,
L"\\SystemRoot\\system32\\smss.exe"
sizeof(L"\\SystemRoot\\system32\\smss.exe") - sizeof(WCHAR)
},
{ NULL, 0, NULL, NULL, 0, NULL, 0 }
};
/* try to query the SMSS path from the registry */
Status = RtlQueryRegistryValues(RTL_REGISTRY_CONTROL,
L"Session Manager",
RegistryValues,
NULL,
Environment);
/* failure or invalid data: use default */
if(!NT_SUCCESS(Status) || ProcessName.Length < sizeof(WCHAR))
RtlInitUnicodeStringFromLiteral(&ProcessName,
L"\\SystemRoot\\system32\\smss.exe");
/* relative path: open \SystemRoot\system32 */
else if(ProcessName.Buffer[0] != L'\\')
{
UNICODE_STRING DirPath;
RtlInitUnicodeStringFromLiteral(&DirPath, L"\\SystemRoot\\system32");
/* Open image file */
InitializeObjectAttributes(&ObjectAttributes, InitializeObjectAttributes(&ObjectAttributes,
&DirPath, ImagePath,
0, 0,
NULL, NULL,
NULL); NULL);
if(!NT_SUCCESS(ZwOpenFile(&RootDir, 0, &ObjectAttributes, NULL, 0, 0)))
/* failure: use default */
RtlInitUnicodeStringFromLiteral(&ProcessName,
L"\\SystemRoot\\system32\\smss.exe");
}
InitializeObjectAttributes(&ObjectAttributes,
&ProcessName,
0,
RootDir,
NULL);
#else
/*
* Get the absolute path to smss.exe using the
* SystemRoot link.
*/
RtlInitUnicodeStringFromLiteral(&ProcessName,
L"\\SystemRoot\\system32\\smss.exe");
/*
* Open process image to determine ImageBase
* and StackBase/Size.
*/
InitializeObjectAttributes(&ObjectAttributes,
&ProcessName,
0,
NULL,
NULL);
#endif
DPRINT("Opening image file %S\n", ObjectAttributes.ObjectName->Buffer); DPRINT("Opening image file %S\n", ObjectAttributes.ObjectName->Buffer);
Status = ZwOpenFile(&FileHandle, Status = NtOpenFile(&FileHandle,
FILE_ALL_ACCESS, FILE_ALL_ACCESS,
&ObjectAttributes, &ObjectAttributes,
NULL, NULL,
0, 0,
0); 0);
#if 0
/* FIXME? ExFreePool() should ignore non-pool data */
RtlFreeUnicodeString(&ProcessName);
NtClose(RootDir);
#endif
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT("Image open failed (Status was %x)\n", Status); DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
return Status; return(Status);
} }
/* /* Create a section for the image */
* Create a section for the image
*/
DPRINT("Creating section\n"); DPRINT("Creating section\n");
Status = ZwCreateSection(&SectionHandle, Status = NtCreateSection(SectionHandle,
SECTION_ALL_ACCESS, SECTION_ALL_ACCESS,
NULL, NULL,
NULL, NULL,
PAGE_READWRITE, PAGE_READWRITE,
SEC_COMMIT | SEC_IMAGE, SEC_COMMIT | SEC_IMAGE,
FileHandle); FileHandle);
NtClose(FileHandle);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT("ZwCreateSection failed (Status %x)\n", Status); DPRINT1("NtCreateSection() failed (Status %lx)\n", Status);
ZwClose(FileHandle); }
return(Status); return(Status);
} }
ZwClose(FileHandle);
/*
* Get information about the process image. static NTSTATUS
*/ LdrpCreateProcessEnvironment(HANDLE ProcessHandle,
Status = ZwQuerySection(SectionHandle, PUNICODE_STRING ImagePath,
PVOID* ImageBaseAddress)
{
NTSTATUS Status;
ULONG BytesWritten;
ULONG Offset;
#if 0
PVOID PpbBase;
ULONG PpbSize;
/* create the PPB */
PpbBase = NULL;
PpbSize = Ppb->AllocationSize;
Status = NtAllocateVirtualMemory(ProcessHandle,
&PpbBase,
0,
&PpbSize,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
if (!NT_SUCCESS(Status))
{
return(Status);
}
DPRINT("Ppb->AllocationSize %x\n", Ppb->AllocationSize);
DPRINT("Ppb->Size %x\n", Ppb->Size);
/* write process parameters block*/
NtWriteVirtualMemory(ProcessHandle,
PpbBase,
Ppb,
Ppb->AllocationSize,
&BytesWritten);
/* write pointer to process parameter block */
Offset = FIELD_OFFSET(PEB, ProcessParameters);
NtWriteVirtualMemory(ProcessHandle,
(PVOID)(PEB_BASE + Offset),
&PpbBase,
sizeof(PpbBase),
&BytesWritten);
#endif
/* Set image file name */
Status = NtSetInformationProcess(ProcessHandle,
ProcessImageFileName,
"SMSS",
5);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtSetInformationProcess() failed (Status %lx)\n", Status);
return(Status);
}
/* Read image base address. */
Offset = FIELD_OFFSET(PEB, ImageBaseAddress);
NtReadVirtualMemory(ProcessHandle,
(PVOID)(PEB_BASE + Offset),
ImageBaseAddress,
sizeof(PVOID),
&BytesWritten);
return(STATUS_SUCCESS);
}
static NTSTATUS
LdrpCreateStack(HANDLE ProcessHandle,
PINITIAL_TEB InitialTeb)
{
ULONG OldPageProtection;
NTSTATUS Status;
InitialTeb->StackAllocate = NULL;
/* Allocate the reserved stack space */
Status = NtAllocateVirtualMemory(ProcessHandle,
&InitialTeb->StackAllocate,
0,
&InitialTeb->StackReserve,
MEM_RESERVE,
PAGE_READWRITE);
if (!NT_SUCCESS(Status))
{
DPRINT1("Stack allocation failed (Status %x)", Status);
return(Status);
}
DPRINT("StackAllocate: %p ReserveSize: 0x%lX\n",
InitialTeb->StackAllocate, InitialTeb->StackReserve);
InitialTeb->StackBase = (PVOID)((ULONG)InitialTeb->StackAllocate + InitialTeb->StackReserve);
InitialTeb->StackLimit = (PVOID)((ULONG)InitialTeb->StackBase - InitialTeb->StackCommit);
DPRINT("StackBase: %p StackCommit: 0x%lX\n",
InitialTeb->StackBase, InitialTeb->StackCommit);
/* Commit stack */
Status = NtAllocateVirtualMemory(ProcessHandle,
&InitialTeb->StackLimit,
0,
&InitialTeb->StackCommit,
MEM_COMMIT,
PAGE_READWRITE);
if (!NT_SUCCESS(Status))
{
DPRINT1("Error comitting stack page!\n");
/* release the stack space */
NtFreeVirtualMemory(ProcessHandle,
InitialTeb->StackAllocate,
&InitialTeb->StackReserve,
MEM_RELEASE);
return(Status);
}
DPRINT1("StackLimit: %p\nStackCommit: 0x%lX\n",
InitialTeb->StackLimit,
InitialTeb->StackCommit);
/* Protect guard page */
Status = NtProtectVirtualMemory(ProcessHandle,
InitialTeb->StackLimit,
PAGE_SIZE,
PAGE_GUARD | PAGE_READWRITE,
&OldPageProtection);
if (!NT_SUCCESS(Status))
{
DPRINT1("Error protecting guard page!\n");
/* release the stack space */
NtFreeVirtualMemory(ProcessHandle,
InitialTeb->StackAllocate,
&InitialTeb->StackReserve,
MEM_RELEASE);
return(Status);
}
return(STATUS_SUCCESS);
}
NTSTATUS
LdrLoadInitialProcess(PHANDLE ProcessHandle,
PHANDLE ThreadHandle)
{
SECTION_IMAGE_INFORMATION Sii;
UNICODE_STRING ImagePath;
HANDLE SectionHandle;
CONTEXT Context;
INITIAL_TEB InitialTeb;
ULONG ResultLength;
PVOID ImageBaseAddress;
ULONG InitialStack[5];
NTSTATUS Status;
/* Get the absolute path to smss.exe. */
RtlInitUnicodeStringFromLiteral(&ImagePath,
L"\\SystemRoot\\system32\\smss.exe");
/* Map process image */
Status = LdrpMapProcessImage(&SectionHandle,
&ImagePath);
if (!NT_SUCCESS(Status))
{
DPRINT1("LdrpMapImage() failed (Status %lx)\n", Status);
return(Status);
}
/* Get information about the process image. */
Status = NtQuerySection(SectionHandle,
SectionImageInformation, SectionImageInformation,
&Sii, &Sii,
sizeof(Sii), sizeof(Sii),
&ResultLength); &ResultLength);
if (!NT_SUCCESS(Status) || ResultLength != sizeof(Sii)) if (!NT_SUCCESS(Status) || ResultLength != sizeof(Sii))
{ {
DPRINT("ZwQuerySection failed (Status %X)\n", Status); DPRINT1("ZwQuerySection failed (Status %X)\n", Status);
ZwClose(SectionHandle); NtClose(ProcessHandle);
NtClose(SectionHandle);
return(Status); return(Status);
} }
DPRINT("Creating process\n"); DPRINT("Creating process\n");
Status = ZwCreateProcess(&ProcessHandle, Status = NtCreateProcess(ProcessHandle,
PROCESS_ALL_ACCESS, PROCESS_ALL_ACCESS,
NULL, NULL,
SystemProcessHandle, SystemProcessHandle,
@ -203,114 +293,58 @@ NTSTATUS LdrLoadInitialProcess (VOID)
SectionHandle, SectionHandle,
NULL, NULL,
NULL); NULL);
NtClose(SectionHandle);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT("Could not create process\n"); DPRINT1("NtCreateProcess() failed (Status %lx)\n", Status);
return Status;
}
/*
* Create initial stack and thread
*/
/*
* Create page backed section for stack
*/
DPRINT("Allocating stack\n");
DPRINT("Referencing process\n");
Status = ObReferenceObjectByHandle(ProcessHandle,
PROCESS_ALL_ACCESS,
PsProcessType,
KernelMode,
(PVOID*)&Process,
NULL);
if (!NT_SUCCESS(Status))
{
DPRINT("ObReferenceObjectByProcess() failed (Status %x)\n", Status);
return(Status); return(Status);
} }
DPRINT("Attaching to process\n"); /* Create process environment */
KeAttachProcess(Process); DPRINT("Creating the process environment\n");
ImageBaseAddress = Process->Peb->ImageBaseAddress; Status = LdrpCreateProcessEnvironment(*ProcessHandle,
NTHeaders = RtlImageNtHeader(ImageBaseAddress); &ImagePath,
DPRINT("NTHeaders %x\n", NTHeaders); &ImageBaseAddress);
InitialTeb.StackReserve = NTHeaders->OptionalHeader.SizeOfStackReserve; if (!NT_SUCCESS(Status))
/* FIXME: use correct commit size */ {
InitialTeb.StackCommit = NTHeaders->OptionalHeader.SizeOfStackReserve - PAGE_SIZE; DPRINT1("LdrpCreateProcessEnvironment() failed (Status %lx)\n", Status);
// InitialTeb.StackCommit = NTHeaders->OptionalHeader.SizeOfStackCommit; NtClose(*ProcessHandle);
return(Status);
}
DPRINT("ImageBaseAddress: %p\n", ImageBaseAddress);
/* Calculate initial stack sizes */
if (Sii.StackReserve > 0x100000)
InitialTeb.StackReserve = Sii.StackReserve;
else
InitialTeb.StackReserve = 0x100000; /* 1MByte */
/* FIXME */
#if 0
if (Sii.StackCommit > PAGE_SIZE)
InitialTeb.StackCommit = Sii.StackCommit;
else
InitialTeb.StackCommit = PAGE_SIZE;
#endif
InitialTeb.StackCommit = InitialTeb.StackReserve - PAGE_SIZE;
/* add guard page size */ /* add guard page size */
InitialTeb.StackCommit += PAGE_SIZE; InitialTeb.StackCommit += PAGE_SIZE;
DPRINT("StackReserve 0x%lX StackCommit 0x%lX\n", DPRINT("StackReserve 0x%lX StackCommit 0x%lX\n",
InitialTeb.StackReserve, InitialTeb.StackCommit); InitialTeb.StackReserve, InitialTeb.StackCommit);
KeDetachProcess();
DPRINT("Dereferencing process\n");
ObDereferenceObject(Process);
DPRINT("Allocating stack\n");
InitialTeb.StackAllocate = NULL; /* Create the process stack */
Status = NtAllocateVirtualMemory(ProcessHandle, Status = LdrpCreateStack(*ProcessHandle,
&InitialTeb.StackAllocate, &InitialTeb);
0,
&InitialTeb.StackReserve,
MEM_RESERVE,
PAGE_READWRITE);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT("Stack allocation failed (Status %x)", Status); DPRINT1("Failed to write initial stack.\n");
NtClose(ProcessHandle);
return(Status); return(Status);
} }
DPRINT("StackAllocate: %p ReserveSize: 0x%lX\n",
InitialTeb.StackAllocate, InitialTeb.StackReserve);
InitialTeb.StackBase = (PVOID)((ULONG)InitialTeb.StackAllocate + InitialTeb.StackReserve);
InitialTeb.StackLimit = (PVOID)((ULONG)InitialTeb.StackBase - InitialTeb.StackCommit);
DPRINT("StackBase: %p StackCommit: 0x%lX\n",
InitialTeb.StackBase, InitialTeb.StackCommit);
/* Commit stack */
Status = NtAllocateVirtualMemory(ProcessHandle,
&InitialTeb.StackLimit,
0,
&InitialTeb.StackCommit,
MEM_COMMIT,
PAGE_READWRITE);
if (!NT_SUCCESS(Status))
{
/* release the stack space */
NtFreeVirtualMemory(ProcessHandle,
InitialTeb.StackAllocate,
&InitialTeb.StackReserve,
MEM_RELEASE);
DPRINT("Error comitting stack page!\n");
return(Status);
}
DPRINT("StackLimit: %p\nStackCommit: 0x%lX\n",
InitialTeb.StackLimit,
InitialTeb.StackCommit);
/* Protect guard page */
Status = NtProtectVirtualMemory(ProcessHandle,
InitialTeb.StackLimit,
PAGE_SIZE,
PAGE_GUARD | PAGE_READWRITE,
&OldPageProtection);
if (!NT_SUCCESS(Status))
{
/* release the stack space */
NtFreeVirtualMemory(ProcessHandle,
InitialTeb.StackAllocate,
&InitialTeb.StackReserve,
MEM_RELEASE);
DPRINT("Error protecting guard page!\n");
return(Status);
}
/* /*
* Initialize context to point to LdrStartup * Initialize context to point to LdrStartup
@ -331,7 +365,7 @@ NTSTATUS LdrLoadInitialProcess (VOID)
*/ */
InitialStack[0] = 0; InitialStack[0] = 0;
InitialStack[1] = PEB_BASE; InitialStack[1] = PEB_BASE;
Status = ZwWriteVirtualMemory(ProcessHandle, Status = NtWriteVirtualMemory(*ProcessHandle,
(PVOID)Context.Esp, (PVOID)Context.Esp,
InitialStack, InitialStack,
sizeof(InitialStack), sizeof(InitialStack),
@ -339,37 +373,40 @@ NTSTATUS LdrLoadInitialProcess (VOID)
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT1("Failed to write initial stack.\n"); DPRINT1("Failed to write initial stack.\n");
NtFreeVirtualMemory(*ProcessHandle,
InitialTeb.StackAllocate,
&InitialTeb.StackReserve,
MEM_RELEASE);
NtClose(*ProcessHandle);
return(Status); return(Status);
} }
/* /* Create initial thread */
* FIXME: Create process and let 'er rip DPRINT1("Creating thread for initial process\n");
*/ Status = NtCreateThread(ThreadHandle,
DPRINT("Creating thread for initial process\n");
Status = ZwCreateThread(&ThreadHandle,
THREAD_ALL_ACCESS, THREAD_ALL_ACCESS,
NULL, NULL,
ProcessHandle, *ProcessHandle,
NULL, NULL,
&Context, &Context,
&InitialTeb, &InitialTeb,
FALSE); FALSE);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT("Thread creation failed (Status %x)\n", Status); DPRINT("NtCreateThread() failed (Status %lx)\n", Status);
NtFreeVirtualMemory(ProcessHandle, NtFreeVirtualMemory(*ProcessHandle,
InitialTeb.StackAllocate, InitialTeb.StackAllocate,
&InitialTeb.StackReserve, &InitialTeb.StackReserve,
MEM_RELEASE); MEM_RELEASE);
/* FIXME: unmap the section here */ NtClose(*ProcessHandle);
/* FIXME: destroy the section here */
/* FIXME: Kill the process here */
return(Status); return(Status);
} }
DPRINT("Process created successfully\n");
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }