]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/proc_info.c
bcdc1c18d353bd3fed9e4f59e89b671fd8888731
[apple/xnu.git] / bsd / kern / proc_info.c
1 /*
2 * Copyright (c) 2005-2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * sysctl system call.
31 */
32
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/file_internal.h>
40 #include <sys/vnode_internal.h>
41 #include <sys/unistd.h>
42 #include <sys/buf.h>
43 #include <sys/ioctl.h>
44 #include <sys/namei.h>
45 #include <sys/tty.h>
46 #include <sys/disklabel.h>
47 #include <sys/vm.h>
48 #include <sys/reason.h>
49 #include <sys/sysctl.h>
50 #include <sys/user.h>
51 #include <sys/aio_kern.h>
52 #include <sys/kern_memorystatus.h>
53
54 #include <security/audit/audit.h>
55
56 #include <mach/machine.h>
57 #include <mach/mach_types.h>
58 #include <mach/vm_param.h>
59 #include <kern/task.h>
60 #include <kern/kalloc.h>
61 #include <kern/assert.h>
62 #include <kern/policy_internal.h>
63
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <mach/host_info.h>
67 #include <mach/task_info.h>
68 #include <mach/thread_info.h>
69 #include <mach/vm_region.h>
70 #include <mach/vm_types.h>
71
72 #include <sys/mount_internal.h>
73 #include <sys/proc_info.h>
74 #include <sys/bsdtask_info.h>
75 #include <sys/kdebug.h>
76 #include <sys/sysproto.h>
77 #include <sys/msgbuf.h>
78 #include <sys/priv.h>
79
80 #include <sys/guarded.h>
81
82 #include <machine/machine_routines.h>
83
84 #include <kern/ipc_misc.h>
85
86 #include <vm/vm_protos.h>
87
88 /* Needed by proc_pidnoteexit(), proc_pidlistuptrs() */
89 #include <sys/event.h>
90 #include <sys/codesign.h>
91
92 /* Needed by proc_listcoalitions() */
93 #ifdef CONFIG_COALITIONS
94 #include <sys/coalition.h>
95 #endif
96
97 #if CONFIG_MACF
98 #include <security/mac_framework.h>
99 #endif
100
101 struct pshmnode;
102 struct psemnode;
103 struct pipe;
104 struct kqueue;
105 struct atalk;
106
107 uint64_t get_dispatchqueue_offset_from_proc(void *);
108 uint64_t get_dispatchqueue_serialno_offset_from_proc(void *);
109 uint64_t get_dispatchqueue_label_offset_from_proc(void *p);
110 uint64_t get_return_to_kernel_offset_from_proc(void *p);
111 int proc_info_internal(int callnum, int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
112
113 /*
114 * TODO: Replace the noinline attribute below. Currently, it serves
115 * to avoid stack bloat caused by inlining multiple functions that
116 * have large stack footprints; when the functions are independent
117 * of each other (will not both be called in any given call to the
118 * caller), this only serves to bloat the stack, as we allocate
119 * space for both functions, despite the fact that we only need a
120 * fraction of that space.
121 *
122 * Long term, these functions should not be allocating everything on
123 * the stack, and should move large allocations (the huge structs
124 * that proc info deals in) to the heap, or eliminate them if
125 * possible.
126 *
127 * The functions that most desperately need to improve stack usage
128 * (starting with the worst offenders):
129 * proc_pidvnodepathinfo
130 * proc_pidinfo
131 * proc_pidregionpathinfo
132 * pid_vnodeinfopath
133 * pid_pshminfo
134 * pid_pseminfo
135 * pid_socketinfo
136 * proc_pid_rusage
137 * proc_pidoriginatorinfo
138 */
139
140 /* protos for proc_info calls */
141 static int __attribute__ ((noinline)) proc_listpids(uint32_t type, uint32_t tyoneinfo, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
142 static int __attribute__ ((noinline)) proc_pidinfo(int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
143 static int __attribute__ ((noinline)) proc_pidfdinfo(int pid, int flavor, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
144 static int __attribute__ ((noinline)) proc_kernmsgbuf(user_addr_t buffer, uint32_t buffersize, int32_t * retval);
145 static int __attribute__ ((noinline)) proc_setcontrol(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
146 static int __attribute__ ((noinline)) proc_pidfileportinfo(int pid, int flavor, mach_port_name_t name, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
147 static int __attribute__ ((noinline)) proc_dirtycontrol(int pid, int flavor, uint64_t arg, int32_t * retval);
148 static int __attribute__ ((noinline)) proc_terminate(int pid, int32_t * retval);
149 static int __attribute__ ((noinline)) proc_pid_rusage(int pid, int flavor, user_addr_t buffer, int32_t * retval);
150 static int __attribute__ ((noinline)) proc_pidoriginatorinfo(int pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
151 static int __attribute__ ((noinline)) proc_listcoalitions(int flavor, int coaltype, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
152 static int __attribute__ ((noinline)) proc_can_use_foreground_hw(int pid, user_addr_t reason, uint32_t resonsize, int32_t *retval);
153
154 /* protos for procpidinfo calls */
155 static int __attribute__ ((noinline)) proc_pidfdlist(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
156 static int __attribute__ ((noinline)) proc_pidbsdinfo(proc_t p, struct proc_bsdinfo *pbsd, int zombie);
157 static int __attribute__ ((noinline)) proc_pidshortbsdinfo(proc_t p, struct proc_bsdshortinfo *pbsd_shortp, int zombie);
158 static int __attribute__ ((noinline)) proc_pidtaskinfo(proc_t p, struct proc_taskinfo *ptinfo);
159 static int __attribute__ ((noinline)) proc_pidthreadinfo(proc_t p, uint64_t arg, bool thuniqueid, struct proc_threadinfo *pthinfo);
160 static int __attribute__ ((noinline)) proc_pidthreadpathinfo(proc_t p, uint64_t arg, struct proc_threadwithpathinfo *pinfo);
161 static int __attribute__ ((noinline)) proc_pidlistthreads(proc_t p, bool thuniqueid, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
162 static int __attribute__ ((noinline)) proc_pidregioninfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
163 static int __attribute__ ((noinline)) proc_pidregionpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
164 static int __attribute__ ((noinline)) proc_pidregionpathinfo2(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
165 static int __attribute__ ((noinline)) proc_pidregionpathinfo3(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
166 static int __attribute__ ((noinline)) proc_pidvnodepathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
167 static int __attribute__ ((noinline)) proc_pidpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
168 static int __attribute__ ((noinline)) proc_pidworkqueueinfo(proc_t p, struct proc_workqueueinfo *pwqinfo);
169 static int __attribute__ ((noinline)) proc_pidfileportlist(proc_t p, user_addr_t buffer, size_t buffersize, int32_t *retval);
170 extern void __attribute__ ((noinline)) proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
171 static void __attribute__ ((noinline)) proc_archinfo(proc_t p, struct proc_archinfo *pai);
172 static void __attribute__ ((noinline)) proc_pidcoalitioninfo(proc_t p, struct proc_pidcoalitioninfo *pci);
173 static int __attribute__ ((noinline)) proc_pidnoteexit(proc_t p, uint64_t arg, uint32_t *data);
174 static int __attribute__ ((noinline)) proc_pidexitreasoninfo(proc_t p, struct proc_exitreasoninfo *peri, struct proc_exitreasonbasicinfo *pberi);
175 static int __attribute__ ((noinline)) proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid);
176 static int __attribute__ ((noinline)) proc_pidlistuptrs(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
177 static int __attribute__ ((noinline)) proc_piddynkqueueinfo(pid_t pid, int flavor, kqueue_id_t id, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
178 static int __attribute__ ((noinline)) proc_pidregionpath(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval);
179 static int __attribute__ ((noinline)) proc_pidipctableinfo(proc_t p, struct proc_ipctableinfo *table_info);
180
181 #if CONFIG_PROC_UDATA_STORAGE
182 int __attribute__ ((noinline)) proc_udata_info(pid_t pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t *retval);
183 #endif
184
185 /* protos for proc_pidfdinfo calls */
186 static int __attribute__ ((noinline)) pid_vnodeinfo(vnode_t vp, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
187 static int __attribute__ ((noinline)) pid_vnodeinfopath(vnode_t vp, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
188 static int __attribute__ ((noinline)) pid_socketinfo(socket_t so, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
189 static int __attribute__ ((noinline)) pid_pseminfo(struct psemnode * psem, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
190 static int __attribute__ ((noinline)) pid_pshminfo(struct pshmnode * pshm, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
191 static int __attribute__ ((noinline)) pid_pipeinfo(struct pipe * p, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
192 static int __attribute__ ((noinline)) pid_kqueueinfo(struct kqueue * kq, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval);
193
194
195 /* protos for misc */
196
197 static int fill_vnodeinfo(vnode_t vp, struct vnode_info *vinfo, boolean_t check_fsgetpath);
198 static void fill_fileinfo(struct fileproc *fp, proc_t proc, int fd, struct proc_fileinfo * finfo);
199 static int proc_security_policy(proc_t targetp, int callnum, int flavor, boolean_t check_same_user);
200 static void munge_vinfo_stat(struct stat64 *sbp, struct vinfo_stat *vsbp);
201 static int proc_piduuidinfo(pid_t pid, uuid_t uuid_buf, uint32_t buffersize);
202
203 extern int proc_pidpathinfo_internal(proc_t p, __unused uint64_t arg, char *buf, uint32_t buffersize, __unused int32_t *retval);
204 extern int cansignal(struct proc *, kauth_cred_t, struct proc *, int);
205 extern int proc_get_rusage(proc_t proc, int flavor, user_addr_t buffer, int is_zombie);
206
207 #define CHECK_SAME_USER TRUE
208 #define NO_CHECK_SAME_USER FALSE
209
210 uint64_t
211 get_dispatchqueue_offset_from_proc(void *p)
212 {
213 if (p != NULL) {
214 proc_t pself = (proc_t)p;
215 return pself->p_dispatchqueue_offset;
216 } else {
217 return (uint64_t)0;
218 }
219 }
220
221 uint64_t
222 get_dispatchqueue_serialno_offset_from_proc(void *p)
223 {
224 if (p != NULL) {
225 proc_t pself = (proc_t)p;
226 return pself->p_dispatchqueue_serialno_offset;
227 } else {
228 return (uint64_t)0;
229 }
230 }
231
232 uint64_t
233 get_dispatchqueue_label_offset_from_proc(void *p)
234 {
235 if (p != NULL) {
236 proc_t pself = (proc_t)p;
237 return pself->p_dispatchqueue_label_offset;
238 } else {
239 return (uint64_t)0;
240 }
241 }
242
243 uint64_t
244 get_return_to_kernel_offset_from_proc(void *p)
245 {
246 if (p != NULL) {
247 proc_t pself = (proc_t)p;
248 return pself->p_return_to_kernel_offset;
249 } else {
250 return (uint64_t)0;
251 }
252 }
253
254 /***************************** proc_info ********************/
255
256 int
257 proc_info(__unused struct proc *p, struct proc_info_args * uap, int32_t *retval)
258 {
259 return proc_info_internal(uap->callnum, uap->pid, 0, 0, uap->flavor, uap->arg, uap->buffer, uap->buffersize, retval);
260 }
261
262 int
263 proc_info_extended_id(__unused struct proc *p, struct proc_info_extended_id_args *uap, int32_t *retval)
264 {
265 uint32_t flags = uap->flags;
266
267 if ((flags & (PIF_COMPARE_IDVERSION | PIF_COMPARE_UNIQUEID)) == (PIF_COMPARE_IDVERSION | PIF_COMPARE_UNIQUEID)) {
268 return EINVAL;
269 }
270
271 return proc_info_internal(uap->callnum, uap->pid, flags, uap->ext_id, uap->flavor, uap->arg, uap->buffer, uap->buffersize, retval);
272 }
273
274 int
275 proc_info_internal(int callnum, int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
276 {
277 switch (callnum) {
278 case PROC_INFO_CALL_LISTPIDS:
279 /* pid contains type and flavor contains typeinfo */
280 return proc_listpids(pid, flavor, buffer, buffersize, retval);
281 case PROC_INFO_CALL_PIDINFO:
282 return proc_pidinfo(pid, flags, ext_id, flavor, arg, buffer, buffersize, retval);
283 case PROC_INFO_CALL_PIDFDINFO:
284 return proc_pidfdinfo(pid, flavor, (int)arg, buffer, buffersize, retval);
285 case PROC_INFO_CALL_KERNMSGBUF:
286 return proc_kernmsgbuf(buffer, buffersize, retval);
287 case PROC_INFO_CALL_SETCONTROL:
288 return proc_setcontrol(pid, flavor, arg, buffer, buffersize, retval);
289 case PROC_INFO_CALL_PIDFILEPORTINFO:
290 return proc_pidfileportinfo(pid, flavor, (mach_port_name_t)arg, buffer, buffersize, retval);
291 case PROC_INFO_CALL_TERMINATE:
292 return proc_terminate(pid, retval);
293 case PROC_INFO_CALL_DIRTYCONTROL:
294 return proc_dirtycontrol(pid, flavor, arg, retval);
295 case PROC_INFO_CALL_PIDRUSAGE:
296 return proc_pid_rusage(pid, flavor, buffer, retval);
297 case PROC_INFO_CALL_PIDORIGINATORINFO:
298 return proc_pidoriginatorinfo(pid, flavor, buffer, buffersize, retval);
299 case PROC_INFO_CALL_LISTCOALITIONS:
300 return proc_listcoalitions(pid /* flavor */, flavor /* coaltype */, buffer,
301 buffersize, retval);
302 case PROC_INFO_CALL_CANUSEFGHW:
303 return proc_can_use_foreground_hw(pid, buffer, buffersize, retval);
304 case PROC_INFO_CALL_PIDDYNKQUEUEINFO:
305 return proc_piddynkqueueinfo(pid, flavor, (kqueue_id_t)arg, buffer, buffersize, retval);
306 #if CONFIG_PROC_UDATA_STORAGE
307 case PROC_INFO_CALL_UDATA_INFO:
308 return proc_udata_info(pid, flavor, buffer, buffersize, retval);
309 #endif /* CONFIG_PROC_UDATA_STORAGE */
310 default:
311 return EINVAL;
312 }
313
314 return EINVAL;
315 }
316
317 /******************* proc_listpids routine ****************/
318 int
319 proc_listpids(uint32_t type, uint32_t typeinfo, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
320 {
321 uint32_t numprocs = 0;
322 uint32_t wantpids;
323 int *kbuf;
324 int *ptr;
325 uint32_t n;
326 int skip;
327 struct proc * p;
328 struct tty * tp;
329 int error = 0;
330 struct proclist *current_list;
331
332 /* Do we have permission to look into this? */
333 if ((error = proc_security_policy(PROC_NULL, PROC_INFO_CALL_LISTPIDS, type, NO_CHECK_SAME_USER))) {
334 return error;
335 }
336
337 /* if the buffer is null, return num of procs */
338 if (buffer == (user_addr_t)0) {
339 *retval = ((nprocs + 20) * sizeof(int));
340 return 0;
341 }
342
343 if (buffersize < sizeof(int)) {
344 return ENOMEM;
345 }
346 wantpids = buffersize / sizeof(int);
347 if ((nprocs + 20) > 0) {
348 numprocs = (uint32_t)(nprocs + 20);
349 }
350 if (numprocs > wantpids) {
351 numprocs = wantpids;
352 }
353
354 kbuf = kheap_alloc(KHEAP_TEMP, numprocs * sizeof(int),
355 Z_WAITOK | Z_ZERO);
356 if (kbuf == NULL) {
357 return ENOMEM;
358 }
359
360 proc_list_lock();
361
362 n = 0;
363 ptr = kbuf;
364 current_list = &allproc;
365 proc_loop:
366 LIST_FOREACH(p, current_list, p_list) {
367 skip = 0;
368 switch (type) {
369 case PROC_PGRP_ONLY:
370 if (p->p_pgrpid != (pid_t)typeinfo) {
371 skip = 1;
372 }
373 break;
374 case PROC_PPID_ONLY:
375 if ((p->p_ppid != (pid_t)typeinfo) && (((p->p_lflag & P_LTRACED) == 0) || (p->p_oppid != (pid_t)typeinfo))) {
376 skip = 1;
377 }
378 break;
379
380 case PROC_ALL_PIDS:
381 skip = 0;
382 break;
383 case PROC_TTY_ONLY:
384 /* racy but list lock is held */
385 if ((p->p_flag & P_CONTROLT) == 0 ||
386 (p->p_pgrp == NULL) || (p->p_pgrp->pg_session == NULL) ||
387 (tp = SESSION_TP(p->p_pgrp->pg_session)) == TTY_NULL ||
388 tp->t_dev != (dev_t)typeinfo) {
389 skip = 1;
390 }
391 break;
392 case PROC_UID_ONLY:
393 if (p->p_ucred == NULL) {
394 skip = 1;
395 } else {
396 kauth_cred_t my_cred;
397 uid_t uid;
398
399 my_cred = kauth_cred_proc_ref(p);
400 uid = kauth_cred_getuid(my_cred);
401 kauth_cred_unref(&my_cred);
402 if (uid != (uid_t)typeinfo) {
403 skip = 1;
404 }
405 }
406 break;
407 case PROC_RUID_ONLY:
408 if (p->p_ucred == NULL) {
409 skip = 1;
410 } else {
411 kauth_cred_t my_cred;
412 uid_t uid;
413
414 my_cred = kauth_cred_proc_ref(p);
415 uid = kauth_cred_getruid(my_cred);
416 kauth_cred_unref(&my_cred);
417 if (uid != (uid_t)typeinfo) {
418 skip = 1;
419 }
420 }
421 break;
422 case PROC_KDBG_ONLY:
423 if (p->p_kdebug == 0) {
424 skip = 1;
425 }
426 break;
427 default:
428 skip = 1;
429 break;
430 }
431 ;
432
433 if (skip == 0) {
434 *ptr++ = p->p_pid;
435 n++;
436 }
437 if (n >= numprocs) {
438 break;
439 }
440 }
441
442 if ((n < numprocs) && (current_list == &allproc)) {
443 current_list = &zombproc;
444 goto proc_loop;
445 }
446
447 proc_list_unlock();
448
449 ptr = kbuf;
450 error = copyout((caddr_t)ptr, buffer, n * sizeof(int));
451 if (error == 0) {
452 *retval = (n * sizeof(int));
453 }
454 kheap_free(KHEAP_TEMP, kbuf, numprocs * sizeof(int));
455
456 return error;
457 }
458
459
460 /********************************** proc_pidfdlist routines ********************************/
461
462 static size_t
463 proc_fdlist_internal(proc_t p, struct proc_fdinfo *pfd, size_t numfds)
464 {
465 struct fileproc *fp;
466 size_t count = 0;
467
468 proc_fdlock(p);
469
470 fdt_foreach(fp, p) {
471 if (count >= numfds) {
472 break;
473 }
474 file_type_t fdtype = FILEGLOB_DTYPE(fp->fp_glob);
475 pfd[count].proc_fd = fdt_foreach_fd();
476 pfd[count].proc_fdtype = (fdtype != DTYPE_ATALK) ?
477 fdtype : PROX_FDTYPE_ATALK;
478 count++;
479 }
480
481 proc_fdunlock(p);
482 return count;
483 }
484
485 int
486 proc_pidfdlist(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval)
487 {
488 uint32_t numfds = 0;
489 uint32_t needfds;
490 char * kbuf;
491 uint32_t count = 0;
492 int error = 0;
493
494 if (p->p_fd->fd_nfiles > 0) {
495 numfds = (uint32_t)p->p_fd->fd_nfiles;
496 }
497
498 if (buffer == (user_addr_t) 0) {
499 numfds += 20;
500 *retval = (numfds * sizeof(struct proc_fdinfo));
501 return 0;
502 }
503
504 /* buffersize is big enough atleast for one struct */
505 needfds = buffersize / sizeof(struct proc_fdinfo);
506
507 if (numfds > needfds) {
508 numfds = needfds;
509 }
510
511 kbuf = kheap_alloc(KHEAP_TEMP, numfds * sizeof(struct proc_fdinfo),
512 Z_WAITOK | Z_ZERO);
513 if (kbuf == NULL) {
514 return ENOMEM;
515 }
516
517 /* cannot overflow due to count <= numfds */
518 count = (uint32_t)proc_fdlist_internal(p, (struct proc_fdinfo *)kbuf, (size_t)numfds);
519
520 error = copyout(kbuf, buffer, count * sizeof(struct proc_fdinfo));
521 kheap_free(KHEAP_TEMP, kbuf, numfds * sizeof(struct proc_fdinfo));
522 if (error == 0) {
523 *retval = count * sizeof(struct proc_fdinfo);
524 }
525 return error;
526 }
527
528 /*
529 * KPI variant of proc_pidfdlist.
530 *
531 * Caller is responsible for adding margin to *count when calling this in
532 * circumstances where file descriptors can appear/disappear between the
533 * two calls to this function.
534 */
535 int
536 proc_fdlist(proc_t p, struct proc_fdinfo *buf, size_t *count)
537 {
538 if (p == NULL || count == NULL) {
539 return EINVAL;
540 }
541
542 if (buf == NULL) {
543 proc_fdlock(p);
544 *count = (size_t)max(min(p->p_fd->fd_lastfile + 1, p->p_fd->fd_nfiles), 0);
545 proc_fdunlock(p);
546 return 0;
547 }
548
549 *count = proc_fdlist_internal(p, buf, *count);
550 return 0;
551 }
552
553 /*
554 * Helper functions for proc_pidfileportlist.
555 */
556 static int
557 proc_fileport_count(__unused mach_port_name_t name,
558 __unused struct fileglob *fg, void *arg)
559 {
560 size_t *counter = arg;
561
562 *counter += 1;
563 return 0;
564 }
565
566 struct fileport_fdtype_args {
567 struct proc_fileportinfo *ffa_pfi;
568 struct proc_fileportinfo *ffa_pfi_end;
569 };
570
571 static int
572 proc_fileport_fdtype(mach_port_name_t name, struct fileglob *fg, void *arg)
573 {
574 struct fileport_fdtype_args *ffa = arg;
575
576 if (ffa->ffa_pfi != ffa->ffa_pfi_end) {
577 file_type_t fdtype = FILEGLOB_DTYPE(fg);
578
579 ffa->ffa_pfi->proc_fdtype = (fdtype != DTYPE_ATALK) ?
580 fdtype : PROX_FDTYPE_ATALK;
581 ffa->ffa_pfi->proc_fileport = name;
582 ffa->ffa_pfi++;
583 return 0; /* keep walking */
584 } else {
585 return -1; /* stop the walk! */
586 }
587 }
588
589 int
590 proc_pidfileportlist(proc_t p,
591 user_addr_t buffer, size_t buffersize, int32_t *retval)
592 {
593 void *kbuf;
594 size_t kbufsize;
595 struct proc_fileportinfo *pfi;
596 size_t needfileports, numfileports;
597 struct fileport_fdtype_args ffa;
598 int error;
599
600 needfileports = buffersize / sizeof(*pfi);
601 if ((user_addr_t)0 == buffer || needfileports > (size_t)maxfilesperproc) {
602 /*
603 * Either (i) the user is asking for a fileport count,
604 * or (ii) the number of fileports they're asking for is
605 * larger than the maximum number of open files (!); count
606 * them to bound subsequent heap allocations.
607 */
608 numfileports = 0;
609 switch (fileport_walk(p->task,
610 proc_fileport_count, &numfileports)) {
611 case KERN_SUCCESS:
612 break;
613 case KERN_RESOURCE_SHORTAGE:
614 return ENOMEM;
615 case KERN_INVALID_TASK:
616 return ESRCH;
617 default:
618 return EINVAL;
619 }
620
621 if (numfileports == 0) {
622 *retval = 0; /* none at all, bail */
623 return 0;
624 }
625 if ((user_addr_t)0 == buffer) {
626 numfileports += 20; /* accelerate convergence */
627 *retval = (int32_t)MIN(numfileports * sizeof(*pfi), INT32_MAX);
628 return 0;
629 }
630 if (needfileports > numfileports) {
631 needfileports = numfileports;
632 }
633 }
634
635 assert(buffersize >= PROC_PIDLISTFILEPORTS_SIZE);
636
637 kbufsize = needfileports * sizeof(*pfi);
638 pfi = kbuf = kheap_alloc(KHEAP_TEMP, kbufsize, Z_WAITOK | Z_ZERO);
639 if (kbuf == NULL) {
640 return ENOMEM;
641 }
642
643 ffa.ffa_pfi = pfi;
644 ffa.ffa_pfi_end = pfi + needfileports;
645
646 switch (fileport_walk(p->task, proc_fileport_fdtype, &ffa)) {
647 case KERN_SUCCESS:
648 error = 0;
649 pfi = ffa.ffa_pfi;
650 if ((numfileports = (size_t)(pfi - (typeof(pfi))kbuf)) == 0) {
651 break;
652 }
653 if (numfileports > needfileports) {
654 panic("more fileports returned than requested");
655 }
656 error = copyout(kbuf, buffer, numfileports * sizeof(*pfi));
657 break;
658 case KERN_RESOURCE_SHORTAGE:
659 error = ENOMEM;
660 break;
661 case KERN_INVALID_TASK:
662 error = ESRCH;
663 break;
664 default:
665 error = EINVAL;
666 break;
667 }
668 kheap_free(KHEAP_TEMP, kbuf, kbufsize);
669 if (error == 0) {
670 *retval = (int32_t)MIN(numfileports * sizeof(*pfi), INT32_MAX);
671 }
672 return error;
673 }
674
675 int
676 proc_pidbsdinfo(proc_t p, struct proc_bsdinfo * pbsd, int zombie)
677 {
678 struct tty *tp;
679 struct session *sessionp = NULL;
680 struct pgrp * pg;
681 kauth_cred_t my_cred;
682
683 pg = proc_pgrp(p);
684 sessionp = proc_session(p);
685
686 my_cred = kauth_cred_proc_ref(p);
687 bzero(pbsd, sizeof(struct proc_bsdinfo));
688 pbsd->pbi_status = p->p_stat;
689 pbsd->pbi_xstatus = p->p_xstat;
690 pbsd->pbi_pid = p->p_pid;
691 pbsd->pbi_ppid = p->p_ppid;
692 pbsd->pbi_uid = kauth_cred_getuid(my_cred);
693 pbsd->pbi_gid = kauth_cred_getgid(my_cred);
694 pbsd->pbi_ruid = kauth_cred_getruid(my_cred);
695 pbsd->pbi_rgid = kauth_cred_getrgid(my_cred);
696 pbsd->pbi_svuid = kauth_cred_getsvuid(my_cred);
697 pbsd->pbi_svgid = kauth_cred_getsvgid(my_cred);
698 kauth_cred_unref(&my_cred);
699
700 pbsd->pbi_nice = p->p_nice;
701 pbsd->pbi_start_tvsec = p->p_start.tv_sec;
702 pbsd->pbi_start_tvusec = p->p_start.tv_usec;
703 bcopy(&p->p_comm, &pbsd->pbi_comm[0], MAXCOMLEN);
704 pbsd->pbi_comm[MAXCOMLEN - 1] = '\0';
705 bcopy(&p->p_name, &pbsd->pbi_name[0], 2 * MAXCOMLEN);
706 pbsd->pbi_name[(2 * MAXCOMLEN) - 1] = '\0';
707
708 pbsd->pbi_flags = 0;
709 if ((p->p_flag & P_SYSTEM) == P_SYSTEM) {
710 pbsd->pbi_flags |= PROC_FLAG_SYSTEM;
711 }
712 if ((p->p_lflag & P_LTRACED) == P_LTRACED) {
713 pbsd->pbi_flags |= PROC_FLAG_TRACED;
714 }
715 if ((p->p_lflag & P_LEXIT) == P_LEXIT) {
716 pbsd->pbi_flags |= PROC_FLAG_INEXIT;
717 }
718 if ((p->p_lflag & P_LPPWAIT) == P_LPPWAIT) {
719 pbsd->pbi_flags |= PROC_FLAG_PPWAIT;
720 }
721 if ((p->p_flag & P_LP64) == P_LP64) {
722 pbsd->pbi_flags |= PROC_FLAG_LP64;
723 }
724 if ((p->p_flag & P_CONTROLT) == P_CONTROLT) {
725 pbsd->pbi_flags |= PROC_FLAG_CONTROLT;
726 }
727 if ((p->p_flag & P_THCWD) == P_THCWD) {
728 pbsd->pbi_flags |= PROC_FLAG_THCWD;
729 }
730 if ((p->p_flag & P_SUGID) == P_SUGID) {
731 pbsd->pbi_flags |= PROC_FLAG_PSUGID;
732 }
733 if ((p->p_flag & P_EXEC) == P_EXEC) {
734 pbsd->pbi_flags |= PROC_FLAG_EXEC;
735 }
736
737 if (sessionp != SESSION_NULL) {
738 if (SESS_LEADER(p, sessionp)) {
739 pbsd->pbi_flags |= PROC_FLAG_SLEADER;
740 }
741 if (sessionp->s_ttyvp) {
742 pbsd->pbi_flags |= PROC_FLAG_CTTY;
743 }
744 }
745
746 #if CONFIG_DELAY_IDLE_SLEEP
747 if ((p->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP) {
748 pbsd->pbi_flags |= PROC_FLAG_DELAYIDLESLEEP;
749 }
750 #endif /* CONFIG_DELAY_IDLE_SLEEP */
751
752 switch (PROC_CONTROL_STATE(p)) {
753 case P_PCTHROTTLE:
754 pbsd->pbi_flags |= PROC_FLAG_PC_THROTTLE;
755 break;
756 case P_PCSUSP:
757 pbsd->pbi_flags |= PROC_FLAG_PC_SUSP;
758 break;
759 case P_PCKILL:
760 pbsd->pbi_flags |= PROC_FLAG_PC_KILL;
761 break;
762 }
763 ;
764
765 switch (PROC_ACTION_STATE(p)) {
766 case P_PCTHROTTLE:
767 pbsd->pbi_flags |= PROC_FLAG_PA_THROTTLE;
768 break;
769 case P_PCSUSP:
770 pbsd->pbi_flags |= PROC_FLAG_PA_SUSP;
771 break;
772 }
773 ;
774
775 /* if process is a zombie skip bg state */
776 if ((zombie == 0) && (p->p_stat != SZOMB) && (p->task != TASK_NULL)) {
777 proc_get_darwinbgstate(p->task, &pbsd->pbi_flags);
778 }
779
780 if (zombie == 0) {
781 pbsd->pbi_nfiles = p->p_fd->fd_nfiles;
782 }
783
784 pbsd->e_tdev = NODEV;
785 if (pg != PGRP_NULL) {
786 pbsd->pbi_pgid = p->p_pgrpid;
787 pbsd->pbi_pjobc = pg->pg_jobc;
788 if ((p->p_flag & P_CONTROLT) && (sessionp != SESSION_NULL) && (tp = SESSION_TP(sessionp))) {
789 pbsd->e_tdev = tp->t_dev;
790 pbsd->e_tpgid = sessionp->s_ttypgrpid;
791 }
792 }
793 if (sessionp != SESSION_NULL) {
794 session_rele(sessionp);
795 }
796 if (pg != PGRP_NULL) {
797 pg_rele(pg);
798 }
799
800 return 0;
801 }
802
803
804 int
805 proc_pidshortbsdinfo(proc_t p, struct proc_bsdshortinfo * pbsd_shortp, int zombie)
806 {
807 bzero(pbsd_shortp, sizeof(struct proc_bsdshortinfo));
808 pbsd_shortp->pbsi_pid = p->p_pid;
809 pbsd_shortp->pbsi_ppid = p->p_ppid;
810 pbsd_shortp->pbsi_pgid = p->p_pgrpid;
811 pbsd_shortp->pbsi_status = p->p_stat;
812 bcopy(&p->p_comm, &pbsd_shortp->pbsi_comm[0], MAXCOMLEN);
813 pbsd_shortp->pbsi_comm[MAXCOMLEN - 1] = '\0';
814
815 pbsd_shortp->pbsi_flags = 0;
816 if ((p->p_flag & P_SYSTEM) == P_SYSTEM) {
817 pbsd_shortp->pbsi_flags |= PROC_FLAG_SYSTEM;
818 }
819 if ((p->p_lflag & P_LTRACED) == P_LTRACED) {
820 pbsd_shortp->pbsi_flags |= PROC_FLAG_TRACED;
821 }
822 if ((p->p_lflag & P_LEXIT) == P_LEXIT) {
823 pbsd_shortp->pbsi_flags |= PROC_FLAG_INEXIT;
824 }
825 if ((p->p_lflag & P_LPPWAIT) == P_LPPWAIT) {
826 pbsd_shortp->pbsi_flags |= PROC_FLAG_PPWAIT;
827 }
828 if ((p->p_flag & P_LP64) == P_LP64) {
829 pbsd_shortp->pbsi_flags |= PROC_FLAG_LP64;
830 }
831 if ((p->p_flag & P_CONTROLT) == P_CONTROLT) {
832 pbsd_shortp->pbsi_flags |= PROC_FLAG_CONTROLT;
833 }
834 if ((p->p_flag & P_THCWD) == P_THCWD) {
835 pbsd_shortp->pbsi_flags |= PROC_FLAG_THCWD;
836 }
837 if ((p->p_flag & P_SUGID) == P_SUGID) {
838 pbsd_shortp->pbsi_flags |= PROC_FLAG_PSUGID;
839 }
840 if ((p->p_flag & P_EXEC) == P_EXEC) {
841 pbsd_shortp->pbsi_flags |= PROC_FLAG_EXEC;
842 }
843 #if CONFIG_DELAY_IDLE_SLEEP
844 if ((p->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP) {
845 pbsd_shortp->pbsi_flags |= PROC_FLAG_DELAYIDLESLEEP;
846 }
847 #endif /* CONFIG_DELAY_IDLE_SLEEP */
848
849 switch (PROC_CONTROL_STATE(p)) {
850 case P_PCTHROTTLE:
851 pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_THROTTLE;
852 break;
853 case P_PCSUSP:
854 pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_SUSP;
855 break;
856 case P_PCKILL:
857 pbsd_shortp->pbsi_flags |= PROC_FLAG_PC_KILL;
858 break;
859 }
860 ;
861
862 switch (PROC_ACTION_STATE(p)) {
863 case P_PCTHROTTLE:
864 pbsd_shortp->pbsi_flags |= PROC_FLAG_PA_THROTTLE;
865 break;
866 case P_PCSUSP:
867 pbsd_shortp->pbsi_flags |= PROC_FLAG_PA_SUSP;
868 break;
869 }
870 ;
871
872 /* if process is a zombie skip bg state */
873 if ((zombie == 0) && (p->p_stat != SZOMB) && (p->task != TASK_NULL)) {
874 proc_get_darwinbgstate(p->task, &pbsd_shortp->pbsi_flags);
875 }
876
877 pbsd_shortp->pbsi_uid = p->p_uid;
878 pbsd_shortp->pbsi_gid = p->p_gid;
879 pbsd_shortp->pbsi_ruid = p->p_ruid;
880 pbsd_shortp->pbsi_rgid = p->p_rgid;
881 pbsd_shortp->pbsi_svuid = p->p_svuid;
882 pbsd_shortp->pbsi_svgid = p->p_svgid;
883
884 return 0;
885 }
886
887 int
888 proc_pidtaskinfo(proc_t p, struct proc_taskinfo * ptinfo)
889 {
890 task_t task;
891
892 task = p->task;
893
894 bzero(ptinfo, sizeof(struct proc_taskinfo));
895 fill_taskprocinfo(task, (struct proc_taskinfo_internal *)ptinfo);
896
897 return 0;
898 }
899
900
901
902 int
903 proc_pidthreadinfo(proc_t p, uint64_t arg, bool thuniqueid, struct proc_threadinfo *pthinfo)
904 {
905 int error = 0;
906 uint64_t threadaddr = (uint64_t)arg;
907
908 bzero(pthinfo, sizeof(struct proc_threadinfo));
909
910 error = fill_taskthreadinfo(p->task, threadaddr, thuniqueid, (struct proc_threadinfo_internal *)pthinfo, NULL, NULL);
911 if (error) {
912 return ESRCH;
913 } else {
914 return 0;
915 }
916 }
917
918 boolean_t
919 bsd_hasthreadname(void *uth)
920 {
921 struct uthread *ut = (struct uthread*)uth;
922
923 /* This doesn't check for the empty string; do we care? */
924 if (ut->pth_name) {
925 return TRUE;
926 } else {
927 return FALSE;
928 }
929 }
930
931 void
932 bsd_getthreadname(void *uth, char *buffer)
933 {
934 struct uthread *ut = (struct uthread *)uth;
935 if (ut->pth_name) {
936 bcopy(ut->pth_name, buffer, MAXTHREADNAMESIZE);
937 } else {
938 *buffer = '\0';
939 }
940 }
941
942 /*
943 * This is known to race with regards to the contents of the thread name; concurrent
944 * callers may result in a garbled name.
945 */
946 void
947 bsd_setthreadname(void *uth, const char *name)
948 {
949 struct uthread *ut = (struct uthread *)uth;
950 char * name_buf = NULL;
951
952 if (!ut->pth_name) {
953 /* If there is no existing thread name, allocate a buffer for one. */
954 name_buf = kalloc(MAXTHREADNAMESIZE);
955 assert(name_buf);
956 bzero(name_buf, MAXTHREADNAMESIZE);
957
958 /* Someone could conceivably have named the thread at the same time we did. */
959 if (!OSCompareAndSwapPtr(NULL, name_buf, &ut->pth_name)) {
960 kfree(name_buf, MAXTHREADNAMESIZE);
961 }
962 } else {
963 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV, ut->pth_name);
964 }
965
966 strncpy(ut->pth_name, name, MAXTHREADNAMESIZE - 1);
967 kernel_debug_string_simple(TRACE_STRING_THREADNAME, ut->pth_name);
968 }
969
970 void
971 bsd_copythreadname(void *dst_uth, void *src_uth)
972 {
973 struct uthread *dst_ut = (struct uthread *)dst_uth;
974 struct uthread *src_ut = (struct uthread *)src_uth;
975
976 if (src_ut->pth_name == NULL) {
977 return;
978 }
979
980 if (dst_ut->pth_name == NULL) {
981 dst_ut->pth_name = (char *)kalloc(MAXTHREADNAMESIZE);
982 if (dst_ut->pth_name == NULL) {
983 return;
984 }
985 }
986
987 bcopy(src_ut->pth_name, dst_ut->pth_name, MAXTHREADNAMESIZE);
988 return;
989 }
990
991 void
992 bsd_threadcdir(void * uth, void *vptr, int *vidp)
993 {
994 struct uthread * ut = (struct uthread *)uth;
995 vnode_t vp;
996 vnode_t *vpp = (vnode_t *)vptr;
997
998 vp = ut->uu_cdir;
999 if (vp != NULLVP) {
1000 if (vpp != NULL) {
1001 *vpp = vp;
1002 if (vidp != NULL) {
1003 *vidp = vp->v_id;
1004 }
1005 }
1006 }
1007 }
1008
1009
1010 int
1011 proc_pidthreadpathinfo(proc_t p, uint64_t arg, struct proc_threadwithpathinfo *pinfo)
1012 {
1013 vnode_t vp = NULLVP;
1014 int vid;
1015 int error = 0;
1016 uint64_t threadaddr = (uint64_t)arg;
1017 int count;
1018
1019 bzero(pinfo, sizeof(struct proc_threadwithpathinfo));
1020
1021 error = fill_taskthreadinfo(p->task, threadaddr, 0, (struct proc_threadinfo_internal *)&pinfo->pt, (void *)&vp, &vid);
1022 if (error) {
1023 return ESRCH;
1024 }
1025
1026 if ((vp != NULLVP) && ((vnode_getwithvid(vp, vid)) == 0)) {
1027 error = fill_vnodeinfo(vp, &pinfo->pvip.vip_vi, FALSE);
1028 if (error == 0) {
1029 count = MAXPATHLEN;
1030 vn_getpath(vp, &pinfo->pvip.vip_path[0], &count);
1031 pinfo->pvip.vip_path[MAXPATHLEN - 1] = 0;
1032 }
1033 vnode_put(vp);
1034 }
1035 return error;
1036 }
1037
1038
1039
1040 int
1041 proc_pidlistthreads(proc_t p, bool thuniqueid, user_addr_t buffer, uint32_t buffersize, int32_t *retval)
1042 {
1043 uint32_t count = 0;
1044 int ret = 0;
1045 int error = 0;
1046 void * kbuf;
1047 uint32_t numthreads = 0;
1048
1049 int num = get_numthreads(p->task) + 10;
1050 if (num > 0) {
1051 numthreads = (uint32_t)num;
1052 }
1053
1054 count = buffersize / (sizeof(uint64_t));
1055
1056 if (numthreads > count) {
1057 numthreads = count;
1058 }
1059
1060 kbuf = kheap_alloc(KHEAP_TEMP,
1061 numthreads * sizeof(uint64_t), Z_WAITOK | Z_ZERO);
1062 if (kbuf == NULL) {
1063 return ENOMEM;
1064 }
1065
1066 ret = fill_taskthreadlist(p->task, kbuf, numthreads, thuniqueid);
1067
1068 error = copyout(kbuf, buffer, ret);
1069 kheap_free(KHEAP_TEMP, kbuf, numthreads * sizeof(uint64_t));
1070 if (error == 0) {
1071 *retval = ret;
1072 }
1073 return error;
1074 }
1075
1076
1077 int
1078 proc_pidregioninfo(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1079 {
1080 struct proc_regioninfo preginfo;
1081 int ret, error = 0;
1082
1083 bzero(&preginfo, sizeof(struct proc_regioninfo));
1084 ret = fill_procregioninfo( p->task, arg, (struct proc_regioninfo_internal *)&preginfo, (uintptr_t *)0, (uint32_t *)0);
1085 if (ret == 0) {
1086 return EINVAL;
1087 }
1088 error = copyout(&preginfo, buffer, sizeof(struct proc_regioninfo));
1089 if (error == 0) {
1090 *retval = sizeof(struct proc_regioninfo);
1091 }
1092 return error;
1093 }
1094
1095
1096 int
1097 proc_pidregionpathinfo(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1098 {
1099 struct proc_regionwithpathinfo preginfo;
1100 int ret, error = 0;
1101 uintptr_t vnodeaddr = 0;
1102 uint32_t vnodeid = 0;
1103 vnode_t vp;
1104 int count;
1105
1106 bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));
1107
1108 ret = fill_procregioninfo( p->task, arg, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
1109 if (ret == 0) {
1110 return EINVAL;
1111 }
1112 if (vnodeaddr) {
1113 vp = (vnode_t)vnodeaddr;
1114 if ((vnode_getwithvid(vp, vnodeid)) == 0) {
1115 /* FILL THE VNODEINFO */
1116 error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi, FALSE);
1117 count = MAXPATHLEN;
1118 vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
1119 /* Always make sure it is null terminated */
1120 preginfo.prp_vip.vip_path[MAXPATHLEN - 1] = 0;
1121 vnode_put(vp);
1122 }
1123 }
1124 error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
1125 if (error == 0) {
1126 *retval = sizeof(struct proc_regionwithpathinfo);
1127 }
1128 return error;
1129 }
1130
1131 int
1132 proc_pidregionpathinfo2(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1133 {
1134 struct proc_regionwithpathinfo preginfo;
1135 int ret, error = 0;
1136 uintptr_t vnodeaddr = 0;
1137 uint32_t vnodeid = 0;
1138 vnode_t vp;
1139 int count;
1140
1141 bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));
1142
1143 ret = fill_procregioninfo_onlymappedvnodes( p->task, arg, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
1144 if (ret == 0) {
1145 return EINVAL;
1146 }
1147 if (!vnodeaddr) {
1148 return EINVAL;
1149 }
1150
1151 vp = (vnode_t)vnodeaddr;
1152 if ((vnode_getwithvid(vp, vnodeid)) == 0) {
1153 /* FILL THE VNODEINFO */
1154 error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi, FALSE);
1155 count = MAXPATHLEN;
1156 vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
1157 /* Always make sure it is null terminated */
1158 preginfo.prp_vip.vip_path[MAXPATHLEN - 1] = 0;
1159 vnode_put(vp);
1160 } else {
1161 return EINVAL;
1162 }
1163
1164 error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
1165 if (error == 0) {
1166 *retval = sizeof(struct proc_regionwithpathinfo);
1167 }
1168 return error;
1169 }
1170
1171 int
1172 proc_pidregionpath(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1173 {
1174 struct proc_regionpath path = {};
1175 int ret, error = 0;
1176 uintptr_t vnodeaddr = 0;
1177 uint32_t vnodeid = 0;
1178 vnode_t vp;
1179
1180 ret = find_region_details(p->task, (vm_map_offset_t) arg,
1181 (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid,
1182 &path.prpo_addr, &path.prpo_regionlength);
1183 if (ret == 0) {
1184 return EINVAL;
1185 }
1186 if (!vnodeaddr) {
1187 return EINVAL;
1188 }
1189
1190 vp = (vnode_t)vnodeaddr;
1191 if ((vnode_getwithvid(vp, vnodeid)) == 0) {
1192 int count = MAXPATHLEN;
1193 vn_getpath(vp, &path.prpo_path[0], &count);
1194 /* Always make sure it is null terminated */
1195 path.prpo_path[MAXPATHLEN - 1] = 0;
1196 vnode_put(vp);
1197 } else {
1198 return EINVAL;
1199 }
1200
1201 error = copyout(&path, buffer, sizeof(struct proc_regionpath));
1202 if (error == 0) {
1203 *retval = sizeof(struct proc_regionpath);
1204 }
1205 return error;
1206 }
1207
1208 int
1209 proc_pidregionpathinfo3(proc_t p, uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1210 {
1211 struct proc_regionwithpathinfo preginfo;
1212 int ret, error = 0;
1213 uintptr_t vnodeaddr;
1214 uint32_t vnodeid;
1215 vnode_t vp;
1216 int count;
1217 uint64_t addr = 0;
1218
1219 /* Loop while looking for vnodes that match dev_t filter */
1220 do {
1221 bzero(&preginfo, sizeof(struct proc_regionwithpathinfo));
1222 vnodeaddr = 0;
1223 vnodeid = 0;
1224
1225 ret = fill_procregioninfo_onlymappedvnodes( p->task, addr, (struct proc_regioninfo_internal *)&preginfo.prp_prinfo, (uintptr_t *)&vnodeaddr, (uint32_t *)&vnodeid);
1226 if (ret == 0) {
1227 return EINVAL;
1228 }
1229 if (!vnodeaddr) {
1230 return EINVAL;
1231 }
1232
1233 vp = (vnode_t)vnodeaddr;
1234 if ((vnode_getwithvid(vp, vnodeid)) == 0) {
1235 /* Check if the vnode matches the filter, otherwise loop looking for the next memory region backed by a vnode */
1236 struct vnode_attr va;
1237
1238 memset(&va, 0, sizeof(va));
1239 VATTR_INIT(&va);
1240 VATTR_WANTED(&va, va_fsid);
1241 VATTR_WANTED(&va, va_fsid64);
1242
1243 ret = vnode_getattr(vp, &va, vfs_context_current());
1244 if (ret) {
1245 vnode_put(vp);
1246 return EINVAL;
1247 }
1248
1249 if (vnode_get_va_fsid(&va) == arg) {
1250 /* FILL THE VNODEINFO */
1251 error = fill_vnodeinfo(vp, &preginfo.prp_vip.vip_vi, FALSE);
1252 count = MAXPATHLEN;
1253 vn_getpath(vp, &preginfo.prp_vip.vip_path[0], &count);
1254 /* Always make sure it is null terminated */
1255 preginfo.prp_vip.vip_path[MAXPATHLEN - 1] = 0;
1256 vnode_put(vp);
1257 break;
1258 }
1259 vnode_put(vp);
1260 } else {
1261 return EINVAL;
1262 }
1263
1264 addr = preginfo.prp_prinfo.pri_address + preginfo.prp_prinfo.pri_size;
1265 } while (1);
1266
1267 error = copyout(&preginfo, buffer, sizeof(struct proc_regionwithpathinfo));
1268 if (error == 0) {
1269 *retval = sizeof(struct proc_regionwithpathinfo);
1270 }
1271 return error;
1272 }
1273
1274 /*
1275 * Path is relative to current process directory; may different from current
1276 * thread directory.
1277 */
1278 int
1279 proc_pidvnodepathinfo(proc_t p, __unused uint64_t arg, user_addr_t buffer, __unused uint32_t buffersize, int32_t *retval)
1280 {
1281 struct proc_vnodepathinfo pvninfo;
1282 int error = 0;
1283 vnode_t vncdirvp = NULLVP;
1284 uint32_t vncdirid = 0;
1285 vnode_t vnrdirvp = NULLVP;
1286 uint32_t vnrdirid = 0;
1287 int count;
1288
1289 bzero(&pvninfo, sizeof(struct proc_vnodepathinfo));
1290
1291 proc_fdlock(p);
1292 if (p->p_fd->fd_cdir) {
1293 vncdirvp = p->p_fd->fd_cdir;
1294 vncdirid = p->p_fd->fd_cdir->v_id;
1295 }
1296 if (p->p_fd->fd_rdir) {
1297 vnrdirvp = p->p_fd->fd_rdir;
1298 vnrdirid = p->p_fd->fd_rdir->v_id;
1299 }
1300 proc_fdunlock(p);
1301
1302 if (vncdirvp != NULLVP) {
1303 if ((error = vnode_getwithvid(vncdirvp, vncdirid)) == 0) {
1304 /* FILL THE VNODEINFO */
1305 error = fill_vnodeinfo(vncdirvp, &pvninfo.pvi_cdir.vip_vi, TRUE);
1306 if (error == 0) {
1307 count = MAXPATHLEN;
1308 vn_getpath(vncdirvp, &pvninfo.pvi_cdir.vip_path[0], &count);
1309 pvninfo.pvi_cdir.vip_path[MAXPATHLEN - 1] = 0;
1310 }
1311 vnode_put(vncdirvp);
1312 } else {
1313 goto out;
1314 }
1315 }
1316
1317 if ((error == 0) && (vnrdirvp != NULLVP)) {
1318 if ((error = vnode_getwithvid(vnrdirvp, vnrdirid)) == 0) {
1319 /* FILL THE VNODEINFO */
1320 error = fill_vnodeinfo(vnrdirvp, &pvninfo.pvi_rdir.vip_vi, TRUE);
1321 if (error == 0) {
1322 count = MAXPATHLEN;
1323 vn_getpath(vnrdirvp, &pvninfo.pvi_rdir.vip_path[0], &count);
1324 pvninfo.pvi_rdir.vip_path[MAXPATHLEN - 1] = 0;
1325 }
1326 vnode_put(vnrdirvp);
1327 } else {
1328 goto out;
1329 }
1330 }
1331 if (error == 0) {
1332 error = copyout(&pvninfo, buffer, sizeof(struct proc_vnodepathinfo));
1333 if (error == 0) {
1334 *retval = sizeof(struct proc_vnodepathinfo);
1335 }
1336 }
1337 out:
1338 return error;
1339 }
1340
1341 int
1342 proc_pidpathinfo(proc_t p, __unused uint64_t arg, user_addr_t buffer, uint32_t buffersize, __unused int32_t *retval)
1343 {
1344 int error;
1345 vnode_t tvp;
1346 int len = buffersize;
1347 char * buf;
1348
1349 tvp = p->p_textvp;
1350
1351 if (tvp == NULLVP) {
1352 return ESRCH;
1353 }
1354
1355 buf = kheap_alloc(KHEAP_TEMP, buffersize, Z_WAITOK | Z_ZERO);
1356 if (buf == NULL) {
1357 return ENOMEM;
1358 }
1359
1360 error = proc_pidpathinfo_internal(p, arg, buf, buffersize, retval);
1361 if (error == 0) {
1362 error = copyout(buf, buffer, len);
1363 }
1364 kheap_free(KHEAP_TEMP, buf, buffersize);
1365 return error;
1366 }
1367
1368 int
1369 proc_pidpathinfo_internal(proc_t p, __unused uint64_t arg, char *buf, uint32_t buffersize, __unused int32_t *retval)
1370 {
1371 int vid, error;
1372 vnode_t tvp;
1373 vnode_t nvp = NULLVP;
1374 int len = buffersize;
1375
1376 tvp = p->p_textvp;
1377
1378 if (tvp == NULLVP) {
1379 return ESRCH;
1380 }
1381
1382 vid = vnode_vid(tvp);
1383 error = vnode_getwithvid(tvp, vid);
1384 if (error == 0) {
1385 error = vn_getpath_fsenter(tvp, buf, &len);
1386 vnode_put(tvp);
1387 if (error == 0) {
1388 error = vnode_lookup(buf, 0, &nvp, vfs_context_current());
1389 if ((error == 0) && (nvp != NULLVP)) {
1390 vnode_put(nvp);
1391 }
1392 }
1393 }
1394 return error;
1395 }
1396
1397
1398 int
1399 proc_pidworkqueueinfo(proc_t p, struct proc_workqueueinfo *pwqinfo)
1400 {
1401 int error = 0;
1402
1403 bzero(pwqinfo, sizeof(struct proc_workqueueinfo));
1404
1405 error = fill_procworkqueue(p, pwqinfo);
1406 if (error) {
1407 return ESRCH;
1408 } else {
1409 return 0;
1410 }
1411 }
1412
1413
1414 void
1415 proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo)
1416 {
1417 p_uniqidinfo->p_uniqueid = proc_uniqueid(p);
1418 proc_getexecutableuuid(p, (unsigned char *)&p_uniqidinfo->p_uuid, sizeof(p_uniqidinfo->p_uuid));
1419 p_uniqidinfo->p_puniqueid = proc_puniqueid(p);
1420 p_uniqidinfo->p_idversion = proc_pidversion(p);
1421 p_uniqidinfo->p_reserve2 = 0;
1422 p_uniqidinfo->p_reserve3 = 0;
1423 p_uniqidinfo->p_reserve4 = 0;
1424 }
1425
1426
1427 static int
1428 proc_piduuidinfo(pid_t pid, uuid_t uuid_buf, uint32_t buffersize)
1429 {
1430 struct proc * p = PROC_NULL;
1431 int zombref = 0;
1432
1433 if (buffersize < sizeof(uuid_t)) {
1434 return EINVAL;
1435 }
1436
1437 if ((p = proc_find(pid)) == PROC_NULL) {
1438 p = proc_find_zombref(pid);
1439 zombref = 1;
1440 }
1441 if (p == PROC_NULL) {
1442 return ESRCH;
1443 }
1444
1445 proc_getexecutableuuid(p, (unsigned char *)uuid_buf, buffersize);
1446
1447 if (zombref) {
1448 proc_drop_zombref(p);
1449 } else {
1450 proc_rele(p);
1451 }
1452
1453 return 0;
1454 }
1455
1456 /*
1457 * Function to get the uuid and pid of the originator of the voucher.
1458 */
1459 int
1460 proc_pidoriginatorpid_uuid(uuid_t uuid, uint32_t buffersize, pid_t *pid)
1461 {
1462 pid_t originator_pid;
1463 kern_return_t kr;
1464 int error;
1465
1466 /*
1467 * Get the current voucher origin pid. The pid returned here
1468 * might not be valid or may have been recycled.
1469 */
1470 kr = thread_get_current_voucher_origin_pid(&originator_pid);
1471 /* If errors, convert errors to appropriate format */
1472 if (kr) {
1473 if (kr == KERN_INVALID_TASK) {
1474 error = ESRCH;
1475 } else if (kr == KERN_INVALID_VALUE) {
1476 error = ENOATTR;
1477 } else {
1478 error = EINVAL;
1479 }
1480 return error;
1481 }
1482
1483 *pid = originator_pid;
1484 error = proc_piduuidinfo(originator_pid, uuid, buffersize);
1485 return error;
1486 }
1487
1488 /*
1489 * Function to get the uuid of the originator of the voucher.
1490 */
1491 int
1492 proc_pidoriginatoruuid(uuid_t uuid, uint32_t buffersize)
1493 {
1494 pid_t originator_pid;
1495 return proc_pidoriginatorpid_uuid(uuid, buffersize, &originator_pid);
1496 }
1497
1498 /*
1499 * Function to get the task ipc table size.
1500 */
1501 int
1502 proc_pidipctableinfo(proc_t p, struct proc_ipctableinfo *table_info)
1503 {
1504 task_t task;
1505 int error = 0;
1506
1507 task = p->task;
1508
1509 bzero(table_info, sizeof(struct proc_ipctableinfo));
1510 error = fill_taskipctableinfo(task, &(table_info->table_size), &(table_info->table_free));
1511
1512 if (error) {
1513 error = EINVAL;
1514 }
1515
1516 return error;
1517 }
1518
1519 /***************************** proc_pidoriginatorinfo ***************************/
1520
1521 int
1522 proc_pidoriginatorinfo(int pid, int flavor, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
1523 {
1524 int error = ENOTSUP;
1525 uint32_t size;
1526
1527 switch (flavor) {
1528 case PROC_PIDORIGINATOR_UUID:
1529 size = PROC_PIDORIGINATOR_UUID_SIZE;
1530 break;
1531 case PROC_PIDORIGINATOR_BGSTATE:
1532 size = PROC_PIDORIGINATOR_BGSTATE_SIZE;
1533 break;
1534 case PROC_PIDORIGINATOR_PID_UUID:
1535 size = PROC_PIDORIGINATOR_PID_UUID_SIZE;
1536 break;
1537 default:
1538 return EINVAL;
1539 }
1540
1541 if (buffersize < size) {
1542 return ENOMEM;
1543 }
1544
1545 if (pid != 0 && pid != proc_selfpid()) {
1546 return EINVAL;
1547 }
1548
1549 switch (flavor) {
1550 case PROC_PIDORIGINATOR_UUID: {
1551 uuid_t uuid = {};
1552
1553 error = proc_pidoriginatoruuid(uuid, sizeof(uuid));
1554 if (error != 0) {
1555 goto out;
1556 }
1557
1558 error = copyout(uuid, buffer, size);
1559 if (error == 0) {
1560 *retval = size;
1561 }
1562 }
1563 break;
1564
1565 case PROC_PIDORIGINATOR_PID_UUID: {
1566 struct proc_originatorinfo originator_info;
1567 bzero(&originator_info, sizeof(originator_info));
1568
1569 error = proc_pidoriginatorpid_uuid(originator_info.originator_uuid,
1570 sizeof(uuid_t), &originator_info.originator_pid);
1571 if (error != 0) {
1572 goto out;
1573 }
1574
1575 error = copyout(&originator_info, buffer, size);
1576 if (error == 0) {
1577 *retval = size;
1578 }
1579 }
1580 break;
1581
1582 case PROC_PIDORIGINATOR_BGSTATE: {
1583 uint32_t is_backgrounded = 0;
1584 error = proc_get_originatorbgstate(&is_backgrounded);
1585 if (error) {
1586 goto out;
1587 }
1588
1589 error = copyout(&is_backgrounded, buffer, size);
1590 if (error == 0) {
1591 *retval = size;
1592 }
1593 }
1594 break;
1595
1596 default:
1597 error = ENOTSUP;
1598 }
1599 out:
1600 return error;
1601 }
1602
1603 /***************************** proc_listcoalitions ***************************/
1604 int
1605 proc_listcoalitions(int flavor, int type, user_addr_t buffer,
1606 uint32_t buffersize, int32_t *retval)
1607 {
1608 #if CONFIG_COALITIONS
1609 int error = ENOTSUP;
1610 int coal_type;
1611 uint32_t elem_size;
1612 void *coalinfo = NULL;
1613 uint32_t k_buffersize = 0, copyout_sz = 0;
1614 int ncoals = 0, ncoals_ = 0;
1615
1616 /* struct procinfo_coalinfo; */
1617
1618 switch (flavor) {
1619 case LISTCOALITIONS_ALL_COALS:
1620 elem_size = LISTCOALITIONS_ALL_COALS_SIZE;
1621 coal_type = -1;
1622 break;
1623 case LISTCOALITIONS_SINGLE_TYPE:
1624 elem_size = LISTCOALITIONS_SINGLE_TYPE_SIZE;
1625 coal_type = type;
1626 break;
1627 default:
1628 return EINVAL;
1629 }
1630
1631 /* find the total number of coalitions */
1632 ncoals = coalitions_get_list(coal_type, NULL, 0);
1633
1634 if (ncoals == 0 || buffer == 0 || buffersize == 0) {
1635 /*
1636 * user just wants buffer size
1637 * or there are no coalitions
1638 */
1639 error = 0;
1640 *retval = (int)(ncoals * elem_size);
1641 goto out;
1642 }
1643
1644 k_buffersize = ncoals * elem_size;
1645 coalinfo = kheap_alloc(KHEAP_TEMP, k_buffersize, Z_WAITOK | Z_ZERO);
1646 if (!coalinfo) {
1647 error = ENOMEM;
1648 goto out;
1649 }
1650
1651 switch (flavor) {
1652 case LISTCOALITIONS_ALL_COALS:
1653 case LISTCOALITIONS_SINGLE_TYPE:
1654 ncoals_ = coalitions_get_list(coal_type, coalinfo, ncoals);
1655 break;
1656 default:
1657 panic("memory corruption?!");
1658 }
1659
1660 if (ncoals_ == 0) {
1661 /* all the coalitions disappeared... weird but valid */
1662 error = 0;
1663 *retval = 0;
1664 goto out;
1665 }
1666
1667 /*
1668 * Some coalitions may have disappeared between our initial check,
1669 * and the the actual list acquisition.
1670 * Only copy out what we really need.
1671 */
1672 copyout_sz = k_buffersize;
1673 if (ncoals_ < ncoals) {
1674 copyout_sz = ncoals_ * elem_size;
1675 }
1676
1677 /*
1678 * copy the list up to user space
1679 * (we're guaranteed to have a non-null pointer/size here)
1680 */
1681 error = copyout(coalinfo, buffer,
1682 copyout_sz < buffersize ? copyout_sz : buffersize);
1683
1684 if (error == 0) {
1685 *retval = (int)copyout_sz;
1686 }
1687
1688 out:
1689 if (coalinfo) {
1690 kheap_free(KHEAP_TEMP, coalinfo, k_buffersize);
1691 }
1692
1693 return error;
1694 #else
1695 /* no coalition support */
1696 (void)flavor;
1697 (void)type;
1698 (void)buffer;
1699 (void)buffersize;
1700 (void)retval;
1701 return ENOTSUP;
1702 #endif
1703 }
1704
1705
1706 /*************************** proc_can_use_forgeound_hw **************************/
1707 int
1708 proc_can_use_foreground_hw(int pid, user_addr_t u_reason, uint32_t reasonsize, int32_t *retval)
1709 {
1710 proc_t p = PROC_NULL;
1711 int error = 0;
1712 uint32_t reason = PROC_FGHW_ERROR;
1713 uint32_t isBG = 0;
1714 task_t task = TASK_NULL;
1715 #if CONFIG_COALITIONS
1716 coalition_t coal = COALITION_NULL;
1717 #endif
1718
1719 *retval = 0;
1720
1721 if (pid <= 0) {
1722 error = EINVAL;
1723 reason = PROC_FGHW_ERROR;
1724 goto out;
1725 }
1726
1727 p = proc_find(pid);
1728 if (p == PROC_NULL) {
1729 error = ESRCH;
1730 reason = PROC_FGHW_ERROR;
1731 goto out;
1732 }
1733
1734 #if CONFIG_COALITIONS
1735 if (p != current_proc() &&
1736 !kauth_cred_issuser(kauth_cred_get())) {
1737 error = EPERM;
1738 reason = PROC_FGHW_ERROR;
1739 goto out;
1740 }
1741
1742 task = p->task;
1743 if (coalition_is_leader(task, task_get_coalition(task, COALITION_TYPE_JETSAM))) {
1744 task_reference(task);
1745 } else {
1746 /* current task is not a coalition leader: find the leader */
1747 task = coalition_get_leader(coal);
1748 }
1749
1750 if (task != TASK_NULL) {
1751 /*
1752 * If task is non-null, then it is the coalition leader of the
1753 * current process' coalition. This could be the same task as
1754 * the current_task, and that's OK.
1755 */
1756 uint32_t flags = 0;
1757 int role;
1758
1759 proc_get_darwinbgstate(task, &flags);
1760 if ((flags & PROC_FLAG_APPLICATION) != PROC_FLAG_APPLICATION) {
1761 /*
1762 * Coalition leader is not an application, continue
1763 * searching for other ways this task could gain
1764 * access to HW
1765 */
1766 reason = PROC_FGHW_DAEMON_LEADER;
1767 goto no_leader;
1768 }
1769
1770 if (proc_get_effective_task_policy(task, TASK_POLICY_DARWIN_BG)) {
1771 /*
1772 * If the leader of the current process' coalition has
1773 * been marked as DARWIN_BG, then it definitely should
1774 * not be using foreground hardware resources.
1775 */
1776 reason = PROC_FGHW_LEADER_BACKGROUND;
1777 goto out;
1778 }
1779
1780 role = proc_get_effective_task_policy(task, TASK_POLICY_ROLE);
1781 switch (role) {
1782 case TASK_FOREGROUND_APPLICATION: /* DARWIN_ROLE_UI_FOCAL */
1783 case TASK_BACKGROUND_APPLICATION: /* DARWIN_ROLE_UI */
1784 /*
1785 * The leader of this coalition is a focal, UI app:
1786 * access granted
1787 * TODO: should extensions/plugins be allowed to use
1788 * this hardware?
1789 */
1790 *retval = 1;
1791 reason = PROC_FGHW_OK;
1792 goto out;
1793 case TASK_DEFAULT_APPLICATION: /* DARWIN_ROLE_UI_NON_FOCAL */
1794 case TASK_NONUI_APPLICATION: /* DARWIN_ROLE_NON_UI */
1795 case TASK_THROTTLE_APPLICATION:
1796 case TASK_UNSPECIFIED:
1797 default:
1798 /* non-focal, non-ui apps don't get access */
1799 reason = PROC_FGHW_LEADER_NONUI;
1800 goto out;
1801 }
1802 }
1803
1804 no_leader:
1805 if (task != TASK_NULL) {
1806 task_deallocate(task);
1807 task = TASK_NULL;
1808 }
1809 #endif /* CONFIG_COALITIONS */
1810
1811 /*
1812 * There is no reasonable semantic to investigate the currently
1813 * adopted voucher of an arbitrary thread in a non-current process.
1814 * We return '0'
1815 */
1816 if (p != current_proc()) {
1817 error = EINVAL;
1818 goto out;
1819 }
1820
1821 /*
1822 * In the absence of coalitions, fall back to a voucher-based lookup
1823 * where a daemon can used foreground HW if it's operating on behalf
1824 * of a foreground application.
1825 * NOTE: this is equivalent to a call to
1826 * proc_pidoriginatorinfo(PROC_PIDORIGINATOR_BGSTATE, &isBG, sizeof(isBG))
1827 */
1828 isBG = 1;
1829 error = proc_get_originatorbgstate(&isBG);
1830 switch (error) {
1831 case 0:
1832 break;
1833 case ESRCH:
1834 reason = PROC_FGHW_NO_ORIGINATOR;
1835 error = 0;
1836 goto out;
1837 case ENOATTR:
1838 reason = PROC_FGHW_NO_VOUCHER_ATTR;
1839 error = 0;
1840 goto out;
1841 case EINVAL:
1842 reason = PROC_FGHW_DAEMON_NO_VOUCHER;
1843 error = 0;
1844 goto out;
1845 default:
1846 /* some other error occurred: report that to the caller */
1847 reason = PROC_FGHW_VOUCHER_ERROR;
1848 goto out;
1849 }
1850
1851 if (isBG) {
1852 reason = PROC_FGHW_ORIGINATOR_BACKGROUND;
1853 error = 0;
1854 } else {
1855 /*
1856 * The process itself is either a foreground app, or has
1857 * adopted a voucher originating from an app that's still in
1858 * the foreground
1859 */
1860 reason = PROC_FGHW_DAEMON_OK;
1861 *retval = 1;
1862 }
1863
1864 out:
1865 if (task != TASK_NULL) {
1866 task_deallocate(task);
1867 }
1868 if (p != PROC_NULL) {
1869 proc_rele(p);
1870 }
1871 if (reasonsize >= sizeof(reason) && u_reason != (user_addr_t)0) {
1872 (void)copyout(&reason, u_reason, sizeof(reason));
1873 }
1874 return error;
1875 }
1876
1877
1878 /********************************** proc_pidinfo ********************************/
1879
1880
1881 int
1882 proc_pidinfo(int pid, uint32_t flags, uint64_t ext_id, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
1883 {
1884 struct proc * p = PROC_NULL;
1885 int error = ENOTSUP;
1886 int gotref = 0;
1887 int findzomb = 0;
1888 int shortversion = 0;
1889 uint32_t size;
1890 int zombie = 0;
1891 bool thuniqueid = false;
1892 int uniqidversion = 0;
1893 bool check_same_user;
1894
1895 switch (flavor) {
1896 case PROC_PIDLISTFDS:
1897 size = PROC_PIDLISTFD_SIZE;
1898 if (buffer == USER_ADDR_NULL) {
1899 size = 0;
1900 }
1901 break;
1902 case PROC_PIDTBSDINFO:
1903 size = PROC_PIDTBSDINFO_SIZE;
1904 break;
1905 case PROC_PIDTASKINFO:
1906 size = PROC_PIDTASKINFO_SIZE;
1907 break;
1908 case PROC_PIDTASKALLINFO:
1909 size = PROC_PIDTASKALLINFO_SIZE;
1910 break;
1911 case PROC_PIDTHREADINFO:
1912 size = PROC_PIDTHREADINFO_SIZE;
1913 break;
1914 case PROC_PIDLISTTHREADIDS:
1915 size = PROC_PIDLISTTHREADIDS_SIZE;
1916 break;
1917 case PROC_PIDLISTTHREADS:
1918 size = PROC_PIDLISTTHREADS_SIZE;
1919 break;
1920 case PROC_PIDREGIONINFO:
1921 size = PROC_PIDREGIONINFO_SIZE;
1922 break;
1923 case PROC_PIDREGIONPATHINFO:
1924 size = PROC_PIDREGIONPATHINFO_SIZE;
1925 break;
1926 case PROC_PIDVNODEPATHINFO:
1927 size = PROC_PIDVNODEPATHINFO_SIZE;
1928 break;
1929 case PROC_PIDTHREADPATHINFO:
1930 size = PROC_PIDTHREADPATHINFO_SIZE;
1931 break;
1932 case PROC_PIDPATHINFO:
1933 size = MAXPATHLEN;
1934 break;
1935 case PROC_PIDWORKQUEUEINFO:
1936 /* kernel does not have workq info */
1937 if (pid == 0) {
1938 return EINVAL;
1939 } else {
1940 size = PROC_PIDWORKQUEUEINFO_SIZE;
1941 }
1942 break;
1943 case PROC_PIDT_SHORTBSDINFO:
1944 size = PROC_PIDT_SHORTBSDINFO_SIZE;
1945 break;
1946 case PROC_PIDLISTFILEPORTS:
1947 size = PROC_PIDLISTFILEPORTS_SIZE;
1948 if (buffer == (user_addr_t)0) {
1949 size = 0;
1950 }
1951 break;
1952 case PROC_PIDTHREADID64INFO:
1953 size = PROC_PIDTHREADID64INFO_SIZE;
1954 break;
1955 case PROC_PIDUNIQIDENTIFIERINFO:
1956 size = PROC_PIDUNIQIDENTIFIERINFO_SIZE;
1957 break;
1958 case PROC_PIDT_BSDINFOWITHUNIQID:
1959 size = PROC_PIDT_BSDINFOWITHUNIQID_SIZE;
1960 break;
1961 case PROC_PIDARCHINFO:
1962 size = PROC_PIDARCHINFO_SIZE;
1963 break;
1964 case PROC_PIDCOALITIONINFO:
1965 size = PROC_PIDCOALITIONINFO_SIZE;
1966 break;
1967 case PROC_PIDNOTEEXIT:
1968 /*
1969 * Set findzomb explicitly because arg passed
1970 * in is used as note exit status bits.
1971 */
1972 size = PROC_PIDNOTEEXIT_SIZE;
1973 findzomb = 1;
1974 break;
1975 case PROC_PIDEXITREASONINFO:
1976 size = PROC_PIDEXITREASONINFO_SIZE;
1977 findzomb = 1;
1978 break;
1979 case PROC_PIDEXITREASONBASICINFO:
1980 size = PROC_PIDEXITREASONBASICINFOSIZE;
1981 findzomb = 1;
1982 break;
1983 case PROC_PIDREGIONPATHINFO2:
1984 size = PROC_PIDREGIONPATHINFO2_SIZE;
1985 break;
1986 case PROC_PIDREGIONPATHINFO3:
1987 size = PROC_PIDREGIONPATHINFO3_SIZE;
1988 break;
1989 case PROC_PIDLISTUPTRS:
1990 size = PROC_PIDLISTUPTRS_SIZE;
1991 if (buffer == USER_ADDR_NULL) {
1992 size = 0;
1993 }
1994 break;
1995 case PROC_PIDLISTDYNKQUEUES:
1996 size = PROC_PIDLISTDYNKQUEUES_SIZE;
1997 if (buffer == USER_ADDR_NULL) {
1998 size = 0;
1999 }
2000 break;
2001 case PROC_PIDVMRTFAULTINFO:
2002 size = sizeof(vm_rtfault_record_t);
2003 if (buffer == USER_ADDR_NULL) {
2004 size = 0;
2005 }
2006 break;
2007 case PROC_PIDPLATFORMINFO:
2008 size = PROC_PIDPLATFORMINFO_SIZE;
2009 findzomb = 1;
2010 break;
2011 case PROC_PIDREGIONPATH:
2012 size = PROC_PIDREGIONPATH_SIZE;
2013 break;
2014 case PROC_PIDIPCTABLEINFO:
2015 size = PROC_PIDIPCTABLEINFO_SIZE;
2016 break;
2017 default:
2018 return EINVAL;
2019 }
2020
2021 if (buffersize < size) {
2022 return ENOMEM;
2023 }
2024
2025 if ((flavor == PROC_PIDPATHINFO) && (buffersize > PROC_PIDPATHINFO_MAXSIZE)) {
2026 return EOVERFLOW;
2027 }
2028
2029 /* Check if we need to look for zombies */
2030 if ((flavor == PROC_PIDTBSDINFO) || (flavor == PROC_PIDT_SHORTBSDINFO) || (flavor == PROC_PIDT_BSDINFOWITHUNIQID)
2031 || (flavor == PROC_PIDUNIQIDENTIFIERINFO)) {
2032 if (arg) {
2033 findzomb = 1;
2034 }
2035 }
2036
2037 if ((p = proc_find(pid)) == PROC_NULL) {
2038 if (findzomb) {
2039 p = proc_find_zombref(pid);
2040 }
2041 if (p == PROC_NULL) {
2042 error = ESRCH;
2043 goto out;
2044 }
2045 zombie = 1;
2046 } else {
2047 gotref = 1;
2048 }
2049
2050 if ((flags & PIF_COMPARE_IDVERSION) && (ext_id != p->p_idversion)) {
2051 error = ESRCH;
2052 goto out;
2053 }
2054 if ((flags & PIF_COMPARE_UNIQUEID) && (ext_id != p->p_uniqueid)) {
2055 error = ESRCH;
2056 goto out;
2057 }
2058
2059 /* Certain operations don't require privileges */
2060 switch (flavor) {
2061 case PROC_PIDT_SHORTBSDINFO:
2062 case PROC_PIDUNIQIDENTIFIERINFO:
2063 case PROC_PIDPATHINFO:
2064 case PROC_PIDCOALITIONINFO:
2065 case PROC_PIDPLATFORMINFO:
2066 check_same_user = NO_CHECK_SAME_USER;
2067 break;
2068 default:
2069 check_same_user = CHECK_SAME_USER;
2070 break;
2071 }
2072
2073 /* Do we have permission to look into this? */
2074 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDINFO, flavor, check_same_user))) {
2075 goto out;
2076 }
2077
2078 switch (flavor) {
2079 case PROC_PIDLISTFDS: {
2080 error = proc_pidfdlist(p, buffer, buffersize, retval);
2081 }
2082 break;
2083
2084 case PROC_PIDUNIQIDENTIFIERINFO: {
2085 struct proc_uniqidentifierinfo p_uniqidinfo;
2086 bzero(&p_uniqidinfo, sizeof(p_uniqidinfo));
2087 proc_piduniqidentifierinfo(p, &p_uniqidinfo);
2088 error = copyout(&p_uniqidinfo, buffer, sizeof(struct proc_uniqidentifierinfo));
2089 if (error == 0) {
2090 *retval = sizeof(struct proc_uniqidentifierinfo);
2091 }
2092 }
2093 break;
2094
2095 case PROC_PIDT_SHORTBSDINFO:
2096 shortversion = 1;
2097 OS_FALLTHROUGH;
2098 case PROC_PIDT_BSDINFOWITHUNIQID:
2099 case PROC_PIDTBSDINFO: {
2100 struct proc_bsdinfo pbsd;
2101 struct proc_bsdshortinfo pbsd_short;
2102 struct proc_bsdinfowithuniqid pbsd_uniqid;
2103
2104 if (flavor == PROC_PIDT_BSDINFOWITHUNIQID) {
2105 uniqidversion = 1;
2106 }
2107
2108 if (shortversion != 0) {
2109 error = proc_pidshortbsdinfo(p, &pbsd_short, zombie);
2110 } else {
2111 error = proc_pidbsdinfo(p, &pbsd, zombie);
2112 if (uniqidversion != 0) {
2113 bzero(&pbsd_uniqid, sizeof(pbsd_uniqid));
2114 proc_piduniqidentifierinfo(p, &pbsd_uniqid.p_uniqidentifier);
2115 pbsd_uniqid.pbsd = pbsd;
2116 }
2117 }
2118
2119 if (error == 0) {
2120 if (shortversion != 0) {
2121 error = copyout(&pbsd_short, buffer, sizeof(struct proc_bsdshortinfo));
2122 if (error == 0) {
2123 *retval = sizeof(struct proc_bsdshortinfo);
2124 }
2125 } else if (uniqidversion != 0) {
2126 error = copyout(&pbsd_uniqid, buffer, sizeof(struct proc_bsdinfowithuniqid));
2127 if (error == 0) {
2128 *retval = sizeof(struct proc_bsdinfowithuniqid);
2129 }
2130 } else {
2131 error = copyout(&pbsd, buffer, sizeof(struct proc_bsdinfo));
2132 if (error == 0) {
2133 *retval = sizeof(struct proc_bsdinfo);
2134 }
2135 }
2136 }
2137 }
2138 break;
2139
2140 case PROC_PIDTASKINFO: {
2141 struct proc_taskinfo ptinfo;
2142
2143 error = proc_pidtaskinfo(p, &ptinfo);
2144 if (error == 0) {
2145 error = copyout(&ptinfo, buffer, sizeof(struct proc_taskinfo));
2146 if (error == 0) {
2147 *retval = sizeof(struct proc_taskinfo);
2148 }
2149 }
2150 }
2151 break;
2152
2153 case PROC_PIDTASKALLINFO: {
2154 struct proc_taskallinfo pall;
2155 bzero(&pall, sizeof(pall));
2156 error = proc_pidbsdinfo(p, &pall.pbsd, 0);
2157 error = proc_pidtaskinfo(p, &pall.ptinfo);
2158 if (error == 0) {
2159 error = copyout(&pall, buffer, sizeof(struct proc_taskallinfo));
2160 if (error == 0) {
2161 *retval = sizeof(struct proc_taskallinfo);
2162 }
2163 }
2164 }
2165 break;
2166
2167 case PROC_PIDTHREADID64INFO:
2168 thuniqueid = true;
2169 OS_FALLTHROUGH;
2170 case PROC_PIDTHREADINFO:{
2171 struct proc_threadinfo pthinfo;
2172
2173 error = proc_pidthreadinfo(p, arg, thuniqueid, &pthinfo);
2174 if (error == 0) {
2175 error = copyout(&pthinfo, buffer, sizeof(struct proc_threadinfo));
2176 if (error == 0) {
2177 *retval = sizeof(struct proc_threadinfo);
2178 }
2179 }
2180 }
2181 break;
2182
2183 case PROC_PIDLISTTHREADIDS:
2184 thuniqueid = true;
2185 OS_FALLTHROUGH;
2186 case PROC_PIDLISTTHREADS:{
2187 error = proc_pidlistthreads(p, thuniqueid, buffer, buffersize, retval);
2188 }
2189 break;
2190
2191 case PROC_PIDREGIONINFO:{
2192 error = proc_pidregioninfo(p, arg, buffer, buffersize, retval);
2193 }
2194 break;
2195
2196
2197 case PROC_PIDREGIONPATHINFO:{
2198 error = proc_pidregionpathinfo(p, arg, buffer, buffersize, retval);
2199 }
2200 break;
2201
2202 case PROC_PIDREGIONPATHINFO2:{
2203 error = proc_pidregionpathinfo2(p, arg, buffer, buffersize, retval);
2204 }
2205 break;
2206
2207 case PROC_PIDREGIONPATHINFO3:{
2208 error = proc_pidregionpathinfo3(p, arg, buffer, buffersize, retval);
2209 }
2210 break;
2211
2212 case PROC_PIDVNODEPATHINFO:{
2213 error = proc_pidvnodepathinfo(p, arg, buffer, buffersize, retval);
2214 }
2215 break;
2216
2217
2218 case PROC_PIDTHREADPATHINFO:{
2219 struct proc_threadwithpathinfo pinfo;
2220
2221 error = proc_pidthreadpathinfo(p, arg, &pinfo);
2222 if (error == 0) {
2223 error = copyout((caddr_t)&pinfo, buffer, sizeof(struct proc_threadwithpathinfo));
2224 if (error == 0) {
2225 *retval = sizeof(struct proc_threadwithpathinfo);
2226 }
2227 }
2228 }
2229 break;
2230
2231 case PROC_PIDPATHINFO: {
2232 error = proc_pidpathinfo(p, arg, buffer, buffersize, retval);
2233 }
2234 break;
2235
2236
2237 case PROC_PIDWORKQUEUEINFO:{
2238 struct proc_workqueueinfo pwqinfo;
2239
2240 error = proc_pidworkqueueinfo(p, &pwqinfo);
2241 if (error == 0) {
2242 error = copyout(&pwqinfo, buffer, sizeof(struct proc_workqueueinfo));
2243 if (error == 0) {
2244 *retval = sizeof(struct proc_workqueueinfo);
2245 }
2246 }
2247 }
2248 break;
2249
2250 case PROC_PIDLISTFILEPORTS: {
2251 error = proc_pidfileportlist(p, buffer, buffersize, retval);
2252 }
2253 break;
2254
2255 case PROC_PIDARCHINFO: {
2256 struct proc_archinfo pai;
2257 bzero(&pai, sizeof(pai));
2258 proc_archinfo(p, &pai);
2259 error = copyout(&pai, buffer, sizeof(struct proc_archinfo));
2260 if (error == 0) {
2261 *retval = sizeof(struct proc_archinfo);
2262 }
2263 }
2264 break;
2265
2266 case PROC_PIDCOALITIONINFO: {
2267 struct proc_pidcoalitioninfo pci;
2268 proc_pidcoalitioninfo(p, &pci);
2269 error = copyout(&pci, buffer, sizeof(struct proc_pidcoalitioninfo));
2270 if (error == 0) {
2271 *retval = sizeof(struct proc_pidcoalitioninfo);
2272 }
2273 }
2274 break;
2275
2276 case PROC_PIDNOTEEXIT: {
2277 uint32_t data;
2278 error = proc_pidnoteexit(p, arg, &data);
2279 if (error == 0) {
2280 error = copyout(&data, buffer, sizeof(data));
2281 if (error == 0) {
2282 *retval = sizeof(data);
2283 }
2284 }
2285 }
2286 break;
2287
2288 case PROC_PIDEXITREASONINFO: {
2289 struct proc_exitreasoninfo eri;
2290
2291 error = copyin(buffer, &eri, sizeof(eri));
2292 if (error != 0) {
2293 break;
2294 }
2295
2296 error = proc_pidexitreasoninfo(p, &eri, NULL);
2297 if (error == 0) {
2298 error = copyout(&eri, buffer, sizeof(eri));
2299 if (error == 0) {
2300 *retval = sizeof(eri);
2301 }
2302 }
2303 }
2304 break;
2305
2306 case PROC_PIDEXITREASONBASICINFO: {
2307 struct proc_exitreasonbasicinfo beri;
2308
2309 bzero(&beri, sizeof(struct proc_exitreasonbasicinfo));
2310
2311 error = proc_pidexitreasoninfo(p, NULL, &beri);
2312 if (error == 0) {
2313 error = copyout(&beri, buffer, sizeof(beri));
2314 if (error == 0) {
2315 *retval = sizeof(beri);
2316 }
2317 }
2318 }
2319 break;
2320
2321 case PROC_PIDLISTUPTRS:
2322 error = proc_pidlistuptrs(p, buffer, buffersize, retval);
2323 break;
2324
2325 case PROC_PIDLISTDYNKQUEUES:
2326 error = kevent_copyout_proc_dynkqids(p, buffer, buffersize, retval);
2327 break;
2328 case PROC_PIDVMRTFAULTINFO: {
2329 /* This interface can only be employed on the current
2330 * process. We will eventually enforce an entitlement.
2331 */
2332 *retval = 0;
2333
2334 if (p != current_proc()) {
2335 error = EINVAL;
2336 break;
2337 }
2338
2339 size_t kbufsz = MIN(buffersize, vmrtfaultinfo_bufsz());
2340 void *vmrtfbuf = kheap_alloc(KHEAP_TEMP, kbufsz, Z_WAITOK | Z_ZERO);
2341
2342 if (vmrtfbuf == NULL) {
2343 error = ENOMEM;
2344 break;
2345 }
2346
2347 uint64_t effpid = get_current_unique_pid();
2348 /* The VM may choose to provide more comprehensive records
2349 * for root-privileged users on internal configurations.
2350 */
2351 boolean_t isroot = (suser(kauth_cred_get(), (u_short *)0) == 0);
2352 size_t num_extracted = 0;
2353 int vmf_residue = vmrtf_extract(effpid, isroot, kbufsz, vmrtfbuf, &num_extracted);
2354 size_t vmfsz = num_extracted * sizeof(vm_rtfault_record_t);
2355
2356 *retval = (int32_t)MIN(num_extracted, INT32_MAX);
2357
2358 error = 0;
2359 if (vmfsz) {
2360 error = copyout(vmrtfbuf, buffer, vmfsz);
2361 }
2362
2363 if (error == 0) {
2364 if (vmf_residue) {
2365 error = ENOMEM;
2366 }
2367 }
2368 kheap_free(KHEAP_TEMP, vmrtfbuf, kbufsz);
2369 }
2370 break;
2371 case PROC_PIDPLATFORMINFO: {
2372 proc_lock(p);
2373 uint32_t platform = p->p_platform;
2374 proc_unlock(p);
2375 error = copyout(&platform, buffer, sizeof(uint32_t));
2376 if (error == 0) {
2377 *retval = sizeof(uint32_t);
2378 }
2379 } break;
2380 case PROC_PIDREGIONPATH: {
2381 error = proc_pidregionpath(p, arg, buffer, buffersize, retval);
2382 }
2383 break;
2384 case PROC_PIDIPCTABLEINFO: {
2385 struct proc_ipctableinfo table_info;
2386
2387 error = proc_pidipctableinfo(p, &table_info);
2388 if (error == 0) {
2389 error = copyout(&table_info, buffer, sizeof(struct proc_ipctableinfo));
2390 if (error == 0) {
2391 *retval = sizeof(struct proc_ipctableinfo);
2392 }
2393 }
2394 }
2395 break;
2396 default:
2397 error = ENOTSUP;
2398 break;
2399 }
2400
2401 out:
2402 if (gotref) {
2403 proc_rele(p);
2404 } else if (zombie) {
2405 proc_drop_zombref(p);
2406 }
2407 return error;
2408 }
2409
2410
2411 int
2412 pid_vnodeinfo(vnode_t vp, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2413 {
2414 struct vnode_fdinfo vfi;
2415 uint32_t vid = vnode_vid(vp);
2416 int error = 0;
2417
2418 if ((error = vnode_getwithvid(vp, vid)) != 0) {
2419 return error;
2420 }
2421 bzero(&vfi, sizeof(struct vnode_fdinfo));
2422 fill_fileinfo(fp, proc, fd, &vfi.pfi);
2423 error = fill_vnodeinfo(vp, &vfi.pvi, FALSE);
2424 vnode_put(vp);
2425 if (error == 0) {
2426 error = copyout((caddr_t)&vfi, buffer, sizeof(struct vnode_fdinfo));
2427 if (error == 0) {
2428 *retval = sizeof(struct vnode_fdinfo);
2429 }
2430 }
2431 return error;
2432 }
2433
2434 int
2435 pid_vnodeinfopath(vnode_t vp, struct fileproc * fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2436 {
2437 struct vnode_fdinfowithpath vfip;
2438 uint32_t vid = vnode_vid(vp);
2439 int count, error = 0;
2440
2441 if ((error = vnode_getwithvid(vp, vid)) != 0) {
2442 return error;
2443 }
2444 bzero(&vfip, sizeof(struct vnode_fdinfowithpath));
2445 fill_fileinfo(fp, proc, fd, &vfip.pfi);
2446 error = fill_vnodeinfo(vp, &vfip.pvip.vip_vi, TRUE);
2447 if (error == 0) {
2448 count = MAXPATHLEN;
2449 vn_getpath(vp, &vfip.pvip.vip_path[0], &count);
2450 vfip.pvip.vip_path[MAXPATHLEN - 1] = 0;
2451 vnode_put(vp);
2452 error = copyout((caddr_t)&vfip, buffer, sizeof(struct vnode_fdinfowithpath));
2453 if (error == 0) {
2454 *retval = sizeof(struct vnode_fdinfowithpath);
2455 }
2456 } else {
2457 vnode_put(vp);
2458 }
2459 return error;
2460 }
2461
2462 void
2463 fill_fileinfo(struct fileproc * fp, proc_t proc, int fd, struct proc_fileinfo * fproc)
2464 {
2465 fproc->fi_openflags = fp->fp_glob->fg_flag;
2466 fproc->fi_status = 0;
2467 fproc->fi_offset = fp->fp_glob->fg_offset;
2468 fproc->fi_type = FILEGLOB_DTYPE(fp->fp_glob);
2469 if (os_ref_get_count_raw(&fp->fp_glob->fg_count) > 1) {
2470 fproc->fi_status |= PROC_FP_SHARED;
2471 }
2472 if (proc != PROC_NULL) {
2473 if ((FDFLAGS_GET(proc, fd) & UF_EXCLOSE) != 0) {
2474 fproc->fi_status |= PROC_FP_CLEXEC;
2475 }
2476 if ((FDFLAGS_GET(proc, fd) & UF_FORKCLOSE) != 0) {
2477 fproc->fi_status |= PROC_FP_CLFORK;
2478 }
2479 }
2480 if (FILEPROC_TYPE(fp) == FTYPE_GUARDED) {
2481 fproc->fi_status |= PROC_FP_GUARDED;
2482 fproc->fi_guardflags = 0;
2483 if (fp_isguarded(fp, GUARD_CLOSE)) {
2484 fproc->fi_guardflags |= PROC_FI_GUARD_CLOSE;
2485 }
2486 if (fp_isguarded(fp, GUARD_DUP)) {
2487 fproc->fi_guardflags |= PROC_FI_GUARD_DUP;
2488 }
2489 if (fp_isguarded(fp, GUARD_SOCKET_IPC)) {
2490 fproc->fi_guardflags |= PROC_FI_GUARD_SOCKET_IPC;
2491 }
2492 if (fp_isguarded(fp, GUARD_FILEPORT)) {
2493 fproc->fi_guardflags |= PROC_FI_GUARD_FILEPORT;
2494 }
2495 }
2496 }
2497
2498
2499
2500 int
2501 fill_vnodeinfo(vnode_t vp, struct vnode_info *vinfo, __unused boolean_t check_fsgetpath)
2502 {
2503 vfs_context_t context;
2504 struct stat64 sb;
2505 int error = 0;
2506
2507 bzero(&sb, sizeof(struct stat64));
2508 context = vfs_context_create((vfs_context_t)0);
2509 #if CONFIG_MACF
2510 /* Called when vnode info is used by the caller to get vnode's path */
2511 if (check_fsgetpath) {
2512 error = mac_vnode_check_fsgetpath(context, vp);
2513 }
2514 #endif
2515 if (!error) {
2516 error = vn_stat(vp, &sb, NULL, 1, 0, context);
2517 munge_vinfo_stat(&sb, &vinfo->vi_stat);
2518 }
2519 (void)vfs_context_rele(context);
2520 if (error != 0) {
2521 goto out;
2522 }
2523
2524 if (vp->v_mount != dead_mountp) {
2525 vinfo->vi_fsid = vp->v_mount->mnt_vfsstat.f_fsid;
2526 } else {
2527 vinfo->vi_fsid.val[0] = 0;
2528 vinfo->vi_fsid.val[1] = 0;
2529 }
2530 vinfo->vi_type = vp->v_type;
2531 out:
2532 return error;
2533 }
2534
2535 int
2536 pid_socketinfo(socket_t so, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2537 {
2538 #if SOCKETS
2539 struct socket_fdinfo s;
2540 int error = 0;
2541
2542 bzero(&s, sizeof(struct socket_fdinfo));
2543 fill_fileinfo(fp, proc, fd, &s.pfi);
2544 if ((error = fill_socketinfo(so, &s.psi)) == 0) {
2545 if ((error = copyout(&s, buffer, sizeof(struct socket_fdinfo))) == 0) {
2546 *retval = sizeof(struct socket_fdinfo);
2547 }
2548 }
2549 return error;
2550 #else
2551 #pragma unused(so, fp, proc, fd, buffer)
2552 *retval = 0;
2553 return ENOTSUP;
2554 #endif
2555 }
2556
2557 int
2558 pid_pseminfo(struct psemnode *psem, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2559 {
2560 struct psem_fdinfo pseminfo;
2561 int error = 0;
2562
2563 bzero(&pseminfo, sizeof(struct psem_fdinfo));
2564 fill_fileinfo(fp, proc, fd, &pseminfo.pfi);
2565
2566 if ((error = fill_pseminfo(psem, &pseminfo.pseminfo)) == 0) {
2567 if ((error = copyout(&pseminfo, buffer, sizeof(struct psem_fdinfo))) == 0) {
2568 *retval = sizeof(struct psem_fdinfo);
2569 }
2570 }
2571
2572 return error;
2573 }
2574
2575 int
2576 pid_pshminfo(struct pshmnode *pshm, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2577 {
2578 struct pshm_fdinfo pshminfo;
2579 int error = 0;
2580
2581 bzero(&pshminfo, sizeof(struct pshm_fdinfo));
2582 fill_fileinfo(fp, proc, fd, &pshminfo.pfi);
2583
2584 if ((error = fill_pshminfo(pshm, &pshminfo.pshminfo)) == 0) {
2585 if ((error = copyout(&pshminfo, buffer, sizeof(struct pshm_fdinfo))) == 0) {
2586 *retval = sizeof(struct pshm_fdinfo);
2587 }
2588 }
2589
2590 return error;
2591 }
2592
2593 int
2594 pid_pipeinfo(struct pipe * p, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2595 {
2596 struct pipe_fdinfo pipeinfo;
2597 int error = 0;
2598
2599 bzero(&pipeinfo, sizeof(struct pipe_fdinfo));
2600 fill_fileinfo(fp, proc, fd, &pipeinfo.pfi);
2601 if ((error = fill_pipeinfo(p, &pipeinfo.pipeinfo)) == 0) {
2602 if ((error = copyout(&pipeinfo, buffer, sizeof(struct pipe_fdinfo))) == 0) {
2603 *retval = sizeof(struct pipe_fdinfo);
2604 }
2605 }
2606
2607 return error;
2608 }
2609
2610 int
2611 pid_kqueueinfo(struct kqueue * kq, struct fileproc *fp, proc_t proc, int fd, user_addr_t buffer, __unused uint32_t buffersize, int32_t * retval)
2612 {
2613 struct kqueue_fdinfo kqinfo;
2614 int error = 0;
2615
2616 bzero(&kqinfo, sizeof(struct kqueue_fdinfo));
2617
2618 /* not all kq's are associated with a file (e.g. workqkq) */
2619 if (fp) {
2620 assert(fd >= 0);
2621 fill_fileinfo(fp, proc, fd, &kqinfo.pfi);
2622 }
2623
2624 if ((error = fill_kqueueinfo(kq, &kqinfo.kqueueinfo)) == 0) {
2625 if ((error = copyout(&kqinfo, buffer, sizeof(struct kqueue_fdinfo))) == 0) {
2626 *retval = sizeof(struct kqueue_fdinfo);
2627 }
2628 }
2629
2630 return error;
2631 }
2632
2633
2634 /************************** proc_pidfdinfo routine ***************************/
2635 int
2636 proc_pidfdinfo(int pid, int flavor, int fd, user_addr_t buffer, uint32_t buffersize, int32_t * retval)
2637 {
2638 proc_t p;
2639 int error = ENOTSUP;
2640 struct fileproc *fp = NULL;
2641 uint32_t size;
2642
2643 switch (flavor) {
2644 case PROC_PIDFDVNODEINFO:
2645 size = PROC_PIDFDVNODEINFO_SIZE;
2646 break;
2647 case PROC_PIDFDVNODEPATHINFO:
2648 size = PROC_PIDFDVNODEPATHINFO_SIZE;
2649 break;
2650 case PROC_PIDFDSOCKETINFO:
2651 size = PROC_PIDFDSOCKETINFO_SIZE;
2652 break;
2653 case PROC_PIDFDPSEMINFO:
2654 size = PROC_PIDFDPSEMINFO_SIZE;
2655 break;
2656 case PROC_PIDFDPSHMINFO:
2657 size = PROC_PIDFDPSHMINFO_SIZE;
2658 break;
2659 case PROC_PIDFDPIPEINFO:
2660 size = PROC_PIDFDPIPEINFO_SIZE;
2661 break;
2662 case PROC_PIDFDKQUEUEINFO:
2663 size = PROC_PIDFDKQUEUEINFO_SIZE;
2664 break;
2665 case PROC_PIDFDKQUEUE_EXTINFO:
2666 size = PROC_PIDFDKQUEUE_EXTINFO_SIZE;
2667 if (buffer == (user_addr_t)0) {
2668 size = 0;
2669 }
2670 break;
2671 case PROC_PIDFDATALKINFO:
2672 size = PROC_PIDFDATALKINFO_SIZE;
2673 break;
2674
2675 default:
2676 return EINVAL;
2677 }
2678
2679 if (buffersize < size) {
2680 return ENOMEM;
2681 }
2682
2683 if ((p = proc_find(pid)) == PROC_NULL) {
2684 error = ESRCH;
2685 goto out;
2686 }
2687
2688 /* Do we have permission to look into this? */
2689 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDFDINFO, flavor, CHECK_SAME_USER))) {
2690 goto out1;
2691 }
2692
2693 switch (flavor) {
2694 case PROC_PIDFDVNODEINFO: {
2695 if ((error = fp_get_ftype(p, fd, DTYPE_VNODE, EBADF, &fp)) != 0) {
2696 goto out1;
2697 }
2698 error = pid_vnodeinfo(fp->fp_glob->fg_data, fp, p, fd, buffer, buffersize, retval);
2699 }
2700 break;
2701
2702 case PROC_PIDFDVNODEPATHINFO: {
2703 if ((error = fp_get_ftype(p, fd, DTYPE_VNODE, EBADF, &fp)) != 0) {
2704 goto out1;
2705 }
2706 error = pid_vnodeinfopath(fp->fp_glob->fg_data, fp, p, fd, buffer, buffersize, retval);
2707 }
2708 break;
2709
2710 case PROC_PIDFDSOCKETINFO: {
2711 if ((error = fp_get_ftype(p, fd, DTYPE_SOCKET, ENOTSOCK, &fp)) != 0) {
2712 goto out1;
2713 }
2714 error = pid_socketinfo(fp->fp_glob->fg_data, fp, p, fd, buffer, buffersize, retval);
2715 }
2716 break;
2717
2718 case PROC_PIDFDPSEMINFO: {
2719 if ((error = fp_get_ftype(p, fd, DTYPE_PSXSHM, EBADF, &fp)) != 0) {
2720 goto out1;
2721 }
2722 error = pid_pseminfo(fp->fp_glob->fg_data, fp, p, fd, buffer, buffersize, retval);
2723 }
2724 break;
2725
2726 case PROC_PIDFDPSHMINFO: {
2727 if ((error = fp_get_ftype(p, fd, DTYPE_PSXSHM, EBADF, &fp)) != 0) {
2728 goto out1;
2729 }
2730 error = pid_pshminfo(fp->fp_glob->fg_data, fp, p, fd, buffer, buffersize, retval);
2731 }
2732 break;
2733
2734 case PROC_PIDFDPIPEINFO: {
2735 if ((error = fp_get_ftype(p, fd, DTYPE_PIPE, EBADF, &fp)) != 0) {
2736 goto out1;
2737 }
2738 error = pid_pipeinfo(fp->fp_glob->fg_data, fp, p, fd, buffer, buffersize, retval);
2739 }
2740 break;
2741
2742 case PROC_PIDFDKQUEUEINFO: {
2743 kqueue_t kqu;
2744
2745 if (fd == -1) {
2746 if ((kqu.kqwq = p->p_fd->fd_wqkqueue) == NULL) {
2747 /* wqkqueue is initialized on-demand */
2748 error = 0;
2749 break;
2750 }
2751 } else if ((error = fp_get_ftype(p, fd, DTYPE_KQUEUE, EBADF, &fp)) != 0) {
2752 goto out1;
2753 } else {
2754 kqu.kq = fp->fp_glob->fg_data;
2755 }
2756
2757 error = pid_kqueueinfo(kqu.kq, fp, p, fd, buffer, buffersize, retval);
2758 }
2759 break;
2760
2761 case PROC_PIDFDKQUEUE_EXTINFO: {
2762 kqueue_t kqu;
2763
2764 if (fd == -1) {
2765 if ((kqu.kqwq = p->p_fd->fd_wqkqueue) == NULL) {
2766 /* wqkqueue is initialized on-demand */
2767 error = 0;
2768 break;
2769 }
2770 } else if ((error = fp_get_ftype(p, fd, DTYPE_KQUEUE, EBADF, &fp)) != 0) {
2771 goto out1;
2772 } else {
2773 kqu.kq = fp->fp_glob->fg_data;
2774 }
2775 error = pid_kqueue_extinfo(p, kqu.kq, buffer, buffersize, retval);
2776 }
2777 break;
2778
2779 default: {
2780 error = EINVAL;
2781 goto out1;
2782 }
2783 }
2784
2785 if (fp) {
2786 fp_drop(p, fd, fp, 0);
2787 }
2788 out1:
2789 proc_rele(p);
2790 out:
2791 return error;
2792 }
2793
2794 #define MAX_UPTRS 16392
2795
2796 int
2797 proc_pidlistuptrs(proc_t p, user_addr_t buffer, uint32_t buffersize, int32_t *retval)
2798 {
2799 uint32_t count = 0;
2800 int error = 0;
2801 void *kbuf = NULL;
2802 int32_t nuptrs = 0;
2803
2804 if (buffer == USER_ADDR_NULL || buffersize < sizeof(uint64_t)) {
2805 buffersize = 0;
2806 } else {
2807 count = MIN(buffersize / sizeof(uint64_t), MAX_UPTRS);
2808 buffersize = count * sizeof(uint64_t);
2809 kbuf = kheap_alloc(KHEAP_TEMP, buffersize, Z_WAITOK);
2810 }
2811
2812 nuptrs = kevent_proc_copy_uptrs(p, kbuf, buffersize);
2813
2814 if (kbuf) {
2815 size_t copysize;
2816 if (os_mul_overflow(nuptrs, sizeof(uint64_t), &copysize)) {
2817 error = ERANGE;
2818 goto out;
2819 }
2820 if (copysize > buffersize) {
2821 copysize = buffersize;
2822 }
2823 error = copyout(kbuf, buffer, copysize);
2824 }
2825
2826 out:
2827 *retval = nuptrs;
2828
2829 if (kbuf) {
2830 kheap_free(KHEAP_TEMP, kbuf, buffersize);
2831 kbuf = NULL;
2832 }
2833
2834 return error;
2835 }
2836
2837 /*
2838 * Helper function for proc_pidfileportinfo
2839 */
2840
2841 struct fileport_info_args {
2842 int fia_flavor;
2843 user_addr_t fia_buffer;
2844 uint32_t fia_buffersize;
2845 int32_t *fia_retval;
2846 };
2847
2848 static kern_return_t
2849 proc_fileport_info(__unused mach_port_name_t name,
2850 struct fileglob *fg, void *arg)
2851 {
2852 struct fileport_info_args *fia = arg;
2853 struct fileproc __fileproc, *fp = &__fileproc;
2854 int error;
2855
2856 bzero(fp, sizeof(*fp));
2857 fp->fp_glob = fg;
2858
2859 switch (fia->fia_flavor) {
2860 case PROC_PIDFILEPORTVNODEPATHINFO: {
2861 vnode_t vp;
2862
2863 if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) {
2864 error = ENOTSUP;
2865 break;
2866 }
2867 vp = (struct vnode *)fg->fg_data;
2868 error = pid_vnodeinfopath(vp, fp, PROC_NULL, 0,
2869 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2870 } break;
2871
2872 case PROC_PIDFILEPORTSOCKETINFO: {
2873 socket_t so;
2874
2875 if (FILEGLOB_DTYPE(fg) != DTYPE_SOCKET) {
2876 error = EOPNOTSUPP;
2877 break;
2878 }
2879 so = (socket_t)fg->fg_data;
2880 error = pid_socketinfo(so, fp, PROC_NULL, 0,
2881 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2882 } break;
2883
2884 case PROC_PIDFILEPORTPSHMINFO: {
2885 struct pshmnode *pshm;
2886
2887 if (FILEGLOB_DTYPE(fg) != DTYPE_PSXSHM) {
2888 error = EBADF; /* ick - mirror fp_getfpshm */
2889 break;
2890 }
2891 pshm = (struct pshmnode *)fg->fg_data;
2892 error = pid_pshminfo(pshm, fp, PROC_NULL, 0,
2893 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2894 } break;
2895
2896 case PROC_PIDFILEPORTPIPEINFO: {
2897 struct pipe *cpipe;
2898
2899 if (FILEGLOB_DTYPE(fg) != DTYPE_PIPE) {
2900 error = EBADF; /* ick - mirror fp_getfpipe */
2901 break;
2902 }
2903 cpipe = (struct pipe *)fg->fg_data;
2904 error = pid_pipeinfo(cpipe, fp, PROC_NULL, 0,
2905 fia->fia_buffer, fia->fia_buffersize, fia->fia_retval);
2906 } break;
2907
2908 default:
2909 error = EINVAL;
2910 break;
2911 }
2912
2913 return error;
2914 }
2915
2916 /************************* proc_pidfileportinfo routine *********************/
2917 int
2918 proc_pidfileportinfo(int pid, int flavor, mach_port_name_t name,
2919 user_addr_t buffer, uint32_t buffersize, int32_t *retval)
2920 {
2921 proc_t p;
2922 int error = ENOTSUP;
2923 uint32_t size;
2924 struct fileport_info_args fia;
2925
2926 /* fileport types are restricted by file_issendable() */
2927
2928 switch (flavor) {
2929 case PROC_PIDFILEPORTVNODEPATHINFO:
2930 size = PROC_PIDFILEPORTVNODEPATHINFO_SIZE;
2931 break;
2932 case PROC_PIDFILEPORTSOCKETINFO:
2933 size = PROC_PIDFILEPORTSOCKETINFO_SIZE;
2934 break;
2935 case PROC_PIDFILEPORTPSHMINFO:
2936 size = PROC_PIDFILEPORTPSHMINFO_SIZE;
2937 break;
2938 case PROC_PIDFILEPORTPIPEINFO:
2939 size = PROC_PIDFILEPORTPIPEINFO_SIZE;
2940 break;
2941 default:
2942 return EINVAL;
2943 }
2944
2945 if (buffersize < size) {
2946 return ENOMEM;
2947 }
2948 if ((p = proc_find(pid)) == PROC_NULL) {
2949 error = ESRCH;
2950 goto out;
2951 }
2952
2953 /* Do we have permission to look into this? */
2954 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDFILEPORTINFO, flavor, CHECK_SAME_USER))) {
2955 goto out1;
2956 }
2957
2958 fia.fia_flavor = flavor;
2959 fia.fia_buffer = buffer;
2960 fia.fia_buffersize = buffersize;
2961 fia.fia_retval = retval;
2962
2963 if (fileport_invoke(p->task, name,
2964 proc_fileport_info, &fia, &error) != KERN_SUCCESS) {
2965 error = EINVAL;
2966 }
2967 out1:
2968 proc_rele(p);
2969 out:
2970 return error;
2971 }
2972
2973 int
2974 proc_security_policy(proc_t targetp, __unused int callnum, __unused int flavor, boolean_t check_same_user)
2975 {
2976 #if CONFIG_MACF
2977 int error = 0;
2978
2979 if ((error = mac_proc_check_proc_info(current_proc(), targetp, callnum, flavor))) {
2980 return error;
2981 }
2982 #endif
2983
2984 /* The 'listpids' call doesn't have a target proc */
2985 if (targetp == PROC_NULL) {
2986 assert(callnum == PROC_INFO_CALL_LISTPIDS && check_same_user == NO_CHECK_SAME_USER);
2987 return 0;
2988 }
2989
2990 /*
2991 * Check for 'get information for processes owned by other users' privilege
2992 * root has this privilege by default
2993 */
2994 if (priv_check_cred(kauth_cred_get(), PRIV_GLOBAL_PROC_INFO, 0) == 0) {
2995 check_same_user = FALSE;
2996 }
2997
2998 if (check_same_user) {
2999 kauth_cred_t target_cred;
3000 uid_t target_uid;
3001
3002 target_cred = kauth_cred_proc_ref(targetp);
3003 target_uid = kauth_cred_getuid(target_cred);
3004 kauth_cred_unref(&target_cred);
3005
3006 if (kauth_getuid() != target_uid) {
3007 return EPERM;
3008 }
3009 }
3010
3011 return 0;
3012 }
3013
3014 int
3015 proc_kernmsgbuf(user_addr_t buffer, uint32_t buffersize, int32_t * retval)
3016 {
3017 #if CONFIG_MACF
3018 int error = 0;
3019
3020 if ((error = mac_system_check_info(kauth_cred_get(), "kern.msgbuf"))) {
3021 return error;
3022 }
3023 #endif
3024
3025 if (suser(kauth_cred_get(), (u_short *)0) == 0) {
3026 return log_dmesg(buffer, buffersize, retval);
3027 } else {
3028 return EPERM;
3029 }
3030 }
3031
3032 /* ********* process control sets on self only */
3033 int
3034 proc_setcontrol(int pid, int flavor, uint64_t arg, user_addr_t buffer, uint32_t buffersize, __unused int32_t * retval)
3035 {
3036 struct proc * pself = PROC_NULL;
3037 int error = 0;
3038 uint32_t pcontrol = (uint32_t)arg;
3039 struct uthread *ut = NULL;
3040 char name_buf[MAXTHREADNAMESIZE];
3041
3042 pself = current_proc();
3043 if (pid != pself->p_pid) {
3044 return EINVAL;
3045 }
3046
3047 /* Do we have permission to look into this? */
3048 if ((error = proc_security_policy(pself, PROC_INFO_CALL_SETCONTROL, flavor, NO_CHECK_SAME_USER))) {
3049 goto out;
3050 }
3051
3052 switch (flavor) {
3053 case PROC_SELFSET_PCONTROL: {
3054 if (pcontrol > P_PCMAX) {
3055 return EINVAL;
3056 }
3057 proc_lock(pself);
3058 /* reset existing control setting while retaining action state */
3059 pself->p_pcaction &= PROC_ACTION_MASK;
3060 /* set new control state */
3061 pself->p_pcaction |= pcontrol;
3062 proc_unlock(pself);
3063 }
3064 break;
3065
3066 case PROC_SELFSET_THREADNAME: {
3067 /*
3068 * This is a bit ugly, as it copies the name into the kernel, and then
3069 * invokes bsd_setthreadname again to copy it into the uthread name
3070 * buffer. Hopefully this isn't such a hot codepath that an additional
3071 * MAXTHREADNAMESIZE copy is a big issue.
3072 */
3073 if (buffersize > (MAXTHREADNAMESIZE - 1)) {
3074 return ENAMETOOLONG;
3075 }
3076
3077 ut = current_uthread();
3078
3079 bzero(name_buf, MAXTHREADNAMESIZE);
3080 error = copyin(buffer, name_buf, buffersize);
3081
3082 if (!error) {
3083 bsd_setthreadname(ut, name_buf);
3084 }
3085 }
3086 break;
3087
3088 case PROC_SELFSET_VMRSRCOWNER: {
3089 /* need to to be superuser */
3090 if (suser(kauth_cred_get(), (u_short *)0) != 0) {
3091 error = EPERM;
3092 goto out;
3093 }
3094
3095 proc_lock(pself);
3096 /* reset existing control setting while retaining action state */
3097 pself->p_lflag |= P_LVMRSRCOWNER;
3098 proc_unlock(pself);
3099 }
3100 break;
3101
3102 case PROC_SELFSET_DELAYIDLESLEEP: {
3103 #if CONFIG_DELAY_IDLE_SLEEP
3104 /* mark or clear the process property to delay idle sleep disk IO */
3105 if (pcontrol != 0) {
3106 OSBitOrAtomic(P_DELAYIDLESLEEP, &pself->p_flag);
3107 } else {
3108 OSBitAndAtomic(~((uint32_t)P_DELAYIDLESLEEP), &pself->p_flag);
3109 }
3110 }
3111 break;
3112 #else
3113 error = ENOTSUP;
3114 goto out;
3115 }
3116 #endif
3117
3118 default:
3119 error = ENOTSUP;
3120 }
3121
3122 out:
3123 return error;
3124 }
3125
3126 #if CONFIG_MEMORYSTATUS
3127
3128 int
3129 proc_dirtycontrol(int pid, int flavor, uint64_t arg, int32_t *retval)
3130 {
3131 struct proc *target_p;
3132 int error = 0;
3133 uint32_t pcontrol = (uint32_t)arg;
3134 kauth_cred_t my_cred, target_cred;
3135 boolean_t self = FALSE;
3136 boolean_t child = FALSE;
3137 boolean_t zombref = FALSE;
3138 pid_t selfpid;
3139
3140 target_p = proc_find(pid);
3141
3142 if (target_p == PROC_NULL) {
3143 if (flavor == PROC_DIRTYCONTROL_GET) {
3144 target_p = proc_find_zombref(pid);
3145 zombref = 1;
3146 }
3147
3148 if (target_p == PROC_NULL) {
3149 return ESRCH;
3150 }
3151 }
3152
3153 my_cred = kauth_cred_get();
3154 target_cred = kauth_cred_proc_ref(target_p);
3155
3156 /* Do we have permission to look into this? */
3157 if ((error = proc_security_policy(target_p, PROC_INFO_CALL_DIRTYCONTROL, flavor, NO_CHECK_SAME_USER))) {
3158 goto out;
3159 }
3160
3161 selfpid = proc_selfpid();
3162 if (pid == selfpid) {
3163 self = TRUE;
3164 } else if (target_p->p_ppid == selfpid) {
3165 child = TRUE;
3166 }
3167
3168 switch (flavor) {
3169 case PROC_DIRTYCONTROL_TRACK: {
3170 /* Only allow the process itself, its parent, or root */
3171 if ((self == FALSE) && (child == FALSE) && kauth_cred_issuser(kauth_cred_get()) != TRUE) {
3172 error = EPERM;
3173 goto out;
3174 }
3175
3176 error = memorystatus_dirty_track(target_p, pcontrol);
3177 }
3178 break;
3179
3180 case PROC_DIRTYCONTROL_SET: {
3181 /* Check privileges; use cansignal() here since the process could be terminated */
3182 if (!cansignal(current_proc(), my_cred, target_p, SIGKILL)) {
3183 error = EPERM;
3184 goto out;
3185 }
3186
3187 error = memorystatus_dirty_set(target_p, self, pcontrol);
3188 }
3189 break;
3190
3191 case PROC_DIRTYCONTROL_GET: {
3192 /* No permissions check - dirty state is freely available */
3193 if (retval) {
3194 *retval = memorystatus_dirty_get(target_p, FALSE);
3195 } else {
3196 error = EINVAL;
3197 }
3198 }
3199 break;
3200
3201 case PROC_DIRTYCONTROL_CLEAR: {
3202 /* Check privileges; use cansignal() here since the process could be terminated */
3203 if (!cansignal(current_proc(), my_cred, target_p, SIGKILL)) {
3204 error = EPERM;
3205 goto out;
3206 }
3207
3208 error = memorystatus_dirty_clear(target_p, pcontrol);
3209 }
3210 break;
3211 }
3212
3213 out:
3214 if (zombref) {
3215 proc_drop_zombref(target_p);
3216 } else {
3217 proc_rele(target_p);
3218 }
3219
3220 kauth_cred_unref(&target_cred);
3221
3222 return error;
3223 }
3224 #else
3225
3226 int
3227 proc_dirtycontrol(__unused int pid, __unused int flavor, __unused uint64_t arg, __unused int32_t *retval)
3228 {
3229 return ENOTSUP;
3230 }
3231
3232 #endif /* CONFIG_MEMORYSTATUS */
3233
3234 /*
3235 * proc_terminate() provides support for sudden termination.
3236 * SIGKILL is issued to tracked, clean processes; otherwise,
3237 * SIGTERM is sent.
3238 */
3239
3240 int
3241 proc_terminate(int pid, int32_t *retval)
3242 {
3243 int error = 0;
3244 proc_t p;
3245 kauth_cred_t uc = kauth_cred_get();
3246 int sig;
3247
3248 #if 0
3249 /* XXX: Check if these are necessary */
3250 AUDIT_ARG(pid, pid);
3251 AUDIT_ARG(signum, sig);
3252 #endif
3253
3254 if (pid <= 0 || retval == NULL) {
3255 return EINVAL;
3256 }
3257
3258 if ((p = proc_find(pid)) == NULL) {
3259 return ESRCH;
3260 }
3261
3262 #if 0
3263 /* XXX: Check if these are necessary */
3264 AUDIT_ARG(process, p);
3265 #endif
3266
3267 /* Check privileges; if SIGKILL can be issued, then SIGTERM is also OK */
3268 if (!cansignal(current_proc(), uc, p, SIGKILL)) {
3269 error = EPERM;
3270 goto out;
3271 }
3272
3273 /* Not allowed to sudden terminate yourself */
3274 if (p == current_proc()) {
3275 error = EPERM;
3276 goto out;
3277 }
3278
3279 #if CONFIG_MEMORYSTATUS
3280 /* Determine requisite signal to issue */
3281 sig = memorystatus_on_terminate(p);
3282 #else
3283 sig = SIGTERM;
3284 #endif
3285
3286 proc_set_task_policy(p->task, TASK_POLICY_ATTRIBUTE,
3287 TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
3288
3289 psignal(p, sig);
3290 *retval = sig;
3291
3292 out:
3293 proc_rele(p);
3294
3295 return error;
3296 }
3297
3298 /*
3299 * copy stat64 structure into vinfo_stat structure.
3300 */
3301 static void
3302 munge_vinfo_stat(struct stat64 *sbp, struct vinfo_stat *vsbp)
3303 {
3304 bzero(vsbp, sizeof(struct vinfo_stat));
3305
3306 vsbp->vst_dev = sbp->st_dev;
3307 vsbp->vst_mode = sbp->st_mode;
3308 vsbp->vst_nlink = sbp->st_nlink;
3309 vsbp->vst_ino = sbp->st_ino;
3310 vsbp->vst_uid = sbp->st_uid;
3311 vsbp->vst_gid = sbp->st_gid;
3312 vsbp->vst_atime = sbp->st_atimespec.tv_sec;
3313 vsbp->vst_atimensec = sbp->st_atimespec.tv_nsec;
3314 vsbp->vst_mtime = sbp->st_mtimespec.tv_sec;
3315 vsbp->vst_mtimensec = sbp->st_mtimespec.tv_nsec;
3316 vsbp->vst_ctime = sbp->st_ctimespec.tv_sec;
3317 vsbp->vst_ctimensec = sbp->st_ctimespec.tv_nsec;
3318 vsbp->vst_birthtime = sbp->st_birthtimespec.tv_sec;
3319 vsbp->vst_birthtimensec = sbp->st_birthtimespec.tv_nsec;
3320 vsbp->vst_size = sbp->st_size;
3321 vsbp->vst_blocks = sbp->st_blocks;
3322 vsbp->vst_blksize = sbp->st_blksize;
3323 vsbp->vst_flags = sbp->st_flags;
3324 vsbp->vst_gen = sbp->st_gen;
3325 vsbp->vst_rdev = sbp->st_rdev;
3326 vsbp->vst_qspare[0] = sbp->st_qspare[0];
3327 vsbp->vst_qspare[1] = sbp->st_qspare[1];
3328 }
3329
3330 int
3331 proc_pid_rusage(int pid, int flavor, user_addr_t buffer, __unused int32_t *retval)
3332 {
3333 proc_t p;
3334 int error;
3335 int zombie = 0;
3336
3337 if ((p = proc_find(pid)) == PROC_NULL) {
3338 if ((p = proc_find_zombref(pid)) == PROC_NULL) {
3339 return ESRCH;
3340 }
3341 zombie = 1;
3342 }
3343
3344 /* Do we have permission to look into this? */
3345 if ((error = proc_security_policy(p, PROC_INFO_CALL_PIDRUSAGE, flavor, CHECK_SAME_USER))) {
3346 goto out;
3347 }
3348
3349 error = proc_get_rusage(p, flavor, buffer, zombie);
3350
3351 out:
3352 if (zombie) {
3353 proc_drop_zombref(p);
3354 } else {
3355 proc_rele(p);
3356 }
3357
3358 return error;
3359 }
3360
3361 void
3362 proc_archinfo(proc_t p, struct proc_archinfo *pai)
3363 {
3364 proc_lock(p);
3365 {
3366 pai->p_cputype = p->p_cputype;
3367 pai->p_cpusubtype = p->p_cpusubtype;
3368 }
3369 proc_unlock(p);
3370 }
3371
3372 void
3373 proc_pidcoalitioninfo(proc_t p, struct proc_pidcoalitioninfo *ppci)
3374 {
3375 bzero(ppci, sizeof(*ppci));
3376 proc_coalitionids(p, ppci->coalition_id);
3377 }
3378
3379 int
3380 proc_pidexitreasoninfo(proc_t p, struct proc_exitreasoninfo *peri, struct proc_exitreasonbasicinfo *pberi)
3381 {
3382 uint32_t reason_data_size = 0;
3383 int error = 0;
3384 pid_t selfpid = proc_selfpid();
3385
3386 proc_lock(p);
3387
3388 /*
3389 * One (and only one) of peri and pberi must be non-NULL.
3390 */
3391 assert((peri != NULL) || (pberi != NULL));
3392 assert((peri == NULL) || (pberi == NULL));
3393
3394 /*
3395 * Allow access to the parent of the exiting
3396 * child or the parent debugger only.
3397 */
3398 do {
3399 if (p->p_ppid == selfpid) {
3400 break; /* parent => ok */
3401 }
3402 if ((p->p_lflag & P_LTRACED) != 0 &&
3403 (p->p_oppid == selfpid)) {
3404 break; /* parent-in-waiting => ok */
3405 }
3406 proc_unlock(p);
3407 return EACCES;
3408 } while (0);
3409
3410 if (p->p_exit_reason == OS_REASON_NULL) {
3411 proc_unlock(p);
3412 return ENOENT;
3413 }
3414
3415 if (p->p_exit_reason->osr_kcd_buf != NULL) {
3416 reason_data_size = (uint32_t)kcdata_memory_get_used_bytes(&p->p_exit_reason->osr_kcd_descriptor);
3417 }
3418
3419 if (peri != NULL) {
3420 peri->eri_namespace = p->p_exit_reason->osr_namespace;
3421 peri->eri_code = p->p_exit_reason->osr_code;
3422 peri->eri_flags = p->p_exit_reason->osr_flags;
3423
3424 if ((peri->eri_kcd_buf == 0) || (peri->eri_reason_buf_size < reason_data_size)) {
3425 proc_unlock(p);
3426 return ENOMEM;
3427 }
3428
3429 peri->eri_reason_buf_size = reason_data_size;
3430 if (reason_data_size != 0) {
3431 error = copyout(p->p_exit_reason->osr_kcd_buf, (user_addr_t)peri->eri_kcd_buf, reason_data_size);
3432 }
3433 } else {
3434 pberi->beri_namespace = p->p_exit_reason->osr_namespace;
3435 pberi->beri_code = p->p_exit_reason->osr_code;
3436 pberi->beri_flags = p->p_exit_reason->osr_flags;
3437 pberi->beri_reason_buf_size = reason_data_size;
3438 }
3439
3440 proc_unlock(p);
3441
3442 return error;
3443 }
3444
3445 /*
3446 * Wrapper to provide NOTE_EXIT_DETAIL and NOTE_EXITSTATUS
3447 * It mimics the data that is typically captured by the
3448 * EVFILT_PROC, NOTE_EXIT event mechanism.
3449 * See filt_proc() in kern_event.c.
3450 */
3451 int
3452 proc_pidnoteexit(proc_t p, uint64_t flags, uint32_t *data)
3453 {
3454 uint32_t exit_data = 0;
3455 uint32_t exit_flags = (uint32_t)flags;
3456
3457 proc_lock(p);
3458
3459 /*
3460 * Allow access to the parent of the exiting
3461 * child or the parent debugger only.
3462 */
3463 do {
3464 pid_t selfpid = proc_selfpid();
3465
3466 if (p->p_ppid == selfpid) {
3467 break; /* parent => ok */
3468 }
3469 if ((p->p_lflag & P_LTRACED) != 0 &&
3470 (p->p_oppid == selfpid)) {
3471 break; /* parent-in-waiting => ok */
3472 }
3473 proc_unlock(p);
3474 return EACCES;
3475 } while (0);
3476
3477 if ((exit_flags & NOTE_EXITSTATUS) != 0) {
3478 /* The signal and exit status */
3479 exit_data |= (p->p_xstat & NOTE_PDATAMASK);
3480 }
3481
3482 if ((exit_flags & NOTE_EXIT_DETAIL) != 0) {
3483 /* The exit detail */
3484 if ((p->p_lflag & P_LTERM_DECRYPTFAIL) != 0) {
3485 exit_data |= NOTE_EXIT_DECRYPTFAIL;
3486 }
3487
3488 if ((p->p_lflag & P_LTERM_JETSAM) != 0) {
3489 exit_data |= NOTE_EXIT_MEMORY;
3490
3491 switch (p->p_lflag & P_JETSAM_MASK) {
3492 case P_JETSAM_VMPAGESHORTAGE:
3493 exit_data |= NOTE_EXIT_MEMORY_VMPAGESHORTAGE;
3494 break;
3495 case P_JETSAM_VMTHRASHING:
3496 exit_data |= NOTE_EXIT_MEMORY_VMTHRASHING;
3497 break;
3498 case P_JETSAM_FCTHRASHING:
3499 exit_data |= NOTE_EXIT_MEMORY_FCTHRASHING;
3500 break;
3501 case P_JETSAM_VNODE:
3502 exit_data |= NOTE_EXIT_MEMORY_VNODE;
3503 break;
3504 case P_JETSAM_HIWAT:
3505 exit_data |= NOTE_EXIT_MEMORY_HIWAT;
3506 break;
3507 case P_JETSAM_PID:
3508 exit_data |= NOTE_EXIT_MEMORY_PID;
3509 break;
3510 case P_JETSAM_IDLEEXIT:
3511 exit_data |= NOTE_EXIT_MEMORY_IDLE;
3512 break;
3513 }
3514 }
3515
3516 if ((p->p_csflags & CS_KILLED) != 0) {
3517 exit_data |= NOTE_EXIT_CSERROR;
3518 }
3519 }
3520
3521 proc_unlock(p);
3522
3523 *data = exit_data;
3524
3525 return 0;
3526 }
3527
3528 int
3529 proc_piddynkqueueinfo(int pid, int flavor, kqueue_id_t kq_id,
3530 user_addr_t ubuf, uint32_t bufsize, int32_t *retval)
3531 {
3532 proc_t p;
3533 int err;
3534
3535 if (ubuf == USER_ADDR_NULL) {
3536 return EFAULT;
3537 }
3538
3539 p = proc_find(pid);
3540 if (p == PROC_NULL) {
3541 return ESRCH;
3542 }
3543
3544 err = proc_security_policy(p, PROC_INFO_CALL_PIDDYNKQUEUEINFO, 0, CHECK_SAME_USER);
3545 if (err) {
3546 goto out;
3547 }
3548
3549 switch (flavor) {
3550 case PROC_PIDDYNKQUEUE_INFO:
3551 err = kevent_copyout_dynkqinfo(p, kq_id, ubuf, bufsize, retval);
3552 break;
3553 case PROC_PIDDYNKQUEUE_EXTINFO:
3554 err = kevent_copyout_dynkqextinfo(p, kq_id, ubuf, bufsize, retval);
3555 break;
3556 default:
3557 err = ENOTSUP;
3558 break;
3559 }
3560
3561 out:
3562 proc_rele(p);
3563
3564 return err;
3565 }
3566
3567 #if CONFIG_PROC_UDATA_STORAGE
3568 int
3569 proc_udata_info(int pid, int flavor, user_addr_t buffer, uint32_t bufsize, int32_t *retval)
3570 {
3571 int err = 0;
3572 proc_t p;
3573
3574 p = proc_find(pid);
3575 if (p == PROC_NULL) {
3576 return ESRCH;
3577 }
3578
3579 /*
3580 * Only support calls against oneself for the moment.
3581 */
3582 if (p->p_pid != proc_selfpid()) {
3583 err = EACCES;
3584 goto out;
3585 }
3586
3587 if (bufsize != sizeof(p->p_user_data)) {
3588 err = EINVAL;
3589 goto out;
3590 }
3591
3592 switch (flavor) {
3593 case PROC_UDATA_INFO_SET:
3594 err = copyin(buffer, &p->p_user_data, sizeof(p->p_user_data));
3595 break;
3596 case PROC_UDATA_INFO_GET:
3597 err = copyout(&p->p_user_data, buffer, sizeof(p->p_user_data));
3598 break;
3599 default:
3600 err = ENOTSUP;
3601 break;
3602 }
3603
3604 out:
3605 proc_rele(p);
3606
3607 if (err == 0) {
3608 *retval = 0;
3609 }
3610
3611 return err;
3612 }
3613 #endif /* CONFIG_PROC_UDATA_STORAGE */