-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolicy.c
3243 lines (2355 loc) · 76.5 KB
/
policy.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:
*
* policy.c
*
* Abstract:
*
* This module implements various security policy parsing and enforcement routines.
*
* Author:
*
* Eugene Tsyrklevich 16-Feb-2004
*
* Revision History:
*
* None.
*/
// XXX rename all funcs as SpYYY ? (same for other modules?)
#include <NTDDK.h>
#include "policy.h"
#include "pathproc.h"
#include "procname.h"
#include "hookproc.h"
#include "media.h"
#include "learn.h"
#include "misc.h"
#include "i386.h"
#include "process.h"
#include "log.h"
BOOLEAN PolicyParseRule(OUT PSECURITY_POLICY pSecPolicy, IN PCHAR rule, OUT BOOLEAN *Critical);
BOOLEAN PolicyParsePolicyRule(OUT PSECURITY_POLICY pSecPolicy, IN PCHAR Operation, IN PCHAR rule, OUT BOOLEAN *Critical);
BOOLEAN PolicyParseObjectRule(PSECURITY_POLICY pSecPolicy, RULE_TYPE RuleType, PCHAR Operation, PCHAR rule);
BOOLEAN PolicyParseSyscallRule(PSECURITY_POLICY pSecPolicy, PCHAR SyscallName, PCHAR rule);
BOOLEAN PolicyParseProtectionRule(PSECURITY_POLICY pSecPolicy, PCHAR Operation, PCHAR rule);
BOOLEAN PolicyParseMediaRule(PSECURITY_POLICY pSecPolicy, PCHAR Operation, PCHAR rule);
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, InitPolicy)
#pragma alloc_text (PAGE, PolicyPostBootup)
#pragma alloc_text (PAGE, PolicyRemove)
#endif
/*
* example:
*
* SystemRoot - \device\harddisk1\windows
* SystemRootUnresolved - c:\windows
* SystemRootDirectory - \windows
* CDrive - \device\harddisk1
*/
CHAR SystemDrive, SystemRoot[MAX_PATH], SystemRootUnresolved[MAX_PATH], *SystemRootDirectory, CDrive[MAX_PATH];
USHORT SystemRootLength = 0, SystemRootUnresolvedLength = 0, SystemRootDirectoryLength = 0, CDriveLength = 0;
USHORT gPolicyLineNumber;
PWSTR gPolicyFilename, gFilePath;
// to be portable on 32 & 64 bit platforms
ULONG NumberOfBitsInUlong, UlongBitShift;
/* LoadPolicy() can be used by only one thread at a time due to use of global variables */
KMUTEX LoadPolicyMutex;
/* Global Security Policy */
SECURITY_POLICY gSecPolicy;
/*
* InitPolicy()
*
* Description:
* Initialize the policy engine. Load the global policy.
*
* NOTE: Called once during driver initialization (DriverEntry()).
*
* Parameters:
* None.
*
* Returns:
* TRUE if everything is OK, FALSE if failed.
*/
BOOLEAN
InitPolicy()
{
NumberOfBitsInUlong = sizeof(ULONG) * 8;
UlongBitShift = sizeof(ULONG) == 4 ? 5 : 6;
/* gets reinitialized correctly once bootup is complete (see PolicyPostBootup) */
SystemDrive = 'C';
if (ReadSymlinkValue(L"\\SystemRoot", SystemRootUnresolved, MAX_PATH) == FALSE)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("InitPolicy: ReadSymlinkValue failed\n"));
return FALSE;
}
SystemRootUnresolvedLength = (USHORT) strlen(SystemRootUnresolved);
ResolveFilename(SystemRootUnresolved, SystemRoot, MAX_PATH);
/* extract the system directory name by itself (i.e. \windows) */
SystemRootDirectory = strrchr(SystemRoot, '\\');
if (SystemRootDirectory == NULL)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("InitPolicy: SystemRootDirectory is NULL\n"));
return FALSE;
}
SystemRootDirectoryLength = (USHORT) strlen(SystemRootDirectory);
if (ReadSymlinkValue(L"\\??\\C:", CDrive, MAX_PATH) == FALSE)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("InitPolicy: Failed to open C: symbolic link\n"));
return FALSE;
}
CDriveLength = (USHORT) strlen(CDrive);
if (PolicyPostBootup() == FALSE)
{
/*
* if boot process is not complete yet then we cannot get SystemRootUnresolved (i.e. c:\windows)
* because parts of registry are not initialized yet (see PolicyPostBootup)
*
* In that case, try to assemble SystemRootUnresolved manually
*/
SystemRootUnresolved[0] = SystemDrive;
SystemRootUnresolved[1] = ':';
strcpy(SystemRootUnresolved + 2, SystemRootDirectory);
}
LOG(LOG_SS_POLICY, LOG_PRIORITY_VERBOSE, ("InitPolicy: SystemRoot=%s (%s, %s)\n", SystemRoot, SystemRootUnresolved, SystemRootDirectory));
KeInitializeMutex(&LoadPolicyMutex, 0);
RtlZeroMemory(&gSecPolicy, sizeof(gSecPolicy));
KeInitializeSpinLock(&gSecPolicy.SpinLock);
if (LearningMode == TRUE)
return TRUE;
if (FindAndLoadSecurityPolicy(&gSecPolicy, L"computer", NULL) == FALSE)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_WARNING, ("InitPolicy: LoadSecurityPolicy(computer.policy) failed\n"));
gSecPolicy.DefaultPolicyAction = ACTION_PERMIT_DEFAULT;
}
if (gSecPolicy.DefaultPolicyAction != ACTION_PERMIT_DEFAULT)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_WARNING, ("InitPolicy: Global policy default action must be permit\n"));
gSecPolicy.DefaultPolicyAction = ACTION_PERMIT_DEFAULT;
}
return TRUE;
}
/*
* PolicyPostBootup()
*
* Description:
* Finish initializing system variables once the bootup process is complete.
* We are unable to read the SystemRoot registry value before the bootup is complete since
* that part of the registry has not been initialized yet.
*
* Parameters:
* None.
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
BOOLEAN
PolicyPostBootup()
{
ASSERT(BootingUp == FALSE);
if (ReadStringRegistryValueA(L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion", L"SystemRoot", SystemRootUnresolved, MAX_PATH) == FALSE)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("PolicyPostBootup: Failed to open SystemRoot registry key\n"));
return FALSE;
}
SystemRootUnresolvedLength = (USHORT) strlen(SystemRootUnresolved);
SystemDrive = (CHAR) toupper(SystemRootUnresolved[0]);
return TRUE;
}
/*
* PolicyRemove()
*
* Description:
* Shutdown the policy engine. Delete the global policy
*
* Parameters:
* None.
*
* Returns:
* Nothing.
*/
void
PolicyRemove()
{
PolicyDelete(&gSecPolicy);
}
/*
* PolicyDelete()
*
* Description:
* Delete a security policy. Free all the rules associated with a policy.
*
* Parameters:
* pSecPolicy - pointer to a security policy to delete.
*
* Returns:
* Nothing.
*/
void
PolicyDelete(IN PSECURITY_POLICY pSecPolicy)
{
PPOLICY_RULE r, tmp;
KIRQL irql;
UCHAR i;
if (pSecPolicy == NULL)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("PolicyDelete: pSecPolicy is NULL\n"));
return;
}
if (pSecPolicy->Initialized == FALSE)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_VERBOSE, ("PolicyDelete: pSecPolicy is not initialized\n"));
return;
}
KeAcquireSpinLock(&pSecPolicy->SpinLock, &irql);
for (i = 0; i < RULE_LASTONE; i++)
{
r = pSecPolicy->RuleList[i];
while (r)
{
tmp = r;
r = (PPOLICY_RULE) r->Next;
ExFreePoolWithTag(tmp, _POOL_TAG);
}
}
if (pSecPolicy->Name)
{
ExFreePoolWithTag(pSecPolicy->Name, _POOL_TAG);
pSecPolicy->Name = NULL;
}
pSecPolicy->Initialized = FALSE;
RtlZeroMemory(pSecPolicy->RuleList, sizeof(pSecPolicy->RuleList));
KeReleaseSpinLock(&pSecPolicy->SpinLock, irql);
}
/*
* LoadSecurityPolicy()
*
* Description:
* Parses and loads a security policy.
*
* Parameters:
* pSecPolicy - pointer to a security policy to initialize.
* PolicyFile - string containing the policy filename to parse
* FilePath - string containing full program path of the file we are loading policy for
*
* Returns:
* TRUE if security policy was successfully parsed and loaded, FALSE otherwise.
*/
BOOLEAN
LoadSecurityPolicy(OUT PSECURITY_POLICY pSecPolicy, IN PWSTR PolicyFile, IN PWSTR FilePath)
{
OBJECT_ATTRIBUTES oa;
HANDLE hFile = 0;
UNICODE_STRING usPolicyFile;
ULONG size;
NTSTATUS status;
IO_STATUS_BLOCK isb;
CHAR *p, buffer[POLICY_MAX_RULE_LENGTH];
INT64 offset;
BOOLEAN ret = TRUE, Critical = FALSE;
if (pSecPolicy == NULL || PolicyFile == NULL)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("LoadSecurityPolicy(%x, %x, %x): NULL parameter\n", pSecPolicy, PolicyFile, FilePath));
return FALSE;
}
pSecPolicy->Initialized = FALSE;
pSecPolicy->DefaultPolicyAction = ACTION_NONE;
pSecPolicy->ProtectionFlags = BootingUp ? PROTECTION_ALL_OFF : PROTECTION_ALL_ON;
RtlInitUnicodeString(&usPolicyFile, PolicyFile);
LOG(LOG_SS_POLICY, LOG_PRIORITY_VERBOSE, ("LoadSecurityPolicy: Parsing %S\n", usPolicyFile.Buffer));
InitializeObjectAttributes(&oa, &usPolicyFile, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
if (!NT_SUCCESS(ZwCreateFile(&hFile, GENERIC_READ, &oa, &isb,
NULL, 0, FILE_SHARE_READ, FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0)))
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_VERBOSE, ("LoadSecurityPolicy: Failed to open file %S\n", usPolicyFile.Buffer));
return FALSE;
}
offset = 0;
buffer[0] = 0;
/* only one thread at a time can use LoadPolicyMutex due to use of global variables (PolicyLineNumber, buffer) */
KeWaitForMutexObject(&LoadPolicyMutex, Executive, KernelMode, FALSE, NULL);
gPolicyLineNumber = 1;
gPolicyFilename = usPolicyFile.Buffer;
gFilePath = FilePath;
while (1)
{
status = ZwReadFile(hFile, NULL, NULL, NULL, &isb, (PVOID) buffer, sizeof(buffer) - 1,
(PLARGE_INTEGER) &offset, NULL);
if (! NT_SUCCESS(status))
{
if (status != STATUS_END_OF_FILE)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("LoadSecurityPolicy: ZwReadFile failed rc=%x\n", status));
ret = FALSE;
PolicyDelete(pSecPolicy);
}
break;
}
if (isb.Information == 0)
break;
buffer[isb.Information] = '\0';
/*
* strchr() will return NULL when the line we read exceeds the size of the buffer or
* the last line was not '\n' terminated
*/
if ((p = strchr(buffer, '\n')) == NULL)
{
/* don't try to parse very long lines */
if (isb.Information == sizeof(buffer) - 1)
{
LOG(LOG_SS_POLICY_PARSER, LOG_PRIORITY_WARNING, ("LoadSecurityPolicy(%s:%d): Rule is too long\n", gPolicyFilename, gPolicyLineNumber));
PolicyDelete(pSecPolicy);
ret = FALSE;
break;
}
/* the last rule was not '\n' terminated */
if (PolicyParseRule(pSecPolicy, buffer, &Critical) == FALSE)
{
if (Critical == TRUE)
{
LOG(LOG_SS_POLICY_PARSER, LOG_PRIORITY_DEBUG, ("LoadSecurityPolicy(%S:%d): Encountered a critical error. Aborting.\n", gPolicyFilename, gPolicyLineNumber));
PolicyDelete(pSecPolicy);
ret = FALSE;
break;
}
}
ret = TRUE;
break;
}
*p = 0;
if (p != buffer && *(p - 1) == '\r')
*(p - 1) = 0;
if (PolicyParseRule(pSecPolicy, buffer, &Critical) == FALSE)
{
if (Critical == TRUE)
{
LOG(LOG_SS_POLICY_PARSER, LOG_PRIORITY_DEBUG, ("LoadSecurityPolicy(%S:%d): Encountered a critical error. Aborting.\n", gPolicyFilename, gPolicyLineNumber));
PolicyDelete(pSecPolicy);
ret = FALSE;
break;
}
}
offset += p - buffer + 1;
if (++gPolicyLineNumber > 10000)
{
LOG(LOG_SS_POLICY_PARSER, LOG_PRIORITY_WARNING, ("LoadSecurityPolicy: Policy '%S' is too long. Maximum number of lines is 10000.\n", gPolicyFilename));
PolicyDelete(pSecPolicy);
ret = FALSE;
break;
}
}
ZwClose(hFile);
if (ret != FALSE)
{
pSecPolicy->Initialized = TRUE;
if (pSecPolicy->DefaultPolicyAction == ACTION_NONE)
pSecPolicy->DefaultPolicyAction = DEFAULT_POLICY_ACTION;
}
LOG(LOG_SS_POLICY, LOG_PRIORITY_VERBOSE, ("LoadSecurityPolicy: Done Parsing %S. Total number of lines %d. (ret=%d)\n", usPolicyFile.Buffer, gPolicyLineNumber, ret));
KeReleaseMutex(&LoadPolicyMutex, FALSE);
return ret;
}
/*
* FindAndLoadSecurityPolicy()
*
* Description:
* Finds and loads a security policy associated with a specified executable filename.
*
* Parameters:
* pSecPolicy - pointer to a security policy to initialize.
* FilePath - string specifying the complete path to an executable
* UserName - optional username, if specified check for a policy in "username" directory first
*
* Returns:
* TRUE if security policy was successfully parsed and loaded, FALSE otherwise.
*/
BOOLEAN
FindAndLoadSecurityPolicy(OUT PSECURITY_POLICY pSecPolicy, IN PWSTR FilePath, IN PWSTR UserName)
{
PWSTR filename;
WCHAR PolicyPath[MAX_PATH];
BOOLEAN ret;
int len;
if (pSecPolicy == NULL || FilePath == NULL)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("FindAndLoadSecurityPolicy: NULL argument %x %x\n", pSecPolicy, FilePath));
return FALSE;
}
if (KeGetCurrentIrql() != 0)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("FindAndLoadSecurityPolicy(): irql=%d\n", KeGetCurrentIrql()));
return FALSE;
}
filename = wcsrchr(FilePath, L'\\');
if (filename == NULL)
filename = FilePath;
else
++filename;
/* if user policy load fails, we loop here again to load the global policy */
ReloadPolicy:
if (UserName != NULL)
_snwprintf(PolicyPath, MAX_PATH, L"\\??\\%s\\policy\\%s\\%s.policy", OzoneInstallPath, UserName, filename);
else
_snwprintf(PolicyPath, MAX_PATH, L"\\??\\%s\\policy\\%s.policy", OzoneInstallPath, filename);
PolicyPath[MAX_PATH - 1] = 0;
LOG(LOG_SS_POLICY, LOG_PRIORITY_VERBOSE, ("FindAndLoadSecurityPolicy: Loading policy for %S (%S)\n", PolicyPath, FilePath));
ret = LoadSecurityPolicy(pSecPolicy, PolicyPath, FilePath);
if (ret == FALSE)
{
/* If we can't find a policy specific to a user, try to load a global policy instead */
if (UserName != NULL)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("FindAndLoadSecurityPolicy: Cannot find '%S' policy for user '%S'. Looking for a global policy..\n", filename, UserName));
UserName = NULL;
goto ReloadPolicy;
}
return FALSE;
}
/* allocate extra space for ".policy" string */
len = wcslen(filename) + 7 + 1;
pSecPolicy->Name = ExAllocatePoolWithTag(NonPagedPool, len * sizeof(WCHAR), _POOL_TAG);
if (pSecPolicy->Name != NULL)
{
_snwprintf(pSecPolicy->Name, len, L"%s.policy", filename);
}
else
{
PolicyDelete(pSecPolicy);
ret = FALSE;
}
return ret;
}
/*
* PolicyParseRule()
*
* Description:
* Parses a specified rule.
*
* Parameters:
* pSecPolicy - pointer to a security policy that will contain the parsed rule.
* rule - string buffer containing a rule to parse.
* Critical - Boolean indicating whether the parser should abort parsing the policy due to a critical error.
*
* Returns:
* TRUE if the policy rule was successfully parsed and loaded, FALSE otherwise.
*/
#define SKIP_WHITESPACE(str) do { while(*(str) == ' ' || *(str) == '\t') ++(str); } while(0)
#define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
/* macro shortcut for bailing out of PolicyParseRule() in case of an error */
#define ABORT_PolicyParseRule(msg) \
do { \
LOG(LOG_SS_POLICY_PARSER, LOG_PRIORITY_WARNING, ("Encountered an error while parsing %S:%d :\n", gPolicyFilename, gPolicyLineNumber)); \
LOG(LOG_SS_POLICY_PARSER, LOG_PRIORITY_WARNING, msg); \
return FALSE; \
} while (0)
static BOOLEAN
PolicyParseRule(OUT PSECURITY_POLICY pSecPolicy, IN PCHAR rule, OUT BOOLEAN *Critical)
{
CHAR ServiceName[POLICY_MAX_SERVICE_NAME_LENGTH];
PCHAR OriginalRule = rule;
int i = 0, SawSpace = 0, SawUnderscore = 0;
*Critical = FALSE;
SKIP_WHITESPACE(rule);
/* skip empty lines */
if (*rule == 0)
return TRUE;
/* comments start with '#' */
if (*rule == '#')
return TRUE;
/*
* Parse the service name. Format: "ServiceName:" or "ServiceName_OperationType:"
* where ServiceName can be "file", "registry", "event", "memory", etc
* and OperationType can be "read", "write", "rw"
*/
while (*rule != 0 && *rule != ':')
{
/* is the specified syscall name too long? */
if (i > POLICY_MAX_SERVICE_NAME_LENGTH - 1)
ABORT_PolicyParseRule(("Rule type specification is too long. Maximum rule type specification length is %d characters.\n", POLICY_MAX_SERVICE_NAME_LENGTH));
/* allow whitespace before the colon */
if (IS_WHITESPACE(*rule))
{
++rule;
SawSpace = 1;
continue;
}
/* Service Names are not allowed to contain a space */
if (SawSpace)
ABORT_PolicyParseRule(("Rule type specification cannot contain a space\n"));
/* Expecting to see 1 underscore '_' */
if (*rule == '_')
{
/* There can be only be 1 underscore char. and it cannot be the first char. */
if (i == 0 || SawUnderscore)
ABORT_PolicyParseRule(("Rule type specification cannot contain multiple underscore characters\n"));
/* remember the underscore position */
SawUnderscore = i;
}
ServiceName[i++] = *rule++;
}
/* Expecting to have read more than 1 character, finishing with a ':' */
if (i == 0 || *rule++ != ':')
ABORT_PolicyParseRule(("Colon not found. Rule type specification must end with a colon ':'.\n"));
ServiceName[i] = 0;
SKIP_WHITESPACE(rule);
/* didn't see any underscores. assume "system_call_name:" format */
if (SawUnderscore == 0)
ABORT_PolicyParseRule(("Underscore not found. Rule type specification must contain an underscore.\n"));
ServiceName[SawUnderscore] = 0;
// file operation
if (strlen(ServiceName) == 4 && _stricmp(ServiceName, "file") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_FILE, ServiceName + SawUnderscore + 1, rule);
// directory operation
if (strlen(ServiceName) == 9 && _stricmp(ServiceName, "directory") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_DIRECTORY, ServiceName + SawUnderscore + 1, rule);
// registry operation
if (strlen(ServiceName) == 8 && _stricmp(ServiceName, "registry") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_REGISTRY, ServiceName + SawUnderscore + 1, rule);
// memory section operation
if (strlen(ServiceName) == 7 && _stricmp(ServiceName, "section") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_SECTION, ServiceName + SawUnderscore + 1, rule);
// memory section / dll operation
if (strlen(ServiceName) == 3 && _stricmp(ServiceName, "dll") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_DLL, ServiceName + SawUnderscore + 1, rule);
// event operation
if (strlen(ServiceName) == 5 && _stricmp(ServiceName, "event") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_EVENT, ServiceName + SawUnderscore + 1, rule);
// semaphore operation
if (strlen(ServiceName) == 9 && _stricmp(ServiceName, "semaphore") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_SEMAPHORE, ServiceName + SawUnderscore + 1, rule);
// mailslot operation
if (strlen(ServiceName) == 8 && _stricmp(ServiceName, "mailslot") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_MAILSLOT, ServiceName + SawUnderscore + 1, rule);
// named pipe operation
if (strlen(ServiceName) == 9 && _stricmp(ServiceName, "namedpipe") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_NAMEDPIPE, ServiceName + SawUnderscore + 1, rule);
// job object operation
if (strlen(ServiceName) == 3 && _stricmp(ServiceName, "job") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_JOB, ServiceName + SawUnderscore + 1, rule);
// mutant operation
if (strlen(ServiceName) == 5 && _stricmp(ServiceName, "mutex") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_MUTANT, ServiceName + SawUnderscore + 1, rule);
// port operation
if (strlen(ServiceName) == 4 && _stricmp(ServiceName, "port") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_PORT, ServiceName + SawUnderscore + 1, rule);
// symlink operation
if (strlen(ServiceName) == 7 && _stricmp(ServiceName, "symlink") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_SYMLINK, ServiceName + SawUnderscore + 1, rule);
// timer operation
if (strlen(ServiceName) == 5 && _stricmp(ServiceName, "timer") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_TIMER, ServiceName + SawUnderscore + 1, rule);
// process operation
if (strlen(ServiceName) == 7 && _stricmp(ServiceName, "process") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_PROCESS, ServiceName + SawUnderscore + 1, rule);
// driver operation
if (strlen(ServiceName) == 6 && _stricmp(ServiceName, "driver") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_DRIVER, ServiceName + SawUnderscore + 1, rule);
// object directory operation
if (strlen(ServiceName) == 6 && _stricmp(ServiceName, "dirobj") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_DIROBJ, ServiceName + SawUnderscore + 1, rule);
// atom operation
if (strlen(ServiceName) == 4 && _stricmp(ServiceName, "atom") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_ATOM, ServiceName + SawUnderscore + 1, rule);
// network operation
if (strlen(ServiceName) == 7 && _stricmp(ServiceName, "network") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_NETWORK, ServiceName + SawUnderscore + 1, rule);
// service operation
if (strlen(ServiceName) == 7 && _stricmp(ServiceName, "service") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_SERVICE, ServiceName + SawUnderscore + 1, rule);
// time operation
if (strlen(ServiceName) == 4 && _stricmp(ServiceName, "time") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_TIME, ServiceName + SawUnderscore + 1, rule);
// token operation
if (strlen(ServiceName) == 5 && _stricmp(ServiceName, "token") == 0)
return PolicyParseObjectRule(pSecPolicy, RULE_TOKEN, ServiceName + SawUnderscore + 1, rule);
/*
* non object rules
*/
// syscall
if (strlen(ServiceName) == 7 && _stricmp(ServiceName, "syscall") == 0)
return PolicyParseSyscallRule(pSecPolicy, ServiceName + SawUnderscore + 1, rule);
// policy
if (strlen(ServiceName) == 6 && _stricmp(ServiceName, "policy") == 0)
return PolicyParsePolicyRule(pSecPolicy, ServiceName + SawUnderscore + 1, rule, Critical);
// protection
if (strlen(ServiceName) == 10 && _stricmp(ServiceName, "protection") == 0)
return PolicyParseProtectionRule(pSecPolicy, ServiceName + SawUnderscore + 1, rule);
// media
if (strlen(ServiceName) == 5 && _stricmp(ServiceName, "media") == 0)
return PolicyParseMediaRule(pSecPolicy, ServiceName + SawUnderscore + 1, rule);
ABORT_PolicyParseRule(("Invalid rule type specification: '%s'.\n", ServiceName));
}
/*
* ParseDllOperation()
*
* Description:
* Parses an operation (i.e. "load" in "dll_load") specified for a DLL object rule.
*
* Parameters:
* Operation - specified operation.
*
* Returns:
* OP_INVALID if a specified operation is invalid or an OP_* value corresponding to the parsed operation.
*/
UCHAR
ParseDllOperation(IN PCHAR Operation)
{
if (strlen(Operation) == 4 && _stricmp(Operation, "load") == 0)
return OP_LOAD;
if (strlen(Operation) == 3 && _stricmp(Operation, "all") == 0)
return OP_ALL;
return OP_INVALID;
}
/*
* ParseTimeOperation()
*
* Description:
* Parses an operation (i.e. "change" in "time_change") specified for a Time object rule.
*
* Parameters:
* Operation - specified operation.
*
* Returns:
* OP_INVALID if a specified operation is invalid or an OP_* value corresponding to the parsed operation.
*/
UCHAR
ParseTimeOperation(IN PCHAR Operation)
{
if (strlen(Operation) == 6 && _stricmp(Operation, "change") == 0)
return OP_LOAD;
if (strlen(Operation) == 3 && _stricmp(Operation, "all") == 0)
return OP_ALL;
return OP_INVALID;
}
/*
* ParseTokenOperation()
*
* Description:
* Parses an operation (i.e. "modify" in "token_modify") specified for a Token object rule.
*
* Parameters:
* Operation - specified operation.
*
* Returns:
* OP_INVALID if a specified operation is invalid or an OP_* value corresponding to the parsed operation.
*/
UCHAR
ParseTokenOperation(IN PCHAR Operation)
{
if (strlen(Operation) == 6 && _stricmp(Operation, "modify") == 0)
return OP_TOKEN_MODIFY;
if (strlen(Operation) == 3 && _stricmp(Operation, "all") == 0)
return OP_ALL;
return OP_INVALID;
}
/*
* ParsePortOperation()
*
* Description:
* Parses an operation (i.e. "create" in "port_create") specified for a port object rule.
*
* Parameters:
* Operation - specified operation.
*
* Returns:
* OP_INVALID if a specified operation is invalid or an OP_* value corresponding to the parsed operation.
*/
UCHAR
ParsePortOperation(IN PCHAR Operation)
{
if (strlen(Operation) == 6 && _stricmp(Operation, "create") == 0)
return OP_PORT_CREATE;
if (strlen(Operation) == 7 && _stricmp(Operation, "connect") == 0)
return OP_PORT_CONNECT;
if (strlen(Operation) == 3 && _stricmp(Operation, "all") == 0)
return OP_ALL;
return OP_INVALID;
}
/*
* ParseCreateOpenOperation()
*
* Description:
* Parses a create or an open operation (i.e. "create" in "dirobj_create").
*
* Parameters:
* Operation - specified operation.
*
* Returns:
* OP_INVALID if a specified operation is invalid or an OP_* value corresponding to the parsed operation.
*/
UCHAR
ParseCreateOpenOperation(IN PCHAR Operation)
{
if (strlen(Operation) == 6 && _stricmp(Operation, "create") == 0)
return OP_CREATE;
if (strlen(Operation) == 4 && _stricmp(Operation, "open") == 0)
return OP_OPEN;
if (strlen(Operation) == 3 && _stricmp(Operation, "all") == 0)
return OP_ALL;
return OP_INVALID;
}
/*
* ParseAtomOperation()
*
* Description:
* Parses an operation (i.e. "find" in "atom_find") specified for an atom object rule.
*
* Parameters:
* Operation - specified operation.
*
* Returns:
* OP_INVALID if a specified operation is invalid or an OP_* value corresponding to the parsed operation.
*/
UCHAR
ParseAtomOperation(IN PCHAR Operation)
{
if (strlen(Operation) == 4 && _stricmp(Operation, "find") == 0)
return OP_FIND;
if (strlen(Operation) == 3 && _stricmp(Operation, "add") == 0)
return OP_ADD;
if (strlen(Operation) == 3 && _stricmp(Operation, "all") == 0)
return OP_ALL;