ReactOS  0.4.15-dev-3207-ga415bd4
uuid.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for uuid.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define SEED_BUFFER_SIZE   6
 
#define TICKS_PER_CLOCK_TICK   1000
 
#define SECSPERDAY   86400
 
#define TICKSPERSEC   10000000
 
#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)
 
#define TIME_FRAME   10000
 
#define VALUE_BUFFER_SIZE   20
 

Functions

BOOLEAN NTAPI ExpUuidInitialization (VOID)
 
static NTSTATUS ExpUuidLoadSequenceNumber (PULONG Sequence)
 
static NTSTATUS ExpUuidSaveSequenceNumber (PULONG Sequence)
 
static VOID ExpUuidSaveSequenceNumberIf (VOID)
 
static NTSTATUS ExpAllocateUuids (PULARGE_INTEGER Time, PULONG Range, PULONG Sequence)
 
static NTSTATUS ExpUuidGetValues (PUUID_CACHED_VALUES_STRUCT CachedValues)
 
BOOLEAN NTAPI ExLuidInitialization (VOID)
 
VOID NTAPI ExAllocateLocallyUniqueId (OUT LUID *LocallyUniqueId)
 
NTSTATUS NTAPI NtAllocateLocallyUniqueId (OUT LUID *LocallyUniqueId)
 
NTSTATUS NTAPI ExUuidCreate (OUT UUID *Uuid)
 
NTSTATUS NTAPI NtAllocateUuids (OUT PULARGE_INTEGER Time, OUT PULONG Range, OUT PULONG Sequence, OUT PUCHAR Seed)
 
NTSTATUS NTAPI NtSetUuidSeed (IN PUCHAR Seed)
 

Variables

FAST_MUTEX ExpUuidLock
 
LARGE_INTEGER ExpUuidLastTimeAllocated
 
ULONG ExpUuidSequenceNumber = 0
 
BOOLEAN ExpUuidSequenceNumberValid
 
BOOLEAN ExpUuidSequenceNumberNotSaved = FALSE
 
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues = {0ULL, 0xFFFFFFFF, {{0, 0, {0x80, 0x6E, 0x6F, 0x6E, 0x69, 0x63}}}}
 
BOOLEAN ExpUuidCacheValid = FALSE
 
ULONG ExpLuidIncrement = 1
 
LARGE_INTEGER ExpLuid = {{0x3e9, 0x0}}
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file uuid.c.

◆ SECS_15_OCT_1582_TO_1601

#define SECS_15_OCT_1582_TO_1601   ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)

Definition at line 26 of file uuid.c.

◆ SECSPERDAY

#define SECSPERDAY   86400

Definition at line 22 of file uuid.c.

◆ SEED_BUFFER_SIZE

#define SEED_BUFFER_SIZE   6

Definition at line 17 of file uuid.c.

◆ TICKS_15_OCT_1582_TO_1601

#define TICKS_15_OCT_1582_TO_1601   ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)

Definition at line 27 of file uuid.c.

◆ TICKS_PER_CLOCK_TICK

#define TICKS_PER_CLOCK_TICK   1000

Definition at line 21 of file uuid.c.

◆ TICKSPERSEC

#define TICKSPERSEC   10000000

Definition at line 23 of file uuid.c.

◆ TIME_FRAME

#define TIME_FRAME   10000

Definition at line 30 of file uuid.c.

◆ VALUE_BUFFER_SIZE

#define VALUE_BUFFER_SIZE   20

Definition at line 67 of file uuid.c.

Function Documentation

◆ ExAllocateLocallyUniqueId()

VOID NTAPI ExAllocateLocallyUniqueId ( OUT LUID LocallyUniqueId)

Definition at line 335 of file uuid.c.

336 {
337  /* Atomically increment the luid */
338  *(LONG64*)LocallyUniqueId = InterlockedExchangeAdd64(&ExpLuid.QuadPart,
340 }
LARGE_INTEGER ExpLuid
Definition: uuid.c:43
int64_t LONG64
Definition: typedefs.h:68
#define InterlockedExchangeAdd64
Definition: interlocked.h:186
LONGLONG QuadPart
Definition: typedefs.h:114
ULONG ExpLuidIncrement
Definition: uuid.c:42

Referenced by NtAdjustGroupsToken(), NtAdjustPrivilegesToken(), NtAllocateLocallyUniqueId(), NtSetInformationToken(), SeCreateAccessStateEx(), SepCreateToken(), and SepDuplicateToken().

◆ ExLuidInitialization()

BOOLEAN NTAPI ExLuidInitialization ( VOID  )

Definition at line 325 of file uuid.c.

326 {
327  return TRUE;
328 }
#define TRUE
Definition: types.h:120

Referenced by SepInitializationPhase0().

◆ ExpAllocateUuids()

static NTSTATUS ExpAllocateUuids ( PULARGE_INTEGER  Time,
PULONG  Range,
PULONG  Sequence 
)
static

Definition at line 197 of file uuid.c.

200 {
202  LARGE_INTEGER Counter, Frequency, CurrentTime, TimeDiff, ClockDiff;
203 
204  PAGED_CODE();
205 
206  /* Initialize sequence number */
208  {
209  /* Try to load sequence number */
211  if (NT_SUCCESS(Status))
212  {
214  }
215  else
216  {
217  /* If we cannot, generate a "true" random */
220  }
221 
222  /* It's valid and to be saved */
225  }
226 
227  KeQuerySystemTime(&CurrentTime);
228  TimeDiff.QuadPart = CurrentTime.QuadPart - ExpUuidLastTimeAllocated.QuadPart;
229  /* If time went backwards, change sequence (see RFC example) */
230  if (TimeDiff.QuadPart < 0)
231  {
233  TimeDiff.QuadPart = 2 * TIME_FRAME;
234 
235  /* It's to be saved */
238  }
239 
240  if (TimeDiff.QuadPart == 0)
241  {
242  return STATUS_RETRY;
243  }
244 
245  /* If time diff > 0.1ms, squash it to reduce it to keep our clock resolution */
246  if (TimeDiff.HighPart > 0 || TimeDiff.QuadPart > TICKS_PER_CLOCK_TICK * TIME_FRAME)
247  {
249  }
250 
251  if (TimeDiff.HighPart < 0 || TimeDiff.QuadPart <= TIME_FRAME)
252  {
253  *Range = TimeDiff.QuadPart;
254  ClockDiff.QuadPart = 0LL;
255  }
256  else
257  {
258  *Range = TIME_FRAME;
259  ClockDiff.QuadPart = TimeDiff.QuadPart - TIME_FRAME;
260  --ClockDiff.HighPart;
261  }
262 
263  Time->QuadPart = CurrentTime.QuadPart - *Range - ClockDiff.QuadPart;
264  ExpUuidLastTimeAllocated.QuadPart = CurrentTime.QuadPart - ClockDiff.QuadPart;
265  *Sequence = ExpUuidSequenceNumber;
266 
267  return STATUS_SUCCESS;
268 }
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(IN PLARGE_INTEGER PerformanceFreq)
Definition: timer.c:138
#define LL
Definition: tui.h:84
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
uint32_t ULONG_PTR
Definition: typedefs.h:65
LARGE_INTEGER ExpUuidLastTimeAllocated
Definition: uuid.c:36
#define TIME_FRAME
Definition: uuid.c:30
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ULONG ExpUuidSequenceNumber
Definition: uuid.c:37
ULONG LowPart
Definition: typedefs.h:106
Definition: range.c:39
#define TICKS_PER_CLOCK_TICK
Definition: uuid.c:21
BOOLEAN ExpUuidSequenceNumberNotSaved
Definition: uuid.c:39
static LARGE_INTEGER Frequency
Definition: clock.c:41
static LARGE_INTEGER Counter
Definition: clock.c:43
#define STATUS_RETRY
Definition: udferr_usr.h:182
static NTSTATUS ExpUuidLoadSequenceNumber(PULONG Sequence)
Definition: uuid.c:69
#define ULONG_PTR
Definition: config.h:101
BOOLEAN ExpUuidSequenceNumberValid
Definition: uuid.c:38
#define STATUS_SUCCESS
Definition: shellext.h:65
static PLARGE_INTEGER Time
Definition: time.c:105
LONGLONG QuadPart
Definition: typedefs.h:114
#define PAGED_CODE()

Referenced by ExpUuidGetValues(), and NtAllocateUuids().

◆ ExpUuidGetValues()

static NTSTATUS ExpUuidGetValues ( PUUID_CACHED_VALUES_STRUCT  CachedValues)
static

Definition at line 276 of file uuid.c.

277 {
280  ULONG Range;
281  ULONG Sequence;
282 
283  PAGED_CODE();
284 
285  /* Allocate UUIDs */
286  Status = ExpAllocateUuids(&Time, &Range, &Sequence);
287  if (Status == STATUS_RETRY)
288  {
289  return Status;
290  }
291 
292  if (!NT_SUCCESS(Status))
293  {
294  return STATUS_NO_MEMORY;
295  }
296 
297  /* We need at least one UUID */
298  ASSERT(Range != 0);
299 
300  /* Set up our internal cache
301  * See format_uuid_v1 in RFC4122 for magic values
302  */
303  CachedValues->ClockSeqLow = Sequence;
304  CachedValues->ClockSeqHiAndReserved = (Sequence & 0x3F00) >> 8;
305  CachedValues->ClockSeqHiAndReserved |= 0x80;
306  CachedValues->AllocatedCount = Range;
307 
308  /*
309  * Time is relative to UUID time
310  * And we set last time range for all the possibly
311  * returnable UUID
312  */
314  CachedValues->Time = Time.QuadPart + (Range - 1);
315 
316  return STATUS_SUCCESS;
317 }
LONG NTSTATUS
Definition: precomp.h:26
struct tagRange Range
#define TICKS_15_OCT_1582_TO_1601
Definition: uuid.c:27
Status
Definition: gdiplustypes.h:24
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Definition: range.c:39
static NTSTATUS ExpAllocateUuids(PULARGE_INTEGER Time, PULONG Range, PULONG Sequence)
Definition: uuid.c:197
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_RETRY
Definition: udferr_usr.h:182
unsigned int ULONG
Definition: retypes.h:1
UCHAR ClockSeqHiAndReserved
Definition: ex.h:181
#define STATUS_SUCCESS
Definition: shellext.h:65
static PLARGE_INTEGER Time
Definition: time.c:105
LONGLONG QuadPart
Definition: typedefs.h:114
#define PAGED_CODE()

Referenced by ExUuidCreate().

◆ ExpUuidInitialization()

BOOLEAN NTAPI ExpUuidInitialization ( VOID  )

Definition at line 53 of file uuid.c.

54 {
56 
59 
60  return TRUE;
61 }
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
#define TRUE
Definition: types.h:120
FAST_MUTEX ExpUuidLock
Definition: uuid.c:35
#define FALSE
Definition: types.h:117
LARGE_INTEGER ExpUuidLastTimeAllocated
Definition: uuid.c:36
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
BOOLEAN ExpUuidSequenceNumberValid
Definition: uuid.c:38

Referenced by ExpInitSystemPhase1().

◆ ExpUuidLoadSequenceNumber()

static NTSTATUS ExpUuidLoadSequenceNumber ( PULONG  Sequence)
static

Definition at line 69 of file uuid.c.

70 {
71  UCHAR ValueBuffer[VALUE_BUFFER_SIZE];
78 
79  PAGED_CODE();
80 
81  RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\Software\\Microsoft\\Rpc");
82  RtlInitUnicodeString(&ValueName, L"UuidSequenceNumber");
83 
85  &KeyName,
87  NULL,
88  NULL);
90  if (!NT_SUCCESS(Status))
91  {
92  DPRINT("ZwOpenKey() failed (Status %lx)\n", Status);
93  return Status;
94  }
95 
96  ValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ValueBuffer;
97  Status = ZwQueryValueKey(KeyHandle,
98  &ValueName,
100  ValueBuffer,
102  &ValueLength);
104  if (!NT_SUCCESS(Status))
105  {
106  DPRINT("ZwQueryValueKey() failed (Status %lx)\n", Status);
107  return Status;
108  }
109 
110  if (ValueInfo->Type != REG_DWORD || ValueInfo->DataLength != sizeof(DWORD))
111  {
112  return STATUS_UNSUCCESSFUL;
113  }
114 
115  *Sequence = *((PULONG)ValueInfo->Data);
116 
117  DPRINT("Loaded sequence %lx\n", *Sequence);
118 
119  return STATUS_SUCCESS;
120 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define VALUE_BUFFER_SIZE
Definition: uuid.c:67
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4711
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2697
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
struct _KEY_VALUE_PARTIAL_INFORMATION * PKEY_VALUE_PARTIAL_INFORMATION
unsigned long DWORD
Definition: ntddk_ex.h:95
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING ValueName
Definition: wdfregistry.h:240
unsigned char UCHAR
Definition: xmlstorage.h:181
static const WCHAR L[]
Definition: oid.c:1250
#define GENERIC_READ
Definition: compat.h:135
unsigned int * PULONG
Definition: retypes.h:1
#define NULL
Definition: types.h:112
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:71
#define REG_DWORD
Definition: sdbapi.c:596
#define PAGED_CODE()
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG ValueLength
Definition: wdfregistry.h:271

Referenced by ExpAllocateUuids().

◆ ExpUuidSaveSequenceNumber()

static NTSTATUS ExpUuidSaveSequenceNumber ( PULONG  Sequence)
static

Definition at line 127 of file uuid.c.

128 {
133 
134  PAGED_CODE();
135 
136  RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\Software\\Microsoft\\Rpc");
137  RtlInitUnicodeString(&ValueName, L"UuidSequenceNumber");
138 
140  &KeyName,
142  NULL,
143  NULL);
144  Status = ZwOpenKey(&KeyHandle,
147  if (!NT_SUCCESS(Status))
148  {
149  DPRINT("ZwOpenKey() failed (Status %lx)\n", Status);
150  return Status;
151  }
152 
153  Status = ZwSetValueKey(KeyHandle,
154  &ValueName,
155  0,
156  REG_DWORD,
157  Sequence,
158  sizeof(ULONG));
160  if (!NT_SUCCESS(Status))
161  {
162  DPRINT("ZwSetValueKey() failed (Status %lx)\n", Status);
163  }
164 
165  return Status;
166 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4711
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define GENERIC_WRITE
Definition: nt_native.h:90
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2697
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING ValueName
Definition: wdfregistry.h:240
static const WCHAR L[]
Definition: oid.c:1250
#define GENERIC_READ
Definition: compat.h:135
#define NULL
Definition: types.h:112
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define DPRINT
Definition: sndvol32.h:71
#define REG_DWORD
Definition: sdbapi.c:596
#define PAGED_CODE()

Referenced by ExpUuidSaveSequenceNumberIf().

◆ ExpUuidSaveSequenceNumberIf()

static VOID ExpUuidSaveSequenceNumberIf ( VOID  )
static

Definition at line 174 of file uuid.c.

175 {
177 
178  PAGED_CODE();
179 
180  /* Only save sequence if it has to */
182  {
184  if (NT_SUCCESS(Status))
185  {
187  }
188  }
189 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
#define FALSE
Definition: types.h:117
static NTSTATUS ExpUuidSaveSequenceNumber(PULONG Sequence)
Definition: uuid.c:127
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ULONG ExpUuidSequenceNumber
Definition: uuid.c:37
BOOLEAN ExpUuidSequenceNumberNotSaved
Definition: uuid.c:39
#define PAGED_CODE()

Referenced by ExUuidCreate(), and NtAllocateUuids().

◆ ExUuidCreate()

NTSTATUS NTAPI ExUuidCreate ( OUT UUID Uuid)

Definition at line 380 of file uuid.c.

381 {
383  LONG AllocatedCount;
385  BOOLEAN Valid;
386 
387  PAGED_CODE();
388 
390  /* Loop until we have an UUID to return */
391  while (TRUE)
392  {
393  /* Try to gather node values */
394  do
395  {
397 
398  RtlCopyMemory(Uuid->Data4,
400  sizeof(Uuid->Data4));
401 
402  Valid = ExpUuidCacheValid;
404  }
405  /* Loop till we can do it without being disturbed */
407 
408  /* We have more than an allocated UUID left, that's OK to return! */
409  if (AllocatedCount >= 0)
410  {
411  break;
412  }
413 
414  /*
415  * Here, we're out of UUIDs, we need to allocate more
416  * We need to be alone to do it, so lock the mutex
417  */
420  {
421  /* If allocation fails, bail out! */
423  if (Status != STATUS_SUCCESS)
424  {
426  return Status;
427  }
428 
429  /* Save our current sequence if changed */
431  }
433  }
434 
435  /*
436  * Once here, we've got an UUID to return
437  * But, if our init wasn't sane, then, make
438  * sure it's only used locally
439  */
440  if (!Valid)
441  {
443  }
444 
445  /* Set our timestamp - see RFC4211 */
446  Time.QuadPart -= AllocatedCount;
447  Uuid->Data1 = Time.LowPart;
448  Uuid->Data2 = Time.HighPart;
449  /* We also set the bit for GUIDv1 */
450  Uuid->Data3 = ((Time.HighPart >> 16) & 0x0FFF) | 0x1000;
451 
452  return Status;
453 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN ExpUuidCacheValid
Definition: uuid.c:41
VOID FASTCALL ExReleaseFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:31
FAST_MUTEX ExpUuidLock
Definition: uuid.c:35
long LONG
Definition: pedump.c:60
unsigned char BOOLEAN
static VOID ExpUuidSaveSequenceNumberIf(VOID)
Definition: uuid.c:174
Status
Definition: gdiplustypes.h:24
static NTSTATUS ExpUuidGetValues(PUUID_CACHED_VALUES_STRUCT CachedValues)
Definition: uuid.c:276
#define InterlockedDecrement
Definition: armddk.h:52
ULONG LowPart
Definition: typedefs.h:106
VOID FASTCALL ExAcquireFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:23
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues
Definition: uuid.c:40
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define STATUS_SUCCESS
Definition: shellext.h:65
static PLARGE_INTEGER Time
Definition: time.c:105
#define RPC_NT_UUID_LOCAL_ONLY
Definition: ntstatus.h:174
LONGLONG QuadPart
Definition: typedefs.h:114
#define PAGED_CODE()

Referenced by AllocAndInitializeTelemetryContext(), ChanMgrCreateChannel(), CreateNewVolumeName(), CreateNoDriveLetterEntry(), Ext2InitializeVcb(), IntCreateNewRegistryPath(), and START_TEST().

◆ NtAllocateLocallyUniqueId()

NTSTATUS NTAPI NtAllocateLocallyUniqueId ( OUT LUID LocallyUniqueId)

Definition at line 348 of file uuid.c.

349 {
351  PAGED_CODE();
352 
353  /* Probe if user mode */
355  if (PreviousMode != KernelMode)
356  {
357  _SEH2_TRY
358  {
359  ProbeForWrite(LocallyUniqueId,
360  sizeof(LUID),
361  sizeof(ULONG));
362  }
364  {
366  }
367  _SEH2_END;
368  }
369 
370  /* Do the allocation */
371  ExAllocateLocallyUniqueId(LocallyUniqueId);
372  return STATUS_SUCCESS;
373 }
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3070
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
_SEH2_TRY
Definition: create.c:4226
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
_SEH2_END
Definition: create.c:4400
VOID NTAPI ExAllocateLocallyUniqueId(OUT LUID *LocallyUniqueId)
Definition: uuid.c:335
unsigned int ULONG
Definition: retypes.h:1
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define PAGED_CODE()

Referenced by AllocateLocallyUniqueId(), and LsaApLogonUserEx2().

◆ NtAllocateUuids()

NTSTATUS NTAPI NtAllocateUuids ( OUT PULARGE_INTEGER  Time,
OUT PULONG  Range,
OUT PULONG  Sequence,
OUT PUCHAR  Seed 
)

Definition at line 460 of file uuid.c.

464 {
465  ULARGE_INTEGER IntTime;
466  ULONG IntRange, IntSequence;
469 
470  PAGED_CODE();
471 
472  /* Probe if user mode */
474  if (PreviousMode != KernelMode)
475  {
476  _SEH2_TRY
477  {
479  sizeof(ULARGE_INTEGER),
480  sizeof(ULONG));
481 
483  sizeof(ULONG),
484  sizeof(ULONG));
485 
486  ProbeForWrite(Sequence,
487  sizeof(ULONG),
488  sizeof(ULONG));
489 
490  ProbeForWrite(Seed,
492  sizeof(UCHAR));
493  }
495  {
497  }
498  _SEH2_END;
499  }
500 
501  /* During allocation we must be alone */
503 
504  Status = ExpAllocateUuids(&IntTime,
505  &IntRange,
506  &IntSequence);
507  if (!NT_SUCCESS(Status))
508  {
510  return Status;
511  }
512 
513  /* If sequence number was changed, save it */
515 
516  /* Allocation done, so we can release */
518 
519  /* Write back UUIDs to caller */
520  _SEH2_TRY
521  {
522  Time->QuadPart = IntTime.QuadPart;
523  *Range = IntRange;
524  *Sequence = IntSequence;
525 
526  RtlCopyMemory(Seed,
529 
531  }
533  {
535  }
536  _SEH2_END;
537 
538  return Status;
539 }
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3070
VOID FASTCALL ExReleaseFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:31
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
FAST_MUTEX ExpUuidLock
Definition: uuid.c:35
_SEH2_TRY
Definition: create.c:4226
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
static VOID ExpUuidSaveSequenceNumberIf(VOID)
Definition: uuid.c:174
Status
Definition: gdiplustypes.h:24
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
unsigned char UCHAR
Definition: xmlstorage.h:181
Definition: range.c:39
_SEH2_END
Definition: create.c:4400
LONG NTAPI ExSystemExceptionFilter(VOID)
Definition: harderr.c:351
VOID FASTCALL ExAcquireFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:23
static NTSTATUS ExpAllocateUuids(PULARGE_INTEGER Time, PULONG Range, PULONG Sequence)
Definition: uuid.c:197
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues
Definition: uuid.c:40
unsigned int ULONG
Definition: retypes.h:1
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define SEED_BUFFER_SIZE
Definition: uuid.c:17
static PLARGE_INTEGER Time
Definition: time.c:105
LONGLONG QuadPart
Definition: typedefs.h:114
#define PAGED_CODE()

◆ NtSetUuidSeed()

NTSTATUS NTAPI NtSetUuidSeed ( IN PUCHAR  Seed)

Definition at line 547 of file uuid.c.

548 {
550  BOOLEAN GotContext;
553  LUID CallerLuid, SystemLuid = SYSTEM_LUID;
554 
555  PAGED_CODE();
556 
557  /* Should only be done by umode */
559 
560  /* No context to release */
561  GotContext = FALSE;
562  _SEH2_TRY
563  {
564  /* Get our caller context and remember to release it */
566  GotContext = TRUE;
567 
568  /* Get caller access token and its associated ID */
570  Status = SeQueryAuthenticationIdToken(Token, &CallerLuid);
571  if (!NT_SUCCESS(Status))
572  {
574  }
575 
576  /* This call is only allowed for SYSTEM */
577  if (!RtlEqualLuid(&CallerLuid, &SystemLuid))
578  {
580  }
581 
582  /* Check for buffer validity and then copy it to our seed */
583  ProbeForRead(Seed, SEED_BUFFER_SIZE, sizeof(UCHAR));
585 
586  /*
587  * According to RFC 4122, UUID seed is based on MAC addresses
588  * If it is randomly set, then, it must have its multicast be set
589  * to be valid and avoid collisions
590  * Reflect it here
591  */
592  ExpUuidCacheValid = ~(*Seed >> 7) & 1;
593 
595  }
597  {
599  }
600  _SEH2_END;
601 
602  /* Release context if required */
603  if (GotContext)
604  {
606  }
607 
608  return Status;
609 }
DECLSPEC_NORETURN NTSYSAPI VOID NTAPI RtlRaiseStatus(_In_ NTSTATUS Status)
_Inout_ PLIST_ENTRY _In_ PVOID _In_ PSTRING _In_ BOOLEAN _In_ BOOLEAN _In_ ULONG _In_ PFLT_CALLBACK_DATA _In_opt_ PCHECK_FOR_TRAVERSE_ACCESS _In_opt_ PSECURITY_SUBJECT_CONTEXT SubjectContext
Definition: fltkernel.h:2238
#define TRUE
Definition: types.h:120
#define KeGetPreviousMode()
Definition: ketypes.h:1107
LONG NTSTATUS
Definition: precomp.h:26
_IRQL_requires_same_ _In_ PLSA_STRING _In_ SECURITY_LOGON_TYPE _In_ ULONG _In_ ULONG _In_opt_ PTOKEN_GROUPS _In_ PTOKEN_SOURCE _Out_ PVOID _Out_ PULONG _Inout_ PLUID _Out_ PHANDLE Token
BOOLEAN ExpUuidCacheValid
Definition: uuid.c:41
_SEH2_TRY
Definition: create.c:4226
#define FALSE
Definition: types.h:117
#define RtlEqualLuid(Luid1, Luid2)
Definition: rtlfuncs.h:301
unsigned char BOOLEAN
Status
Definition: gdiplustypes.h:24
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
VOID NTAPI SeCaptureSubjectContext(_Out_ PSECURITY_SUBJECT_CONTEXT SubjectContext)
Captures the security subject context of the calling thread and calling process.
Definition: access.c:434
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
unsigned char UCHAR
Definition: xmlstorage.h:181
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
#define SeQuerySubjectContextToken(SubjectContext)
Definition: sefuncs.h:583
#define SYSTEM_LUID
Definition: setypes.h:687
_SEH2_END
Definition: create.c:4400
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues
Definition: uuid.c:40
NTSTATUS NTAPI SeQueryAuthenticationIdToken(_In_ PACCESS_TOKEN Token, _Out_ PLUID LogonId)
Queries the authentication ID of an access token.
Definition: token.c:2711
VOID NTAPI SeReleaseSubjectContext(_In_ PSECURITY_SUBJECT_CONTEXT SubjectContext)
Releases both the primary and client tokens of a security subject context.
Definition: access.c:520
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define SEED_BUFFER_SIZE
Definition: uuid.c:17
#define PAGED_CODE()

Referenced by DealWithDeviceEvent().

Variable Documentation

◆ ExpLuid

LARGE_INTEGER ExpLuid = {{0x3e9, 0x0}}

Definition at line 43 of file uuid.c.

Referenced by ExAllocateLocallyUniqueId().

◆ ExpLuidIncrement

ULONG ExpLuidIncrement = 1

Definition at line 42 of file uuid.c.

Referenced by ExAllocateLocallyUniqueId().

◆ ExpUuidCachedValues

UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues = {0ULL, 0xFFFFFFFF, {{0, 0, {0x80, 0x6E, 0x6F, 0x6E, 0x69, 0x63}}}}

Definition at line 40 of file uuid.c.

Referenced by ExUuidCreate(), NtAllocateUuids(), and NtSetUuidSeed().

◆ ExpUuidCacheValid

BOOLEAN ExpUuidCacheValid = FALSE

Definition at line 41 of file uuid.c.

Referenced by ExUuidCreate(), and NtSetUuidSeed().

◆ ExpUuidLastTimeAllocated

LARGE_INTEGER ExpUuidLastTimeAllocated

Definition at line 36 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidInitialization().

◆ ExpUuidLock

FAST_MUTEX ExpUuidLock

Definition at line 35 of file uuid.c.

Referenced by ExpUuidInitialization(), ExUuidCreate(), and NtAllocateUuids().

◆ ExpUuidSequenceNumber

ULONG ExpUuidSequenceNumber = 0

Definition at line 37 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidSaveSequenceNumberIf().

◆ ExpUuidSequenceNumberNotSaved

BOOLEAN ExpUuidSequenceNumberNotSaved = FALSE

Definition at line 39 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidSaveSequenceNumberIf().

◆ ExpUuidSequenceNumberValid

BOOLEAN ExpUuidSequenceNumberValid

Definition at line 38 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidInitialization().