-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathKauthHandler.cpp
913 lines (794 loc) · 25.9 KB
/
KauthHandler.cpp
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
#include <kern/debug.h>
#include <sys/kauth.h>
#include <sys/proc.h>
#include <libkern/OSAtomic.h>
#include <kern/assert.h>
#include <stdatomic.h>
#include "PrjFSCommon.h"
#include "VirtualizationRoots.hpp"
#include "VnodeUtilities.hpp"
#include "KauthHandler.hpp"
#include "KextLog.hpp"
#include "Message.h"
#include "Locks.hpp"
#include "PrjFSProviderUserClient.hpp"
// Function prototypes
static int HandleVnodeOperation(
kauth_cred_t credential,
void* idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3);
static int HandleFileOpOperation(
kauth_cred_t credential,
void* idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3);
static int GetPid(vfs_context_t context);
static uint32_t ReadVNodeFileFlags(vnode_t vn, vfs_context_t context);
static inline bool FileFlagsBitIsSet(uint32_t fileFlags, uint32_t bit);
static inline bool FileIsFlaggedAsInRoot(vnode_t vnode, vfs_context_t context);
static inline bool ActionBitIsSet(kauth_action_t action, kauth_action_t mask);
static bool IsFileSystemCrawler(char* procname);
static const char* GetRelativePath(const char* path, const char* root);
static void Sleep(int seconds, void* channel);
static bool TrySendRequestAndWaitForResponse(
const VirtualizationRoot* root,
MessageType messageType,
const vnode_t vnode,
const FsidInode& vnodeFsidInode,
const char* vnodePath,
int pid,
const char* procname,
int* kauthResult,
int* kauthError);
static void AbortAllOutstandingEvents();
static bool ShouldIgnoreVnodeType(vtype vnodeType, vnode_t vnode);
static bool ShouldHandleVnodeOpEvent(
// In params:
vfs_context_t context,
const vnode_t vnode,
kauth_action_t action,
// Out params:
VirtualizationRoot** root,
vtype* vnodeType,
uint32_t* vnodeFileFlags,
FsidInode* vnodeFsidInode,
int* pid,
char procname[MAXCOMLEN + 1],
int* kauthResult);
static bool ShouldHandleFileOpEvent(
// In params:
vfs_context_t context,
const vnode_t vnode,
kauth_action_t action,
// Out params:
VirtualizationRoot** root,
FsidInode* vnodeFsidInode,
int* pid);
// Structs
typedef struct OutstandingMessage
{
MessageHeader request;
MessageType response;
bool receivedResponse;
LIST_ENTRY(OutstandingMessage) _list_privates;
} OutstandingMessage;
// State
static kauth_listener_t s_vnodeListener = nullptr;
static kauth_listener_t s_fileopListener = nullptr;
static LIST_HEAD(OutstandingMessage_Head, OutstandingMessage) s_outstandingMessages = LIST_HEAD_INITIALIZER(OutstandingMessage_Head);
static Mutex s_outstandingMessagesMutex = {};
static volatile int s_nextMessageId;
static atomic_int s_numActiveKauthEvents;
static volatile bool s_isShuttingDown;
// Public functions
kern_return_t KauthHandler_Init()
{
if (nullptr != s_vnodeListener)
{
goto CleanupAndFail;
}
LIST_INIT(&s_outstandingMessages);
s_nextMessageId = 1;
s_isShuttingDown = false;
s_outstandingMessagesMutex = Mutex_Alloc();
if (!Mutex_IsValid(s_outstandingMessagesMutex))
{
goto CleanupAndFail;
}
if (VirtualizationRoots_Init())
{
goto CleanupAndFail;
}
s_vnodeListener = kauth_listen_scope(KAUTH_SCOPE_VNODE, HandleVnodeOperation, nullptr);
if (nullptr == s_vnodeListener)
{
goto CleanupAndFail;
}
s_fileopListener = kauth_listen_scope(KAUTH_SCOPE_FILEOP, HandleFileOpOperation, nullptr);
if (nullptr == s_fileopListener)
{
goto CleanupAndFail;
}
return KERN_SUCCESS;
CleanupAndFail:
KauthHandler_Cleanup();
return KERN_FAILURE;
}
kern_return_t KauthHandler_Cleanup()
{
kern_return_t result = KERN_SUCCESS;
// First, stop new listener callback calls
if (nullptr != s_vnodeListener)
{
kauth_unlisten_scope(s_vnodeListener);
s_vnodeListener = nullptr;
}
else
{
result = KERN_FAILURE;
}
if (nullptr != s_fileopListener)
{
kauth_unlisten_scope(s_fileopListener);
s_fileopListener = nullptr;
}
else
{
result = KERN_FAILURE;
}
// Then, ensure there are no more callbacks in flight.
AbortAllOutstandingEvents();
if (VirtualizationRoots_Cleanup())
{
result = KERN_FAILURE;
}
if (Mutex_IsValid(s_outstandingMessagesMutex))
{
Mutex_FreeMemory(&s_outstandingMessagesMutex);
}
else
{
result = KERN_FAILURE;
}
return result;
}
void KauthHandler_HandleKernelMessageResponse(uint64_t messageId, MessageType responseType)
{
switch (responseType)
{
case MessageType_Response_Success:
case MessageType_Response_Fail:
{
Mutex_Acquire(s_outstandingMessagesMutex);
{
OutstandingMessage* outstandingMessage;
LIST_FOREACH(outstandingMessage, &s_outstandingMessages, _list_privates)
{
if (outstandingMessage->request.messageId == messageId)
{
// Save the response for the blocked thread.
outstandingMessage->response = responseType;
outstandingMessage->receivedResponse = true;
wakeup(outstandingMessage);
break;
}
}
}
Mutex_Release(s_outstandingMessagesMutex);
break;
}
// The follow are not valid responses to kernel messages
case MessageType_Invalid:
case MessageType_UtoK_StartVirtualizationInstance:
case MessageType_UtoK_StopVirtualizationInstance:
case MessageType_KtoU_EnumerateDirectory:
case MessageType_KtoU_RecursivelyEnumerateDirectory:
case MessageType_KtoU_HydrateFile:
case MessageType_KtoU_NotifyFileModified:
case MessageType_KtoU_NotifyFilePreDelete:
case MessageType_KtoU_NotifyDirectoryPreDelete:
case MessageType_KtoU_NotifyFileCreated:
case MessageType_KtoU_NotifyFileRenamed:
case MessageType_KtoU_NotifyDirectoryRenamed:
case MessageType_KtoU_NotifyFileHardLinkCreated:
KextLog_Error("KauthHandler_HandleKernelMessageResponse: Unexpected responseType: %d", responseType);
break;
}
return;
}
// Private functions
static int HandleVnodeOperation(
kauth_cred_t credential,
void* idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3)
{
atomic_fetch_add(&s_numActiveKauthEvents, 1);
vfs_context_t context = reinterpret_cast<vfs_context_t>(arg0);
vnode_t currentVnode = reinterpret_cast<vnode_t>(arg1);
// arg2 is the (vnode_t) parent vnode
int* kauthError = reinterpret_cast<int*>(arg3);
int kauthResult = KAUTH_RESULT_DEFER;
const char* vnodePath = nullptr;
char vnodePathBuffer[PrjFSMaxPath];
int vnodePathLength = PrjFSMaxPath;
VirtualizationRoot* root = nullptr;
vtype vnodeType;
uint32_t currentVnodeFileFlags;
FsidInode vnodeFsidInode;
int pid;
char procname[MAXCOMLEN + 1];
bool isDeleteAction = false;
bool isDirectory = false;
// TODO(Mac): Issue #271 - Reduce reliance on vn_getpath
// Call vn_getpath first when the cache is hottest to increase the chances
// of successfully getting the path
if (0 == vn_getpath(currentVnode, vnodePathBuffer, &vnodePathLength))
{
vnodePath = vnodePathBuffer;
}
if (!ShouldHandleVnodeOpEvent(
context,
currentVnode,
action,
&root,
&vnodeType,
¤tVnodeFileFlags,
&vnodeFsidInode,
&pid,
procname,
&kauthResult))
{
goto CleanupAndReturn;
}
isDeleteAction = ActionBitIsSet(action, KAUTH_VNODE_DELETE);
isDirectory = VDIR == vnodeType;
if (isDeleteAction)
{
if (!TrySendRequestAndWaitForResponse(
root,
isDirectory ?
MessageType_KtoU_NotifyDirectoryPreDelete :
MessageType_KtoU_NotifyFilePreDelete,
currentVnode,
vnodeFsidInode,
vnodePath,
pid,
procname,
&kauthResult,
kauthError))
{
goto CleanupAndReturn;
}
}
if (isDirectory)
{
if (ActionBitIsSet(
action,
KAUTH_VNODE_LIST_DIRECTORY |
KAUTH_VNODE_SEARCH |
KAUTH_VNODE_READ_SECURITY |
KAUTH_VNODE_READ_ATTRIBUTES |
KAUTH_VNODE_READ_EXTATTRIBUTES |
KAUTH_VNODE_DELETE))
{
// Recursively expand directory on delete to ensure child placeholders are created before rename operations
if (isDeleteAction || FileFlagsBitIsSet(currentVnodeFileFlags, FileFlags_IsEmpty))
{
if (!TrySendRequestAndWaitForResponse(
root,
isDeleteAction ?
MessageType_KtoU_RecursivelyEnumerateDirectory :
MessageType_KtoU_EnumerateDirectory,
currentVnode,
vnodeFsidInode,
vnodePath,
pid,
procname,
&kauthResult,
kauthError))
{
goto CleanupAndReturn;
}
}
}
}
else
{
if (ActionBitIsSet(
action,
KAUTH_VNODE_READ_ATTRIBUTES |
KAUTH_VNODE_WRITE_ATTRIBUTES |
KAUTH_VNODE_READ_EXTATTRIBUTES |
KAUTH_VNODE_WRITE_EXTATTRIBUTES |
KAUTH_VNODE_READ_DATA |
KAUTH_VNODE_WRITE_DATA |
KAUTH_VNODE_EXECUTE |
KAUTH_VNODE_DELETE)) // Hydrate on delete to ensure files are hydrated before rename operations
{
if (FileFlagsBitIsSet(currentVnodeFileFlags, FileFlags_IsEmpty))
{
if (!TrySendRequestAndWaitForResponse(
root,
MessageType_KtoU_HydrateFile,
currentVnode,
vnodeFsidInode,
vnodePath,
pid,
procname,
&kauthResult,
kauthError))
{
goto CleanupAndReturn;
}
}
}
}
CleanupAndReturn:
atomic_fetch_sub(&s_numActiveKauthEvents, 1);
return kauthResult;
}
// Note: a fileop listener MUST NOT return an error, or it will result in a kernel panic.
// Fileop events are informational only.
static int HandleFileOpOperation(
kauth_cred_t credential,
void* idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3)
{
atomic_fetch_add(&s_numActiveKauthEvents, 1);
vfs_context_t context = vfs_context_create(NULL);
vnode_t currentVnodeFromPath = NULLVP;
if (KAUTH_FILEOP_RENAME == action ||
KAUTH_FILEOP_LINK == action)
{
// arg0 is the (const char *) fromPath (or the file being linked to)
const char* newPath = (const char*)arg1;
// TODO(Mac): We need to handle failures to lookup the vnode. If we fail to lookup the vnode
// it's possible that we'll miss notifications
errno_t toErr = vnode_lookup(newPath, 0 /* flags */, ¤tVnodeFromPath, context);
if (0 != toErr)
{
goto CleanupAndReturn;
}
VirtualizationRoot* root = nullptr;
FsidInode vnodeFsidInode;
int pid;
if (!ShouldHandleFileOpEvent(
context,
currentVnodeFromPath,
action,
&root,
&vnodeFsidInode,
&pid))
{
goto CleanupAndReturn;
}
char procname[MAXCOMLEN + 1];
proc_name(pid, procname, MAXCOMLEN + 1);
MessageType messageType;
if (KAUTH_FILEOP_RENAME == action)
{
messageType = vnode_isdir(currentVnodeFromPath) ? MessageType_KtoU_NotifyDirectoryRenamed : MessageType_KtoU_NotifyFileRenamed;
}
else
{
messageType = MessageType_KtoU_NotifyFileHardLinkCreated;
}
int kauthResult;
int kauthError;
if (!TrySendRequestAndWaitForResponse(
root,
messageType,
currentVnodeFromPath,
vnodeFsidInode,
newPath,
pid,
procname,
&kauthResult,
&kauthError))
{
goto CleanupAndReturn;
}
}
else if (KAUTH_FILEOP_CLOSE == action)
{
vnode_t currentVnode = reinterpret_cast<vnode_t>(arg0);
const char* path = (const char*)arg1;
int closeFlags = static_cast<int>(arg2);
if (vnode_isdir(currentVnode))
{
goto CleanupAndReturn;
}
bool fileFlaggedInRoot = FileIsFlaggedAsInRoot(currentVnode, context);
if (fileFlaggedInRoot && KAUTH_FILEOP_CLOSE_MODIFIED != closeFlags)
{
goto CleanupAndReturn;
}
VirtualizationRoot* root = nullptr;
FsidInode vnodeFsidInode;
int pid;
if (!ShouldHandleFileOpEvent(
context,
currentVnode,
action,
&root,
&vnodeFsidInode,
&pid))
{
goto CleanupAndReturn;
}
char procname[MAXCOMLEN + 1];
proc_name(pid, procname, MAXCOMLEN + 1);
if (fileFlaggedInRoot)
{
int kauthResult;
int kauthError;
if (!TrySendRequestAndWaitForResponse(
root,
MessageType_KtoU_NotifyFileModified,
currentVnode,
vnodeFsidInode,
path,
pid,
procname,
&kauthResult,
&kauthError))
{
goto CleanupAndReturn;
}
}
else
{
int kauthResult;
int kauthError;
if (!TrySendRequestAndWaitForResponse(
root,
MessageType_KtoU_NotifyFileCreated,
currentVnode,
vnodeFsidInode,
path,
pid,
procname,
&kauthResult,
&kauthError))
{
goto CleanupAndReturn;
}
}
}
CleanupAndReturn:
if (NULLVP != currentVnodeFromPath)
{
vnode_put(currentVnodeFromPath);
}
vfs_context_rele(context);
atomic_fetch_sub(&s_numActiveKauthEvents, 1);
// We must always return DEFER from a fileop listener. The kernel does not allow any other
// result and will panic if we return anything else.
return KAUTH_RESULT_DEFER;
}
static bool ShouldHandleVnodeOpEvent(
// In params:
vfs_context_t context,
const vnode_t vnode,
kauth_action_t action,
// Out params:
VirtualizationRoot** root,
vtype* vnodeType,
uint32_t* vnodeFileFlags,
FsidInode* vnodeFsidInode,
int* pid,
char procname[MAXCOMLEN + 1],
int* kauthResult)
{
*root = nullptr;
*kauthResult = KAUTH_RESULT_DEFER;
if (!VirtualizationRoot_VnodeIsOnAllowedFilesystem(vnode))
{
*kauthResult = KAUTH_RESULT_DEFER;
return false;
}
*vnodeType = vnode_vtype(vnode);
if (ShouldIgnoreVnodeType(*vnodeType, vnode))
{
*kauthResult = KAUTH_RESULT_DEFER;
return false;
}
*vnodeFileFlags = ReadVNodeFileFlags(vnode, context);
if (!FileFlagsBitIsSet(*vnodeFileFlags, FileFlags_IsInVirtualizationRoot))
{
// This vnode is not part of ANY virtualization root, so exit now before doing any more work.
// This gives us a cheap way to avoid adding overhead to IO outside of a virtualization root.
*kauthResult = KAUTH_RESULT_DEFER;
return false;
}
*pid = GetPid(context);
proc_name(*pid, procname, MAXCOMLEN + 1);
if (FileFlagsBitIsSet(*vnodeFileFlags, FileFlags_IsEmpty))
{
// This vnode is not yet hydrated, so do not allow a file system crawler to force hydration.
// Once a vnode is hydrated, it's fine to allow crawlers to access those contents.
if (IsFileSystemCrawler(procname))
{
// We must DENY file system crawlers rather than DEFER.
// If we allow the crawler's access to succeed without hydrating, the kauth result will be cached and we won't
// get called again, so we lose the opportunity to hydrate the file/directory and it will appear as though
// it is missing its contents.
*kauthResult = KAUTH_RESULT_DENY;
return false;
}
}
*vnodeFsidInode = Vnode_GetFsidAndInode(vnode, context);
*root = VirtualizationRoots_FindForVnode(vnode, *vnodeFsidInode);
if (nullptr == *root)
{
KextLog_FileNote(vnode, "No virtualization root found for file with set flag.");
*kauthResult = KAUTH_RESULT_DEFER;
return false;
}
else if (nullptr == (*root)->providerUserClient)
{
// There is no registered provider for this root
// TODO(Mac): Protect files in the worktree from modification (and prevent
// the creation of new files) when the provider is offline
*kauthResult = KAUTH_RESULT_DEFER;
return false;
}
// If the calling process is the provider, we must exit right away to avoid deadlocks
if (*pid == (*root)->providerPid)
{
*kauthResult = KAUTH_RESULT_DEFER;
return false;
}
return true;
}
static bool ShouldHandleFileOpEvent(
// In params:
vfs_context_t context,
const vnode_t vnode,
kauth_action_t action,
// Out params:
VirtualizationRoot** root,
FsidInode* vnodeFsidInode,
int* pid)
{
vtype vnodeType = vnode_vtype(vnode);
if (ShouldIgnoreVnodeType(vnodeType, vnode))
{
return false;
}
*vnodeFsidInode = Vnode_GetFsidAndInode(vnode, context);
*root = VirtualizationRoots_FindForVnode(vnode, *vnodeFsidInode);
if (nullptr == *root)
{
return false;
}
else if (nullptr == (*root)->providerUserClient)
{
// There is no registered provider for this root
return false;
}
// If the calling process is the provider, we must exit right away to avoid deadlocks
*pid = GetPid(context);
if (*pid == (*root)->providerPid)
{
return false;
}
return true;
}
static bool TrySendRequestAndWaitForResponse(
const VirtualizationRoot* root,
MessageType messageType,
const vnode_t vnode,
const FsidInode& vnodeFsidInode,
const char* vnodePath,
int pid,
const char* procname,
int* kauthResult,
int* kauthError)
{
bool result = false;
OutstandingMessage message;
message.receivedResponse = false;
if (nullptr == vnodePath)
{
// Default error code is EACCES. See errno.h for more codes.
*kauthError = EAGAIN;
*kauthResult = KAUTH_RESULT_DENY;
return false;
}
const char* relativePath = GetRelativePath(vnodePath, root->path);
int nextMessageId = OSIncrementAtomic(&s_nextMessageId);
Message messageSpec = {};
Message_Init(
&messageSpec,
&(message.request),
nextMessageId,
messageType,
vnodeFsidInode,
pid,
procname,
relativePath);
bool isShuttingDown = false;
Mutex_Acquire(s_outstandingMessagesMutex);
{
// Only read s_isShuttingDown once so we either insert & send message, or neither.
isShuttingDown = s_isShuttingDown;
if (!isShuttingDown)
{
LIST_INSERT_HEAD(&s_outstandingMessages, &message, _list_privates);
}
}
Mutex_Release(s_outstandingMessagesMutex);
// TODO(Mac): Should we pass in the root directly, rather than root->index?
// The index seems more like a private implementation detail.
if (!isShuttingDown && 0 != ActiveProvider_SendMessage(root->index, messageSpec))
{
// TODO: appropriately handle unresponsive providers
*kauthResult = KAUTH_RESULT_DEFER;
goto CleanupAndReturn;
}
while (!message.receivedResponse &&
!s_isShuttingDown)
{
Sleep(5, &message);
}
if (s_isShuttingDown)
{
*kauthResult = KAUTH_RESULT_DENY;
goto CleanupAndReturn;
}
if (MessageType_Response_Success == message.response)
{
*kauthResult = KAUTH_RESULT_DEFER;
result = true;
goto CleanupAndReturn;
}
else
{
// Default error code is EACCES. See errno.h for more codes.
*kauthError = EAGAIN;
*kauthResult = KAUTH_RESULT_DENY;
goto CleanupAndReturn;
}
CleanupAndReturn:
Mutex_Acquire(s_outstandingMessagesMutex);
{
LIST_REMOVE(&message, _list_privates);
}
Mutex_Release(s_outstandingMessagesMutex);
return result;
}
static void AbortAllOutstandingEvents()
{
// Wake up all sleeping threads so they can see that that we're shutting down and return an error
Mutex_Acquire(s_outstandingMessagesMutex);
{
s_isShuttingDown = true;
OutstandingMessage* outstandingMessage;
LIST_FOREACH(outstandingMessage, &s_outstandingMessages, _list_privates)
{
wakeup(outstandingMessage);
}
}
Mutex_Release(s_outstandingMessagesMutex);
// ... and wait until all kauth events have noticed and returned.
// Always sleeping at least once reduces the likelihood of a race condition
// between kauth_unlisten_scope and the s_numActiveKauthEvents increment at
// the start of the callback.
// This race condition and the inability to work around it is a longstanding
// bug in the xnu kernel - see comment block in RemoveListener() function of
// the KauthORama sample code:
// https://developer.apple.com/library/archive/samplecode/KauthORama/Listings/KauthORama_c.html#//apple_ref/doc/uid/DTS10003633-KauthORama_c-DontLinkElementID_3
do
{
Sleep(1, NULL);
} while (atomic_load(&s_numActiveKauthEvents) > 0);
}
static void Sleep(int seconds, void* channel)
{
struct timespec timeout;
timeout.tv_sec = seconds;
timeout.tv_nsec = 0;
msleep(channel, nullptr, PUSER, "io.gvfs.PrjFSKext.Sleep", &timeout);
}
static int GetPid(vfs_context_t context)
{
proc_t callingProcess = vfs_context_proc(context);
return proc_pid(callingProcess);
}
static errno_t GetVNodeAttributes(vnode_t vn, vfs_context_t context, struct vnode_attr* attrs)
{
VATTR_INIT(attrs);
VATTR_WANTED(attrs, va_flags);
return vnode_getattr(vn, attrs, context);
}
static uint32_t ReadVNodeFileFlags(vnode_t vn, vfs_context_t context)
{
struct vnode_attr attributes = {};
errno_t err = GetVNodeAttributes(vn, context, &attributes);
// TODO: May fail on some file system types? Perhaps we should early-out depending on mount point anyway.
assert(0 == err);
assert(VATTR_IS_SUPPORTED(&attributes, va_flags));
return attributes.va_flags;
}
static inline bool FileFlagsBitIsSet(uint32_t fileFlags, uint32_t bit)
{
// Note: if multiple bits are set in 'bit', this will return true if ANY are set in fileFlags
return 0 != (fileFlags & bit);
}
static inline bool FileIsFlaggedAsInRoot(vnode_t vnode, vfs_context_t context)
{
uint32_t vnodeFileFlags = ReadVNodeFileFlags(vnode, context);
return FileFlagsBitIsSet(vnodeFileFlags, FileFlags_IsInVirtualizationRoot);
}
static inline bool ActionBitIsSet(kauth_action_t action, kauth_action_t mask)
{
return action & mask;
}
static bool IsFileSystemCrawler(char* procname)
{
// These process will crawl the file system and force a full hydration
if (!strcmp(procname, "mds") ||
!strcmp(procname, "mdworker") ||
!strcmp(procname, "mds_stores") ||
!strcmp(procname, "fseventsd") ||
!strcmp(procname, "Spotlight"))
{
return true;
}
return false;
}
static const char* GetRelativePath(const char* path, const char* root)
{
assert(strlen(path) >= strlen(root));
const char* relativePath = path + strlen(root);
if (relativePath[0] == '/')
{
relativePath++;
}
return relativePath;
}
static bool ShouldIgnoreVnodeType(vtype vnodeType, vnode_t vnode)
{
switch (vnodeType)
{
case VNON:
case VBLK:
case VCHR:
case VSOCK:
case VFIFO:
case VBAD:
return true;
case VREG:
case VDIR:
case VLNK:
return false;
case VSTR:
case VCPLX:
{
char vnodePath[PrjFSMaxPath];
int vnodePathLength = PrjFSMaxPath;
vn_getpath(vnode, vnodePath, &vnodePathLength);
KextLog_Info("vnode with type %s encountered, path %s", vnodeType == VSTR ? "VSTR" : "VCPLX", vnodePath);
return false;
}
default:
KextLog_Info("vnode with unknown type %d encountered", vnodeType);
return false;
}
}