forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkern_credential.c
5468 lines (4971 loc) · 153 KB
/
kern_credential.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2004-2020 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
* support for mandatory and extensible security protections. This notice
* is included in support of clause 2.2 (b) of the Apple Public License,
* Version 2.0.
*/
/*
* Kernel Authorization framework: Management of process/thread credentials
* and identity information.
*/
#include <sys/param.h> /* XXX trim includes */
#include <sys/acct.h>
#include <sys/systm.h>
#include <sys/ucred.h>
#include <sys/proc_internal.h>
#include <sys/user.h>
#include <sys/timeb.h>
#include <sys/times.h>
#include <sys/malloc.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/sdt.h>
#include <security/audit/audit.h>
#include <sys/mount.h>
#include <sys/stat.h> /* For manifest constants in posix_cred_access */
#include <sys/sysproto.h>
#include <mach/message.h>
#include <mach/host_security.h>
#include <machine/atomic.h>
#include <kern/task.h>
#include <kern/locks.h>
#ifdef MACH_ASSERT
# undef MACH_ASSERT
#endif
#define MACH_ASSERT 1 /* XXX so bogus */
#include <kern/assert.h>
#if CONFIG_MACF
#include <security/mac.h>
#include <security/mac_framework.h>
#include <security/_label.h>
#endif
#include <os/hash.h>
#include <IOKit/IOBSD.h>
void mach_kauth_cred_uthread_update( void );
# define NULLCRED_CHECK(_c) do {if (!IS_VALID_CRED(_c)) panic("%s: bad credential %p", __FUNCTION__,_c);} while(0)
/* Set to 1 to turn on KAUTH_DEBUG for kern_credential.c */
#if 0
#ifdef KAUTH_DEBUG
#undef KAUTH_DEBUG
#endif
#ifdef K_UUID_FMT
#undef K_UUID_FMT
#endif
#ifdef K_UUID_ARG
#undef K_UUID_ARG
#endif
# define K_UUID_FMT "%08x:%08x:%08x:%08x"
# define K_UUID_ARG(_u) &_u.g_guid_asint[0],&_u.g_guid_asint[1],&_u.g_guid_asint[2],&_u.g_guid_asint[3]
# define KAUTH_DEBUG(fmt, args...) do { printf("%s:%d: " fmt "\n", __PRETTY_FUNCTION__, __LINE__ , ##args); } while (0)
#endif
/*
* Credential debugging; we can track entry into a function that might
* change a credential, and we can track actual credential changes that
* result.
*
* Note: Does *NOT* currently include per-thread credential changes
*/
#if DEBUG_CRED
#define DEBUG_CRED_ENTER printf
#define DEBUG_CRED_CHANGE printf
#else /* !DEBUG_CRED */
#define DEBUG_CRED_ENTER(fmt, ...) do {} while (0)
#define DEBUG_CRED_CHANGE(fmt, ...) do {} while (0)
#endif /* !DEBUG_CRED */
#if CONFIG_EXT_RESOLVER
/*
* Interface to external identity resolver.
*
* The architecture of the interface is simple; the external resolver calls
* in to get work, then calls back with completed work. It also calls us
* to let us know that it's (re)started, so that we can resubmit work if it
* times out.
*/
static LCK_MTX_DECLARE(kauth_resolver_mtx, &kauth_lck_grp);
#define KAUTH_RESOLVER_LOCK() lck_mtx_lock(&kauth_resolver_mtx);
#define KAUTH_RESOLVER_UNLOCK() lck_mtx_unlock(&kauth_resolver_mtx);
static volatile pid_t kauth_resolver_identity;
static int kauth_identitysvc_has_registered;
static int kauth_resolver_registered;
static uint32_t kauth_resolver_sequence = 31337;
static int kauth_resolver_timeout = 30; /* default: 30 seconds */
struct kauth_resolver_work {
TAILQ_ENTRY(kauth_resolver_work) kr_link;
struct kauth_identity_extlookup kr_work;
uint64_t kr_extend;
uint32_t kr_seqno;
int kr_refs;
int kr_flags;
#define KAUTH_REQUEST_UNSUBMITTED (1<<0)
#define KAUTH_REQUEST_SUBMITTED (1<<1)
#define KAUTH_REQUEST_DONE (1<<2)
int kr_result;
};
TAILQ_HEAD(kauth_resolver_unsubmitted_head, kauth_resolver_work) kauth_resolver_unsubmitted =
TAILQ_HEAD_INITIALIZER(kauth_resolver_unsubmitted);
TAILQ_HEAD(kauth_resolver_submitted_head, kauth_resolver_work) kauth_resolver_submitted =
TAILQ_HEAD_INITIALIZER(kauth_resolver_submitted);
TAILQ_HEAD(kauth_resolver_done_head, kauth_resolver_work) kauth_resolver_done =
TAILQ_HEAD_INITIALIZER(kauth_resolver_done);
/* Number of resolver timeouts between logged complaints */
#define KAUTH_COMPLAINT_INTERVAL 1000
int kauth_resolver_timeout_cnt = 0;
#if DEVELOPMENT || DEBUG
/* Internal builds get different (less ambiguous) breadcrumbs. */
#define KAUTH_RESOLVER_FAILED_ERRCODE EOWNERDEAD
#else
/* But non-Internal builds get errors that are allowed by standards. */
#define KAUTH_RESOLVER_FAILED_ERRCODE EIO
#endif /* DEVELOPMENT || DEBUG */
int kauth_resolver_failed_cnt = 0;
#define RESOLVER_FAILED_MESSAGE(fmt, args...) \
do { \
if (!(kauth_resolver_failed_cnt++ % 100)) { \
printf("%s: " fmt "\n", __PRETTY_FUNCTION__, ##args); \
} \
} while (0)
static int kauth_resolver_submit(struct kauth_identity_extlookup *lkp, uint64_t extend_data);
static int kauth_resolver_complete(user_addr_t message);
static int kauth_resolver_getwork(user_addr_t message);
static int kauth_resolver_getwork2(user_addr_t message);
static __attribute__((noinline)) int __KERNEL_IS_WAITING_ON_EXTERNAL_CREDENTIAL_RESOLVER__(
struct kauth_resolver_work *);
#define KAUTH_CACHES_MAX_SIZE 10000 /* Max # entries for both groups and id caches */
struct kauth_identity {
TAILQ_ENTRY(kauth_identity) ki_link;
int ki_valid;
uid_t ki_uid;
gid_t ki_gid;
uint32_t ki_supgrpcnt;
gid_t ki_supgrps[NGROUPS];
guid_t ki_guid;
ntsid_t ki_ntsid;
const char *ki_name; /* string name from string cache */
/*
* Expiry times are the earliest time at which we will disregard the
* cached state and go to userland. Before then if the valid bit is
* set, we will return the cached value. If it's not set, we will
* not go to userland to resolve, just assume that there is no answer
* available.
*/
time_t ki_groups_expiry;
time_t ki_guid_expiry;
time_t ki_ntsid_expiry;
};
static TAILQ_HEAD(kauth_identity_head, kauth_identity) kauth_identities =
TAILQ_HEAD_INITIALIZER(kauth_identities);
static LCK_MTX_DECLARE(kauth_identity_mtx, &kauth_lck_grp);
#define KAUTH_IDENTITY_LOCK() lck_mtx_lock(&kauth_identity_mtx);
#define KAUTH_IDENTITY_UNLOCK() lck_mtx_unlock(&kauth_identity_mtx);
#define KAUTH_IDENTITY_CACHEMAX_DEFAULT 100 /* XXX default sizing? */
static int kauth_identity_cachemax = KAUTH_IDENTITY_CACHEMAX_DEFAULT;
static int kauth_identity_count;
static struct kauth_identity *kauth_identity_alloc(uid_t uid, gid_t gid, guid_t *guidp, time_t guid_expiry,
ntsid_t *ntsidp, time_t ntsid_expiry, size_t supgrpcnt, gid_t *supgrps, time_t groups_expiry,
const char *name, int nametype);
static void kauth_identity_register_and_free(struct kauth_identity *kip);
static void kauth_identity_updatecache(struct kauth_identity_extlookup *elp, struct kauth_identity *kip, uint64_t extend_data);
static void kauth_identity_trimcache(int newsize);
static void kauth_identity_lru(struct kauth_identity *kip);
static int kauth_identity_guid_expired(struct kauth_identity *kip);
static int kauth_identity_ntsid_expired(struct kauth_identity *kip);
static int kauth_identity_find_uid(uid_t uid, struct kauth_identity *kir, char *getname);
static int kauth_identity_find_gid(gid_t gid, struct kauth_identity *kir, char *getname);
static int kauth_identity_find_guid(guid_t *guidp, struct kauth_identity *kir, char *getname);
static int kauth_identity_find_ntsid(ntsid_t *ntsid, struct kauth_identity *kir, char *getname);
static int kauth_identity_find_nam(char *name, int valid, struct kauth_identity *kir);
struct kauth_group_membership {
TAILQ_ENTRY(kauth_group_membership) gm_link;
uid_t gm_uid; /* the identity whose membership we're recording */
gid_t gm_gid; /* group of which they are a member */
time_t gm_expiry; /* TTL for the membership, or 0 for persistent entries */
int gm_flags;
#define KAUTH_GROUP_ISMEMBER (1<<0)
};
TAILQ_HEAD(kauth_groups_head, kauth_group_membership) kauth_groups =
TAILQ_HEAD_INITIALIZER(kauth_groups);
static LCK_MTX_DECLARE(kauth_groups_mtx, &kauth_lck_grp);
#define KAUTH_GROUPS_LOCK() lck_mtx_lock(&kauth_groups_mtx);
#define KAUTH_GROUPS_UNLOCK() lck_mtx_unlock(&kauth_groups_mtx);
#define KAUTH_GROUPS_CACHEMAX_DEFAULT 100 /* XXX default sizing? */
static int kauth_groups_cachemax = KAUTH_GROUPS_CACHEMAX_DEFAULT;
static int kauth_groups_count;
static int kauth_groups_expired(struct kauth_group_membership *gm);
static void kauth_groups_lru(struct kauth_group_membership *gm);
static void kauth_groups_updatecache(struct kauth_identity_extlookup *el);
static void kauth_groups_trimcache(int newsize);
#endif /* CONFIG_EXT_RESOLVER */
#define KAUTH_CRED_TABLE_SIZE 128
ZONE_DECLARE(ucred_zone, "cred", sizeof(struct ucred), ZC_ZFREE_CLEARMEM);
LIST_HEAD(kauth_cred_entry_head, ucred);
static struct kauth_cred_entry_head
kauth_cred_table_anchor[KAUTH_CRED_TABLE_SIZE];
static struct kauth_cred_entry_head *kauth_cred_get_bucket(kauth_cred_t cred);
static kauth_cred_t kauth_cred_add(kauth_cred_t new_cred, struct kauth_cred_entry_head *bucket);
static void kauth_cred_remove_locked(kauth_cred_t cred);
static kauth_cred_t kauth_cred_update(kauth_cred_t old_cred, kauth_cred_t new_cred, boolean_t retain_auditinfo);
static kauth_cred_t kauth_cred_find_and_ref(kauth_cred_t cred,
struct kauth_cred_entry_head *bucket);
static bool kauth_cred_is_equal(kauth_cred_t cred1, kauth_cred_t cred2);
#if CONFIG_EXT_RESOLVER
/*
* __KERNEL_IS_WAITING_ON_EXTERNAL_CREDENTIAL_RESOLVER__
*
* Description: Waits for the user space daemon to respond to the request
* we made. Function declared non inline to be visible in
* stackshots and spindumps as well as debugging.
*
* Parameters: workp Work queue entry.
*
* Returns: 0 on Success.
* EIO if Resolver is dead.
* EINTR thread interrupted in msleep
* EWOULDBLOCK thread timed out in msleep
* ERESTART returned by msleep.
*
*/
static __attribute__((noinline)) int
__KERNEL_IS_WAITING_ON_EXTERNAL_CREDENTIAL_RESOLVER__(
struct kauth_resolver_work *workp)
{
int error = 0;
struct timespec ts;
for (;;) {
/* we could compute a better timeout here */
ts.tv_sec = kauth_resolver_timeout;
ts.tv_nsec = 0;
error = msleep(workp, &kauth_resolver_mtx, PCATCH, "kr_submit", &ts);
/* request has been completed? */
if ((error == 0) && (workp->kr_flags & KAUTH_REQUEST_DONE)) {
break;
}
/* woken because the resolver has died? */
if (kauth_resolver_identity == 0) {
RESOLVER_FAILED_MESSAGE("kauth external resolver died while while waiting for work to complete");
error = KAUTH_RESOLVER_FAILED_ERRCODE;
break;
}
/* an error? */
if (error != 0) {
break;
}
}
return error;
}
/*
* kauth_resolver_identity_reset
*
* Description: Reset the identity of the external resolver in certain
* controlled circumstances.
*
* Parameters: None.
*
* Returns: Nothing.
*/
void
kauth_resolver_identity_reset(void)
{
KAUTH_RESOLVER_LOCK();
if (kauth_resolver_identity != 0) {
printf("kauth external resolver %d failed to de-register.\n",
kauth_resolver_identity);
kauth_resolver_identity = 0;
kauth_resolver_registered = 0;
}
KAUTH_RESOLVER_UNLOCK();
}
/*
* kauth_resolver_submit
*
* Description: Submit an external credential identity resolution request to
* the user space daemon.
*
* Parameters: lkp A pointer to an external
* lookup request
* extend_data extended data for kr_extend
*
* Returns: 0 Success
* EWOULDBLOCK No resolver registered
* EINTR Operation interrupted (e.g. by
* a signal)
* ENOMEM Could not allocate work item
* copyinstr:EFAULT Bad message from user space
* workp->kr_result:??? An error from the user space
* daemon (includes ENOENT!)
*
* Implicit returns:
* *lkp Modified
*
* Notes: Allocate a work queue entry, submit the work and wait for
* the operation to either complete or time out. Outstanding
* operations may also be cancelled.
*
* Submission is by means of placing the item on a work queue
* which is serviced by an external resolver thread calling
* into the kernel. The caller then sleeps until timeout,
* cancellation, or an external resolver thread calls in with
* a result message to kauth_resolver_complete(). All of these
* events wake the caller back up.
*
* This code is called from either kauth_cred_ismember_gid()
* for a group membership request, or it is called from
* kauth_cred_cache_lookup() when we get a cache miss.
*/
static int
kauth_resolver_submit(struct kauth_identity_extlookup *lkp, uint64_t extend_data)
{
struct kauth_resolver_work *workp, *killp;
struct timespec ts;
int error, shouldfree;
/* no point actually blocking if the resolver isn't up yet */
if (kauth_resolver_identity == 0) {
/*
* We've already waited an initial <kauth_resolver_timeout>
* seconds with no result.
*
* Sleep on a stack address so no one wakes us before timeout;
* we sleep a half a second in case we are a high priority
* process, so that memberd doesn't starve while we are in a
* tight loop between user and kernel, eating all the CPU.
*/
error = tsleep(&ts, PZERO | PCATCH, "kr_submit", hz / 2);
if (kauth_resolver_identity == 0) {
/*
* if things haven't changed while we were asleep,
* tell the caller we couldn't get an authoritative
* answer.
*/
return EWOULDBLOCK;
}
}
workp = kheap_alloc(KM_KAUTH, sizeof(struct kauth_resolver_work),
Z_WAITOK);
if (workp == NULL) {
return ENOMEM;
}
workp->kr_work = *lkp;
workp->kr_extend = extend_data;
workp->kr_refs = 1;
workp->kr_flags = KAUTH_REQUEST_UNSUBMITTED;
workp->kr_result = 0;
/*
* We insert the request onto the unsubmitted queue, the call in from
* the resolver will it to the submitted thread when appropriate.
*/
KAUTH_RESOLVER_LOCK();
workp->kr_seqno = workp->kr_work.el_seqno = kauth_resolver_sequence++;
workp->kr_work.el_result = KAUTH_EXTLOOKUP_INPROG;
/*
* XXX We *MUST NOT* attempt to coalesce identical work items due to
* XXX the inability to ensure order of update of the request item
* XXX extended data vs. the wakeup; instead, we let whoever is waiting
* XXX for each item repeat the update when they wake up.
*/
TAILQ_INSERT_TAIL(&kauth_resolver_unsubmitted, workp, kr_link);
/*
* Wake up an external resolver thread to deal with the new work; one
* may not be available, and if not, then the request will be grabbed
* when a resolver thread comes back into the kernel to request new
* work.
*/
wakeup_one((caddr_t)&kauth_resolver_unsubmitted);
error = __KERNEL_IS_WAITING_ON_EXTERNAL_CREDENTIAL_RESOLVER__(workp);
/* if the request was processed, copy the result */
if (error == 0) {
*lkp = workp->kr_work;
}
if (error == EWOULDBLOCK) {
if ((kauth_resolver_timeout_cnt++ % KAUTH_COMPLAINT_INTERVAL) == 0) {
printf("kauth external resolver timed out (%d timeout(s) of %d seconds).\n",
kauth_resolver_timeout_cnt, kauth_resolver_timeout);
}
if (workp->kr_flags & KAUTH_REQUEST_UNSUBMITTED) {
/*
* If the request timed out and was never collected, the resolver
* is dead and probably not coming back anytime soon. In this
* case we revert to no-resolver behaviour, and punt all the other
* sleeping requests to clear the backlog.
*/
KAUTH_DEBUG("RESOLVER - request timed out without being collected for processing, resolver dead");
/*
* Make the current resolver non-authoritative, and mark it as
* no longer registered to prevent kauth_cred_ismember_gid()
* enqueueing more work until a new one is registered. This
* mitigates the damage a crashing resolver may inflict.
*/
kauth_resolver_identity = 0;
kauth_resolver_registered = 0;
/* kill all the other requestes that are waiting as well */
TAILQ_FOREACH(killp, &kauth_resolver_submitted, kr_link)
wakeup(killp);
TAILQ_FOREACH(killp, &kauth_resolver_unsubmitted, kr_link)
wakeup(killp);
/* Cause all waiting-for-work threads to return EIO */
wakeup((caddr_t)&kauth_resolver_unsubmitted);
}
}
/*
* drop our reference on the work item, and note whether we should
* free it or not
*/
if (--workp->kr_refs <= 0) {
/* work out which list we have to remove it from */
if (workp->kr_flags & KAUTH_REQUEST_DONE) {
TAILQ_REMOVE(&kauth_resolver_done, workp, kr_link);
} else if (workp->kr_flags & KAUTH_REQUEST_SUBMITTED) {
TAILQ_REMOVE(&kauth_resolver_submitted, workp, kr_link);
} else if (workp->kr_flags & KAUTH_REQUEST_UNSUBMITTED) {
TAILQ_REMOVE(&kauth_resolver_unsubmitted, workp, kr_link);
} else {
KAUTH_DEBUG("RESOLVER - completed request has no valid queue");
}
shouldfree = 1;
} else {
/* someone else still has a reference on this request */
shouldfree = 0;
}
/* collect request result */
if (error == 0) {
error = workp->kr_result;
}
KAUTH_RESOLVER_UNLOCK();
/*
* If we dropped the last reference, free the request.
*/
if (shouldfree) {
kheap_free(KM_KAUTH, workp, sizeof(struct kauth_resolver_work));
}
KAUTH_DEBUG("RESOLVER - returning %d", error);
return error;
}
/*
* identitysvc
*
* Description: System call interface for the external identity resolver.
*
* Parameters: uap->message Message from daemon to kernel
*
* Returns: 0 Successfully became resolver
* EPERM Not the resolver process
* kauth_authorize_generic:EPERM Not root user
* kauth_resolver_complete:EIO
* kauth_resolver_complete:EFAULT
* kauth_resolver_getwork:EINTR
* kauth_resolver_getwork:EFAULT
*
* Notes: This system call blocks until there is work enqueued, at
* which time the kernel wakes it up, and a message from the
* kernel is copied out to the identity resolution daemon, which
* proceed to attempt to resolve it. When the resolution has
* completed (successfully or not), the daemon called back into
* this system call to give the result to the kernel, and wait
* for the next request.
*/
int
identitysvc(__unused struct proc *p, struct identitysvc_args *uap, __unused int32_t *retval)
{
int opcode = uap->opcode;
user_addr_t message = uap->message;
struct kauth_resolver_work *workp;
struct kauth_cache_sizes sz_arg = {};
int error;
pid_t new_id;
if (!IOTaskHasEntitlement(current_task(), IDENTITYSVC_ENTITLEMENT)) {
KAUTH_DEBUG("RESOLVER - pid %d not entitled to call identitysvc", current_proc()->p_pid);
return EPERM;
}
/*
* New server registering itself.
*/
if (opcode == KAUTH_EXTLOOKUP_REGISTER) {
new_id = current_proc()->p_pid;
if ((error = kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER)) != 0) {
KAUTH_DEBUG("RESOLVER - pid %d refused permission to become identity resolver", new_id);
return error;
}
KAUTH_RESOLVER_LOCK();
if (kauth_resolver_identity != new_id) {
KAUTH_DEBUG("RESOLVER - new resolver %d taking over from old %d", new_id, kauth_resolver_identity);
/*
* We have a new server, so assume that all the old requests have been lost.
*/
while ((workp = TAILQ_LAST(&kauth_resolver_submitted, kauth_resolver_submitted_head)) != NULL) {
TAILQ_REMOVE(&kauth_resolver_submitted, workp, kr_link);
workp->kr_flags &= ~KAUTH_REQUEST_SUBMITTED;
workp->kr_flags |= KAUTH_REQUEST_UNSUBMITTED;
TAILQ_INSERT_HEAD(&kauth_resolver_unsubmitted, workp, kr_link);
}
/*
* Allow user space resolver to override the
* external resolution timeout
*/
if (message > 30 && message < 10000) {
kauth_resolver_timeout = (int)message;
KAUTH_DEBUG("RESOLVER - new resolver changes timeout to %d seconds\n", (int)message);
}
kauth_resolver_identity = new_id;
kauth_resolver_registered = 1;
kauth_identitysvc_has_registered = 1;
wakeup(&kauth_resolver_unsubmitted);
}
KAUTH_RESOLVER_UNLOCK();
return 0;
}
/*
* Beyond this point, we must be the resolver process. We verify this
* by confirming the resolver credential and pid.
*/
if ((kauth_cred_getuid(kauth_cred_get()) != 0) || (current_proc()->p_pid != kauth_resolver_identity)) {
KAUTH_DEBUG("RESOLVER - call from bogus resolver %d\n", current_proc()->p_pid);
return EPERM;
}
if (opcode == KAUTH_GET_CACHE_SIZES) {
KAUTH_IDENTITY_LOCK();
sz_arg.kcs_id_size = kauth_identity_cachemax;
KAUTH_IDENTITY_UNLOCK();
KAUTH_GROUPS_LOCK();
sz_arg.kcs_group_size = kauth_groups_cachemax;
KAUTH_GROUPS_UNLOCK();
if ((error = copyout(&sz_arg, uap->message, sizeof(sz_arg))) != 0) {
return error;
}
return 0;
} else if (opcode == KAUTH_SET_CACHE_SIZES) {
if ((error = copyin(uap->message, &sz_arg, sizeof(sz_arg))) != 0) {
return error;
}
if ((sz_arg.kcs_group_size > KAUTH_CACHES_MAX_SIZE) ||
(sz_arg.kcs_id_size > KAUTH_CACHES_MAX_SIZE)) {
return EINVAL;
}
KAUTH_IDENTITY_LOCK();
kauth_identity_cachemax = sz_arg.kcs_id_size;
kauth_identity_trimcache(kauth_identity_cachemax);
KAUTH_IDENTITY_UNLOCK();
KAUTH_GROUPS_LOCK();
kauth_groups_cachemax = sz_arg.kcs_group_size;
kauth_groups_trimcache(kauth_groups_cachemax);
KAUTH_GROUPS_UNLOCK();
return 0;
} else if (opcode == KAUTH_CLEAR_CACHES) {
KAUTH_IDENTITY_LOCK();
kauth_identity_trimcache(0);
KAUTH_IDENTITY_UNLOCK();
KAUTH_GROUPS_LOCK();
kauth_groups_trimcache(0);
KAUTH_GROUPS_UNLOCK();
} else if (opcode == KAUTH_EXTLOOKUP_DEREGISTER) {
/*
* Terminate outstanding requests; without an authoritative
* resolver, we are now back on our own authority.
*/
struct kauth_resolver_work *killp;
KAUTH_RESOLVER_LOCK();
/*
* Clear the identity, but also mark it as unregistered so
* there is no explicit future expectation of us getting a
* new resolver any time soon.
*/
kauth_resolver_identity = 0;
kauth_resolver_registered = 0;
TAILQ_FOREACH(killp, &kauth_resolver_submitted, kr_link)
wakeup(killp);
TAILQ_FOREACH(killp, &kauth_resolver_unsubmitted, kr_link)
wakeup(killp);
/* Cause all waiting-for-work threads to return EIO */
wakeup((caddr_t)&kauth_resolver_unsubmitted);
KAUTH_RESOLVER_UNLOCK();
}
/*
* Got a result returning?
*/
if (opcode & KAUTH_EXTLOOKUP_RESULT) {
if ((error = kauth_resolver_complete(message)) != 0) {
return error;
}
}
/*
* Caller wants to take more work?
*/
if (opcode & KAUTH_EXTLOOKUP_WORKER) {
if ((error = kauth_resolver_getwork(message)) != 0) {
return error;
}
}
return 0;
}
/*
* kauth_resolver_getwork_continue
*
* Description: Continuation for kauth_resolver_getwork
*
* Parameters: result Error code or 0 for the sleep
* that got us to this function
*
* Returns: 0 Success
* EINTR Interrupted (e.g. by signal)
* kauth_resolver_getwork2:EFAULT
*
* Notes: See kauth_resolver_getwork(0 and kauth_resolver_getwork2() for
* more information.
*/
static int
kauth_resolver_getwork_continue(int result)
{
thread_t thread;
struct uthread *ut;
user_addr_t message;
if (result) {
KAUTH_RESOLVER_UNLOCK();
return result;
}
/*
* If we lost a race with another thread/memberd restarting, then we
* need to go back to sleep to look for more work. If it was memberd
* restarting, then the msleep0() will error out here, as our thread
* will already be "dead".
*/
if (TAILQ_FIRST(&kauth_resolver_unsubmitted) == NULL) {
int error;
error = msleep0(&kauth_resolver_unsubmitted, &kauth_resolver_mtx, PCATCH, "GRGetWork", 0, kauth_resolver_getwork_continue);
/*
* If this is a wakeup from another thread in the resolver
* deregistering it, error out the request-for-work thread
*/
if (!kauth_resolver_identity) {
RESOLVER_FAILED_MESSAGE("external resolver died");
error = KAUTH_RESOLVER_FAILED_ERRCODE;
}
KAUTH_RESOLVER_UNLOCK();
return error;
}
thread = current_thread();
ut = get_bsdthread_info(thread);
message = ut->uu_save.uus_kauth.message;
return kauth_resolver_getwork2(message);
}
/*
* kauth_resolver_getwork2
*
* Decription: Common utility function to copy out a identity resolver work
* item from the kernel to user space as part of the user space
* identity resolver requesting work.
*
* Parameters: message message to user space
*
* Returns: 0 Success
* EFAULT Bad user space message address
*
* Notes: This common function exists to permit the use of continuations
* in the identity resolution process. This frees up the stack
* while we are waiting for the user space resolver to complete
* a request. This is specifically used so that our per thread
* cost can be small, and we will therefore be willing to run a
* larger number of threads in the user space identity resolver.
*/
static int
kauth_resolver_getwork2(user_addr_t message)
{
struct kauth_resolver_work *workp;
int error;
/*
* Note: We depend on the caller protecting us from a NULL work item
* queue, since we must have the kauth resolver lock on entry to this
* function.
*/
workp = TAILQ_FIRST(&kauth_resolver_unsubmitted);
/*
* Copy out the external lookup structure for the request, not
* including the el_extend field, which contains the address of the
* external buffer provided by the external resolver into which we
* copy the extension request information.
*/
/* BEFORE FIELD */
if ((error = copyout(&workp->kr_work, message, offsetof(struct kauth_identity_extlookup, el_extend))) != 0) {
KAUTH_DEBUG("RESOLVER - error submitting work to resolve");
goto out;
}
/* AFTER FIELD */
if ((error = copyout(&workp->kr_work.el_info_reserved_1,
message + offsetof(struct kauth_identity_extlookup, el_info_reserved_1),
sizeof(struct kauth_identity_extlookup) - offsetof(struct kauth_identity_extlookup, el_info_reserved_1))) != 0) {
KAUTH_DEBUG("RESOLVER - error submitting work to resolve");
goto out;
}
/*
* Handle extended requests here; if we have a request of a type where
* the kernel wants a translation of extended information, then we need
* to copy it out into the extended buffer, assuming the buffer is
* valid; we only attempt to get the buffer address if we have request
* data to copy into it.
*/
/*
* translate a user@domain string into a uid/gid/whatever
*/
if (workp->kr_work.el_flags & (KAUTH_EXTLOOKUP_VALID_PWNAM | KAUTH_EXTLOOKUP_VALID_GRNAM)) {
uint64_t uaddr;
error = copyin(message + offsetof(struct kauth_identity_extlookup, el_extend), &uaddr, sizeof(uaddr));
if (!error) {
size_t actual; /* not used */
/*
* Use copyoutstr() to reduce the copy size; we let
* this catch a NULL uaddr because we shouldn't be
* asking in that case anyway.
*/
error = copyoutstr(CAST_DOWN(void *, workp->kr_extend), uaddr, MAXPATHLEN, &actual);
}
if (error) {
KAUTH_DEBUG("RESOLVER - error submitting work to resolve");
goto out;
}
}
TAILQ_REMOVE(&kauth_resolver_unsubmitted, workp, kr_link);
workp->kr_flags &= ~KAUTH_REQUEST_UNSUBMITTED;
workp->kr_flags |= KAUTH_REQUEST_SUBMITTED;
TAILQ_INSERT_TAIL(&kauth_resolver_submitted, workp, kr_link);
out:
KAUTH_RESOLVER_UNLOCK();
return error;
}
/*
* kauth_resolver_getwork
*
* Description: Get a work item from the enqueued requests from the kernel and
* give it to the user space daemon.
*
* Parameters: message message to user space
*
* Returns: 0 Success
* EINTR Interrupted (e.g. by signal)
* kauth_resolver_getwork2:EFAULT
*
* Notes: This function blocks in a continuation if there are no work
* items available for processing at the time the user space
* identity resolution daemon makes a request for work. This
* permits a large number of threads to be used by the daemon,
* without using a lot of wired kernel memory when there are no
* actual request outstanding.
*/
static int
kauth_resolver_getwork(user_addr_t message)
{
struct kauth_resolver_work *workp;
int error;
KAUTH_RESOLVER_LOCK();
error = 0;
while ((workp = TAILQ_FIRST(&kauth_resolver_unsubmitted)) == NULL) {
thread_t thread = current_thread();
struct uthread *ut = get_bsdthread_info(thread);
ut->uu_save.uus_kauth.message = message;
error = msleep0(&kauth_resolver_unsubmitted, &kauth_resolver_mtx, PCATCH, "GRGetWork", 0, kauth_resolver_getwork_continue);
KAUTH_RESOLVER_UNLOCK();
/*
* If this is a wakeup from another thread in the resolver
* deregistering it, error out the request-for-work thread
*/
if (!kauth_resolver_identity) {
printf("external resolver died");
error = KAUTH_RESOLVER_FAILED_ERRCODE;
}
return error;
}
return kauth_resolver_getwork2(message);
}
/*
* kauth_resolver_complete
*
* Description: Return a result from userspace.
*
* Parameters: message message from user space
*
* Returns: 0 Success
* EIO The resolver is dead
* copyin:EFAULT Bad message from user space
*/
static int
kauth_resolver_complete(user_addr_t message)
{
struct kauth_identity_extlookup extl;
struct kauth_resolver_work *workp;
struct kauth_resolver_work *killp;
int error, result, want_extend_data;
/*
* Copy in the mesage, including the extension field, since we are
* copying into a local variable.
*/
if ((error = copyin(message, &extl, sizeof(extl))) != 0) {
KAUTH_DEBUG("RESOLVER - error getting completed work\n");
return error;
}
KAUTH_RESOLVER_LOCK();
error = 0;
result = 0;
switch (extl.el_result) {
case KAUTH_EXTLOOKUP_INPROG:
{
static int once = 0;
/* XXX this should go away once memberd is updated */
if (!once) {
printf("kauth_resolver: memberd is not setting valid result codes (assuming always successful)\n");
once = 1;
}
}
OS_FALLTHROUGH;
case KAUTH_EXTLOOKUP_SUCCESS:
break;
case KAUTH_EXTLOOKUP_FATAL:
/* fatal error means the resolver is dead */
KAUTH_DEBUG("RESOLVER - resolver %d died, waiting for a new one", kauth_resolver_identity);
RESOLVER_FAILED_MESSAGE("resolver %d died, waiting for a new one", kauth_resolver_identity);
/*
* Terminate outstanding requests; without an authoritative
* resolver, we are now back on our own authority. Tag the
* resolver unregistered to prevent kauth_cred_ismember_gid()
* enqueueing more work until a new one is registered. This
* mitigates the damage a crashing resolver may inflict.
*/
kauth_resolver_identity = 0;
kauth_resolver_registered = 0;
TAILQ_FOREACH(killp, &kauth_resolver_submitted, kr_link)
wakeup(killp);
TAILQ_FOREACH(killp, &kauth_resolver_unsubmitted, kr_link)
wakeup(killp);
/* Cause all waiting-for-work threads to return EIO */
wakeup((caddr_t)&kauth_resolver_unsubmitted);
/* and return EIO to the caller */
error = KAUTH_RESOLVER_FAILED_ERRCODE;
break;
case KAUTH_EXTLOOKUP_BADRQ:
KAUTH_DEBUG("RESOLVER - resolver reported invalid request %d", extl.el_seqno);
result = EINVAL;
break;
case KAUTH_EXTLOOKUP_FAILURE:
KAUTH_DEBUG("RESOLVER - resolver reported transient failure for request %d", extl.el_seqno);
RESOLVER_FAILED_MESSAGE("resolver reported transient failure for request %d", extl.el_seqno);
result = KAUTH_RESOLVER_FAILED_ERRCODE;
break;
default:
KAUTH_DEBUG("RESOLVER - resolver returned unexpected status %d", extl.el_result);
RESOLVER_FAILED_MESSAGE("resolver returned unexpected status %d", extl.el_result);
result = KAUTH_RESOLVER_FAILED_ERRCODE;
break;
}
/*
* In the case of a fatal error, we assume that the resolver will
* restart quickly and re-collect all of the outstanding requests.
* Thus, we don't complete the request which returned the fatal
* error status.
*/
if (extl.el_result != KAUTH_EXTLOOKUP_FATAL) {
/* scan our list for this request */
TAILQ_FOREACH(workp, &kauth_resolver_submitted, kr_link) {
/* found it? */
if (workp->kr_seqno == extl.el_seqno) {
/*
* Do we want extend_data?
*/