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>
38 #include <sys/kauth.h>
39 #include <sys/unistd.h>
41 #include <sys/ioctl.h>
45 #include <security/audit/audit.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/lock.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_background(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
74 static int handle_hwaccess(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
75 static int handle_lowresrouce(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
76 static int handle_resourceuse(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
77 static int handle_apptype(int scope
, int action
, int policy
, int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
);
79 extern kern_return_t
task_suspend(task_t
);
80 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 implementaion */
94 process_policy(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 proc
= PROC_NULL
;
105 proc_t curp
= current_proc();
106 kauth_cred_t my_cred
;
108 kauth_cred_t target_cred
;
111 if ((scope
!= PROC_POLICY_SCOPE_PROCESS
) && (scope
!= PROC_POLICY_SCOPE_THREAD
)) {
114 proc
= proc_find(target_pid
);
115 if (proc
== PROC_NULL
) {
119 my_cred
= kauth_cred_proc_ref(curp
);
122 target_cred
= kauth_cred_proc_ref(proc
);
124 if (suser(my_cred
, NULL
) && kauth_cred_getruid(my_cred
) &&
125 kauth_cred_getuid(my_cred
) != kauth_cred_getuid(target_cred
) &&
126 kauth_cred_getruid(my_cred
) != kauth_cred_getuid(target_cred
))
129 * Resoure starvation control can be used by unpriv resource owner but priv at the time of ownership claim. This is
130 * checked in low resource handle routine. So bypass the checks here.
132 if ((policy
!= PROC_POLICY_RESOURCE_STARVATION
) &&
133 (policy
!= PROC_POLICY_APPTYPE
) &&
134 (suser(my_cred
, NULL
) && curp
!= p
))
142 error
= mac_proc_check_sched(curp
, p
);
149 case PROC_POLICY_BACKGROUND
:
150 error
= handle_background(scope
, action
, policy
, policy_subtype
, attrp
, proc
, target_threadid
);
152 case PROC_POLICY_HARDWARE_ACCESS
:
153 error
= handle_hwaccess(scope
, action
, policy
, policy_subtype
, attrp
, proc
, target_threadid
);
155 case PROC_POLICY_RESOURCE_STARVATION
:
156 error
= handle_lowresrouce(scope
, action
, policy
, policy_subtype
, attrp
, proc
, target_threadid
);
158 case PROC_POLICY_RESOURCE_USAGE
:
159 error
= handle_resourceuse(scope
, action
, policy
, policy_subtype
, attrp
, proc
, target_threadid
);
161 case PROC_POLICY_APPTYPE
:
162 error
= handle_apptype(scope
, action
, policy
, policy_subtype
, attrp
, proc
, target_threadid
);
171 kauth_cred_unref(&my_cred
);
173 kauth_cred_unref(&target_cred
);
179 /* darwin background handling code */
181 handle_background(int scope
, int action
, __unused
int policy
, __unused
int policy_subtype
, user_addr_t attrp
, proc_t proc
, uint64_t target_threadid
)
183 int intval
, error
= 0;
187 case PROC_POLICY_ACTION_GET
:
188 if (scope
== PROC_POLICY_SCOPE_PROCESS
) {
189 intval
= proc_get_task_bg_policy(proc
->task
);
192 intval
= proc_get_thread_bg_policy(proc
->task
, target_threadid
);
194 error
= copyout((int *)&intval
, (user_addr_t
)attrp
, sizeof(int));
197 case PROC_POLICY_ACTION_SET
:
198 error
= copyin((user_addr_t
)attrp
, (int *)&intval
, sizeof(int));
201 if (intval
> PROC_POLICY_BG_ALL
) {
205 if (scope
== PROC_POLICY_SCOPE_PROCESS
) {
206 error
= proc_set_bgtaskpolicy(proc
->task
, intval
);
209 error
= proc_set_bgthreadpolicy(proc
->task
, target_threadid
, intval
);
213 case PROC_POLICY_ACTION_ADD
:
214 error
= copyin((user_addr_t
)attrp
, (int *)&intval
, sizeof(int));
217 if (intval
> PROC_POLICY_BG_ALL
) {
221 if (scope
== PROC_POLICY_SCOPE_PROCESS
) {
222 error
= proc_add_bgtaskpolicy(proc
->task
, intval
);
225 error
= proc_add_bgthreadpolicy(proc
->task
, target_threadid
, intval
);
229 case PROC_POLICY_ACTION_REMOVE
:
230 error
= copyin((user_addr_t
)attrp
, (int *)&intval
, sizeof(int));
233 if (intval
> PROC_POLICY_BG_ALL
) {
237 if (scope
== PROC_POLICY_SCOPE_PROCESS
) {
238 error
= proc_remove_bgtaskpolicy(proc
->task
, intval
);
241 error
= proc_remove_bgthreadpolicy(proc
->task
, target_threadid
, intval
);
245 case PROC_POLICY_ACTION_APPLY
:
246 if (scope
== PROC_POLICY_SCOPE_PROCESS
) {
247 error
= proc_apply_bgtaskpolicy(proc
->task
);
250 error
= proc_apply_bgthreadpolicy(proc
->task
, target_threadid
);
254 case PROC_POLICY_ACTION_RESTORE
:
255 if (scope
== PROC_POLICY_SCOPE_PROCESS
) {
256 error
= proc_restore_bgtaskpolicy(proc
->task
);
259 error
= proc_restore_bgthreadpolicy(proc
->task
, target_threadid
);
263 case PROC_POLICY_ACTION_DENYINHERIT
:
264 error
= proc_denyinherit_policy(proc
->task
);
267 case PROC_POLICY_ACTION_DENYSELFSET
:
268 error
= proc_denyselfset_policy(proc
->task
);
280 handle_hwaccess(__unused
int scope
, __unused
int action
, __unused
int policy
, int policy_subtype
, __unused user_addr_t attrp
, __unused proc_t proc
, __unused
uint64_t target_threadid
)
282 switch(policy_subtype
) {
283 case PROC_POLICY_HWACCESS_NONE
:
284 case PROC_POLICY_HWACCESS_DISK
:
285 case PROC_POLICY_HWACCESS_GPU
:
286 case PROC_POLICY_HWACCESS_NETWORK
:
287 case PROC_POLICY_HWACCESS_CPU
:
296 handle_lowresrouce(__unused
int scope
, int action
, __unused
int policy
, int policy_subtype
, __unused user_addr_t attrp
, proc_t proc
, __unused
uint64_t target_threadid
)
300 switch(policy_subtype
) {
301 case PROC_POLICY_RS_NONE
:
302 case PROC_POLICY_RS_VIRTUALMEM
:
308 if (action
== PROC_POLICY_ACTION_RESTORE
)
309 error
= proc_resetpcontrol(proc_pid(proc
));
318 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
)
320 proc_policy_cpuusage_attr_t cpuattr
;
323 switch(policy_subtype
) {
324 case PROC_POLICY_RUSAGE_NONE
:
325 case PROC_POLICY_RUSAGE_WIREDMEM
:
326 case PROC_POLICY_RUSAGE_VIRTMEM
:
327 case PROC_POLICY_RUSAGE_DISK
:
328 case PROC_POLICY_RUSAGE_NETWORK
:
329 case PROC_POLICY_RUSAGE_POWER
:
334 case PROC_POLICY_RUSAGE_CPU
:
339 case PROC_POLICY_ACTION_GET
:
340 error
= proc_get_task_ruse_cpu(proc
->task
, &cpuattr
.ppattr_cpu_attr
,
341 &cpuattr
.ppattr_cpu_percentage
,
342 &cpuattr
.ppattr_cpu_attr_interval
,
343 &cpuattr
.ppattr_cpu_attr_deadline
);
345 error
= copyout((proc_policy_cpuusage_attr_t
*)&cpuattr
, (user_addr_t
)attrp
, sizeof(proc_policy_cpuusage_attr_t
));
348 case PROC_POLICY_ACTION_APPLY
:
349 case PROC_POLICY_ACTION_SET
:
350 error
= copyin((user_addr_t
)attrp
, (proc_policy_cpuusage_attr_t
*)&cpuattr
, sizeof(proc_policy_cpuusage_attr_t
));
353 error
= proc_set_task_ruse_cpu(proc
->task
, cpuattr
.ppattr_cpu_attr
,
354 cpuattr
.ppattr_cpu_percentage
,
355 cpuattr
.ppattr_cpu_attr_interval
,
356 cpuattr
.ppattr_cpu_attr_deadline
);
369 handle_apptype(__unused
int scope
, int action
, __unused
int policy
, int policy_subtype
, __unused user_addr_t attrp
, proc_t proc
, __unused
uint64_t target_threadid
)
373 switch(policy_subtype
) {
374 case PROC_POLICY_OSX_APPTYPE_TAL
:
375 /* need to be super user to do this */
376 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
381 case PROC_POLICY_OSX_APPTYPE_DASHCLIENT
:
382 /* no special priv needed */
384 case PROC_POLICY_OSX_APPTYPE_NONE
:
385 case PROC_POLICY_IOS_APPTYPE
:
386 case PROC_POLICY_IOS_NONUITYPE
:
394 case PROC_POLICY_ACTION_ENABLE
:
395 /* reapply the app foreground/background policy */
396 error
= proc_enable_task_apptype(proc
->task
, policy_subtype
);
398 case PROC_POLICY_ACTION_DISABLE
:
399 /* remove the app foreground/background policy */
400 error
= proc_disable_task_apptype(proc
->task
, policy_subtype
);
412 proc_apply_resource_actions(void * bsdinfo
, int type
, int action
)
414 proc_t p
= (proc_t
)bsdinfo
;
417 case PROC_POLICY_RSRCACT_THROTTLE
:
418 /* no need to do anything */
421 case PROC_POLICY_RSRCACT_SUSPEND
:
422 task_suspend(p
->task
);
425 case PROC_POLICY_RSRCACT_TERMINATE
:
429 case PROC_POLICY_RSRCACT_NOTIFY
:
431 proc_knote(p
, NOTE_RESOURCEEND
| (type
& 0xff));
441 proc_restore_resource_actions(void * bsdinfo
, __unused
int type
, int action
)
443 proc_t p
= (proc_t
)bsdinfo
;
446 case PROC_POLICY_RSRCACT_THROTTLE
:
447 case PROC_POLICY_RSRCACT_TERMINATE
:
448 case PROC_POLICY_RSRCACT_NOTIFY
:
449 /* no need to do anything */
452 case PROC_POLICY_RSRCACT_SUSPEND
:
453 task_resume(p
->task
);