2 * Copyright (c) 2005-2016 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * process policy syscall implementation
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/proc_internal.h>
40 #include <sys/kauth.h>
41 #include <sys/unistd.h>
43 #include <sys/ioctl.h>
47 #include <mach/machine.h>
48 #include <mach/mach_types.h>
49 #include <mach/vm_param.h>
50 #include <kern/task.h>
51 #include <kern/kalloc.h>
52 #include <kern/assert.h>
53 #include <kern/policy_internal.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_map.h>
57 #include <mach/host_info.h>
58 #include <mach/task_info.h>
59 #include <mach/thread_info.h>
60 #include <mach/vm_region.h>
62 #include <sys/process_policy.h>
63 #include <sys/proc_info.h>
64 #include <sys/bsdtask_info.h>
65 #include <sys/kdebug.h>
66 #include <sys/sysproto.h>
67 #include <sys/msgbuf.h>
69 #include <machine/machine_routines.h>
71 #include <kern/ipc_misc.h>
72 #include <vm/vm_protos.h>
75 #include <sys/kern_memorystatus.h>
76 #endif /* CONFIG_EMBEDDED */
79 #include <security/mac.h>
80 #include <security/mac_framework.h>
81 #endif /* CONFIG_MACF */
83 static int handle_lowresource(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
84 static int handle_cpuuse(int action
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
85 static int handle_apptype(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
86 static int handle_boost(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
88 extern kern_return_t
task_suspend(task_t
);
89 extern kern_return_t
task_resume(task_t
);
92 static int handle_applifecycle(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
93 #endif /* CONFIG_EMBEDDED */
95 /***************************** process_policy ********************/
98 * int process_policy(int scope, int action, int policy, int policy_subtype,
99 * proc_policy_attribute_t * attrp, pid_t target_pid,
100 * uint64_t target_threadid)
101 *{ int process_policy(int scope, int action, int policy, int policy_subtype,
102 * user_addr_t attrp, pid_t target_pid, uint64_t target_threadid); }
105 /* system call implementation */
107 process_policy(__unused
struct proc
*p
, struct process_policy_args
* uap
, __unused
int32_t *retval
)
110 int scope
= uap
->scope
;
111 int policy
= uap
->policy
;
112 int action
= uap
->action
;
113 int policy_subtype
= uap
->policy_subtype
;
114 user_addr_t attrp
= uap
->attrp
;
115 pid_t target_pid
= uap
->target_pid
;
116 uint64_t target_threadid
= uap
->target_threadid
;
117 proc_t target_proc
= PROC_NULL
;
118 #if CONFIG_MACF || !CONFIG_EMBEDDED
119 proc_t curp
= current_proc();
121 kauth_cred_t my_cred
;
123 kauth_cred_t target_cred
;
126 if ((scope
!= PROC_POLICY_SCOPE_PROCESS
) && (scope
!= PROC_POLICY_SCOPE_THREAD
)) {
130 if (target_pid
== 0 || target_pid
== proc_selfpid()) {
131 target_proc
= proc_self();
133 target_proc
= proc_find(target_pid
);
136 if (target_proc
== PROC_NULL
) {
140 my_cred
= kauth_cred_get();
143 target_cred
= kauth_cred_proc_ref(target_proc
);
145 if (!kauth_cred_issuser(my_cred
) && kauth_cred_getruid(my_cred
) &&
146 kauth_cred_getuid(my_cred
) != kauth_cred_getuid(target_cred
) &&
147 kauth_cred_getruid(my_cred
) != kauth_cred_getuid(target_cred
))
150 * Resoure starvation control can be used by unpriv resource owner but priv at the time of ownership claim. This is
151 * checked in low resource handle routine. So bypass the checks here.
153 if ((policy
!= PROC_POLICY_RESOURCE_STARVATION
) &&
154 (policy
!= PROC_POLICY_APPTYPE
) &&
155 (!kauth_cred_issuser(my_cred
) && curp
!= target_proc
))
164 case PROC_POLICY_BOOST
:
165 case PROC_POLICY_RESOURCE_USAGE
:
167 case PROC_POLICY_APPTYPE
:
168 case PROC_POLICY_APP_LIFECYCLE
:
170 /* These policies do their own appropriate mac checks */
173 error
= mac_proc_check_sched(curp
, target_proc
);
179 #endif /* CONFIG_MACF */
182 case PROC_POLICY_BACKGROUND
:
185 case PROC_POLICY_HARDWARE_ACCESS
:
188 case PROC_POLICY_RESOURCE_STARVATION
:
189 error
= handle_lowresource(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
191 case PROC_POLICY_RESOURCE_USAGE
:
192 switch (policy_subtype
) {
193 case PROC_POLICY_RUSAGE_NONE
:
194 case PROC_POLICY_RUSAGE_WIREDMEM
:
195 case PROC_POLICY_RUSAGE_VIRTMEM
:
196 case PROC_POLICY_RUSAGE_DISK
:
197 case PROC_POLICY_RUSAGE_NETWORK
:
198 case PROC_POLICY_RUSAGE_POWER
:
204 case PROC_POLICY_RUSAGE_CPU
:
208 error
= handle_cpuuse(action
, attrp
, target_proc
, target_threadid
);
211 case PROC_POLICY_APP_LIFECYCLE
:
212 error
= handle_applifecycle(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
214 #endif /* CONFIG_EMBEDDED */
215 case PROC_POLICY_APPTYPE
:
216 error
= handle_apptype(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
218 case PROC_POLICY_BOOST
:
219 error
= handle_boost(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
227 proc_rele(target_proc
);
229 kauth_cred_unref(&target_cred
);
235 handle_lowresource(__unused
int scope
, int action
, __unused
int policy
, int policy_subtype
, __unused user_addr_t attrp
, proc_t proc
, __unused
uint64_t target_threadid
)
239 switch (policy_subtype
) {
240 case PROC_POLICY_RS_NONE
:
241 case PROC_POLICY_RS_VIRTUALMEM
:
247 if (action
== PROC_POLICY_ACTION_RESTORE
) {
248 error
= proc_resetpcontrol(proc_pid(proc
));
258 handle_cpuuse(int action
, user_addr_t attrp
, proc_t proc
, __unused
uint64_t target_threadid
)
260 proc_policy_cpuusage_attr_t cpuattr
= { };
261 #if CONFIG_MACF || !CONFIG_EMBEDDED
262 proc_t curp
= current_proc();
264 Boolean privileged
= FALSE
;
265 Boolean canEnable
= FALSE
;
266 uint64_t interval
= -1ULL;
271 /* On macOS, tasks can only set and clear their own CPU limits. */
272 if ((action
== PROC_POLICY_ACTION_APPLY
|| action
== PROC_POLICY_ACTION_RESTORE
)
276 /* No privilege required on macOS. */
281 /* Is caller privileged to set less-restrictive scheduling parameters? */
283 privileged
= (priv_check_cred(kauth_cred_get(), PRIV_PROC_CPUMON_OVERRIDE
, 0) == 0);
285 canEnable
= (privileged
&& action
== PROC_POLICY_ACTION_ENABLE
);
287 if (!canEnable
&& curp
!= proc
) {
289 * Can the current process change scheduling parameters for
290 * the target process?
292 error
= mac_proc_check_sched(curp
, proc
);
300 case PROC_POLICY_ACTION_GET
:
301 error
= proc_get_task_ruse_cpu(proc
->task
, &cpuattr
.ppattr_cpu_attr
,
303 &cpuattr
.ppattr_cpu_attr_interval
,
304 &cpuattr
.ppattr_cpu_attr_deadline
);
306 cpuattr
.ppattr_cpu_percentage
= percentage
;
307 cpuattr
.ppattr_cpu_attr_interval
/= NSEC_PER_SEC
;
308 error
= copyout((proc_policy_cpuusage_attr_t
*)&cpuattr
, (user_addr_t
)attrp
, sizeof(proc_policy_cpuusage_attr_t
));
312 case PROC_POLICY_ACTION_APPLY
:
313 case PROC_POLICY_ACTION_SET
:
314 error
= copyin((user_addr_t
)attrp
, (proc_policy_cpuusage_attr_t
*)&cpuattr
, sizeof(proc_policy_cpuusage_attr_t
));
320 * The process_policy API uses seconds as the units for the interval,
321 * but the mach task policy SPI uses nanoseconds. Do the conversion,
322 * but preserve -1 as it has special meaning.
324 if (cpuattr
.ppattr_cpu_attr_interval
!= -1ULL) {
325 interval
= cpuattr
.ppattr_cpu_attr_interval
* NSEC_PER_SEC
;
330 error
= proc_set_task_ruse_cpu(proc
->task
, cpuattr
.ppattr_cpu_attr
,
331 cpuattr
.ppattr_cpu_percentage
,
333 cpuattr
.ppattr_cpu_attr_deadline
,
337 /* restore process to prior state */
338 case PROC_POLICY_ACTION_RESTORE
:
339 error
= proc_clear_task_ruse_cpu(proc
->task
, privileged
);
342 /* re-enable suspended monitor */
343 case PROC_POLICY_ACTION_ENABLE
:
344 error
= task_resume_cpumon(proc
->task
);
347 case PROC_POLICY_ACTION_REMOVE
:
359 handle_applifecycle(__unused
int scope
,
365 uint64_t target_threadid
)
370 switch (policy_subtype
) {
371 case PROC_POLICY_APPLIFE_NONE
:
375 case PROC_POLICY_APPLIFE_STATE
:
376 /* appstate is no longer supported */
380 case PROC_POLICY_APPLIFE_DEVSTATUS
:
382 /* ToDo - this should be a generic check, since we could potentially hang other behaviours here. */
383 error
= mac_proc_check_suspend_resume(current_proc(), MAC_PROC_CHECK_HIBERNATE
);
389 #if CONFIG_MEMORYSTATUS
390 if (action
== PROC_POLICY_ACTION_APPLY
) {
391 /* Used as a freeze hint */
392 memorystatus_on_inactivity(proc
);
394 /* in future use devicestatus for pid_socketshutdown() */
403 case PROC_POLICY_APPLIFE_PIDBIND
:
405 error
= mac_proc_check_suspend_resume(current_proc(), MAC_PROC_CHECK_PIDBIND
);
411 error
= copyin((user_addr_t
)attrp
, (int *)&state
, sizeof(int));
415 if (action
== PROC_POLICY_ACTION_APPLY
) {
416 /* bind the thread in target_thread in current process to target_proc */
417 error
= proc_lf_pidbind(current_task(), target_threadid
, proc
->task
, state
);
430 #endif /* CONFIG_EMBEDDED */
433 handle_apptype( int scope
,
437 __unused user_addr_t attrp
,
439 __unused
uint64_t target_threadid
)
443 if (scope
!= PROC_POLICY_SCOPE_PROCESS
) {
447 /* Temporary compatibility with old importance donation interface until libproc is moved to new boost calls */
448 switch (policy_subtype
) {
449 case PROC_POLICY_IOS_DONATEIMP
:
450 if (action
!= PROC_POLICY_ACTION_ENABLE
) {
453 if (target_proc
!= current_proc()) {
457 /* PROCESS ENABLE APPTYPE DONATEIMP */
458 task_importance_mark_donor(target_proc
->task
, TRUE
);
462 case PROC_POLICY_IOS_HOLDIMP
:
463 if (action
!= PROC_POLICY_ACTION_ENABLE
) {
466 if (target_proc
!= current_proc()) {
470 /* PROCESS ENABLE APPTYPE HOLDIMP */
471 error
= task_importance_hold_legacy_external_assertion(current_task(), 1);
475 case PROC_POLICY_IOS_DROPIMP
:
476 if (action
!= PROC_POLICY_ACTION_ENABLE
) {
479 if (target_proc
!= current_proc()) {
483 /* PROCESS ENABLE APPTYPE DROPIMP */
484 error
= task_importance_drop_legacy_external_assertion(current_task(), 1);
489 /* continue to TAL handling */
493 if (policy_subtype
!= PROC_POLICY_OSX_APPTYPE_TAL
) {
497 /* need to be super user to do this */
498 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
502 if (proc_task_is_tal(target_proc
->task
) == FALSE
) {
507 case PROC_POLICY_ACTION_ENABLE
:
508 /* PROCESS ENABLE APPTYPE TAL */
509 proc_set_task_policy(target_proc
->task
,
510 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_TAL
,
513 case PROC_POLICY_ACTION_DISABLE
:
514 /* PROCESS DISABLE APPTYPE TAL */
515 proc_set_task_policy(target_proc
->task
,
516 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_TAL
,
517 TASK_POLICY_DISABLE
);
527 handle_boost(int scope
,
531 __unused user_addr_t attrp
,
533 __unused
uint64_t target_threadid
)
537 assert(policy
== PROC_POLICY_BOOST
);
539 if (scope
!= PROC_POLICY_SCOPE_PROCESS
) {
543 if (target_proc
!= current_proc()) {
547 switch (policy_subtype
) {
548 case PROC_POLICY_IMP_IMPORTANT
:
549 if (task_is_importance_receiver_type(target_proc
->task
) == FALSE
) {
554 case PROC_POLICY_ACTION_HOLD
:
555 /* PROCESS HOLD BOOST IMPORTANT */
556 error
= task_importance_hold_legacy_external_assertion(current_task(), 1);
558 case PROC_POLICY_ACTION_DROP
:
559 /* PROCESS DROP BOOST IMPORTANT */
560 error
= task_importance_drop_legacy_external_assertion(current_task(), 1);
568 case PROC_POLICY_IMP_DONATION
:
570 error
= mac_proc_check_sched(current_proc(), target_proc
);
576 case PROC_POLICY_ACTION_SET
:
577 /* PROCESS SET BOOST DONATION */
578 task_importance_mark_donor(target_proc
->task
, TRUE
);
596 * KPI to determine if a pid is currently backgrounded.
597 * Returns ESRCH if pid cannot be found or has started exiting.
598 * Returns EINVAL if state is NULL.
599 * Sets *state to 1 if pid is backgrounded, and 0 otherwise.
602 proc_pidbackgrounded(pid_t pid
, uint32_t* state
)
604 proc_t target_proc
= PROC_NULL
;
610 target_proc
= proc_find(pid
);
612 if (target_proc
== PROC_NULL
) {
616 if (proc_get_effective_task_policy(target_proc
->task
, TASK_POLICY_DARWIN_BG
)) {
622 proc_rele(target_proc
);
627 * Get the darwin background state of the originator. If the current
628 * process app type is App, then it is the originator, else if it is
629 * a Daemon, then creator of the Resource Accounting attribute of
630 * the current thread voucher is the originator of the work.
633 proc_get_originatorbgstate(uint32_t *is_backgrounded
)
636 proc_t p
= current_proc();
641 thread_t thread
= current_thread();
643 bgstate
= proc_get_effective_thread_policy(thread
, TASK_POLICY_DARWIN_BG
);
645 /* If current thread or task backgrounded, return background */
647 *is_backgrounded
= 1;
651 /* Check if current process app type is App, then return foreground */
652 proc_get_darwinbgstate(p
->task
, &flagsp
);
653 if ((flagsp
& PROC_FLAG_APPLICATION
) == PROC_FLAG_APPLICATION
) {
654 *is_backgrounded
= 0;
659 * Get the current voucher origin pid and it's bgstate.The pid
660 * returned here might not be valid or may have been recycled.
662 kr
= thread_get_current_voucher_origin_pid(&pid
);
663 if (kr
!= KERN_SUCCESS
) {
664 if (kr
== KERN_INVALID_TASK
) {
666 } else if (kr
== KERN_INVALID_VALUE
) {
673 ret
= proc_pidbackgrounded(pid
, is_backgrounded
);
678 proc_apply_resource_actions(void * bsdinfo
, __unused
int type
, int action
)
680 proc_t p
= (proc_t
)bsdinfo
;
683 case PROC_POLICY_RSRCACT_THROTTLE
:
684 /* no need to do anything */
687 case PROC_POLICY_RSRCACT_SUSPEND
:
688 task_suspend(p
->task
);
691 case PROC_POLICY_RSRCACT_TERMINATE
:
695 case PROC_POLICY_RSRCACT_NOTIFY_KQ
:
696 /* not implemented */
699 case PROC_POLICY_RSRCACT_NOTIFY_EXC
:
700 panic("shouldn't be applying exception notification to process!");
708 proc_restore_resource_actions(void * bsdinfo
, __unused
int type
, int action
)
710 proc_t p
= (proc_t
)bsdinfo
;
713 case PROC_POLICY_RSRCACT_THROTTLE
:
714 case PROC_POLICY_RSRCACT_TERMINATE
:
715 case PROC_POLICY_RSRCACT_NOTIFY_KQ
:
716 case PROC_POLICY_RSRCACT_NOTIFY_EXC
:
717 /* no need to do anything */
720 case PROC_POLICY_RSRCACT_SUSPEND
:
721 task_resume(p
->task
);