-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
process_util.c
1560 lines (1188 loc) · 42.1 KB
/
process_util.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 2004-2020 Sandboxie Holdings, LLC
* Copyright 2020 David Xanatos, xanasoft.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// Process Management: Various Utilities
//---------------------------------------------------------------------------
#include "process.h"
#include "conf.h"
#include "file.h"
#include "token.h"
#include "api.h"
#include "obj.h"
#include "common/pattern.h"
#include "common/my_version.h"
//---------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------
static BOOLEAN Process_MatchImageGroup(
BOX *box, const WCHAR *group, ULONG group_len, const WCHAR *test_str,
ULONG depth);
static BOOLEAN Process_AddPath_2(
PROCESS *proc, LIST *list, const WCHAR *value, const WCHAR *setting_name,
BOOLEAN AddFirst, BOOLEAN AddStar,
BOOLEAN RemoveBackslashes, BOOLEAN CheckReparse, BOOLEAN* Reparsed, ULONG Level);
//---------------------------------------------------------------------------
// Variables
//---------------------------------------------------------------------------
static const WCHAR *Process_Normal = L"Normal";
static const WCHAR *Process_Open = L"Open";
static const WCHAR *Process_Read = L"Read";
static const WCHAR *Process_Write = L"Write";
static const WCHAR *Process_Closed = L"Closed";
//---------------------------------------------------------------------------
// Process_IsSameBox
//---------------------------------------------------------------------------
_FX BOOLEAN Process_IsSameBox(
PROCESS *proc, PROCESS *proc2, ULONG_PTR proc2_pid)
{
KIRQL irql;
BOOLEAN ok;
BOOLEAN locked;
if (proc2)
locked = FALSE;
else {
proc2 = Process_Find((HANDLE)(ULONG_PTR)proc2_pid, &irql);
locked = TRUE;
}
if (proc2 == proc) {
//
// write access is permitted into the same process
//
ok = TRUE;
} else if (proc2 && (! proc2->terminated) && (! proc2->untouchable)) {
//
// when host image protection is enabled, sandboxed processes
// are protected form being written to by other sandboxed processes even from the same box
//
// Note: this restriction will prevent images located in the sandbox from
// starting not images located on the host
//
if (!proc2->image_from_box && proc2->protect_host_images && proc->image_from_box)
ok = FALSE;
else
//
// write access is only permitted within the same sandbox
// and same session
//
ok = (proc->box->session_id == proc2->box->session_id) &&
(proc->box->name_len == proc2->box->name_len)
&& (_wcsicmp(proc->box->name, proc2->box->name) == 0);
} else {
//
// deny write access
//
ok = FALSE;
}
if (locked) {
ExReleaseResourceLite(Process_ListLock);
KeLowerIrql(irql);
}
return ok;
}
//---------------------------------------------------------------------------
// Process_IsStarter
//---------------------------------------------------------------------------
#ifdef DRV_BREAKOUT
_FX BOOLEAN Process_IsStarter(
PROCESS* proc1, PROCESS* proc2)
{
if (proc1->create_time > proc2->create_time)
return FALSE; // reused pid? the new process can not be older than the on that started it
if (proc1->box->session_id != proc2->box->session_id)
return FALSE; // SID must be same
return proc1->pid == proc2->starter_id;
}
#endif
//---------------------------------------------------------------------------
// Process_MatchImage
//---------------------------------------------------------------------------
_FX BOOLEAN Process_MatchImage(
BOX *box, const WCHAR *pat_str, ULONG pat_len, const WCHAR *test_str,
ULONG depth)
{
PATTERN *pat;
WCHAR *tmp, *expnd;
ULONG tmp_len;
BOOLEAN ok;
//
// if pat_len was specified, we should create the match pattern
// using only the first pat_len characters of pat_str
//
if (pat_len) {
tmp_len = (pat_len + 1) * sizeof(WCHAR);
tmp = Mem_Alloc(box->expand_args->pool, tmp_len);
if (! tmp)
return FALSE;
wcsncpy(tmp, pat_str, pat_len);
tmp[pat_len] = L'\0';
expnd = Conf_Expand(box->expand_args, tmp, NULL);
Mem_Free(tmp, tmp_len);
} else {
expnd = Conf_Expand(box->expand_args, pat_str, NULL);
}
if (! expnd)
return FALSE;
pat = Pattern_Create(box->expand_args->pool, expnd, TRUE, 0);
Mem_FreeString(expnd);
if (! pat)
return FALSE;
//
//
//
if (*pat_str == L'<') {
Conf_AdjustUseCount(TRUE);
ok = Process_MatchImageGroup(
box, Pattern_Source(pat), 0, test_str, depth + 1);
Conf_AdjustUseCount(FALSE);
Pattern_Free(pat);
return ok;
}
//
// create a lower-case copy of test_str
//
ok = FALSE;
tmp_len = (wcslen(test_str) + 1) * sizeof(WCHAR);
tmp = Mem_Alloc(box->expand_args->pool, tmp_len);
if (tmp) {
memcpy(tmp, test_str, tmp_len);
_wcslwr(tmp);
ok = Pattern_Match(pat, tmp, wcslen(tmp));
Mem_Free(tmp, tmp_len);
}
Pattern_Free(pat);
return ok;
}
//---------------------------------------------------------------------------
// Process_MatchImageGroup
//---------------------------------------------------------------------------
_FX BOOLEAN Process_MatchImageGroup(
BOX *box, const WCHAR *group, ULONG group_len, const WCHAR *test_str,
ULONG depth)
{
ULONG index;
BOOLEAN match = FALSE;
if (! group_len)
group_len = wcslen(group);
Conf_AdjustUseCount(TRUE);
for (index = 0; (! match); ++index) {
//
// get next process group setting, compare to passed group name.
// if the setting is <passed_group_name>= then we accept it.
//
ULONG value_len;
const WCHAR *value = Conf_Get(box->name, L"ProcessGroup", index);
if (! value)
break;
value_len = wcslen(value);
if (value_len <= group_len + 1)
continue;
if (_wcsnicmp(value, group, group_len) != 0)
continue;
value += group_len;
if (*value != L',')
continue;
++value;
//
// value now points at the comma-separated
// list of processes in this process group
//
while (*value) {
WCHAR *ptr = wcschr(value, L',');
if (ptr)
value_len = (ULONG)(ULONG_PTR)(ptr - value);
else
value_len = wcslen(value);
if (value_len) {
if (*value != L'<') {
match = Process_MatchImage(
box, value, value_len, test_str, depth + 1);
} else if (depth < 6) {
match = Process_MatchImageGroup(
box, value, value_len, test_str, depth + 1);
}
if (match)
break;
}
value += value_len;
while (*value == L',')
++value;
}
}
Conf_AdjustUseCount(FALSE);
return match;
}
//---------------------------------------------------------------------------
// Process_MatchImageAndGetValue
//---------------------------------------------------------------------------
_FX const WCHAR* Process_MatchImageAndGetValue(BOX *box, const WCHAR* value, const WCHAR* ImageName, ULONG* pLevel)
{
WCHAR* tmp;
ULONG len;
//
// if the setting indicates an image name followed by a comma,
// then match the image name against the executing process.
//
tmp = wcschr(value, L',');
if (tmp) {
BOOLEAN inv, match;
//
// exclamation marks negates the matching
//
if (*value == L'!') {
inv = TRUE;
++value;
} else
inv = FALSE;
len = (ULONG)(tmp - value);
if (len) {
match = Process_MatchImage(box, value, len, ImageName, 1);
if (inv)
match = !match;
if (!match)
return NULL;
else if (pLevel) {
if (len == 1 && *value == L'*')
*pLevel = 2; // 2 - match all
else
*pLevel = inv ? 1 : 0; // 1 - match by negation, 0 - exact match
}
}
value = tmp + 1;
}
else {
if (pLevel) *pLevel = 3; // 3 - global default
}
if (! *value)
return NULL;
return value;
}
//---------------------------------------------------------------------------
// Process_GetConfEx
//---------------------------------------------------------------------------
_FX const WCHAR* Process_GetConfEx(BOX *box, const WCHAR *image_name, const WCHAR* setting)
{
ULONG index = 0;
const WCHAR *value;
const WCHAR *found_value = NULL;
ULONG found_level = -1;
for (index = 0; ; ++index) {
value = Conf_Get(box->name, setting, index);
if (! value)
break;
ULONG level = -1;
value = Process_MatchImageAndGetValue(box, value, image_name, &level);
if (!value || level > found_level)
continue;
found_value = value;
found_level = level;
}
return found_value;
}
//---------------------------------------------------------------------------
// Process_GetConf
//---------------------------------------------------------------------------
_FX const WCHAR* Process_GetConf(PROCESS* proc, const WCHAR* setting)
{
return Process_GetConfEx(proc->box, proc->image_name, setting);
}
//---------------------------------------------------------------------------
// Process_GetConfEx_bool
//---------------------------------------------------------------------------
_FX BOOLEAN Process_GetConfEx_bool(BOX *box, const WCHAR *image_name, const WCHAR* setting, BOOLEAN def)
{
const WCHAR *value;
BOOLEAN retval;
Conf_AdjustUseCount(TRUE);
value = Process_GetConfEx(box, image_name, setting);
retval = def;
if (value) {
if (*value == 'y' || *value == 'Y')
retval = TRUE;
else if (*value == 'n' || *value == 'N')
retval = FALSE;
}
Conf_AdjustUseCount(FALSE);
return retval;
}
//---------------------------------------------------------------------------
// Process_GetConf_bool
//---------------------------------------------------------------------------
_FX BOOLEAN Process_GetConf_bool(PROCESS* proc, const WCHAR* setting, BOOLEAN def)
{
return Process_GetConfEx_bool(proc->box, proc->image_name, setting, def);
}
//---------------------------------------------------------------------------
// Process_GetPaths
//---------------------------------------------------------------------------
_FX BOOLEAN Process_GetPaths(
PROCESS *proc, LIST *list, const WCHAR *section_name, const WCHAR *setting_name, BOOLEAN AddStar)
{
ULONG index;
const WCHAR *value;
BOOLEAN ok = TRUE;
BOOLEAN closed = (_wcsnicmp(setting_name, Process_Closed, 6) == 0);
BOOLEAN closed_ipc = FALSE;
if (closed)
closed_ipc = (_wcsnicmp(setting_name + 6, L"Ipc", 3) == 0);
Conf_AdjustUseCount(TRUE);
for (index = 0; ; ++index) {
//
// get next configuration setting for this path list
//
value = Conf_Get(section_name, setting_name, index);
if (! value)
break;
if (closed && (*value == L'!')) {
// don't close paths for sbie components
if (closed_ipc && proc->image_sbie)
continue;
// for all other advance to the path and apply the block for all sandboxed images
if (proc->image_from_box && proc->always_close_for_boxed) {
value = wcschr(value, L',');
if (! value)
continue;
++value;
}
}
if (! Process_AddPath(
proc, list, setting_name, FALSE, value, AddStar)) {
ok = FALSE;
break;
}
}
Conf_AdjustUseCount(FALSE);
return ok;
}
//---------------------------------------------------------------------------
// Process_GetPaths2
//---------------------------------------------------------------------------
#ifndef USE_MATCH_PATH_EX
_FX BOOLEAN Process_GetPaths2(
PROCESS *proc, LIST *list, LIST *list2,
const WCHAR *setting_name, BOOLEAN AddStar)
{
LIST dummy_list;
PATTERN *pat;
const WCHAR *value;
ULONG len;
BOOLEAN is_open, is_closed;
//
// this function gets a list of settings, typically WriteXxxPath,
// and compares it to an already-populated list of settings, typically
// ClosedXxxPath, in order to discard duplicate settings. the intent
// is to keep the general rule that ClosedXxxPath settings override
// any other settings, including WriteXxxPath settings
//
List_Init(&dummy_list);
if (! Process_GetPaths(proc, &dummy_list, setting_name, AddStar))
return FALSE;
while (1) {
pat = List_Head(&dummy_list);
if (! pat)
break;
//
// get a setting from the list of potential settings,
// and discard suffix wildcards
//
value = Pattern_Source(pat);
for (len = wcslen(value); len && value[len - 1] == L'*'; --len)
;
if (! len)
is_closed = TRUE;
else {
Process_MatchPath(proc->pool, value, len,
NULL, list2, &is_open, &is_closed);
}
List_Remove(&dummy_list, pat);
if (is_closed)
Pattern_Free(pat);
else
List_Insert_After(list, NULL, pat);
}
return TRUE;
}
#endif
//---------------------------------------------------------------------------
// Process_GetTemplatePaths
//---------------------------------------------------------------------------
#ifdef USE_TEMPLATE_PATHS
BOOLEAN Process_GetTemplatePaths(PROCESS *proc, LIST *list, const WCHAR *setting_name)
{
BOOLEAN ok;
ok = Process_GetPaths(proc, list, L"TemplateDefaultPaths", setting_name, FALSE);
if (ok && proc->restrict_devices)
ok = Process_GetPaths(proc, list, L"TemplateSModPaths", setting_name, FALSE);
if (ok && proc->use_privacy_mode)
ok = Process_GetPaths(proc, list, L"TemplatePModPaths", setting_name, FALSE);
if (ok && proc->bAppCompartment)
ok = Process_GetPaths(proc, list, L"TemplateAppCPaths", setting_name, FALSE);
return ok;
}
#endif
//---------------------------------------------------------------------------
// Process_AddPath
//---------------------------------------------------------------------------
_FX BOOLEAN Process_AddPath(
PROCESS *proc, LIST *list, const WCHAR *setting_name,
BOOLEAN AddFirst, const WCHAR *value, BOOLEAN AddStar)
{
WCHAR *tmp;
ULONG len;
BOOLEAN RemoveBackslashes = FALSE;
BOOLEAN CheckReparse = FALSE;
BOOLEAN AddDrives = FALSE;
BOOLEAN Reparsed;
BOOLEAN ok;
ULONG Level;
//
// if this is a file/pipe/key setting, remove duplicate backslashes
// if this is a file setting, also check the path for reparse points
//
if (setting_name) {
const WCHAR *setting_name_ptr = setting_name;
if (_wcsnicmp(setting_name, Process_Normal, 6) == 0 ||
_wcsnicmp(setting_name, Process_Closed, 6) == 0)
setting_name_ptr = setting_name + 6;
else if (_wcsnicmp(setting_name, Process_Write, 5) == 0)
setting_name_ptr = setting_name + 5;
else if (_wcsnicmp(setting_name, Process_Read, 4) == 0 ||
_wcsnicmp(setting_name, Process_Open, 4) == 0)
setting_name_ptr = setting_name + 4;
else
setting_name_ptr = NULL;
if (setting_name_ptr) {
if (_wcsnicmp(setting_name_ptr, L"Key", 3) == 0
|| _wcsnicmp(setting_name_ptr, L"Conf", 4) == 0) {
if (AddStar) {
RemoveBackslashes = TRUE;
}
} else if (_wcsnicmp(setting_name_ptr, L"File", 4) == 0
|| _wcsnicmp(setting_name_ptr, L"Pipe", 4) == 0) {
if (AddStar) {
RemoveBackslashes = TRUE;
CheckReparse = TRUE;
}
AddDrives = TRUE;
}
}
}
value = Process_MatchImageAndGetValue(proc->box, value, proc->image_name, &Level);
if (!value)
return TRUE;
//
// image name matches, or was not specified. next, expand
// the configuration path setting.
//
// note that if we're removing backslashes (i.e. for file/pipe/key)
// and the setting value begins with a pipe character, then we
// do not append a suffix wildcard character
//
if (RemoveBackslashes && *value == L'|') {
++value;
AddStar = FALSE;
}
//
// add the value as requested
//
ok = Process_AddPath_2(proc, list, value, setting_name,
AddFirst, AddStar, RemoveBackslashes, CheckReparse, &Reparsed, Level);
if (ok && CheckReparse && Reparsed) {
//
// If the path was reparsed, add also the original path to the list
//
ok = Process_AddPath_2(proc, list, value, setting_name,
AddFirst, AddStar, RemoveBackslashes, FALSE, NULL, Level);
}
//
// if this is a file setting (AddDrives == TRUE) and starts with
// *: or ?: then manually replace with each of the 26 possible drives
//
if (ok && AddDrives && (value[0] == L'?' || value[0] == L'*')
&& (value[1] == L':')) {
tmp = Mem_AllocString(proc->pool, value);
if (! tmp)
return FALSE;
for (len = L'A'; (len <= L'Z') && ok; ++len) {
*tmp = (WCHAR)len;
ok = Process_AddPath_2(proc, list, tmp, setting_name,
AddFirst, AddStar, RemoveBackslashes, CheckReparse, &Reparsed, Level);
if (ok && CheckReparse && Reparsed) {
ok = Process_AddPath_2(proc, list, tmp, setting_name,
AddFirst, AddStar, RemoveBackslashes, FALSE, NULL, Level);
}
}
Mem_FreeString(tmp);
}
return ok;
}
//---------------------------------------------------------------------------
// Process_AddPath_2
//---------------------------------------------------------------------------
_FX BOOLEAN Process_AddPath_2(
PROCESS *proc, LIST *list, const WCHAR *value, const WCHAR *setting_name,
BOOLEAN AddFirst, BOOLEAN AddStar,
BOOLEAN RemoveBackslashes, BOOLEAN CheckReparse, BOOLEAN* Reparsed, ULONG Level)
{
PATTERN *pat;
WCHAR *expand, *tmp;
ULONG len;
//
// expand any variables in the value
//
expand = Conf_Expand(proc->box->expand_args, value, setting_name);
if (! expand)
return FALSE;
//
// duplicate the expanded string as a temp string, in case we
// need to add a star at the end
//
len = (wcslen(expand) + 1) * sizeof(WCHAR);
if (AddStar) {
if (wcschr(expand, L'*') == NULL)
len += sizeof(WCHAR);
else
AddStar = FALSE;
}
tmp = Mem_Alloc(proc->pool, len);
if (! tmp) {
Mem_FreeString(expand);
return FALSE;
}
//
// copy the expanded path string into the temporary string
// optionally, removing backslashes
// optionally, adding a star at the end
//
if (RemoveBackslashes) {
WCHAR *src_ptr = expand;
WCHAR *dst_ptr = tmp;
while (*src_ptr) {
if (src_ptr[0] == L'\\' && src_ptr[1] == L'\\') {
++src_ptr;
continue;
}
*dst_ptr = *src_ptr;
++src_ptr;
++dst_ptr;
}
*dst_ptr = L'\0';
} else
wcscpy(tmp, expand);
if (AddStar)
wcscat(tmp, L"*");
//
// check for reparse points
//
if (CheckReparse) {
WCHAR *tmp2 = File_TranslateReparsePoints(tmp, proc->pool);
if (tmp2) {
*Reparsed = _wcsicmp(tmp, tmp2) != 0; // check if its actually different
Mem_FreeString(tmp);
tmp = tmp2;
} else {
*Reparsed = FALSE; // nothing found
}
}
//
// add the pattern
//
pat = Pattern_Create(proc->pool, tmp, TRUE, Level);
if (pat) {
if (AddFirst)
List_Insert_Before(list, NULL, pat);
else
List_Insert_After(list, NULL, pat);
}
Mem_FreeString(tmp);
Mem_FreeString(expand);
if (! pat)
return FALSE;
return TRUE;
}
//---------------------------------------------------------------------------
// Process_MatchPath
//---------------------------------------------------------------------------
_FX const WCHAR *Process_MatchPath(
POOL *pool, const WCHAR *path, ULONG path_len,
LIST *open_list, LIST *closed_list,
BOOLEAN *is_open, BOOLEAN *is_closed)
{
PATTERN *pat;
WCHAR *path_lwr;
ULONG path_lwr_len;
const WCHAR *patsrc = NULL;
*is_open = FALSE;
*is_closed = FALSE;
//
// scan paths list. if the path to match does not already end with
// a backslash character, we will check it twice, second time with
// a suffixing backslash. this will make sure we match C:\X even
// even when {Open,Closed}XxxPath=C:\X\ (with a backslash suffix)
//
path_lwr_len = (path_len + 4) * sizeof(WCHAR);
path_lwr = Mem_Alloc(pool, path_lwr_len);
if (! path_lwr)
return NULL;
wmemcpy(path_lwr, path, path_len);
path_lwr[path_len] = L'\0';
path_len = wcslen(path_lwr);
if (! path_len) {
Mem_Free(path_lwr, path_lwr_len);
return NULL;
}
path_lwr[path_len] = L'\0';
path_lwr[path_len + 1] = L'\0';
_wcslwr(path_lwr);
if (closed_list) {
pat = List_Head(closed_list);
while (pat) {
if (Pattern_Match(pat, path_lwr, path_len)) {
*is_closed = TRUE;
patsrc = Pattern_Source(pat);
break;
}
if (path_lwr[path_len - 1] != L'\\') {
path_lwr[path_len] = L'\\';
if (Pattern_Match(pat, path_lwr, path_len + 1)) {
path_lwr[path_len] = L'\0';
*is_closed = TRUE;
patsrc = Pattern_Source(pat);
break;
}
path_lwr[path_len] = L'\0';
}
pat = List_Next(pat);
}
}
if (open_list && (! *is_closed)) {
pat = List_Head(open_list);
while (pat) {
if (Pattern_Match(pat, path_lwr, path_len)) {
*is_open = TRUE;
patsrc = Pattern_Source(pat);
break;
}
if (path_lwr[path_len - 1] != L'\\') {
path_lwr[path_len] = L'\\';
if (Pattern_Match(pat, path_lwr, path_len + 1)) {
path_lwr[path_len] = L'\0';
*is_open = TRUE;
patsrc = Pattern_Source(pat);
break;
}
path_lwr[path_len] = L'\0';
}
pat = List_Next(pat);
}
}
Mem_Free(path_lwr, path_lwr_len);
return patsrc;
}
//---------------------------------------------------------------------------
// Process_MatchPathEx
//---------------------------------------------------------------------------
#ifdef USE_MATCH_PATH_EX
_FX ULONG Process_MatchPathEx(
PROCESS *proc, const WCHAR *path, ULONG path_len, WCHAR path_code,
LIST *normal_list,
LIST *open_list, LIST *closed_list,
LIST *read_list, LIST *write_list,
const WCHAR** patsrc)
{
PATTERN *pat;
WCHAR *path_lwr;
ULONG path_lwr_len;
int match_len;
ULONG level;
ULONG flags;
USHORT wildc;
ULONG mp_flags;
path_lwr_len = (path_len + 4) * sizeof(WCHAR);
path_lwr = Mem_Alloc(proc->pool, path_lwr_len);
if (! path_lwr)
return 0;
wmemcpy(path_lwr, path, path_len);
path_lwr[path_len] = L'\0';
path_len = wcslen(path_lwr);
if (! path_len) {
Mem_Free(path_lwr, path_lwr_len);
return 0;
}
path_lwr[path_len] = L'\0';
path_lwr[path_len + 1] = L'\0';
_wcslwr(path_lwr);
//
// Rule priorities are implemented based on their specificity and match level with the process.
// The specificity describes how well a pattern matches a given path,
// i.e. how many characters of the path it matches, disregarding the last wild card.
// The process match level describes in which way a rule applies to a given process:
// 0 - exact match, eg. ...Path=program.exe,...
// 1 - match by negation, eg. ...Path=!program.exe,...
// 2 - match all, eg. ...Path=*,...
// 3 - global default, eg. ...Path=...
// Rules with the most exact matches overrule the more generic once.
// The match level overrules the specificity.
//
// A rule with less wildcards will overrule one with more
//
// If a rule ends with an * it is not exact and will be overruled by an exact rule
//
// Adding UseRuleSpecificity=n disables this behaviour and reverts to the old classical one
//
//
// set default behaviour
//
level = 3; // 3 - global default - lower is better, 3 is max value
flags = 0;
wildc = -1; // lower is better
match_len = 0;
if (path_code == L'n' && proc->file_block_network_files) {
//
// handle network share access preset
//
mp_flags = TRUE_PATH_CLOSED_FLAG | COPY_PATH_CLOSED_FLAG;
}
//else if (!proc->use_privacy_mode || path_code == L'i') {
else {
//
// in normal sandbox mode we have read access to all locations unless restricted,