xref: /xnu-11215.61.5/bsd/kern/kern_newsysctl.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1 /*
2  * Copyright (c) 2000-2019 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  * Copyright (c) 1982, 1986, 1989, 1993
30  *	The Regents of the University of California.  All rights reserved.
31  *
32  * This code is derived from software contributed to Berkeley by
33  * Mike Karels at Berkeley Software Design, Inc.
34  *
35  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
36  * project, to make these variables more userfriendly.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
67  */
68 
69 
70 #include <kern/counter.h>
71 #include <sys/param.h>
72 #include <sys/buf.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/malloc.h>
76 #include <sys/proc_internal.h>
77 #include <sys/kauth.h>
78 #include <sys/systm.h>
79 #include <sys/sysproto.h>
80 
81 #include <vm/vm_pageout_xnu.h>
82 
83 #include <os/atomic_private.h>
84 
85 #include <security/audit/audit.h>
86 #include <pexpert/pexpert.h>
87 
88 #include <IOKit/IOBSD.h>
89 
90 #if CONFIG_MACF
91 #include <security/mac_framework.h>
92 #endif
93 
94 #if defined(HAS_APPLE_PAC)
95 #include <os/hash.h>
96 #include <ptrauth.h>
97 #endif /* defined(HAS_APPLE_PAC) */
98 
99 #include <libkern/coreanalytics/coreanalytics.h>
100 
101 #if DEBUG || DEVELOPMENT
102 #include <os/system_event_log.h>
103 #endif /* DEBUG || DEVELOPMENT */
104 
105 static LCK_GRP_DECLARE(sysctl_lock_group, "sysctl");
106 static LCK_RW_DECLARE(sysctl_geometry_lock, &sysctl_lock_group);
107 static LCK_MTX_DECLARE(sysctl_unlocked_node_lock, &sysctl_lock_group);
108 
109 /*
110  * Conditionally allow dtrace to see these functions for debugging purposes.
111  */
112 #ifdef STATIC
113 #undef STATIC
114 #endif
115 #if 0
116 #define STATIC
117 #else
118 #define STATIC static
119 #endif
120 
121 /* forward declarations  of static functions */
122 STATIC void sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i);
123 STATIC int sysctl_sysctl_debug(struct sysctl_oid *oidp, void *arg1,
124     int arg2, struct sysctl_req *req);
125 STATIC int sysctl_sysctl_name(struct sysctl_oid *oidp, void *arg1,
126     int arg2, struct sysctl_req *req);
127 STATIC int sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp,
128     int *name, u_int namelen, int *next, int *len, int level,
129     struct sysctl_oid **oidpp);
130 STATIC int sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l);
131 STATIC int sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l);
132 STATIC int name2oid(char *name, int *oid, size_t *len);
133 STATIC int sysctl_sysctl_name2oid(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
134 STATIC int sysctl_sysctl_next(struct sysctl_oid *oidp, void *arg1, int arg2,
135     struct sysctl_req *req);
136 STATIC int sysctl_sysctl_oidfmt(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
137 STATIC int sysctl_old_user(struct sysctl_req *req, const void *p, size_t l);
138 STATIC int sysctl_new_user(struct sysctl_req *req, void *p, size_t l);
139 
140 STATIC void sysctl_create_user_req(struct sysctl_req *req, struct proc *p, user_addr_t oldp,
141     size_t oldlen, user_addr_t newp, size_t newlen);
142 STATIC int sysctl_root(boolean_t from_kernel, boolean_t string_is_canonical, char *namestring, size_t namestringlen, int *name, size_t namelen, struct sysctl_req *req);
143 
144 int     kernel_sysctl(struct proc *p, int *name, size_t namelen, void *old, size_t *oldlenp, void *new, size_t newlen);
145 int     kernel_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
146 int     userland_sysctl(boolean_t string_is_canonical,
147     char *namestring, size_t namestringlen,
148     int *name, u_int namelen, struct sysctl_req *req,
149     size_t *retval);
150 
151 SECURITY_READ_ONLY_LATE(struct sysctl_oid_list) sysctl__children; /* root list */
152 __SYSCTL_EXTENSION_NODE();
153 
154 /*
155  * Initialization of the MIB tree.
156  *
157  * Order by number in each list.
158  */
159 
160 static void
sysctl_register_oid_locked(struct sysctl_oid * new_oidp,struct sysctl_oid * oidp)161 sysctl_register_oid_locked(struct sysctl_oid *new_oidp,
162     struct sysctl_oid *oidp)
163 {
164 	struct sysctl_oid_list *parent = new_oidp->oid_parent;
165 	struct sysctl_oid_list *parent_rw = NULL;
166 	struct sysctl_oid *p, **prevp;
167 
168 	p = SLIST_FIRST(parent);
169 	if (p && p->oid_number == OID_MUTABLE_ANCHOR) {
170 		parent_rw = p->oid_arg1;
171 	}
172 
173 	if (oidp->oid_number == OID_AUTO) {
174 		int n = OID_AUTO_START;
175 
176 		/*
177 		 * If this oid has a number OID_AUTO, give it a number which
178 		 * is greater than any current oid.  Make sure it is at least
179 		 * OID_AUTO_START to leave space for pre-assigned oid numbers.
180 		 */
181 
182 		SLIST_FOREACH_PREVPTR(p, prevp, parent, oid_link) {
183 			if (p->oid_number >= n) {
184 				n = p->oid_number + 1;
185 			}
186 		}
187 
188 		if (parent_rw) {
189 			SLIST_FOREACH_PREVPTR(p, prevp, parent_rw, oid_link) {
190 				if (p->oid_number >= n) {
191 					n = p->oid_number + 1;
192 				}
193 			}
194 		}
195 
196 		/*
197 		 * Reflect the number in an allocated OID into the template
198 		 * of the caller for sysctl_unregister_oid() compares.
199 		 */
200 		oidp->oid_number = new_oidp->oid_number = n;
201 	} else {
202 		/*
203 		 * Insert the oid into the parent's list in order.
204 		 */
205 		SLIST_FOREACH_PREVPTR(p, prevp, parent, oid_link) {
206 			if (oidp->oid_number == p->oid_number) {
207 				panic("attempting to register a sysctl at previously registered slot : %d",
208 				    oidp->oid_number);
209 			} else if (oidp->oid_number < p->oid_number) {
210 				break;
211 			}
212 		}
213 
214 		if (parent_rw) {
215 			SLIST_FOREACH_PREVPTR(p, prevp, parent_rw, oid_link) {
216 				if (oidp->oid_number == p->oid_number) {
217 					panic("attempting to register a sysctl at previously registered slot : %d",
218 					    oidp->oid_number);
219 				} else if (oidp->oid_number < p->oid_number) {
220 					break;
221 				}
222 			}
223 		}
224 	}
225 
226 #if defined(HAS_APPLE_PAC)
227 	if (oidp->oid_handler) {
228 		/*
229 		 * Sign oid_handler address-discriminated upon installation to make it
230 		 * harder to replace with an arbitrary function pointer.  Blend with
231 		 * a hash of oid_arg1 for robustness against memory corruption.
232 		 */
233 		oidp->oid_handler = ptrauth_auth_and_resign(oidp->oid_handler,
234 		    ptrauth_key_function_pointer,
235 		    ptrauth_function_pointer_type_discriminator(typeof(oidp->oid_handler)),
236 		    ptrauth_key_function_pointer,
237 		    ptrauth_blend_discriminator(&oidp->oid_handler,
238 		    os_hash_kernel_pointer(oidp->oid_arg1)));
239 	}
240 #endif /* defined(HAS_APPLE_PAC) */
241 
242 	SLIST_NEXT(oidp, oid_link) = *prevp;
243 	*prevp = oidp;
244 }
245 
246 void
sysctl_register_oid(struct sysctl_oid * new_oidp)247 sysctl_register_oid(struct sysctl_oid *new_oidp)
248 {
249 	struct sysctl_oid *oidp;
250 
251 	if (new_oidp->oid_number < OID_AUTO) {
252 		panic("trying to register a node %p with an invalid oid_number: %d",
253 		    new_oidp, new_oidp->oid_number);
254 	}
255 	if (new_oidp->oid_kind & CTLFLAG_PERMANENT) {
256 		panic("Use sysctl_register_oid_early to register permanent nodes");
257 	}
258 
259 	/*
260 	 * The OID can be old-style (needs copy), new style without an earlier
261 	 * version (also needs copy), or new style with a matching version (no
262 	 * copy needed).  Later versions are rejected (presumably, the OID
263 	 * structure was changed for a necessary reason).
264 	 */
265 	if (!(new_oidp->oid_kind & CTLFLAG_OID2)) {
266 #if __x86_64__
267 		oidp = kalloc_type(struct sysctl_oid, Z_WAITOK | Z_ZERO | Z_NOFAIL);
268 		/*
269 		 * Copy the structure only through the oid_fmt field, which
270 		 * is the last field in a non-OID2 OID structure.
271 		 *
272 		 * Note:	We may want to set the oid_descr to the
273 		 *		oid_name (or "") at some future date.
274 		 */
275 		memcpy(oidp, new_oidp, offsetof(struct sysctl_oid, oid_descr));
276 #else
277 		panic("Old style sysctl without a version number isn't supported");
278 #endif
279 	} else {
280 		/* It's a later version; handle the versions we know about */
281 		switch (new_oidp->oid_version) {
282 		case SYSCTL_OID_VERSION:
283 			/* current version */
284 			oidp = new_oidp;
285 			break;
286 		default:
287 			return;                 /* rejects unknown version */
288 		}
289 	}
290 
291 	lck_rw_lock_exclusive(&sysctl_geometry_lock);
292 	sysctl_register_oid_locked(new_oidp, oidp);
293 	lck_rw_unlock_exclusive(&sysctl_geometry_lock);
294 }
295 
296 __startup_func
297 void
sysctl_register_oid_early(struct sysctl_oid * oidp)298 sysctl_register_oid_early(struct sysctl_oid *oidp)
299 {
300 	assert((oidp->oid_kind & CTLFLAG_OID2) &&
301 	    (oidp->oid_kind & CTLFLAG_PERMANENT) &&
302 	    oidp->oid_version == SYSCTL_OID_VERSION);
303 	assert(startup_phase < STARTUP_SUB_SYSCTL);
304 
305 	/*
306 	 * Clear the flag so that callers can use sysctl_register_oid_early
307 	 * again if they wish to register their node.
308 	 */
309 	if (oidp->oid_kind & CTLFLAG_NOAUTO) {
310 		oidp->oid_kind &= ~CTLFLAG_NOAUTO;
311 		return;
312 	}
313 
314 	sysctl_register_oid_locked(oidp, oidp);
315 }
316 
317 void
sysctl_unregister_oid(struct sysctl_oid * oidp)318 sysctl_unregister_oid(struct sysctl_oid *oidp)
319 {
320 	struct sysctl_oid *removed_oidp = NULL; /* OID removed from tree */
321 #if __x86_64__
322 	struct sysctl_oid *old_oidp = NULL;     /* OID compatibility copy */
323 #endif
324 	struct sysctl_oid_list *lsp;
325 
326 	/* Get the write lock to modify the geometry */
327 	lck_rw_lock_exclusive(&sysctl_geometry_lock);
328 
329 	lsp = oidp->oid_parent;
330 	if (SLIST_FIRST(lsp) && SLIST_FIRST(lsp)->oid_number == OID_MUTABLE_ANCHOR) {
331 		lsp = SLIST_FIRST(lsp)->oid_arg1;
332 	}
333 
334 	if (oidp->oid_kind & CTLFLAG_PERMANENT) {
335 		panic("Trying to unregister permanent sysctl %p", oidp);
336 	}
337 
338 	if (!(oidp->oid_kind & CTLFLAG_OID2)) {
339 #if __x86_64__
340 		/*
341 		 * We're using a copy so we can get the new fields in an
342 		 * old structure, so we have to iterate to compare the
343 		 * partial structure; when we find a match, we remove it
344 		 * normally and free the memory.
345 		 */
346 		SLIST_FOREACH(old_oidp, lsp, oid_link) {
347 			if (!memcmp(&oidp->oid_number, &old_oidp->oid_number, (offsetof(struct sysctl_oid, oid_descr) - offsetof(struct sysctl_oid, oid_number)))) {
348 				break;
349 			}
350 		}
351 		if (old_oidp != NULL) {
352 			SLIST_REMOVE(lsp, old_oidp, sysctl_oid, oid_link);
353 			removed_oidp = old_oidp;
354 		}
355 #else
356 		panic("Old style sysctl without a version number isn't supported");
357 #endif
358 	} else {
359 		/* It's a later version; handle the versions we know about */
360 		switch (oidp->oid_version) {
361 		case SYSCTL_OID_VERSION:
362 			/* We can just remove the OID directly... */
363 			SLIST_REMOVE(lsp, oidp, sysctl_oid, oid_link);
364 			removed_oidp = oidp;
365 			break;
366 		default:
367 			/* XXX: Can't happen; probably tree coruption.*/
368 			break;                  /* rejects unknown version */
369 		}
370 	}
371 
372 #if defined(HAS_APPLE_PAC)
373 	if (removed_oidp && removed_oidp->oid_handler) {
374 		/*
375 		 * Revert address-discriminated signing performed by
376 		 * sysctl_register_oid() (in case this oid is registered again).
377 		 */
378 		removed_oidp->oid_handler = ptrauth_auth_and_resign(removed_oidp->oid_handler,
379 		    ptrauth_key_function_pointer,
380 		    ptrauth_blend_discriminator(&removed_oidp->oid_handler,
381 		    os_hash_kernel_pointer(removed_oidp->oid_arg1)),
382 		    ptrauth_key_function_pointer,
383 		    ptrauth_function_pointer_type_discriminator(typeof(removed_oidp->oid_handler)));
384 	}
385 #endif /* defined(HAS_APPLE_PAC) */
386 
387 	/*
388 	 * We've removed it from the list at this point, but we don't want
389 	 * to return to the caller until all handler references have drained
390 	 * out.  Doing things in this order prevent other people coming in
391 	 * and starting new operations against the OID node we want removed.
392 	 *
393 	 * Note:	oidp could be NULL if it wasn't found.
394 	 */
395 	while (removed_oidp && removed_oidp->oid_refcnt) {
396 		lck_rw_sleep(&sysctl_geometry_lock, LCK_SLEEP_EXCLUSIVE,
397 		    &removed_oidp->oid_refcnt, THREAD_UNINT);
398 	}
399 
400 	/* Release the write lock */
401 	lck_rw_unlock_exclusive(&sysctl_geometry_lock);
402 
403 #if __x86_64__
404 	/* If it was allocated, free it after dropping the lock */
405 	kfree_type(struct sysctl_oid, old_oidp);
406 #endif
407 }
408 
409 /*
410  * Exported in BSDKernel.exports, kept for binary compatibility
411  */
412 #if defined(__x86_64__)
413 void
sysctl_register_fixed(void)414 sysctl_register_fixed(void)
415 {
416 }
417 #endif
418 
419 /*
420  * New handler interface
421  *   If the sysctl caller (user mode or kernel mode) is interested in the
422  *   value (req->oldptr != NULL), we copy the data (bigValue etc.) out,
423  *   if the caller wants to set the value (req->newptr), we copy
424  *   the data in (*pValue etc.).
425  */
426 
427 int
sysctl_io_number(struct sysctl_req * req,long long bigValue,size_t valueSize,void * pValue,int * changed)428 sysctl_io_number(struct sysctl_req *req, long long bigValue, size_t valueSize, void *pValue, int *changed)
429 {
430 	int             smallValue;
431 	int             error;
432 
433 	if (changed) {
434 		*changed = 0;
435 	}
436 
437 	/*
438 	 * Handle the various combinations of caller buffer size and
439 	 * data value size.  We are generous in the case where the
440 	 * caller has specified a 32-bit buffer but the value is 64-bit
441 	 * sized.
442 	 */
443 
444 	/* 32 bit value expected or 32 bit buffer offered */
445 	if (((valueSize == sizeof(int)) ||
446 	    ((req->oldlen == sizeof(int)) && (valueSize == sizeof(long long))))
447 	    && (req->oldptr)) {
448 		smallValue = (int)bigValue;
449 		if ((long long)smallValue != bigValue) {
450 			return ERANGE;
451 		}
452 		error = SYSCTL_OUT(req, &smallValue, sizeof(smallValue));
453 	} else {
454 		/* any other case is either size-equal or a bug */
455 		error = SYSCTL_OUT(req, &bigValue, valueSize);
456 	}
457 	/* error or nothing to set */
458 	if (error || !req->newptr) {
459 		return error;
460 	}
461 
462 	/* set request for constant */
463 	if (pValue == NULL) {
464 		return EPERM;
465 	}
466 
467 	/* set request needs to convert? */
468 	if ((req->newlen == sizeof(int)) && (valueSize == sizeof(long long))) {
469 		/* new value is 32 bits, upconvert to 64 bits */
470 		error = SYSCTL_IN(req, &smallValue, sizeof(smallValue));
471 		if (!error) {
472 			*(long long *)pValue = (long long)smallValue;
473 		}
474 	} else if ((req->newlen == sizeof(long long)) && (valueSize == sizeof(int))) {
475 		/* new value is 64 bits, downconvert to 32 bits and range check */
476 		error = SYSCTL_IN(req, &bigValue, sizeof(bigValue));
477 		if (!error) {
478 			smallValue = (int)bigValue;
479 			if ((long long)smallValue != bigValue) {
480 				return ERANGE;
481 			}
482 			*(int *)pValue = smallValue;
483 		}
484 	} else {
485 		/* sizes match, just copy in */
486 		error = SYSCTL_IN(req, pValue, valueSize);
487 	}
488 	if (!error && changed) {
489 		*changed = 1;
490 	}
491 	return error;
492 }
493 
494 int
sysctl_io_string(struct sysctl_req * req,char * pValue,size_t valueSize,int trunc,int * changed)495 sysctl_io_string(struct sysctl_req *req, char *pValue, size_t valueSize, int trunc, int *changed)
496 {
497 	int error;
498 	size_t len = strlen(pValue) + 1;
499 
500 	if (changed) {
501 		*changed = 0;
502 	}
503 
504 	if (trunc && req->oldptr && req->oldlen && (req->oldlen < len)) {
505 		/* If trunc != 0, if you give it a too small (but larger than
506 		 * 0 bytes) buffer, instead of returning ENOMEM, it truncates the
507 		 * returned string to the buffer size.  This preserves the semantics
508 		 * of some library routines implemented via sysctl, which truncate
509 		 * their returned data, rather than simply returning an error. The
510 		 * returned string is always nul (ascii '\0') terminated. */
511 		error = SYSCTL_OUT(req, pValue, req->oldlen - 1);
512 		if (!error) {
513 			char c = '\0';
514 			error = SYSCTL_OUT(req, &c, 1);
515 		}
516 	} else {
517 		/* Copy string out */
518 		error = SYSCTL_OUT(req, pValue, len);
519 	}
520 
521 	/* error or no new value */
522 	if (error || !req->newptr) {
523 		return error;
524 	}
525 
526 	/* attempt to set read-only value */
527 	if (valueSize == 0) {
528 		return EPERM;
529 	}
530 
531 	/* make sure there's room for the new string */
532 	if (req->newlen >= valueSize) {
533 		return EINVAL;
534 	}
535 
536 	/* copy the string in and force nul termination */
537 	error = SYSCTL_IN(req, pValue, req->newlen);
538 	pValue[req->newlen] = '\0';
539 
540 	if (!error && changed) {
541 		*changed = 1;
542 	}
543 	return error;
544 }
545 
546 int
sysctl_io_opaque(struct sysctl_req * req,void * pValue,size_t valueSize,int * changed)547 sysctl_io_opaque(struct sysctl_req *req, void *pValue, size_t valueSize, int *changed)
548 {
549 	int error;
550 
551 	if (changed) {
552 		*changed = 0;
553 	}
554 
555 	/* Copy blob out */
556 	error = SYSCTL_OUT(req, pValue, valueSize);
557 
558 	/* error or nothing to set */
559 	if (error || !req->newptr) {
560 		return error;
561 	}
562 
563 	error = SYSCTL_IN(req, pValue, valueSize);
564 
565 	if (!error && changed) {
566 		*changed = 1;
567 	}
568 	return error;
569 }
570 
571 /*
572  * SYSCTL_OID enumerators
573  *
574  * Because system OIDs are immutable, they are composed of 2 lists hanging from
575  * a first dummy OID_MUTABLE_ANCHOR node that has an immutable list hanging from
576  * its `oid_parent` field and a mutable list hanging from its oid_arg1 one.
577  *
578  * Those enumerators abstract away the implicit merging of those two lists in
579  * two possible order:
580  * - oid_number order (which will interleave both sorted lists)
581  * - system order which will list the immutable list first,
582  *   and the mutable list second.
583  */
584 struct sysctl_oid_iterator {
585 	struct sysctl_oid *a;
586 	struct sysctl_oid *b;
587 };
588 
589 static struct sysctl_oid_iterator
sysctl_oid_iterator_begin(struct sysctl_oid_list * l)590 sysctl_oid_iterator_begin(struct sysctl_oid_list *l)
591 {
592 	struct sysctl_oid_iterator it = { };
593 	struct sysctl_oid *a = SLIST_FIRST(l);
594 
595 	if (a == NULL) {
596 		return it;
597 	}
598 
599 	if (a->oid_number == OID_MUTABLE_ANCHOR) {
600 		it.a = SLIST_NEXT(a, oid_link);
601 		it.b = SLIST_FIRST((struct sysctl_oid_list *)a->oid_arg1);
602 	} else {
603 		it.a = a;
604 	}
605 	return it;
606 }
607 
608 static struct sysctl_oid *
sysctl_oid_iterator_next_num_order(struct sysctl_oid_iterator * it)609 sysctl_oid_iterator_next_num_order(struct sysctl_oid_iterator *it)
610 {
611 	struct sysctl_oid *a = it->a;
612 	struct sysctl_oid *b = it->b;
613 
614 	if (a == NULL && b == NULL) {
615 		return NULL;
616 	}
617 
618 	if (a == NULL) {
619 		it->b = SLIST_NEXT(b, oid_link);
620 		return b;
621 	}
622 
623 	if (b == NULL || a->oid_number <= b->oid_number) {
624 		it->a = SLIST_NEXT(a, oid_link);
625 		return a;
626 	}
627 
628 	it->b = SLIST_NEXT(b, oid_link);
629 	return b;
630 }
631 
632 #define SYSCTL_OID_FOREACH_NUM_ORDER(oidp, l) \
633 	for (struct sysctl_oid_iterator it = sysctl_oid_iterator_begin(l); \
634 	        ((oidp) = sysctl_oid_iterator_next_num_order(&it)); )
635 
636 static struct sysctl_oid *
sysctl_oid_iterator_next_system_order(struct sysctl_oid_iterator * it)637 sysctl_oid_iterator_next_system_order(struct sysctl_oid_iterator *it)
638 {
639 	struct sysctl_oid *a = it->a;
640 	struct sysctl_oid *b = it->b;
641 
642 	if (a) {
643 		it->a = SLIST_NEXT(a, oid_link);
644 		return a;
645 	}
646 
647 	if (b) {
648 		it->b = SLIST_NEXT(b, oid_link);
649 		return b;
650 	}
651 
652 	return NULL;
653 }
654 
655 #define SYSCTL_OID_FOREACH_SYS_ORDER(oidp, l) \
656 	for (struct sysctl_oid_iterator it = sysctl_oid_iterator_begin(l); \
657 	        ((oidp) = sysctl_oid_iterator_next_system_order(&it)); )
658 
659 /*
660  * "Staff-functions"
661  *
662  * These functions implement a presently undocumented interface
663  * used by the sysctl program to walk the tree, and get the type
664  * so it can print the value.
665  * This interface is under work and consideration, and should probably
666  * be killed with a big axe by the first person who can find the time.
667  * (be aware though, that the proper interface isn't as obvious as it
668  * may seem, there are various conflicting requirements.
669  *
670  * {0,0}	printf the entire MIB-tree.
671  * {0,1,...}	return the name of the "..." OID.
672  * {0,2,...}	return the next OID.
673  * {0,3}	return the OID of the name in "new"
674  * {0,4,...}	return the kind & format info for the "..." OID.
675  */
676 
677 /*
678  * sysctl_sysctl_debug_dump_node
679  *
680  * Description:	Dump debug information for a given sysctl_oid_list at the
681  *		given oid depth out to the kernel log, via printf
682  *
683  * Parameters:	l				sysctl_oid_list pointer
684  *		i				current node depth
685  *
686  * Returns:	(void)
687  *
688  * Implicit:	kernel log, modified
689  *
690  * Locks:	Assumes sysctl_geometry_lock is held prior to calling
691  *
692  * Notes:	This function may call itself recursively to resolve Node
693  *		values, which potentially have an inferioer sysctl_oid_list
694  *
695  *		This function is only callable indirectly via the function
696  *		sysctl_sysctl_debug()
697  *
698  * Bugs:	The node depth indentation does not work; this may be an
699  *		artifact of leading space removal by the log daemon itself
700  *		or some intermediate routine.
701  */
702 STATIC void
sysctl_sysctl_debug_dump_node(struct sysctl_oid_list * l,int i)703 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
704 {
705 	struct sysctl_oid *oidp;
706 	struct sysctl_oid_list *lp;
707 	const char *what;
708 
709 	SYSCTL_OID_FOREACH_SYS_ORDER(oidp, l) {
710 		switch (oidp->oid_kind & CTLTYPE) {
711 		case CTLTYPE_NODE:
712 			lp = oidp->oid_arg1;
713 			what = "Node   ";
714 			if (lp && SLIST_FIRST(lp) &&
715 			    SLIST_FIRST(lp)->oid_number == OID_MUTABLE_ANCHOR) {
716 				what = "NodeExt";
717 			} else {
718 			}
719 			break;
720 		case CTLTYPE_INT:
721 			what = "Int    ";
722 			break;
723 		case CTLTYPE_STRING:
724 			what = "String ";
725 			break;
726 		case CTLTYPE_QUAD:
727 			what = "Quad   ";
728 			break;
729 		case CTLTYPE_OPAQUE:
730 			what = "Opaque ";
731 			break;
732 		default:
733 			what = "Unknown";
734 			break;
735 		}
736 
737 		printf("%*s%-3d[%c%c%c%c%c] %s %s\n", i, "", oidp->oid_number,
738 		    oidp->oid_kind & CTLFLAG_LOCKED ? 'L':' ',
739 		    oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
740 		    oidp->oid_kind & CTLFLAG_WR ? 'W':' ',
741 		    oidp->oid_kind & CTLFLAG_PERMANENT ? ' ':'*',
742 		    oidp->oid_handler ? 'h' : ' ',
743 		    what, oidp->oid_name);
744 
745 		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
746 			if (!oidp->oid_handler) {
747 				sysctl_sysctl_debug_dump_node(lp, i + 2);
748 			}
749 		}
750 	}
751 }
752 
753 /*
754  * sysctl_sysctl_debug
755  *
756  * Description:	This function implements the "sysctl.debug" portion of the
757  *		OID space for sysctl.
758  *
759  * OID:		0, 0
760  *
761  * Parameters:	__unused
762  *
763  * Returns:	ENOENT
764  *
765  * Implicit:	kernel log, modified
766  *
767  * Locks:	Acquires and then releases a read lock on the
768  *		sysctl_geometry_lock
769  */
770 STATIC int
sysctl_sysctl_debug(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,__unused struct sysctl_req * req)771 sysctl_sysctl_debug(__unused struct sysctl_oid *oidp, __unused void *arg1,
772     __unused int arg2, __unused struct sysctl_req *req)
773 {
774 	lck_rw_lock_shared(&sysctl_geometry_lock);
775 	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
776 	lck_rw_done(&sysctl_geometry_lock);
777 	return ENOENT;
778 }
779 
780 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_LOCKED,
781     0, 0, sysctl_sysctl_debug, "-", "");
782 
783 /*
784  * sysctl_sysctl_name
785  *
786  * Description:	Convert an OID into a string name; this is used by the user
787  *		space sysctl() command line utility; this is done in a purely
788  *		advisory capacity (e.g. to provide node names for "sysctl -A"
789  *		output).
790  *
791  * OID:		0, 1
792  *
793  * Parameters:	oidp				__unused
794  *		arg1				A pointer to the OID name list
795  *						integer array, beginning at
796  *						adjusted option base 2
797  *		arg2				The number of elements which
798  *						remain in the name array
799  *
800  * Returns:	0				Success
801  *	SYSCTL_OUT:EPERM			Permission denied
802  *	SYSCTL_OUT:EFAULT			Bad user supplied buffer
803  *	SYSCTL_OUT:???				Return value from user function
804  *						for SYSCTL_PROC leaf node
805  *
806  * Implict:	Contents of user request buffer, modified
807  *
808  * Locks:	Acquires and then releases a read lock on the
809  *		sysctl_geometry_lock
810  *
811  * Notes:	SPI (System Programming Interface); this is subject to change
812  *		and may not be relied upon by third party applications; use
813  *		a subprocess to communicate with the "sysctl" command line
814  *		command instead, if you believe you need this functionality.
815  *		Preferrably, use sysctlbyname() instead.
816  *
817  *		Setting of the NULL termination of the output string is
818  *		delayed until after the geometry lock is dropped.  If there
819  *		are no Entries remaining in the OID name list when this
820  *		function is called, it will still write out the termination
821  *		byte.
822  *
823  *		This function differs from other sysctl functions in that
824  *		it can not take an output buffer length of 0 to determine the
825  *		space which will be required.  It is suggested that the buffer
826  *		length be PATH_MAX, and that authors of new sysctl's refrain
827  *		from exceeding this string length.
828  */
829 STATIC int
sysctl_sysctl_name(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)830 sysctl_sysctl_name(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
831     struct sysctl_req *req)
832 {
833 	int *name = (int *) arg1;
834 	u_int namelen = arg2;
835 	int error = 0;
836 	struct sysctl_oid *oid;
837 	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
838 	char tempbuf[10] = {};
839 
840 	lck_rw_lock_shared(&sysctl_geometry_lock);
841 	while (namelen) {
842 		if (!lsp) {
843 			snprintf(tempbuf, sizeof(tempbuf), "%d", *name);
844 			if (req->oldidx) {
845 				error = SYSCTL_OUT(req, ".", 1);
846 			}
847 			if (!error) {
848 				error = SYSCTL_OUT(req, tempbuf, strlen(tempbuf));
849 			}
850 			if (error) {
851 				lck_rw_done(&sysctl_geometry_lock);
852 				return error;
853 			}
854 			namelen--;
855 			name++;
856 			continue;
857 		}
858 		lsp2 = 0;
859 		SYSCTL_OID_FOREACH_NUM_ORDER(oid, lsp) {
860 			if (oid->oid_number != *name) {
861 				continue;
862 			}
863 
864 			if (req->oldidx) {
865 				error = SYSCTL_OUT(req, ".", 1);
866 			}
867 			if (!error) {
868 				error = SYSCTL_OUT(req, oid->oid_name,
869 				    strlen(oid->oid_name));
870 			}
871 			if (error) {
872 				lck_rw_done(&sysctl_geometry_lock);
873 				return error;
874 			}
875 
876 			namelen--;
877 			name++;
878 
879 			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
880 				break;
881 			}
882 
883 			if (oid->oid_handler) {
884 				break;
885 			}
886 
887 			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
888 			break;
889 		}
890 		lsp = lsp2;
891 	}
892 	lck_rw_done(&sysctl_geometry_lock);
893 	return SYSCTL_OUT(req, "", 1);
894 }
895 
896 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_LOCKED, sysctl_sysctl_name, "");
897 
898 /*
899  * sysctl_sysctl_next_ls
900  *
901  * Description:	For a given OID name value, return the next consecutive OID
902  *		name value within the geometry tree
903  *
904  * Parameters:	lsp				The OID list to look in
905  *		name				The OID name to start from
906  *		namelen				The length of the OID name
907  *		next				Pointer to new oid storage to
908  *						fill in
909  *		len				Pointer to receive new OID
910  *						length value of storage written
911  *		level				OID tree depth (used to compute
912  *						len value)
913  *		oidpp				Pointer to OID list entry
914  *						pointer; used to walk the list
915  *						forward across recursion
916  *
917  * Returns:	0				Returning a new entry
918  *		1				End of geometry list reached
919  *
920  * Implicit:	*next				Modified to contain the new OID
921  *		*len				Modified to contain new length
922  *
923  * Locks:	Assumes sysctl_geometry_lock is held prior to calling
924  *
925  * Notes:	This function will not return OID values that have special
926  *		handlers, since we can not tell wheter these handlers consume
927  *		elements from the OID space as parameters.  For this reason,
928  *		we STRONGLY discourage these types of handlers
929  */
930 STATIC int
sysctl_sysctl_next_ls(struct sysctl_oid_list * lsp,int * name,u_int namelen,int * next,int * len,int level,struct sysctl_oid ** oidpp)931 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
932     int *next, int *len, int level, struct sysctl_oid **oidpp)
933 {
934 	struct sysctl_oid *oidp;
935 
936 	*len = level;
937 	SYSCTL_OID_FOREACH_NUM_ORDER(oidp, lsp) {
938 		*next = oidp->oid_number;
939 		*oidpp = oidp;
940 
941 		if (!namelen) {
942 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
943 				return 0;
944 			}
945 			if (oidp->oid_handler) {
946 				/* We really should call the handler here...*/
947 				return 0;
948 			}
949 			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
950 
951 			if (!SLIST_FIRST(lsp)) {
952 				/* This node had no children - skip it! */
953 				continue;
954 			}
955 
956 			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next + 1,
957 			    len, level + 1, oidpp)) {
958 				return 0;
959 			}
960 			goto next;
961 		}
962 
963 		if (oidp->oid_number < *name) {
964 			continue;
965 		}
966 
967 		if (oidp->oid_number > *name) {
968 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
969 				return 0;
970 			}
971 			if (oidp->oid_handler) {
972 				return 0;
973 			}
974 			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
975 			if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1,
976 			    next + 1, len, level + 1, oidpp)) {
977 				return 0;
978 			}
979 			goto next;
980 		}
981 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
982 			continue;
983 		}
984 
985 		if (oidp->oid_handler) {
986 			continue;
987 		}
988 
989 		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
990 		if (!sysctl_sysctl_next_ls(lsp, name + 1, namelen - 1, next + 1,
991 		    len, level + 1, oidpp)) {
992 			return 0;
993 		}
994 next:
995 		namelen = 1;
996 		*len = level;
997 	}
998 	return 1;
999 }
1000 
1001 /*
1002  * sysctl_sysctl_next
1003  *
1004  * Description:	This is an iterator function designed to iterate the oid tree
1005  *		and provide a list of OIDs for use by the user space "sysctl"
1006  *		command line tool
1007  *
1008  * OID:		0, 2
1009  *
1010  * Parameters:	oidp				__unused
1011  *		arg1				Pointer to start OID name
1012  *		arg2				Start OID name length
1013  *		req				Pointer to user request buffer
1014  *
1015  * Returns:	0				Success
1016  *		ENOENT				Reached end of OID space
1017  *	SYSCTL_OUT:EPERM			Permission denied
1018  *	SYSCTL_OUT:EFAULT			Bad user supplied buffer
1019  *	SYSCTL_OUT:???				Return value from user function
1020  *						for SYSCTL_PROC leaf node
1021  *
1022  * Implict:	Contents of user request buffer, modified
1023  *
1024  * Locks:	Acquires and then releases a read lock on the
1025  *		sysctl_geometry_lock
1026  *
1027  * Notes:	SPI (System Programming Interface); this is subject to change
1028  *		and may not be relied upon by third party applications; use
1029  *		a subprocess to communicate with the "sysctl" command line
1030  *		command instead, if you believe you need this functionality.
1031  *		Preferrably, use sysctlbyname() instead.
1032  *
1033  *		This function differs from other sysctl functions in that
1034  *		it can not take an output buffer length of 0 to determine the
1035  *		space which will be required.  It is suggested that the buffer
1036  *		length be PATH_MAX, and that authors of new sysctl's refrain
1037  *		from exceeding this string length.
1038  */
1039 STATIC int
sysctl_sysctl_next(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1040 sysctl_sysctl_next(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
1041     struct sysctl_req *req)
1042 {
1043 	int *name = (int *) arg1;
1044 	u_int namelen = arg2;
1045 	int i, j, error;
1046 	struct sysctl_oid *oid;
1047 	struct sysctl_oid_list *lsp = &sysctl__children;
1048 	int newoid[CTL_MAXNAME] = {};
1049 
1050 	lck_rw_lock_shared(&sysctl_geometry_lock);
1051 	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
1052 	lck_rw_done(&sysctl_geometry_lock);
1053 	if (i) {
1054 		return ENOENT;
1055 	}
1056 	error = SYSCTL_OUT(req, newoid, j * sizeof(int));
1057 	return error;
1058 }
1059 
1060 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_LOCKED, sysctl_sysctl_next, "");
1061 
1062 /*
1063  * name2oid
1064  *
1065  * Description:	Support function for use by sysctl_sysctl_name2oid(); looks
1066  *		up an OID name given a string name.
1067  *
1068  * Parameters:	name				NULL terminated string name
1069  *		oid				Pointer to receive OID name
1070  *		len				Pointer to receive OID length
1071  *						pointer value (see "Notes")
1072  *
1073  * Returns:	0				Success
1074  *		ENOENT				Entry not found
1075  *
1076  * Implicit:	*oid				Modified to contain OID value
1077  *		*len				Modified to contain OID length
1078  *
1079  * Locks:	Assumes sysctl_geometry_lock is held prior to calling
1080  */
1081 STATIC int
name2oid(char * name,int * oid,size_t * len)1082 name2oid(char *name, int *oid, size_t *len)
1083 {
1084 	struct sysctl_oid_iterator it;
1085 	struct sysctl_oid *oidp;
1086 	char *p;
1087 	char i;
1088 
1089 	if (!*name) {
1090 		return ENOENT;
1091 	}
1092 
1093 	p = name + strlen(name) - 1;
1094 	if (*p == '.') {
1095 		*p = '\0';
1096 	}
1097 
1098 	*len = 0;
1099 
1100 	for (p = name; *p && *p != '.'; p++) {
1101 		;
1102 	}
1103 	i = *p;
1104 	if (i == '.') {
1105 		*p = '\0';
1106 	}
1107 
1108 	it = sysctl_oid_iterator_begin(&sysctl__children);
1109 	oidp = sysctl_oid_iterator_next_system_order(&it);
1110 
1111 	while (oidp && *len < CTL_MAXNAME) {
1112 		if (strcmp(name, oidp->oid_name)) {
1113 			oidp = sysctl_oid_iterator_next_system_order(&it);
1114 			continue;
1115 		}
1116 		*oid++ = oidp->oid_number;
1117 		(*len)++;
1118 
1119 		if (i == '\0') {
1120 			return 0;
1121 		}
1122 
1123 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) {
1124 			break;
1125 		}
1126 
1127 		if (oidp->oid_handler) {
1128 			break;
1129 		}
1130 
1131 		it = sysctl_oid_iterator_begin(oidp->oid_arg1);
1132 		oidp = sysctl_oid_iterator_next_system_order(&it);
1133 
1134 		*p = i; /* restore */
1135 		name = p + 1;
1136 		for (p = name; *p && *p != '.'; p++) {
1137 			;
1138 		}
1139 		i = *p;
1140 		if (i == '.') {
1141 			*p = '\0';
1142 		}
1143 	}
1144 	return ENOENT;
1145 }
1146 
1147 /*
1148  * sysctl_sysctl_name2oid
1149  *
1150  * Description:	Translate a string name to an OID name value; this is used by
1151  *		the sysctlbyname() function as well as by the "sysctl" command
1152  *		line command.
1153  *
1154  * OID:		0, 3
1155  *
1156  * Parameters:	oidp				__unused
1157  *		arg1				__unused
1158  *		arg2				__unused
1159  *		req				Request structure
1160  *
1161  * Returns:	ENOENT				Input length too short
1162  *		ENAMETOOLONG			Input length too long
1163  *		ENOMEM				Could not allocate work area
1164  *	SYSCTL_IN/OUT:EPERM			Permission denied
1165  *	SYSCTL_IN/OUT:EFAULT			Bad user supplied buffer
1166  *	SYSCTL_IN/OUT:???			Return value from user function
1167  *	name2oid:ENOENT				Not found
1168  *
1169  * Implicit:	*req				Contents of request, modified
1170  *
1171  * Locks:	Acquires and then releases a read lock on the
1172  *		sysctl_geometry_lock
1173  *
1174  * Notes:	SPI (System Programming Interface); this is subject to change
1175  *		and may not be relied upon by third party applications; use
1176  *		a subprocess to communicate with the "sysctl" command line
1177  *		command instead, if you believe you need this functionality.
1178  *		Preferrably, use sysctlbyname() instead.
1179  *
1180  *		This function differs from other sysctl functions in that
1181  *		it can not take an output buffer length of 0 to determine the
1182  *		space which will be required.  It is suggested that the buffer
1183  *		length be PATH_MAX, and that authors of new sysctl's refrain
1184  *		from exceeding this string length.
1185  */
1186 STATIC int
sysctl_sysctl_name2oid(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1187 sysctl_sysctl_name2oid(__unused struct sysctl_oid *oidp, __unused void *arg1,
1188     __unused int arg2, struct sysctl_req *req)
1189 {
1190 	char *p;
1191 	int error, oid[CTL_MAXNAME] = {};
1192 	size_t len = 0;          /* set by name2oid() */
1193 
1194 	if (req->newlen < 1) {
1195 		return ENOENT;
1196 	}
1197 	if (req->newlen >= MAXPATHLEN) { /* XXX arbitrary, undocumented */
1198 		return ENAMETOOLONG;
1199 	}
1200 
1201 	p = (char *)kalloc_data(req->newlen + 1, Z_WAITOK);
1202 	if (!p) {
1203 		return ENOMEM;
1204 	}
1205 
1206 	error = SYSCTL_IN(req, p, req->newlen);
1207 	if (error) {
1208 		kfree_data(p, req->newlen + 1);
1209 		return error;
1210 	}
1211 
1212 	p[req->newlen] = '\0';
1213 
1214 	/*
1215 	 * Note:	We acquire and release the geometry lock here to
1216 	 *		avoid making name2oid needlessly complex.
1217 	 */
1218 	lck_rw_lock_shared(&sysctl_geometry_lock);
1219 	error = name2oid(p, oid, &len);
1220 	lck_rw_done(&sysctl_geometry_lock);
1221 
1222 	kfree_data(p, req->newlen + 1);
1223 
1224 	if (error) {
1225 		return error;
1226 	}
1227 
1228 	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1229 	return error;
1230 }
1231 
1232 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_KERN | CTLFLAG_LOCKED, 0, 0,
1233     sysctl_sysctl_name2oid, "I", "");
1234 
1235 /*
1236  * sysctl_sysctl_oidfmt
1237  *
1238  * Description:	For a given OID name, determine the format of the data which
1239  *		is associated with it.  This is used by the "sysctl" command
1240  *		line command.
1241  *
1242  * OID:		0, 4
1243  *
1244  * Parameters:	oidp				__unused
1245  *		arg1				The OID name to look up
1246  *		arg2				The length of the OID name
1247  *		req				Pointer to user request buffer
1248  *
1249  * Returns:	0				Success
1250  *		EISDIR				Malformed request
1251  *		ENOENT				No such OID name
1252  *	SYSCTL_OUT:EPERM			Permission denied
1253  *	SYSCTL_OUT:EFAULT			Bad user supplied buffer
1254  *	SYSCTL_OUT:???				Return value from user function
1255  *
1256  * Implict:	Contents of user request buffer, modified
1257  *
1258  * Locks:	Acquires and then releases a read lock on the
1259  *		sysctl_geometry_lock
1260  *
1261  * Notes:	SPI (System Programming Interface); this is subject to change
1262  *		and may not be relied upon by third party applications; use
1263  *		a subprocess to communicate with the "sysctl" command line
1264  *		command instead, if you believe you need this functionality.
1265  *
1266  *		This function differs from other sysctl functions in that
1267  *		it can not take an output buffer length of 0 to determine the
1268  *		space which will be required.  It is suggested that the buffer
1269  *		length be PATH_MAX, and that authors of new sysctl's refrain
1270  *		from exceeding this string length.
1271  */
1272 STATIC int
sysctl_sysctl_oidfmt(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1273 sysctl_sysctl_oidfmt(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
1274     struct sysctl_req *req)
1275 {
1276 	int *name = (int *) arg1;
1277 	int error = ENOENT;             /* default error: not found */
1278 	u_int namelen = arg2;
1279 	u_int indx;
1280 	struct sysctl_oid_iterator it;
1281 	struct sysctl_oid *oid;
1282 
1283 	lck_rw_lock_shared(&sysctl_geometry_lock);
1284 
1285 	it = sysctl_oid_iterator_begin(&sysctl__children);
1286 	oid = sysctl_oid_iterator_next_system_order(&it);
1287 
1288 	indx = 0;
1289 	while (oid && indx < CTL_MAXNAME) {
1290 		if (oid->oid_number == name[indx]) {
1291 			indx++;
1292 			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1293 				if (oid->oid_handler) {
1294 					goto found;
1295 				}
1296 				if (indx == namelen) {
1297 					goto found;
1298 				}
1299 				it = sysctl_oid_iterator_begin(oid->oid_arg1);
1300 				oid = sysctl_oid_iterator_next_system_order(&it);
1301 			} else {
1302 				if (indx != namelen) {
1303 					error =  EISDIR;
1304 					goto err;
1305 				}
1306 				goto found;
1307 			}
1308 		} else {
1309 			oid = sysctl_oid_iterator_next_system_order(&it);
1310 		}
1311 	}
1312 	/* Not found */
1313 	goto err;
1314 
1315 found:
1316 	if (!oid->oid_fmt) {
1317 		goto err;
1318 	}
1319 	error = SYSCTL_OUT(req,
1320 	    &oid->oid_kind, sizeof(oid->oid_kind));
1321 	if (!error) {
1322 		error = SYSCTL_OUT(req, oid->oid_fmt,
1323 		    strlen(oid->oid_fmt) + 1);
1324 	}
1325 err:
1326 	lck_rw_done(&sysctl_geometry_lock);
1327 	return error;
1328 }
1329 
1330 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD | CTLFLAG_LOCKED, sysctl_sysctl_oidfmt, "");
1331 
1332 
1333 /*
1334  * Default "handler" functions.
1335  */
1336 
1337 /*
1338  * Handle an int, signed or unsigned.
1339  * Two cases:
1340  *     a variable:  point arg1 at it.
1341  *     a constant:  pass it in arg2.
1342  */
1343 
1344 int
sysctl_handle_int(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1345 sysctl_handle_int(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
1346     struct sysctl_req *req)
1347 {
1348 	return sysctl_io_number(req, arg1? *(int*)arg1: arg2, sizeof(int), arg1, NULL);
1349 }
1350 
1351 /*
1352  * Handle a long, signed or unsigned.  arg1 points to it.
1353  */
1354 
1355 int
sysctl_handle_long(__unused struct sysctl_oid * oidp,void * arg1,__unused int arg2,struct sysctl_req * req)1356 sysctl_handle_long(__unused struct sysctl_oid *oidp, void *arg1,
1357     __unused int arg2, struct sysctl_req *req)
1358 {
1359 	if (!arg1) {
1360 		return EINVAL;
1361 	}
1362 	return sysctl_io_number(req, *(long*)arg1, sizeof(long), arg1, NULL);
1363 }
1364 
1365 /*
1366  * Handle a quad, signed or unsigned.  arg1 points to it.
1367  */
1368 
1369 int
sysctl_handle_quad(__unused struct sysctl_oid * oidp,void * arg1,__unused int arg2,struct sysctl_req * req)1370 sysctl_handle_quad(__unused struct sysctl_oid *oidp, void *arg1,
1371     __unused int arg2, struct sysctl_req *req)
1372 {
1373 	if (!arg1) {
1374 		return EINVAL;
1375 	}
1376 	return sysctl_io_number(req, *(long long*)arg1, sizeof(long long), arg1, NULL);
1377 }
1378 
1379 /*
1380  * Expose an int value as a quad.
1381  *
1382  * This interface allows us to support interfaces defined
1383  * as using quad values while the implementation is still
1384  * using ints.
1385  */
1386 int
sysctl_handle_int2quad(__unused struct sysctl_oid * oidp,void * arg1,__unused int arg2,struct sysctl_req * req)1387 sysctl_handle_int2quad(__unused struct sysctl_oid *oidp, void *arg1,
1388     __unused int arg2, struct sysctl_req *req)
1389 {
1390 	int error = 0;
1391 	long long val;
1392 	int newval;
1393 
1394 	if (!arg1) {
1395 		return EINVAL;
1396 	}
1397 	val = (long long)*(int *)arg1;
1398 	error = SYSCTL_OUT(req, &val, sizeof(long long));
1399 
1400 	if (error || !req->newptr) {
1401 		return error;
1402 	}
1403 
1404 	error = SYSCTL_IN(req, &val, sizeof(long long));
1405 	if (!error) {
1406 		/*
1407 		 * Value must be representable; check by
1408 		 * casting and then casting back.
1409 		 */
1410 		newval = (int)val;
1411 		if ((long long)newval != val) {
1412 			error = ERANGE;
1413 		} else {
1414 			*(int *)arg1 = newval;
1415 		}
1416 	}
1417 	return error;
1418 }
1419 
1420 /*
1421  * Handle our generic '\0' terminated 'C' string.
1422  * Two cases:
1423  *      a variable string:  point arg1 at it, arg2 is max length.
1424  *      a constant string:  point arg1 at it, arg2 is zero.
1425  */
1426 
1427 int
sysctl_handle_string(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1428 sysctl_handle_string( __unused struct sysctl_oid *oidp, void *arg1, int arg2,
1429     struct sysctl_req *req)
1430 {
1431 	return sysctl_io_string(req, arg1, arg2, 0, NULL);
1432 }
1433 
1434 /*
1435  * Handle any kind of opaque data.
1436  * arg1 points to it, arg2 is the size.
1437  */
1438 
1439 int
sysctl_handle_opaque(__unused struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)1440 sysctl_handle_opaque(__unused struct sysctl_oid *oidp, void *arg1, int arg2,
1441     struct sysctl_req *req)
1442 {
1443 	return sysctl_io_opaque(req, arg1, arg2, NULL);
1444 }
1445 
1446 /*
1447  * Transfer functions to/from kernel space.
1448  */
1449 STATIC int
sysctl_old_kernel(struct sysctl_req * req,const void * p,size_t l)1450 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
1451 {
1452 	size_t i = 0;
1453 
1454 	if (req->oldptr) {
1455 		i = l;
1456 		if (i > req->oldlen - req->oldidx) {
1457 			i = req->oldlen - req->oldidx;
1458 		}
1459 		if (i > 0) {
1460 			bcopy((const void*)p, CAST_DOWN(char *, (req->oldptr + req->oldidx)), i);
1461 		}
1462 	}
1463 	req->oldidx += l;
1464 	if (req->oldptr && i != l) {
1465 		return ENOMEM;
1466 	}
1467 	return 0;
1468 }
1469 
1470 STATIC int
sysctl_new_kernel(struct sysctl_req * req,void * p,size_t l)1471 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
1472 {
1473 	if (!req->newptr) {
1474 		return 0;
1475 	}
1476 	if (req->newlen - req->newidx < l) {
1477 		return EINVAL;
1478 	}
1479 	bcopy(CAST_DOWN(char *, (req->newptr + req->newidx)), p, l);
1480 	req->newidx += l;
1481 	return 0;
1482 }
1483 
1484 int
kernel_sysctl(struct proc * p,int * name,size_t namelen,void * old,size_t * oldlenp,void * new,size_t newlen)1485 kernel_sysctl(struct proc *p, int *name, size_t namelen, void *old, size_t *oldlenp, void *new, size_t newlen)
1486 {
1487 	int error = 0;
1488 	struct sysctl_req req;
1489 
1490 	/*
1491 	 * Construct request.
1492 	 */
1493 	bzero(&req, sizeof req);
1494 	req.p = p;
1495 	if (oldlenp) {
1496 		req.oldlen = *oldlenp;
1497 	}
1498 	if (old) {
1499 		req.oldptr = CAST_USER_ADDR_T(old);
1500 	}
1501 	if (newlen) {
1502 		req.newlen = newlen;
1503 		req.newptr = CAST_USER_ADDR_T(new);
1504 	}
1505 	req.oldfunc = sysctl_old_kernel;
1506 	req.newfunc = sysctl_new_kernel;
1507 	req.lock = 1;
1508 
1509 	/* make the request */
1510 	error = sysctl_root(TRUE, FALSE, NULL, 0, name, namelen, &req);
1511 
1512 	if (error && error != ENOMEM) {
1513 		return error;
1514 	}
1515 
1516 	if (oldlenp) {
1517 		*oldlenp = req.oldidx;
1518 	}
1519 
1520 	return error;
1521 }
1522 
1523 /*
1524  * Transfer function to/from user space.
1525  */
1526 STATIC int
sysctl_old_user(struct sysctl_req * req,const void * p,size_t l)1527 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1528 {
1529 	int error = 0;
1530 	size_t i = 0;
1531 
1532 	if (req->oldptr) {
1533 		if (req->oldlen - req->oldidx < l) {
1534 			return ENOMEM;
1535 		}
1536 		i = l;
1537 		if (i > req->oldlen - req->oldidx) {
1538 			i = req->oldlen - req->oldidx;
1539 		}
1540 		if (i > 0) {
1541 			error = copyout((const void*)p, (req->oldptr + req->oldidx), i);
1542 		}
1543 	}
1544 	req->oldidx += l;
1545 	if (error) {
1546 		return error;
1547 	}
1548 	if (req->oldptr && i < l) {
1549 		return ENOMEM;
1550 	}
1551 	return 0;
1552 }
1553 
1554 STATIC int
sysctl_new_user(struct sysctl_req * req,void * p,size_t l)1555 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1556 {
1557 	int error;
1558 
1559 	if (!req->newptr) {
1560 		return 0;
1561 	}
1562 	if (req->newlen - req->newidx < l) {
1563 		return EINVAL;
1564 	}
1565 	error = copyin((req->newptr + req->newidx), p, l);
1566 	req->newidx += l;
1567 	return error;
1568 }
1569 
1570 #define WRITE_EXPERIMENT_FACTORS_ENTITLEMENT "com.apple.private.write-kr-experiment-factors"
1571 /*
1572  * Is the current task allowed to write to experiment factors?
1573  * tasks with the WRITE_EXPERIMENT_FACTORS_ENTITLEMENT are always allowed to write these.
1574  * In the development / debug kernel we also allow root to write them.
1575  */
1576 STATIC bool
can_write_experiment_factors(__unused struct sysctl_req * req)1577 can_write_experiment_factors(__unused struct sysctl_req *req)
1578 {
1579 	if (IOCurrentTaskHasEntitlement(WRITE_EXPERIMENT_FACTORS_ENTITLEMENT)) {
1580 		return true;
1581 	}
1582 #if DEBUG || DEVELOPMENT
1583 	return !proc_suser(req->p);
1584 #else
1585 	return false;
1586 #endif /* DEBUG || DEVELOPMENT */
1587 }
1588 
1589 /*
1590  * Traverse our tree, and find the right node, execute whatever it points
1591  * at, and return the resulting error code.
1592  */
1593 
1594 int
sysctl_root(boolean_t from_kernel,boolean_t string_is_canonical,char * namestring,size_t namestringlen,int * name,size_t namelen,struct sysctl_req * req)1595 sysctl_root(boolean_t from_kernel, boolean_t string_is_canonical,
1596     char *namestring, size_t namestringlen,
1597     int *name, size_t namelen, struct sysctl_req *req)
1598 {
1599 	u_int indx;
1600 	int i;
1601 	struct sysctl_oid_iterator it;
1602 	struct sysctl_oid *oid;
1603 	sysctl_handler_t oid_handler = NULL;
1604 	int error;
1605 	boolean_t unlocked_node_found = FALSE;
1606 	boolean_t namestring_started = FALSE;
1607 
1608 	/* Get the read lock on the geometry */
1609 	lck_rw_lock_shared(&sysctl_geometry_lock);
1610 
1611 	if (string_is_canonical) {
1612 		/* namestring is actually canonical, name/namelen needs to be populated */
1613 		error = name2oid(namestring, name, &namelen);
1614 		if (error) {
1615 			goto err;
1616 		}
1617 	}
1618 
1619 	it = sysctl_oid_iterator_begin(&sysctl__children);
1620 	oid = sysctl_oid_iterator_next_system_order(&it);
1621 
1622 	indx = 0;
1623 	while (oid && indx < CTL_MAXNAME) {
1624 		if (oid->oid_number == name[indx]) {
1625 			if (!from_kernel && !string_is_canonical) {
1626 				if (namestring_started) {
1627 					if (strlcat(namestring, ".", namestringlen) >= namestringlen) {
1628 						error = ENAMETOOLONG;
1629 						goto err;
1630 					}
1631 				}
1632 
1633 				if (strlcat(namestring, oid->oid_name, namestringlen) >= namestringlen) {
1634 					error = ENAMETOOLONG;
1635 					goto err;
1636 				}
1637 				namestring_started = TRUE;
1638 			}
1639 
1640 			indx++;
1641 			if (!(oid->oid_kind & CTLFLAG_LOCKED)) {
1642 				unlocked_node_found = TRUE;
1643 			}
1644 			if (oid->oid_kind & CTLFLAG_NOLOCK) {
1645 				req->lock = 0;
1646 			}
1647 			/*
1648 			 * For SYSCTL_PROC() functions which are for sysctl's
1649 			 * which have parameters at the end of their OID
1650 			 * space, you need to OR CTLTYPE_NODE into their
1651 			 * access value.
1652 			 *
1653 			 * NOTE: For binary backward compatibility ONLY! Do
1654 			 * NOT add new sysctl's that do this!  Existing
1655 			 * sysctl's which do this will eventually have
1656 			 * compatibility code in user space, and this method
1657 			 * will become unsupported.
1658 			 */
1659 			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1660 				if (oid->oid_handler) {
1661 					goto found;
1662 				}
1663 				if (indx == namelen) {
1664 					error = ENOENT;
1665 					goto err;
1666 				}
1667 
1668 				it = sysctl_oid_iterator_begin(oid->oid_arg1);
1669 				oid = sysctl_oid_iterator_next_system_order(&it);
1670 			} else {
1671 				if (indx != namelen) {
1672 					error = EISDIR;
1673 					goto err;
1674 				}
1675 				goto found;
1676 			}
1677 		} else {
1678 			oid = sysctl_oid_iterator_next_system_order(&it);
1679 		}
1680 	}
1681 	error = ENOENT;
1682 	goto err;
1683 found:
1684 
1685 	/*
1686 	 * indx is the index of the first remaining OID name,
1687 	 * for sysctls that take them as arguments
1688 	 */
1689 	if (!from_kernel && !string_is_canonical && (indx < namelen)) {
1690 		char tempbuf[10];
1691 		u_int indx2;
1692 
1693 		for (indx2 = indx; indx2 < namelen; indx2++) {
1694 			snprintf(tempbuf, sizeof(tempbuf), "%d", name[indx2]);
1695 
1696 			if (namestring_started) {
1697 				if (strlcat(namestring, ".", namestringlen) >= namestringlen) {
1698 					error = ENAMETOOLONG;
1699 					goto err;
1700 				}
1701 			}
1702 
1703 			if (strlcat(namestring, tempbuf, namestringlen) >= namestringlen) {
1704 				error = ENAMETOOLONG;
1705 				goto err;
1706 			}
1707 			namestring_started = TRUE;
1708 		}
1709 	}
1710 
1711 	/* If writing isn't allowed */
1712 	if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1713 	    ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0))) {
1714 		error = (EPERM);
1715 		goto err;
1716 	}
1717 
1718 	/*
1719 	 * If we're inside the kernel, the OID must be marked as kernel-valid.
1720 	 */
1721 	if (from_kernel && !(oid->oid_kind & CTLFLAG_KERN)) {
1722 		error = (EPERM);
1723 		goto err;
1724 	}
1725 
1726 	if (req->newptr && req->p) {
1727 		if (oid->oid_kind & CTLFLAG_EXPERIMENT) {
1728 			/*
1729 			 * Experiment factors have different permissions since they need to be
1730 			 * writable by procs with WRITE_EXPERIMENT_FACTORS_ENTITLEMENT.
1731 			 */
1732 			if (!can_write_experiment_factors(req)) {
1733 				error = (EPERM);
1734 				goto err;
1735 			}
1736 		} else {
1737 			/*
1738 			 * This is where legacy enforcement of permissions occurs.  If the
1739 			 * flag does not say CTLFLAG_ANYBODY, then we prohibit anyone but
1740 			 * root from writing new values down.  If local enforcement happens
1741 			 * at the leaf node, then it needs to be set as CTLFLAG_ANYBODY.  In
1742 			 * addition, if the leaf node is set this way, then in order to do
1743 			 * specific enforcement, it has to be of type SYSCTL_PROC.
1744 			 */
1745 			if (!(oid->oid_kind & CTLFLAG_ANYBODY) &&
1746 			    (error = proc_suser(req->p))) {
1747 				goto err;
1748 			}
1749 		}
1750 	}
1751 
1752 	/*
1753 	 * sysctl_unregister_oid() may change the handler value, so grab it
1754 	 * under the lock.
1755 	 */
1756 	oid_handler = oid->oid_handler;
1757 	if (!oid_handler) {
1758 		error = EINVAL;
1759 		goto err;
1760 	}
1761 
1762 	/*
1763 	 * Reference the OID and drop the geometry lock; this prevents the
1764 	 * OID from being deleted out from under the handler call, but does
1765 	 * not prevent other calls into handlers or calls to manage the
1766 	 * geometry elsewhere from blocking...
1767 	 */
1768 	if ((oid->oid_kind & CTLFLAG_PERMANENT) == 0) {
1769 		OSAddAtomic(1, &oid->oid_refcnt);
1770 	}
1771 
1772 	lck_rw_done(&sysctl_geometry_lock);
1773 
1774 #if CONFIG_MACF
1775 	if (!from_kernel) {
1776 		error = mac_system_check_sysctlbyname(kauth_cred_get(),
1777 		    namestring,
1778 		    name,
1779 		    namelen,
1780 		    req->oldptr,
1781 		    req->oldlen,
1782 		    req->newptr,
1783 		    req->newlen);
1784 		if (error) {
1785 			goto dropref;
1786 		}
1787 	}
1788 #endif
1789 
1790 	/*
1791 	 * ...however, we still have to grab the mutex for those calls which
1792 	 * may be into code whose reentrancy is protected by it.
1793 	 */
1794 	if (unlocked_node_found) {
1795 		lck_mtx_lock(&sysctl_unlocked_node_lock);
1796 	}
1797 
1798 #if defined(HAS_APPLE_PAC)
1799 	/*
1800 	 * oid_handler is signed address-discriminated by sysctl_register_oid().
1801 	 */
1802 	oid_handler = ptrauth_auth_and_resign(oid_handler,
1803 	    ptrauth_key_function_pointer,
1804 	    ptrauth_blend_discriminator(&oid->oid_handler,
1805 	    os_hash_kernel_pointer(oid->oid_arg1)),
1806 	    ptrauth_key_function_pointer,
1807 	    ptrauth_function_pointer_type_discriminator(typeof(oid_handler)));
1808 #endif /* defined(HAS_APPLE_PAC) */
1809 
1810 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1811 		i = oid_handler(oid, name + indx, (int)(namelen - indx), req);
1812 	} else {
1813 		i = oid_handler(oid, oid->oid_arg1, oid->oid_arg2, req);
1814 	}
1815 	error = i;
1816 
1817 	if (unlocked_node_found) {
1818 		lck_mtx_unlock(&sysctl_unlocked_node_lock);
1819 	}
1820 
1821 #if CONFIG_MACF
1822 	/* only used from another CONFIG_MACF block */
1823 dropref:
1824 #endif
1825 
1826 	/*
1827 	 * This is tricky... we re-grab the geometry lock in order to drop
1828 	 * the reference and wake on the address; since the geometry
1829 	 * lock is a reader/writer lock rather than a mutex, we have to
1830 	 * wake on all apparent 1->0 transitions.  This abuses the drop
1831 	 * after the reference decrement in order to wake any lck_rw_sleep()
1832 	 * in progress in sysctl_unregister_oid() that slept because of a
1833 	 * non-zero reference count.
1834 	 *
1835 	 * Note:	OSAddAtomic() is defined to return the previous value;
1836 	 *		we use this and the fact that the lock itself is a
1837 	 *		barrier to avoid waking every time through on "hot"
1838 	 *		OIDs.
1839 	 */
1840 	lck_rw_lock_shared(&sysctl_geometry_lock);
1841 
1842 	if ((oid->oid_kind & CTLFLAG_PERMANENT) == 0) {
1843 		if (OSAddAtomic(-1, &oid->oid_refcnt) == 1) {
1844 			wakeup(&oid->oid_refcnt);
1845 		}
1846 	}
1847 
1848 err:
1849 	lck_rw_done(&sysctl_geometry_lock);
1850 	return error;
1851 }
1852 
1853 void
sysctl_create_user_req(struct sysctl_req * req,struct proc * p,user_addr_t oldp,size_t oldlen,user_addr_t newp,size_t newlen)1854 sysctl_create_user_req(struct sysctl_req *req, struct proc *p, user_addr_t oldp,
1855     size_t oldlen, user_addr_t newp, size_t newlen)
1856 {
1857 	bzero(req, sizeof(*req));
1858 
1859 	req->p = p;
1860 
1861 	req->oldlen = oldlen;
1862 	req->oldptr = oldp;
1863 
1864 	if (newlen) {
1865 		req->newlen = newlen;
1866 		req->newptr = newp;
1867 	}
1868 
1869 	req->oldfunc = sysctl_old_user;
1870 	req->newfunc = sysctl_new_user;
1871 	req->lock = 1;
1872 
1873 	return;
1874 }
1875 
1876 int
sysctl(proc_t p,struct sysctl_args * uap,__unused int32_t * retval)1877 sysctl(proc_t p, struct sysctl_args *uap, __unused int32_t *retval)
1878 {
1879 	int error, new_error;
1880 	size_t oldlen = 0, newlen;
1881 	int name[CTL_MAXNAME];
1882 	struct sysctl_req req;
1883 	char *namestring;
1884 	size_t namestringlen = MAXPATHLEN;
1885 
1886 	/*
1887 	 * all top-level sysctl names are non-terminal
1888 	 */
1889 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) {
1890 		return EINVAL;
1891 	}
1892 	error = copyin(uap->name, &name[0], uap->namelen * sizeof(int));
1893 	if (error) {
1894 		return error;
1895 	}
1896 
1897 	AUDIT_ARG(ctlname, name, uap->namelen);
1898 
1899 	if (uap->newlen > SIZE_T_MAX) {
1900 		return EINVAL;
1901 	}
1902 	newlen = (size_t)uap->newlen;
1903 
1904 	if (uap->oldlenp != USER_ADDR_NULL) {
1905 		uint64_t        oldlen64 = fuulong(uap->oldlenp);
1906 
1907 		/*
1908 		 * If more than 4G, clamp to 4G
1909 		 */
1910 		if (oldlen64 > SIZE_T_MAX) {
1911 			oldlen = SIZE_T_MAX;
1912 		} else {
1913 			oldlen = (size_t)oldlen64;
1914 		}
1915 	}
1916 
1917 	sysctl_create_user_req(&req, p, uap->old, oldlen, uap->new, newlen);
1918 
1919 	/* Guess that longest length for the passed-in MIB, if we can be more aggressive than MAXPATHLEN */
1920 	if (uap->namelen == 2) {
1921 		if (name[0] == CTL_KERN && name[1] < KERN_MAXID) {
1922 			namestringlen = 32; /* "kern.speculative_reads_disabled" */
1923 		} else if (name[0] == CTL_HW && name[1] < HW_MAXID) {
1924 			namestringlen = 32; /* "hw.cachelinesize_compat" */
1925 		}
1926 	}
1927 
1928 	namestring = (char *)kalloc_data(namestringlen, Z_WAITOK);
1929 	if (!namestring) {
1930 		oldlen = 0;
1931 		goto err;
1932 	}
1933 
1934 	error = userland_sysctl(FALSE, namestring, namestringlen, name, uap->namelen, &req, &oldlen);
1935 
1936 	kfree_data(namestring, namestringlen);
1937 
1938 	if ((error) && (error != ENOMEM)) {
1939 		return error;
1940 	}
1941 
1942 err:
1943 	if (uap->oldlenp != USER_ADDR_NULL) {
1944 		/*
1945 		 * Only overwrite the old error value on a new error
1946 		 */
1947 		new_error = suulong(uap->oldlenp, oldlen);
1948 
1949 		if (new_error) {
1950 			error = new_error;
1951 		}
1952 	}
1953 
1954 	return error;
1955 }
1956 
1957 // sysctlbyname is also exported as KPI to kexts
1958 // and the syscall name cannot conflict with it
1959 int
sys_sysctlbyname(proc_t p,struct sysctlbyname_args * uap,__unused int32_t * retval)1960 sys_sysctlbyname(proc_t p, struct sysctlbyname_args *uap, __unused int32_t *retval)
1961 {
1962 	int error, new_error;
1963 	size_t oldlen = 0, newlen;
1964 	char *name;
1965 	size_t namelen = 0;
1966 	struct sysctl_req req;
1967 	int oid[CTL_MAXNAME];
1968 
1969 	if (uap->namelen >= MAXPATHLEN) { /* XXX arbitrary, undocumented */
1970 		return ENAMETOOLONG;
1971 	}
1972 	namelen = (size_t)uap->namelen;
1973 
1974 	name = (char *)kalloc_data(namelen + 1, Z_WAITOK);
1975 	if (!name) {
1976 		return ENOMEM;
1977 	}
1978 
1979 	error = copyin(uap->name, name, namelen);
1980 	if (error) {
1981 		kfree_data(name, namelen + 1);
1982 		return error;
1983 	}
1984 	name[namelen] = '\0';
1985 
1986 	/* XXX
1987 	 * AUDIT_ARG(ctlname, name, uap->namelen);
1988 	 */
1989 
1990 	if (uap->newlen > SIZE_T_MAX) {
1991 		kfree_data(name, namelen + 1);
1992 		return EINVAL;
1993 	}
1994 	newlen = (size_t)uap->newlen;
1995 
1996 	if (uap->oldlenp != USER_ADDR_NULL) {
1997 		uint64_t        oldlen64 = fuulong(uap->oldlenp);
1998 
1999 		/*
2000 		 * If more than 4G, clamp to 4G
2001 		 */
2002 		if (oldlen64 > SIZE_T_MAX) {
2003 			oldlen = SIZE_T_MAX;
2004 		} else {
2005 			oldlen = (size_t)oldlen64;
2006 		}
2007 	}
2008 
2009 	sysctl_create_user_req(&req, p, uap->old, oldlen, uap->new, newlen);
2010 
2011 	error = userland_sysctl(TRUE, name, namelen + 1, oid, CTL_MAXNAME, &req, &oldlen);
2012 
2013 	kfree_data(name, namelen + 1);
2014 
2015 	if ((error) && (error != ENOMEM)) {
2016 		return error;
2017 	}
2018 
2019 	if (uap->oldlenp != USER_ADDR_NULL) {
2020 		/*
2021 		 * Only overwrite the old error value on a new error
2022 		 */
2023 		new_error = suulong(uap->oldlenp, oldlen);
2024 
2025 		if (new_error) {
2026 			error = new_error;
2027 		}
2028 	}
2029 
2030 	return error;
2031 }
2032 
2033 /*
2034  * This is used from various compatibility syscalls too.  That's why name
2035  * must be in kernel space.
2036  */
2037 int
userland_sysctl(boolean_t string_is_canonical,char * namestring,size_t namestringlen,int * name,u_int namelen,struct sysctl_req * req,size_t * retval)2038 userland_sysctl(boolean_t string_is_canonical,
2039     char *namestring, size_t namestringlen,
2040     int *name, u_int namelen, struct sysctl_req *req,
2041     size_t *retval)
2042 {
2043 	int error = 0;
2044 	struct sysctl_req req2;
2045 
2046 	do {
2047 		/* if EAGAIN, reset output cursor */
2048 		req2 = *req;
2049 		if (!string_is_canonical) {
2050 			namestring[0] = '\0';
2051 		}
2052 
2053 		error = sysctl_root(FALSE, string_is_canonical, namestring, namestringlen, name, namelen, &req2);
2054 	} while (error == EAGAIN);
2055 
2056 	if (error && error != ENOMEM) {
2057 		return error;
2058 	}
2059 
2060 	if (retval) {
2061 		if (req2.oldptr && req2.oldidx > req2.oldlen) {
2062 			*retval = req2.oldlen;
2063 		} else {
2064 			*retval = req2.oldidx;
2065 		}
2066 	}
2067 	return error;
2068 }
2069 
2070 /*
2071  * Kernel versions of the userland sysctl helper functions.
2072  *
2073  * These allow sysctl to be used in the same fashion in both
2074  * userland and the kernel.
2075  *
2076  * Note that some sysctl handlers use copyin/copyout, which
2077  * may not work correctly.
2078  *
2079  * The "sysctlbyname" KPI for use by kexts is aliased to this function.
2080  */
2081 
2082 int
kernel_sysctlbyname(const char * name,void * oldp,size_t * oldlenp,void * newp,size_t newlen)2083 kernel_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
2084 {
2085 	int oid[CTL_MAXNAME];
2086 	int name2mib_oid[2];
2087 	int error;
2088 	size_t oidlen;
2089 
2090 	/* look up the OID with magic service node */
2091 	name2mib_oid[0] = 0;
2092 	name2mib_oid[1] = 3;
2093 
2094 	oidlen = sizeof(oid);
2095 	error = kernel_sysctl(current_proc(), name2mib_oid, 2, oid, &oidlen, __DECONST(void *, name), strlen(name));
2096 	oidlen /= sizeof(int);
2097 	if (oidlen > UINT_MAX) {
2098 		error = EDOM;
2099 	}
2100 
2101 	/* now use the OID */
2102 	if (error == 0) {
2103 		error = kernel_sysctl(current_proc(), oid, (u_int)oidlen, oldp, oldlenp, newp, newlen);
2104 	}
2105 	return error;
2106 }
2107 
2108 int
2109 scalable_counter_sysctl_handler SYSCTL_HANDLER_ARGS
2110 {
2111 #pragma unused(arg2, oidp)
2112 	scalable_counter_t counter = *(scalable_counter_t*) arg1;
2113 	uint64_t value = counter_load(&counter);
2114 	return SYSCTL_OUT(req, &value, sizeof(value));
2115 }
2116 
2117 #define X(name, T) \
2118 int \
2119 experiment_factor_##name##_handler SYSCTL_HANDLER_ARGS \
2120 { \
2121 	int error, changed = 0; \
2122 	T *ptr; \
2123 	T new_value, current_value; \
2124 	struct experiment_spec *spec = (struct experiment_spec *) arg1; \
2125 	if (!arg1) { \
2126 	        return EINVAL; \
2127 	} \
2128 	ptr = (T *)(spec->ptr); \
2129 	current_value = *ptr; \
2130 	error = sysctl_io_number(req, current_value, sizeof(T), &new_value, &changed); \
2131 	if (error != 0) { \
2132 	        return error; \
2133 	} \
2134 	if (changed) { \
2135 	        if (new_value < (T) spec->min_value || new_value > (T) spec->max_value) { \
2136 	                return EINVAL; \
2137 	        } \
2138 	        if (os_atomic_cmpxchg(&spec->modified, false, true, acq_rel)) { \
2139 	                spec->original_value = current_value; \
2140 	        } \
2141 	        os_atomic_store_wide(ptr, new_value, relaxed); \
2142 	} \
2143 	return 0; \
2144 }
2145 
2146 experiment_factor_numeric_types
2147 #undef X
2148 
2149 #if DEBUG || DEVELOPMENT
2150 static int
2151 sysctl_test_handler SYSCTL_HANDLER_ARGS
2152 {
2153 	int error;
2154 	int64_t value, out = 0;
2155 
2156 	error = SYSCTL_IN(req, &value, sizeof(value));
2157 	/* Only run test when new value was provided to prevent just reading or
2158 	 * querying from triggering the test, but still allow for sysctl
2159 	 * presence tests via read requests with NULL oldptr */
2160 	if (error == 0 && req->newptr) {
2161 		/* call the test that was specified in SYSCTL_TEST_REGISTER */
2162 		error = ((int (*)(int64_t, int64_t *))(uintptr_t)arg1)(value, &out);
2163 	}
2164 	if (error == 0) {
2165 		error = SYSCTL_OUT(req, &out, sizeof(out));
2166 	}
2167 	return error;
2168 }
2169 
2170 void
sysctl_register_test_startup(struct sysctl_test_setup_spec * spec)2171 sysctl_register_test_startup(struct sysctl_test_setup_spec *spec)
2172 {
2173 	struct sysctl_oid *oid = zalloc_permanent_type(struct sysctl_oid);
2174 
2175 	*oid = (struct sysctl_oid){
2176 		.oid_parent     = &sysctl__debug_test_children,
2177 		.oid_number     = OID_AUTO,
2178 		.oid_kind       = CTLTYPE_QUAD | CTLFLAG_OID2 | CTLFLAG_WR |
2179 	    CTLFLAG_PERMANENT | CTLFLAG_LOCKED | CTLFLAG_MASKED,
2180 		.oid_arg1       = (void *)(uintptr_t)spec->st_func,
2181 		.oid_name       = spec->st_name,
2182 		.oid_handler    = sysctl_test_handler,
2183 		.oid_fmt        = "Q",
2184 		.oid_version    = SYSCTL_OID_VERSION,
2185 		.oid_descr      = "",
2186 	};
2187 	sysctl_register_oid_early(oid);
2188 }
2189 
2190 
2191 extern void vm_analytics_tick(void *arg0, void *arg1);
2192 
2193 /* Manual trigger of vm_analytics_tick for testing on dev/debug kernel. */
2194 static int
2195 sysctl_vm_analytics_tick SYSCTL_HANDLER_ARGS
2196 {
2197 #pragma unused(arg1, arg2)
2198 	int error, val = 0;
2199 	error = sysctl_handle_int(oidp, &val, 0, req);
2200 	if (error || !req->newptr) {
2201 		return error;
2202 	}
2203 	vm_analytics_tick(NULL, NULL);
2204 	return 0;
2205 }
2206 
2207 SYSCTL_PROC(_vm, OID_AUTO, analytics_report, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_vm_analytics_tick, "I", "");
2208 
2209 /* Manual trigger of record_system_event for testing on dev/debug kernel */
2210 static int
2211 sysctl_test_record_system_event SYSCTL_HANDLER_ARGS
2212 {
2213 #pragma unused(arg1, arg2)
2214 	int error, val = 0;
2215 	error = sysctl_handle_int(oidp, &val, 0, req);
2216 	if (error || !req->newptr) {
2217 		return error;
2218 	}
2219 	record_system_event(SYSTEM_EVENT_TYPE_INFO, SYSTEM_EVENT_SUBSYSTEM_TEST, "sysctl test", "this is a test %s", "message");
2220 	return 0;
2221 }
2222 
2223 SYSCTL_PROC(_kern, OID_AUTO, test_record_system_event, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_test_record_system_event, "-", "");
2224 
2225 #endif /* DEBUG || DEVELOPMENT */
2226 
2227 
2228 CA_EVENT(ca_test_event,
2229     CA_INT, TestKey,
2230     CA_BOOL, TestBool,
2231     CA_STATIC_STRING(CA_UUID_LEN), TestString);
2232 
2233 /*
2234  * Manual testing of sending a CoreAnalytics event
2235  */
2236 static int
2237 sysctl_test_ca_event SYSCTL_HANDLER_ARGS
2238 {
2239 #pragma unused(arg1, arg2)
2240 	int error, val = 0;
2241 	/*
2242 	 * Only send on write
2243 	 */
2244 	error = sysctl_handle_int(oidp, &val, 0, req);
2245 	if (error || !req->newptr) {
2246 		return error;
2247 	}
2248 
2249 	ca_event_t event = CA_EVENT_ALLOCATE(ca_test_event);
2250 	CA_EVENT_TYPE(ca_test_event) * event_data = event->data;
2251 	event_data->TestKey = val;
2252 	event_data->TestBool = true;
2253 	uuid_string_t test_str = "sysctl_test_ca_event";
2254 	strlcpy(event_data->TestString, test_str, CA_UUID_LEN);
2255 	CA_EVENT_SEND(event);
2256 	return 0;
2257 }
2258 
2259 SYSCTL_PROC(_kern, OID_AUTO, test_ca_event, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, &sysctl_test_ca_event, "I", "");
2260 
2261 
2262 #if DEVELOPMENT || DEBUG
2263 struct perf_compressor_data {
2264 	user_addr_t buffer;
2265 	size_t buffer_size;
2266 	uint64_t benchmark_time;
2267 	uint64_t bytes_processed;
2268 	uint64_t compressor_growth;
2269 };
2270 
2271 static int
2272 sysctl_perf_compressor SYSCTL_HANDLER_ARGS
2273 {
2274 	int error = EINVAL;
2275 	size_t len = sizeof(struct perf_compressor_data);
2276 	struct perf_compressor_data benchmark_data = {0};
2277 
2278 	if (req->oldptr == USER_ADDR_NULL || req->oldlen != len ||
2279 	    req->newptr == USER_ADDR_NULL || req->newlen != len) {
2280 		return EINVAL;
2281 	}
2282 
2283 	error = SYSCTL_IN(req, &benchmark_data, len);
2284 	if (error) {
2285 		return error;
2286 	}
2287 
2288 	kern_return_t ret = run_compressor_perf_test(benchmark_data.buffer, benchmark_data.buffer_size,
2289 	    &benchmark_data.benchmark_time, &benchmark_data.bytes_processed, &benchmark_data.compressor_growth);
2290 	switch (ret) {
2291 	case KERN_SUCCESS:
2292 		error = 0;
2293 		break;
2294 	case KERN_NOT_SUPPORTED:
2295 		error = ENOTSUP;
2296 		break;
2297 	case KERN_INVALID_ARGUMENT:
2298 		error = EINVAL;
2299 		break;
2300 	case KERN_RESOURCE_SHORTAGE:
2301 		error = EAGAIN;
2302 		break;
2303 	default:
2304 		error = ret;
2305 		break;
2306 	}
2307 	if (error != 0) {
2308 		return error;
2309 	}
2310 
2311 	return SYSCTL_OUT(req, &benchmark_data, len);
2312 }
2313 
2314 /*
2315  * Compressor & swap performance test
2316  */
2317 SYSCTL_PROC(_kern, OID_AUTO, perf_compressor, CTLFLAG_WR | CTLFLAG_MASKED | CTLTYPE_STRUCT,
2318     0, 0, sysctl_perf_compressor, "S", "Compressor & swap benchmark");
2319 #endif /* DEVELOPMENT || DEBUG */
2320 
2321 #if CONFIG_JETSAM
2322 extern uint32_t swapout_sleep_threshold;
2323 #if DEVELOPMENT || DEBUG
2324 SYSCTL_UINT(_vm, OID_AUTO, swapout_sleep_threshold, CTLFLAG_RW | CTLFLAG_LOCKED, &swapout_sleep_threshold, 0, "");
2325 #else /* DEVELOPMENT || DEBUG */
2326 SYSCTL_UINT(_vm, OID_AUTO, swapout_sleep_threshold, CTLFLAG_RD | CTLFLAG_LOCKED, &swapout_sleep_threshold, 0, "");
2327 #endif /* DEVELOPMENT || DEBUG */
2328 #endif /* CONFIG_JETSAM */
2329