xref: /freebsd/sys/compat/linux/linux_mib.c (revision fc7510aef78781b0068da1a6ba190a636a54d6e7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 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/sdt.h>
35 #include <sys/systm.h>
36 #include <sys/sysctl.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/jail.h>
41 #include <sys/lock.h>
42 #include <sys/sx.h>
43 
44 #include <compat/linux/linux_mib.h>
45 #include <compat/linux/linux_misc.h>
46 
47 struct linux_prison {
48 	char	pr_osname[LINUX_MAX_UTSNAME];
49 	char	pr_osrelease[LINUX_MAX_UTSNAME];
50 	int	pr_oss_version;
51 	int	pr_osrel;
52 };
53 
54 static struct linux_prison lprison0 = {
55 	.pr_osname =		"Linux",
56 	.pr_osrelease =		LINUX_VERSION_STR,
57 	.pr_oss_version =	0x030600,
58 	.pr_osrel =		LINUX_VERSION_CODE
59 };
60 
61 static unsigned linux_osd_jail_slot;
62 
63 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0, "Linux mode");
64 
65 int linux_ignore_ip_recverr = 1;
66 SYSCTL_INT(_compat_linux, OID_AUTO, ignore_ip_recverr, CTLFLAG_RWTUN,
67     &linux_ignore_ip_recverr, 0, "Ignore enabling IP_RECVERR");
68 
69 int linux_preserve_vstatus = 0;
70 SYSCTL_INT(_compat_linux, OID_AUTO, preserve_vstatus, CTLFLAG_RWTUN,
71     &linux_preserve_vstatus, 0, "Preserve VSTATUS termios(4) flag");
72 
73 static int	linux_set_osname(struct thread *td, char *osname);
74 static int	linux_set_osrelease(struct thread *td, char *osrelease);
75 static int	linux_set_oss_version(struct thread *td, int oss_version);
76 
77 static int
78 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
79 {
80 	char osname[LINUX_MAX_UTSNAME];
81 	int error;
82 
83 	linux_get_osname(req->td, osname);
84 	error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
85 	if (error != 0 || req->newptr == NULL)
86 		return (error);
87 	error = linux_set_osname(req->td, osname);
88 
89 	return (error);
90 }
91 
92 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
93 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
94 	    0, 0, linux_sysctl_osname, "A",
95 	    "Linux kernel OS name");
96 
97 static int
98 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
99 {
100 	char osrelease[LINUX_MAX_UTSNAME];
101 	int error;
102 
103 	linux_get_osrelease(req->td, osrelease);
104 	error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
105 	if (error != 0 || req->newptr == NULL)
106 		return (error);
107 	error = linux_set_osrelease(req->td, osrelease);
108 
109 	return (error);
110 }
111 
112 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
113 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
114 	    0, 0, linux_sysctl_osrelease, "A",
115 	    "Linux kernel OS release");
116 
117 static int
118 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
119 {
120 	int oss_version;
121 	int error;
122 
123 	oss_version = linux_get_oss_version(req->td);
124 	error = sysctl_handle_int(oidp, &oss_version, 0, req);
125 	if (error != 0 || req->newptr == NULL)
126 		return (error);
127 	error = linux_set_oss_version(req->td, oss_version);
128 
129 	return (error);
130 }
131 
132 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
133 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
134 	    0, 0, linux_sysctl_oss_version, "I",
135 	    "Linux OSS version");
136 
137 /*
138  * Map the osrelease into integer
139  */
140 static int
141 linux_map_osrel(char *osrelease, int *osrel)
142 {
143 	char *sep, *eosrelease;
144 	int len, v0, v1, v2, v;
145 
146 	len = strlen(osrelease);
147 	eosrelease = osrelease + len;
148 	v0 = strtol(osrelease, &sep, 10);
149 	if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
150 		return (EINVAL);
151 	osrelease = sep + 1;
152 	v1 = strtol(osrelease, &sep, 10);
153 	if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
154 		return (EINVAL);
155 	osrelease = sep + 1;
156 	v2 = strtol(osrelease, &sep, 10);
157 	if (osrelease == sep ||
158 	    (sep != eosrelease && (sep + 1 >= eosrelease || *sep != '-')))
159 		return (EINVAL);
160 
161 	v = LINUX_KERNVER(v0, v1, v2);
162 	if (v < LINUX_KERNVER(1, 0, 0))
163 		return (EINVAL);
164 
165 	if (osrel != NULL)
166 		*osrel = v;
167 
168 	return (0);
169 }
170 
171 /*
172  * Find a prison with Linux info.
173  * Return the Linux info and the (locked) prison.
174  */
175 static struct linux_prison *
176 linux_find_prison(struct prison *spr, struct prison **prp)
177 {
178 	struct prison *pr;
179 	struct linux_prison *lpr;
180 
181 	for (pr = spr;; pr = pr->pr_parent) {
182 		mtx_lock(&pr->pr_mtx);
183 		lpr = (pr == &prison0)
184 		    ? &lprison0
185 		    : osd_jail_get(pr, linux_osd_jail_slot);
186 		if (lpr != NULL)
187 			break;
188 		mtx_unlock(&pr->pr_mtx);
189 	}
190 	*prp = pr;
191 
192 	return (lpr);
193 }
194 
195 /*
196  * Ensure a prison has its own Linux info.  If lprp is non-null, point it to
197  * the Linux info and lock the prison.
198  */
199 static void
200 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
201 {
202 	struct prison *ppr;
203 	struct linux_prison *lpr, *nlpr;
204 	void **rsv;
205 
206 	/* If this prison already has Linux info, return that. */
207 	lpr = linux_find_prison(pr, &ppr);
208 	if (ppr == pr)
209 		goto done;
210 	/*
211 	 * Allocate a new info record.  Then check again, in case something
212 	 * changed during the allocation.
213 	 */
214 	mtx_unlock(&ppr->pr_mtx);
215 	nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
216 	rsv = osd_reserve(linux_osd_jail_slot);
217 	lpr = linux_find_prison(pr, &ppr);
218 	if (ppr == pr) {
219 		free(nlpr, M_PRISON);
220 		osd_free_reserved(rsv);
221 		goto done;
222 	}
223 	/* Inherit the initial values from the ancestor. */
224 	mtx_lock(&pr->pr_mtx);
225 	(void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
226 	bcopy(lpr, nlpr, sizeof(*lpr));
227 	lpr = nlpr;
228 	mtx_unlock(&ppr->pr_mtx);
229  done:
230 	if (lprp != NULL)
231 		*lprp = lpr;
232 	else
233 		mtx_unlock(&pr->pr_mtx);
234 }
235 
236 /*
237  * Jail OSD methods for Linux prison data.
238  */
239 static int
240 linux_prison_create(void *obj, void *data)
241 {
242 	struct prison *pr = obj;
243 	struct vfsoptlist *opts = data;
244 	int jsys;
245 
246 	if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
247 	    jsys == JAIL_SYS_INHERIT)
248 		return (0);
249 	/*
250 	 * Inherit a prison's initial values from its parent
251 	 * (different from JAIL_SYS_INHERIT which also inherits changes).
252 	 */
253 	linux_alloc_prison(pr, NULL);
254 	return (0);
255 }
256 
257 static int
258 linux_prison_check(void *obj __unused, void *data)
259 {
260 	struct vfsoptlist *opts = data;
261 	char *osname, *osrelease;
262 	int error, jsys, len, oss_version;
263 
264 	/* Check that the parameters are correct. */
265 	error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
266 	if (error != ENOENT) {
267 		if (error != 0)
268 			return (error);
269 		if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
270 			return (EINVAL);
271 	}
272 	error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
273 	if (error != ENOENT) {
274 		if (error != 0)
275 			return (error);
276 		if (len == 0 || osname[len - 1] != '\0')
277 			return (EINVAL);
278 		if (len > LINUX_MAX_UTSNAME) {
279 			vfs_opterror(opts, "linux.osname too long");
280 			return (ENAMETOOLONG);
281 		}
282 	}
283 	error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
284 	if (error != ENOENT) {
285 		if (error != 0)
286 			return (error);
287 		if (len == 0 || osrelease[len - 1] != '\0')
288 			return (EINVAL);
289 		if (len > LINUX_MAX_UTSNAME) {
290 			vfs_opterror(opts, "linux.osrelease too long");
291 			return (ENAMETOOLONG);
292 		}
293 		error = linux_map_osrel(osrelease, NULL);
294 		if (error != 0) {
295 			vfs_opterror(opts, "linux.osrelease format error");
296 			return (error);
297 		}
298 	}
299 	error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
300 	    sizeof(oss_version));
301 
302 	if (error == ENOENT)
303 		error = 0;
304 	return (error);
305 }
306 
307 static int
308 linux_prison_set(void *obj, void *data)
309 {
310 	struct linux_prison *lpr;
311 	struct prison *pr = obj;
312 	struct vfsoptlist *opts = data;
313 	char *osname, *osrelease;
314 	int error, gotversion, jsys, len, oss_version;
315 
316 	/* Set the parameters, which should be correct. */
317 	error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
318 	if (error == ENOENT)
319 		jsys = -1;
320 	error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
321 	if (error == ENOENT)
322 		osname = NULL;
323 	else
324 		jsys = JAIL_SYS_NEW;
325 	error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
326 	if (error == ENOENT)
327 		osrelease = NULL;
328 	else
329 		jsys = JAIL_SYS_NEW;
330 	error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
331 	    sizeof(oss_version));
332 	if (error == ENOENT)
333 		gotversion = 0;
334 	else {
335 		gotversion = 1;
336 		jsys = JAIL_SYS_NEW;
337 	}
338 	switch (jsys) {
339 	case JAIL_SYS_INHERIT:
340 		/* "linux=inherit": inherit the parent's Linux info. */
341 		mtx_lock(&pr->pr_mtx);
342 		osd_jail_del(pr, linux_osd_jail_slot);
343 		mtx_unlock(&pr->pr_mtx);
344 		break;
345 	case JAIL_SYS_NEW:
346 		/*
347 		 * "linux=new" or "linux.*":
348 		 * the prison gets its own Linux info.
349 		 */
350 		linux_alloc_prison(pr, &lpr);
351 		if (osrelease) {
352 			(void)linux_map_osrel(osrelease, &lpr->pr_osrel);
353 			strlcpy(lpr->pr_osrelease, osrelease,
354 			    LINUX_MAX_UTSNAME);
355 		}
356 		if (osname)
357 			strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
358 		if (gotversion)
359 			lpr->pr_oss_version = oss_version;
360 		mtx_unlock(&pr->pr_mtx);
361 	}
362 
363 	return (0);
364 }
365 
366 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
367 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
368     "Jail Linux kernel OS name");
369 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
370     "Jail Linux kernel OS release");
371 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
372     "I", "Jail Linux OSS version");
373 
374 static int
375 linux_prison_get(void *obj, void *data)
376 {
377 	struct linux_prison *lpr;
378 	struct prison *ppr;
379 	struct prison *pr = obj;
380 	struct vfsoptlist *opts = data;
381 	int error, i;
382 
383 	static int version0;
384 
385 	/* See if this prison is the one with the Linux info. */
386 	lpr = linux_find_prison(pr, &ppr);
387 	i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
388 	error = vfs_setopt(opts, "linux", &i, sizeof(i));
389 	if (error != 0 && error != ENOENT)
390 		goto done;
391 	if (i) {
392 		error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
393 		if (error != 0 && error != ENOENT)
394 			goto done;
395 		error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
396 		if (error != 0 && error != ENOENT)
397 			goto done;
398 		error = vfs_setopt(opts, "linux.oss_version",
399 		    &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
400 		if (error != 0 && error != ENOENT)
401 			goto done;
402 	} else {
403 		/*
404 		 * If this prison is inheriting its Linux info, report
405 		 * empty/zero parameters.
406 		 */
407 		error = vfs_setopts(opts, "linux.osname", "");
408 		if (error != 0 && error != ENOENT)
409 			goto done;
410 		error = vfs_setopts(opts, "linux.osrelease", "");
411 		if (error != 0 && error != ENOENT)
412 			goto done;
413 		error = vfs_setopt(opts, "linux.oss_version", &version0,
414 		    sizeof(lpr->pr_oss_version));
415 		if (error != 0 && error != ENOENT)
416 			goto done;
417 	}
418 	error = 0;
419 
420  done:
421 	mtx_unlock(&ppr->pr_mtx);
422 
423 	return (error);
424 }
425 
426 static void
427 linux_prison_destructor(void *data)
428 {
429 
430 	free(data, M_PRISON);
431 }
432 
433 void
434 linux_osd_jail_register(void)
435 {
436 	struct prison *pr;
437 	osd_method_t methods[PR_MAXMETHOD] = {
438 	    [PR_METHOD_CREATE] =	linux_prison_create,
439 	    [PR_METHOD_GET] =		linux_prison_get,
440 	    [PR_METHOD_SET] =		linux_prison_set,
441 	    [PR_METHOD_CHECK] =		linux_prison_check
442 	};
443 
444 	linux_osd_jail_slot =
445 	    osd_jail_register(linux_prison_destructor, methods);
446 	/* Copy the system Linux info to any current prisons. */
447 	sx_slock(&allprison_lock);
448 	TAILQ_FOREACH(pr, &allprison, pr_list)
449 		linux_alloc_prison(pr, NULL);
450 	sx_sunlock(&allprison_lock);
451 }
452 
453 void
454 linux_osd_jail_deregister(void)
455 {
456 
457 	osd_jail_deregister(linux_osd_jail_slot);
458 }
459 
460 void
461 linux_get_osname(struct thread *td, char *dst)
462 {
463 	struct prison *pr;
464 	struct linux_prison *lpr;
465 
466 	lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
467 	bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
468 	mtx_unlock(&pr->pr_mtx);
469 }
470 
471 static int
472 linux_set_osname(struct thread *td, char *osname)
473 {
474 	struct prison *pr;
475 	struct linux_prison *lpr;
476 
477 	lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
478 	strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
479 	mtx_unlock(&pr->pr_mtx);
480 
481 	return (0);
482 }
483 
484 void
485 linux_get_osrelease(struct thread *td, char *dst)
486 {
487 	struct prison *pr;
488 	struct linux_prison *lpr;
489 
490 	lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
491 	bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
492 	mtx_unlock(&pr->pr_mtx);
493 }
494 
495 int
496 linux_kernver(struct thread *td)
497 {
498 	struct prison *pr;
499 	struct linux_prison *lpr;
500 	int osrel;
501 
502 	lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
503 	osrel = lpr->pr_osrel;
504 	mtx_unlock(&pr->pr_mtx);
505 
506 	return (osrel);
507 }
508 
509 static int
510 linux_set_osrelease(struct thread *td, char *osrelease)
511 {
512 	struct prison *pr;
513 	struct linux_prison *lpr;
514 	int error;
515 
516 	lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
517 	error = linux_map_osrel(osrelease, &lpr->pr_osrel);
518 	if (error == 0)
519 		strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
520 	mtx_unlock(&pr->pr_mtx);
521 
522 	return (error);
523 }
524 
525 int
526 linux_get_oss_version(struct thread *td)
527 {
528 	struct prison *pr;
529 	struct linux_prison *lpr;
530 	int version;
531 
532 	lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
533 	version = lpr->pr_oss_version;
534 	mtx_unlock(&pr->pr_mtx);
535 
536 	return (version);
537 }
538 
539 static int
540 linux_set_oss_version(struct thread *td, int oss_version)
541 {
542 	struct prison *pr;
543 	struct linux_prison *lpr;
544 
545 	lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
546 	lpr->pr_oss_version = oss_version;
547 	mtx_unlock(&pr->pr_mtx);
548 
549 	return (0);
550 }
551