ReactOS  0.4.13-dev-563-g0561610
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 INIT_FUNCTION 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 INIT_FUNCTION 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 72 of file uuid.c.

Function Documentation

◆ ExAllocateLocallyUniqueId()

VOID NTAPI ExAllocateLocallyUniqueId ( OUT LUID LocallyUniqueId)

Definition at line 340 of file uuid.c.

341 {
342  LARGE_INTEGER PrevLuid;
343  LONGLONG NewLuid, CompLuid;
344 
345  /* Atomically increment the luid */
346  PrevLuid.QuadPart = ExpLuid.QuadPart;
347  for (NewLuid = ExpLuid.QuadPart + ExpLuidIncrement; ;
348  NewLuid = PrevLuid.QuadPart + ExpLuidIncrement)
349  {
351  NewLuid,
352  PrevLuid.QuadPart);
353  if (CompLuid == PrevLuid.QuadPart)
354  {
355  break;
356  }
357 
358  PrevLuid.QuadPart = CompLuid;
359  }
360 
361  LocallyUniqueId->LowPart = PrevLuid.LowPart;
362  LocallyUniqueId->HighPart = PrevLuid.HighPart;
363 }
#define InterlockedCompareExchange64
Definition: interlocked.h:114
LARGE_INTEGER ExpLuid
Definition: uuid.c:48
int64_t LONGLONG
Definition: typedefs.h:66
ULONG LowPart
Definition: typedefs.h:104
LONGLONG QuadPart
Definition: typedefs.h:112
ULONG ExpLuidIncrement
Definition: uuid.c:47

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

◆ ExLuidInitialization()

BOOLEAN INIT_FUNCTION NTAPI ExLuidInitialization ( VOID  )

Definition at line 330 of file uuid.c.

331 {
332  return TRUE;
333 }
#define TRUE
Definition: types.h:120

Referenced by SepInitializationPhase0().

◆ ExpAllocateUuids()

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

Definition at line 202 of file uuid.c.

205 {
207  LARGE_INTEGER Counter, Frequency, CurrentTime, TimeDiff, ClockDiff;
208 
209  PAGED_CODE();
210 
211  /* Initialize sequence number */
213  {
214  /* Try to load sequence number */
216  if (NT_SUCCESS(Status))
217  {
219  }
220  else
221  {
222  /* If we cannot, generate a "true" random */
225  }
226 
227  /* It's valid and to be saved */
230  }
231 
232  KeQuerySystemTime(&CurrentTime);
233  TimeDiff.QuadPart = CurrentTime.QuadPart - ExpUuidLastTimeAllocated.QuadPart;
234  /* If time went backwards, change sequence (see RFC example) */
235  if (TimeDiff.QuadPart < 0)
236  {
238  TimeDiff.QuadPart = 2 * TIME_FRAME;
239 
240  /* It's to be saved */
243  }
244 
245  if (TimeDiff.QuadPart == 0)
246  {
247  return STATUS_RETRY;
248  }
249 
250  /* If time diff > 0.1ms, squash it to reduce it to keep our clock resolution */
251  if (TimeDiff.HighPart > 0 || TimeDiff.QuadPart > TICKS_PER_CLOCK_TICK * TIME_FRAME)
252  {
254  }
255 
256  if (TimeDiff.HighPart < 0 || TimeDiff.QuadPart <= TIME_FRAME)
257  {
258  *Range = TimeDiff.QuadPart;
259  ClockDiff.QuadPart = 0LL;
260  }
261  else
262  {
263  *Range = TIME_FRAME;
264  ClockDiff.QuadPart = TimeDiff.QuadPart - TIME_FRAME;
265  --ClockDiff.HighPart;
266  }
267 
268  Time->QuadPart = CurrentTime.QuadPart - *Range - ClockDiff.QuadPart;
269  ExpUuidLastTimeAllocated.QuadPart = CurrentTime.QuadPart - ClockDiff.QuadPart;
270  *Sequence = ExpUuidSequenceNumber;
271 
272  return STATUS_SUCCESS;
273 }
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
LARGE_INTEGER NTAPI KeQueryPerformanceCounter(IN PLARGE_INTEGER PerformanceFreq)
Definition: timer.c:138
#define TRUE
Definition: types.h:120
#define LL
Definition: tui.h:72
LONG NTSTATUS
Definition: precomp.h:26
#define PAGED_CODE()
Definition: video.h:57
uint32_t ULONG_PTR
Definition: typedefs.h:63
LARGE_INTEGER ExpUuidLastTimeAllocated
Definition: uuid.c:41
#define TIME_FRAME
Definition: uuid.c:30
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ULONG ExpUuidSequenceNumber
Definition: uuid.c:42
ULONG LowPart
Definition: typedefs.h:104
Definition: range.c:43
Status
Definition: gdiplustypes.h:24
#define TICKS_PER_CLOCK_TICK
Definition: uuid.c:21
BOOLEAN ExpUuidSequenceNumberNotSaved
Definition: uuid.c:44
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:74
#define ULONG_PTR
Definition: config.h:101
BOOLEAN ExpUuidSequenceNumberValid
Definition: uuid.c:43
return STATUS_SUCCESS
Definition: btrfs.c:2777
static PLARGE_INTEGER Time
Definition: time.c:105
LONGLONG QuadPart
Definition: typedefs.h:112

Referenced by ExpUuidGetValues(), and NtAllocateUuids().

◆ ExpUuidGetValues()

static NTSTATUS ExpUuidGetValues ( PUUID_CACHED_VALUES_STRUCT  CachedValues)
static

Definition at line 281 of file uuid.c.

282 {
285  ULONG Range;
286  ULONG Sequence;
287 
288  PAGED_CODE();
289 
290  /* Allocate UUIDs */
291  Status = ExpAllocateUuids(&Time, &Range, &Sequence);
292  if (Status == STATUS_RETRY)
293  {
294  return Status;
295  }
296 
297  if (!NT_SUCCESS(Status))
298  {
299  return STATUS_NO_MEMORY;
300  }
301 
302  /* We need at least one UUID */
303  ASSERT(Range != 0);
304 
305  /* Set up our internal cache
306  * See format_uuid_v1 in RFC4122 for magic values
307  */
308  CachedValues->ClockSeqLow = Sequence;
309  CachedValues->ClockSeqHiAndReserved = (Sequence & 0x3F00) >> 8;
310  CachedValues->ClockSeqHiAndReserved |= 0x80;
311  CachedValues->AllocatedCount = Range;
312 
313  /*
314  * Time is relative to UUID time
315  * And we set last time range for all the possibly
316  * returnable UUID
317  */
319  CachedValues->Time = Time.QuadPart + (Range - 1);
320 
321  return STATUS_SUCCESS;
322 }
LONG NTSTATUS
Definition: precomp.h:26
#define PAGED_CODE()
Definition: video.h:57
struct tagRange Range
#define TICKS_15_OCT_1582_TO_1601
Definition: uuid.c:27
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Definition: range.c:43
Status
Definition: gdiplustypes.h:24
static NTSTATUS ExpAllocateUuids(PULARGE_INTEGER Time, PULONG Range, PULONG Sequence)
Definition: uuid.c:202
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
#define STATUS_RETRY
Definition: udferr_usr.h:182
unsigned int ULONG
Definition: retypes.h:1
UCHAR ClockSeqHiAndReserved
Definition: ex.h:174
return STATUS_SUCCESS
Definition: btrfs.c:2777
static PLARGE_INTEGER Time
Definition: time.c:105
LONGLONG QuadPart
Definition: typedefs.h:112

Referenced by ExUuidCreate().

◆ ExpUuidInitialization()

BOOLEAN INIT_FUNCTION NTAPI ExpUuidInitialization ( VOID  )

Definition at line 58 of file uuid.c.

59 {
61 
64 
65  return TRUE;
66 }
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
#define TRUE
Definition: types.h:120
FAST_MUTEX ExpUuidLock
Definition: uuid.c:40
LARGE_INTEGER ExpUuidLastTimeAllocated
Definition: uuid.c:41
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274
BOOLEAN ExpUuidSequenceNumberValid
Definition: uuid.c:43

Referenced by ExpInitSystemPhase1().

◆ ExpUuidLoadSequenceNumber()

static NTSTATUS ExpUuidLoadSequenceNumber ( PULONG  Sequence)
static

Definition at line 74 of file uuid.c.

75 {
76  UCHAR ValueBuffer[VALUE_BUFFER_SIZE];
83 
84  PAGED_CODE();
85 
86  RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\Software\\Microsoft\\Rpc");
87  RtlInitUnicodeString(&ValueName, L"UuidSequenceNumber");
88 
90  &KeyName,
92  NULL,
93  NULL);
95  if (!NT_SUCCESS(Status))
96  {
97  DPRINT("ZwOpenKey() failed (Status %lx)\n", Status);
98  return Status;
99  }
100 
101  ValueInfo = (PKEY_VALUE_PARTIAL_INFORMATION)ValueBuffer;
102  Status = ZwQueryValueKey(KeyHandle,
103  &ValueName,
105  ValueBuffer,
107  &ValueLength);
109  if (!NT_SUCCESS(Status))
110  {
111  DPRINT("ZwQueryValueKey() failed (Status %lx)\n", Status);
112  return Status;
113  }
114 
115  if (ValueInfo->Type != REG_DWORD || ValueInfo->DataLength != sizeof(DWORD))
116  {
117  return STATUS_UNSUCCESSFUL;
118  }
119 
120  *Sequence = *((PULONG)ValueInfo->Data);
121 
122  DPRINT("Loaded sequence %lx\n", *Sequence);
123 
124  return STATUS_SUCCESS;
125 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING KeyName
Definition: ndis.h:4711
#define VALUE_BUFFER_SIZE
Definition: uuid.c:72
_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 PAGED_CODE()
Definition: video.h:57
_In_ PUNICODE_STRING ValueName
Definition: cmfuncs.h:264
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#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 OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
unsigned char UCHAR
Definition: xmlstorage.h:181
static const WCHAR L[]
Definition: oid.c:1250
_In_ GUID _In_ PVOID _In_ ULONG ValueLength
Definition: hubbusif.h:311
#define GENERIC_READ
Definition: compat.h:124
Status
Definition: gdiplustypes.h:24
unsigned int * PULONG
Definition: retypes.h:1
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
return STATUS_SUCCESS
Definition: btrfs.c:2777
#define REG_DWORD
Definition: sdbapi.c:596
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231

Referenced by ExpAllocateUuids().

◆ ExpUuidSaveSequenceNumber()

static NTSTATUS ExpUuidSaveSequenceNumber ( PULONG  Sequence)
static

Definition at line 132 of file uuid.c.

133 {
138 
139  PAGED_CODE();
140 
141  RtlInitUnicodeString(&KeyName, L"\\Registry\\Machine\\Software\\Microsoft\\Rpc");
142  RtlInitUnicodeString(&ValueName, L"UuidSequenceNumber");
143 
145  &KeyName,
147  NULL,
148  NULL);
149  Status = ZwOpenKey(&KeyHandle,
152  if (!NT_SUCCESS(Status))
153  {
154  DPRINT("ZwOpenKey() failed (Status %lx)\n", Status);
155  return Status;
156  }
157 
158  Status = ZwSetValueKey(KeyHandle,
159  &ValueName,
160  0,
161  REG_DWORD,
162  Sequence,
163  sizeof(ULONG));
165  if (!NT_SUCCESS(Status))
166  {
167  DPRINT("ZwSetValueKey() failed (Status %lx)\n", Status);
168  }
169 
170  return Status;
171 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING KeyName
Definition: ndis.h:4711
_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 PAGED_CODE()
Definition: video.h:57
_In_ PUNICODE_STRING ValueName
Definition: cmfuncs.h:264
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static const WCHAR L[]
Definition: oid.c:1250
#define GENERIC_READ
Definition: compat.h:124
Status
Definition: gdiplustypes.h:24
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 REG_DWORD
Definition: sdbapi.c:596
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231

Referenced by ExpUuidSaveSequenceNumberIf().

◆ ExpUuidSaveSequenceNumberIf()

static VOID ExpUuidSaveSequenceNumberIf ( VOID  )
static

Definition at line 179 of file uuid.c.

180 {
182 
183  PAGED_CODE();
184 
185  /* Only save sequence if it has to */
187  {
189  if (NT_SUCCESS(Status))
190  {
192  }
193  }
194 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
#define PAGED_CODE()
Definition: video.h:57
static NTSTATUS ExpUuidSaveSequenceNumber(PULONG Sequence)
Definition: uuid.c:132
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ULONG ExpUuidSequenceNumber
Definition: uuid.c:42
Status
Definition: gdiplustypes.h:24
BOOLEAN ExpUuidSequenceNumberNotSaved
Definition: uuid.c:44

Referenced by ExUuidCreate(), and NtAllocateUuids().

◆ ExUuidCreate()

NTSTATUS NTAPI ExUuidCreate ( OUT UUID Uuid)

Definition at line 403 of file uuid.c.

404 {
406  LONG AllocatedCount;
408  BOOLEAN Valid;
409 
410  PAGED_CODE();
411 
413  /* Loop until we have an UUID to return */
414  while (TRUE)
415  {
416  /* Try to gather node values */
417  do
418  {
420 
421  RtlCopyMemory(&Uuid->Data4[0],
424  Valid = ExpUuidCacheValid;
426  }
427  /* Loop till we can do it without being disturbed */
429 
430  /* We have more than an allocated UUID left, that's OK to return! */
431  if (AllocatedCount >= 0)
432  {
433  break;
434  }
435 
436  /*
437  * Here, we're out of UUIDs, we need to allocate more
438  * We need to be alone to do it, so lock the mutex
439  */
442  {
443  /* If allocation fails, bail out! */
445  if (Status != STATUS_SUCCESS)
446  {
448  return Status;
449  }
450 
451  /* Save our current sequence if changed */
453  }
455  }
456 
457  /*
458  * Once here, we've got an UUID to return
459  * But, if our init wasn't sane, then, make
460  * sure it's only used locally
461  */
462  if (!Valid)
463  {
465  }
466 
467  /* Set our timestamp - see RFC4211 */
468  Time.QuadPart -= AllocatedCount;
469  Uuid->Data1 = Time.LowPart;
470  Uuid->Data2 = Time.HighPart;
471  /* We also set the bit for GUIDv1 */
472  Uuid->Data3 = ((Time.HighPart >> 16) & 0x0FFF) | 0x1000;
473 
474  return Status;
475 }
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN ExpUuidCacheValid
Definition: uuid.c:46
VOID FASTCALL ExReleaseFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:31
#define PAGED_CODE()
Definition: video.h:57
FAST_MUTEX ExpUuidLock
Definition: uuid.c:40
long LONG
Definition: pedump.c:60
unsigned char BOOLEAN
static VOID ExpUuidSaveSequenceNumberIf(VOID)
Definition: uuid.c:179
static NTSTATUS ExpUuidGetValues(PUUID_CACHED_VALUES_STRUCT CachedValues)
Definition: uuid.c:281
#define InterlockedDecrement
Definition: armddk.h:52
ULONG LowPart
Definition: typedefs.h:104
Status
Definition: gdiplustypes.h:24
VOID FASTCALL ExAcquireFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:23
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues
Definition: uuid.c:45
return STATUS_SUCCESS
Definition: btrfs.c:2777
#define SEED_BUFFER_SIZE
Definition: uuid.c:17
static PLARGE_INTEGER Time
Definition: time.c:105
#define RPC_NT_UUID_LOCAL_ONLY
Definition: ntstatus.h:162
LONGLONG QuadPart
Definition: typedefs.h:112

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

◆ NtAllocateLocallyUniqueId()

NTSTATUS NTAPI NtAllocateLocallyUniqueId ( OUT LUID LocallyUniqueId)

Definition at line 371 of file uuid.c.

372 {
374  PAGED_CODE();
375 
376  /* Probe if user mode */
378  if (PreviousMode != KernelMode)
379  {
380  _SEH2_TRY
381  {
382  ProbeForWrite(LocallyUniqueId,
383  sizeof(LUID),
384  sizeof(ULONG));
385  }
387  {
389  }
390  _SEH2_END;
391  }
392 
393  /* Do the allocation */
394  ExAllocateLocallyUniqueId(LocallyUniqueId);
395  return STATUS_SUCCESS;
396 }
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:2982
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
_SEH2_END
Definition: create.c:4424
VOID NTAPI ExAllocateLocallyUniqueId(OUT LUID *LocallyUniqueId)
Definition: uuid.c:340
unsigned int ULONG
Definition: retypes.h:1
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
return STATUS_SUCCESS
Definition: btrfs.c:2777

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 482 of file uuid.c.

486 {
487  ULARGE_INTEGER IntTime;
488  ULONG IntRange, IntSequence;
491 
492  PAGED_CODE();
493 
494  /* Probe if user mode */
496  if (PreviousMode != KernelMode)
497  {
498  _SEH2_TRY
499  {
501  sizeof(ULARGE_INTEGER),
502  sizeof(ULONG));
503 
505  sizeof(ULONG),
506  sizeof(ULONG));
507 
508  ProbeForWrite(Sequence,
509  sizeof(ULONG),
510  sizeof(ULONG));
511 
512  ProbeForWrite(Seed,
514  sizeof(UCHAR));
515  }
517  {
519  }
520  _SEH2_END;
521  }
522 
523  /* During allocation we must be alone */
525 
526  Status = ExpAllocateUuids(&IntTime,
527  &IntRange,
528  &IntSequence);
529  if (!NT_SUCCESS(Status))
530  {
532  return Status;
533  }
534 
535  /* If sequence number was changed, save it */
537 
538  /* Allocation done, so we can release */
540 
541  /* Write back UUIDs to caller */
542  _SEH2_TRY
543  {
544  Time->QuadPart = IntTime.QuadPart;
545  *Range = IntRange;
546  *Sequence = IntSequence;
547 
548  RtlCopyMemory(Seed,
551 
553  }
555  {
557  }
558  _SEH2_END;
559 
560  return Status;
561 }
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
LONG NTSTATUS
Definition: precomp.h:26
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:2982
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
#define PAGED_CODE()
Definition: video.h:57
FAST_MUTEX ExpUuidLock
Definition: uuid.c:40
_SEH2_TRY
Definition: create.c:4250
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
static VOID ExpUuidSaveSequenceNumberIf(VOID)
Definition: uuid.c:179
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
unsigned char UCHAR
Definition: xmlstorage.h:181
Definition: range.c:43
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
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:202
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues
Definition: uuid.c:45
unsigned int ULONG
Definition: retypes.h:1
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
return STATUS_SUCCESS
Definition: btrfs.c:2777
#define SEED_BUFFER_SIZE
Definition: uuid.c:17
static PLARGE_INTEGER Time
Definition: time.c:105
LONGLONG QuadPart
Definition: typedefs.h:112

◆ NtSetUuidSeed()

NTSTATUS NTAPI NtSetUuidSeed ( IN PUCHAR  Seed)

Definition at line 569 of file uuid.c.

570 {
572  BOOLEAN GotContext;
575  LUID CallerLuid, SystemLuid = SYSTEM_LUID;
576 
577  PAGED_CODE();
578 
579  /* Should only be done by umode */
581 
582  /* No context to release */
583  GotContext = FALSE;
584  _SEH2_TRY
585  {
586  /* Get our caller context and remember to release it */
588  GotContext = TRUE;
589 
590  /* Get caller access token and its associated ID */
592  Status = SeQueryAuthenticationIdToken(Token, &CallerLuid);
593  if (!NT_SUCCESS(Status))
594  {
596  }
597 
598  /* This call is only allowed for SYSTEM */
599  if (!RtlEqualLuid(&CallerLuid, &SystemLuid))
600  {
602  }
603 
604  /* Check for buffer validity and then copy it to our seed */
605  ProbeForRead(Seed, SEED_BUFFER_SIZE, sizeof(UCHAR));
607 
608  /*
609  * According to RFC 4122, UUID seed is based on MAC addresses
610  * If it is randomly set, then, it must have its multicast be set
611  * to be valid and avoid collisions
612  * Reflect it here
613  */
614  ExpUuidCacheValid = ~(*Seed >> 7) & 1;
615 
617  }
619  {
621  }
622  _SEH2_END;
623 
624  /* Release context if required */
625  if (GotContext)
626  {
628  }
629 
630  return Status;
631 }
VOID NTAPI SeCaptureSubjectContext(OUT PSECURITY_SUBJECT_CONTEXT SubjectContext)
Definition: access.c:301
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:2239
VOID NTAPI SeReleaseSubjectContext(IN PSECURITY_SUBJECT_CONTEXT SubjectContext)
Definition: access.c:360
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define KeGetPreviousMode()
Definition: ketypes.h:1081
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:46
#define PAGED_CODE()
Definition: video.h:57
_SEH2_TRY
Definition: create.c:4250
NTSTATUS NTAPI SeQueryAuthenticationIdToken(IN PACCESS_TOKEN Token, OUT PLUID LogonId)
Definition: token.c:1752
#define RtlEqualLuid(Luid1, Luid2)
Definition: rtlfuncs.h:301
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
unsigned char BOOLEAN
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
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:672
Status
Definition: gdiplustypes.h:24
_SEH2_END
Definition: create.c:4424
UUID_CACHED_VALUES_STRUCT ExpUuidCachedValues
Definition: uuid.c:45
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
return STATUS_SUCCESS
Definition: btrfs.c:2777
#define SEED_BUFFER_SIZE
Definition: uuid.c:17

Referenced by DealWithDeviceEvent().

Variable Documentation

◆ ExpLuid

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

Definition at line 48 of file uuid.c.

Referenced by ExAllocateLocallyUniqueId().

◆ ExpLuidIncrement

ULONG ExpLuidIncrement = 1

Definition at line 47 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 45 of file uuid.c.

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

◆ ExpUuidCacheValid

BOOLEAN ExpUuidCacheValid = FALSE

Definition at line 46 of file uuid.c.

Referenced by ExUuidCreate(), and NtSetUuidSeed().

◆ ExpUuidLastTimeAllocated

LARGE_INTEGER ExpUuidLastTimeAllocated

Definition at line 41 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidInitialization().

◆ ExpUuidLock

FAST_MUTEX ExpUuidLock

Definition at line 40 of file uuid.c.

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

◆ ExpUuidSequenceNumber

ULONG ExpUuidSequenceNumber = 0

Definition at line 42 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidSaveSequenceNumberIf().

◆ ExpUuidSequenceNumberNotSaved

BOOLEAN ExpUuidSequenceNumberNotSaved = FALSE

Definition at line 44 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidSaveSequenceNumberIf().

◆ ExpUuidSequenceNumberValid

BOOLEAN ExpUuidSequenceNumberValid

Definition at line 43 of file uuid.c.

Referenced by ExpAllocateUuids(), and ExpUuidInitialization().