2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
44 #include <compat/linux/linux_mib.h>
45 #include <compat/linux/linux_misc.h>
48 char pr_osname[LINUX_MAX_UTSNAME];
49 char pr_osrelease[LINUX_MAX_UTSNAME];
54 static struct linux_prison lprison0 = {
56 .pr_osrelease = LINUX_VERSION_STR,
57 .pr_oss_version = 0x030600,
58 .pr_osrel = LINUX_VERSION_CODE
61 static unsigned linux_osd_jail_slot;
63 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
67 SYSCTL_INT(_compat_linux, OID_AUTO, debug, CTLFLAG_RWTUN,
68 &linux_debug, 0, "Log warnings from linux(4); or 0 to disable");
70 int linux_default_openfiles = 1024;
71 SYSCTL_INT(_compat_linux, OID_AUTO, default_openfiles, CTLFLAG_RWTUN,
72 &linux_default_openfiles, 0,
73 "Default soft openfiles resource limit, or -1 for unlimited");
75 int linux_default_stacksize = 8 * 1024 * 1024;
76 SYSCTL_INT(_compat_linux, OID_AUTO, default_stacksize, CTLFLAG_RWTUN,
77 &linux_default_stacksize, 0,
78 "Default soft stack size resource limit, or -1 for unlimited");
80 int linux_ignore_ip_recverr = 1;
81 SYSCTL_INT(_compat_linux, OID_AUTO, ignore_ip_recverr, CTLFLAG_RWTUN,
82 &linux_ignore_ip_recverr, 0, "Ignore enabling IP_RECVERR");
84 int linux_preserve_vstatus = 0;
85 SYSCTL_INT(_compat_linux, OID_AUTO, preserve_vstatus, CTLFLAG_RWTUN,
86 &linux_preserve_vstatus, 0, "Preserve VSTATUS termios(4) flag");
88 bool linux_map_sched_prio = true;
89 SYSCTL_BOOL(_compat_linux, OID_AUTO, map_sched_prio, CTLFLAG_RDTUN,
90 &linux_map_sched_prio, 0, "Map scheduler priorities to Linux priorities "
91 "(not POSIX compliant)");
93 int linux_use_emul_path = 1;
94 SYSCTL_INT(_compat_linux, OID_AUTO, use_emul_path, CTLFLAG_RWTUN,
95 &linux_use_emul_path, 0, "Use linux.compat.emul_path");
97 static int linux_set_osname(struct thread *td, char *osname);
98 static int linux_set_osrelease(struct thread *td, char *osrelease);
99 static int linux_set_oss_version(struct thread *td, int oss_version);
102 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
104 char osname[LINUX_MAX_UTSNAME];
107 linux_get_osname(req->td, osname);
108 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
109 if (error != 0 || req->newptr == NULL)
111 error = linux_set_osname(req->td, osname);
116 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
117 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
118 0, 0, linux_sysctl_osname, "A",
119 "Linux kernel OS name");
122 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
124 char osrelease[LINUX_MAX_UTSNAME];
127 linux_get_osrelease(req->td, osrelease);
128 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
129 if (error != 0 || req->newptr == NULL)
131 error = linux_set_osrelease(req->td, osrelease);
136 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
137 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
138 0, 0, linux_sysctl_osrelease, "A",
139 "Linux kernel OS release");
142 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
147 oss_version = linux_get_oss_version(req->td);
148 error = sysctl_handle_int(oidp, &oss_version, 0, req);
149 if (error != 0 || req->newptr == NULL)
151 error = linux_set_oss_version(req->td, oss_version);
156 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
157 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
158 0, 0, linux_sysctl_oss_version, "I",
159 "Linux OSS version");
162 * Map the osrelease into integer
165 linux_map_osrel(char *osrelease, int *osrel)
167 char *sep, *eosrelease;
168 int len, v0, v1, v2, v;
170 len = strlen(osrelease);
171 eosrelease = osrelease + len;
172 v0 = strtol(osrelease, &sep, 10);
173 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
176 v1 = strtol(osrelease, &sep, 10);
177 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
180 v2 = strtol(osrelease, &sep, 10);
181 if (osrelease == sep ||
182 (sep != eosrelease && (sep + 1 >= eosrelease || *sep != '-')))
185 v = LINUX_KERNVER(v0, v1, v2);
186 if (v < LINUX_KERNVER(1, 0, 0))
196 * Find a prison with Linux info.
197 * Return the Linux info and the (locked) prison.
199 static struct linux_prison *
200 linux_find_prison(struct prison *spr, struct prison **prp)
203 struct linux_prison *lpr;
205 for (pr = spr;; pr = pr->pr_parent) {
206 mtx_lock(&pr->pr_mtx);
207 lpr = (pr == &prison0)
209 : osd_jail_get(pr, linux_osd_jail_slot);
212 mtx_unlock(&pr->pr_mtx);
220 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
221 * the Linux info and lock the prison.
224 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
227 struct linux_prison *lpr, *nlpr;
230 /* If this prison already has Linux info, return that. */
231 lpr = linux_find_prison(pr, &ppr);
235 * Allocate a new info record. Then check again, in case something
236 * changed during the allocation.
238 mtx_unlock(&ppr->pr_mtx);
239 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
240 rsv = osd_reserve(linux_osd_jail_slot);
241 lpr = linux_find_prison(pr, &ppr);
243 free(nlpr, M_PRISON);
244 osd_free_reserved(rsv);
247 /* Inherit the initial values from the ancestor. */
248 mtx_lock(&pr->pr_mtx);
249 (void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
250 bcopy(lpr, nlpr, sizeof(*lpr));
252 mtx_unlock(&ppr->pr_mtx);
257 mtx_unlock(&pr->pr_mtx);
261 * Jail OSD methods for Linux prison data.
264 linux_prison_create(void *obj, void *data)
266 struct prison *pr = obj;
267 struct vfsoptlist *opts = data;
270 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
271 jsys == JAIL_SYS_INHERIT)
274 * Inherit a prison's initial values from its parent
275 * (different from JAIL_SYS_INHERIT which also inherits changes).
277 linux_alloc_prison(pr, NULL);
282 linux_prison_check(void *obj __unused, void *data)
284 struct vfsoptlist *opts = data;
285 char *osname, *osrelease;
286 int error, jsys, len, oss_version;
288 /* Check that the parameters are correct. */
289 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
290 if (error != ENOENT) {
293 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
296 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
297 if (error != ENOENT) {
300 if (len == 0 || osname[len - 1] != '\0')
302 if (len > LINUX_MAX_UTSNAME) {
303 vfs_opterror(opts, "linux.osname too long");
304 return (ENAMETOOLONG);
307 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
308 if (error != ENOENT) {
311 if (len == 0 || osrelease[len - 1] != '\0')
313 if (len > LINUX_MAX_UTSNAME) {
314 vfs_opterror(opts, "linux.osrelease too long");
315 return (ENAMETOOLONG);
317 error = linux_map_osrel(osrelease, NULL);
319 vfs_opterror(opts, "linux.osrelease format error");
323 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
324 sizeof(oss_version));
332 linux_prison_set(void *obj, void *data)
334 struct linux_prison *lpr;
335 struct prison *pr = obj;
336 struct vfsoptlist *opts = data;
337 char *osname, *osrelease;
338 int error, gotversion, jsys, len, oss_version;
340 /* Set the parameters, which should be correct. */
341 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
344 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
349 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
354 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
355 sizeof(oss_version));
363 case JAIL_SYS_INHERIT:
364 /* "linux=inherit": inherit the parent's Linux info. */
365 mtx_lock(&pr->pr_mtx);
366 osd_jail_del(pr, linux_osd_jail_slot);
367 mtx_unlock(&pr->pr_mtx);
371 * "linux=new" or "linux.*":
372 * the prison gets its own Linux info.
374 linux_alloc_prison(pr, &lpr);
376 (void)linux_map_osrel(osrelease, &lpr->pr_osrel);
377 strlcpy(lpr->pr_osrelease, osrelease,
381 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
383 lpr->pr_oss_version = oss_version;
384 mtx_unlock(&pr->pr_mtx);
390 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
391 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
392 "Jail Linux kernel OS name");
393 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
394 "Jail Linux kernel OS release");
395 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
396 "I", "Jail Linux OSS version");
399 linux_prison_get(void *obj, void *data)
401 struct linux_prison *lpr;
403 struct prison *pr = obj;
404 struct vfsoptlist *opts = data;
409 /* See if this prison is the one with the Linux info. */
410 lpr = linux_find_prison(pr, &ppr);
411 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
412 error = vfs_setopt(opts, "linux", &i, sizeof(i));
413 if (error != 0 && error != ENOENT)
416 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
417 if (error != 0 && error != ENOENT)
419 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
420 if (error != 0 && error != ENOENT)
422 error = vfs_setopt(opts, "linux.oss_version",
423 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
424 if (error != 0 && error != ENOENT)
428 * If this prison is inheriting its Linux info, report
429 * empty/zero parameters.
431 error = vfs_setopts(opts, "linux.osname", "");
432 if (error != 0 && error != ENOENT)
434 error = vfs_setopts(opts, "linux.osrelease", "");
435 if (error != 0 && error != ENOENT)
437 error = vfs_setopt(opts, "linux.oss_version", &version0,
438 sizeof(lpr->pr_oss_version));
439 if (error != 0 && error != ENOENT)
445 mtx_unlock(&ppr->pr_mtx);
451 linux_prison_destructor(void *data)
454 free(data, M_PRISON);
458 linux_osd_jail_register(void)
461 osd_method_t methods[PR_MAXMETHOD] = {
462 [PR_METHOD_CREATE] = linux_prison_create,
463 [PR_METHOD_GET] = linux_prison_get,
464 [PR_METHOD_SET] = linux_prison_set,
465 [PR_METHOD_CHECK] = linux_prison_check
468 linux_osd_jail_slot =
469 osd_jail_register(linux_prison_destructor, methods);
470 /* Copy the system Linux info to any current prisons. */
471 sx_slock(&allprison_lock);
472 TAILQ_FOREACH(pr, &allprison, pr_list)
473 linux_alloc_prison(pr, NULL);
474 sx_sunlock(&allprison_lock);
478 linux_osd_jail_deregister(void)
481 osd_jail_deregister(linux_osd_jail_slot);
485 linux_get_osname(struct thread *td, char *dst)
488 struct linux_prison *lpr;
490 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
491 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
492 mtx_unlock(&pr->pr_mtx);
496 linux_set_osname(struct thread *td, char *osname)
499 struct linux_prison *lpr;
501 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
502 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
503 mtx_unlock(&pr->pr_mtx);
509 linux_get_osrelease(struct thread *td, char *dst)
512 struct linux_prison *lpr;
514 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
515 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
516 mtx_unlock(&pr->pr_mtx);
520 linux_kernver(struct thread *td)
523 struct linux_prison *lpr;
526 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
527 osrel = lpr->pr_osrel;
528 mtx_unlock(&pr->pr_mtx);
534 linux_set_osrelease(struct thread *td, char *osrelease)
537 struct linux_prison *lpr;
540 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
541 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
543 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
544 mtx_unlock(&pr->pr_mtx);
550 linux_get_oss_version(struct thread *td)
553 struct linux_prison *lpr;
556 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
557 version = lpr->pr_oss_version;
558 mtx_unlock(&pr->pr_mtx);
564 linux_set_oss_version(struct thread *td, int oss_version)
567 struct linux_prison *lpr;
569 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
570 lpr->pr_oss_version = oss_version;
571 mtx_unlock(&pr->pr_mtx);