xref: /netbsd-src/sys/compat/linux32/common/linux32_sysctl.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /*	$NetBSD: linux32_sysctl.c,v 1.17 2017/09/29 17:47:29 maxv Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux32_sysctl.c,v 1.17 2017/09/29 17:47:29 maxv Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/proc.h>
40 #include <sys/mount.h>
41 #include <sys/sysctl.h>
42 #include <sys/syscallargs.h>
43 #include <sys/ktrace.h>
44 
45 #include <compat/netbsd32/netbsd32.h>
46 
47 #include <compat/linux/common/linux_types.h>
48 #include <compat/linux/common/linux_signal.h>
49 #include <compat/linux/common/linux_sysctl.h>
50 #include <compat/linux/common/linux_ipc.h>
51 #include <compat/linux/common/linux_sem.h>
52 #include <compat/linux/linux_syscallargs.h>
53 
54 #include <compat/linux32/common/linux32_types.h>
55 #include <compat/linux32/common/linux32_signal.h>
56 #include <compat/linux32/common/linux32_sysctl.h>
57 #include <compat/linux32/linux32_syscallargs.h>
58 
59 char linux32_sysname[128] = "Linux";
60 char linux32_release[128] = "3.11.6";
61 char linux32_version[128] = "#1 SMP PREEMPT Thu Oct 24 16:23:02 UTC 2013";
62 
63 struct sysctlnode linux32_sysctl_root = {
64 	.sysctl_flags = SYSCTL_VERSION|
65 	    CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
66 	.sysctl_num = 0,
67 	.sysctl_name = "(linux32_root)",
68 	.sysctl_size = sizeof(struct sysctlnode),
69 };
70 
71 static struct sysctllog *linux32_clog1;
72 static struct sysctllog *linux32_clog2;
73 extern int linux32_enabled;
74 
75 void
76 linux32_sysctl_fini(void)
77 {
78 
79 	sysctl_teardown(&linux32_clog2);
80 	sysctl_teardown(&linux32_clog1);
81 	sysctl_free(&linux32_sysctl_root);
82 }
83 
84 void
85 linux32_sysctl_init(void)
86 {
87 	const struct sysctlnode *node = &linux32_sysctl_root;
88 
89 	sysctl_createv(&linux32_clog1, 0, NULL, NULL,
90 		       CTLFLAG_PERMANENT,
91 		       CTLTYPE_NODE, "linux32",
92 		       SYSCTL_DESCR("Linux 32 bit emulation settings"),
93 		       NULL, 0, NULL, 0,
94 		       CTL_EMUL, EMUL_LINUX32, CTL_EOL);
95 	sysctl_createv(&linux32_clog1, 0, NULL, NULL,
96 		       CTLFLAG_PERMANENT,
97 		       CTLTYPE_NODE, "kern",
98 		       SYSCTL_DESCR("Linux 32 bit kernel emulation settings"),
99 		       NULL, 0, NULL, 0,
100 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN, CTL_EOL);
101 
102 	sysctl_createv(&linux32_clog1, 0, NULL, NULL,
103 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
104 		       CTLTYPE_STRING, "ostype",
105 		       SYSCTL_DESCR("Linux 32 bit operating system type"),
106 		       NULL, 0, linux32_sysname, sizeof(linux32_sysname),
107 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
108 		       EMUL_LINUX32_KERN_OSTYPE, CTL_EOL);
109 	sysctl_createv(&linux32_clog1, 0, NULL, NULL,
110 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
111 		       CTLTYPE_STRING, "osrelease",
112 		       SYSCTL_DESCR("Linux 32 bit operating system release"),
113 		       NULL, 0, linux32_release, sizeof(linux32_release),
114 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
115 		       EMUL_LINUX32_KERN_OSRELEASE, CTL_EOL);
116 	sysctl_createv(&linux32_clog1, 0, NULL, NULL,
117 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
118 		       CTLTYPE_STRING, "osversion",
119 		       SYSCTL_DESCR("Linux 32 bit operating system revision"),
120 		       NULL, 0, linux32_version, sizeof(linux32_version),
121 		       CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
122 		       EMUL_LINUX32_KERN_VERSION, CTL_EOL);
123 	sysctl_createv(&linux32_clog1, 0, NULL, NULL,
124 		       CTLFLAG_READWRITE,
125 		       CTLTYPE_INT, "enabled",
126 		       SYSCTL_DESCR("Linux 32 bit compat enabled."),
127 		       linux32_sysctl_enable, 0, &linux32_enabled, 0,
128 		       CTL_EMUL, EMUL_LINUX32, CTL_CREATE, CTL_EOL);
129 
130 	sysctl_createv(&linux32_clog2, 0, &node, &node,
131 		       CTLFLAG_PERMANENT,
132 		       CTLTYPE_NODE, "kern", NULL,
133 		       NULL, 0, NULL, 0,
134 		       LINUX_CTL_KERN, CTL_EOL);
135 
136 	sysctl_createv(&linux32_clog2, 0, &node, NULL,
137 		       CTLFLAG_PERMANENT,
138 		       CTLTYPE_STRING, "ostype", NULL,
139 		       NULL, 0, linux32_sysname, sizeof(linux32_sysname),
140 		       LINUX_KERN_OSTYPE, CTL_EOL);
141 	sysctl_createv(&linux32_clog2, 0, &node, NULL,
142 		       CTLFLAG_PERMANENT,
143 		       CTLTYPE_STRING, "osrelease", NULL,
144 		       NULL, 0, linux32_release, sizeof(linux32_release),
145 		       LINUX_KERN_OSRELEASE, CTL_EOL);
146 	sysctl_createv(&linux32_clog2, 0, &node, NULL,
147 		       CTLFLAG_PERMANENT,
148 		       CTLTYPE_STRING, "version", NULL,
149 		       NULL, 0, linux32_version, sizeof(linux32_version),
150 		       LINUX_KERN_VERSION, CTL_EOL);
151 
152 	linux32_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
153 }
154 
155 int
156 linux32_sys___sysctl(struct lwp *l, const struct linux32_sys___sysctl_args *uap, register_t *retval)
157 {
158 	/* {
159 		syscallarg(linux32___sysctlp_t) lsp;
160 	} */
161 	struct linux32_sysctl ls32;
162 	int name[CTL_MAXNAME];
163 	size_t savelen;
164 	netbsd32_size_t oldlen32;
165 	size_t oldlen;
166 	int error;
167 
168 	/*
169 	 * Read sysctl arguments
170 	 */
171 	if ((error = copyin(SCARG_P32(uap, lsp), &ls32, sizeof(ls32))) != 0)
172 		return error;
173 
174 	/*
175 	 * Read oldlen
176 	 */
177 	if (NETBSD32PTR64(ls32.oldlenp) != NULL) {
178 		if ((error = copyin(NETBSD32PTR64(ls32.oldlenp),
179 		    &oldlen32, sizeof(oldlen32))) != 0)
180 			return error;
181 	} else {
182 		oldlen32 = 0;
183 	}
184 
185 	savelen = (size_t)oldlen32;
186 
187 	/*
188 	 * Sanity check nlen
189 	 */
190 	if ((ls32.nlen > CTL_MAXNAME) || (ls32.nlen < 1))
191 		return ENOTDIR;
192 
193 	/*
194 	 * Read the sysctl name
195 	 */
196 	if ((error = copyin(NETBSD32PTR64(ls32.name), &name,
197 	   ls32.nlen * sizeof(int))) != 0)
198 		return error;
199 
200 	ktrmib(name, ls32.nlen);
201 	/*
202 	 * First try linux32 tree, then linux tree
203 	 */
204 	oldlen = (size_t)oldlen32;
205 	sysctl_lock(NETBSD32PTR64(ls32.newval) != NULL);
206 	error = sysctl_dispatch(name, ls32.nlen,
207 				NETBSD32PTR64(ls32.oldval), &oldlen,
208 				NETBSD32PTR64(ls32.newval), ls32.newlen,
209 				name, l, &linux32_sysctl_root);
210 	oldlen32 = (netbsd32_size_t)oldlen;
211 	sysctl_unlock();
212 
213 	/*
214 	 * Check for oldlen overflow (not likely, but who knows...)
215 	 */
216 	if (oldlen != oldlen32) {
217 #ifdef DEBUG_LINUX
218 		printf("%s: oldlen32 = %d, oldlen = %ld\n",
219 		    __func__, oldlen32, oldlen);
220 #endif
221 		return EINVAL;
222 	}
223 
224 	/*
225 	 * set caller's oldlen, even if we got an error
226 	 */
227 	if (NETBSD32PTR64(ls32.oldlenp)) {
228 		int nerror;
229 
230 		nerror = copyout(&oldlen32,
231 		    NETBSD32PTR64(ls32.oldlenp), sizeof(oldlen32));
232 
233 		if (error == 0)
234 			error = nerror;
235 	}
236 
237 	/*
238 	 * oldlen was too short
239 	 */
240 	if ((error == 0) &&
241 	    (NETBSD32PTR64(ls32.oldval) != NULL) &&
242 	    (savelen < oldlen32))
243 		error = ENOMEM;
244 
245 	return error;
246 }
247