xref: /freebsd/sys/compat/linux/linux_sysctl.c (revision 9768746b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/sdt.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <sys/sbuf.h>
42 
43 #ifdef COMPAT_LINUX32
44 #include <machine/../linux32/linux.h>
45 #include <machine/../linux32/linux32_proto.h>
46 #else
47 #include <machine/../linux/linux.h>
48 #include <machine/../linux/linux_proto.h>
49 #endif
50 
51 #include <compat/linux/linux_dtrace.h>
52 #include <compat/linux/linux_misc.h>
53 #include <compat/linux/linux_util.h>
54 
55 #define	LINUX_CTL_KERN		1
56 #define	LINUX_CTL_VM		2
57 #define	LINUX_CTL_NET		3
58 #define	LINUX_CTL_PROC		4
59 #define	LINUX_CTL_FS		5
60 #define	LINUX_CTL_DEBUG		6
61 #define	LINUX_CTL_DEV		7
62 #define	LINUX_CTL_BUS		8
63 
64 /* CTL_KERN names */
65 #define	LINUX_KERN_OSTYPE	1
66 #define	LINUX_KERN_OSRELEASE	2
67 #define	LINUX_KERN_OSREV	3
68 #define	LINUX_KERN_VERSION	4
69 
70 /* DTrace init */
71 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
72 
73 /**
74  * DTrace probes in this module.
75  */
76 LIN_SDT_PROBE_DEFINE1(sysctl, handle_string, copyout_error, "int");
77 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, copyin_error, "int");
78 LIN_SDT_PROBE_DEFINE2(sysctl, linux_sysctl, wrong_length, "int", "int");
79 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, unsupported_sysctl, "char *");
80 
81 #ifdef LINUX_LEGACY_SYSCALLS
82 static int
83 handle_string(struct l___sysctl_args *la, char *value)
84 {
85 	int error;
86 
87 	if (la->oldval != 0) {
88 		l_int len = strlen(value);
89 		error = copyout(value, PTRIN(la->oldval), len + 1);
90 		if (!error && la->oldlenp != 0)
91 			error = copyout(&len, PTRIN(la->oldlenp), sizeof(len));
92 		if (error) {
93 			LIN_SDT_PROBE1(sysctl, handle_string, copyout_error,
94 			    error);
95 			return (error);
96 		}
97 	}
98 
99 	if (la->newval != 0)
100 		return (ENOTDIR);
101 
102 	return (0);
103 }
104 
105 int
106 linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
107 {
108 	struct l___sysctl_args la;
109 	struct sbuf *sb;
110 	l_int *mib;
111 	char *sysctl_string;
112 	int error, i;
113 
114 	error = copyin(args->args, &la, sizeof(la));
115 	if (error) {
116 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
117 		return (error);
118 	}
119 
120 	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME) {
121 		LIN_SDT_PROBE2(sysctl, linux_sysctl, wrong_length, la.nlen,
122 		    LINUX_CTL_MAXNAME);
123 		return (ENOTDIR);
124 	}
125 
126 	mib = malloc(la.nlen * sizeof(l_int), M_LINUX, M_WAITOK);
127 	error = copyin(PTRIN(la.name), mib, la.nlen * sizeof(l_int));
128 	if (error) {
129 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
130 		free(mib, M_LINUX);
131 		return (error);
132 	}
133 
134 	switch (mib[0]) {
135 	case LINUX_CTL_KERN:
136 		if (la.nlen < 2)
137 			break;
138 
139 		switch (mib[1]) {
140 		case LINUX_KERN_VERSION:
141 			error = handle_string(&la, version);
142 			free(mib, M_LINUX);
143 			return (error);
144 		default:
145 			break;
146 		}
147 		break;
148 	default:
149 		break;
150 	}
151 
152 	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
153 	if (sb == NULL) {
154 		linux_msg(td, "sysctl is not implemented");
155 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
156 		    "unknown sysctl, ENOMEM during lookup");
157 	} else {
158 		sbuf_printf(sb, "sysctl ");
159 		for (i = 0; i < la.nlen; i++)
160 			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
161 		sbuf_printf(sb, "} is not implemented");
162 		sbuf_finish(sb);
163 		sysctl_string = sbuf_data(sb);
164 		linux_msg(td, "%s", sysctl_string);
165 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
166 		    sysctl_string);
167 		sbuf_delete(sb);
168 	}
169 
170 	free(mib, M_LINUX);
171 
172 	return (ENOTDIR);
173 }
174 #endif
175