Commit graph

65 commits

Author SHA1 Message Date
Hermès Bélusca-Maïto 86e0d5e9b8
[NTOS:MM/PS] Remove code duplication between LookupEntryPoint/MiLocateExportName/MiFindExportedRoutineByName. (#4918)
As it turns out, those three functions were duplicating the same code
between each other. Reimplement these in terms of a common helper,
RtlFindExportedRoutineByName().
Indeed: MiFindExportedRoutineByName() was just MiLocateExportName()
but taking a PANSI_STRING instead of a NULL-terminated string.

A similar state of affairs also existed in Windows <= 2003, and the
MS guys also noticed it. Both routines have been then merged and renamed
to MiFindExportedRoutineByName() on Windows 8 (taking a PCSTR instead),
and finally renamed and exported as RtlFindExportedRoutineByName()
on Windows 10.
2023-08-29 17:26:57 +02:00
Hermès Bélusca-Maïto 4e55236662
[NTOS:MM/PS] De-duplicate export name-to-ordinal functionality (#4918)
It was implemented in psmgr.c but in a recursive way. That implementation
is replaced, in the NameToOrdinal() helper, by the better non-recursive one
found in the MiLocateExportName() and MiFindExportedRoutineByName() functions.

This NameToOrdinal() helper is then called in lieu of the duplicated code
in MiLocateExportName() and MiFindExportedRoutineByName(). In addition,
one block of code in MiSnapThunk() is simplified in a similar manner.
2023-08-29 17:26:56 +02:00
George Bișoc 8e2fe925f2
[NTOS:PS] Do not reference the copied token twice and properly assign the impersonation level in case the server can't impersonate
As it currently stands the PsImpersonateClient routine does the following approach.
If impersonation couldn't be granted to a client the routine will make a copy
of the client's access token. As it makes a copy of the said token PsImpersonateClient
will reference the copied token after impersonation info have been filled out.
In the same code path we are assigning the desired level for impersonation to thread
impersonation info.

This is wrong for two reasons:

- On a copy situation the SeCopyClientToken routine holds a reference as the object
has been created. Referencing it at the bottom of the PsImpersonateClient routine
will make it that the token is referenced twice and whenever a server stops
impersonation the token still has an extra reference count which keeps the token
still alive in object database and memory space.

- If client impersonation is not possible the thread impersonation info should
have been assigned SecurityIdentification level to further indicate that the
actual impersonation of the thread is not currently in force but instead we
are assigning the impersonation level that is supplied by the caller. For instance
if the requested level is SecurityDelegation but impersonation is not possible
the level will be assigned that of SecurityDelegation yet the token has an
impersonation level of SecurityIdentification. This could lead to erratic behaviors
as well as potential impersonation escalation.

Fix the aforementioned issues by avoiding a double reference and properly assign
the impersonation level to SecurityIdentification if the server is not able to
impersonate the target client.
2023-06-09 11:53:55 +02:00
Hermès Bélusca-Maïto 1b25fe161c
[KERNEL32][NTOS:PS][RTL] Cleanup some DbgPrompt() calls. 2023-01-06 14:44:49 +01:00
Hermès Bélusca-Maïto 9c8a8cf2aa
[CSRSRV][NTOS:PS] Misc typos fixes. 2022-11-14 00:10:30 +01:00
George Bișoc d0d86ab588
[NTOSKRNL] Force a probe against ReturnLength on query & Misc ICIF stuff
NtQueryInformationToken is by far the only system call in NT where ReturnLength simply cannot be optional. On Windows this parameter is always probed and an argument to NULL directly leads to an access violation exception.
This is due to the fact of how tokens work, as its information contents (token user, owner, primary group, et al) are dynamic and can vary throughout over time in memory.

What happens on current ReactOS master however is that ReturnLength is only probed if the parameter is not NULL. On a NULL case scenario the probing checks succeed and NtQueryInformationToken fails later. For this, just get rid of CompleteProbing
parameter and opt in for a bit mask flag based approach, with ICIF_FORCE_RETURN_LENGTH_PROBE being set on DefaultQueryInfoBufferCheck which NtQueryInformationToken calls it to do sanity checks.

In addition to that...

- Document the ICIF probe helpers
- Annotate the ICIF prope helpers with SAL
- With the riddance of CompleteProbing and adoption of flags based approach, add ICIF_PROBE_READ_WRITE and ICIF_PROBE_READ flags alongside with ICIF_FORCE_RETURN_LENGTH_PROBE
2022-06-12 11:05:05 +02:00
George Bișoc be56aff102
[NTOS:PS] Use quota types on process quota querying
Quota limits on a block are enumerated on a per quota type basis thus we should use the values from PS_QUOTA_TYPE enumeration, not from POOL_TYPE.
2022-01-11 11:21:15 +01:00
George Bișoc ee697cfeef
[NTOS:PS] Dereference the quota block during process cleanup
Ensure that when we're cleaning up the EPROCESS object, that we are dereferencing the quota block the process in question was using. The routine will automatically request a quota block cleanup if the process that dereferenced the quota block was the last.
2022-01-11 10:11:10 +01:00
George Bișoc b22eefac88
[NTOS:PS] Process Quota Overhaul
-- Rewrite PspChargeProcessQuotaSpecifiedPool and PspReturnProcessQuotaSpecifiedPool private kernel routines, with the goal to implement the algorithms necessary to manage the fields of quota blocks (Usage, Return, Limit and Peak).
-- Invoke the Mm if quota limit raising or returning has to be done
-- When destroying a quota block, make sure that we're giving back all the rest of non-returned quotas to Memory Mm
-- Crash the system with QUOTA_UNDERFLOW if someone is returning way too much quota than it was previously charged
-- When a process exits, ensure that it doesn't hold up any charged quotas in QuotaUsage field of the process object, that way we're enforcing proper kernel consistency
-- Implement PsChargeSharedPoolQuota and PsChargeProcessPageFileQuota functions, used exclusively by the Object Manager. These routines are used to charge or return amount of quotas of a newly created object.
-- On PspInheritQuota, when assigning to process the default quota block if no parent process is given, we must increment the reference counts as we're using it
-- Handle the ProcessCount reference field, as it wasn't used
-- Annotate the functions with SAL
-- Document the code

=== REMARKS ===
Windows LogOn (Winlogon) is responsible for setting up a different quota block for all the processes within an interactive session, which is what we don't do. What we're currently doing instead is we're using the default block, PspDefaultQuotaBlock, for all the processes
across the system. The default block contains the default limits of -1 (which would imply no limits). By definition, the kernel won't ever return STATUS_QUOTA_EXCEEDED as we literally don't set up a definite limit for regular processes. This situation has to be tackled
in the future.

=== TODO FOR FUTURE ===
Most of the code in PspChargeProcessQuotaSpecifiedPool and PspReturnProcessQuotaSpecifiedPool private routines must be refactored in order to reduce the usage of the quota spin lock, possibly wrapping such code in a loop and whatnot.

CORE-17784
2022-01-11 10:11:09 +01:00
George Bișoc 3bc2d590a1
[NTOSKRNL] Regroup the pool allocation tags in one dedicated place
We have a special file, tag.h, which serves as a place to store whatever kernel pool allocation tag yet we still have some tags sparse over the kernel code... So just re-group them in one unique place.
2021-12-27 18:57:03 +01:00
Thomas Faber e8b79e89eb
[NTOS:PS] Fix copypasta in NtQueryInformationJobObject. CID 1441354 2021-11-13 21:23:40 -05:00
Hermès Bélusca-Maïto 9b1edceae1
[REACTOS] Fix some instances of DPRINTs with trailing whitespace before newlines. 2021-09-13 03:52:19 +02:00
George Bișoc 2791ecd303
[NTOS:PS] Fix several issues within info classes in AMD64 build
ProcessUserModeIOPL, ProcessWow64Information and ThreadZeroTlsCell classes fail on AMD64 build because of wrong IQS values assigned to them. Also explicitly tell the compiler that ProcessUserModeIOPL is strictly for x86 as user mode I/O privilege level is 32-bit stuff.
In addition to that, implement IQS_NO_TYPE_LENGTH macro which it'll be used for classes such as ProcessUserModeIOPL where type length is not required and that it should be 0. With that said, we indirectly fix a size length mismatch issue with ProcessUserModeIOPL on 32-bit of ReactOS as well.
2021-06-25 10:33:18 +02:00
Victor Perevertkin 34593d933b
[FORMATTING][NTOS][HAL][DRIVERS][SDK] Fix trailing spaces 2021-06-11 15:33:08 +03:00
George Bișoc a330b56787
[NTOS:PS] Enable alignment probing for thread/process information classes
In addition to that, here are some stuff done in this commit whilst testing:

- ICIF_QUERY_SIZE_VARIABLE and friends were badly misused, they should be used only when an information class whose information length size is dyanmic and not fixed. By removing such flags from erroneous classes, this fixes the STATUS_INFO_LENGTH_MISMATCH testcases.

- Use CHAR instead of UCHAR for classes that do not need alignment probing, as every other class in the table do, for the sake of consistency.

- ProcessEnableAlignmentFaultFixup uses BOOLEAN as type size, not CHAR. This fixes a testcase failure on ROS.

- Check for information length size before proceeding further on querying the process' cookie information.

- ProcessHandleTracing wants an alignment of a ULONG, not CHAR.

- Move PROCESS_LDT_INFORMATION and PROCESS_LDT_SIZE outside of NTOS_MODE_USER macro case. This fixes a compilation issue when enabling the alignment probing. My mistake of having them inside NTOS_MODE_USER case, sorry.

- On functions like NtQueryInformationThread and the Process equivalent, complete probing is not done at the beginning of the function, complete probing including if the buffer is writable alongside with datatype misalignment check that is. Instead such check is done on each information class case basis. With that said, we have to explicitly tell DefaultQueryInfoBufferCheck if we want a complete probing or not initially.
2021-06-06 17:14:22 +02:00
Timo Kreuzer e8496b4fbf [NTOS:PS/x64] Fix PspGetOrSetContextKernelRoutine
It now uses KiSetTrapContext to set the non-volatile context on the stack and the volatile context in the trap frame.
2021-06-02 18:25:36 +02:00
Jérôme Gardou 04e9251612 [NTOS:PS] Use KD routine to safely read memory from thread stack
Should fix a crash when hitting TAB+(Whatever I typed that triggerred this)
2021-05-10 19:11:21 +02:00
Stanislav Motylkov 83dbb6b27e
[NTOS:PS] Do not leak the Thread. Addendum to 0c8f0785 2021-05-08 00:32:14 +03:00
Stanislav Motylkov 0c8f07854b
[NTOS:PS] Fix misplaced Thread variable reference caught by RTC
Addendum to 2e88e2b9.
2021-05-07 21:54:10 +03:00
George Bișoc 242efae9a2
[NTOS:PS] Make sure we can impersonate the given token first
PsImpersonateClient blindly impersonates the requested client even though it doesn't know if the actual token given to the call can be impersonated for the thread of the client which we are going to begin impersonation. In the case where impersonation is not possible, make a copy of the given token and assign the newly one for impersonation instead.
CORE-17539
2021-05-02 16:55:20 +02:00
Eric Kohl a7d6483e65 [NTOS:PS] Revert 4d7062abb6 on request 2021-05-02 16:26:11 +02:00
Eric Kohl 9fa31e0f9b [NTOS:PS] Add the missing privilege check to NtSetInformationThread:ThreadPriority
This fixes the remaining failure in the NtSetInformationThread test.
2021-05-02 15:28:26 +02:00
Eric Kohl 4d7062abb6 [NTOS:PS] Add ThreadInformation probing to NtSetInformationThread
Also get rid of unused buffer check code.
This fixes two test failures.
2021-05-02 13:55:29 +02:00
Eric Kohl 2e88e2b904 [NTOS:PS] Rewrite NtSetInformationThread to match NtQueryInformationThread
The Information length must always be checked before referencing the thread object. This fixes a test failure.
2021-05-02 13:46:22 +02:00
Eric Kohl 5585767460 [NTOS:PS] Rewrite NtQueryInformationThread to match NtQueryInformationProcess
The information length must always be checked before referencing the thread object. This fixes the remaining test failure.
2021-05-02 12:46:55 +02:00
Serge Gautherie 8a61e4f08c [NTOS:PS] NtQueryInformationProcess(): Fix ProcessDeviceMap case
Fix Clang-Cl
'...\ntoskrnl\ps\query.c(583,33): warning: variable 'Status' is uninitialized when used here [-Wuninitialized]'

Addendum to 1074a9a.
2021-04-29 21:40:58 +02:00
Serge Gautherie c0961cac10 [NTOS:PS] NtQueryInformationProcess(): Sync' annotations 2021-04-29 21:40:58 +02:00
George Bișoc a340ec1767
[NTOS:PS] Guard the quota in a spin lock (#3419)
Prior to acquiring a quota from the process and do whatever it's needed to do (charge it or return it back), we must guard ourselves with a spinlock so that we may not get into potential race conditions. In Windows Server 2003, PspGivebackQuota and PspExpandQuota do the same thing and they're the equivalent to PspReturnProcessQuotaSpecifiedPool and PspChargeProcessQuotaSpecifiedPool in our codebase.
2021-03-04 04:43:30 +03:00
George Bișoc 6170b574f0
[NTOS:PS] Implement PS_QUOTA_TYPE and let the quota code use it (#3389)
This will replace the PoolIndex variable and as such we'll only be using the PS_QUOTA_TYPE enumeration, as Windows does. Both QuotaEntry, QuotaUsage and QuotaPeak depend explicitly or implicitly on this enumeration. Further details about this enum can be found in the following articles.
https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/ps/psquota/type.htm?tx=68,143
https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/ps/psquota/block.htm?tx=68,142,143
https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/ps/eprocess/index.htm (see QuotaPeak and QuotaUsage)
2021-03-02 20:09:58 +01:00
Jérôme Gardou 073ec9b0d4 [NTOSKRNL][NDK] Get rid of SECTION_OBJECT typedef
We will use the more recent SECTION (already used in ARM3) and this will avoid confusions
2021-02-03 09:41:21 +01:00
Victor Perevertkin 5c7ce4475e
[REACTOS] Cleanup INIT and some PAGE section allocations
- Change INIT_FUNCTION and INIT_SECTION to CODE_SEG("INIT") and DATA_SEG("INIT") respectively
- Remove INIT_FUNCTION from function prototypes
- Remove alloc_text pragma calls as they are not needed anymore
2020-11-02 21:45:31 +03:00
Timo Kreuzer d74037b666 [NTOS:PS] Fix size handling in ProcessDeviceMap
If you want to know the details, browse history or use git blame.
2020-09-28 00:24:53 +02:00
Timo Kreuzer cb22d5f697 [NTOS:PS] On x64 don't fail in NtSetInformationProcess with ProcessUserModeIOPL information class, instead just don't do anything.
For NT6+ appcompat setting return STATUS_NOT_IMPLEMENTED
2020-09-20 23:08:17 +02:00
Nguyen Trung Khanh bc584bdf99
[NTOS:PS] Fix stack memory disclosure in PsGetContextThread #3024
Fix stack memory disclosure in PsGetContextThread.
2020-08-15 19:18:35 +09:00
Thomas Faber db180c29c7
[NTOS:SE] Implement job case in PsImpersonateClient. CORE-8787 2020-05-17 16:05:00 +02:00
Andrew Boyarshin b607e0119f [NTOSKRNL][PS] Implement NtQueueApcThreadEx and use it in NtQueueApcThread
Actually rename NtQueueApcThread to NtQueueApcThreadEx and ignore one additional parameter for now.
2019-08-15 12:32:09 +02:00
Bișoc George 42ce8519b6 [FASTFAT][FLTMGR][NTOS] Do not explicitly call ObfDereferenceObject() (#1636)
"Most code should not directly call the platform-specific ObfDereferenceObject() function but use the ObDereferenceObject() macro instead."

CORE-16081
2019-06-22 16:20:58 +02:00
Pierre Schweitzer ad80715b1a
[NTOSKRNL] Implement NtSetInformationProcess:ProcessDeviceMap 2019-06-10 10:35:24 +02:00
Pierre Schweitzer f8a4d31da4
[NTOSKRNL] On process primary token change, dereference device map 2019-06-02 10:00:17 +02:00
Pierre Schweitzer 445e895932
[NTOSKRNL] Simplify NtQueryInformationProcess:ProcessDeviceMap implementation 2019-06-01 17:40:23 +02:00
Pierre Schweitzer 1074a9aaff
[NTOSKRNL] Implement support for PROCESS_DEVICEMAP_INFORMATION_EX in NtQueryInformationProcess 2019-06-01 15:18:52 +02:00
Pierre Schweitzer b16a07fa69
[NTOSKRNL] Implement ObIsLUIDDeviceMapsEnabled and call it in NtQueryInformationProcess 2019-06-01 14:56:28 +02:00
Hervé Poussineau f7ec84eea0 [NTOS:KD] Remove some _WINDK_ usages
- Always include kd64.h
- Change KdpPrompt() prototype to be compatible between KDBG and _WINDK_
- Rename KdComponentTable to KdpComponentTable to prevent a conflict
- Add some functions stubs and global variables
2019-05-23 11:04:40 +02:00
Timo Kreuzer 8a4c5763da [NTOS:PS] Use NULL instead of 0 in ps/kill.c 2019-03-06 00:49:59 +01:00
Mark Jansen 5c05a29d3d
[APPHELP] Implement ShimDumpCache and ShimFlushCache
These functions simply call into the Base* functions,
having a prototype that is compatible with rundll32
CORE-11301
2019-02-25 20:00:34 +01:00
Thomas Faber e7de564bfc
[NTOS:MM] Implement big pool table expansion. CORE-15051 2019-01-09 08:18:38 +01:00
Pierre Schweitzer 47b48520b5
[NTOSKRNL] Reimplement !irpfind using !poolfind helpers
This allows avoiding one of the previous implementation limits:
leaked IRP not queued to a thread are now totally visible since
we look directly in the memory pool.
2019-01-06 12:49:57 +01:00
Pierre Schweitzer d6dc1fd231
[NTOSKRNL] Add a raw implementation of !irpfind in kdbg
This is far from perfect, and totally doesn't match the
WinDBG way of doing it. Instead of browsing pool to find
matching 'IRP' tags, we just browse all the processes
to find the queued IRP. This requires the IRPs to be queued,
obviously, and will make us miss the leaked IRPs, for instance.

Proper way to do it would be to implement !poolfind and then
rely on its implementation to find our IRPs.

Perhaps later ;-)
2019-01-01 22:00:56 +01:00
Pierre Schweitzer 1a93d83f92
[SDK] Add PsGetCurrentThreadTeb() to the DDK 2018-12-31 11:19:35 +01:00
Timo Kreuzer 71fefa32db
[NDK][NTOS] Add global definition of INIT_FUNCTION/INIT_SECTION (#779)
* Add an NDK header to define INIT_FUNCTION/INIT_SECTION globally
* Use _declspec(allocate(x)) and _declspec(code_seg(x)) on MSVC versions that support it
* Use INIT_FUNCTION on functions only and INIT_SECTION on data only (required by MSVC)
* Place INIT_FUNCTION before the return type (required by MSVC)
* Make sure declarations and implementations share the same modifiers (required by MSVC)
* Add a global linker option to suppress warnings about defined but unused INIT section
* Merge INIT section into .text in freeldr
2018-12-30 12:19:11 +01:00