]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_resource.c
780159263d051645e27aea413e9f7592123608a2
[apple/xnu.git] / bsd / kern / kern_resource.c
1 /*
2 * Copyright (c) 2000-2017 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 /*
1425 * iopolicysys
1426 *
1427 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1428 *
1429 * Parameters: cmd Policy command
1430 * arg Pointer to policy arguments
1431 *
1432 * Returns: 0 Success
1433 * EINVAL Invalid command or invalid policy arguments
1434 *
1435 */
1436
1437 static int
1438 iopolicysys_disk(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
1439 static int
1440 iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
1441
1442 int
1443 iopolicysys(struct proc *p, struct iopolicysys_args *uap, int32_t *retval)
1444 {
1445 int error = 0;
1446 struct _iopol_param_t iop_param;
1447
1448 if ((error = copyin(uap->arg, &iop_param, sizeof(iop_param))) != 0)
1449 goto out;
1450
1451 switch (iop_param.iop_iotype) {
1452 case IOPOL_TYPE_DISK:
1453 error = iopolicysys_disk(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
1454 if (error == EIDRM) {
1455 *retval = -2;
1456 error = 0;
1457 }
1458 if (error)
1459 goto out;
1460 break;
1461 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY:
1462 error = iopolicysys_vfs(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
1463 if (error)
1464 goto out;
1465 break;
1466 default:
1467 error = EINVAL;
1468 goto out;
1469 }
1470
1471 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1472 if (uap->cmd == IOPOL_CMD_GET) {
1473 error = copyout((caddr_t)&iop_param, uap->arg, sizeof(iop_param));
1474 if (error)
1475 goto out;
1476 }
1477
1478 out:
1479 return (error);
1480 }
1481
1482 static int
1483 iopolicysys_disk(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
1484 {
1485 int error = 0;
1486 thread_t thread;
1487 int policy_flavor;
1488
1489 /* Validate scope */
1490 switch (scope) {
1491 case IOPOL_SCOPE_PROCESS:
1492 thread = THREAD_NULL;
1493 policy_flavor = TASK_POLICY_IOPOL;
1494 break;
1495
1496 case IOPOL_SCOPE_THREAD:
1497 thread = current_thread();
1498 policy_flavor = TASK_POLICY_IOPOL;
1499
1500 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1501 if (cmd == IOPOL_CMD_SET && thread_has_qos_policy(thread)) {
1502 switch (policy) {
1503 case IOPOL_DEFAULT:
1504 case IOPOL_PASSIVE:
1505 break;
1506 case IOPOL_UTILITY:
1507 case IOPOL_THROTTLE:
1508 case IOPOL_IMPORTANT:
1509 case IOPOL_STANDARD:
1510 if (!thread_is_static_param(thread)) {
1511 thread_remove_qos_policy(thread);
1512 /*
1513 * This is not an error case, this is to return a marker to user-space that
1514 * we stripped the thread of its QoS class.
1515 */
1516 error = EIDRM;
1517 break;
1518 }
1519 /* otherwise, fall through to the error case. */
1520 default:
1521 error = EINVAL;
1522 goto out;
1523 }
1524 }
1525 break;
1526
1527 case IOPOL_SCOPE_DARWIN_BG:
1528 #if CONFIG_EMBEDDED
1529 /* Embedded doesn't want this as BG is always IOPOL_THROTTLE */
1530 error = ENOTSUP;
1531 goto out;
1532 #else /* CONFIG_EMBEDDED */
1533 thread = THREAD_NULL;
1534 policy_flavor = TASK_POLICY_DARWIN_BG_IOPOL;
1535 break;
1536 #endif /* CONFIG_EMBEDDED */
1537
1538 default:
1539 error = EINVAL;
1540 goto out;
1541 }
1542
1543 /* Validate policy */
1544 if (cmd == IOPOL_CMD_SET) {
1545 switch (policy) {
1546 case IOPOL_DEFAULT:
1547 if (scope == IOPOL_SCOPE_DARWIN_BG) {
1548 /* the current default BG throttle level is UTILITY */
1549 policy = IOPOL_UTILITY;
1550 } else {
1551 policy = IOPOL_IMPORTANT;
1552 }
1553 break;
1554 case IOPOL_UTILITY:
1555 /* fall-through */
1556 case IOPOL_THROTTLE:
1557 /* These levels are OK */
1558 break;
1559 case IOPOL_IMPORTANT:
1560 /* fall-through */
1561 case IOPOL_STANDARD:
1562 /* fall-through */
1563 case IOPOL_PASSIVE:
1564 if (scope == IOPOL_SCOPE_DARWIN_BG) {
1565 /* These levels are invalid for BG */
1566 error = EINVAL;
1567 goto out;
1568 } else {
1569 /* OK for other scopes */
1570 }
1571 break;
1572 default:
1573 error = EINVAL;
1574 goto out;
1575 }
1576 }
1577
1578 /* Perform command */
1579 switch(cmd) {
1580 case IOPOL_CMD_SET:
1581 if (thread != THREAD_NULL)
1582 proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor, policy);
1583 else
1584 proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor, policy);
1585 break;
1586 case IOPOL_CMD_GET:
1587 if (thread != THREAD_NULL)
1588 policy = proc_get_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor);
1589 else
1590 policy = proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor);
1591 iop_param->iop_policy = policy;
1592 break;
1593 default:
1594 error = EINVAL; /* unknown command */
1595 break;
1596 }
1597
1598 out:
1599 return (error);
1600 }
1601
1602 static int
1603 iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
1604 {
1605 int error = 0;
1606
1607 /* Validate scope */
1608 switch (scope) {
1609 case IOPOL_SCOPE_PROCESS:
1610 /* Only process OK */
1611 break;
1612 default:
1613 error = EINVAL;
1614 goto out;
1615 }
1616
1617 /* Validate policy */
1618 if (cmd == IOPOL_CMD_SET) {
1619 switch (policy) {
1620 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
1621 /* fall-through */
1622 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
1623 /* These policies are OK */
1624 break;
1625 default:
1626 error = EINVAL;
1627 goto out;
1628 }
1629 }
1630
1631 /* Perform command */
1632 switch(cmd) {
1633 case IOPOL_CMD_SET:
1634 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1635 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1636 boolean_t entitled = FALSE;
1637 entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1638 if (!entitled) {
1639 error = EPERM;
1640 goto out;
1641 }
1642 }
1643
1644 switch (policy) {
1645 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
1646 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY), &p->p_vfs_iopolicy);
1647 break;
1648 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
1649 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY, &p->p_vfs_iopolicy);
1650 break;
1651 default:
1652 error = EINVAL;
1653 goto out;
1654 }
1655
1656 break;
1657 case IOPOL_CMD_GET:
1658 iop_param->iop_policy = (p->p_vfs_iopolicy & P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY)
1659 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1660 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT;
1661 break;
1662 default:
1663 error = EINVAL; /* unknown command */
1664 break;
1665 }
1666
1667 out:
1668 return (error);
1669 }
1670
1671 /* BSD call back function for task_policy networking changes */
1672 void
1673 proc_apply_task_networkbg(void * bsd_info, thread_t thread)
1674 {
1675 assert(bsd_info != PROC_NULL);
1676
1677 pid_t pid = proc_pid((proc_t)bsd_info);
1678
1679 proc_t p = proc_find(pid);
1680
1681 if (p != PROC_NULL) {
1682 assert(p == (proc_t)bsd_info);
1683
1684 do_background_socket(p, thread);
1685 proc_rele(p);
1686 }
1687 }
1688
1689 void
1690 gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor)
1691 {
1692 struct rusage_info_child *ri_child;
1693
1694 assert(p->p_stats != NULL);
1695 memset(ru, 0, sizeof(*ru));
1696 switch(flavor) {
1697 case RUSAGE_INFO_V4:
1698 ru->ri_logical_writes = get_task_logical_writes(p->task);
1699 ru->ri_lifetime_max_phys_footprint = get_task_phys_footprint_lifetime_max(p->task);
1700 fill_task_monotonic_rusage(p->task, ru);
1701 /* fall through */
1702
1703 case RUSAGE_INFO_V3:
1704 fill_task_qos_rusage(p->task, ru);
1705 fill_task_billed_usage(p->task, ru);
1706 /* fall through */
1707
1708 case RUSAGE_INFO_V2:
1709 fill_task_io_rusage(p->task, ru);
1710 /* fall through */
1711
1712 case RUSAGE_INFO_V1:
1713 /*
1714 * p->p_stats->ri_child statistics are protected under proc lock.
1715 */
1716 proc_lock(p);
1717
1718 ri_child = &(p->p_stats->ri_child);
1719 ru->ri_child_user_time = ri_child->ri_child_user_time;
1720 ru->ri_child_system_time = ri_child->ri_child_system_time;
1721 ru->ri_child_pkg_idle_wkups = ri_child->ri_child_pkg_idle_wkups;
1722 ru->ri_child_interrupt_wkups = ri_child->ri_child_interrupt_wkups;
1723 ru->ri_child_pageins = ri_child->ri_child_pageins;
1724 ru->ri_child_elapsed_abstime = ri_child->ri_child_elapsed_abstime;
1725
1726 proc_unlock(p);
1727 /* fall through */
1728
1729 case RUSAGE_INFO_V0:
1730 proc_getexecutableuuid(p, (unsigned char *)&ru->ri_uuid, sizeof (ru->ri_uuid));
1731 fill_task_rusage(p->task, ru);
1732 ru->ri_proc_start_abstime = p->p_stats->ps_start;
1733 }
1734 }
1735
1736 int
1737 proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie)
1738 {
1739 rusage_info_current ri_current;
1740
1741 int error = 0;
1742 size_t size = 0;
1743
1744 switch (flavor) {
1745 case RUSAGE_INFO_V0:
1746 size = sizeof(struct rusage_info_v0);
1747 break;
1748
1749 case RUSAGE_INFO_V1:
1750 size = sizeof(struct rusage_info_v1);
1751 break;
1752
1753 case RUSAGE_INFO_V2:
1754 size = sizeof(struct rusage_info_v2);
1755 break;
1756
1757 case RUSAGE_INFO_V3:
1758 size = sizeof(struct rusage_info_v3);
1759 break;
1760
1761 case RUSAGE_INFO_V4:
1762 size = sizeof(struct rusage_info_v4);
1763 break;
1764
1765 default:
1766 return EINVAL;
1767 }
1768
1769 if(size == 0) {
1770 return EINVAL;
1771 }
1772
1773 /*
1774 * If task is still alive, collect info from the live task itself.
1775 * Otherwise, look to the cached info in the zombie proc.
1776 */
1777 if (p->p_ru == NULL) {
1778 gather_rusage_info(p, &ri_current, flavor);
1779 ri_current.ri_proc_exit_abstime = 0;
1780 error = copyout(&ri_current, buffer, size);
1781 } else {
1782 ri_current = p->p_ru->ri;
1783 error = copyout(&p->p_ru->ri, buffer, size);
1784 }
1785
1786 return (error);
1787 }
1788
1789 static int
1790 mach_to_bsd_rv(int mach_rv)
1791 {
1792 int bsd_rv = 0;
1793
1794 switch (mach_rv) {
1795 case KERN_SUCCESS:
1796 bsd_rv = 0;
1797 break;
1798 case KERN_INVALID_ARGUMENT:
1799 bsd_rv = EINVAL;
1800 break;
1801 default:
1802 panic("unknown error %#x", mach_rv);
1803 }
1804
1805 return bsd_rv;
1806 }
1807
1808 /*
1809 * Resource limit controls
1810 *
1811 * uap->flavor available flavors:
1812 *
1813 * RLIMIT_WAKEUPS_MONITOR
1814 */
1815 int
1816 proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *uap, __unused int32_t *retval)
1817 {
1818 proc_t targetp;
1819 int error = 0;
1820 struct proc_rlimit_control_wakeupmon wakeupmon_args;
1821 uint32_t cpumon_flags;
1822 uint32_t cpulimits_flags;
1823 kauth_cred_t my_cred, target_cred;
1824
1825 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
1826 if (uap->pid == -1) {
1827 targetp = proc_self();
1828 } else {
1829 targetp = proc_find(uap->pid);
1830 }
1831
1832 /* proc_self() can return NULL for an exiting process */
1833 if (targetp == PROC_NULL) {
1834 return (ESRCH);
1835 }
1836
1837 my_cred = kauth_cred_get();
1838 target_cred = kauth_cred_proc_ref(targetp);
1839
1840 if (!kauth_cred_issuser(my_cred) && kauth_cred_getruid(my_cred) &&
1841 kauth_cred_getuid(my_cred) != kauth_cred_getuid(target_cred) &&
1842 kauth_cred_getruid(my_cred) != kauth_cred_getuid(target_cred)) {
1843 proc_rele(targetp);
1844 kauth_cred_unref(&target_cred);
1845 return (EACCES);
1846 }
1847
1848 switch (uap->flavor) {
1849 case RLIMIT_WAKEUPS_MONITOR:
1850 if ((error = copyin(uap->arg, &wakeupmon_args, sizeof (wakeupmon_args))) != 0) {
1851 break;
1852 }
1853 if ((error = mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp->task, &wakeupmon_args.wm_flags,
1854 &wakeupmon_args.wm_rate))) != 0) {
1855 break;
1856 }
1857 error = copyout(&wakeupmon_args, uap->arg, sizeof (wakeupmon_args));
1858 break;
1859 case RLIMIT_CPU_USAGE_MONITOR:
1860 cpumon_flags = uap->arg; // XXX temporarily stashing flags in argp (12592127)
1861 error = mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp->task, &cpumon_flags));
1862 break;
1863 case RLIMIT_THREAD_CPULIMITS:
1864 cpulimits_flags = (uint32_t)uap->arg; // only need a limited set of bits, pass in void * argument
1865
1866 if (uap->pid != -1) {
1867 error = EINVAL;
1868 break;
1869 }
1870
1871 uint8_t percent = 0;
1872 uint32_t ms_refill = 0;
1873 uint64_t ns_refill;
1874
1875 percent = (uint8_t)(cpulimits_flags & 0xffU); /* low 8 bits for percent */
1876 ms_refill = (cpulimits_flags >> 8) & 0xffffff; /* next 24 bits represent ms refill value */
1877 if (percent >= 100) {
1878 error = EINVAL;
1879 break;
1880 }
1881
1882 ns_refill = ((uint64_t)ms_refill) * NSEC_PER_MSEC;
1883
1884 error = mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, percent, ns_refill));
1885 break;
1886 default:
1887 error = EINVAL;
1888 break;
1889 }
1890
1891 proc_rele(targetp);
1892 kauth_cred_unref(&target_cred);
1893
1894 /*
1895 * Return value from this function becomes errno to userland caller.
1896 */
1897 return (error);
1898 }
1899
1900 /*
1901 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
1902 */
1903 int thread_selfusage(struct proc *p __unused, struct thread_selfusage_args *uap __unused, uint64_t *retval)
1904 {
1905 uint64_t runtime;
1906
1907 runtime = thread_get_runtime_self();
1908 *retval = runtime;
1909
1910 return (0);
1911 }
1912
1913 #if !MONOTONIC
1914 int thread_selfcounts(__unused struct proc *p, __unused struct thread_selfcounts_args *uap, __unused int *ret_out)
1915 {
1916 return ENOTSUP;
1917 }
1918 #endif /* !MONOTONIC */