-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisting1.html
executable file
·1195 lines (987 loc) · 44.2 KB
/
listing1.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>KauthORama - /KauthORama.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Darwin/index.html">Darwin</a> > <a href="../../samplecode/Darwin/idxKernel-date.html">Kernel</a> > <A HREF="javascript:location.replace('index.html');">KauthORama</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">KauthORama</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/KauthORama.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/KauthORama.c</option>
<option value="listing2.html">/Read Me About KauthORama.txt</option></select>
</p>
</form>
<p><strong><a href="KauthORama.zip">Download Sample</a></strong> (“KauthORama.zip”, 17.6K)<BR>
<strong><a href="KauthORama.dmg">Download Sample</a></strong> (“KauthORama.dmg”, 73.7K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: KauthORama.c
Contains: A kernel extension to dump all known Kauth operations.
Written by: DTS
Copyright: Copyright (c) 2007 by Apple Computer, Inc., All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Change History (most recent first):
$Log: KauthORama.c,v $
Revision 1.4 2007/01/02 16:58:52
Work around <rdar://problem/4605516> by checking for NULL string pointers.
Revision 1.3 2005/07/21 23:36:10
Initialise a local variable to defeat an erroneous warning from GCC 4.0.
Revision 1.2 2005/05/31 13:21:05
Correct grammo in comment.
Revision 1.1 2005/05/16 10:28:29
First checked in.
*/
/////////////////////////////////////////////////////////////////
#include <kern/assert.h>
#include <mach/mach_types.h>
#include <libkern/libkern.h>
#include <libkern/OSAtomic.h>
#include <libkern/OSMalloc.h>
#include <sys/sysctl.h>
#include <sys/kauth.h>
#include <sys/vnode.h>
// strprefix is in libkern's export list, but not in any header <rdar://problem/4116835>.
int strprefix(const char *s1, const char *s2);
/////////////////////////////////////////////////////////////////
#pragma mark ***** Global Resources
// These declarations are required to allocate memory and create locks.
// They're created when we start and destroyed when we stop.
static OSMallocTag gMallocTag = NULL;
static lck_grp_t * gLockGroup = NULL;
/////////////////////////////////////////////////////////////////
#pragma mark ***** Vnode Utilities
// I've pulled these vnode utility routines out of the VnodeScopeListener to make it
// easier to understand.
// VnodeActionInfo describes one of the action bits in the vnode scope's action
// field.
struct VnodeActionInfo {
kauth_action_t fMask; // only one bit should be set
const char * fOpNameFile; // descriptive name of the bit for files
const char * fOpNameDir; // descriptive name of the bit for directories
// NULL implies equivalent to fOpNameFile
};
typedef struct VnodeActionInfo VnodeActionInfo;
// Some evil macros (aren't they all) to make it easier to initialise kVnodeActionInfo.
#define VNODE_ACTION(action) { KAUTH_VNODE_ ## action, #action, NULL }
#define VNODE_ACTION_FILEDIR(actionFile, actionDir) { KAUTH_VNODE_ ## actionFile, #actionFile, #actionDir }
// kVnodeActionInfo is a table of all the known action bits and their human readable names.
static const VnodeActionInfo kVnodeActionInfo[] = {
VNODE_ACTION_FILEDIR(READ_DATA, LIST_DIRECTORY),
VNODE_ACTION_FILEDIR(WRITE_DATA, ADD_FILE),
VNODE_ACTION_FILEDIR(EXECUTE, SEARCH),
VNODE_ACTION(DELETE),
VNODE_ACTION_FILEDIR(APPEND_DATA, ADD_SUBDIRECTORY),
VNODE_ACTION(DELETE_CHILD),
VNODE_ACTION(READ_ATTRIBUTES),
VNODE_ACTION(WRITE_ATTRIBUTES),
VNODE_ACTION(READ_EXTATTRIBUTES),
VNODE_ACTION(WRITE_EXTATTRIBUTES),
VNODE_ACTION(READ_SECURITY),
VNODE_ACTION(WRITE_SECURITY),
VNODE_ACTION(TAKE_OWNERSHIP),
VNODE_ACTION(SYNCHRONIZE),
VNODE_ACTION(LINKTARGET),
VNODE_ACTION(CHECKIMMUTABLE),
VNODE_ACTION(ACCESS),
VNODE_ACTION(NOIMMUTABLE)
};
#define kVnodeActionInfoCount (sizeof(kVnodeActionInfo) / sizeof(*kVnodeActionInfo))
static int CreateVnodeActionString(
kauth_action_t action,
boolean_t isDir,
char ** actionStrPtr,
size_t * actionStrBufSizePtr
)
// Creates a human readable description of a vnode action bitmap.
// action is the bitmap. isDir is true if the action relates to a
// directory, and false otherwise. This allows the action name to
// be context sensitive (KAUTH_VNODE_EXECUTE vs KAUTH_VNODE_SEARCH).
// actionStrPtr is a place to store the allocated string pointer.
// The caller is responsible for freeing this memory using OSFree.
// actionStrBufSizePtr is a place to store the size of the resulting
// allocation (because the annoying kernel memory allocator requires
// you to provide the size when you free).
{
int err;
enum { kCalcLen, kCreateString } pass;
kauth_action_t actionsLeft;
unsigned int infoIndex;
size_t actionStrLen;
char * actionStr;
assert( actionStrPtr != NULL);
assert(*actionStrPtr != NULL);
assert( actionStrBufSizePtr != NULL);
err = 0;
actionStr = NULL;
// A two pass algorithm. In the first pass, actionStr is NULL and we just
// calculate actionStrLen; at the end of the first pass we actually allocate
// actionStr. In the second pass, actionStr is not NULL and we actually
// initialise the string in that buffer.
for (pass = kCalcLen; pass <= kCreateString; pass++) {
actionsLeft = action;
// Process action bits that are described in kVnodeActionInfo.
infoIndex = 0;
actionStrLen = 0;
while ( (actionsLeft != 0) && (infoIndex < kVnodeActionInfoCount) ) {
if ( actionsLeft & kVnodeActionInfo[infoIndex].fMask ) {
const char * thisStr;
size_t thisStrLen;
// Increment the length of the acion string by the action name.
if ( isDir && (kVnodeActionInfo[infoIndex].fOpNameDir != NULL) ) {
thisStr = kVnodeActionInfo[infoIndex].fOpNameDir;
} else {
thisStr = kVnodeActionInfo[infoIndex].fOpNameFile;
}
thisStrLen = strlen(thisStr);
if (actionStr != NULL) {
memcpy(&actionStr[actionStrLen], thisStr, thisStrLen);
}
actionStrLen += thisStrLen;
// Now clear the bit in actionsLeft, indicating that we've
// processed this one.
actionsLeft &= ~kVnodeActionInfo[infoIndex].fMask;
// If there's any actions left, account for the intervening "|".
if (actionsLeft != 0) {
if (actionStr != NULL) {
actionStr[actionStrLen] = '|';
}
actionStrLen += 1;
}
}
infoIndex += 1;
}
// Now include any remaining actions as a hex number.
if (actionsLeft != 0) {
if (actionStr != NULL) {
// This will write 11 bytes (10 bytes of string plus a null
// char), but that's OK because we know that we allocated
// space for the null.
sprintf(&actionStr[actionStrLen], "0x%08x", actionsLeft);
}
actionStrLen += 10; // strlen("0x") + 8 chars of hex
}
// If we're at the end of the first pass, allocate actionStr
// based on the size we just calculated. Remember that actionStrLen
// is a string length, so we have to allocate an extra character to
// account for the null terminator. If we're at the end of the
// second pass, just place the null terminator.
if (pass == kCalcLen) {
actionStr = OSMalloc(actionStrLen + 1, gMallocTag);
if (actionStr == NULL) {
err = ENOMEM;
}
} else {
actionStr[actionStrLen] = 0;
}
if (err != 0) {
break;
}
}
// Clean up.
*actionStrPtr = actionStr;
*actionStrBufSizePtr = actionStrLen + 1;
assert( (err == 0) == (*actionStrPtr != NULL) );
return err;
}
static int CreateVnodePath(vnode_t vp, char **vpPathPtr)
// Creates a full path for a vnode. vp may be NULL, in which
// case the returned path is NULL (that is, no memory is allocated).
// vpPathPtr is a place to store the allocated path buffer.
// The caller is responsible for freeing this memory using OSFree
// (the size is always MAXPATHLEN).
{
int err;
int pathLen;
assert( vpPathPtr != NULL);
assert(*vpPathPtr == NULL);
err = 0;
if (vp != NULL) {
*vpPathPtr = OSMalloc(MAXPATHLEN, gMallocTag);
if (*vpPathPtr == NULL) {
err = ENOMEM;
}
if (err == 0) {
pathLen = MAXPATHLEN;
err = vn_getpath(vp, *vpPathPtr, &pathLen);
}
}
return err;
}
/////////////////////////////////////////////////////////////////
#pragma mark ***** Listener Resources
// Some scopes (for example KAUTH_SCOPE_VNODE) are called a /lot/. Thus,
// it's a good idea to avoid taking mutexes in your listener if at all
// possible. Thus, we use non-blocking synchronisation to protect the
// global data that's accessed by our listener (gPrefix and gListenerScope).
// Every time we enter a listener, we increment gActivationCount, and ever
// time we leave we decrement it. When we want to change the listener, we
// first remove the listener, then we wait for the activation count to hit,
// then we can modify the globals protected by that activation count.
//
// IMPORTANT:
// There is still a race condition here. See RemoveListener for a description
// of the race and why we can't fix it.
static SInt32 gActivationCount = 0;
static const char * gPrefix = NULL; // points into gConfiguration, so doesn't need to be freed
static char * gListenerScope = NULL; // must be freed using OSFree
static int GenericScopeListener(
kauth_cred_t credential,
void * idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3
)
// A Kauth listener that's called to authorize an action in the generic
// scope (KAUTH_SCOPE_GENERIC). See the Kauth documentation for a description
// of the parameters. In this case, we just dump out the parameters to the
// operation and return KAUTH_RESULT_DEFER, allowing the other listeners
// to decide whether the operation is allowed or not.
{
#pragma unused(idata)
#pragma unused(arg0)
#pragma unused(arg1)
#pragma unused(arg2)
#pragma unused(arg3)
(void) OSIncrementAtomic(&gActivationCount);
// Tell the user about this request.
switch (action) {
case KAUTH_GENERIC_ISSUSER:
printf(
"scope=" KAUTH_SCOPE_GENERIC ", action=KAUTH_GENERIC_ISSUSER, actor=%ld\n",
(long) kauth_cred_getuid(credential)
);
break;
default:
printf("KauthORama.GenericScopeListener: Unknown action (%d).\n", action);
break;
}
(void) OSDecrementAtomic(&gActivationCount);
return KAUTH_RESULT_DEFER;
}
static int ProcessScopeListener(
kauth_cred_t credential,
void * idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3
)
// A Kauth listener that's called to authorize an action in the process
// scope (KAUTH_SCOPE_PROCESS). See the Kauth documentation for a description
// of the parameters. In this case, we just dump out the parameters to the
// operation and return KAUTH_RESULT_DEFER, allowing the other listeners
// to decide whether the operation is allowed or not.
{
#pragma unused(idata)
#pragma unused(arg2)
#pragma unused(arg3)
(void) OSIncrementAtomic(&gActivationCount);
// Tell the user about this request.
switch (action) {
case KAUTH_PROCESS_CANSIGNAL:
printf(
"scope=" KAUTH_SCOPE_PROCESS ", action=KAUTH_PROCESS_CANSIGNAL, uid=%ld, pid=%ld, target=%ld, signal=%ld\n",
(long) kauth_cred_getuid(credential),
(long) proc_selfpid(),
(long) proc_pid((proc_t) arg0),
(long) arg1
);
break;
case KAUTH_PROCESS_CANTRACE:
printf(
"scope=" KAUTH_SCOPE_PROCESS ", action=KAUTH_PROCESS_CANTRACE, uid=%ld, pid=%ld, target=%ld\n",
(long) kauth_cred_getuid(credential),
(long) proc_selfpid(),
(long) proc_pid((proc_t) arg0)
);
break;
default:
printf("KauthORama.ProcessScopeListener: Unknown action (%d).\n", action);
break;
}
(void) OSDecrementAtomic(&gActivationCount);
return KAUTH_RESULT_DEFER;
}
static int VnodeScopeListener(
kauth_cred_t credential,
void * idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3
)
// A Kauth listener that's called to authorize an action in the vnode
// scope (KAUTH_SCOPE_PROCESS). See the Kauth documentation for a description
// of the parameters. In this case, we just dump out the parameters to the
// operation and return KAUTH_RESULT_DEFER, allowing the other listeners
// to decide whether the operation is allowed or not.
{
#pragma unused(credential)
#pragma unused(idata)
#pragma unused(arg3)
int err;
vfs_context_t context;
vnode_t vp;
vnode_t dvp;
char * vpPath;
char * dvpPath;
boolean_t isDir;
char * actionStr;
size_t actionStrBufSize;
// The following initialisation of actionStrBufSize is just to quieten a warning in
// optimised builds; GCC erroneously thinks that the call to OSFree(actionStr, ...)
// can use actionStrBufSize without it being initialised. However, my error handling
// idiom means that can't happen. Rather than turn off this warning for the entire
// file, I just initialise the variable, even though it's not necessary.
actionStrBufSize = 0;
(void) OSIncrementAtomic(&gActivationCount);
context = (vfs_context_t) arg0;
vp = (vnode_t) arg1;
dvp = (vnode_t) arg2;
vpPath = NULL;
dvpPath = NULL;
actionStr = NULL;
// Convert the vnode, if any, to a path.
err = CreateVnodePath(vp, &vpPath);
// Convert the parent directory vnode, if any, to a path.
if (err == 0) {
err = CreateVnodePath(dvp, &dvpPath);
}
// Create actionStr as a human readable description of action.
if (err == 0) {
if (vp != NULL) {
isDir = ( vnode_vtype(vp) == VDIR );
} else {
isDir = FALSE;
}
err = CreateVnodeActionString(action, isDir, &actionStr, &actionStrBufSize);
}
// Tell the user about this request. Note that we filter requests
// based on gPrefix. If gPrefix is set, only requests where one
// of the paths is prefixed by gPrefix will be printed.
if (err == 0) {
if ( (gPrefix == NULL)
|| ( ( (vpPath != NULL) && strprefix(vpPath, gPrefix) )
|| ( (dvpPath != NULL) && strprefix(dvpPath, gPrefix) )
)
) {
printf(
"scope=" KAUTH_SCOPE_VNODE ", action=%s, uid=%ld, vp=%s, dvp=%s\n",
actionStr,
(long) kauth_cred_getuid(vfs_context_ucred(context)),
(vpPath != NULL) ? vpPath : "<null>",
(dvpPath != NULL) ? dvpPath : "<null>"
);
}
} else {
printf("KauthORama.VnodeScopeListener: Error %d.\n", err);
}
// Clean up.
if (actionStr != NULL) {
OSFree(actionStr, actionStrBufSize, gMallocTag);
}
if (vpPath != NULL) {
OSFree(vpPath, MAXPATHLEN, gMallocTag);
}
if (dvpPath != NULL) {
OSFree(dvpPath, MAXPATHLEN, gMallocTag);
}
(void) OSDecrementAtomic(&gActivationCount);
return KAUTH_RESULT_DEFER;
}
static void PrintWarning(const char *action)
// Under some circumstances it's possible for the kernel to not send us some
// critical path information. The first time this occurs, we print a warning.
// This problem <rdar://problem/4605516> affects all versions of Mac OS X 10.4.x.
// We expect that it will be fixed in a future major release of Mac OS X.
{
static boolean_t sHavePrinted;
if ( ! sHavePrinted ) {
printf("KauthORama.FileOpScopeListener: Encountered <rdar://problem/4605516> in %s case.\n", action);
sHavePrinted = TRUE;
}
}
static int FileOpScopeListener(
kauth_cred_t credential,
void * idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3
)
// A Kauth listener that's called to authorize an action in the file operation
// scope (KAUTH_SCOPE_PROCESS). See the Kauth documentation for a description
// of the parameters. In this case, we just dump out the parameters to the
// operation and return KAUTH_RESULT_DEFER, allowing the other listeners
// to decide whether the operation is allowed or not.
{
#pragma unused(idata)
#pragma unused(arg2)
#pragma unused(arg3)
(void) OSIncrementAtomic(&gActivationCount);
// Tell the user about this request. Note that we filter requests
// based on gPrefix. If gPrefix is set, only requests there is a
// path that's prefixed by gPrefix will be printed.
switch (action) {
case KAUTH_FILEOP_OPEN:
if ( (gPrefix == NULL) || strprefix( (const char *) arg1, gPrefix) ) {
printf(
"scope=" KAUTH_SCOPE_FILEOP ", action=KAUTH_FILEOP_OPEN, uid=%ld, vnode=0x%lx, path=%s\n",
(long) kauth_cred_getuid(credential),
(long) arg0,
(const char *) arg1
);
}
break;
case KAUTH_FILEOP_CLOSE:
if ( (gPrefix == NULL) || strprefix( (const char *) arg1, gPrefix) ) {
printf(
"scope=" KAUTH_SCOPE_FILEOP ", action=KAUTH_FILEOP_CLOSE, uid=%ld, vnode=0x%lx, path=%s, dirty=%s\n",
(long) kauth_cred_getuid(credential),
(long) arg0,
(const char *) arg1,
((int) arg2 & KAUTH_FILEOP_CLOSE_MODIFIED) ? "true" : "false"
);
}
break;
case KAUTH_FILEOP_RENAME:
if ( (arg0 == NULL) || (arg1 == NULL) ) {
PrintWarning("KAUTH_FILEOP_RENAME");
} else {
if ( (gPrefix == NULL) || ( strprefix( (const char *) arg0, gPrefix) || strprefix( (const char *) arg1, gPrefix) ) ) {
printf(
"scope=" KAUTH_SCOPE_FILEOP ", action=KAUTH_FILEOP_RENAME, uid=%ld, from=%s, to=%s\n",
(long) kauth_cred_getuid(credential),
(const char *) arg0,
(const char *) arg1
);
}
}
break;
case KAUTH_FILEOP_EXCHANGE:
if ( (arg0 == NULL) || (arg1 == NULL) ) {
PrintWarning("KAUTH_FILEOP_EXCHANGE");
} else {
if ( (gPrefix == NULL) || ( strprefix( (const char *) arg0, gPrefix) || strprefix( (const char *) arg1, gPrefix) ) ) {
printf(
"scope=" KAUTH_SCOPE_FILEOP ", action=KAUTH_FILEOP_EXCHANGE, uid=%ld, file1=%s, file2=%s\n",
(long) kauth_cred_getuid(credential),
(const char *) arg0,
(const char *) arg1
);
}
}
break;
case KAUTH_FILEOP_LINK:
if ( (arg0 == NULL) || (arg1 == NULL) ) {
PrintWarning("KAUTH_FILEOP_LINK");
} else {
if ( (gPrefix == NULL) || ( strprefix( (const char *) arg0, gPrefix) || strprefix( (const char *) arg1, gPrefix) ) ) {
printf(
"scope=" KAUTH_SCOPE_FILEOP ", action=KAUTH_FILEOP_LINK, uid=%ld, original=%s, new=%s\n",
(long) kauth_cred_getuid(credential),
(const char *) arg0,
(const char *) arg1
);
}
}
break;
case KAUTH_FILEOP_EXEC:
if ( (gPrefix == NULL) || strprefix( (const char *) arg1, gPrefix) ) {
printf(
"scope=" KAUTH_SCOPE_FILEOP ", action=KAUTH_FILEOP_EXEC, uid=%ld, vnode=0x%lx, path=%s\n",
(long) kauth_cred_getuid(credential),
(long) arg0,
(const char *) arg1
);
}
break;
default:
printf("KauthORama.FileOpScopeListener: Unknown action (%d).\n", action);
break;
}
(void) OSDecrementAtomic(&gActivationCount);
return KAUTH_RESULT_DEFER;
}
static int UnknownScopeListener(
kauth_cred_t credential,
void * idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3
)
// A Kauth listener that's called to authorize an action in any scope
// that we don't recognise). See the Kauth documentation for a description
// of the parameters. In this case, we just dump out the parameters to the
// operation and return KAUTH_RESULT_DEFER, allowing the other listeners
// to decide whether the operation is allowed or not.
{
#pragma unused(idata)
(void) OSIncrementAtomic(&gActivationCount);
// Tell the user about this request.
printf(
"scope=%s, action=%d, uid=%ld, arg0=0x%lx, arg1=0x%lx, arg2=0x%lx, arg3=0x%lx\n",
gListenerScope,
action,
(long) kauth_cred_getuid(credential),
(long) arg0,
(long) arg1,
(long) arg2,
(long) arg3
);
(void) OSDecrementAtomic(&gActivationCount);
return KAUTH_RESULT_DEFER;
}
/////////////////////////////////////////////////////////////////
#pragma mark ***** Listener Install/Remove
// gConfigurationLock is a mutex that protects us from two threads trying to
// simultaneously modify the configuration. The configuration is protect in
// N ways:
//
// o During startup, we register our sysctl OID last, so no one can start
// modifying the configuration until everything is set up nicely.
//
// o During normal operations, the sysctl handler (SysctlHandler) takes
// the lock to prevent two threads from reconfiguring the system at the
// same time.
//
// o During termination, the stop routine first removes the sysctl OID
// and then takes the lock before it removes the listener. The first
// act prevents any new sysctl requests coming it, the second blocks
// until current sysctl requests are done.
//
// IMPORTANT:
// There is still a race condition here. See the stop routine for a description
// of the race and why we can't fix it.
static lck_mtx_t * gConfigurationLock = NULL;
// gListener is our handle to the installed scope listener. We need to
// keep it around so that we can remove the listener when we're done.
static kauth_listener_t gListener = NULL;
static void RemoveListener(void)
// Removes the installed scope listener, if any.
//
// Under almost all circumstances this routine runs under the
// gConfigurationLock. The only time that this might not be the case
// is when the KEXT's start routine fails prior to gConfigurationLock
// being created.
{
// First prevent any more threads entering our listener.
if (gListener != NULL) {
kauth_unlisten_scope(gListener);
gListener = NULL;
}
// Then wait for any threads within out listener to stop. Note that there
// is still a race condition here; there could still be a thread executing
// between the OSDecrementAtomic and the return from the listener function
// (for example, FileOpScopeListener). However, there's no way to close
// this race because of the weak concurrency guarantee for kauth_unlisten_scope.
// Moreover, the window is very small and, seeing as this only happens during
// reconfiguration, I'm not too worried. However, I am worried enough
// to ensure that this loop runs at least once, so we always delay the teardown
// for at least one second waiting for the threads to drain from our
// listener.
do {
struct timespec oneSecond;
oneSecond.tv_sec = 1;
oneSecond.tv_nsec = 0;
(void) msleep(&gActivationCount, NULL, PUSER, "com_apple_dts_kext_KauthORama.RemoveListener", &oneSecond);
} while ( gActivationCount > 0 );
// gListenerScope and gPrefix are both accessed by the listener callbacks
// without taking any form of lock. So, we don't destroy them until after
// all the listener callbacks have drained.
if (gListenerScope != NULL) {
OSFree(gListenerScope, strlen(gListenerScope) + 1, gMallocTag);
gListenerScope = NULL;
}
gPrefix = NULL;
}
static void InstallListener(const char *scope, size_t scopeLen, const char *prefix)
// Installs a listener for the specified scope. scope and scopeLen specifies
// the scope to listen for. prefix is a parameter for the scope listener.
// It may be NULL.
//
// prefix points into the gConfiguration global variable, so this routine
// doesn't make a copy of it. However, it has to make a copy of scope
// because scope can point to a place in the middle of the gConfiguration
// variable, so there's no guarantee it's null terminated (which we need it
// to be in order to call kauth_listen_scope.
//
// This routine always runs under the gConfigurationLock.
{
kauth_scope_callback_t callback;
assert(scope != NULL);
assert(scopeLen > 0);
// Allocate memory for the scope string. We need to keep a persistent
// copy of this string because kauth_listen_scope doesn't make a copy of
// its scope identifier input parameter. Normally you'd use a constant
// string, which persists as long as the kext is loaded, but I can't do
// that because the scope identifier is supplied by the user via sysctl.
assert(gListenerScope == NULL);
gListenerScope = OSMalloc(scopeLen + 1, gMallocTag);
if (gListenerScope == NULL) {
printf("KauthORama.InstallListener: Could not allocate gListenerScope.\n");
} else {
memcpy(gListenerScope, scope, scopeLen);
gListenerScope[scopeLen] = 0;
// Copy the prefix pointer over to gPrefix.
assert(gPrefix == NULL);
gPrefix = prefix;
// Register the appropriate listener with Kauth.
if ( strcmp(gListenerScope, KAUTH_SCOPE_GENERIC) == 0 ) {
callback = GenericScopeListener;
} else if ( strcmp(gListenerScope, KAUTH_SCOPE_PROCESS) == 0 ) {
callback = ProcessScopeListener;
} else if ( strcmp(gListenerScope, KAUTH_SCOPE_VNODE) == 0 ) {
callback = VnodeScopeListener;
} else if ( strcmp(gListenerScope, KAUTH_SCOPE_FILEOP) == 0 ) {
callback = FileOpScopeListener;
} else {
callback = UnknownScopeListener;
}
assert(gListener == NULL);
gListener = kauth_listen_scope(gListenerScope, callback, NULL);
if (gListener == NULL) {
printf("KauthORama.InstallListener: Could not create gListener.\n");
}
}
// In the event of any failure, call RemoveListener which will
// do all the right cleanup.
if ( gListenerScope == NULL || gListener == NULL ) {
RemoveListener();
}
}
static void ConfigureKauth(const char *configuration)
// This routine is called by the sysctl handler when it notices
// that the configuration has changed. It's responsible for
// parsing the new configuration string and updating the listener.
//
// See SysctlHandler for a description of how I chose to handle the
// failure case.
//
// This routine always runs under the gConfigurationLock.
{
assert(configuration != NULL);
// Remove the existing listener.
RemoveListener();
// Parse the configuration string and install the new listener.
if (strcmp(configuration, "remove") == 0) {
printf("KauthORama.ConfigureKauth: Removed listener.\n", configuration);
} else if ( strprefix(configuration, "add ") ) {
const char *cursor;
const char *scopeStart;
const char *scopeEnd;
const char *prefixStart;
// Skip the "add ".
cursor = configuration + strlen("add "); // yergh!
// Work out the span of the scope.
scopeStart = cursor;
while ( (*cursor != ' ') && (*cursor != 0) ) {
cursor += 1;
}
scopeEnd = cursor;
if (scopeStart == scopeEnd) {
printf("KauthORama.ConfigureKauth: Bad configuration '%s'.\n", configuration);
} else {
// Look for a prefix.
if (*cursor == ' ') {
cursor += 1;
}
if (*cursor == 0) {
prefixStart = NULL;
} else {
prefixStart = cursor;
}
// Tell the user what we're doing.
if (prefixStart == NULL) {
printf("KauthORama.ConfigureKauth: scope = %.*s\n", (int) (scopeEnd - scopeStart), scopeStart);
} else {
printf("KauthORama.ConfigureKauth: scope = %.*s, prefix = %s\n", (int) (scopeEnd - scopeStart), scopeStart, prefixStart);
}
// Do it.
InstallListener(scopeStart, scopeEnd - scopeStart, prefixStart);
}
} else {
printf("KauthORama.ConfigureKauth: Bad configuration '%s'.\n", configuration);
}
}
// gConfiguration holds our current configuration string. It's modified by
// SysctlHandler (well, by sysctl_handle_string which is called by SysctlHandler).
static char gConfiguration[1024];
static int SysctlHandler(
struct sysctl_oid * oidp,
void * arg1,
int arg2,
struct sysctl_req * req
)
// This routine is called by the kernel when the user reads or
// writes our sysctl variable. The arguments are standard for
// a sysctl handler.
{
int result;
// Prevent two threads trying to change our configuration at the same
// time.
lck_mtx_lock(gConfigurationLock);
// Let sysctl_handle_string do all the heavy lifting of getting
// and setting the variable.
result = sysctl_handle_string(oidp, arg1, arg2, req);