Deleted Added
full compact
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Mike Karels at Berkeley Software Design, Inc.
7 *
8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD

--- 22 unchanged lines hidden (view full) ---

31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 286094 2015-07-30 19:52:43Z mjg $");
39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 287835 2015-09-15 23:06:56Z mjg $");
40
41#include "opt_capsicum.h"
42#include "opt_compat.h"
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/fail.h>
47#include <sys/systm.h>
48#include <sys/capsicum.h>
49#include <sys/kernel.h>
50#include <sys/sysctl.h>
51#include <sys/malloc.h>
52#include <sys/priv.h>
53#include <sys/proc.h>
54#include <sys/jail.h>
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#include <sys/rmlock.h>
58#include <sys/sbuf.h>
59#include <sys/sx.h>
60#include <sys/sysproto.h>
61#include <sys/uio.h>
62#ifdef KTRACE
63#include <sys/ktrace.h>
64#endif
65

--- 7 unchanged lines hidden (view full) ---

73static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
74static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
75static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
76
77/*
78 * The sysctllock protects the MIB tree. It also protects sysctl
79 * contexts used with dynamic sysctls. The sysctl_register_oid() and
80 * sysctl_unregister_oid() routines require the sysctllock to already
80 * be held, so the sysctl_xlock() and sysctl_xunlock() routines are
81 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are
82 * provided for the few places in the kernel which need to use that
83 * API rather than using the dynamic API. Use of the dynamic API is
84 * strongly encouraged for most code.
85 *
86 * The sysctlmemlock is used to limit the amount of user memory wired for
87 * sysctl requests. This is implemented by serializing any userland
88 * sysctl requests larger than a single page via an exclusive lock.
89 */
89static struct sx sysctllock;
90static struct rmlock sysctllock;
91static struct sx sysctlmemlock;
92
92#define SYSCTL_XLOCK() sx_xlock(&sysctllock)
93#define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock)
94#define SYSCTL_SLOCK() sx_slock(&sysctllock)
95#define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock)
96#define SYSCTL_XLOCKED() sx_xlocked(&sysctllock)
97#define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED)
98#define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED)
99#define SYSCTL_ASSERT_SLOCKED() sx_assert(&sysctllock, SA_SLOCKED)
100#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock")
93#define SYSCTL_WLOCK() rm_wlock(&sysctllock)
94#define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock)
95#define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker))
96#define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker))
97#define SYSCTL_WLOCKED() rm_wowned(&sysctllock)
98#define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED)
99#define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED)
100#define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED)
101#define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \
102 RM_SLEEPABLE)
103#define SYSCTL_SLEEP(ch, wmesg, timo) \
102 sx_sleep(ch, &sysctllock, 0, wmesg, timo)
104 rm_sleep(ch, &sysctllock, 0, wmesg, timo)
105
106static int sysctl_root(SYSCTL_HANDLER_ARGS);
107
108/* Root list */
109struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children);
110
111static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del,
112 int recurse);
113static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t);
114static int sysctl_new_kernel(struct sysctl_req *, void *, size_t);
115
114static void
115sysctl_lock(bool xlock)
116{
117
118 if (xlock)
119 SYSCTL_XLOCK();
120 else
121 SYSCTL_SLOCK();
122}
123
124static bool
125sysctl_unlock(void)
126{
127 bool xlocked;
128
129 xlocked = SYSCTL_XLOCKED();
130 if (xlocked)
131 SYSCTL_XUNLOCK();
132 else
133 SYSCTL_SUNLOCK();
134 return (xlocked);
135}
136
116static struct sysctl_oid *
117sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
118{
119 struct sysctl_oid *oidp;
120
121 SYSCTL_ASSERT_LOCKED();
122 SLIST_FOREACH(oidp, list, oid_link) {
123 if (strcmp(oidp->oid_name, name) == 0) {

--- 4 unchanged lines hidden (view full) ---

128}
129
130/*
131 * Initialization of the MIB tree.
132 *
133 * Order by number in each list.
134 */
135void
157sysctl_xlock(void)
136sysctl_wlock(void)
137{
138
160 SYSCTL_XLOCK();
139 SYSCTL_WLOCK();
140}
141
142void
164sysctl_xunlock(void)
143sysctl_wunlock(void)
144{
145
167 SYSCTL_XUNLOCK();
146 SYSCTL_WUNLOCK();
147}
148
149static int
150sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intptr_t arg2,
172 struct sysctl_req *req)
151 struct sysctl_req *req, struct rm_priotracker *tracker)
152{
153 int error;
175 bool xlocked;
154
155 if (oid->oid_kind & CTLFLAG_DYN)
156 atomic_add_int(&oid->oid_running, 1);
179 xlocked = sysctl_unlock();
157
158 if (tracker != NULL)
159 SYSCTL_RUNLOCK(tracker);
160 else
161 SYSCTL_WUNLOCK();
162
163 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
164 mtx_lock(&Giant);
165 error = oid->oid_handler(oid, arg1, arg2, req);
166 if (!(oid->oid_kind & CTLFLAG_MPSAFE))
167 mtx_unlock(&Giant);
168
187 sysctl_lock(xlocked);
169 if (tracker != NULL)
170 SYSCTL_RLOCK(tracker);
171 else
172 SYSCTL_WLOCK();
173
174 if (oid->oid_kind & CTLFLAG_DYN) {
175 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 &&
176 (oid->oid_kind & CTLFLAG_DYING) != 0)
177 wakeup(&oid->oid_running);
178 }
179
180 return (error);
181}

--- 82 unchanged lines hidden (view full) ---

264 return;
265 req.newlen = strlen(penv);
266 req.newptr = penv;
267 break;
268 default:
269 return;
270 }
271 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1,
286 oidp->oid_arg2, &req);
272 oidp->oid_arg2, &req, NULL);
273 if (error != 0)
274 printf("Setting sysctl %s failed: %d\n", path + rem, error);
275 if (penv != NULL)
276 freeenv(penv);
277}
278
279void
280sysctl_register_oid(struct sysctl_oid *oidp)
281{
282 struct sysctl_oid_list *parent = oidp->oid_parent;
283 struct sysctl_oid *p;
284 struct sysctl_oid *q;
285 int oid_number;
286 int timeout = 2;
287
288 /*
289 * First check if another oid with the same name already
290 * exists in the parent's list.
291 */
306 SYSCTL_ASSERT_XLOCKED();
292 SYSCTL_ASSERT_WLOCKED();
293 p = sysctl_find_oidname(oidp->oid_name, parent);
294 if (p != NULL) {
295 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
296 p->oid_refcnt++;
297 return;
298 } else {
299 printf("can't re-use a leaf (%s)!\n", p->oid_name);
300 return;

--- 77 unchanged lines hidden (view full) ---

378}
379
380void
381sysctl_unregister_oid(struct sysctl_oid *oidp)
382{
383 struct sysctl_oid *p;
384 int error;
385
400 SYSCTL_ASSERT_XLOCKED();
386 SYSCTL_ASSERT_WLOCKED();
387 error = ENOENT;
388 if (oidp->oid_number == OID_AUTO) {
389 error = EINVAL;
390 } else {
391 SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
392 if (p == oidp) {
393 SLIST_REMOVE(oidp->oid_parent, oidp,
394 sysctl_oid, oid_link);

--- 39 unchanged lines hidden (view full) ---

434
435 error = 0;
436 /*
437 * First perform a "dry run" to check if it's ok to remove oids.
438 * XXX FIXME
439 * XXX This algorithm is a hack. But I don't know any
440 * XXX better solution for now...
441 */
456 SYSCTL_XLOCK();
442 SYSCTL_WLOCK();
443 TAILQ_FOREACH(e, clist, link) {
444 error = sysctl_remove_oid_locked(e->entry, 0, 0);
445 if (error)
446 break;
447 }
448 /*
449 * Restore deregistered entries, either from the end,
450 * or from the place where error occured.
451 * e contains the entry that was not unregistered
452 */
453 if (error)
454 e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
455 else
456 e1 = TAILQ_LAST(clist, sysctl_ctx_list);
457 while (e1 != NULL) {
458 sysctl_register_oid(e1->entry);
459 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
460 }
461 if (error) {
476 SYSCTL_XUNLOCK();
462 SYSCTL_WUNLOCK();
463 return(EBUSY);
464 }
465 /* Now really delete the entries */
466 e = TAILQ_FIRST(clist);
467 while (e != NULL) {
468 e1 = TAILQ_NEXT(e, link);
469 error = sysctl_remove_oid_locked(e->entry, 1, 0);
470 if (error)
471 panic("sysctl_remove_oid: corrupt tree, entry: %s",
472 e->entry->oid_name);
473 free(e, M_SYSCTLOID);
474 e = e1;
475 }
490 SYSCTL_XUNLOCK();
476 SYSCTL_WUNLOCK();
477 return (error);
478}
479
480/* Add an entry to the context */
481struct sysctl_ctx_entry *
482sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
483{
484 struct sysctl_ctx_entry *e;
485
500 SYSCTL_ASSERT_XLOCKED();
486 SYSCTL_ASSERT_WLOCKED();
487 if (clist == NULL || oidp == NULL)
488 return(NULL);
489 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
490 e->entry = oidp;
491 TAILQ_INSERT_HEAD(clist, e, link);
492 return (e);
493}
494
495/* Find an entry in the context */
496struct sysctl_ctx_entry *
497sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
498{
499 struct sysctl_ctx_entry *e;
500
515 SYSCTL_ASSERT_XLOCKED();
501 SYSCTL_ASSERT_WLOCKED();
502 if (clist == NULL || oidp == NULL)
503 return(NULL);
504 TAILQ_FOREACH(e, clist, link) {
505 if(e->entry == oidp)
506 return(e);
507 }
508 return (e);
509}

--- 5 unchanged lines hidden (view full) ---

515 */
516int
517sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
518{
519 struct sysctl_ctx_entry *e;
520
521 if (clist == NULL || oidp == NULL)
522 return (EINVAL);
537 SYSCTL_XLOCK();
523 SYSCTL_WLOCK();
524 e = sysctl_ctx_entry_find(clist, oidp);
525 if (e != NULL) {
526 TAILQ_REMOVE(clist, e, link);
541 SYSCTL_XUNLOCK();
527 SYSCTL_WUNLOCK();
528 free(e, M_SYSCTLOID);
529 return (0);
530 } else {
545 SYSCTL_XUNLOCK();
531 SYSCTL_WUNLOCK();
532 return (ENOENT);
533 }
534}
535
536/*
537 * Remove dynamically created sysctl trees.
538 * oidp - top of the tree to be removed
539 * del - if 0 - just deregister, otherwise free up entries as well
540 * recurse - if != 0 traverse the subtree to be deleted
541 */
542int
543sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
544{
545 int error;
546
561 SYSCTL_XLOCK();
547 SYSCTL_WLOCK();
548 error = sysctl_remove_oid_locked(oidp, del, recurse);
563 SYSCTL_XUNLOCK();
549 SYSCTL_WUNLOCK();
550 return (error);
551}
552
553int
554sysctl_remove_name(struct sysctl_oid *parent, const char *name,
555 int del, int recurse)
556{
557 struct sysctl_oid *p, *tmp;
558 int error;
559
560 error = ENOENT;
575 SYSCTL_XLOCK();
561 SYSCTL_WLOCK();
562 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) {
563 if (strcmp(p->oid_name, name) == 0) {
564 error = sysctl_remove_oid_locked(p, del, recurse);
565 break;
566 }
567 }
582 SYSCTL_XUNLOCK();
568 SYSCTL_WUNLOCK();
569
570 return (error);
571}
572
573
574static int
575sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
576{
577 struct sysctl_oid *p, *tmp;
578 int error;
579
594 SYSCTL_ASSERT_XLOCKED();
580 SYSCTL_ASSERT_WLOCKED();
581 if (oidp == NULL)
582 return(EINVAL);
583 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
584 printf("can't remove non-dynamic nodes!\n");
585 return (EINVAL);
586 }
587 /*
588 * WARNING: normal method to do this should be through

--- 58 unchanged lines hidden (view full) ---

647 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
648{
649 struct sysctl_oid *oidp;
650
651 /* You have to hook up somewhere.. */
652 if (parent == NULL)
653 return(NULL);
654 /* Check if the node already exists, otherwise create it */
669 SYSCTL_XLOCK();
655 SYSCTL_WLOCK();
656 oidp = sysctl_find_oidname(name, parent);
657 if (oidp != NULL) {
658 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
659 oidp->oid_refcnt++;
660 /* Update the context */
661 if (clist != NULL)
662 sysctl_ctx_entry_add(clist, oidp);
677 SYSCTL_XUNLOCK();
663 SYSCTL_WUNLOCK();
664 return (oidp);
665 } else {
680 SYSCTL_XUNLOCK();
666 SYSCTL_WUNLOCK();
667 printf("can't re-use a leaf (%s)!\n", name);
668 return (NULL);
669 }
670 }
671 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
672 oidp->oid_parent = parent;
673 SLIST_INIT(&oidp->oid_children);
674 oidp->oid_number = number;

--- 6 unchanged lines hidden (view full) ---

681 oidp->oid_fmt = fmt;
682 if (descr != NULL)
683 oidp->oid_descr = strdup(descr, M_SYSCTLOID);
684 /* Update the context, if used */
685 if (clist != NULL)
686 sysctl_ctx_entry_add(clist, oidp);
687 /* Register this oid */
688 sysctl_register_oid(oidp);
703 SYSCTL_XUNLOCK();
689 SYSCTL_WUNLOCK();
690 return (oidp);
691}
692
693/*
694 * Rename an existing oid.
695 */
696void
697sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
698{
699 char *newname;
700 char *oldname;
701
702 newname = strdup(name, M_SYSCTLOID);
717 SYSCTL_XLOCK();
703 SYSCTL_WLOCK();
704 oldname = __DECONST(char *, oidp->oid_name);
705 oidp->oid_name = newname;
720 SYSCTL_XUNLOCK();
706 SYSCTL_WUNLOCK();
707 free(oldname, M_SYSCTLOID);
708}
709
710/*
711 * Reparent an existing oid.
712 */
713int
714sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
715{
716 struct sysctl_oid *oidp;
717
732 SYSCTL_XLOCK();
718 SYSCTL_WLOCK();
719 if (oid->oid_parent == parent) {
734 SYSCTL_XUNLOCK();
720 SYSCTL_WUNLOCK();
721 return (0);
722 }
723 oidp = sysctl_find_oidname(oid->oid_name, parent);
724 if (oidp != NULL) {
739 SYSCTL_XUNLOCK();
725 SYSCTL_WUNLOCK();
726 return (EEXIST);
727 }
728 sysctl_unregister_oid(oid);
729 oid->oid_parent = parent;
730 oid->oid_number = OID_AUTO;
731 sysctl_register_oid(oid);
746 SYSCTL_XUNLOCK();
732 SYSCTL_WUNLOCK();
733 return (0);
734}
735
736/*
737 * Register the kernel's oids on startup.
738 */
739SET_DECLARE(sysctl_set, struct sysctl_oid);
740
741static void
742sysctl_register_all(void *arg)
743{
744 struct sysctl_oid **oidp;
745
746 sx_init(&sysctlmemlock, "sysctl mem");
747 SYSCTL_INIT();
762 SYSCTL_XLOCK();
748 SYSCTL_WLOCK();
749 SET_FOREACH(oidp, sysctl_set)
750 sysctl_register_oid(*oidp);
765 SYSCTL_XUNLOCK();
751 SYSCTL_WUNLOCK();
752}
753SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0);
754
755/*
756 * "Staff-functions"
757 *
758 * These functions implement a presently undocumented interface
759 * used by the sysctl program to walk the tree, and get the type

--- 53 unchanged lines hidden (view full) ---

813 }
814
815 }
816}
817
818static int
819sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
820{
821 struct rm_priotracker tracker;
822 int error;
823
824 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
825 if (error)
826 return (error);
840 SYSCTL_SLOCK();
827 SYSCTL_RLOCK(&tracker);
828 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
842 SYSCTL_SUNLOCK();
829 SYSCTL_RUNLOCK(&tracker);
830 return (ENOENT);
831}
832
833SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE,
834 0, 0, sysctl_sysctl_debug, "-", "");
835#endif
836
837static int
838sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
839{
840 int *name = (int *) arg1;
841 u_int namelen = arg2;
842 int error = 0;
843 struct sysctl_oid *oid;
844 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
845 struct rm_priotracker tracker;
846 char buf[10];
847
860 SYSCTL_SLOCK();
848 SYSCTL_RLOCK(&tracker);
849 while (namelen) {
850 if (!lsp) {
851 snprintf(buf,sizeof(buf),"%d",*name);
852 if (req->oldidx)
853 error = SYSCTL_OUT(req, ".", 1);
854 if (!error)
855 error = SYSCTL_OUT(req, buf, strlen(buf));
856 if (error)

--- 26 unchanged lines hidden (view full) ---

883
884 lsp2 = SYSCTL_CHILDREN(oid);
885 break;
886 }
887 lsp = lsp2;
888 }
889 error = SYSCTL_OUT(req, "", 1);
890 out:
903 SYSCTL_SUNLOCK();
891 SYSCTL_RUNLOCK(&tracker);
892 return (error);
893}
894
895/*
896 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in
897 * capability mode.
898 */
899static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD,

--- 62 unchanged lines hidden (view full) ---

962static int
963sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
964{
965 int *name = (int *) arg1;
966 u_int namelen = arg2;
967 int i, j, error;
968 struct sysctl_oid *oid;
969 struct sysctl_oid_list *lsp = &sysctl__children;
970 struct rm_priotracker tracker;
971 int newoid[CTL_MAXNAME];
972
984 SYSCTL_SLOCK();
973 SYSCTL_RLOCK(&tracker);
974 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
986 SYSCTL_SUNLOCK();
975 SYSCTL_RUNLOCK(&tracker);
976 if (i)
977 return (ENOENT);
978 error = SYSCTL_OUT(req, newoid, j * sizeof (int));
979 return (error);
980}
981
982/*
983 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in

--- 42 unchanged lines hidden (view full) ---

1026}
1027
1028static int
1029sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
1030{
1031 char *p;
1032 int error, oid[CTL_MAXNAME], len = 0;
1033 struct sysctl_oid *op = 0;
1034 struct rm_priotracker tracker;
1035
1036 if (!req->newlen)
1037 return (ENOENT);
1038 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */
1039 return (ENAMETOOLONG);
1040
1041 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
1042
1043 error = SYSCTL_IN(req, p, req->newlen);
1044 if (error) {
1045 free(p, M_SYSCTL);
1046 return (error);
1047 }
1048
1049 p [req->newlen] = '\0';
1050
1061 SYSCTL_SLOCK();
1051 SYSCTL_RLOCK(&tracker);
1052 error = name2oid(p, oid, &len, &op);
1063 SYSCTL_SUNLOCK();
1053 SYSCTL_RUNLOCK(&tracker);
1054
1055 free(p, M_SYSCTL);
1056
1057 if (error)
1058 return (error);
1059
1060 error = SYSCTL_OUT(req, oid, len * sizeof *oid);
1061 return (error);

--- 6 unchanged lines hidden (view full) ---

1068SYSCTL_PROC(_sysctl, 3, name2oid,
1069 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE
1070 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", "");
1071
1072static int
1073sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
1074{
1075 struct sysctl_oid *oid;
1076 struct rm_priotracker tracker;
1077 int error;
1078
1088 SYSCTL_SLOCK();
1079 SYSCTL_RLOCK(&tracker);
1080 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1081 if (error)
1082 goto out;
1083
1084 if (oid->oid_fmt == NULL) {
1085 error = ENOENT;
1086 goto out;
1087 }
1088 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
1089 if (error)
1090 goto out;
1091 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
1092 out:
1102 SYSCTL_SUNLOCK();
1093 SYSCTL_RUNLOCK(&tracker);
1094 return (error);
1095}
1096
1097
1098static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1099 sysctl_sysctl_oidfmt, "");
1100
1101static int
1102sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
1103{
1104 struct sysctl_oid *oid;
1105 struct rm_priotracker tracker;
1106 int error;
1107
1116 SYSCTL_SLOCK();
1108 SYSCTL_RLOCK(&tracker);
1109 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
1110 if (error)
1111 goto out;
1112
1113 if (oid->oid_descr == NULL) {
1114 error = ENOENT;
1115 goto out;
1116 }
1117 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
1118 out:
1127 SYSCTL_SUNLOCK();
1119 SYSCTL_RUNLOCK(&tracker);
1120 return (error);
1121}
1122
1123static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD,
1124 sysctl_sysctl_oiddescr, "");
1125
1126/*
1127 * Default "handler" functions.

--- 288 unchanged lines hidden (view full) ---

1416 req.newlen = newlen;
1417 req.newptr = new;
1418 }
1419
1420 req.oldfunc = sysctl_old_kernel;
1421 req.newfunc = sysctl_new_kernel;
1422 req.lock = REQ_UNWIRED;
1423
1432 SYSCTL_SLOCK();
1424 error = sysctl_root(0, name, namelen, &req);
1434 SYSCTL_SUNLOCK();
1425
1426 if (req.lock == REQ_WIRED && req.validlen > 0)
1427 vsunlock(req.oldptr, req.validlen);
1428
1429 if (error && error != ENOMEM)
1430 return (error);
1431
1432 if (retval) {

--- 160 unchanged lines hidden (view full) ---

1593 * Traverse our tree, and find the right node, execute whatever it points
1594 * to, and return the resulting error code.
1595 */
1596
1597static int
1598sysctl_root(SYSCTL_HANDLER_ARGS)
1599{
1600 struct sysctl_oid *oid;
1601 struct rm_priotracker tracker;
1602 int error, indx, lvl;
1603
1613 SYSCTL_ASSERT_SLOCKED();
1604 SYSCTL_RLOCK(&tracker);
1605
1606 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1607 if (error)
1617 return (error);
1608 goto out;
1609
1610 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1611 /*
1612 * You can't call a sysctl when it's a node, but has
1613 * no handler. Inform the user that it's a node.
1614 * The indx may or may not be the same as namelen.
1615 */
1625 if (oid->oid_handler == NULL)
1626 return (EISDIR);
1616 if (oid->oid_handler == NULL) {
1617 error = EISDIR;
1618 goto out;
1619 }
1620 }
1621
1622 /* Is this sysctl writable? */
1630 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1631 return (EPERM);
1623 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) {
1624 error = EPERM;
1625 goto out;
1626 }
1627
1628 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1629
1630#ifdef CAPABILITY_MODE
1631 /*
1632 * If the process is in capability mode, then don't permit reading or
1633 * writing unless specifically granted for the node.
1634 */
1635 if (IN_CAPABILITY_MODE(req->td)) {
1641 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1642 return (EPERM);
1643 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1644 return (EPERM);
1636 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) ||
1637 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) {
1638 error = EPERM;
1639 goto out;
1640 }
1641 }
1642#endif
1643
1644 /* Is this sysctl sensitive to securelevels? */
1645 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1646 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1647 error = securelevel_gt(req->td->td_ucred, lvl);
1648 if (error)
1653 return (error);
1649 goto out;
1650 }
1651
1652 /* Is this sysctl writable by only privileged users? */
1653 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1654 int priv;
1655
1656 if (oid->oid_kind & CTLFLAG_PRISON)
1657 priv = PRIV_SYSCTL_WRITEJAIL;
1658#ifdef VIMAGE
1659 else if ((oid->oid_kind & CTLFLAG_VNET) &&
1660 prison_owns_vnet(req->td->td_ucred))
1661 priv = PRIV_SYSCTL_WRITEJAIL;
1662#endif
1663 else
1664 priv = PRIV_SYSCTL_WRITE;
1665 error = priv_check(req->td, priv);
1666 if (error)
1671 return (error);
1667 goto out;
1668 }
1669
1674 if (!oid->oid_handler)
1675 return (EINVAL);
1670 if (!oid->oid_handler) {
1671 error = EINVAL;
1672 goto out;
1673 }
1674
1675 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1676 arg1 = (int *)arg1 + indx;
1677 arg2 -= indx;
1678 } else {
1679 arg1 = oid->oid_arg1;
1680 arg2 = oid->oid_arg2;
1681 }
1682#ifdef MAC
1683 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1684 req);
1685 if (error != 0)
1688 return (error);
1686 goto out;
1687#endif
1688#ifdef VIMAGE
1689 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL)
1690 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1);
1691#endif
1694 error = sysctl_root_handler_locked(oid, arg1, arg2, req);
1692 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker);
1693
1694 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1695
1696out:
1697 SYSCTL_RUNLOCK(&tracker);
1698 return (error);
1699}
1700
1701#ifndef _SYS_SYSPROTO_H_
1702struct sysctl_args {
1703 int *name;
1704 u_int namelen;
1705 void *old;

--- 83 unchanged lines hidden (view full) ---

1789 sx_xlock(&sysctlmemlock);
1790 } else
1791 memlocked = 0;
1792 CURVNET_SET(TD_TO_VNET(td));
1793
1794 for (;;) {
1795 req.oldidx = 0;
1796 req.newidx = 0;
1797 SYSCTL_SLOCK();
1797 error = sysctl_root(0, name, namelen, &req);
1799 SYSCTL_SUNLOCK();
1798 if (error != EAGAIN)
1799 break;
1800 kern_yield(PRI_USER);
1801 }
1802
1803 CURVNET_RESTORE();
1804
1805 if (req.lock == REQ_WIRED && req.validlen > 0)

--- 43 unchanged lines hidden ---