forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkern_resource.c
1918 lines (1639 loc) · 46.5 KB
/
kern_resource.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) 2000-2017 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) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
/*-
* Copyright (c) 1982, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
*/
/*
* 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.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/kernel.h>
#include <sys/file_internal.h>
#include <sys/resourcevar.h>
#include <sys/malloc.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/mount_internal.h>
#include <sys/sysproto.h>
#include <security/audit/audit.h>
#include <machine/vmparam.h>
#include <mach/mach_types.h>
#include <mach/time_value.h>
#include <mach/task.h>
#include <mach/task_info.h>
#include <mach/vm_map.h>
#include <mach/mach_vm.h>
#include <mach/thread_act.h> /* for thread_policy_set( ) */
#include <kern/thread.h>
#include <kern/policy_internal.h>
#include <kern/task.h>
#include <kern/clock.h> /* for absolutetime_to_microtime() */
#include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
#include <sys/socketvar.h> /* for struct socket */
#if NECP
#include <net/necp.h>
#endif /* NECP */
#include <vm/vm_map.h>
#include <kern/assert.h>
#include <sys/resource.h>
#include <sys/priv.h>
#include <IOKit/IOBSD.h>
#if CONFIG_MACF
#include <security/mac_framework.h>
#endif
int donice(struct proc *curp, struct proc *chgp, int n);
int dosetrlimit(struct proc *p, u_int which, struct rlimit *limp);
int uthread_get_background_state(uthread_t);
static void do_background_socket(struct proc *p, thread_t thread);
static int do_background_thread(thread_t thread, int priority);
static int do_background_proc(struct proc *curp, struct proc *targetp, int priority);
static int set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority);
static int proc_set_darwin_role(proc_t curp, proc_t targetp, int priority);
static int proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority);
static int get_background_proc(struct proc *curp, struct proc *targetp, int *priority);
int proc_pid_rusage(int pid, int flavor, user_addr_t buf, int32_t *retval);
void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor);
int fill_task_rusage(task_t task, rusage_info_current *ri);
void fill_task_billed_usage(task_t task, rusage_info_current *ri);
int fill_task_io_rusage(task_t task, rusage_info_current *ri);
int fill_task_qos_rusage(task_t task, rusage_info_current *ri);
uint64_t get_task_logical_writes(task_t task);
void fill_task_monotonic_rusage(task_t task, rusage_info_current *ri);
int proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie);
rlim_t maxdmap = MAXDSIZ; /* XXX */
rlim_t maxsmap = MAXSSIZ - PAGE_MAX_SIZE; /* XXX */
/*
* Limits on the number of open files per process, and the number
* of child processes per process.
*
* Note: would be in kern/subr_param.c in FreeBSD.
*/
__private_extern__ int maxfilesperproc = OPEN_MAX; /* per-proc open files limit */
SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW | CTLFLAG_LOCKED,
&maxprocperuid, 0, "Maximum processes allowed per userid" );
SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW | CTLFLAG_LOCKED,
&maxfilesperproc, 0, "Maximum files allowed open per process" );
/* Args and fn for proc_iteration callback used in setpriority */
struct puser_nice_args {
proc_t curp;
int prio;
id_t who;
int * foundp;
int * errorp;
};
static int puser_donice_callback(proc_t p, void * arg);
/* Args and fn for proc_iteration callback used in setpriority */
struct ppgrp_nice_args {
proc_t curp;
int prio;
int * foundp;
int * errorp;
};
static int ppgrp_donice_callback(proc_t p, void * arg);
/*
* Resource controls and accounting.
*/
int
getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
{
struct proc *p;
int low = PRIO_MAX + 1;
kauth_cred_t my_cred;
int refheld = 0;
int error = 0;
/* would also test (uap->who < 0), but id_t is unsigned */
if (uap->who > 0x7fffffff)
return (EINVAL);
switch (uap->which) {
case PRIO_PROCESS:
if (uap->who == 0) {
p = curp;
low = p->p_nice;
} else {
p = proc_find(uap->who);
if (p == 0)
break;
low = p->p_nice;
proc_rele(p);
}
break;
case PRIO_PGRP: {
struct pgrp *pg = PGRP_NULL;
if (uap->who == 0) {
/* returns the pgrp to ref */
pg = proc_pgrp(curp);
} else if ((pg = pgfind(uap->who)) == PGRP_NULL) {
break;
}
/* No need for iteration as it is a simple scan */
pgrp_lock(pg);
PGMEMBERS_FOREACH(pg, p) {
if (p->p_nice < low)
low = p->p_nice;
}
pgrp_unlock(pg);
pg_rele(pg);
break;
}
case PRIO_USER:
if (uap->who == 0)
uap->who = kauth_cred_getuid(kauth_cred_get());
proc_list_lock();
for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
my_cred = kauth_cred_proc_ref(p);
if (kauth_cred_getuid(my_cred) == uap->who &&
p->p_nice < low)
low = p->p_nice;
kauth_cred_unref(&my_cred);
}
proc_list_unlock();
break;
case PRIO_DARWIN_THREAD:
/* we currently only support the current thread */
if (uap->who != 0)
return (EINVAL);
low = proc_get_thread_policy(current_thread(), TASK_POLICY_INTERNAL, TASK_POLICY_DARWIN_BG);
break;
case PRIO_DARWIN_PROCESS:
if (uap->who == 0) {
p = curp;
} else {
p = proc_find(uap->who);
if (p == PROC_NULL)
break;
refheld = 1;
}
error = get_background_proc(curp, p, &low);
if (refheld)
proc_rele(p);
if (error)
return (error);
break;
case PRIO_DARWIN_ROLE:
if (uap->who == 0) {
p = curp;
} else {
p = proc_find(uap->who);
if (p == PROC_NULL)
break;
refheld = 1;
}
error = proc_get_darwin_role(curp, p, &low);
if (refheld)
proc_rele(p);
if (error)
return (error);
break;
default:
return (EINVAL);
}
if (low == PRIO_MAX + 1)
return (ESRCH);
*retval = low;
return (0);
}
/* call back function used for proc iteration in PRIO_USER */
static int
puser_donice_callback(proc_t p, void * arg)
{
int error, n;
struct puser_nice_args * pun = (struct puser_nice_args *)arg;
kauth_cred_t my_cred;
my_cred = kauth_cred_proc_ref(p);
if (kauth_cred_getuid(my_cred) == pun->who) {
error = donice(pun->curp, p, pun->prio);
if (pun->errorp != NULL)
*pun->errorp = error;
if (pun->foundp != NULL) {
n = *pun->foundp;
*pun->foundp = n+1;
}
}
kauth_cred_unref(&my_cred);
return(PROC_RETURNED);
}
/* call back function used for proc iteration in PRIO_PGRP */
static int
ppgrp_donice_callback(proc_t p, void * arg)
{
int error;
struct ppgrp_nice_args * pun = (struct ppgrp_nice_args *)arg;
int n;
error = donice(pun->curp, p, pun->prio);
if (pun->errorp != NULL)
*pun->errorp = error;
if (pun->foundp!= NULL) {
n = *pun->foundp;
*pun->foundp = n+1;
}
return(PROC_RETURNED);
}
/*
* Returns: 0 Success
* EINVAL
* ESRCH
* donice:EPERM
* donice:EACCES
*/
/* ARGSUSED */
int
setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
{
struct proc *p;
int found = 0, error = 0;
int refheld = 0;
AUDIT_ARG(cmd, uap->which);
AUDIT_ARG(owner, uap->who, 0);
AUDIT_ARG(value32, uap->prio);
/* would also test (uap->who < 0), but id_t is unsigned */
if (uap->who > 0x7fffffff)
return (EINVAL);
switch (uap->which) {
case PRIO_PROCESS:
if (uap->who == 0)
p = curp;
else {
p = proc_find(uap->who);
if (p == 0)
break;
refheld = 1;
}
error = donice(curp, p, uap->prio);
found++;
if (refheld != 0)
proc_rele(p);
break;
case PRIO_PGRP: {
struct pgrp *pg = PGRP_NULL;
struct ppgrp_nice_args ppgrp;
if (uap->who == 0) {
pg = proc_pgrp(curp);
} else if ((pg = pgfind(uap->who)) == PGRP_NULL)
break;
ppgrp.curp = curp;
ppgrp.prio = uap->prio;
ppgrp.foundp = &found;
ppgrp.errorp = &error;
/* PGRP_DROPREF drops the reference on process group */
pgrp_iterate(pg, PGRP_DROPREF, ppgrp_donice_callback, (void *)&ppgrp, NULL, NULL);
break;
}
case PRIO_USER: {
struct puser_nice_args punice;
if (uap->who == 0)
uap->who = kauth_cred_getuid(kauth_cred_get());
punice.curp = curp;
punice.prio = uap->prio;
punice.who = uap->who;
punice.foundp = &found;
error = 0;
punice.errorp = &error;
proc_iterate(PROC_ALLPROCLIST, puser_donice_callback, (void *)&punice, NULL, NULL);
break;
}
case PRIO_DARWIN_THREAD: {
/* we currently only support the current thread */
if (uap->who != 0)
return (EINVAL);
error = do_background_thread(current_thread(), uap->prio);
found++;
break;
}
case PRIO_DARWIN_PROCESS: {
if (uap->who == 0)
p = curp;
else {
p = proc_find(uap->who);
if (p == 0)
break;
refheld = 1;
}
error = do_background_proc(curp, p, uap->prio);
found++;
if (refheld != 0)
proc_rele(p);
break;
}
case PRIO_DARWIN_GPU: {
if (uap->who == 0)
return (EINVAL);
p = proc_find(uap->who);
if (p == PROC_NULL)
break;
error = set_gpudeny_proc(curp, p, uap->prio);
found++;
proc_rele(p);
break;
}
case PRIO_DARWIN_ROLE: {
if (uap->who == 0) {
p = curp;
} else {
p = proc_find(uap->who);
if (p == PROC_NULL)
break;
refheld = 1;
}
error = proc_set_darwin_role(curp, p, uap->prio);
found++;
if (refheld != 0)
proc_rele(p);
break;
}
default:
return (EINVAL);
}
if (found == 0)
return (ESRCH);
if (error == EIDRM) {
*retval = -2;
error = 0;
}
return (error);
}
/*
* Returns: 0 Success
* EPERM
* EACCES
* mac_check_proc_sched:???
*/
int
donice(struct proc *curp, struct proc *chgp, int n)
{
int error = 0;
kauth_cred_t ucred;
kauth_cred_t my_cred;
ucred = kauth_cred_proc_ref(curp);
my_cred = kauth_cred_proc_ref(chgp);
if (suser(ucred, NULL) && kauth_cred_getruid(ucred) &&
kauth_cred_getuid(ucred) != kauth_cred_getuid(my_cred) &&
kauth_cred_getruid(ucred) != kauth_cred_getuid(my_cred)) {
error = EPERM;
goto out;
}
if (n > PRIO_MAX)
n = PRIO_MAX;
if (n < PRIO_MIN)
n = PRIO_MIN;
if (n < chgp->p_nice && suser(ucred, &curp->p_acflag)) {
error = EACCES;
goto out;
}
#if CONFIG_MACF
error = mac_proc_check_sched(curp, chgp);
if (error)
goto out;
#endif
proc_lock(chgp);
chgp->p_nice = n;
proc_unlock(chgp);
(void)resetpriority(chgp);
out:
kauth_cred_unref(&ucred);
kauth_cred_unref(&my_cred);
return (error);
}
static int
set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority)
{
int error = 0;
kauth_cred_t ucred;
kauth_cred_t target_cred;
ucred = kauth_cred_get();
target_cred = kauth_cred_proc_ref(targetp);
/* TODO: Entitlement instead of uid check */
if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
error = EPERM;
goto out;
}
if (curp == targetp) {
error = EPERM;
goto out;
}
#if CONFIG_MACF
error = mac_proc_check_sched(curp, targetp);
if (error)
goto out;
#endif
switch (priority) {
case PRIO_DARWIN_GPU_DENY:
task_set_gpu_denied(proc_task(targetp), TRUE);
break;
case PRIO_DARWIN_GPU_ALLOW:
task_set_gpu_denied(proc_task(targetp), FALSE);
break;
default:
error = EINVAL;
goto out;
}
out:
kauth_cred_unref(&target_cred);
return (error);
}
static int
proc_set_darwin_role(proc_t curp, proc_t targetp, int priority)
{
int error = 0;
uint32_t flagsp = 0;
kauth_cred_t ucred, target_cred;
ucred = kauth_cred_get();
target_cred = kauth_cred_proc_ref(targetp);
if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
if (priv_check_cred(ucred, PRIV_SETPRIORITY_DARWIN_ROLE, 0) != 0) {
error = EPERM;
goto out;
}
}
if (curp != targetp) {
#if CONFIG_MACF
if ((error = mac_proc_check_sched(curp, targetp)))
goto out;
#endif
}
proc_get_darwinbgstate(proc_task(targetp), &flagsp);
if ((flagsp & PROC_FLAG_APPLICATION) != PROC_FLAG_APPLICATION) {
error = ENOTSUP;
goto out;
}
integer_t role = 0;
if ((error = proc_darwin_role_to_task_role(priority, &role)))
goto out;
proc_set_task_policy(proc_task(targetp), TASK_POLICY_ATTRIBUTE,
TASK_POLICY_ROLE, role);
out:
kauth_cred_unref(&target_cred);
return (error);
}
static int
proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority)
{
int error = 0;
int role = 0;
kauth_cred_t ucred, target_cred;
ucred = kauth_cred_get();
target_cred = kauth_cred_proc_ref(targetp);
if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
error = EPERM;
goto out;
}
if (curp != targetp) {
#if CONFIG_MACF
if ((error = mac_proc_check_sched(curp, targetp)))
goto out;
#endif
}
role = proc_get_task_policy(proc_task(targetp), TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE);
*priority = proc_task_role_to_darwin_role(role);
out:
kauth_cred_unref(&target_cred);
return (error);
}
static int
get_background_proc(struct proc *curp, struct proc *targetp, int *priority)
{
int external = 0;
int error = 0;
kauth_cred_t ucred, target_cred;
ucred = kauth_cred_get();
target_cred = kauth_cred_proc_ref(targetp);
if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
error = EPERM;
goto out;
}
external = (curp == targetp) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
*priority = proc_get_task_policy(current_task(), external, TASK_POLICY_DARWIN_BG);
out:
kauth_cred_unref(&target_cred);
return (error);
}
static int
do_background_proc(struct proc *curp, struct proc *targetp, int priority)
{
#if !CONFIG_MACF
#pragma unused(curp)
#endif
int error = 0;
kauth_cred_t ucred;
kauth_cred_t target_cred;
int external;
int enable;
ucred = kauth_cred_get();
target_cred = kauth_cred_proc_ref(targetp);
if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred))
{
error = EPERM;
goto out;
}
#if CONFIG_MACF
error = mac_proc_check_sched(curp, targetp);
if (error)
goto out;
#endif
external = (curp == targetp) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
switch (priority) {
case PRIO_DARWIN_BG:
enable = TASK_POLICY_ENABLE;
break;
case PRIO_DARWIN_NONUI:
/* ignored for compatibility */
goto out;
default:
/* TODO: EINVAL if priority != 0 */
enable = TASK_POLICY_DISABLE;
break;
}
proc_set_task_policy(proc_task(targetp), external, TASK_POLICY_DARWIN_BG, enable);
out:
kauth_cred_unref(&target_cred);
return (error);
}
static void
do_background_socket(struct proc *p, thread_t thread)
{
#if SOCKETS
struct filedesc *fdp;
struct fileproc *fp;
int i, background;
proc_fdlock(p);
if (thread != THREAD_NULL)
background = proc_get_effective_thread_policy(thread, TASK_POLICY_ALL_SOCKETS_BG);
else
background = proc_get_effective_task_policy(proc_task(p), TASK_POLICY_ALL_SOCKETS_BG);
if (background) {
/*
* For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
* the sockets with the background flag. There's nothing
* to do here for the PRIO_DARWIN_THREAD case.
*/
if (thread == THREAD_NULL) {
fdp = p->p_fd;
for (i = 0; i < fdp->fd_nfiles; i++) {
fp = fdp->fd_ofiles[i];
if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0) {
continue;
}
if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_SOCKET) {
struct socket *sockp = (struct socket *)fp->f_fglob->fg_data;
socket_set_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
sockp->so_background_thread = NULL;
}
#if NECP
else if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_NETPOLICY) {
necp_set_client_as_background(p, fp, background);
}
#endif /* NECP */
}
}
} else {
/* disable networking IO throttle.
* NOTE - It is a known limitation of the current design that we
* could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
* sockets created by other threads within this process.
*/
fdp = p->p_fd;
for ( i = 0; i < fdp->fd_nfiles; i++ ) {
struct socket *sockp;
fp = fdp->fd_ofiles[ i ];
if (fp == NULL || (fdp->fd_ofileflags[ i ] & UF_RESERVED) != 0) {
continue;
}
if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_SOCKET) {
sockp = (struct socket *)fp->f_fglob->fg_data;
/* skip if only clearing this thread's sockets */
if ((thread) && (sockp->so_background_thread != thread)) {
continue;
}
socket_clear_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
sockp->so_background_thread = NULL;
}
#if NECP
else if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_NETPOLICY) {
necp_set_client_as_background(p, fp, background);
}
#endif /* NECP */
}
}
proc_fdunlock(p);
#else
#pragma unused(p, thread)
#endif
}
/*
* do_background_thread
*
* Requires: thread reference
*
* Returns: 0 Success
* EPERM Tried to background while in vfork
* XXX - todo - does this need a MACF hook?
*/
static int
do_background_thread(thread_t thread, int priority)
{
struct uthread *ut;
int enable, external;
int rv = 0;
ut = get_bsdthread_info(thread);
/* Backgrounding is unsupported for threads in vfork */
if ((ut->uu_flag & UT_VFORK) != 0)
return(EPERM);
/* Backgrounding is unsupported for workq threads */
if (thread_is_static_param(thread)) {
return(EPERM);
}
/* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
if (thread_has_qos_policy(thread)) {
thread_remove_qos_policy(thread);
rv = EIDRM;
}
/* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
enable = (priority == PRIO_DARWIN_BG) ? TASK_POLICY_ENABLE : TASK_POLICY_DISABLE;
external = (current_thread() == thread) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
proc_set_thread_policy(thread, external, TASK_POLICY_DARWIN_BG, enable);
return rv;
}
/*
* Returns: 0 Success
* copyin:EFAULT
* dosetrlimit:
*/
/* ARGSUSED */
int
setrlimit(struct proc *p, struct setrlimit_args *uap, __unused int32_t *retval)
{
struct rlimit alim;
int error;
if ((error = copyin(uap->rlp, (caddr_t)&alim,
sizeof (struct rlimit))))
return (error);
return (dosetrlimit(p, uap->which, &alim));
}
/*
* Returns: 0 Success
* EINVAL
* ENOMEM Cannot copy limit structure
* suser:EPERM
*
* Notes: EINVAL is returned both for invalid arguments, and in the
* case that the current usage (e.g. RLIMIT_STACK) is already
* in excess of the requested limit.
*/
int
dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
{
struct rlimit *alimp;
int error;
kern_return_t kr;
int posix = (which & _RLIMIT_POSIX_FLAG) ? 1 : 0;
/* Mask out POSIX flag, saved above */
which &= ~_RLIMIT_POSIX_FLAG;
if (which >= RLIM_NLIMITS)
return (EINVAL);
alimp = &p->p_rlimit[which];
if (limp->rlim_cur > limp->rlim_max)
return EINVAL;
if (limp->rlim_cur > alimp->rlim_max ||
limp->rlim_max > alimp->rlim_max)
if ((error = suser(kauth_cred_get(), &p->p_acflag))) {
return (error);
}
proc_limitblock(p);
if ((error = proc_limitreplace(p)) != 0) {
proc_limitunblock(p);
return(error);
}
alimp = &p->p_rlimit[which];
switch (which) {
case RLIMIT_CPU:
if (limp->rlim_cur == RLIM_INFINITY) {
task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
timerclear(&p->p_rlim_cpu);
}
else {
task_absolutetime_info_data_t tinfo;
mach_msg_type_number_t count;
struct timeval ttv, tv;
clock_sec_t tv_sec;
clock_usec_t tv_usec;
count = TASK_ABSOLUTETIME_INFO_COUNT;
task_info(p->task, TASK_ABSOLUTETIME_INFO,
(task_info_t)&tinfo, &count);
absolutetime_to_microtime(tinfo.total_user + tinfo.total_system,
&tv_sec, &tv_usec);
ttv.tv_sec = tv_sec;
ttv.tv_usec = tv_usec;
tv.tv_sec = (limp->rlim_cur > __INT_MAX__ ? __INT_MAX__ : limp->rlim_cur);
tv.tv_usec = 0;
timersub(&tv, &ttv, &p->p_rlim_cpu);
timerclear(&tv);
if (timercmp(&p->p_rlim_cpu, &tv, >))
task_vtimer_set(p->task, TASK_VTIMER_RLIM);
else {
task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
timerclear(&p->p_rlim_cpu);
psignal(p, SIGXCPU);
}
}
break;
case RLIMIT_DATA:
if (limp->rlim_cur > maxdmap)
limp->rlim_cur = maxdmap;
if (limp->rlim_max > maxdmap)
limp->rlim_max = maxdmap;
break;
case RLIMIT_STACK:
/* Disallow illegal stack size instead of clipping */
if (limp->rlim_cur > maxsmap ||
limp->rlim_max > maxsmap) {
if (posix) {
error = EINVAL;
goto out;
}
else {
/*
* 4797860 - workaround poorly written installers by
* doing previous implementation (< 10.5) when caller
* is non-POSIX conforming.
*/
if (limp->rlim_cur > maxsmap)
limp->rlim_cur = maxsmap;
if (limp->rlim_max > maxsmap)
limp->rlim_max = maxsmap;
}
}
/*
* Stack is allocated to the max at exec time with only
* "rlim_cur" bytes accessible. If stack limit is going
* up make more accessible, if going down make inaccessible.