/
VFSHooks.cpp
2959 lines (2308 loc) · 104 KB
/
VFSHooks.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
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
//
// VFSHooks.cpp
// VFSFilter0
//
// Created by slava on 3/06/2015.
// Copyright (c) 2015 Slava Imameev. All rights reserved.
//
#ifdef __cplusplus
extern "C" {
#endif
#include <vfs/vfs_support.h>
#include <sys/ubc.h>
#include <sys/buf.h>
#include <sys/vm.h>
#ifdef __cplusplus
}
#endif
#include "VFSHooks.h"
#include "VmPmap.h"
#include "VFSFilter0UserClient.h"
#include "Kauth.h"
#include "RecursionEngine.h"
#include <sys/fcntl.h>
#include "VNode.h"
#include "VNodeHook.h"
#include "VersionDependent.h"
#include "ApplicationsData.h"
//--------------------------------------------------------------------
static bool gSynchroniseWithOriginal = true;
static bool gRedirectionIsNullAndVoid = false; // TEST
//--------------------------------------------------------------------
errno_t
QvrReadInCacheFromBackingFile(
__in vnode_t vnode,
__in vnode_t vnodeIO,
__in off_t offset,
__in size_t size
);
//--------------------------------------------------------------------
IOReturn
VFSHookInit()
{
return kIOReturnSuccess;
}
//--------------------------------------------------------------------
void
VFSHookRelease()
{
}
//--------------------------------------------------------------------
#define SHADOW_PREFIX ".QS_"
errno_t
QvrConvertToShadowCopyPath(
__in const char* path,
__out char** _shadowPath,
__out vm_size_t* _shadowPathSize
)
/*
converts a path by adding SHADOW_PREFIX to a file name, e.g.
/a/b/c/file.docxx -> /a/b/c/.QS_file.docxx
OR
name.docxx -> .QS_name.docxx
*/
{
size_t pathLen;
size_t shadowPathLen;
size_t prefixLen;
vm_size_t shadowPathSize;
char* shadowPath;
pathLen = strlen( path );
prefixLen = strlen( SHADOW_PREFIX );
shadowPathLen = pathLen + prefixLen;
shadowPathSize = shadowPathLen + sizeof('\0');
shadowPath = (char*)IOMalloc( shadowPathSize );
assert( shadowPath );
if( ! shadowPath )
return ENOMEM;
assert( pathLen < shadowPathLen && shadowPathLen < shadowPathSize );
memcpy( shadowPath, path, pathLen + sizeof('\0'));
//
// now move file name right to prefixLen positions
// and squeeze the prefix
//
int pos = pathLen;
while( pos != (-1) && shadowPath[ pos ] != '/' ){
pos -= 1;
}
//
// pos points to the most right '/' or one position in front of string
// if there is no slashes in the path, i.e. a file name was provided instead
// the fully qualified path
//
pos += 1;
if( 0x0 == strncasecmp( &shadowPath[ pos ], SHADOW_PREFIX, prefixLen ) ){
//
// this is already a shadow path/name
//
goto __exit;
}
//
// move the file name to the right, do not forget about terminating zero
//
memmove( &shadowPath[ pos+prefixLen ], &shadowPath[ pos ], (pathLen + sizeof('\0'))- pos);
//
// squeeze in the prefix
//
memcpy( &shadowPath[ pos ], SHADOW_PREFIX, prefixLen );
assert( strlen( shadowPath ) == (strlen( path ) + strlen( SHADOW_PREFIX )) );
assert( strlen( shadowPath ) < shadowPathSize );
__exit:
*_shadowPath = shadowPath;
*_shadowPathSize = shadowPathSize;
return 0;
}
void
QvrFreeShadowPath(
__in char* shadowPath,
__in size_t shadowPathSize
)
{
IOFree( shadowPath, shadowPathSize );
}
//--------------------------------------------------------------------
bool
QvrIsExtensionEqual(
__in const char* path,
__in const char* extension
)
{
size_t pathLen = strlen( path );
size_t extLen = strlen( extension );
if( extLen >= pathLen )
return false;
return 0x0 == strncasecmp( &path[ pathLen - extLen ], extension, extLen );
}
//--------------------------------------------------------------------
errno_t
QvrConvertToRedirectedPath(
__in const char* file, // a file name stripped from the directories , i.e. "file" not /A/B/C/file, may contain '/' as a prefix
__in const char* redirectedDir, // without the terminating '/' , may be emty "" but not NULL
__out char** _redirectedFilePath,
__out vm_size_t* _nameBufferSize
)
{
assert( 0 == strlen(redirectedDir) || '/' != redirectedDir[strlen(redirectedDir) - 1] );
const char* prefix = redirectedDir;
size_t prefixLength = strlen(prefix);
bool slashRequired = ( 0 == prefixLength || file[0] == '/' || file[0] == '\0') ? false : true;
size_t slashLength = (slashRequired ? sizeof('/') : 0);
vm_size_t nameBufferSize = prefixLength + slashLength + strlen(file) + sizeof(L'\0');
char* redirectedFilePath = (char*)IOMalloc( nameBufferSize );
assert( redirectedFilePath );
if( ! redirectedFilePath )
return ENOMEM;
if( prefixLength ){
memcpy( redirectedFilePath, prefix, prefixLength );
if( slashRequired )
redirectedFilePath[ prefixLength ] = '/';
} else {
//
// a caller wants to convert only the file name,
// by providing a full path with an empty redirectedDir,
// in that case a leading '/' must be removed
//
while( '/' == file[ 0 ] )
file = &file[ 1 ];
//
// in that case the slash doesn't make sense
//
assert( ! slashRequired );
}
memcpy( redirectedFilePath + prefixLength + slashLength, file, strlen(file) + sizeof('\0') );
//
// convert directories to be part of the name by replacing / to $
//
size_t i = prefixLength + slashLength;
//
// leading / in the name is not harmfull
//
while( '/' == redirectedFilePath[ i ] ) ++i;
//
// replace '/' to $
//
while( '\0' != redirectedFilePath[ i ] )
{
if( '/' == redirectedFilePath[ i ] )
redirectedFilePath[ i ] = '$';
++i;
} // end while
*_redirectedFilePath = redirectedFilePath;
*_nameBufferSize = nameBufferSize;
return 0;
}
void
QvrFreeRedirectedPath(
__in char* redirectedFilePath,
__in size_t nameBufferSize
)
{
IOFree( redirectedFilePath, nameBufferSize );
}
//--------------------------------------------------------------------
errno_t
QvrConvertToShadowAndThenRedirectedPath(
__in const char* path, // a file name stripped from the directories , i.e. "file" not /A/B/C/file, may contain '/' as a prefix
__in const char* redirectedDir, // without the terminating '/' , may be emty "" but not NULL
__out char** redirectedFilePath,
__out vm_size_t* redirectedFilePathSize
)
{
errno_t error;
char* shadowPath;
vm_size_t shadowPathSize;
error = QvrConvertToShadowCopyPath( path,
&shadowPath,
&shadowPathSize );
assert( ! error );
if( error )
return error;
error = QvrConvertToRedirectedPath( shadowPath,
redirectedDir,
redirectedFilePath,
redirectedFilePathSize );
assert( ! error );
QvrFreeShadowPath( shadowPath, shadowPathSize );
return error;
}
//--------------------------------------------------------------------
static
vnode_t
QvrGetBackingVnodeForRedirectedIO(
__in vnode_t vn,
__in_opt const ApplicationData* appData,
__in bool forceUpdate
)
/*
a caller must release the vnode with vnode_put()
*/
{
vnode_t backingVnode = NULLVP;
char* redirectedFilePath = NULL;
vm_size_t nameBufferSize = 0;
char* vnodePath = NULL;
int vnodePathLength = MAXPATHLEN;
int error;
if( VREG != vnode_vtype( vn ) || (appData && !appData->redirectIO) )
goto __exit;
if( forceUpdate ){
VNodeMap::removeVnodeIO( vn );
} else {
backingVnode = VNodeMap::getVnodeIORef( vn );
if( backingVnode )
goto __exit;
}
vnodePath = (char*)IOMalloc( MAXPATHLEN );
assert( vnodePath );
if( ! vnodePath )
goto __exit;
//
// query vnode's name
//
error = vn_getpath( vn, vnodePath, &vnodePathLength );
if( error )
goto __exit;
error = QvrConvertToRedirectedPath( vnodePath, //GetVnodeNamePtr(vn),
appData->redirectTo,
&redirectedFilePath,
&nameBufferSize );
if( error )
goto __exit;
RecursionEngine::EnterRecursiveCall( current_thread() );
{
error = vnode_lookup( redirectedFilePath,
0x0, //VNODE_LOOKUP_NOFOLLOW,
&backingVnode,
gSuperUserContext );
}
RecursionEngine::LeaveRecursiveCall();
if( error )
goto __exit;
VNodeMap::addVnodeIO( vn, backingVnode );
RecursionEngine::EnterRecursiveCall( current_thread() );
{
//
// refill the cache with the new data from the backing file
// as the cache might contain invalid data read in by mdworker or similar
// appication prefetching data
//
off_t fileSize;
error = QvrVnodeGetSize( vn, &fileSize, gSuperUserContext );
assert( ! error );
if( ! error ){
//
// invalidate all exisiting pages, i.e. make all pages non resident
//
ubc_msync(vn, 0, i386_round_page(fileSize), NULL, UBC_INVALIDATE);
//
// if there is something left then overwrite it, the current solution is a brute force one
// by reading the whole file in the cache, TO DO modify only resident pages
//
if( ubc_pages_resident( vn ) ){
error = QvrReadInCacheFromBackingFile( vn, backingVnode, 0, i386_round_page(fileSize) );
assert( ! error );
}
} // end if( ! error )
}
RecursionEngine::LeaveRecursiveCall();
__exit:
if( redirectedFilePath )
QvrFreeRedirectedPath( redirectedFilePath, nameBufferSize );
if( vnodePath )
IOFree( vnodePath, MAXPATHLEN );
return backingVnode;
}
void
QvrAssociateVnodeForRedirectedIO(
__in vnode_t vn,
__in const ApplicationData* appData,
__in bool forceUpdate
)
{
//
// QvrGetBackingVnodeForRedirectedIO puts an entry into VNodeMap
//
assert( appData->redirectIO );
vnode_t vnodeIO = QvrGetBackingVnodeForRedirectedIO( vn, appData, forceUpdate );
if( vnodeIO )
vnode_put( vnodeIO );
}
//--------------------------------------------------------------------
int
QvrVnopLookupHookEx2(
__inout struct vnop_lookup_args *ap
)
/*
struct vnop_lookup_args {
struct vnodeop_desc *a_desc;
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
vfs_context_t a_context;
} *ap)
*/
{
int (*origVnop)(struct vnop_lookup_args *ap);
origVnop = (int (*)(struct vnop_lookup_args*))QvrGetOriginalVnodeOp( ap->a_dvp, QvrVopEnum_lookup );
assert( origVnop );
//
// the logic is as follows
// - get an application data for ADT_CreateNew to know whether redirectIO might be employed
// - if redirectIO is applicable get original vnode
// - check whether the original vnode has an association with ADT_CreateNew data
// - if ADT_CreateNew association doesn't exist then use ADT_OpenExisting association
//
const ApplicationData* appData = QvrGetApplicationDataByContext( ap->a_context, ADT_CreateNew );
if( !appData || RecursionEngine::IsRecursiveCall() || IsUserClient() ){
struct componentname* a_cnp = (struct componentname*)RecursionEngine::CookieForRecursiveCall();
if( a_cnp && (void*)current_thread() != (void*)a_cnp ){
//
// fix a cn_nameiop to a required one, we are particually interested in DELETE for rename purposes
//
ap->a_cnp->cn_nameiop = a_cnp->cn_nameiop;
}
return origVnop( ap );
}
errno_t error = (-1);
char* shadowPath = NULL;
vm_size_t shadowPathSize = 0;
char* shadowName = NULL;
vm_size_t shadowNameSize = 0;
char* redirectedFilePath = NULL;
vm_size_t nameBufferSize = 0;
vnode_t shadowVnode = NULLVP;
vnode_t originalVnode = NULLVP;
bool CreateOrRenameOrDelete = ( CREATE == ap->a_cnp->cn_nameiop ||
RENAME == ap->a_cnp->cn_nameiop ||
DELETE == ap->a_cnp->cn_nameiop);
assert( ! RecursionEngine::IsRecursiveCall() );
assert( appData );
if( 0x0 == strncasecmp( ap->a_cnp->cn_pnbuf, appData->redirectTo, strlen( appData->redirectTo ) ) ){
//
// an open of a file on the protected storage by a protected application, carry on
//
return origVnop( ap );
}
if( appData->redirectIO ){
//
// IO redirection , i.e. redirectIO == true
//
RecursionEngine::EnterRecursiveCall( current_thread() );
{ // start of the recursion
error = origVnop( ap );
} // end of the recursion
RecursionEngine::LeaveRecursiveCall();
if( error ){
//
// we are here if there an error was returned, this might be a case of create or rename
//
goto __exit;
}
assert( NULLVP != *ap->a_vpp );
const ApplicationData* appDataVnode = VNodeMap::getVnodeAppData( *ap->a_vpp );
//
// check whether the original vnode has redirectIO association
//
if( appDataVnode && appDataVnode->redirectIO ){
//
// the association exists, nothing to do here
//
goto __exit; // done
}
if( VREG != vnode_vtype( *ap->a_vpp ) ){
//
// we are only interested in regular files
//
goto __exit;
}
{
//
// filter
//
QvrPreOperationCallback inData;
bzero( &inData, sizeof(inData) );
inData.op = VFSOpcode_Filter;
inData.Parameters.Filter.in.op = VFSOpcode_Lookup;
inData.Parameters.Filter.in.path = ap->a_cnp->cn_pnbuf;
QvrPreOperationCallbackAndWaitForReply( &inData );
assert( inData.Parameters.Filter.out.replyWasReceived || inData.Parameters.Filter.out.noClient );
if( ! inData.Parameters.Filter.out.isControlledFile )
goto __exit;
}
/*
if( ! QvrIsExtensionEqual( ap->a_cnp->cn_nameptr, ".docx" ) &&
! QvrIsExtensionEqual( ap->a_cnp->cn_nameptr, ".tmp" ) ){
//
// for the TEST we are only interested in docx files
//
goto __exit;
}
*/
//
// there is no association with redirectIO, try to create one via shadow copying
//
originalVnode = *ap->a_vpp;
vnode_get( originalVnode );
vnode_put( *ap->a_vpp );
*ap->a_vpp = NULLVP;
//
// try to make a shadow copy anticipating possible write,
// if creating fails the file should be read only ( TO DO in KAUTH callback )
//
//
// create a shadow copy, if the copy existed and was connected with the vnode
// it was found on the previous step
// ATTENTION! If an application tries to opefile itself it will be allowed to do this
// as QvrConvertToShadowCopyPath returns an unmodified shadow path, we need to address
// this in the future - TO DO. e.g. disable shadow files open by noncontrolleded applications.
//
error = QvrConvertToShadowCopyPath( ap->a_cnp->cn_pnbuf, &shadowPath, &shadowPathSize );
assert( ! error );
if( error )
goto __exit;
error = QvrConvertToShadowCopyPath( ap->a_cnp->cn_nameptr, &shadowName, &shadowNameSize );
assert( ! error );
if( error )
goto __exit;
bool callDaemon = false;
assert( gSuperUserContext );
//
// create the shadow file, truncate the existing file
//
RecursionEngine::EnterRecursiveCall( current_thread() );
{ // start of the recursion
bool attemptToOpenShadowFile = false;
error = vnode_lookup( shadowPath,
0x0, //VNODE_LOOKUP_NOFOLLOW,
&shadowVnode,
gSuperUserContext );
if( error ){
//
// a shadow file doesn't exist, create a new one
//
error = vnode_open( shadowPath,
(O_CREAT | O_TRUNC | FREAD | FWRITE /*| O_NOFOLLOW*/), // fmode
(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH),// cmode
0x0, //VNODE_LOOKUP_NOFOLLOW,// flags
&shadowVnode,
gSuperUserContext ); // use superuser's context to avoid sandbox rules application
if( !error ) {
attemptToOpenShadowFile = (shadowVnode == originalVnode);
//
// the daemon must control file
//
callDaemon = true;
/*
//
// copy data
//
off_t fileSize;
error = QvrVnodeGetSize( originalVnode, &fileSize, ap->a_context );
if( ! error ){
QvrVnodeSetsize( shadowVnode, fileSize, 0x0, gSuperUserContext );
/*
const vm_size_t bufferSize = PAGE_SIZE;
void* buffer = IOMalloc( bufferSize );
assert( buffer );
if( buffer ){
off_t bytesRead = 0;
while( bytesRead < fileSize ){
off_t bytesToRead = min( (fileSize - bytesRead) , bufferSize );
error = vn_rdwr( UIO_READ,
originalVnode,
(char*)buffer,
bytesToRead,
bytesRead,
UIO_SYSSPACE,
IO_NOAUTH | IO_SYNC,
vfs_context_ucred(ap->a_context),
NULL,
vfs_context_proc(ap->a_context) );
if( error )
break;
error = vn_rdwr( UIO_WRITE,
shadowVnode,
(char*)buffer,
bytesToRead,
bytesRead,
UIO_SYSSPACE,
IO_NOAUTH | IO_SYNC,
vfs_context_ucred(gSuperUserContext),
NULL,
current_proc() );
if( error )
break;
bytesRead = bytesRead + bytesToRead;
} // end while( bytesRead < fileSize )
//
// the daemon must control file
//
// callDaemon = ! error;
IOFree( buffer, bufferSize );
} // end if( buffer );
//
// the daemon must control file
//
callDaemon = ! error;
} // if( ! error )
*/
//
// vnode_open returns with both an iocount and a usecount on the returned vnode,
// drop the usecount to avoid sharing violation error on subsequent file opens or
// a deadlock when a synchronous daemon callbac( callse by QvrPreOperationCallbackAndWaitForReply )
// waits on vnode_waitforwrites called by some system call from user mode
//
vnode_get( shadowVnode ); // bump an iocount as vnode_close() calls vnode_put()
vnode_close( shadowVnode , FREAD | FWRITE, gSuperUserContext );
} // if( !error )
if( (! error) && (! attemptToOpenShadowFile) ){
//
// equalize file sizes
//
off_t originalFileSize = 0;
off_t shadowFileSize = 0;
error = QvrVnodeGetSize( originalVnode, &originalFileSize, ap->a_context );
if( ! error )
error = QvrVnodeGetSize( shadowVnode, &shadowFileSize, gSuperUserContext );
if( (! error) && shadowFileSize != originalFileSize )
error = QvrVnodeSetsize( shadowVnode, originalFileSize, 0x0, gSuperUserContext );
}
} else { // else for if( error )
attemptToOpenShadowFile = (shadowVnode == originalVnode);
}
if( (! error) && (! attemptToOpenShadowFile) )
VNodeMap::addVnodeShadowReverse( shadowVnode, originalVnode );
} // end of the recursion
RecursionEngine::LeaveRecursiveCall();
if( ! error ) {
//
// continue with the shadow vnode
//
assert( shadowVnode );
assert( appData->redirectIO );
char* redirectedShadowPath = NULL;
vm_size_t redirectedShadowPathSize = 0;
error = QvrConvertToRedirectedPath( shadowPath,
appData->redirectTo,
&redirectedShadowPath,
&redirectedShadowPathSize);
assert( ! error );
if( ! error ){
//
// call the user mode daemon to control a shadow file
// counterpart in the protected storage
//
if( callDaemon ){
QvrPreOperationCallback inData;
bzero( &inData, sizeof(inData) );
inData.op = VFSOpcode_Lookup;
inData.Parameters.Lookup.pathToLookup = ap->a_cnp->cn_pnbuf;
inData.Parameters.Lookup.shadowFilePath = shadowPath;
inData.Parameters.Lookup.redirectedFilePath = redirectedShadowPath;
QvrPreOperationCallbackAndWaitForReply( &inData );
}
QvrFreeRedirectedPath( redirectedShadowPath, redirectedShadowPathSize );
redirectedShadowPath = NULL;
redirectedShadowPathSize = 0;
//
// associate with the file in the protected storage
//
QvrAssociateVnodeForRedirectedIO( shadowVnode, appData, false );
//
// get the backing vnode
//
vnode_t backingVnode = VNodeMap::getVnodeIORef( shadowVnode );
if( backingVnode ){
//
// associate the application data with the vnode returned to a caller
//
assert( appData->redirectIO );
VNodeMap::addVnodeAppData( shadowVnode, appData );
vnode_put( backingVnode );
// TEST TTTTTTTTTTTTT
//vnode_get( originalVnode );
//*ap->a_vpp = originalVnode;
//goto __exit;
// END TEST
//
// provide a caller with the shadow vnode
//
assert( NULLVP == *ap->a_vpp );
*ap->a_vpp = shadowVnode;
vnode_get( shadowVnode );
//
// fix the name
//
/*
char* name = (char*)GetVnodeNamePtr( shadowVnode );
size_t prefixLen = strlen( SHADOW_PREFIX );
if( 0x0 == strncmp( name, SHADOW_PREFIX, prefixLen ) )
{
memmove( name, name + prefixLen, strlen( name ) - prefixLen + sizeof('\0') );
}
*/
goto __exit;
} else {
//
// there is no association with redirectIO, switch to path redirection if applicable
//
error = ENFILE;
}
} // end if( ! error )
} // end if( ! error )
if( error ){
//
// general error OR read only media, carry on with path redirection ( TO DO disable write access in KAUTH callback )
//
error = (-1);
}
} // end if( appData->redirectIO )
//
// Path redirection, i.e. redirectIO = false , ( TO DO disable write access in KAUTH callback )
//
appData = QvrGetApplicationDataByContext( ap->a_context, ADT_OpenExisting );
assert( appData && !appData->redirectIO );
//
// never add to the name cache
//
ap->a_cnp->cn_flags &= ~MAKEENTRY;
error = QvrConvertToRedirectedPath( ap->a_cnp->cn_pnbuf,//ap->a_cnp->cn_nameptr,
appData->redirectTo,
&redirectedFilePath,
&nameBufferSize);
if( error )
goto __exit;
//
// call the daemon to control the file on the protected storage
//
{
QvrPreOperationCallback inData;
bzero( &inData, sizeof(inData) );
inData.op = VFSOpcode_Lookup;
inData.Parameters.Lookup.pathToLookup = ap->a_cnp->cn_pnbuf;
inData.Parameters.Lookup.shadowFilePath = NULL;
inData.Parameters.Lookup.redirectedFilePath = redirectedFilePath;
inData.Parameters.Lookup.calledFromCreate = false;
QvrPreOperationCallbackAndWaitForReply( &inData );
}
if( gRedirectionIsNullAndVoid )
{
RecursionEngine::EnterRecursiveCall( current_thread() );
{
error = origVnop( ap );
}
RecursionEngine::LeaveRecursiveCall();
goto __exit;
}
RecursionEngine::EnterRecursiveCall( current_thread() );
{ // start of the recursion
//__asm__ volatile( "int $0x3" );
//
// TO DO - make ap's copy to avoid side effects from the call
//
if( gSynchroniseWithOriginal )
error = origVnop( ap );
else
error = CreateOrRenameOrDelete ? EJUSTRETURN : ENOENT;
} // end of the recursion
RecursionEngine::LeaveRecursiveCall();
if( !error && VREG != vnode_vtype( *ap->a_vpp ) )
goto __exit;
if( (! error /*&& ! CreateOrRenameOrDelete*/ )// file exist in the original directory look for the redirected one
||
( EJUSTRETURN == error && CreateOrRenameOrDelete ) // create or rename, file doesn't exist
||
ENOENT == error // an original file doesn't exists, e.g. it has been renamed in the redirected directory,
// if it doesn't exit in the redirected one the same error will be returned
)
{
vnode_t redirectedVnode = NULLVP;
errno_t error2;
RecursionEngine::EnterRecursiveCall( current_thread() );
{ // start of the recursion
error2 = vnode_lookup( redirectedFilePath,
0x0, //VNODE_LOOKUP_NOFOLLOW,
&redirectedVnode,
gSuperUserContext );
} // end of the recursion
RecursionEngine::LeaveRecursiveCall();
if( error2 ){
if( error ){
//
// file doesn't exist in the both original and redirected directories
//
assert( ! *ap->a_vpp );
//
// N.B. we do not need this processing if ap->a_cnp->cn_nameiop is used with recursive call as vnode_lookup calls namei
// that converts EJUSTRETURN to 0 for create, rename and delete(?)
//
if( EJUSTRETURN == error ){
//
// file doesn't exist in the original directory and
// this is a create or rename operation
//
if( ENOENT == error2 || EJUSTRETURN == error2 ){
//
// this is a create or rename operation, the kernel's lookup() will return 0 and namei() will call VNOP_CREATE
//
error2 = EJUSTRETURN; // just fix error2
} else {
//
// an unknown conflict between two directories on rename or delete
//
error = error2;
}
} else {
//
// a genuine error while looking in the redirected directory,
// transfer the error2 to a returned value
//
error = error2;
DBG_PRINT_ERROR(( "vnode_lookup( %s ) failed with an error(%u)\n", redirectedFilePath, error ));
}
//
// a genuine error while looking in the redirected directory,
// transfer the error2 to a returned value
//
error = error2;
DBG_PRINT_ERROR(( "vnode_lookup( %s ) failed with an error(%u)\n", redirectedFilePath, error ));
} else {
//
// return the vnode,
// this is a case of a file not supported by a
// redirected one
//
}
} else {
//
// a vnode has been found by the redirected path or this is create or rename
//
assert( ! error2 );
if( !redirectedVnode && !*ap->a_vpp ){
assert( CreateOrRenameOrDelete );
assert( EJUSTRETURN == error );
error2 = error = EJUSTRETURN;