forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac_base.c
2029 lines (1741 loc) · 53.2 KB
/
mac_base.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) 2007-2016 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@
*/
/*-
* Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
* Copyright (c) 2001 Ilmar S. Habibulin
* Copyright (c) 2001, 2002, 2003, 2004 Networks Associates Technology, Inc.
* Copyright (c) 2005-2006 SPARTA, Inc.
*
* This software was developed by Robert Watson and Ilmar Habibulin for the
* TrustedBSD Project.
*
* This software was developed for the FreeBSD Project in part by Network
* Associates Laboratories, the Security Research Division of Network
* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
* as part of the DARPA CHATS research program.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*-
* Framework for extensible kernel access control. This file contains
* Kernel and userland interface to the framework, policy registration
* and composition. Per-object interfaces, controls, and labeling may be
* found in src/sys/mac/. Sample policies may be found in src/sys/mac*.
*/
#include <stdarg.h>
#include <string.h>
#include <security/mac_internal.h>
#include <security/mac_mach_internal.h>
#include <sys/param.h>
#include <sys/vnode.h>
#include <sys/vnode_internal.h>
#include <sys/vfs_context.h>
#include <sys/namei.h>
#include <bsd/bsm/audit.h>
#include <bsd/security/audit/audit.h>
#include <sys/file.h>
#include <sys/file_internal.h>
#include <sys/filedesc.h>
#include <sys/proc.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/sysproto.h>
#include <mach/exception_types.h>
#include <mach/vm_types.h>
#include <mach/vm_prot.h>
#include <kern/zalloc.h>
#include <kern/sched_prim.h>
#include <osfmk/kern/task.h>
#include <osfmk/kern/kalloc.h>
#if CONFIG_MACF
#include <security/mac.h>
#include <security/mac_policy.h>
#include <security/mac_framework.h>
#include <security/mac_internal.h>
#include <security/mac_mach_internal.h>
#endif
#if CONFIG_EMBEDDED
#include <libkern/section_keywords.h>
#endif
/*
* define MB_DEBUG to display run-time debugging information
* #define MB_DEBUG 1
*/
#ifdef MB_DEBUG
#define DPRINTF(x) printf x
#else
#define MB_DEBUG
#define DPRINTF(x)
#endif
#if CONFIG_MACF
SYSCTL_NODE(, OID_AUTO, security, CTLFLAG_RW|CTLFLAG_LOCKED, 0,
"Security Controls");
SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW|CTLFLAG_LOCKED, 0,
"TrustedBSD MAC policy controls");
/*
* Declare that the kernel provides MAC support, version 1. This permits
* modules to refuse to be loaded if the necessary support isn't present,
* even if it's pre-boot.
*/
#if 0
MODULE_VERSION(kernel_mac_support, 1);
#endif
#if MAC_MAX_SLOTS > 32
#error "MAC_MAX_SLOTS too large"
#endif
static unsigned int mac_max_slots = MAC_MAX_SLOTS;
static unsigned int mac_slot_offsets_free = (1 << MAC_MAX_SLOTS) - 1;
SYSCTL_UINT(_security_mac, OID_AUTO, max_slots, CTLFLAG_RD | CTLFLAG_LOCKED,
&mac_max_slots, 0, "");
/*
* Has the kernel started generating labeled objects yet? All read/write
* access to this variable is serialized during the boot process. Following
* the end of serialization, we don't update this flag; no locking.
*/
int mac_late = 0;
/*
* Flag to indicate whether or not we should allocate label storage for
* new mbufs. Since most dynamic policies we currently work with don't
* rely on mbuf labeling, try to avoid paying the cost of mtag allocation
* unless specifically notified of interest. One result of this is
* that if a dynamically loaded policy requests mbuf labels, it must
* be able to deal with a NULL label being returned on any mbufs that
* were already in flight when the policy was loaded. Since the policy
* already has to deal with uninitialized labels, this probably won't
* be a problem. Note: currently no locking. Will this be a problem?
*/
#if CONFIG_MACF_NET
unsigned int mac_label_mbufs = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, label_mbufs, SECURITY_MAC_CTLFLAGS,
&mac_label_mbufs, 0, "Label all MBUFs");
#endif
/*
* Flag to indicate whether or not we should allocate label storage for
* new vnodes. Since most dynamic policies we currently work with don't
* rely on vnode labeling, try to avoid paying the cost of mtag allocation
* unless specifically notified of interest. One result of this is
* that if a dynamically loaded policy requests vnode labels, it must
* be able to deal with a NULL label being returned on any vnodes that
* were already in flight when the policy was loaded. Since the policy
* already has to deal with uninitialized labels, this probably won't
* be a problem.
*/
unsigned int mac_label_vnodes = 0;
SYSCTL_UINT(_security_mac, OID_AUTO, labelvnodes, SECURITY_MAC_CTLFLAGS,
&mac_label_vnodes, 0, "Label all vnodes");
unsigned int mac_device_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, device_enforce, SECURITY_MAC_CTLFLAGS,
&mac_device_enforce, 0, "Enforce MAC policy on device operations");
unsigned int mac_pipe_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, pipe_enforce, SECURITY_MAC_CTLFLAGS,
&mac_pipe_enforce, 0, "Enforce MAC policy on pipe operations");
unsigned int mac_posixsem_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, posixsem_enforce, SECURITY_MAC_CTLFLAGS,
&mac_posixsem_enforce, 0, "Enforce MAC policy on POSIX semaphores");
unsigned int mac_posixshm_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, posixshm_enforce, SECURITY_MAC_CTLFLAGS,
&mac_posixshm_enforce, 0, "Enforce MAC policy on Posix Shared Memory");
unsigned int mac_proc_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, proc_enforce, SECURITY_MAC_CTLFLAGS,
&mac_proc_enforce, 0, "Enforce MAC policy on process operations");
unsigned int mac_socket_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, socket_enforce, SECURITY_MAC_CTLFLAGS,
&mac_socket_enforce, 0, "Enforce MAC policy on socket operations");
unsigned int mac_system_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, system_enforce, SECURITY_MAC_CTLFLAGS,
&mac_system_enforce, 0, "Enforce MAC policy on system-wide interfaces");
unsigned int mac_sysvmsg_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, sysvmsg_enforce, SECURITY_MAC_CTLFLAGS,
&mac_sysvmsg_enforce, 0, "Enforce MAC policy on System V IPC message queues");
unsigned int mac_sysvsem_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, sysvsem_enforce, SECURITY_MAC_CTLFLAGS,
&mac_sysvsem_enforce, 0, "Enforce MAC policy on System V IPC semaphores");
unsigned int mac_sysvshm_enforce = 1;
SYSCTL_INT(_security_mac, OID_AUTO, sysvshm_enforce, SECURITY_MAC_CTLFLAGS,
&mac_sysvshm_enforce, 0, "Enforce MAC policy on System V Shared Memory");
unsigned int mac_vm_enforce = 1;
SYSCTL_INT(_security_mac, OID_AUTO, vm_enforce, SECURITY_MAC_CTLFLAGS,
&mac_vm_enforce, 0, "Enforce MAC policy on VM operations");
unsigned int mac_vnode_enforce = 1;
SYSCTL_UINT(_security_mac, OID_AUTO, vnode_enforce, SECURITY_MAC_CTLFLAGS,
&mac_vnode_enforce, 0, "Enforce MAC policy on vnode operations");
#if CONFIG_AUDIT
/*
* mac_audit_data_zone is the zone used for data pushed into the audit
* record by policies. Using a zone simplifies memory management of this
* data, and allows tracking of the amount of data in flight.
*/
extern zone_t mac_audit_data_zone;
#endif
/*
* mac_policy_list holds the list of policy modules. Modules with a
* handle lower than staticmax are considered "static" and cannot be
* unloaded. Such policies can be invoked without holding the busy count.
*
* Modules with a handle at or above the staticmax high water mark
* are considered to be "dynamic" policies. A busy count is maintained
* for the list, stored in mac_policy_busy. The busy count is protected
* by mac_policy_mtx; the list may be modified only while the busy
* count is 0, requiring that the lock be held to prevent new references
* to the list from being acquired. For almost all operations,
* incrementing the busy count is sufficient to guarantee consistency,
* as the list cannot be modified while the busy count is elevated.
* For a few special operations involving a change to the list of
* active policies, the mtx itself must be held.
*/
static lck_mtx_t *mac_policy_mtx;
/*
* Policy list array allocation chunk size. Trying to set this so that we
* allocate a page at a time.
*/
#define MAC_POLICY_LIST_CHUNKSIZE 512
static int mac_policy_busy;
#if CONFIG_EMBEDDED
SECURITY_READ_ONLY_LATE(mac_policy_list_t) mac_policy_list;
SECURITY_READ_ONLY_LATE(static struct mac_policy_list_element) mac_policy_static_entries[MAC_POLICY_LIST_CHUNKSIZE];
#else
mac_policy_list_t mac_policy_list;
#endif
/*
* mac_label_element_list holds the master list of label namespaces for
* all the policies. When a policy is loaded, each of it's label namespace
* elements is added to the master list if not already present. When a
* policy is unloaded, the namespace elements are removed if no other
* policy is interested in that namespace element.
*/
struct mac_label_element_list_t mac_label_element_list;
struct mac_label_element_list_t mac_static_label_element_list;
static __inline void
mac_policy_grab_exclusive(void)
{
lck_mtx_lock(mac_policy_mtx);
while (mac_policy_busy != 0) {
lck_mtx_sleep(mac_policy_mtx, LCK_SLEEP_UNLOCK,
(event_t)&mac_policy_busy, THREAD_UNINT);
lck_mtx_lock(mac_policy_mtx);
}
}
static __inline void
mac_policy_release_exclusive(void)
{
KASSERT(mac_policy_busy == 0,
("mac_policy_release_exclusive(): not exclusive"));
lck_mtx_unlock(mac_policy_mtx);
thread_wakeup((event_t) &mac_policy_busy);
}
void
mac_policy_list_busy(void)
{
lck_mtx_lock(mac_policy_mtx);
mac_policy_busy++;
lck_mtx_unlock(mac_policy_mtx);
}
int
mac_policy_list_conditional_busy(void)
{
int ret;
if (mac_policy_list.numloaded <= mac_policy_list.staticmax)
return(0);
lck_mtx_lock(mac_policy_mtx);
if (mac_policy_list.numloaded > mac_policy_list.staticmax) {
mac_policy_busy++;
ret = 1;
} else
ret = 0;
lck_mtx_unlock(mac_policy_mtx);
return (ret);
}
void
mac_policy_list_unbusy(void)
{
lck_mtx_lock(mac_policy_mtx);
mac_policy_busy--;
KASSERT(mac_policy_busy >= 0, ("MAC_POLICY_LIST_LOCK"));
if (mac_policy_busy == 0)
thread_wakeup(&mac_policy_busy);
lck_mtx_unlock(mac_policy_mtx);
}
/*
* Early pre-malloc MAC initialization, including appropriate SMP locks.
*/
void
mac_policy_init(void)
{
lck_grp_attr_t *mac_lck_grp_attr;
lck_attr_t *mac_lck_attr;
lck_grp_t *mac_lck_grp;
mac_policy_list.numloaded = 0;
mac_policy_list.max = MAC_POLICY_LIST_CHUNKSIZE;
mac_policy_list.maxindex = 0;
mac_policy_list.staticmax = 0;
mac_policy_list.freehint = 0;
mac_policy_list.chunks = 1;
#if CONFIG_EMBEDDED
mac_policy_list.entries = mac_policy_static_entries;
#else
mac_policy_list.entries = kalloc(sizeof(struct mac_policy_list_element) * MAC_POLICY_LIST_CHUNKSIZE);
#endif
bzero(mac_policy_list.entries, sizeof(struct mac_policy_list_element) * MAC_POLICY_LIST_CHUNKSIZE);
LIST_INIT(&mac_label_element_list);
LIST_INIT(&mac_static_label_element_list);
mac_lck_grp_attr = lck_grp_attr_alloc_init();
lck_grp_attr_setstat(mac_lck_grp_attr);
mac_lck_grp = lck_grp_alloc_init("MAC lock", mac_lck_grp_attr);
mac_lck_attr = lck_attr_alloc_init();
lck_attr_setdefault(mac_lck_attr);
mac_policy_mtx = lck_mtx_alloc_init(mac_lck_grp, mac_lck_attr);
lck_attr_free(mac_lck_attr);
lck_grp_attr_free(mac_lck_grp_attr);
lck_grp_free(mac_lck_grp);
mac_labelzone_init();
}
/* Function pointer set up for loading security extensions.
* It is set to an actual function after OSlibkernInit()
* has been called, and is set back to 0 by OSKextRemoveKextBootstrap()
* after bsd_init().
*/
void (*load_security_extensions_function)(void) = 0;
/*
* Init after early Mach startup, but before BSD
*/
void
mac_policy_initmach(void)
{
/*
* For the purposes of modules that want to know if they were
* loaded "early", set the mac_late flag once we've processed
* modules either linked into the kernel, or loaded before the
* kernel startup.
*/
if (load_security_extensions_function) {
load_security_extensions_function();
}
mac_late = 1;
}
/*
* BSD startup.
*/
void
mac_policy_initbsd(void)
{
struct mac_policy_conf *mpc;
u_int i;
#if CONFIG_AUDIT
mac_audit_data_zone = zinit(MAC_AUDIT_DATA_LIMIT,
AQ_HIWATER * MAC_AUDIT_DATA_LIMIT,
8192, "mac_audit_data_zone");
#endif
printf("MAC Framework successfully initialized\n");
/* Call bsd init functions of already loaded policies */
/*
* Using the exclusive lock means no other framework entry
* points can proceed while initializations are running.
* This may not be necessary.
*/
mac_policy_grab_exclusive();
for (i = 0; i <= mac_policy_list.maxindex; i++) {
mpc = mac_get_mpc(i);
if ((mpc != NULL) && (mpc->mpc_ops->mpo_policy_initbsd != NULL))
(*(mpc->mpc_ops->mpo_policy_initbsd))(mpc);
}
mac_policy_release_exclusive();
}
/*
* After a policy has been loaded, add the label namespaces managed by the
* policy to either the static or non-static label namespace list.
* A namespace is added to the the list only if it is not already on one of
* the lists.
*/
void
mac_policy_addto_labellist(mac_policy_handle_t handle, int static_entry)
{
struct mac_label_listener **new_mlls;
struct mac_label_element *mle, **new_mles;
struct mac_label_element_list_t *list;
struct mac_policy_conf *mpc;
const char *name, *name2;
u_int idx, mle_free, mll_free;
mpc = mac_get_mpc(handle);
if (mpc->mpc_labelnames == NULL)
return;
if (mpc->mpc_labelname_count == 0)
return;
if (static_entry)
list = &mac_static_label_element_list;
else
list = &mac_label_element_list;
/*
* Before we grab the policy list lock, allocate enough memory
* to contain the potential new elements so we don't have to
* give up the lock, or allocate with the lock held.
*/
MALLOC(new_mles, struct mac_label_element **,
sizeof(struct mac_label_element *) *
mpc->mpc_labelname_count, M_MACTEMP, M_WAITOK | M_ZERO);
for (idx = 0; idx < mpc->mpc_labelname_count; idx++)
MALLOC(new_mles[idx], struct mac_label_element *,
sizeof(struct mac_label_element),
M_MACTEMP, M_WAITOK);
mle_free = 0;
MALLOC(new_mlls, struct mac_label_listener **,
sizeof(struct mac_label_listener *) *
mpc->mpc_labelname_count, M_MACTEMP, M_WAITOK);
for (idx = 0; idx < mpc->mpc_labelname_count; idx++)
MALLOC(new_mlls[idx], struct mac_label_listener *,
sizeof(struct mac_label_listener), M_MACTEMP, M_WAITOK);
mll_free = 0;
if (mac_late)
mac_policy_grab_exclusive();
for (idx = 0; idx < mpc->mpc_labelname_count; idx++) {
if (*(name = mpc->mpc_labelnames[idx]) == '?')
name++;
/*
* Check both label element lists and add to the
* appropriate list only if not already on a list.
*/
LIST_FOREACH(mle, &mac_static_label_element_list, mle_list) {
if (*(name2 = mle->mle_name) == '?')
name2++;
if (strcmp(name, name2) == 0)
break;
}
if (mle == NULL) {
LIST_FOREACH(mle, &mac_label_element_list, mle_list) {
if (*(name2 = mle->mle_name) == '?')
name2++;
if (strcmp(name, name2) == 0)
break;
}
}
if (mle == NULL) {
mle = new_mles[mle_free];
strlcpy(mle->mle_name, mpc->mpc_labelnames[idx],
MAC_MAX_LABEL_ELEMENT_NAME);
LIST_INIT(&mle->mle_listeners);
LIST_INSERT_HEAD(list, mle, mle_list);
mle_free++;
}
/* Add policy handler as a listener. */
new_mlls[mll_free]->mll_handle = handle;
LIST_INSERT_HEAD(&mle->mle_listeners, new_mlls[mll_free],
mll_list);
mll_free++;
}
if (mac_late)
mac_policy_release_exclusive();
/* Free up any unused label elements and listeners */
for (idx = mle_free; idx < mpc->mpc_labelname_count; idx++)
FREE(new_mles[idx], M_MACTEMP);
FREE(new_mles, M_MACTEMP);
for (idx = mll_free; idx < mpc->mpc_labelname_count; idx++)
FREE(new_mlls[idx], M_MACTEMP);
FREE(new_mlls, M_MACTEMP);
}
/*
* After a policy has been unloaded, remove the label namespaces that the
* the policy manages from the non-static list of namespaces.
* The removal only takes place when no other policy is interested in the
* namespace.
*
* Must be called with the policy exclusive lock held.
*/
void
mac_policy_removefrom_labellist(mac_policy_handle_t handle)
{
struct mac_label_listener *mll;
struct mac_label_element *mle;
struct mac_policy_conf *mpc;
mpc = mac_get_mpc(handle);
if (mpc->mpc_labelnames == NULL)
return;
if (mpc->mpc_labelname_count == 0)
return;
/*
* Unregister policy as being interested in any label
* namespaces. If no other policy is listening, remove
* that label element from the list. Note that we only
* have to worry about the non-static list.
*/
LIST_FOREACH(mle, &mac_label_element_list, mle_list) {
LIST_FOREACH(mll, &mle->mle_listeners, mll_list) {
if (mll->mll_handle == handle) {
LIST_REMOVE(mll, mll_list);
FREE(mll, M_MACTEMP);
if (LIST_EMPTY(&mle->mle_listeners)) {
LIST_REMOVE(mle, mle_list);
FREE(mle, M_MACTEMP);
}
return;
}
}
}
}
/*
* After the policy list has changed, walk the list to update any global
* flags.
*/
static void
mac_policy_updateflags(void)
{
}
static __inline void
mac_policy_fixup_mmd_list(struct mac_module_data *new)
{
struct mac_module_data *old;
struct mac_module_data_element *ele, *aele;
struct mac_module_data_list *arr, *dict;
unsigned int i, j, k;
old = new->base_addr;
DPRINTF(("fixup_mmd: old %p new %p\n", old, new));
for (i = 0; i < new->count; i++) {
ele = &(new->data[i]);
DPRINTF(("fixup_mmd: ele %p\n", ele));
DPRINTF((" key %p value %p\n", ele->key, ele->value));
mmd_fixup_ele(old, new, ele); /* Fix up key/value ptrs. */
DPRINTF((" key %p value %p\n", ele->key, ele->value));
if (ele->value_type == MAC_DATA_TYPE_ARRAY) {
arr = (struct mac_module_data_list *)ele->value;
DPRINTF(("fixup_mmd: array @%p\n", arr));
for (j = 0; j < arr->count; j++) {
aele = &(arr->list[j]);
DPRINTF(("fixup_mmd: aele %p\n", aele));
DPRINTF((" key %p value %p\n", aele->key, aele->value));
mmd_fixup_ele(old, new, aele);
DPRINTF((" key %p value %p\n", aele->key, aele->value));
if (arr->type == MAC_DATA_TYPE_DICT) {
dict = (struct mac_module_data_list *)aele->value;
DPRINTF(("fixup_mmd: dict @%p\n", dict));
for (k = 0; k < dict->count; k++)
mmd_fixup_ele(old, new,
&(dict->list[k]));
}
}
}
}
new->base_addr = new;
}
int
mac_policy_register(struct mac_policy_conf *mpc, mac_policy_handle_t *handlep,
void *xd)
{
#if !CONFIG_EMBEDDED
struct mac_policy_list_element *tmac_policy_list_element;
#endif
int error, slot, static_entry = 0;
u_int i;
/*
* Some preliminary checks to make sure the policy's conf structure
* contains the required fields.
*/
if (mpc->mpc_name == NULL)
panic("policy's name is not set\n");
if (mpc->mpc_fullname == NULL)
panic("policy's full name is not set\n");
if (mpc->mpc_labelname_count > MAC_MAX_MANAGED_NAMESPACES)
panic("policy's managed label namespaces exceeds maximum\n");
if (mpc->mpc_ops == NULL)
panic("policy's OPs field is NULL\n");
error = 0;
if (mac_late) {
if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE) {
printf("Module %s does not support late loading.\n",
mpc->mpc_name);
return (EPERM);
}
mac_policy_grab_exclusive();
}
if (mac_policy_list.numloaded >= mac_policy_list.max) {
#if !CONFIG_EMBEDDED
/* allocate new policy list array, zero new chunk */
tmac_policy_list_element =
kalloc((sizeof(struct mac_policy_list_element) *
MAC_POLICY_LIST_CHUNKSIZE) * (mac_policy_list.chunks + 1));
bzero(&tmac_policy_list_element[mac_policy_list.max],
sizeof(struct mac_policy_list_element) *
MAC_POLICY_LIST_CHUNKSIZE);
/* copy old entries into new list */
memcpy(tmac_policy_list_element, mac_policy_list.entries,
sizeof(struct mac_policy_list_element) *
MAC_POLICY_LIST_CHUNKSIZE * mac_policy_list.chunks);
/* free old array */
kfree(mac_policy_list.entries,
sizeof(struct mac_policy_list_element) *
MAC_POLICY_LIST_CHUNKSIZE * mac_policy_list.chunks);
mac_policy_list.entries = tmac_policy_list_element;
/* Update maximums, etc */
mac_policy_list.max += MAC_POLICY_LIST_CHUNKSIZE;
mac_policy_list.chunks++;
#else
printf("out of space in mac_policy_list.\n");
return (ENOMEM);
#endif /* CONFIG_EMBEDDED */
}
/* Check for policy with same name already loaded */
for (i = 0; i <= mac_policy_list.maxindex; i++) {
if (mac_policy_list.entries[i].mpc == NULL)
continue;
if (strcmp(mac_policy_list.entries[i].mpc->mpc_name,
mpc->mpc_name) == 0) {
error = EEXIST;
goto out;
}
}
if (mpc->mpc_field_off != NULL) {
slot = ffs(mac_slot_offsets_free);
if (slot == 0) {
error = ENOMEM;
goto out;
}
slot--;
mac_slot_offsets_free &= ~(1 << slot);
*mpc->mpc_field_off = slot;
}
mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
if (xd) {
struct mac_module_data *mmd = xd; /* module data from plist */
/* Make a copy of the data. */
mpc->mpc_data = (void *)kalloc(mmd->size);
if (mpc->mpc_data != NULL) {
memcpy(mpc->mpc_data, mmd, mmd->size);
/* Fix up pointers after copy. */
mac_policy_fixup_mmd_list(mpc->mpc_data);
}
}
/* Find the first free handle in the list (using our hint). */
for (i = mac_policy_list.freehint; i < mac_policy_list.max; i++) {
if (mac_policy_list.entries[i].mpc == NULL) {
*handlep = i;
mac_policy_list.freehint = ++i;
break;
}
}
/*
* If we are loading a MAC module before the framework has
* finished initializing or the module is not unloadable and
* we can place its handle adjacent to the last static entry,
* bump the static policy high water mark.
* Static policies can get by with weaker locking requirements.
*/
if (!mac_late ||
((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0 &&
*handlep == mac_policy_list.staticmax)) {
static_entry = 1;
mac_policy_list.staticmax++;
}
mac_policy_list.entries[*handlep].mpc = mpc;
/* Update counters, etc */
if (*handlep > mac_policy_list.maxindex)
mac_policy_list.maxindex = *handlep;
mac_policy_list.numloaded++;
/* Per-policy initialization. */
printf ("calling mpo_policy_init for %s\n", mpc->mpc_name);
if (mpc->mpc_ops->mpo_policy_init != NULL)
(*(mpc->mpc_ops->mpo_policy_init))(mpc);
if (mac_late && mpc->mpc_ops->mpo_policy_initbsd != NULL) {
printf ("calling mpo_policy_initbsd for %s\n", mpc->mpc_name);
(*(mpc->mpc_ops->mpo_policy_initbsd))(mpc);
}
mac_policy_updateflags();
if (mac_late)
mac_policy_release_exclusive();
mac_policy_addto_labellist(*handlep, static_entry);
printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
mpc->mpc_name);
return (0);
out:
if (mac_late)
mac_policy_release_exclusive();
return (error);
}
int
mac_policy_unregister(mac_policy_handle_t handle)
{
struct mac_policy_conf *mpc;
/*
* If we fail the load, we may get a request to unload. Check
* to see if we did the run-time registration, and if not,
* silently succeed.
*/
mac_policy_grab_exclusive();
mpc = mac_get_mpc(handle);
if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED) == 0) {
mac_policy_release_exclusive();
return (0);
}
#if 0
/*
* Don't allow unloading modules with private data.
*/
if (mpc->mpc_field_off != NULL) {
MAC_POLICY_LIST_UNLOCK();
return (EBUSY);
}
#endif
/*
* Only allow the unload to proceed if the module is unloadable
* by its own definition.
*/
if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0) {
mac_policy_release_exclusive();
return (EBUSY);
}
mac_policy_removefrom_labellist(handle);
mac_get_mpc(handle) = NULL;
if (handle < mac_policy_list.freehint &&
handle >= mac_policy_list.staticmax)
mac_policy_list.freehint = handle;
if (handle == mac_policy_list.maxindex)
mac_policy_list.maxindex--;
mac_policy_list.numloaded--;
if (mpc->mpc_field_off != NULL) {
mac_slot_offsets_free |= (1 << *mpc->mpc_field_off);
}
if (mpc->mpc_ops->mpo_policy_destroy != NULL)
(*(mpc->mpc_ops->mpo_policy_destroy))(mpc);
mpc->mpc_runtime_flags &= ~MPC_RUNTIME_FLAG_REGISTERED;
mac_policy_updateflags();
mac_policy_release_exclusive();
if (mpc->mpc_data) {
struct mac_module_data *mmd = mpc->mpc_data;
kfree(mmd, mmd->size);
mpc->mpc_data = NULL;
}
printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
mpc->mpc_name);
return (0);
}
/*
* Define an error value precedence, and given two arguments, selects the
* value with the higher precedence.
*/
int
mac_error_select(int error1, int error2)
{
/* Certain decision-making errors take top priority. */
if (error1 == EDEADLK || error2 == EDEADLK)
return (EDEADLK);
/* Invalid arguments should be reported where possible. */
if (error1 == EINVAL || error2 == EINVAL)
return (EINVAL);
/* Precedence goes to "visibility", with both process and file. */
if (error1 == ESRCH || error2 == ESRCH)
return (ESRCH);
if (error1 == ENOENT || error2 == ENOENT)
return (ENOENT);
/* Precedence goes to DAC/MAC protections. */
if (error1 == EACCES || error2 == EACCES)
return (EACCES);
/* Precedence goes to privilege. */
if (error1 == EPERM || error2 == EPERM)
return (EPERM);
/* Precedence goes to error over success; otherwise, arbitrary. */
if (error1 != 0)
return (error1);
return (error2);
}
void
mac_label_init(struct label *label)
{
bzero(label, sizeof(*label));
label->l_flags = MAC_FLAG_INITIALIZED;
}
void
mac_label_destroy(struct label *label)
{
KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
("destroying uninitialized label"));
bzero(label, sizeof(*label));
/* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
}
int
mac_check_structmac_consistent(struct user_mac *mac)
{
if (mac->m_buflen > MAC_MAX_LABEL_BUF_LEN || mac->m_buflen == 0)
return (EINVAL);
return (0);
}
/*
* Get the external forms of labels from all policies, for a single
* label namespace or "*" for all namespaces. Returns ENOENT if no policy
* is registered for the namespace, unless the namespace begins with a '?'.
*/
static int
mac_label_externalize(size_t mpo_externalize_off, struct label *label,
const char *element, struct sbuf *sb)
{
struct mac_policy_conf *mpc;
struct mac_label_listener *mll;
struct mac_label_element *mle;
struct mac_label_element_list_t *element_list;
const char *name;
int (*mpo_externalize)(struct label *, char *, struct sbuf *);
int all_labels = 0, ignorenotfound = 0, error = 0, busy = FALSE;
unsigned int count = 0;
if (element[0] == '?') {
element++;
ignorenotfound = 1;
} else if (element[0] == '*' && element[1] == '\0')
all_labels = 1;
element_list = &mac_static_label_element_list;
element_loop:
LIST_FOREACH(mle, element_list, mle_list) {
name = mle->mle_name;
if (all_labels) {
if (*name == '?')
continue;
} else {
if (*name == '?')
name++;
if (strcmp(name, element) != 0)
continue;
}
LIST_FOREACH(mll, &mle->mle_listeners, mll_list) {
mpc = mac_policy_list.entries[mll->mll_handle].mpc;
if (mpc == NULL)
continue;
mpo_externalize = *(const typeof(mpo_externalize) *)
((const char *)mpc->mpc_ops + mpo_externalize_off);
if (mpo_externalize == NULL)
continue;
error = sbuf_printf(sb, "%s/", name);
if (error)
goto done;
error = mpo_externalize(label, mle->mle_name, sb);
if (error) {
if (error != ENOENT)
goto done;
/*
* If a policy doesn't have a label to
* externalize it returns ENOENT. This
* may occur for policies that support
* multiple label elements for some
* (but not all) object types.
*/
sbuf_setpos(sb, sbuf_len(sb) -
(strlen(name) + 1));