]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_resource.c
xnu-4903.221.2.tar.gz
[apple/xnu.git] / bsd / kern / kern_resource.c
1 /*
2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29 /*-
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
67 */
68 /*
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
72 * Version 2.0.
73 */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
79 #include <sys/file_internal.h>
80 #include <sys/resourcevar.h>
81 #include <sys/malloc.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <sys/mount_internal.h>
85 #include <sys/sysproto.h>
86
87 #include <security/audit/audit.h>
88
89 #include <machine/vmparam.h>
90
91 #include <mach/mach_types.h>
92 #include <mach/time_value.h>
93 #include <mach/task.h>
94 #include <mach/task_info.h>
95 #include <mach/vm_map.h>
96 #include <mach/mach_vm.h>
97 #include <mach/thread_act.h> /* for thread_policy_set( ) */
98 #include <kern/thread.h>
99 #include <kern/policy_internal.h>
100
101 #include <kern/task.h>
102 #include <kern/clock.h> /* for absolutetime_to_microtime() */
103 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
104 #include <sys/socketvar.h> /* for struct socket */
105 #if NECP
106 #include <net/necp.h>
107 #endif /* NECP */
108
109 #include <vm/vm_map.h>
110
111 #include <kern/assert.h>
112 #include <sys/resource.h>
113 #include <sys/priv.h>
114 #include <IOKit/IOBSD.h>
115
116 #if CONFIG_MACF
117 #include <security/mac_framework.h>
118 #endif
119
120 int donice(struct proc *curp, struct proc *chgp, int n);
121 int dosetrlimit(struct proc *p, u_int which, struct rlimit *limp);
122 int uthread_get_background_state(uthread_t);
123 static void do_background_socket(struct proc *p, thread_t thread);
124 static int do_background_thread(thread_t thread, int priority);
125 static int do_background_proc(struct proc *curp, struct proc *targetp, int priority);
126 static int set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority);
127 static int proc_set_darwin_role(proc_t curp, proc_t targetp, int priority);
128 static int proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority);
129 static int get_background_proc(struct proc *curp, struct proc *targetp, int *priority);
130 int proc_pid_rusage(int pid, int flavor, user_addr_t buf, int32_t *retval);
131 void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor);
132 int fill_task_rusage(task_t task, rusage_info_current *ri);
133 void fill_task_billed_usage(task_t task, rusage_info_current *ri);
134 int fill_task_io_rusage(task_t task, rusage_info_current *ri);
135 int fill_task_qos_rusage(task_t task, rusage_info_current *ri);
136 uint64_t get_task_logical_writes(task_t task);
137 void fill_task_monotonic_rusage(task_t task, rusage_info_current *ri);
138
139 int proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie);
140
141 rlim_t maxdmap = MAXDSIZ; /* XXX */
142 rlim_t maxsmap = MAXSSIZ - PAGE_MAX_SIZE; /* XXX */
143
144 /*
145 * Limits on the number of open files per process, and the number
146 * of child processes per process.
147 *
148 * Note: would be in kern/subr_param.c in FreeBSD.
149 */
150 __private_extern__ int maxfilesperproc = OPEN_MAX; /* per-proc open files limit */
151
152 SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW | CTLFLAG_LOCKED,
153 &maxprocperuid, 0, "Maximum processes allowed per userid" );
154
155 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW | CTLFLAG_LOCKED,
156 &maxfilesperproc, 0, "Maximum files allowed open per process" );
157
158 /* Args and fn for proc_iteration callback used in setpriority */
159 struct puser_nice_args {
160 proc_t curp;
161 int prio;
162 id_t who;
163 int * foundp;
164 int * errorp;
165 };
166 static int puser_donice_callback(proc_t p, void * arg);
167
168
169 /* Args and fn for proc_iteration callback used in setpriority */
170 struct ppgrp_nice_args {
171 proc_t curp;
172 int prio;
173 int * foundp;
174 int * errorp;
175 };
176 static int ppgrp_donice_callback(proc_t p, void * arg);
177
178 /*
179 * Resource controls and accounting.
180 */
181 int
182 getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
183 {
184 struct proc *p;
185 int low = PRIO_MAX + 1;
186 kauth_cred_t my_cred;
187 int refheld = 0;
188 int error = 0;
189
190 /* would also test (uap->who < 0), but id_t is unsigned */
191 if (uap->who > 0x7fffffff)
192 return (EINVAL);
193
194 switch (uap->which) {
195
196 case PRIO_PROCESS:
197 if (uap->who == 0) {
198 p = curp;
199 low = p->p_nice;
200 } else {
201 p = proc_find(uap->who);
202 if (p == 0)
203 break;
204 low = p->p_nice;
205 proc_rele(p);
206
207 }
208 break;
209
210 case PRIO_PGRP: {
211 struct pgrp *pg = PGRP_NULL;
212
213 if (uap->who == 0) {
214 /* returns the pgrp to ref */
215 pg = proc_pgrp(curp);
216 } else if ((pg = pgfind(uap->who)) == PGRP_NULL) {
217 break;
218 }
219 /* No need for iteration as it is a simple scan */
220 pgrp_lock(pg);
221 PGMEMBERS_FOREACH(pg, p) {
222 if (p->p_nice < low)
223 low = p->p_nice;
224 }
225 pgrp_unlock(pg);
226 pg_rele(pg);
227 break;
228 }
229
230 case PRIO_USER:
231 if (uap->who == 0)
232 uap->who = kauth_cred_getuid(kauth_cred_get());
233
234 proc_list_lock();
235
236 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
237 my_cred = kauth_cred_proc_ref(p);
238 if (kauth_cred_getuid(my_cred) == uap->who &&
239 p->p_nice < low)
240 low = p->p_nice;
241 kauth_cred_unref(&my_cred);
242 }
243
244 proc_list_unlock();
245
246 break;
247
248 case PRIO_DARWIN_THREAD:
249 /* we currently only support the current thread */
250 if (uap->who != 0)
251 return (EINVAL);
252
253 low = proc_get_thread_policy(current_thread(), TASK_POLICY_INTERNAL, TASK_POLICY_DARWIN_BG);
254
255 break;
256
257 case PRIO_DARWIN_PROCESS:
258 if (uap->who == 0) {
259 p = curp;
260 } else {
261 p = proc_find(uap->who);
262 if (p == PROC_NULL)
263 break;
264 refheld = 1;
265 }
266
267 error = get_background_proc(curp, p, &low);
268
269 if (refheld)
270 proc_rele(p);
271 if (error)
272 return (error);
273 break;
274
275 case PRIO_DARWIN_ROLE:
276 if (uap->who == 0) {
277 p = curp;
278 } else {
279 p = proc_find(uap->who);
280 if (p == PROC_NULL)
281 break;
282 refheld = 1;
283 }
284
285 error = proc_get_darwin_role(curp, p, &low);
286
287 if (refheld)
288 proc_rele(p);
289 if (error)
290 return (error);
291 break;
292
293 default:
294 return (EINVAL);
295 }
296 if (low == PRIO_MAX + 1)
297 return (ESRCH);
298 *retval = low;
299 return (0);
300 }
301
302 /* call back function used for proc iteration in PRIO_USER */
303 static int
304 puser_donice_callback(proc_t p, void * arg)
305 {
306 int error, n;
307 struct puser_nice_args * pun = (struct puser_nice_args *)arg;
308 kauth_cred_t my_cred;
309
310 my_cred = kauth_cred_proc_ref(p);
311 if (kauth_cred_getuid(my_cred) == pun->who) {
312 error = donice(pun->curp, p, pun->prio);
313 if (pun->errorp != NULL)
314 *pun->errorp = error;
315 if (pun->foundp != NULL) {
316 n = *pun->foundp;
317 *pun->foundp = n+1;
318 }
319 }
320 kauth_cred_unref(&my_cred);
321
322 return(PROC_RETURNED);
323 }
324
325 /* call back function used for proc iteration in PRIO_PGRP */
326 static int
327 ppgrp_donice_callback(proc_t p, void * arg)
328 {
329 int error;
330 struct ppgrp_nice_args * pun = (struct ppgrp_nice_args *)arg;
331 int n;
332
333 error = donice(pun->curp, p, pun->prio);
334 if (pun->errorp != NULL)
335 *pun->errorp = error;
336 if (pun->foundp!= NULL) {
337 n = *pun->foundp;
338 *pun->foundp = n+1;
339 }
340
341 return(PROC_RETURNED);
342 }
343
344 /*
345 * Returns: 0 Success
346 * EINVAL
347 * ESRCH
348 * donice:EPERM
349 * donice:EACCES
350 */
351 /* ARGSUSED */
352 int
353 setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
354 {
355 struct proc *p;
356 int found = 0, error = 0;
357 int refheld = 0;
358
359 AUDIT_ARG(cmd, uap->which);
360 AUDIT_ARG(owner, uap->who, 0);
361 AUDIT_ARG(value32, uap->prio);
362
363 /* would also test (uap->who < 0), but id_t is unsigned */
364 if (uap->who > 0x7fffffff)
365 return (EINVAL);
366
367 switch (uap->which) {
368
369 case PRIO_PROCESS:
370 if (uap->who == 0)
371 p = curp;
372 else {
373 p = proc_find(uap->who);
374 if (p == 0)
375 break;
376 refheld = 1;
377 }
378 error = donice(curp, p, uap->prio);
379 found++;
380 if (refheld != 0)
381 proc_rele(p);
382 break;
383
384 case PRIO_PGRP: {
385 struct pgrp *pg = PGRP_NULL;
386 struct ppgrp_nice_args ppgrp;
387
388 if (uap->who == 0) {
389 pg = proc_pgrp(curp);
390 } else if ((pg = pgfind(uap->who)) == PGRP_NULL)
391 break;
392
393 ppgrp.curp = curp;
394 ppgrp.prio = uap->prio;
395 ppgrp.foundp = &found;
396 ppgrp.errorp = &error;
397
398 /* PGRP_DROPREF drops the reference on process group */
399 pgrp_iterate(pg, PGRP_DROPREF, ppgrp_donice_callback, (void *)&ppgrp, NULL, NULL);
400
401 break;
402 }
403
404 case PRIO_USER: {
405 struct puser_nice_args punice;
406
407 if (uap->who == 0)
408 uap->who = kauth_cred_getuid(kauth_cred_get());
409
410 punice.curp = curp;
411 punice.prio = uap->prio;
412 punice.who = uap->who;
413 punice.foundp = &found;
414 error = 0;
415 punice.errorp = &error;
416 proc_iterate(PROC_ALLPROCLIST, puser_donice_callback, (void *)&punice, NULL, NULL);
417
418 break;
419 }
420
421 case PRIO_DARWIN_THREAD: {
422 /* we currently only support the current thread */
423 if (uap->who != 0)
424 return (EINVAL);
425
426 error = do_background_thread(current_thread(), uap->prio);
427 found++;
428 break;
429 }
430
431 case PRIO_DARWIN_PROCESS: {
432 if (uap->who == 0)
433 p = curp;
434 else {
435 p = proc_find(uap->who);
436 if (p == 0)
437 break;
438 refheld = 1;
439 }
440
441 error = do_background_proc(curp, p, uap->prio);
442
443 found++;
444 if (refheld != 0)
445 proc_rele(p);
446 break;
447 }
448
449 case PRIO_DARWIN_GPU: {
450 if (uap->who == 0)
451 return (EINVAL);
452
453 p = proc_find(uap->who);
454 if (p == PROC_NULL)
455 break;
456
457 error = set_gpudeny_proc(curp, p, uap->prio);
458
459 found++;
460 proc_rele(p);
461 break;
462 }
463
464 case PRIO_DARWIN_ROLE: {
465 if (uap->who == 0) {
466 p = curp;
467 } else {
468 p = proc_find(uap->who);
469 if (p == PROC_NULL)
470 break;
471 refheld = 1;
472 }
473
474 error = proc_set_darwin_role(curp, p, uap->prio);
475
476 found++;
477 if (refheld != 0)
478 proc_rele(p);
479 break;
480 }
481
482 default:
483 return (EINVAL);
484 }
485 if (found == 0)
486 return (ESRCH);
487 if (error == EIDRM) {
488 *retval = -2;
489 error = 0;
490 }
491 return (error);
492 }
493
494
495 /*
496 * Returns: 0 Success
497 * EPERM
498 * EACCES
499 * mac_check_proc_sched:???
500 */
501 int
502 donice(struct proc *curp, struct proc *chgp, int n)
503 {
504 int error = 0;
505 kauth_cred_t ucred;
506 kauth_cred_t my_cred;
507
508 ucred = kauth_cred_proc_ref(curp);
509 my_cred = kauth_cred_proc_ref(chgp);
510
511 if (suser(ucred, NULL) && kauth_cred_getruid(ucred) &&
512 kauth_cred_getuid(ucred) != kauth_cred_getuid(my_cred) &&
513 kauth_cred_getruid(ucred) != kauth_cred_getuid(my_cred)) {
514 error = EPERM;
515 goto out;
516 }
517 if (n > PRIO_MAX)
518 n = PRIO_MAX;
519 if (n < PRIO_MIN)
520 n = PRIO_MIN;
521 if (n < chgp->p_nice && suser(ucred, &curp->p_acflag)) {
522 error = EACCES;
523 goto out;
524 }
525 #if CONFIG_MACF
526 error = mac_proc_check_sched(curp, chgp);
527 if (error)
528 goto out;
529 #endif
530 proc_lock(chgp);
531 chgp->p_nice = n;
532 proc_unlock(chgp);
533 (void)resetpriority(chgp);
534 out:
535 kauth_cred_unref(&ucred);
536 kauth_cred_unref(&my_cred);
537 return (error);
538 }
539
540 static int
541 set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority)
542 {
543 int error = 0;
544 kauth_cred_t ucred;
545 kauth_cred_t target_cred;
546
547 ucred = kauth_cred_get();
548 target_cred = kauth_cred_proc_ref(targetp);
549
550 /* TODO: Entitlement instead of uid check */
551
552 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
553 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
554 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
555 error = EPERM;
556 goto out;
557 }
558
559 if (curp == targetp) {
560 error = EPERM;
561 goto out;
562 }
563
564 #if CONFIG_MACF
565 error = mac_proc_check_sched(curp, targetp);
566 if (error)
567 goto out;
568 #endif
569
570 switch (priority) {
571 case PRIO_DARWIN_GPU_DENY:
572 task_set_gpu_denied(proc_task(targetp), TRUE);
573 break;
574 case PRIO_DARWIN_GPU_ALLOW:
575 task_set_gpu_denied(proc_task(targetp), FALSE);
576 break;
577 default:
578 error = EINVAL;
579 goto out;
580 }
581
582 out:
583 kauth_cred_unref(&target_cred);
584 return (error);
585
586 }
587
588 static int
589 proc_set_darwin_role(proc_t curp, proc_t targetp, int priority)
590 {
591 int error = 0;
592 uint32_t flagsp = 0;
593
594 kauth_cred_t ucred, target_cred;
595
596 ucred = kauth_cred_get();
597 target_cred = kauth_cred_proc_ref(targetp);
598
599 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
600 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
601 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
602 if (priv_check_cred(ucred, PRIV_SETPRIORITY_DARWIN_ROLE, 0) != 0) {
603 error = EPERM;
604 goto out;
605 }
606 }
607
608 if (curp != targetp) {
609 #if CONFIG_MACF
610 if ((error = mac_proc_check_sched(curp, targetp)))
611 goto out;
612 #endif
613 }
614
615 proc_get_darwinbgstate(proc_task(targetp), &flagsp);
616 if ((flagsp & PROC_FLAG_APPLICATION) != PROC_FLAG_APPLICATION) {
617 error = ENOTSUP;
618 goto out;
619 }
620
621 integer_t role = 0;
622
623 if ((error = proc_darwin_role_to_task_role(priority, &role)))
624 goto out;
625
626 proc_set_task_policy(proc_task(targetp), TASK_POLICY_ATTRIBUTE,
627 TASK_POLICY_ROLE, role);
628
629 out:
630 kauth_cred_unref(&target_cred);
631 return (error);
632 }
633
634 static int
635 proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority)
636 {
637 int error = 0;
638 int role = 0;
639
640 kauth_cred_t ucred, target_cred;
641
642 ucred = kauth_cred_get();
643 target_cred = kauth_cred_proc_ref(targetp);
644
645 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
646 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
647 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
648 error = EPERM;
649 goto out;
650 }
651
652 if (curp != targetp) {
653 #if CONFIG_MACF
654 if ((error = mac_proc_check_sched(curp, targetp)))
655 goto out;
656 #endif
657 }
658
659 role = proc_get_task_policy(proc_task(targetp), TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE);
660
661 *priority = proc_task_role_to_darwin_role(role);
662
663 out:
664 kauth_cred_unref(&target_cred);
665 return (error);
666 }
667
668
669 static int
670 get_background_proc(struct proc *curp, struct proc *targetp, int *priority)
671 {
672 int external = 0;
673 int error = 0;
674 kauth_cred_t ucred, target_cred;
675
676 ucred = kauth_cred_get();
677 target_cred = kauth_cred_proc_ref(targetp);
678
679 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
680 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
681 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
682 error = EPERM;
683 goto out;
684 }
685
686 external = (curp == targetp) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
687
688 *priority = proc_get_task_policy(current_task(), external, TASK_POLICY_DARWIN_BG);
689
690 out:
691 kauth_cred_unref(&target_cred);
692 return (error);
693 }
694
695 static int
696 do_background_proc(struct proc *curp, struct proc *targetp, int priority)
697 {
698 #if !CONFIG_MACF
699 #pragma unused(curp)
700 #endif
701 int error = 0;
702 kauth_cred_t ucred;
703 kauth_cred_t target_cred;
704 int external;
705 int enable;
706
707 ucred = kauth_cred_get();
708 target_cred = kauth_cred_proc_ref(targetp);
709
710 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
711 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
712 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred))
713 {
714 error = EPERM;
715 goto out;
716 }
717
718 #if CONFIG_MACF
719 error = mac_proc_check_sched(curp, targetp);
720 if (error)
721 goto out;
722 #endif
723
724 external = (curp == targetp) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
725
726 switch (priority) {
727 case PRIO_DARWIN_BG:
728 enable = TASK_POLICY_ENABLE;
729 break;
730 case PRIO_DARWIN_NONUI:
731 /* ignored for compatibility */
732 goto out;
733 default:
734 /* TODO: EINVAL if priority != 0 */
735 enable = TASK_POLICY_DISABLE;
736 break;
737 }
738
739 proc_set_task_policy(proc_task(targetp), external, TASK_POLICY_DARWIN_BG, enable);
740
741 out:
742 kauth_cred_unref(&target_cred);
743 return (error);
744 }
745
746 static void
747 do_background_socket(struct proc *p, thread_t thread)
748 {
749 #if SOCKETS
750 struct filedesc *fdp;
751 struct fileproc *fp;
752 int i, background;
753
754 proc_fdlock(p);
755
756 if (thread != THREAD_NULL)
757 background = proc_get_effective_thread_policy(thread, TASK_POLICY_ALL_SOCKETS_BG);
758 else
759 background = proc_get_effective_task_policy(proc_task(p), TASK_POLICY_ALL_SOCKETS_BG);
760
761 if (background) {
762 /*
763 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
764 * the sockets with the background flag. There's nothing
765 * to do here for the PRIO_DARWIN_THREAD case.
766 */
767 if (thread == THREAD_NULL) {
768 fdp = p->p_fd;
769
770 for (i = 0; i < fdp->fd_nfiles; i++) {
771 fp = fdp->fd_ofiles[i];
772 if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0) {
773 continue;
774 }
775 if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_SOCKET) {
776 struct socket *sockp = (struct socket *)fp->f_fglob->fg_data;
777 socket_set_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
778 sockp->so_background_thread = NULL;
779 }
780 #if NECP
781 else if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_NETPOLICY) {
782 necp_set_client_as_background(p, fp, background);
783 }
784 #endif /* NECP */
785 }
786 }
787 } else {
788 /* disable networking IO throttle.
789 * NOTE - It is a known limitation of the current design that we
790 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
791 * sockets created by other threads within this process.
792 */
793 fdp = p->p_fd;
794 for ( i = 0; i < fdp->fd_nfiles; i++ ) {
795 struct socket *sockp;
796
797 fp = fdp->fd_ofiles[ i ];
798 if (fp == NULL || (fdp->fd_ofileflags[ i ] & UF_RESERVED) != 0) {
799 continue;
800 }
801 if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_SOCKET) {
802 sockp = (struct socket *)fp->f_fglob->fg_data;
803 /* skip if only clearing this thread's sockets */
804 if ((thread) && (sockp->so_background_thread != thread)) {
805 continue;
806 }
807 socket_clear_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
808 sockp->so_background_thread = NULL;
809 }
810 #if NECP
811 else if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_NETPOLICY) {
812 necp_set_client_as_background(p, fp, background);
813 }
814 #endif /* NECP */
815 }
816 }
817
818 proc_fdunlock(p);
819 #else
820 #pragma unused(p, thread)
821 #endif
822 }
823
824
825 /*
826 * do_background_thread
827 *
828 * Requires: thread reference
829 *
830 * Returns: 0 Success
831 * EPERM Tried to background while in vfork
832 * XXX - todo - does this need a MACF hook?
833 */
834 static int
835 do_background_thread(thread_t thread, int priority)
836 {
837 struct uthread *ut;
838 int enable, external;
839 int rv = 0;
840
841 ut = get_bsdthread_info(thread);
842
843 /* Backgrounding is unsupported for threads in vfork */
844 if ((ut->uu_flag & UT_VFORK) != 0)
845 return(EPERM);
846
847 /* Backgrounding is unsupported for workq threads */
848 if (thread_is_static_param(thread)) {
849 return(EPERM);
850 }
851
852 /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
853 if (thread_has_qos_policy(thread)) {
854 thread_remove_qos_policy(thread);
855 rv = EIDRM;
856 }
857
858 /* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
859 enable = (priority == PRIO_DARWIN_BG) ? TASK_POLICY_ENABLE : TASK_POLICY_DISABLE;
860 external = (current_thread() == thread) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
861
862 proc_set_thread_policy(thread, external, TASK_POLICY_DARWIN_BG, enable);
863
864 return rv;
865 }
866
867
868 /*
869 * Returns: 0 Success
870 * copyin:EFAULT
871 * dosetrlimit:
872 */
873 /* ARGSUSED */
874 int
875 setrlimit(struct proc *p, struct setrlimit_args *uap, __unused int32_t *retval)
876 {
877 struct rlimit alim;
878 int error;
879
880 if ((error = copyin(uap->rlp, (caddr_t)&alim,
881 sizeof (struct rlimit))))
882 return (error);
883
884 return (dosetrlimit(p, uap->which, &alim));
885 }
886
887 /*
888 * Returns: 0 Success
889 * EINVAL
890 * ENOMEM Cannot copy limit structure
891 * suser:EPERM
892 *
893 * Notes: EINVAL is returned both for invalid arguments, and in the
894 * case that the current usage (e.g. RLIMIT_STACK) is already
895 * in excess of the requested limit.
896 */
897 int
898 dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
899 {
900 struct rlimit *alimp;
901 int error;
902 kern_return_t kr;
903 int posix = (which & _RLIMIT_POSIX_FLAG) ? 1 : 0;
904
905 /* Mask out POSIX flag, saved above */
906 which &= ~_RLIMIT_POSIX_FLAG;
907
908 if (which >= RLIM_NLIMITS)
909 return (EINVAL);
910
911 alimp = &p->p_rlimit[which];
912 if (limp->rlim_cur > limp->rlim_max)
913 return EINVAL;
914
915 if (limp->rlim_cur > alimp->rlim_max ||
916 limp->rlim_max > alimp->rlim_max)
917 if ((error = suser(kauth_cred_get(), &p->p_acflag))) {
918 return (error);
919 }
920
921 proc_limitblock(p);
922
923 if ((error = proc_limitreplace(p)) != 0) {
924 proc_limitunblock(p);
925 return(error);
926 }
927
928 alimp = &p->p_rlimit[which];
929
930 switch (which) {
931
932 case RLIMIT_CPU:
933 if (limp->rlim_cur == RLIM_INFINITY) {
934 task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
935 timerclear(&p->p_rlim_cpu);
936 }
937 else {
938 task_absolutetime_info_data_t tinfo;
939 mach_msg_type_number_t count;
940 struct timeval ttv, tv;
941 clock_sec_t tv_sec;
942 clock_usec_t tv_usec;
943
944 count = TASK_ABSOLUTETIME_INFO_COUNT;
945 task_info(p->task, TASK_ABSOLUTETIME_INFO,
946 (task_info_t)&tinfo, &count);
947 absolutetime_to_microtime(tinfo.total_user + tinfo.total_system,
948 &tv_sec, &tv_usec);
949 ttv.tv_sec = tv_sec;
950 ttv.tv_usec = tv_usec;
951
952 tv.tv_sec = (limp->rlim_cur > __INT_MAX__ ? __INT_MAX__ : limp->rlim_cur);
953 tv.tv_usec = 0;
954 timersub(&tv, &ttv, &p->p_rlim_cpu);
955
956 timerclear(&tv);
957 if (timercmp(&p->p_rlim_cpu, &tv, >))
958 task_vtimer_set(p->task, TASK_VTIMER_RLIM);
959 else {
960 task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
961
962 timerclear(&p->p_rlim_cpu);
963
964 psignal(p, SIGXCPU);
965 }
966 }
967 break;
968
969 case RLIMIT_DATA:
970 if (limp->rlim_cur > maxdmap)
971 limp->rlim_cur = maxdmap;
972 if (limp->rlim_max > maxdmap)
973 limp->rlim_max = maxdmap;
974 break;
975
976 case RLIMIT_STACK:
977 /* Disallow illegal stack size instead of clipping */
978 if (limp->rlim_cur > maxsmap ||
979 limp->rlim_max > maxsmap) {
980 if (posix) {
981 error = EINVAL;
982 goto out;
983 }
984 else {
985 /*
986 * 4797860 - workaround poorly written installers by
987 * doing previous implementation (< 10.5) when caller
988 * is non-POSIX conforming.
989 */
990 if (limp->rlim_cur > maxsmap)
991 limp->rlim_cur = maxsmap;
992 if (limp->rlim_max > maxsmap)
993 limp->rlim_max = maxsmap;
994 }
995 }
996
997 /*
998 * Stack is allocated to the max at exec time with only
999 * "rlim_cur" bytes accessible. If stack limit is going
1000 * up make more accessible, if going down make inaccessible.
1001 */
1002 if (limp->rlim_cur > alimp->rlim_cur) {
1003 user_addr_t addr;
1004 user_size_t size;
1005
1006 /* grow stack */
1007 size = round_page_64(limp->rlim_cur);
1008 size -= round_page_64(alimp->rlim_cur);
1009
1010 addr = p->user_stack - round_page_64(limp->rlim_cur);
1011 kr = mach_vm_protect(current_map(),
1012 addr, size,
1013 FALSE, VM_PROT_DEFAULT);
1014 if (kr != KERN_SUCCESS) {
1015 error = EINVAL;
1016 goto out;
1017 }
1018 } else if (limp->rlim_cur < alimp->rlim_cur) {
1019 user_addr_t addr;
1020 user_size_t size;
1021 user_addr_t cur_sp;
1022
1023 /* shrink stack */
1024
1025 /*
1026 * First check if new stack limit would agree
1027 * with current stack usage.
1028 * Get the current thread's stack pointer...
1029 */
1030 cur_sp = thread_adjuserstack(current_thread(),
1031 0);
1032 if (cur_sp <= p->user_stack &&
1033 cur_sp > (p->user_stack -
1034 round_page_64(alimp->rlim_cur))) {
1035 /* stack pointer is in main stack */
1036 if (cur_sp <= (p->user_stack -
1037 round_page_64(limp->rlim_cur))) {
1038 /*
1039 * New limit would cause
1040 * current usage to be invalid:
1041 * reject new limit.
1042 */
1043 error = EINVAL;
1044 goto out;
1045 }
1046 } else {
1047 /* not on the main stack: reject */
1048 error = EINVAL;
1049 goto out;
1050 }
1051
1052 size = round_page_64(alimp->rlim_cur);
1053 size -= round_page_64(limp->rlim_cur);
1054
1055 addr = p->user_stack - round_page_64(alimp->rlim_cur);
1056
1057 kr = mach_vm_protect(current_map(),
1058 addr, size,
1059 FALSE, VM_PROT_NONE);
1060 if (kr != KERN_SUCCESS) {
1061 error = EINVAL;
1062 goto out;
1063 }
1064 } else {
1065 /* no change ... */
1066 }
1067 break;
1068
1069 case RLIMIT_NOFILE:
1070 /*
1071 * Only root can set the maxfiles limits, as it is
1072 * systemwide resource. If we are expecting POSIX behavior,
1073 * instead of clamping the value, return EINVAL. We do this
1074 * because historically, people have been able to attempt to
1075 * set RLIM_INFINITY to get "whatever the maximum is".
1076 */
1077 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1078 if (limp->rlim_cur != alimp->rlim_cur &&
1079 limp->rlim_cur > (rlim_t)maxfiles) {
1080 if (posix) {
1081 error = EINVAL;
1082 goto out;
1083 }
1084 limp->rlim_cur = maxfiles;
1085 }
1086 if (limp->rlim_max != alimp->rlim_max &&
1087 limp->rlim_max > (rlim_t)maxfiles)
1088 limp->rlim_max = maxfiles;
1089 }
1090 else {
1091 if (limp->rlim_cur != alimp->rlim_cur &&
1092 limp->rlim_cur > (rlim_t)maxfilesperproc) {
1093 if (posix) {
1094 error = EINVAL;
1095 goto out;
1096 }
1097 limp->rlim_cur = maxfilesperproc;
1098 }
1099 if (limp->rlim_max != alimp->rlim_max &&
1100 limp->rlim_max > (rlim_t)maxfilesperproc)
1101 limp->rlim_max = maxfilesperproc;
1102 }
1103 break;
1104
1105 case RLIMIT_NPROC:
1106 /*
1107 * Only root can set to the maxproc limits, as it is
1108 * systemwide resource; all others are limited to
1109 * maxprocperuid (presumably less than maxproc).
1110 */
1111 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1112 if (limp->rlim_cur > (rlim_t)maxproc)
1113 limp->rlim_cur = maxproc;
1114 if (limp->rlim_max > (rlim_t)maxproc)
1115 limp->rlim_max = maxproc;
1116 }
1117 else {
1118 if (limp->rlim_cur > (rlim_t)maxprocperuid)
1119 limp->rlim_cur = maxprocperuid;
1120 if (limp->rlim_max > (rlim_t)maxprocperuid)
1121 limp->rlim_max = maxprocperuid;
1122 }
1123 break;
1124
1125 case RLIMIT_MEMLOCK:
1126 /*
1127 * Tell the Mach VM layer about the new limit value.
1128 */
1129
1130 vm_map_set_user_wire_limit(current_map(), limp->rlim_cur);
1131 break;
1132
1133 } /* switch... */
1134 proc_lock(p);
1135 *alimp = *limp;
1136 proc_unlock(p);
1137 error = 0;
1138 out:
1139 proc_limitunblock(p);
1140 return (error);
1141 }
1142
1143 /* ARGSUSED */
1144 int
1145 getrlimit(struct proc *p, struct getrlimit_args *uap, __unused int32_t *retval)
1146 {
1147 struct rlimit lim = {};
1148
1149 /*
1150 * Take out flag now in case we need to use it to trigger variant
1151 * behaviour later.
1152 */
1153 uap->which &= ~_RLIMIT_POSIX_FLAG;
1154
1155 if (uap->which >= RLIM_NLIMITS)
1156 return (EINVAL);
1157 proc_limitget(p, uap->which, &lim);
1158 return (copyout((caddr_t)&lim,
1159 uap->rlp, sizeof (struct rlimit)));
1160 }
1161
1162 /*
1163 * Transform the running time and tick information in proc p into user,
1164 * system, and interrupt time usage.
1165 */
1166 /* No lock on proc is held for this.. */
1167 void
1168 calcru(struct proc *p, struct timeval *up, struct timeval *sp, struct timeval *ip)
1169 {
1170 task_t task;
1171
1172 timerclear(up);
1173 timerclear(sp);
1174 if (ip != NULL)
1175 timerclear(ip);
1176
1177 task = p->task;
1178 if (task) {
1179 mach_task_basic_info_data_t tinfo;
1180 task_thread_times_info_data_t ttimesinfo;
1181 task_events_info_data_t teventsinfo;
1182 mach_msg_type_number_t task_info_count, task_ttimes_count;
1183 mach_msg_type_number_t task_events_count;
1184 struct timeval ut,st;
1185
1186 task_info_count = MACH_TASK_BASIC_INFO_COUNT;
1187 task_info(task, MACH_TASK_BASIC_INFO,
1188 (task_info_t)&tinfo, &task_info_count);
1189 ut.tv_sec = tinfo.user_time.seconds;
1190 ut.tv_usec = tinfo.user_time.microseconds;
1191 st.tv_sec = tinfo.system_time.seconds;
1192 st.tv_usec = tinfo.system_time.microseconds;
1193 timeradd(&ut, up, up);
1194 timeradd(&st, sp, sp);
1195
1196 task_ttimes_count = TASK_THREAD_TIMES_INFO_COUNT;
1197 task_info(task, TASK_THREAD_TIMES_INFO,
1198 (task_info_t)&ttimesinfo, &task_ttimes_count);
1199
1200 ut.tv_sec = ttimesinfo.user_time.seconds;
1201 ut.tv_usec = ttimesinfo.user_time.microseconds;
1202 st.tv_sec = ttimesinfo.system_time.seconds;
1203 st.tv_usec = ttimesinfo.system_time.microseconds;
1204 timeradd(&ut, up, up);
1205 timeradd(&st, sp, sp);
1206
1207 task_events_count = TASK_EVENTS_INFO_COUNT;
1208 task_info(task, TASK_EVENTS_INFO,
1209 (task_info_t)&teventsinfo, &task_events_count);
1210
1211 /*
1212 * No need to lock "p": this does not need to be
1213 * completely consistent, right ?
1214 */
1215 p->p_stats->p_ru.ru_minflt = (teventsinfo.faults -
1216 teventsinfo.pageins);
1217 p->p_stats->p_ru.ru_majflt = teventsinfo.pageins;
1218 p->p_stats->p_ru.ru_nivcsw = (teventsinfo.csw -
1219 p->p_stats->p_ru.ru_nvcsw);
1220 if (p->p_stats->p_ru.ru_nivcsw < 0)
1221 p->p_stats->p_ru.ru_nivcsw = 0;
1222
1223 p->p_stats->p_ru.ru_maxrss = tinfo.resident_size_max;
1224 }
1225 }
1226
1227 __private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
1228 __private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
1229
1230 /* ARGSUSED */
1231 int
1232 getrusage(struct proc *p, struct getrusage_args *uap, __unused int32_t *retval)
1233 {
1234 struct rusage *rup, rubuf;
1235 struct user64_rusage rubuf64 = {};
1236 struct user32_rusage rubuf32 = {};
1237 size_t retsize = sizeof(rubuf); /* default: 32 bits */
1238 caddr_t retbuf = (caddr_t)&rubuf; /* default: 32 bits */
1239 struct timeval utime;
1240 struct timeval stime;
1241
1242
1243 switch (uap->who) {
1244 case RUSAGE_SELF:
1245 calcru(p, &utime, &stime, NULL);
1246 proc_lock(p);
1247 rup = &p->p_stats->p_ru;
1248 rup->ru_utime = utime;
1249 rup->ru_stime = stime;
1250
1251 rubuf = *rup;
1252 proc_unlock(p);
1253
1254 break;
1255
1256 case RUSAGE_CHILDREN:
1257 proc_lock(p);
1258 rup = &p->p_stats->p_cru;
1259 rubuf = *rup;
1260 proc_unlock(p);
1261 break;
1262
1263 default:
1264 return (EINVAL);
1265 }
1266 if (IS_64BIT_PROCESS(p)) {
1267 retsize = sizeof(rubuf64);
1268 retbuf = (caddr_t)&rubuf64;
1269 munge_user64_rusage(&rubuf, &rubuf64);
1270 } else {
1271 retsize = sizeof(rubuf32);
1272 retbuf = (caddr_t)&rubuf32;
1273 munge_user32_rusage(&rubuf, &rubuf32);
1274 }
1275
1276 return (copyout(retbuf, uap->rusage, retsize));
1277 }
1278
1279 void
1280 ruadd(struct rusage *ru, struct rusage *ru2)
1281 {
1282 long *ip, *ip2;
1283 long i;
1284
1285 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
1286 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
1287 if (ru->ru_maxrss < ru2->ru_maxrss)
1288 ru->ru_maxrss = ru2->ru_maxrss;
1289 ip = &ru->ru_first; ip2 = &ru2->ru_first;
1290 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1291 *ip++ += *ip2++;
1292 }
1293
1294 /*
1295 * Add the rusage stats of child in parent.
1296 *
1297 * It adds rusage statistics of child process and statistics of all its
1298 * children to its parent.
1299 *
1300 * Note: proc lock of parent should be held while calling this function.
1301 */
1302 void
1303 update_rusage_info_child(struct rusage_info_child *ri, rusage_info_current *ri_current)
1304 {
1305 ri->ri_child_user_time += (ri_current->ri_user_time +
1306 ri_current->ri_child_user_time);
1307 ri->ri_child_system_time += (ri_current->ri_system_time +
1308 ri_current->ri_child_system_time);
1309 ri->ri_child_pkg_idle_wkups += (ri_current->ri_pkg_idle_wkups +
1310 ri_current->ri_child_pkg_idle_wkups);
1311 ri->ri_child_interrupt_wkups += (ri_current->ri_interrupt_wkups +
1312 ri_current->ri_child_interrupt_wkups);
1313 ri->ri_child_pageins += (ri_current->ri_pageins +
1314 ri_current->ri_child_pageins);
1315 ri->ri_child_elapsed_abstime += ((ri_current->ri_proc_exit_abstime -
1316 ri_current->ri_proc_start_abstime) + ri_current->ri_child_elapsed_abstime);
1317 }
1318
1319 void
1320 proc_limitget(proc_t p, int which, struct rlimit * limp)
1321 {
1322 proc_list_lock();
1323 limp->rlim_cur = p->p_rlimit[which].rlim_cur;
1324 limp->rlim_max = p->p_rlimit[which].rlim_max;
1325 proc_list_unlock();
1326 }
1327
1328
1329 void
1330 proc_limitdrop(proc_t p, int exiting)
1331 {
1332 struct plimit * freelim = NULL;
1333 struct plimit * freeoldlim = NULL;
1334
1335 proc_list_lock();
1336
1337 if (--p->p_limit->pl_refcnt == 0) {
1338 freelim = p->p_limit;
1339 p->p_limit = NULL;
1340 }
1341 if ((exiting != 0) && (p->p_olimit != NULL) && (--p->p_olimit->pl_refcnt == 0)) {
1342 freeoldlim = p->p_olimit;
1343 p->p_olimit = NULL;
1344 }
1345
1346 proc_list_unlock();
1347 if (freelim != NULL)
1348 FREE_ZONE(freelim, sizeof *p->p_limit, M_PLIMIT);
1349 if (freeoldlim != NULL)
1350 FREE_ZONE(freeoldlim, sizeof *p->p_olimit, M_PLIMIT);
1351 }
1352
1353
1354 void
1355 proc_limitfork(proc_t parent, proc_t child)
1356 {
1357 proc_list_lock();
1358 child->p_limit = parent->p_limit;
1359 child->p_limit->pl_refcnt++;
1360 child->p_olimit = NULL;
1361 proc_list_unlock();
1362 }
1363
1364 void
1365 proc_limitblock(proc_t p)
1366 {
1367 proc_lock(p);
1368 while (p->p_lflag & P_LLIMCHANGE) {
1369 p->p_lflag |= P_LLIMWAIT;
1370 msleep(&p->p_olimit, &p->p_mlock, 0, "proc_limitblock", NULL);
1371 }
1372 p->p_lflag |= P_LLIMCHANGE;
1373 proc_unlock(p);
1374
1375 }
1376
1377
1378 void
1379 proc_limitunblock(proc_t p)
1380 {
1381 proc_lock(p);
1382 p->p_lflag &= ~P_LLIMCHANGE;
1383 if (p->p_lflag & P_LLIMWAIT) {
1384 p->p_lflag &= ~P_LLIMWAIT;
1385 wakeup(&p->p_olimit);
1386 }
1387 proc_unlock(p);
1388 }
1389
1390 /* This is called behind serialization provided by proc_limitblock/unlbock */
1391 int
1392 proc_limitreplace(proc_t p)
1393 {
1394 struct plimit *copy;
1395
1396
1397 proc_list_lock();
1398
1399 if (p->p_limit->pl_refcnt == 1) {
1400 proc_list_unlock();
1401 return(0);
1402 }
1403
1404 proc_list_unlock();
1405
1406 MALLOC_ZONE(copy, struct plimit *,
1407 sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1408 if (copy == NULL) {
1409 return(ENOMEM);
1410 }
1411
1412 proc_list_lock();
1413 bcopy(p->p_limit->pl_rlimit, copy->pl_rlimit,
1414 sizeof(struct rlimit) * RLIM_NLIMITS);
1415 copy->pl_refcnt = 1;
1416 /* hang on to reference to old till process exits */
1417 p->p_olimit = p->p_limit;
1418 p->p_limit = copy;
1419 proc_list_unlock();
1420
1421 return(0);
1422 }
1423
1424 static int
1425 iopolicysys_disk(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
1426 static int
1427 iopolicysys_vfs_hfs_case_sensitivity(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
1428 static int
1429 iopolicysys_vfs_atime_updates(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
1430
1431 /*
1432 * iopolicysys
1433 *
1434 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1435 *
1436 * Parameters: cmd Policy command
1437 * arg Pointer to policy arguments
1438 *
1439 * Returns: 0 Success
1440 * EINVAL Invalid command or invalid policy arguments
1441 *
1442 */
1443 int
1444 iopolicysys(struct proc *p, struct iopolicysys_args *uap, int32_t *retval)
1445 {
1446 int error = 0;
1447 struct _iopol_param_t iop_param;
1448
1449 if ((error = copyin(uap->arg, &iop_param, sizeof(iop_param))) != 0)
1450 goto out;
1451
1452 switch (iop_param.iop_iotype) {
1453 case IOPOL_TYPE_DISK:
1454 error = iopolicysys_disk(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
1455 if (error == EIDRM) {
1456 *retval = -2;
1457 error = 0;
1458 }
1459 if (error)
1460 goto out;
1461 break;
1462 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY:
1463 error = iopolicysys_vfs_hfs_case_sensitivity(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
1464 if (error)
1465 goto out;
1466 break;
1467 case IOPOL_TYPE_VFS_ATIME_UPDATES:
1468 error = iopolicysys_vfs_atime_updates(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
1469 if (error)
1470 goto out;
1471 break;
1472 default:
1473 error = EINVAL;
1474 goto out;
1475 }
1476
1477 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1478 if (uap->cmd == IOPOL_CMD_GET) {
1479 error = copyout((caddr_t)&iop_param, uap->arg, sizeof(iop_param));
1480 if (error)
1481 goto out;
1482 }
1483
1484 out:
1485 return (error);
1486 }
1487
1488 static int
1489 iopolicysys_disk(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
1490 {
1491 int error = 0;
1492 thread_t thread;
1493 int policy_flavor;
1494
1495 /* Validate scope */
1496 switch (scope) {
1497 case IOPOL_SCOPE_PROCESS:
1498 thread = THREAD_NULL;
1499 policy_flavor = TASK_POLICY_IOPOL;
1500 break;
1501
1502 case IOPOL_SCOPE_THREAD:
1503 thread = current_thread();
1504 policy_flavor = TASK_POLICY_IOPOL;
1505
1506 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1507 if (cmd == IOPOL_CMD_SET && thread_has_qos_policy(thread)) {
1508 switch (policy) {
1509 case IOPOL_DEFAULT:
1510 case IOPOL_PASSIVE:
1511 break;
1512 case IOPOL_UTILITY:
1513 case IOPOL_THROTTLE:
1514 case IOPOL_IMPORTANT:
1515 case IOPOL_STANDARD:
1516 if (!thread_is_static_param(thread)) {
1517 thread_remove_qos_policy(thread);
1518 /*
1519 * This is not an error case, this is to return a marker to user-space that
1520 * we stripped the thread of its QoS class.
1521 */
1522 error = EIDRM;
1523 break;
1524 }
1525 /* otherwise, fall through to the error case. */
1526 default:
1527 error = EINVAL;
1528 goto out;
1529 }
1530 }
1531 break;
1532
1533 case IOPOL_SCOPE_DARWIN_BG:
1534 #if CONFIG_EMBEDDED
1535 /* Embedded doesn't want this as BG is always IOPOL_THROTTLE */
1536 error = ENOTSUP;
1537 goto out;
1538 #else /* CONFIG_EMBEDDED */
1539 thread = THREAD_NULL;
1540 policy_flavor = TASK_POLICY_DARWIN_BG_IOPOL;
1541 break;
1542 #endif /* CONFIG_EMBEDDED */
1543
1544 default:
1545 error = EINVAL;
1546 goto out;
1547 }
1548
1549 /* Validate policy */
1550 if (cmd == IOPOL_CMD_SET) {
1551 switch (policy) {
1552 case IOPOL_DEFAULT:
1553 if (scope == IOPOL_SCOPE_DARWIN_BG) {
1554 /* the current default BG throttle level is UTILITY */
1555 policy = IOPOL_UTILITY;
1556 } else {
1557 policy = IOPOL_IMPORTANT;
1558 }
1559 break;
1560 case IOPOL_UTILITY:
1561 /* fall-through */
1562 case IOPOL_THROTTLE:
1563 /* These levels are OK */
1564 break;
1565 case IOPOL_IMPORTANT:
1566 /* fall-through */
1567 case IOPOL_STANDARD:
1568 /* fall-through */
1569 case IOPOL_PASSIVE:
1570 if (scope == IOPOL_SCOPE_DARWIN_BG) {
1571 /* These levels are invalid for BG */
1572 error = EINVAL;
1573 goto out;
1574 } else {
1575 /* OK for other scopes */
1576 }
1577 break;
1578 default:
1579 error = EINVAL;
1580 goto out;
1581 }
1582 }
1583
1584 /* Perform command */
1585 switch(cmd) {
1586 case IOPOL_CMD_SET:
1587 if (thread != THREAD_NULL)
1588 proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor, policy);
1589 else
1590 proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor, policy);
1591 break;
1592 case IOPOL_CMD_GET:
1593 if (thread != THREAD_NULL)
1594 policy = proc_get_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor);
1595 else
1596 policy = proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor);
1597 iop_param->iop_policy = policy;
1598 break;
1599 default:
1600 error = EINVAL; /* unknown command */
1601 break;
1602 }
1603
1604 out:
1605 return (error);
1606 }
1607
1608 static int
1609 iopolicysys_vfs_hfs_case_sensitivity(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
1610 {
1611 int error = 0;
1612
1613 /* Validate scope */
1614 switch (scope) {
1615 case IOPOL_SCOPE_PROCESS:
1616 /* Only process OK */
1617 break;
1618 default:
1619 error = EINVAL;
1620 goto out;
1621 }
1622
1623 /* Validate policy */
1624 if (cmd == IOPOL_CMD_SET) {
1625 switch (policy) {
1626 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
1627 /* fall-through */
1628 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
1629 /* These policies are OK */
1630 break;
1631 default:
1632 error = EINVAL;
1633 goto out;
1634 }
1635 }
1636
1637 /* Perform command */
1638 switch(cmd) {
1639 case IOPOL_CMD_SET:
1640 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1641 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1642 boolean_t entitled = FALSE;
1643 entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1644 if (!entitled) {
1645 error = EPERM;
1646 goto out;
1647 }
1648 }
1649
1650 switch (policy) {
1651 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
1652 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY), &p->p_vfs_iopolicy);
1653 break;
1654 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
1655 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY, &p->p_vfs_iopolicy);
1656 break;
1657 default:
1658 error = EINVAL;
1659 goto out;
1660 }
1661
1662 break;
1663 case IOPOL_CMD_GET:
1664 iop_param->iop_policy = (p->p_vfs_iopolicy & P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY)
1665 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1666 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT;
1667 break;
1668 default:
1669 error = EINVAL; /* unknown command */
1670 break;
1671 }
1672
1673 out:
1674 return (error);
1675 }
1676
1677 static inline int
1678 get_thread_atime_policy(struct uthread *ut)
1679 {
1680 return (ut->uu_flag & UT_ATIME_UPDATE)? IOPOL_ATIME_UPDATES_OFF: IOPOL_ATIME_UPDATES_DEFAULT;
1681 }
1682
1683 static inline void
1684 set_thread_atime_policy(struct uthread *ut, int policy)
1685 {
1686 if (policy == IOPOL_ATIME_UPDATES_OFF) {
1687 ut->uu_flag |= UT_ATIME_UPDATE;
1688 } else {
1689 ut->uu_flag &= ~UT_ATIME_UPDATE;
1690 }
1691 }
1692
1693 static inline void
1694 set_task_atime_policy(struct proc *p, int policy)
1695 {
1696 if (policy == IOPOL_ATIME_UPDATES_OFF) {
1697 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES, &p->p_vfs_iopolicy);
1698 } else {
1699 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES), &p->p_vfs_iopolicy);
1700 }
1701 }
1702
1703 static inline int
1704 get_task_atime_policy(struct proc *p)
1705 {
1706 return (p->p_vfs_iopolicy & P_VFS_IOPOLICY_ATIME_UPDATES)? IOPOL_ATIME_UPDATES_OFF: IOPOL_ATIME_UPDATES_DEFAULT;
1707 }
1708
1709 static int
1710 iopolicysys_vfs_atime_updates(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
1711 {
1712 int error = 0;
1713 thread_t thread;
1714
1715 /* Validate scope */
1716 switch (scope) {
1717 case IOPOL_SCOPE_THREAD:
1718 thread = current_thread();
1719 break;
1720 case IOPOL_SCOPE_PROCESS:
1721 thread = THREAD_NULL;
1722 break;
1723 default:
1724 error = EINVAL;
1725 goto out;
1726 }
1727
1728 /* Validate policy */
1729 if (cmd == IOPOL_CMD_SET) {
1730 switch (policy) {
1731 case IOPOL_ATIME_UPDATES_DEFAULT:
1732 case IOPOL_ATIME_UPDATES_OFF:
1733 break;
1734 default:
1735 error = EINVAL;
1736 goto out;
1737 }
1738 }
1739
1740 /* Perform command */
1741 switch(cmd) {
1742 case IOPOL_CMD_SET:
1743 if (thread != THREAD_NULL)
1744 set_thread_atime_policy(get_bsdthread_info(thread), policy);
1745 else
1746 set_task_atime_policy(p, policy);
1747 break;
1748 case IOPOL_CMD_GET:
1749 if (thread != THREAD_NULL)
1750 policy = get_thread_atime_policy(get_bsdthread_info(thread));
1751 else
1752 policy = get_task_atime_policy(p);
1753 iop_param->iop_policy = policy;
1754 break;
1755 default:
1756 error = EINVAL; /* unknown command */
1757 break;
1758 }
1759
1760 out:
1761 return (error);
1762 }
1763
1764 /* BSD call back function for task_policy networking changes */
1765 void
1766 proc_apply_task_networkbg(void * bsd_info, thread_t thread)
1767 {
1768 assert(bsd_info != PROC_NULL);
1769
1770 pid_t pid = proc_pid((proc_t)bsd_info);
1771
1772 proc_t p = proc_find(pid);
1773
1774 if (p != PROC_NULL) {
1775 assert(p == (proc_t)bsd_info);
1776
1777 do_background_socket(p, thread);
1778 proc_rele(p);
1779 }
1780 }
1781
1782 void
1783 gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor)
1784 {
1785 struct rusage_info_child *ri_child;
1786
1787 assert(p->p_stats != NULL);
1788 memset(ru, 0, sizeof(*ru));
1789 switch(flavor) {
1790 case RUSAGE_INFO_V4:
1791 ru->ri_logical_writes = get_task_logical_writes(p->task);
1792 ru->ri_lifetime_max_phys_footprint = get_task_phys_footprint_lifetime_max(p->task);
1793 #if CONFIG_LEDGER_INTERVAL_MAX
1794 ru->ri_interval_max_phys_footprint = get_task_phys_footprint_interval_max(p->task, FALSE);
1795 #endif
1796 fill_task_monotonic_rusage(p->task, ru);
1797 /* fall through */
1798
1799 case RUSAGE_INFO_V3:
1800 fill_task_qos_rusage(p->task, ru);
1801 fill_task_billed_usage(p->task, ru);
1802 /* fall through */
1803
1804 case RUSAGE_INFO_V2:
1805 fill_task_io_rusage(p->task, ru);
1806 /* fall through */
1807
1808 case RUSAGE_INFO_V1:
1809 /*
1810 * p->p_stats->ri_child statistics are protected under proc lock.
1811 */
1812 proc_lock(p);
1813
1814 ri_child = &(p->p_stats->ri_child);
1815 ru->ri_child_user_time = ri_child->ri_child_user_time;
1816 ru->ri_child_system_time = ri_child->ri_child_system_time;
1817 ru->ri_child_pkg_idle_wkups = ri_child->ri_child_pkg_idle_wkups;
1818 ru->ri_child_interrupt_wkups = ri_child->ri_child_interrupt_wkups;
1819 ru->ri_child_pageins = ri_child->ri_child_pageins;
1820 ru->ri_child_elapsed_abstime = ri_child->ri_child_elapsed_abstime;
1821
1822 proc_unlock(p);
1823 /* fall through */
1824
1825 case RUSAGE_INFO_V0:
1826 proc_getexecutableuuid(p, (unsigned char *)&ru->ri_uuid, sizeof (ru->ri_uuid));
1827 fill_task_rusage(p->task, ru);
1828 ru->ri_proc_start_abstime = p->p_stats->ps_start;
1829 }
1830 }
1831
1832 int
1833 proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie)
1834 {
1835 rusage_info_current ri_current = {};
1836
1837 int error = 0;
1838 size_t size = 0;
1839
1840 switch (flavor) {
1841 case RUSAGE_INFO_V0:
1842 size = sizeof(struct rusage_info_v0);
1843 break;
1844
1845 case RUSAGE_INFO_V1:
1846 size = sizeof(struct rusage_info_v1);
1847 break;
1848
1849 case RUSAGE_INFO_V2:
1850 size = sizeof(struct rusage_info_v2);
1851 break;
1852
1853 case RUSAGE_INFO_V3:
1854 size = sizeof(struct rusage_info_v3);
1855 break;
1856
1857 case RUSAGE_INFO_V4:
1858 size = sizeof(struct rusage_info_v4);
1859 break;
1860
1861 default:
1862 return EINVAL;
1863 }
1864
1865 if(size == 0) {
1866 return EINVAL;
1867 }
1868
1869 /*
1870 * If task is still alive, collect info from the live task itself.
1871 * Otherwise, look to the cached info in the zombie proc.
1872 */
1873 if (p->p_ru == NULL) {
1874 gather_rusage_info(p, &ri_current, flavor);
1875 ri_current.ri_proc_exit_abstime = 0;
1876 error = copyout(&ri_current, buffer, size);
1877 } else {
1878 ri_current = p->p_ru->ri;
1879 error = copyout(&p->p_ru->ri, buffer, size);
1880 }
1881
1882 return (error);
1883 }
1884
1885 static int
1886 mach_to_bsd_rv(int mach_rv)
1887 {
1888 int bsd_rv = 0;
1889
1890 switch (mach_rv) {
1891 case KERN_SUCCESS:
1892 bsd_rv = 0;
1893 break;
1894 case KERN_INVALID_ARGUMENT:
1895 bsd_rv = EINVAL;
1896 break;
1897 default:
1898 panic("unknown error %#x", mach_rv);
1899 }
1900
1901 return bsd_rv;
1902 }
1903
1904 /*
1905 * Resource limit controls
1906 *
1907 * uap->flavor available flavors:
1908 *
1909 * RLIMIT_WAKEUPS_MONITOR
1910 * RLIMIT_CPU_USAGE_MONITOR
1911 * RLIMIT_THREAD_CPULIMITS
1912 * RLIMIT_FOOTPRINT_INTERVAL
1913 */
1914 int
1915 proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *uap, __unused int32_t *retval)
1916 {
1917 proc_t targetp;
1918 int error = 0;
1919 struct proc_rlimit_control_wakeupmon wakeupmon_args;
1920 uint32_t cpumon_flags;
1921 uint32_t cpulimits_flags;
1922 kauth_cred_t my_cred, target_cred;
1923 #if CONFIG_LEDGER_INTERVAL_MAX
1924 uint32_t footprint_interval_flags;
1925 uint64_t interval_max_footprint;
1926 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
1927
1928 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
1929 if (uap->pid == -1) {
1930 targetp = proc_self();
1931 } else {
1932 targetp = proc_find(uap->pid);
1933 }
1934
1935 /* proc_self() can return NULL for an exiting process */
1936 if (targetp == PROC_NULL) {
1937 return (ESRCH);
1938 }
1939
1940 my_cred = kauth_cred_get();
1941 target_cred = kauth_cred_proc_ref(targetp);
1942
1943 if (!kauth_cred_issuser(my_cred) && kauth_cred_getruid(my_cred) &&
1944 kauth_cred_getuid(my_cred) != kauth_cred_getuid(target_cred) &&
1945 kauth_cred_getruid(my_cred) != kauth_cred_getuid(target_cred)) {
1946 proc_rele(targetp);
1947 kauth_cred_unref(&target_cred);
1948 return (EACCES);
1949 }
1950
1951 switch (uap->flavor) {
1952 case RLIMIT_WAKEUPS_MONITOR:
1953 if ((error = copyin(uap->arg, &wakeupmon_args, sizeof (wakeupmon_args))) != 0) {
1954 break;
1955 }
1956 if ((error = mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp->task, &wakeupmon_args.wm_flags,
1957 &wakeupmon_args.wm_rate))) != 0) {
1958 break;
1959 }
1960 error = copyout(&wakeupmon_args, uap->arg, sizeof (wakeupmon_args));
1961 break;
1962 case RLIMIT_CPU_USAGE_MONITOR:
1963 cpumon_flags = uap->arg; // XXX temporarily stashing flags in argp (12592127)
1964 error = mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp->task, &cpumon_flags));
1965 break;
1966 case RLIMIT_THREAD_CPULIMITS:
1967 cpulimits_flags = (uint32_t)uap->arg; // only need a limited set of bits, pass in void * argument
1968
1969 if (uap->pid != -1) {
1970 error = EINVAL;
1971 break;
1972 }
1973
1974 uint8_t percent = 0;
1975 uint32_t ms_refill = 0;
1976 uint64_t ns_refill;
1977
1978 percent = (uint8_t)(cpulimits_flags & 0xffU); /* low 8 bits for percent */
1979 ms_refill = (cpulimits_flags >> 8) & 0xffffff; /* next 24 bits represent ms refill value */
1980 if (percent >= 100) {
1981 error = EINVAL;
1982 break;
1983 }
1984
1985 ns_refill = ((uint64_t)ms_refill) * NSEC_PER_MSEC;
1986
1987 error = mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, percent, ns_refill));
1988 break;
1989
1990 #if CONFIG_LEDGER_INTERVAL_MAX
1991 case RLIMIT_FOOTPRINT_INTERVAL:
1992 footprint_interval_flags = uap->arg; // XXX temporarily stashing flags in argp (12592127)
1993 /*
1994 * There is currently only one option for this flavor.
1995 */
1996 if ((footprint_interval_flags & FOOTPRINT_INTERVAL_RESET) == 0) {
1997 error = EINVAL;
1998 break;
1999 }
2000 interval_max_footprint = get_task_phys_footprint_interval_max(targetp->task, TRUE);
2001 break;
2002 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
2003 default:
2004 error = EINVAL;
2005 break;
2006 }
2007
2008 proc_rele(targetp);
2009 kauth_cred_unref(&target_cred);
2010
2011 /*
2012 * Return value from this function becomes errno to userland caller.
2013 */
2014 return (error);
2015 }
2016
2017 /*
2018 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
2019 */
2020 int thread_selfusage(struct proc *p __unused, struct thread_selfusage_args *uap __unused, uint64_t *retval)
2021 {
2022 uint64_t runtime;
2023
2024 runtime = thread_get_runtime_self();
2025 *retval = runtime;
2026
2027 return (0);
2028 }
2029
2030 #if !MONOTONIC
2031 int thread_selfcounts(__unused struct proc *p, __unused struct thread_selfcounts_args *uap, __unused int *ret_out)
2032 {
2033 return ENOTSUP;
2034 }
2035 #endif /* !MONOTONIC */