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