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,
66 int linux_default_openfiles = 1024;
67 SYSCTL_INT(_compat_linux, OID_AUTO, default_openfiles, CTLFLAG_RWTUN,
68 &linux_default_openfiles, 0,
69 "Default soft openfiles resource limit, or -1 for unlimited");
71 int linux_ignore_ip_recverr = 1;
72 SYSCTL_INT(_compat_linux, OID_AUTO, ignore_ip_recverr, CTLFLAG_RWTUN,
73 &linux_ignore_ip_recverr, 0, "Ignore enabling IP_RECVERR");
75 int linux_preserve_vstatus = 0;
76 SYSCTL_INT(_compat_linux, OID_AUTO, preserve_vstatus, CTLFLAG_RWTUN,
77 &linux_preserve_vstatus, 0, "Preserve VSTATUS termios(4) flag");
79 bool linux_map_sched_prio = true;
80 SYSCTL_BOOL(_compat_linux, OID_AUTO, map_sched_prio, CTLFLAG_RDTUN,
81 &linux_map_sched_prio, 0, "Map scheduler priorities to Linux priorities "
82 "(not POSIX compliant)");
84 static int linux_set_osname(struct thread *td, char *osname);
85 static int linux_set_osrelease(struct thread *td, char *osrelease);
86 static int linux_set_oss_version(struct thread *td, int oss_version);
89 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
91 char osname[LINUX_MAX_UTSNAME];
94 linux_get_osname(req->td, osname);
95 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
96 if (error != 0 || req->newptr == NULL)
98 error = linux_set_osname(req->td, osname);
103 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
104 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
105 0, 0, linux_sysctl_osname, "A",
106 "Linux kernel OS name");
109 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
111 char osrelease[LINUX_MAX_UTSNAME];
114 linux_get_osrelease(req->td, osrelease);
115 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
116 if (error != 0 || req->newptr == NULL)
118 error = linux_set_osrelease(req->td, osrelease);
123 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
124 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
125 0, 0, linux_sysctl_osrelease, "A",
126 "Linux kernel OS release");
129 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
134 oss_version = linux_get_oss_version(req->td);
135 error = sysctl_handle_int(oidp, &oss_version, 0, req);
136 if (error != 0 || req->newptr == NULL)
138 error = linux_set_oss_version(req->td, oss_version);
143 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
144 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
145 0, 0, linux_sysctl_oss_version, "I",
146 "Linux OSS version");
149 * Map the osrelease into integer
152 linux_map_osrel(char *osrelease, int *osrel)
154 char *sep, *eosrelease;
155 int len, v0, v1, v2, v;
157 len = strlen(osrelease);
158 eosrelease = osrelease + len;
159 v0 = strtol(osrelease, &sep, 10);
160 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
163 v1 = strtol(osrelease, &sep, 10);
164 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
167 v2 = strtol(osrelease, &sep, 10);
168 if (osrelease == sep ||
169 (sep != eosrelease && (sep + 1 >= eosrelease || *sep != '-')))
172 v = LINUX_KERNVER(v0, v1, v2);
173 if (v < LINUX_KERNVER(1, 0, 0))
183 * Find a prison with Linux info.
184 * Return the Linux info and the (locked) prison.
186 static struct linux_prison *
187 linux_find_prison(struct prison *spr, struct prison **prp)
190 struct linux_prison *lpr;
192 for (pr = spr;; pr = pr->pr_parent) {
193 mtx_lock(&pr->pr_mtx);
194 lpr = (pr == &prison0)
196 : osd_jail_get(pr, linux_osd_jail_slot);
199 mtx_unlock(&pr->pr_mtx);
207 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
208 * the Linux info and lock the prison.
211 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
214 struct linux_prison *lpr, *nlpr;
217 /* If this prison already has Linux info, return that. */
218 lpr = linux_find_prison(pr, &ppr);
222 * Allocate a new info record. Then check again, in case something
223 * changed during the allocation.
225 mtx_unlock(&ppr->pr_mtx);
226 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
227 rsv = osd_reserve(linux_osd_jail_slot);
228 lpr = linux_find_prison(pr, &ppr);
230 free(nlpr, M_PRISON);
231 osd_free_reserved(rsv);
234 /* Inherit the initial values from the ancestor. */
235 mtx_lock(&pr->pr_mtx);
236 (void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
237 bcopy(lpr, nlpr, sizeof(*lpr));
239 mtx_unlock(&ppr->pr_mtx);
244 mtx_unlock(&pr->pr_mtx);
248 * Jail OSD methods for Linux prison data.
251 linux_prison_create(void *obj, void *data)
253 struct prison *pr = obj;
254 struct vfsoptlist *opts = data;
257 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
258 jsys == JAIL_SYS_INHERIT)
261 * Inherit a prison's initial values from its parent
262 * (different from JAIL_SYS_INHERIT which also inherits changes).
264 linux_alloc_prison(pr, NULL);
269 linux_prison_check(void *obj __unused, void *data)
271 struct vfsoptlist *opts = data;
272 char *osname, *osrelease;
273 int error, jsys, len, oss_version;
275 /* Check that the parameters are correct. */
276 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
277 if (error != ENOENT) {
280 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
283 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
284 if (error != ENOENT) {
287 if (len == 0 || osname[len - 1] != '\0')
289 if (len > LINUX_MAX_UTSNAME) {
290 vfs_opterror(opts, "linux.osname too long");
291 return (ENAMETOOLONG);
294 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
295 if (error != ENOENT) {
298 if (len == 0 || osrelease[len - 1] != '\0')
300 if (len > LINUX_MAX_UTSNAME) {
301 vfs_opterror(opts, "linux.osrelease too long");
302 return (ENAMETOOLONG);
304 error = linux_map_osrel(osrelease, NULL);
306 vfs_opterror(opts, "linux.osrelease format error");
310 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
311 sizeof(oss_version));
319 linux_prison_set(void *obj, void *data)
321 struct linux_prison *lpr;
322 struct prison *pr = obj;
323 struct vfsoptlist *opts = data;
324 char *osname, *osrelease;
325 int error, gotversion, jsys, len, oss_version;
327 /* Set the parameters, which should be correct. */
328 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
331 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
336 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
341 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
342 sizeof(oss_version));
350 case JAIL_SYS_INHERIT:
351 /* "linux=inherit": inherit the parent's Linux info. */
352 mtx_lock(&pr->pr_mtx);
353 osd_jail_del(pr, linux_osd_jail_slot);
354 mtx_unlock(&pr->pr_mtx);
358 * "linux=new" or "linux.*":
359 * the prison gets its own Linux info.
361 linux_alloc_prison(pr, &lpr);
363 (void)linux_map_osrel(osrelease, &lpr->pr_osrel);
364 strlcpy(lpr->pr_osrelease, osrelease,
368 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
370 lpr->pr_oss_version = oss_version;
371 mtx_unlock(&pr->pr_mtx);
377 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
378 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
379 "Jail Linux kernel OS name");
380 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
381 "Jail Linux kernel OS release");
382 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
383 "I", "Jail Linux OSS version");
386 linux_prison_get(void *obj, void *data)
388 struct linux_prison *lpr;
390 struct prison *pr = obj;
391 struct vfsoptlist *opts = data;
396 /* See if this prison is the one with the Linux info. */
397 lpr = linux_find_prison(pr, &ppr);
398 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
399 error = vfs_setopt(opts, "linux", &i, sizeof(i));
400 if (error != 0 && error != ENOENT)
403 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
404 if (error != 0 && error != ENOENT)
406 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
407 if (error != 0 && error != ENOENT)
409 error = vfs_setopt(opts, "linux.oss_version",
410 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
411 if (error != 0 && error != ENOENT)
415 * If this prison is inheriting its Linux info, report
416 * empty/zero parameters.
418 error = vfs_setopts(opts, "linux.osname", "");
419 if (error != 0 && error != ENOENT)
421 error = vfs_setopts(opts, "linux.osrelease", "");
422 if (error != 0 && error != ENOENT)
424 error = vfs_setopt(opts, "linux.oss_version", &version0,
425 sizeof(lpr->pr_oss_version));
426 if (error != 0 && error != ENOENT)
432 mtx_unlock(&ppr->pr_mtx);
438 linux_prison_destructor(void *data)
441 free(data, M_PRISON);
445 linux_osd_jail_register(void)
448 osd_method_t methods[PR_MAXMETHOD] = {
449 [PR_METHOD_CREATE] = linux_prison_create,
450 [PR_METHOD_GET] = linux_prison_get,
451 [PR_METHOD_SET] = linux_prison_set,
452 [PR_METHOD_CHECK] = linux_prison_check
455 linux_osd_jail_slot =
456 osd_jail_register(linux_prison_destructor, methods);
457 /* Copy the system Linux info to any current prisons. */
458 sx_slock(&allprison_lock);
459 TAILQ_FOREACH(pr, &allprison, pr_list)
460 linux_alloc_prison(pr, NULL);
461 sx_sunlock(&allprison_lock);
465 linux_osd_jail_deregister(void)
468 osd_jail_deregister(linux_osd_jail_slot);
472 linux_get_osname(struct thread *td, char *dst)
475 struct linux_prison *lpr;
477 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
478 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
479 mtx_unlock(&pr->pr_mtx);
483 linux_set_osname(struct thread *td, char *osname)
486 struct linux_prison *lpr;
488 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
489 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
490 mtx_unlock(&pr->pr_mtx);
496 linux_get_osrelease(struct thread *td, char *dst)
499 struct linux_prison *lpr;
501 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
502 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
503 mtx_unlock(&pr->pr_mtx);
507 linux_kernver(struct thread *td)
510 struct linux_prison *lpr;
513 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
514 osrel = lpr->pr_osrel;
515 mtx_unlock(&pr->pr_mtx);
521 linux_set_osrelease(struct thread *td, char *osrelease)
524 struct linux_prison *lpr;
527 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
528 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
530 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
531 mtx_unlock(&pr->pr_mtx);
537 linux_get_oss_version(struct thread *td)
540 struct linux_prison *lpr;
543 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
544 version = lpr->pr_oss_version;
545 mtx_unlock(&pr->pr_mtx);
551 linux_set_oss_version(struct thread *td, int oss_version)
554 struct linux_prison *lpr;
556 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
557 lpr->pr_oss_version = oss_version;
558 mtx_unlock(&pr->pr_mtx);