2 * Copyright (c) 2005, 2010 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>
37 #include <sys/proc_internal.h>
39 #include <sys/kauth.h>
40 #include <sys/unistd.h>
42 #include <sys/ioctl.h>
46 #include <security/audit/audit.h>
48 #include <mach/machine.h>
49 #include <mach/mach_types.h>
50 #include <mach/vm_param.h>
51 #include <kern/task.h>
52 #include <kern/kalloc.h>
53 #include <kern/assert.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_map.h>
56 #include <mach/host_info.h>
57 #include <mach/task_info.h>
58 #include <mach/thread_info.h>
59 #include <mach/vm_region.h>
61 #include <sys/process_policy.h>
62 #include <sys/proc_info.h>
63 #include <sys/bsdtask_info.h>
64 #include <sys/kdebug.h>
65 #include <sys/sysproto.h>
66 #include <sys/msgbuf.h>
68 #include <machine/machine_routines.h>
70 #include <kern/ipc_misc.h>
71 #include <vm/vm_protos.h>
73 static int handle_lowresource(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
74 static int handle_resourceuse(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
75 static int handle_apptype(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
76 static int handle_boost(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
78 extern kern_return_t
task_suspend(task_t
);
79 extern kern_return_t
task_resume(task_t
);
82 /***************************** process_policy ********************/
85 *int process_policy(int scope, int action, int policy, int policy_subtype,
86 * proc_policy_attribute_t * attrp, pid_t target_pid,
87 * uint64_t target_threadid)
88 *{ int process_policy(int scope, int action, int policy, int policy_subtype,
89 * user_addr_t attrp, pid_t target_pid, uint64_t target_threadid); }
92 /* system call implementation */
94 process_policy(__unused
struct proc
*p
, struct process_policy_args
* uap
, __unused
int32_t *retval
)
97 int scope
= uap
->scope
;
98 int policy
= uap
->policy
;
99 int action
= uap
->action
;
100 int policy_subtype
= uap
->policy_subtype
;
101 user_addr_t attrp
= uap
->attrp
;
102 pid_t target_pid
= uap
->target_pid
;
103 uint64_t target_threadid
= uap
->target_threadid
;
104 proc_t target_proc
= PROC_NULL
;
105 proc_t curp
= current_proc();
106 kauth_cred_t my_cred
;
108 if ((scope
!= PROC_POLICY_SCOPE_PROCESS
) && (scope
!= PROC_POLICY_SCOPE_THREAD
)) {
112 if (target_pid
== 0 || target_pid
== proc_selfpid())
113 target_proc
= proc_self();
115 target_proc
= proc_find(target_pid
);
117 if (target_proc
== PROC_NULL
)
120 my_cred
= kauth_cred_get();
123 * Resoure starvation control can be used by unpriv resource owner but priv at the time of ownership claim. This is
124 * checked in low resource handle routine. So bypass the checks here.
126 if ((policy
!= PROC_POLICY_RESOURCE_STARVATION
) &&
127 (policy
!= PROC_POLICY_APPTYPE
) &&
128 (!kauth_cred_issuser(my_cred
) && curp
!= p
))
136 case PROC_POLICY_BOOST
:
137 case PROC_POLICY_RESOURCE_USAGE
:
138 /* These policies do their own appropriate mac checks */
141 error
= mac_proc_check_sched(curp
, target_proc
);
145 #endif /* CONFIG_MACF */
148 case PROC_POLICY_BACKGROUND
:
151 case PROC_POLICY_HARDWARE_ACCESS
:
154 case PROC_POLICY_RESOURCE_STARVATION
:
155 error
= handle_lowresource(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
157 case PROC_POLICY_RESOURCE_USAGE
:
158 error
= handle_resourceuse(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
160 case PROC_POLICY_APPTYPE
:
161 error
= handle_apptype(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
163 case PROC_POLICY_BOOST
:
164 error
= handle_boost(scope
, action
, policy
, policy_subtype
, attrp
, target_proc
, target_threadid
);
172 proc_rele(target_proc
);
177 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
)
181 switch(policy_subtype
) {
182 case PROC_POLICY_RS_NONE
:
183 case PROC_POLICY_RS_VIRTUALMEM
:
189 if (action
== PROC_POLICY_ACTION_RESTORE
)
190 error
= proc_resetpcontrol(proc_pid(proc
));
199 handle_resourceuse(__unused
int scope
, __unused
int action
, __unused
int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, __unused
uint64_t target_threadid
)
201 proc_policy_cpuusage_attr_t cpuattr
;
203 proc_t curp
= current_proc();
206 uint64_t interval
= -1ULL;
210 switch(policy_subtype
) {
211 case PROC_POLICY_RUSAGE_NONE
:
212 case PROC_POLICY_RUSAGE_WIREDMEM
:
213 case PROC_POLICY_RUSAGE_VIRTMEM
:
214 case PROC_POLICY_RUSAGE_DISK
:
215 case PROC_POLICY_RUSAGE_NETWORK
:
216 case PROC_POLICY_RUSAGE_POWER
:
221 case PROC_POLICY_RUSAGE_CPU
:
227 /* the cpumon entitlement manages messing with CPU limits on self */
228 error
= mac_proc_check_sched(curp
, proc
);
234 * Allow a process to change CPU usage monitor parameters, unless a MAC policy
235 * overrides it with an entitlement check.
237 entitled
= (mac_proc_check_cpumon(curp
) == 0) ? TRUE
: FALSE
;
241 case PROC_POLICY_ACTION_GET
:
242 error
= proc_get_task_ruse_cpu(proc
->task
, &cpuattr
.ppattr_cpu_attr
,
244 &cpuattr
.ppattr_cpu_attr_interval
,
245 &cpuattr
.ppattr_cpu_attr_deadline
);
247 cpuattr
.ppattr_cpu_percentage
= percentage
;
248 cpuattr
.ppattr_cpu_attr_interval
/= NSEC_PER_SEC
;
249 error
= copyout((proc_policy_cpuusage_attr_t
*)&cpuattr
, (user_addr_t
)attrp
, sizeof(proc_policy_cpuusage_attr_t
));
253 case PROC_POLICY_ACTION_APPLY
:
254 case PROC_POLICY_ACTION_SET
:
255 error
= copyin((user_addr_t
)attrp
, (proc_policy_cpuusage_attr_t
*)&cpuattr
, sizeof(proc_policy_cpuusage_attr_t
));
261 * The process_policy API uses seconds as the units for the interval,
262 * but the mach task policy SPI uses nanoseconds. Do the conversion,
263 * but preserve -1 as it has special meaning.
265 if (cpuattr
.ppattr_cpu_attr_interval
!= -1ULL) {
266 interval
= cpuattr
.ppattr_cpu_attr_interval
* NSEC_PER_SEC
;
271 error
= proc_set_task_ruse_cpu(proc
->task
, cpuattr
.ppattr_cpu_attr
,
272 cpuattr
.ppattr_cpu_percentage
,
274 cpuattr
.ppattr_cpu_attr_deadline
,
278 case PROC_POLICY_ACTION_RESTORE
:
279 error
= proc_clear_task_ruse_cpu(proc
->task
, entitled
);
293 handle_apptype( int scope
,
297 __unused user_addr_t attrp
,
299 __unused
uint64_t target_threadid
)
303 if (scope
!= PROC_POLICY_SCOPE_PROCESS
)
306 /* Temporary compatibility with old importance donation interface until libproc is moved to new boost calls */
307 switch (policy_subtype
) {
308 case PROC_POLICY_IOS_DONATEIMP
:
309 if (action
!= PROC_POLICY_ACTION_ENABLE
)
311 if (target_proc
!= current_proc())
314 /* PROCESS ENABLE APPTYPE DONATEIMP */
315 task_importance_mark_donor(target_proc
->task
, TRUE
);
319 case PROC_POLICY_IOS_HOLDIMP
:
320 if (action
!= PROC_POLICY_ACTION_ENABLE
)
322 if (target_proc
!= current_proc())
325 /* PROCESS ENABLE APPTYPE HOLDIMP */
326 error
= task_importance_hold_legacy_external_assertion(current_task(), 1);
330 case PROC_POLICY_IOS_DROPIMP
:
331 if (action
!= PROC_POLICY_ACTION_ENABLE
)
333 if (target_proc
!= current_proc())
336 /* PROCESS ENABLE APPTYPE DROPIMP */
337 error
= task_importance_drop_legacy_external_assertion(current_task(), 1);
342 /* continue to TAL handling */
346 if (policy_subtype
!= PROC_POLICY_OSX_APPTYPE_TAL
)
349 /* need to be super user to do this */
350 if (kauth_cred_issuser(kauth_cred_get()) == 0)
353 if (proc_task_is_tal(target_proc
->task
) == FALSE
)
357 case PROC_POLICY_ACTION_ENABLE
:
358 /* PROCESS ENABLE APPTYPE TAL */
359 proc_set_task_policy(target_proc
->task
, THREAD_NULL
,
360 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_TAL
,
363 case PROC_POLICY_ACTION_DISABLE
:
364 /* PROCESS DISABLE APPTYPE TAL */
365 proc_set_task_policy(target_proc
->task
, THREAD_NULL
,
366 TASK_POLICY_ATTRIBUTE
, TASK_POLICY_TAL
,
367 TASK_POLICY_DISABLE
);
378 handle_boost(int scope
,
382 __unused user_addr_t attrp
,
384 __unused
uint64_t target_threadid
)
388 assert(policy
== PROC_POLICY_BOOST
);
390 if (scope
!= PROC_POLICY_SCOPE_PROCESS
)
393 if (target_proc
!= current_proc())
396 switch(policy_subtype
) {
397 case PROC_POLICY_IMP_IMPORTANT
:
398 if (task_is_importance_receiver_type(target_proc
->task
) == FALSE
)
402 case PROC_POLICY_ACTION_HOLD
:
403 /* PROCESS HOLD BOOST IMPORTANT */
404 error
= task_importance_hold_legacy_external_assertion(current_task(), 1);
406 case PROC_POLICY_ACTION_DROP
:
407 /* PROCESS DROP BOOST IMPORTANT */
408 error
= task_importance_drop_legacy_external_assertion(current_task(), 1);
416 case PROC_POLICY_IMP_DONATION
:
418 error
= mac_proc_check_sched(current_proc(), target_proc
);
419 if (error
) return error
;
422 case PROC_POLICY_ACTION_SET
:
423 /* PROCESS SET BOOST DONATION */
424 task_importance_mark_donor(target_proc
->task
, TRUE
);
442 * KPI to determine if a pid is currently backgrounded.
443 * Returns ESRCH if pid cannot be found or has started exiting.
444 * Returns EINVAL if state is NULL.
445 * Sets *state to 1 if pid is backgrounded, and 0 otherwise.
448 proc_pidbackgrounded(pid_t pid
, uint32_t* state
)
450 proc_t target_proc
= PROC_NULL
;
455 target_proc
= proc_find(pid
);
457 if (target_proc
== PROC_NULL
)
460 if ( proc_get_effective_task_policy(target_proc
->task
, TASK_POLICY_DARWIN_BG
) ) {
466 proc_rele(target_proc
);
471 * Get the darwin background state of the originator. If the current
472 * process app type is App, then it is the originator, else if it is
473 * a Daemon, then creator of the Resource Accounting attribute of
474 * the current thread voucher is the originator of the work.
477 proc_get_originatorbgstate(uint32_t *is_backgrounded
)
480 proc_t p
= current_proc();
485 thread_t thread
= current_thread();
487 bgstate
= proc_get_effective_thread_policy(thread
, TASK_POLICY_DARWIN_BG
);
489 /* If current thread or task backgrounded, return background */
491 *is_backgrounded
= 1;
495 /* Check if current process app type is App, then return foreground */
496 proc_get_darwinbgstate(p
->task
, &flagsp
);
497 if ((flagsp
& PROC_FLAG_APPLICATION
) == PROC_FLAG_APPLICATION
) {
498 *is_backgrounded
= 0;
503 * Get the current voucher origin pid and it's bgstate.The pid
504 * returned here might not be valid or may have been recycled.
506 kr
= thread_get_current_voucher_origin_pid(&pid
);
507 if (kr
!= KERN_SUCCESS
) {
508 if (kr
== KERN_INVALID_TASK
)
510 else if (kr
== KERN_INVALID_VALUE
)
516 ret
= proc_pidbackgrounded(pid
, is_backgrounded
);
521 proc_apply_resource_actions(void * bsdinfo
, __unused
int type
, int action
)
523 proc_t p
= (proc_t
)bsdinfo
;
526 case PROC_POLICY_RSRCACT_THROTTLE
:
527 /* no need to do anything */
530 case PROC_POLICY_RSRCACT_SUSPEND
:
531 task_suspend(p
->task
);
534 case PROC_POLICY_RSRCACT_TERMINATE
:
538 case PROC_POLICY_RSRCACT_NOTIFY_KQ
:
539 /* not implemented */
542 case PROC_POLICY_RSRCACT_NOTIFY_EXC
:
543 panic("shouldn't be applying exception notification to process!");
551 proc_restore_resource_actions(void * bsdinfo
, __unused
int type
, int action
)
553 proc_t p
= (proc_t
)bsdinfo
;
556 case PROC_POLICY_RSRCACT_THROTTLE
:
557 case PROC_POLICY_RSRCACT_TERMINATE
:
558 case PROC_POLICY_RSRCACT_NOTIFY_KQ
:
559 case PROC_POLICY_RSRCACT_NOTIFY_EXC
:
560 /* no need to do anything */
563 case PROC_POLICY_RSRCACT_SUSPEND
:
564 task_resume(p
->task
);