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