2008-03-09 14:11:42 +00:00
|
|
|
/*
|
2005-01-26 13:58:37 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: ntoskrnl/ex/uuid.c
|
|
|
|
* PURPOSE: UUID generator
|
2006-05-18 12:06:28 +00:00
|
|
|
* PROGRAMMERS: Eric Kohl
|
2018-12-20 02:46:42 +00:00
|
|
|
* Thomas Weidenmueller
|
2019-02-27 20:12:32 +00:00
|
|
|
* Pierre Schweitzer
|
2004-11-12 12:04:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
2004-11-12 12:04:32 +00:00
|
|
|
|
2004-12-18 13:27:58 +00:00
|
|
|
#define SEED_BUFFER_SIZE 6
|
|
|
|
|
|
|
|
/* Number of 100ns ticks per clock tick. To be safe, assume that the clock
|
|
|
|
resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
|
|
|
|
#define TICKS_PER_CLOCK_TICK 1000
|
|
|
|
#define SECSPERDAY 86400
|
|
|
|
#define TICKSPERSEC 10000000
|
|
|
|
|
|
|
|
/* UUID system time starts at October 15, 1582 */
|
|
|
|
#define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
|
|
|
|
#define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
|
|
|
|
|
2019-04-22 17:41:19 +00:00
|
|
|
/* 10000 in 100-ns model = 0.1 microsecond */
|
2019-02-27 20:12:32 +00:00
|
|
|
#define TIME_FRAME 10000
|
|
|
|
|
2021-05-11 15:13:14 +00:00
|
|
|
|
2004-12-18 13:27:58 +00:00
|
|
|
/* GLOBALS ****************************************************************/
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
FAST_MUTEX ExpUuidLock;
|
|
|
|
LARGE_INTEGER ExpUuidLastTimeAllocated;
|
|
|
|
ULONG ExpUuidSequenceNumber = 0;
|
|
|
|
BOOLEAN ExpUuidSequenceNumberValid;
|
|
|
|
BOOLEAN ExpUuidSequenceNumberNotSaved = FALSE;
|
2020-09-09 01:46:56 +00:00
|
|
|
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues = {0ULL, 0xFFFFFFFF, {{0, 0, {0x80, 0x6E, 0x6F, 0x6E, 0x69, 0x63}}}};
|
2019-02-27 20:12:32 +00:00
|
|
|
BOOLEAN ExpUuidCacheValid = FALSE;
|
|
|
|
ULONG ExpLuidIncrement = 1;
|
2019-03-02 10:10:37 +00:00
|
|
|
LARGE_INTEGER ExpLuid = {{0x3e9, 0x0}};
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2004-11-12 12:04:32 +00:00
|
|
|
/* FUNCTIONS ****************************************************************/
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2020-10-06 19:44:01 +00:00
|
|
|
CODE_SEG("INIT")
|
2020-05-23 13:56:10 +00:00
|
|
|
BOOLEAN
|
2006-05-18 12:06:28 +00:00
|
|
|
NTAPI
|
2019-02-27 20:12:32 +00:00
|
|
|
ExpUuidInitialization(VOID)
|
2004-12-18 13:27:58 +00:00
|
|
|
{
|
2019-02-27 07:54:48 +00:00
|
|
|
ExInitializeFastMutex(&ExpUuidLock);
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
ExpUuidSequenceNumberValid = FALSE;
|
|
|
|
KeQuerySystemTime(&ExpUuidLastTimeAllocated);
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
return TRUE;
|
2004-12-18 13:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
#define VALUE_BUFFER_SIZE 20
|
2004-12-19 12:52:42 +00:00
|
|
|
static NTSTATUS
|
2019-02-27 20:12:32 +00:00
|
|
|
ExpUuidLoadSequenceNumber(PULONG Sequence)
|
2004-12-18 13:27:58 +00:00
|
|
|
{
|
2006-05-18 12:06:28 +00:00
|
|
|
UCHAR ValueBuffer[VALUE_BUFFER_SIZE];
|
|
|
|
PKEY_VALUE_PARTIAL_INFORMATION ValueInfo;
|
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
2019-02-27 20:12:32 +00:00
|
|
|
UNICODE_STRING KeyName, ValueName;
|
2006-05-18 12:06:28 +00:00
|
|
|
HANDLE KeyHandle;
|
|
|
|
ULONG ValueLength;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\Software\\Microsoft\\Rpc");
|
|
|
|
RtlInitUnicodeString(&ValueName, L"UuidSequenceNumber");
|
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
InitializeObjectAttributes(&ObjectAttributes,
|
2019-02-27 20:12:32 +00:00
|
|
|
&KeyName,
|
|
|
|
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
|
2006-05-18 12:06:28 +00:00
|
|
|
NULL,
|
|
|
|
NULL);
|
2019-02-27 20:12:32 +00:00
|
|
|
Status = ZwOpenKey(&KeyHandle, GENERIC_READ, &ObjectAttributes);
|
2006-05-18 12:06:28 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("ZwOpenKey() failed (Status %lx)\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ValueBuffer;
|
|
|
|
Status = ZwQueryValueKey(KeyHandle,
|
2019-02-27 20:12:32 +00:00
|
|
|
&ValueName,
|
2006-05-18 12:06:28 +00:00
|
|
|
KeyValuePartialInformation,
|
|
|
|
ValueBuffer,
|
|
|
|
VALUE_BUFFER_SIZE,
|
|
|
|
&ValueLength);
|
|
|
|
ZwClose(KeyHandle);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("ZwQueryValueKey() failed (Status %lx)\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
2004-12-19 12:52:42 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
if (ValueInfo->Type != REG_DWORD || ValueInfo->DataLength != sizeof(DWORD))
|
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
*Sequence = *((PULONG)ValueInfo->Data);
|
2004-12-19 12:52:42 +00:00
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
DPRINT("Loaded sequence %lx\n", *Sequence);
|
2004-12-19 12:52:42 +00:00
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
return STATUS_SUCCESS;
|
2004-12-18 13:27:58 +00:00
|
|
|
}
|
2004-12-19 12:52:42 +00:00
|
|
|
#undef VALUE_BUFFER_SIZE
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2004-12-19 12:52:42 +00:00
|
|
|
static NTSTATUS
|
2019-02-27 20:12:32 +00:00
|
|
|
ExpUuidSaveSequenceNumber(PULONG Sequence)
|
2004-12-18 13:27:58 +00:00
|
|
|
{
|
2006-05-18 12:06:28 +00:00
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
2019-02-27 20:12:32 +00:00
|
|
|
UNICODE_STRING KeyName, ValueName;
|
2006-05-18 12:06:28 +00:00
|
|
|
HANDLE KeyHandle;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\Software\\Microsoft\\Rpc");
|
|
|
|
RtlInitUnicodeString(&ValueName, L"UuidSequenceNumber");
|
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
InitializeObjectAttributes(&ObjectAttributes,
|
2019-02-27 20:12:32 +00:00
|
|
|
&KeyName,
|
|
|
|
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
|
2006-05-18 12:06:28 +00:00
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
Status = ZwOpenKey(&KeyHandle,
|
2019-02-27 20:12:32 +00:00
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
2006-05-18 12:06:28 +00:00
|
|
|
&ObjectAttributes);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("ZwOpenKey() failed (Status %lx)\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = ZwSetValueKey(KeyHandle,
|
2019-02-27 20:12:32 +00:00
|
|
|
&ValueName,
|
2006-05-18 12:06:28 +00:00
|
|
|
0,
|
|
|
|
REG_DWORD,
|
|
|
|
Sequence,
|
|
|
|
sizeof(ULONG));
|
|
|
|
ZwClose(KeyHandle);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("ZwSetValueKey() failed (Status %lx)\n", Status);
|
|
|
|
}
|
|
|
|
|
2004-12-19 12:52:42 +00:00
|
|
|
return Status;
|
2004-12-18 13:27:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
* Warning! This function must be called
|
|
|
|
* with ExpUuidLock held!
|
|
|
|
*/
|
2004-12-18 13:27:58 +00:00
|
|
|
static VOID
|
2019-02-27 20:12:32 +00:00
|
|
|
ExpUuidSaveSequenceNumberIf(VOID)
|
2004-12-18 13:27:58 +00:00
|
|
|
{
|
2019-02-27 20:12:32 +00:00
|
|
|
NTSTATUS Status;
|
2004-12-20 14:07:34 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
PAGED_CODE();
|
2004-12-20 14:07:34 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/* Only save sequence if it has to */
|
|
|
|
if (ExpUuidSequenceNumberNotSaved == TRUE)
|
|
|
|
{
|
|
|
|
Status = ExpUuidSaveSequenceNumber(&ExpUuidSequenceNumber);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ExpUuidSequenceNumberNotSaved = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2004-12-18 13:27:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
* Warning! This function must be called
|
|
|
|
* with ExpUuidLock held!
|
|
|
|
*/
|
2004-12-18 13:27:58 +00:00
|
|
|
static NTSTATUS
|
2019-02-27 20:12:32 +00:00
|
|
|
ExpAllocateUuids(PULARGE_INTEGER Time,
|
|
|
|
PULONG Range,
|
|
|
|
PULONG Sequence)
|
2004-12-18 13:27:58 +00:00
|
|
|
{
|
2019-02-27 20:12:32 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
LARGE_INTEGER Counter, Frequency, CurrentTime, TimeDiff, ClockDiff;
|
2006-05-18 12:06:28 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
PAGED_CODE();
|
2006-05-18 12:06:28 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/* Initialize sequence number */
|
|
|
|
if (!ExpUuidSequenceNumberValid)
|
|
|
|
{
|
|
|
|
/* Try to load sequence number */
|
|
|
|
Status = ExpUuidLoadSequenceNumber(&ExpUuidSequenceNumber);
|
|
|
|
if (NT_SUCCESS(Status))
|
2006-05-18 12:06:28 +00:00
|
|
|
{
|
2019-02-27 20:12:32 +00:00
|
|
|
++ExpUuidSequenceNumber;
|
2006-05-18 12:06:28 +00:00
|
|
|
}
|
2019-02-27 20:12:32 +00:00
|
|
|
else
|
2006-05-18 12:06:28 +00:00
|
|
|
{
|
2019-02-27 20:12:32 +00:00
|
|
|
/* If we cannot, generate a "true" random */
|
|
|
|
Counter = KeQueryPerformanceCounter(&Frequency);
|
|
|
|
ExpUuidSequenceNumber ^= (ULONG_PTR)&Status ^ (ULONG_PTR)Sequence ^ Counter.LowPart ^ Counter.HighPart;
|
2006-05-18 12:06:28 +00:00
|
|
|
}
|
2019-02-27 20:12:32 +00:00
|
|
|
|
|
|
|
/* It's valid and to be saved */
|
|
|
|
ExpUuidSequenceNumberValid = TRUE;
|
|
|
|
ExpUuidSequenceNumberNotSaved = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeQuerySystemTime(&CurrentTime);
|
|
|
|
TimeDiff.QuadPart = CurrentTime.QuadPart - ExpUuidLastTimeAllocated.QuadPart;
|
|
|
|
/* If time went backwards, change sequence (see RFC example) */
|
|
|
|
if (TimeDiff.QuadPart < 0)
|
|
|
|
{
|
|
|
|
++ExpUuidSequenceNumber;
|
|
|
|
TimeDiff.QuadPart = 2 * TIME_FRAME;
|
|
|
|
|
|
|
|
/* It's to be saved */
|
|
|
|
ExpUuidSequenceNumberNotSaved = TRUE;
|
|
|
|
ExpUuidLastTimeAllocated.QuadPart = CurrentTime.QuadPart - 2 * TIME_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TimeDiff.QuadPart == 0)
|
|
|
|
{
|
|
|
|
return STATUS_RETRY;
|
2004-12-18 13:27:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 17:41:19 +00:00
|
|
|
/* If time diff > 0.1ms, squash it to reduce it to keep our clock resolution */
|
2019-02-27 20:12:32 +00:00
|
|
|
if (TimeDiff.HighPart > 0 || TimeDiff.QuadPart > TICKS_PER_CLOCK_TICK * TIME_FRAME)
|
|
|
|
{
|
|
|
|
TimeDiff.QuadPart = TICKS_PER_CLOCK_TICK * TIME_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TimeDiff.HighPart < 0 || TimeDiff.QuadPart <= TIME_FRAME)
|
|
|
|
{
|
|
|
|
*Range = TimeDiff.QuadPart;
|
|
|
|
ClockDiff.QuadPart = 0LL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*Range = TIME_FRAME;
|
2019-03-02 09:45:34 +00:00
|
|
|
ClockDiff.QuadPart = TimeDiff.QuadPart - TIME_FRAME;
|
2019-02-27 20:12:32 +00:00
|
|
|
--ClockDiff.HighPart;
|
|
|
|
}
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
Time->QuadPart = CurrentTime.QuadPart - *Range - ClockDiff.QuadPart;
|
|
|
|
ExpUuidLastTimeAllocated.QuadPart = CurrentTime.QuadPart - ClockDiff.QuadPart;
|
|
|
|
*Sequence = ExpUuidSequenceNumber;
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
return STATUS_SUCCESS;
|
2004-12-18 13:27:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
* Warning! This function must be called
|
|
|
|
* with ExpUuidLock held!
|
|
|
|
*/
|
|
|
|
static NTSTATUS
|
|
|
|
ExpUuidGetValues(PUUID_CACHED_VALUES_STRUCT CachedValues)
|
2008-04-23 20:38:37 +00:00
|
|
|
{
|
2019-02-27 20:12:32 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
ULARGE_INTEGER Time;
|
|
|
|
ULONG Range;
|
|
|
|
ULONG Sequence;
|
|
|
|
|
|
|
|
PAGED_CODE();
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/* Allocate UUIDs */
|
|
|
|
Status = ExpAllocateUuids(&Time, &Range, &Sequence);
|
|
|
|
if (Status == STATUS_RETRY)
|
|
|
|
{
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We need at least one UUID */
|
|
|
|
ASSERT(Range != 0);
|
|
|
|
|
|
|
|
/* Set up our internal cache
|
|
|
|
* See format_uuid_v1 in RFC4122 for magic values
|
|
|
|
*/
|
|
|
|
CachedValues->ClockSeqLow = Sequence;
|
|
|
|
CachedValues->ClockSeqHiAndReserved = (Sequence & 0x3F00) >> 8;
|
|
|
|
CachedValues->ClockSeqHiAndReserved |= 0x80;
|
|
|
|
CachedValues->AllocatedCount = Range;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time is relative to UUID time
|
|
|
|
* And we set last time range for all the possibly
|
|
|
|
* returnable UUID
|
|
|
|
*/
|
|
|
|
Time.QuadPart += TICKS_15_OCT_1582_TO_1601;
|
|
|
|
CachedValues->Time = Time.QuadPart + (Range - 1);
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2008-04-23 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2020-10-06 19:44:01 +00:00
|
|
|
CODE_SEG("INIT")
|
2020-05-23 13:56:10 +00:00
|
|
|
BOOLEAN
|
2019-02-27 20:12:32 +00:00
|
|
|
NTAPI
|
|
|
|
ExLuidInitialization(VOID)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
2008-04-23 20:38:37 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2015-09-28 07:44:49 +00:00
|
|
|
VOID
|
2008-04-23 20:38:37 +00:00
|
|
|
NTAPI
|
2015-09-28 07:44:49 +00:00
|
|
|
ExAllocateLocallyUniqueId(OUT LUID *LocallyUniqueId)
|
2008-04-23 20:38:37 +00:00
|
|
|
{
|
2019-02-27 20:12:32 +00:00
|
|
|
/* Atomically increment the luid */
|
2018-07-12 13:54:52 +00:00
|
|
|
*(LONG64*)LocallyUniqueId = InterlockedExchangeAdd64(&ExpLuid.QuadPart,
|
|
|
|
ExpLuidIncrement);
|
2008-04-23 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2015-09-28 07:44:49 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2008-04-23 20:38:37 +00:00
|
|
|
NtAllocateLocallyUniqueId(OUT LUID *LocallyUniqueId)
|
|
|
|
{
|
|
|
|
KPROCESSOR_MODE PreviousMode;
|
|
|
|
PAGED_CODE();
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2015-09-28 07:44:49 +00:00
|
|
|
/* Probe if user mode */
|
2008-04-23 20:38:37 +00:00
|
|
|
PreviousMode = ExGetPreviousMode();
|
2015-09-28 07:44:49 +00:00
|
|
|
if (PreviousMode != KernelMode)
|
2008-04-23 20:38:37 +00:00
|
|
|
{
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_TRY
|
2008-04-23 20:38:37 +00:00
|
|
|
{
|
|
|
|
ProbeForWrite(LocallyUniqueId,
|
|
|
|
sizeof(LUID),
|
|
|
|
sizeof(ULONG));
|
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2008-04-23 20:38:37 +00:00
|
|
|
{
|
2009-08-26 17:31:02 +00:00
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
2008-04-23 20:38:37 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
_SEH2_END;
|
2008-04-23 20:38:37 +00:00
|
|
|
}
|
2008-11-24 13:40:26 +00:00
|
|
|
|
2015-09-28 07:44:49 +00:00
|
|
|
/* Do the allocation */
|
2019-02-27 20:12:32 +00:00
|
|
|
ExAllocateLocallyUniqueId(LocallyUniqueId);
|
|
|
|
return STATUS_SUCCESS;
|
2008-04-23 20:38:37 +00:00
|
|
|
}
|
|
|
|
|
2005-08-05 06:13:23 +00:00
|
|
|
/*
|
2019-02-27 20:12:32 +00:00
|
|
|
* @implemented
|
2005-08-05 06:13:23 +00:00
|
|
|
*/
|
|
|
|
NTSTATUS
|
2006-05-18 12:06:28 +00:00
|
|
|
NTAPI
|
|
|
|
ExUuidCreate(OUT UUID *Uuid)
|
2005-08-05 06:13:23 +00:00
|
|
|
{
|
2019-02-27 20:12:32 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
LONG AllocatedCount;
|
|
|
|
LARGE_INTEGER Time;
|
|
|
|
BOOLEAN Valid;
|
|
|
|
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
/* Loop until we have an UUID to return */
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
/* Try to gather node values */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Time.QuadPart = ExpUuidCachedValues.Time;
|
|
|
|
|
2020-09-09 01:46:56 +00:00
|
|
|
RtlCopyMemory(Uuid->Data4,
|
|
|
|
ExpUuidCachedValues.GuidInit,
|
|
|
|
sizeof(Uuid->Data4));
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
Valid = ExpUuidCacheValid;
|
|
|
|
AllocatedCount = InterlockedDecrement(&ExpUuidCachedValues.AllocatedCount);
|
|
|
|
}
|
|
|
|
/* Loop till we can do it without being disturbed */
|
|
|
|
while (Time.QuadPart != ExpUuidCachedValues.Time);
|
|
|
|
|
|
|
|
/* We have more than an allocated UUID left, that's OK to return! */
|
|
|
|
if (AllocatedCount >= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, we're out of UUIDs, we need to allocate more
|
|
|
|
* We need to be alone to do it, so lock the mutex
|
|
|
|
*/
|
|
|
|
ExAcquireFastMutex(&ExpUuidLock);
|
|
|
|
if (Time.QuadPart == ExpUuidCachedValues.Time)
|
|
|
|
{
|
|
|
|
/* If allocation fails, bail out! */
|
|
|
|
Status = ExpUuidGetValues(&ExpUuidCachedValues);
|
|
|
|
if (Status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ExReleaseFastMutex(&ExpUuidLock);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save our current sequence if changed */
|
|
|
|
ExpUuidSaveSequenceNumberIf();
|
|
|
|
}
|
|
|
|
ExReleaseFastMutex(&ExpUuidLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Once here, we've got an UUID to return
|
|
|
|
* But, if our init wasn't sane, then, make
|
|
|
|
* sure it's only used locally
|
|
|
|
*/
|
|
|
|
if (!Valid)
|
|
|
|
{
|
|
|
|
Status = RPC_NT_UUID_LOCAL_ONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set our timestamp - see RFC4211 */
|
|
|
|
Time.QuadPart -= AllocatedCount;
|
|
|
|
Uuid->Data1 = Time.LowPart;
|
|
|
|
Uuid->Data2 = Time.HighPart;
|
|
|
|
/* We also set the bit for GUIDv1 */
|
|
|
|
Uuid->Data3 = ((Time.HighPart >> 16) & 0x0FFF) | 0x1000;
|
|
|
|
|
|
|
|
return Status;
|
2005-08-05 06:13:23 +00:00
|
|
|
}
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2004-11-12 12:04:32 +00:00
|
|
|
/*
|
2019-02-27 20:12:32 +00:00
|
|
|
* @implemented
|
2004-11-12 12:04:32 +00:00
|
|
|
*/
|
2006-05-18 12:06:28 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2004-11-12 12:04:32 +00:00
|
|
|
NtAllocateUuids(OUT PULARGE_INTEGER Time,
|
2006-05-18 12:06:28 +00:00
|
|
|
OUT PULONG Range,
|
|
|
|
OUT PULONG Sequence,
|
|
|
|
OUT PUCHAR Seed)
|
2004-11-12 12:04:32 +00:00
|
|
|
{
|
2006-05-18 12:06:28 +00:00
|
|
|
ULARGE_INTEGER IntTime;
|
2019-02-27 20:12:32 +00:00
|
|
|
ULONG IntRange, IntSequence;
|
2006-05-18 12:06:28 +00:00
|
|
|
NTSTATUS Status;
|
2018-12-19 07:07:28 +00:00
|
|
|
KPROCESSOR_MODE PreviousMode;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
PAGED_CODE();
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2018-12-19 07:07:28 +00:00
|
|
|
/* Probe if user mode */
|
|
|
|
PreviousMode = ExGetPreviousMode();
|
|
|
|
if (PreviousMode != KernelMode)
|
|
|
|
{
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForWrite(Time,
|
|
|
|
sizeof(ULARGE_INTEGER),
|
|
|
|
sizeof(ULONG));
|
|
|
|
|
|
|
|
ProbeForWrite(Range,
|
|
|
|
sizeof(ULONG),
|
|
|
|
sizeof(ULONG));
|
|
|
|
|
|
|
|
ProbeForWrite(Sequence,
|
|
|
|
sizeof(ULONG),
|
|
|
|
sizeof(ULONG));
|
|
|
|
|
|
|
|
ProbeForWrite(Seed,
|
|
|
|
SEED_BUFFER_SIZE,
|
|
|
|
sizeof(UCHAR));
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
_SEH2_YIELD(return _SEH2_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
}
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/* During allocation we must be alone */
|
2019-02-27 07:54:48 +00:00
|
|
|
ExAcquireFastMutex(&ExpUuidLock);
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
Status = ExpAllocateUuids(&IntTime,
|
|
|
|
&IntRange,
|
|
|
|
&IntSequence);
|
2006-05-18 12:06:28 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2004-12-18 13:27:58 +00:00
|
|
|
{
|
2019-02-27 07:54:48 +00:00
|
|
|
ExReleaseFastMutex(&ExpUuidLock);
|
2006-05-18 12:06:28 +00:00
|
|
|
return Status;
|
2004-12-18 13:27:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/* If sequence number was changed, save it */
|
|
|
|
ExpUuidSaveSequenceNumberIf();
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2019-02-27 20:12:32 +00:00
|
|
|
/* Allocation done, so we can release */
|
2019-02-27 07:54:48 +00:00
|
|
|
ExReleaseFastMutex(&ExpUuidLock);
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2018-12-19 07:15:11 +00:00
|
|
|
/* Write back UUIDs to caller */
|
2018-12-19 07:07:28 +00:00
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
Time->QuadPart = IntTime.QuadPart;
|
|
|
|
*Range = IntRange;
|
2019-02-27 20:12:32 +00:00
|
|
|
*Sequence = IntSequence;
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2018-12-19 07:07:28 +00:00
|
|
|
RtlCopyMemory(Seed,
|
2019-02-27 20:12:32 +00:00
|
|
|
&ExpUuidCachedValues.NodeId[0],
|
2018-12-19 07:07:28 +00:00
|
|
|
SEED_BUFFER_SIZE);
|
2018-12-20 02:45:43 +00:00
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
2018-12-19 07:07:28 +00:00
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(ExSystemExceptionFilter())
|
|
|
|
{
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
2004-12-18 13:27:58 +00:00
|
|
|
|
2018-12-20 02:45:43 +00:00
|
|
|
return Status;
|
2004-11-12 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-12-18 13:27:58 +00:00
|
|
|
* @implemented
|
2004-11-12 12:04:32 +00:00
|
|
|
*/
|
2006-05-18 12:06:28 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2004-11-12 12:04:32 +00:00
|
|
|
NtSetUuidSeed(IN PUCHAR Seed)
|
|
|
|
{
|
2018-12-18 22:08:19 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
BOOLEAN GotContext;
|
|
|
|
PACCESS_TOKEN Token;
|
|
|
|
SECURITY_SUBJECT_CONTEXT SubjectContext;
|
|
|
|
LUID CallerLuid, SystemLuid = SYSTEM_LUID;
|
|
|
|
|
2006-05-18 12:06:28 +00:00
|
|
|
PAGED_CODE();
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2018-12-18 22:08:19 +00:00
|
|
|
/* Should only be done by umode */
|
|
|
|
ASSERT(KeGetPreviousMode() != KernelMode);
|
|
|
|
|
|
|
|
/* No context to release */
|
|
|
|
GotContext = FALSE;
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
/* Get our caller context and remember to release it */
|
|
|
|
SeCaptureSubjectContext(&SubjectContext);
|
|
|
|
GotContext = TRUE;
|
|
|
|
|
|
|
|
/* Get caller access token and its associated ID */
|
|
|
|
Token = SeQuerySubjectContextToken(&SubjectContext);
|
|
|
|
Status = SeQueryAuthenticationIdToken(Token, &CallerLuid);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
RtlRaiseStatus(Status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This call is only allowed for SYSTEM */
|
|
|
|
if (!RtlEqualLuid(&CallerLuid, &SystemLuid))
|
|
|
|
{
|
|
|
|
RtlRaiseStatus(STATUS_ACCESS_DENIED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for buffer validity and then copy it to our seed */
|
2018-12-20 02:46:42 +00:00
|
|
|
ProbeForRead(Seed, SEED_BUFFER_SIZE, sizeof(UCHAR));
|
2019-02-27 20:12:32 +00:00
|
|
|
RtlCopyMemory(&ExpUuidCachedValues.NodeId[0], Seed, SEED_BUFFER_SIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* According to RFC 4122, UUID seed is based on MAC addresses
|
|
|
|
* If it is randomly set, then, it must have its multicast be set
|
|
|
|
* to be valid and avoid collisions
|
|
|
|
* Reflect it here
|
|
|
|
*/
|
|
|
|
ExpUuidCacheValid = ~(*Seed >> 7) & 1;
|
2018-12-18 22:08:19 +00:00
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
|
|
|
|
/* Release context if required */
|
|
|
|
if (GotContext)
|
|
|
|
{
|
|
|
|
SeReleaseSubjectContext(&SubjectContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
2004-11-12 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|