-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpathproc.c
1093 lines (779 loc) · 24.8 KB
/
pathproc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2004 Security Architects Corporation. All rights reserved.
*
* Module Name:
*
* pathproc.c
*
* Abstract:
*
* This module implements various pathname handling routines.
*
* Author:
*
* Eugene Tsyrklevich 19-Feb-2004
*
* Revision History:
*
* None.
*/
#include "pathproc.h"
#include "procname.h"
#include "policy.h"
#include "learn.h"
#include "log.h"
/*
* ResolveFilename() XXX rewrite
*
* Description:
* Get canonical name for a file by resolving symbolic links.
*
* Parameters:
* szFileName - filename to resolve.
* szResult - output buffer.
* szResultSize - size of an output buffer.
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
BOOLEAN
ResolveFilename(IN PCHAR szFileName, OUT PCHAR szResult, IN USHORT szResultSize)
{
CHAR *p, c;
OBJECT_ATTRIBUTES oa;
ANSI_STRING FileNameAnsi;
UNICODE_STRING FileNameUnicode;
HANDLE hLink;
NTSTATUS rc;
int NumberOfLinks = 0;
WCHAR buffer[chMAX_PATH];
CHAR buffer2[chMAX_PATH];
restart:
*szResult = '\0';
if (szFileName[0] == '\\' && szFileName[1] == '\\')
szFileName++;
/* move to the end of the object name */
for (p = szFileName; *p != '\0'; p++)
;
/* process the object name from end to the beginning */
while (p != szFileName)
{
/* find the last slash */
if (*p != '\\' && *p != '\0')
{
p--;
continue;
}
c = *p;
*p = '\0';
RtlInitAnsiString(&FileNameAnsi, szFileName);
RtlAnsiStringToUnicodeString(&FileNameUnicode, &FileNameAnsi, TRUE);
InitializeObjectAttributes(&oa, &FileNameUnicode, OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE, NULL, NULL);
rc = ZwOpenSymbolicLinkObject(&hLink, GENERIC_READ, &oa);
if (NT_SUCCESS(rc))
{
UNICODE_STRING target;
ANSI_STRING targeta;
target.Buffer = buffer;
target.MaximumLength = bMAX_PATH;
target.Length = 0;
rc = ZwQuerySymbolicLinkObject(hLink, &target, NULL);
ZwClose(hLink);
if (NT_SUCCESS(rc))
{
targeta.Length = 0;
targeta.MaximumLength = szResultSize;
targeta.Buffer = szResult;
RtlUnicodeStringToAnsiString(&targeta, &target, FALSE);
targeta.Buffer[targeta.Length] = '\0';
//XXX szResultSize -= targeta.Length;
RtlFreeUnicodeString(&FileNameUnicode);
*p = c;
//XXX can we have circular links?
if (NumberOfLinks++ < MAX_NUMBER_OF_LINKS)
{
strncat(szResult, p, szResultSize);
// if (NumberOfLinks > 1)
// LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("ResolveFilename: NumberOfLinks=%d. Resolved %s to %s. Restarting.\n", NumberOfLinks, szFileName, szResult));
/*
* switch szFileName to a different buffer. we cannot reuse szFileName buffer
* since the resolved link might end up being longer than the original buffer
*/
szFileName = (PCHAR) buffer2;
strcpy(szFileName, szResult);
goto restart;
}
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("ResolveFilename: NumberOfLinks=%d. bailing out. %s\n", NumberOfLinks, szResult));
break;
}
}
RtlFreeUnicodeString(&FileNameUnicode);
*p-- = c;
}
strncat(szResult, p, szResultSize);
// LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("ResolveFilename: name=%s number of links=%d\n", szResult, NumberOfLinks));
return TRUE;
}
BOOLEAN
ResolveFilenameW(IN PUNICODE_STRING szFileName, OUT PCHAR szResult, IN USHORT szResultSize)
{
WCHAR *p, c;
OBJECT_ATTRIBUTES oa;
ANSI_STRING FileNameAnsi;
UNICODE_STRING FileNameUnicode;
HANDLE hLink;
NTSTATUS rc;
int NumberOfLinks = 0;
WCHAR buffer[chMAX_PATH];
USHORT OriginalLength;
UNICODE_STRING target;
ANSI_STRING targeta;
ASSERT(szFileName);
ASSERT(szResult);
OriginalLength = szFileName->Length;
restart:
*szResult = '\0';
/* move to the end of the object name */
p = (PWCHAR) ((PCHAR)szFileName->Buffer + szFileName->Length);
/* process the object name from end to the beginning */
while (p != szFileName->Buffer)
{
/* find the last slash */
// p = wcsrchr(p, L'\\');
/*
if (p == NULL)
{
p = szFileName->Buffer;
break;
}
*/
if (*p != L'\\' && *p != L'\0')
{
p--;
continue;
}
c = *p;
*p = L'\0';
// szFileName->Length = OriginalLength - (p - szFileName->Buffer);
// RtlInitAnsiString(&FileNameAnsi, szFileName);
// RtlAnsiStringToUnicodeString(&FileNameUnicode, &FileNameAnsi, TRUE);
// InitializeObjectAttributes(&oa, &FileNameUnicode, OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE, NULL, NULL);
InitializeObjectAttributes(&oa, szFileName, OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE, NULL, NULL);
rc = ZwOpenSymbolicLinkObject(&hLink, GENERIC_READ, &oa);
if (! NT_SUCCESS(rc))
{
*p-- = c;
continue;
}
target.Buffer = buffer;
target.MaximumLength = bMAX_PATH;
target.Length = 0;
rc = ZwQuerySymbolicLinkObject(hLink, &target, NULL);
ZwClose(hLink);
if (! NT_SUCCESS(rc))
{
*p-- = c;
continue;
}
*p = c;
//XXX can we have circular links?
if (NumberOfLinks++ < MAX_NUMBER_OF_LINKS)
{
wcscat(buffer, p);
RtlInitUnicodeString(szFileName, buffer);
goto restart;
}
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("ResolveFilename: NumberOfLinks=%d. bailing out. %s\n", NumberOfLinks, szResult));
break;
}
// wcscat(szResult, p);
// LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("ResolveFilename: name=%s number of links=%d\n", szResult, NumberOfLinks));
return TRUE;
}
/*
* GetPathFromOA()
*
* Description:
* Resolve an object handle to an object name.
*
* Parameters:
* ObjectAttributes - opaque structure describing an object handle.
* OutBuffer - output buffer where an object name will be saved to.
* OutBufferSize - size of an output buffer.
* ResolveLinks - do symbolic links need to be resolved?
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
#define FINISH_GetPathFromOA(msg) \
do { \
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, msg); \
return FALSE; \
} while(0)
BOOLEAN
GetPathFromOA(IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PCHAR OutBuffer, IN USHORT OutBufferSize, IN BOOLEAN ResolveLinks)
{
NTSTATUS rc;
PVOID Object = NULL;
ULONG len;
CHAR Buffer[sizeof(OBJECT_NAME_INFORMATION) + bMAX_PATH];
POBJECT_NAME_INFORMATION pONI = (POBJECT_NAME_INFORMATION) Buffer;
BOOLEAN ret = FALSE;
UNICODE_STRING ObjectName;
PUNICODE_STRING pusFilename = NULL;
ANSI_STRING name;
if (! ARGUMENT_PRESENT(ObjectAttributes) || OutBuffer == NULL)
return(FALSE);
try
{
if (KeGetPreviousMode() != KernelMode)
ProbeForRead(ObjectAttributes, sizeof(OBJECT_ATTRIBUTES), sizeof(ULONG));
if (ObjectAttributes->Length != sizeof(OBJECT_ATTRIBUTES))
FINISH_GetPathFromOA(("GetPathFromOA: Invalid ObjectAttributes length %d\n", ObjectAttributes->Length));
if (! ARGUMENT_PRESENT(ObjectAttributes->ObjectName) )
return FALSE;
if (KeGetPreviousMode() != KernelMode)
{
ProbeForRead(ObjectAttributes->ObjectName, sizeof(UNICODE_STRING), sizeof(ULONG));
ObjectName = ProbeAndReadUnicodeString(ObjectAttributes->ObjectName);
}
else
{
ObjectName = *ObjectAttributes->ObjectName;
}
if (ObjectName.Length == 0)
return FALSE;
if (((ObjectName.Length & (sizeof(WCHAR) - 1)) != 0) ||
(ObjectName.Length > bMAX_PATH - sizeof(WCHAR)) )
FINISH_GetPathFromOA(("GetPathFromOA: invalid wchar string length = %d\n", ObjectName.Length));
if (KeGetPreviousMode() != KernelMode)
ProbeForRead(ObjectName.Buffer, ObjectName.Length, sizeof(WCHAR));
}
except(EXCEPTION_EXECUTE_HANDLER)
{
NTSTATUS status = GetExceptionCode();
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("GetPathFromOA(): caught an exception. status = 0x%x\n", status));
return FALSE;
}
pusFilename = &ObjectName;
/*
* is the filename referenced in relation to some directory?
* if so, append the filename to a specified directory name
*/
if (ARGUMENT_PRESENT(ObjectAttributes->RootDirectory))
{
if (! NT_SUCCESS( ObReferenceObjectByHandle(ObjectAttributes->RootDirectory, 0, 0,
KernelMode, &Object, NULL) ))
{
FINISH_GetPathFromOA(("GetPathFromOA(): ObReferenceObjectByHandle() failed. Object = %x\n", Object));
}
if (Object == NULL)
{
FINISH_GetPathFromOA(("GetPathFromOA(): Object = NULL\n"));
}
if (! NT_SUCCESS( ObQueryNameString(Object, pONI, bMAX_PATH, &len) ))
{
ObDereferenceObject(Object);
FINISH_GetPathFromOA(("GetPathFromOA(): ObQueryNameString() failed\n"));
}
ObDereferenceObject(Object);
Object = NULL;
/* extracted directory name */
pusFilename = &pONI->Name;
/* is the directory name too long? */
if (pusFilename->Length >= bMAX_PATH - sizeof(WCHAR))
FINISH_GetPathFromOA(("GetPathFromOA(): directory name is too long\n"));
/*
* pusFilename points to a buffer of MAX_PATH size, ObQueryNameString() sets MaximumLength to the length
* of the directory name, we need to reset this back to MAX_PATH to be able to append a filename
* (reusing the same buffer)
*/
pusFilename->MaximumLength = bMAX_PATH;
if (pusFilename->Buffer[ (pusFilename->Length / sizeof(WCHAR)) - 1 ] != L'\\')
{
pusFilename->Buffer[ pusFilename->Length / sizeof(WCHAR) ] = L'\\';
pusFilename->Length += sizeof(WCHAR);
}
if (RtlAppendUnicodeStringToString(pusFilename, ObjectAttributes->ObjectName) == STATUS_BUFFER_TOO_SMALL)
{
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("GetPathFromOA: 1 %S\n", pusFilename->Buffer));
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("GetPathFromOA: 2 %S\n", ObjectAttributes->ObjectName->Buffer));
FINISH_GetPathFromOA(("GetPathFromOA(): RtlAppendUnicodeStringToString() = STATUS_BUFFER_TOO_SMALL\n"));
}
}
if (NT_SUCCESS(RtlUnicodeStringToAnsiString(&name, pusFilename, TRUE)))
{
if (ResolveLinks == TRUE)
{
ret = ResolveFilename(name.Buffer, OutBuffer, OutBufferSize);
}
else
{
if (name.Length >= OutBufferSize - 1)
{
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("GetPathFromOA: Pathname too long %d\n", name.Length));
OutBuffer[0] = 0;
ret = FALSE;
}
else
{
strcpy(OutBuffer, name.Buffer);
ret = TRUE;
}
}
RtlFreeAnsiString(&name);
}
// LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("%d GetPathFromOA: %s (%S)\n", (ULONG) PsGetCurrentProcessId(), OutBuffer, pusFilename->Buffer));
return ret;
}
BOOLEAN
GetPathFromOAW(IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PCHAR OutBuffer, IN USHORT OutBufferSize, IN BOOLEAN ResolveLinks)
{
NTSTATUS rc;
PVOID Object = NULL;
ULONG len;
CHAR Buffer[sizeof(OBJECT_NAME_INFORMATION) + bMAX_PATH];
POBJECT_NAME_INFORMATION pONI = (POBJECT_NAME_INFORMATION) Buffer;
BOOLEAN ret = FALSE;
UNICODE_STRING ObjectName;
PUNICODE_STRING pusFilename = NULL;
ANSI_STRING name;
if (! ARGUMENT_PRESENT(ObjectAttributes) || OutBuffer == NULL)
return(FALSE);
try
{
if (KeGetPreviousMode() != KernelMode)
ProbeForRead(ObjectAttributes, sizeof(OBJECT_ATTRIBUTES), sizeof(ULONG));
if (ObjectAttributes->Length != sizeof(OBJECT_ATTRIBUTES))
FINISH_GetPathFromOA(("GetPathFromOA: Invalid ObjectAttributes length %d\n", ObjectAttributes->Length));
if (! ARGUMENT_PRESENT(ObjectAttributes->ObjectName) )
return FALSE;
if (KeGetPreviousMode() != KernelMode)
{
ProbeForRead(ObjectAttributes->ObjectName, sizeof(UNICODE_STRING), sizeof(ULONG));
ObjectName = ProbeAndReadUnicodeString(ObjectAttributes->ObjectName);
}
else
{
ObjectName = *ObjectAttributes->ObjectName;
}
if (ObjectName.Length == 0)
return FALSE;
if (((ObjectName.Length & (sizeof(WCHAR) - 1)) != 0) ||
(ObjectName.Length > bMAX_PATH - sizeof(WCHAR)) )
FINISH_GetPathFromOA(("GetPathFromOA: invalid wchar string length = %d\n", ObjectName.Length));
if (KeGetPreviousMode() != KernelMode)
ProbeForRead(ObjectName.Buffer, ObjectName.Length, sizeof(WCHAR));
}
except(EXCEPTION_EXECUTE_HANDLER)
{
NTSTATUS status = GetExceptionCode();
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("GetPathFromOA(): caught an exception. status = 0x%x\n", status));
return FALSE;
}
pusFilename = &ObjectName;
/*
* is the filename referenced in relation to some directory?
* if so, append the filename to a specified directory name
*/
if (ARGUMENT_PRESENT(ObjectAttributes->RootDirectory))
{
if (! NT_SUCCESS( ObReferenceObjectByHandle(ObjectAttributes->RootDirectory, 0, 0,
KernelMode, &Object, NULL) ))
{
ObDereferenceObject(Object);
FINISH_GetPathFromOA(("GetPathFromOA(): ObReferenceObjectByHandle() failed\n"));
}
if (Object == NULL)
{
ObDereferenceObject(Object);
FINISH_GetPathFromOA(("GetPathFromOA(): Object = NULL\n"));
}
if (! NT_SUCCESS( ObQueryNameString(Object, pONI, bMAX_PATH, &len) ))
{
ObDereferenceObject(Object);
FINISH_GetPathFromOA(("GetPathFromOA(): ObQueryNameString() failed\n"));
}
ObDereferenceObject(Object);
Object = NULL;
/* extracted directory name */
pusFilename = &pONI->Name;
/* is the directory name too long? */
if (pusFilename->Length >= bMAX_PATH - sizeof(WCHAR))
FINISH_GetPathFromOA(("GetPathFromOA(): directory name is too long\n"));
/*
* pusFilename points to a buffer of MAX_PATH size, ObQueryNameString() sets MaximumLength to the length
* of the directory name, we need to reset this back to MAX_PATH to be able to append a filename
* (reusing the same buffer)
*/
pusFilename->MaximumLength = bMAX_PATH;
pusFilename->Buffer[ pusFilename->Length / sizeof(WCHAR) ] = L'\\';
pusFilename->Length += sizeof(WCHAR);
if (RtlAppendUnicodeStringToString(pusFilename, ObjectAttributes->ObjectName) == STATUS_BUFFER_TOO_SMALL)
{
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("GetPathFromOA: 1 %S\n", pusFilename->Buffer));
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("GetPathFromOA: 2 %S\n", ObjectAttributes->ObjectName->Buffer));
FINISH_GetPathFromOA(("GetPathFromOA(): RtlAppendUnicodeStringToString() = STATUS_BUFFER_TOO_SMALL\n"));
}
}
if (ResolveLinks == TRUE)
{
ret = ResolveFilenameW(pusFilename, OutBuffer, OutBufferSize);
}
//XXX
if (NT_SUCCESS(RtlUnicodeStringToAnsiString(&name, pusFilename, TRUE)))
{
if (ResolveLinks == TRUE)
{
ret = ResolveFilename(name.Buffer, OutBuffer, OutBufferSize);
}
else
{
if (name.Length >= OutBufferSize - 1)
{
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("GetPathFromOA: Pathname too long %d\n", name.Length));
OutBuffer[0] = 0;
ret = FALSE;
}
else
{
strcpy(OutBuffer, name.Buffer);
ret = TRUE;
}
}
RtlFreeAnsiString(&name);
}
// LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("%d GetPathFromOA: %s (%S)\n", (ULONG) PsGetCurrentProcessId(), OutBuffer, pusFilename->Buffer));
return ret;
}
/*
* ConvertLongFileNameToShort()
*
* Description:
* Converts long windows filenames to their DOS short equivalent filenames
* (i.e. c:\program files to c:\progra~1).
*
* Parameters:
* LongFileName - long filename buffer.
* ShortFileName - output buffer where a short filename is written to.
* ShortFileNameSize - size of an output buffer (in bytes).
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
#if 0
BOOLEAN
ConvertLongFileNameToShort(IN PCHAR LongFileName, OUT PCHAR ShortFileName, IN USHORT ShortFileNameSize)
{
int LongFileNameIndex = 0, ShortFileNameIndex = 0, CurrentFileNameLength, TotalLength, ExtensionLength, NumberOfSpaces;
BOOLEAN ProcessingExtension = FALSE;
CHAR ch, Extension[3];
if (LongFileName == NULL)
return FALSE;
TotalLength = strlen(LongFileName);
/* if the filename does not start with X:\ then assume \device\blah\ format and skip over the first 2 slashes */
if (LongFileName[0] == '\\')
{
int Slashes = 0;
do
{
if ( (ch = ShortFileName[ShortFileNameIndex++] = LongFileName[LongFileNameIndex++]) == '\0') return TRUE;
if (ch == '\\') ++Slashes;
} while (Slashes != 3);
}
for (NumberOfSpaces = ExtensionLength = CurrentFileNameLength = 0; ; LongFileNameIndex++)
{
/* if we finished traversing the entire directory path or reached a '\' then process the filename (append the extension if necessary) */
if (LongFileNameIndex == TotalLength || LongFileName[LongFileNameIndex] == '\\')
{
/*
* if the filename is longer than 8 chars or extension is longer than 3 chars then we need
* to create a 6 char filename followed by a '~1' and the first 3 chars of the last extension
*/
if (CurrentFileNameLength > 8 || ExtensionLength > 3 || NumberOfSpaces > 0)
{
CurrentFileNameLength -= NumberOfSpaces;
if (CurrentFileNameLength > 7)
{
ShortFileName[ShortFileNameIndex - 2] = '~';
ShortFileName[ShortFileNameIndex - 1] = '1';
}
else if (CurrentFileNameLength == 7)
{
ShortFileName[ShortFileNameIndex - 1] = '~';
ShortFileName[ShortFileNameIndex++] = '1';
}
else
{
ShortFileName[ShortFileNameIndex++] = '~';
ShortFileName[ShortFileNameIndex++] = '1';
}
}
if (ExtensionLength > 0)
{
ShortFileName[ShortFileNameIndex++] = '.';
ShortFileName[ShortFileNameIndex++] = Extension[0];
if (ExtensionLength > 1)
{
ShortFileName[ShortFileNameIndex++] = Extension[1];
if (ExtensionLength > 2)
ShortFileName[ShortFileNameIndex++] = Extension[2];
}
ExtensionLength = 0;
ProcessingExtension = FALSE;
}
/* if we are done traversing the entire path than we can bail */
if (LongFileNameIndex == TotalLength)
break;
ShortFileName[ShortFileNameIndex++] = '\\';
NumberOfSpaces = CurrentFileNameLength = 0;
continue;
}
if (LongFileName[LongFileNameIndex] == '.')
{
ProcessingExtension = TRUE;
ExtensionLength = 0;
continue;
}
if (ProcessingExtension == TRUE)
{
if (ExtensionLength++ < 3)
Extension[ExtensionLength - 1] = LongFileName[LongFileNameIndex];
continue;
}
if (((CurrentFileNameLength++) - NumberOfSpaces) < 8)
{
if (LongFileName[LongFileNameIndex] != ' ')
ShortFileName[ShortFileNameIndex++] = LongFileName[LongFileNameIndex];
else
++NumberOfSpaces;
}
}
ShortFileName[ShortFileNameIndex++] = 0;
return TRUE;
}
#endif
/*
* GetNameFromHandle()
*
* Description:
* Resolve an object handle to an object name.
*
* Parameters:
* ObjectHandle - handle of an object whose name we are trying to obtain.
* OutBuffer - output buffer where an object name will be saved to.
* OutBufferSize - size of an output buffer (in bytes).
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
PWSTR
GetNameFromHandle(IN HANDLE ObjectHandle, OUT PWSTR OutBuffer, IN USHORT OutBufferSize)
{
PVOID Object = NULL;
NTSTATUS rc;
POBJECT_NAME_INFORMATION pONI = (POBJECT_NAME_INFORMATION) OutBuffer;
ULONG len;
rc = ObReferenceObjectByHandle(ObjectHandle, GENERIC_READ, NULL, KernelMode, &Object, NULL);
if (! NT_SUCCESS(rc))
{
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("%d GetNameFromHandle: ObReferenceObjectByHandle failed\n", (ULONG) PsGetCurrentProcessId()));
return NULL;
}
rc = ObQueryNameString(Object, pONI, OutBufferSize - sizeof(OBJECT_NAME_INFORMATION)*sizeof(WCHAR), &len);
if (! NT_SUCCESS(rc))
{
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_VERBOSE, ("%d GetNameFromHandle: ObQueryNameString failed\n", (ULONG) PsGetCurrentProcessId()));
return NULL;
}
// _snprintf(OutBuffer, OutBufferSize, "%S", pONI->Name.Buffer);
// OutBuffer[OutBufferSize - 1] = 0;
// LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("%S (%s)\n", pONI->Name.Buffer, OutBuffer));
ObDereferenceObject(Object);
return pONI->Name.Buffer;
// return TRUE;
}
/*
* StripFileMacros()
*
* Description:
* Strip file names of %SystemRoot% and %SystemDrive% macros as well as any specifications.
*
* Parameters:
* Path - ASCII file path to parse.
* Buffer - pointer to an Object where the final result will be saved.
* BufferSize - size of the output Buffer.
*
* Returns:
* Pointer to a stripped ASCII path.
*/
PCHAR
StripFileMacros(IN PCHAR Path, OUT PCHAR Buffer, IN USHORT BufferSize)
{
if (_strnicmp(Path, "%systemdrive%:", 14) == 0)
{
return Path + 14;
}
if (_strnicmp(Path, "%systemroot%\\", 13) == 0)
{
if (_snprintf(Buffer, MAX_PATH, "%s%s", SystemRootUnresolved, Path + 12) < 0)
return NULL;
Path = Buffer;
}
if (Path[1] == ':' && Path[2] == '\\' && (isalpha(Path[0]) || Path[0] == '?' || Path[0] == '*'))
{
Path += 2;
}
return Path;
}
/*
* FixupFilename()
*
* Description:
* Get canonical name for a file (without the drive specification, i.e. \windows\blah.exe)
*
* Parameters:
* szFileName - filename to resolve.
* szResult - output buffer.
* szResultSize - size of an output buffer.
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
BOOLEAN
FixupFilename(IN PCHAR szFileName, OUT PCHAR szResult, IN USHORT szResultSize)
{
/* skip over \??\ */
if (_strnicmp(szFileName, "\\??\\", 4) == 0)
{
szFileName += 4;
}
/* replace "\SystemRoot" references with the actual system root directory */
if (_strnicmp(szFileName, "\\SystemRoot\\", 12) == 0)
{
_snprintf(szResult, szResultSize, "%s\\%s", SystemRootDirectory, szFileName + 12);
szResult[ szResultSize - 1 ] = 0;
return TRUE;
}
/* skip over X: drive specifications */
if (isalpha(szFileName[0]) && szFileName[1] == ':' && szFileName[2] == '\\')
{
szFileName += 2;
}
strncpy(szResult, szFileName, szResultSize);
szResult[ szResultSize - 1 ] = 0;
return TRUE;
}
/*
* AreMalformedExtensionsAllowed()
*
* Description:
* Check whether the current process is allowed to run binaries with malformed file extensions.
*
* Parameters:
* None.
*
* Returns:
* FALSE if binaries with malformed extensions are not allowed to run. TRUE otherwise.
*/
BOOLEAN
AreMalformedExtensionsAllowed()
{
PIMAGE_PID_ENTRY CurrentProcess;
BOOLEAN MalformedExtensionsAllowed = FALSE;
/* check the global policy first */
if (! IS_EXTENSION_PROTECTION_ON(gSecPolicy))
return TRUE;
/* now check the process specific policy */
CurrentProcess = FindImagePidEntry(CURRENT_PROCESS_PID, 0);
if (CurrentProcess != NULL)
{
MalformedExtensionsAllowed = ! IS_EXTENSION_PROTECTION_ON(CurrentProcess->SecPolicy);
}
else
{
LOG(LOG_SS_PATHPROC, LOG_PRIORITY_DEBUG, ("%d AreMalformedExtensionsAllowed: CurrentProcess = NULL!\n", (ULONG) PsGetCurrentProcessId()));
}
return MalformedExtensionsAllowed;
}
/*
* VerifyExecutableName()
*
* Description:
* Make sure the executed binary does not have a funny filename.
* Look out for non-standard extensions (.exe, etc) and double
* extensions that are commonly "ab"used by malware.
*
* Parameters:
* szFileName - filename to verify.
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/