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, 0, "Linux mode");
65 static int linux_set_osname(struct thread *td, char *osname);
66 static int linux_set_osrelease(struct thread *td, char *osrelease);
67 static int linux_set_oss_version(struct thread *td, int oss_version);
70 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
72 char osname[LINUX_MAX_UTSNAME];
75 linux_get_osname(req->td, osname);
76 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
77 if (error != 0 || req->newptr == NULL)
79 error = linux_set_osname(req->td, osname);
84 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
85 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
86 0, 0, linux_sysctl_osname, "A",
87 "Linux kernel OS name");
90 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
92 char osrelease[LINUX_MAX_UTSNAME];
95 linux_get_osrelease(req->td, osrelease);
96 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
97 if (error != 0 || req->newptr == NULL)
99 error = linux_set_osrelease(req->td, osrelease);
104 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
105 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
106 0, 0, linux_sysctl_osrelease, "A",
107 "Linux kernel OS release");
110 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
115 oss_version = linux_get_oss_version(req->td);
116 error = sysctl_handle_int(oidp, &oss_version, 0, req);
117 if (error != 0 || req->newptr == NULL)
119 error = linux_set_oss_version(req->td, oss_version);
124 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
125 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
126 0, 0, linux_sysctl_oss_version, "I",
127 "Linux OSS version");
130 * Map the osrelease into integer
133 linux_map_osrel(char *osrelease, int *osrel)
135 char *sep, *eosrelease;
136 int len, v0, v1, v2, v;
138 len = strlen(osrelease);
139 eosrelease = osrelease + len;
140 v0 = strtol(osrelease, &sep, 10);
141 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
144 v1 = strtol(osrelease, &sep, 10);
145 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
148 v2 = strtol(osrelease, &sep, 10);
149 if (osrelease == sep ||
150 (sep != eosrelease && (sep + 1 >= eosrelease || *sep != '-')))
153 v = LINUX_KERNVER(v0, v1, v2);
154 if (v < LINUX_KERNVER(1, 0, 0))
164 * Find a prison with Linux info.
165 * Return the Linux info and the (locked) prison.
167 static struct linux_prison *
168 linux_find_prison(struct prison *spr, struct prison **prp)
171 struct linux_prison *lpr;
173 for (pr = spr;; pr = pr->pr_parent) {
174 mtx_lock(&pr->pr_mtx);
175 lpr = (pr == &prison0)
177 : osd_jail_get(pr, linux_osd_jail_slot);
180 mtx_unlock(&pr->pr_mtx);
188 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
189 * the Linux info and lock the prison.
192 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
195 struct linux_prison *lpr, *nlpr;
198 /* If this prison already has Linux info, return that. */
199 lpr = linux_find_prison(pr, &ppr);
203 * Allocate a new info record. Then check again, in case something
204 * changed during the allocation.
206 mtx_unlock(&ppr->pr_mtx);
207 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
208 rsv = osd_reserve(linux_osd_jail_slot);
209 lpr = linux_find_prison(pr, &ppr);
211 free(nlpr, M_PRISON);
212 osd_free_reserved(rsv);
215 /* Inherit the initial values from the ancestor. */
216 mtx_lock(&pr->pr_mtx);
217 (void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
218 bcopy(lpr, nlpr, sizeof(*lpr));
220 mtx_unlock(&ppr->pr_mtx);
225 mtx_unlock(&pr->pr_mtx);
229 * Jail OSD methods for Linux prison data.
232 linux_prison_create(void *obj, void *data)
234 struct prison *pr = obj;
235 struct vfsoptlist *opts = data;
238 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
239 jsys == JAIL_SYS_INHERIT)
242 * Inherit a prison's initial values from its parent
243 * (different from JAIL_SYS_INHERIT which also inherits changes).
245 linux_alloc_prison(pr, NULL);
250 linux_prison_check(void *obj __unused, void *data)
252 struct vfsoptlist *opts = data;
253 char *osname, *osrelease;
254 int error, jsys, len, oss_version;
256 /* Check that the parameters are correct. */
257 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
258 if (error != ENOENT) {
261 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
264 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
265 if (error != ENOENT) {
268 if (len == 0 || osname[len - 1] != '\0')
270 if (len > LINUX_MAX_UTSNAME) {
271 vfs_opterror(opts, "linux.osname too long");
272 return (ENAMETOOLONG);
275 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
276 if (error != ENOENT) {
279 if (len == 0 || osrelease[len - 1] != '\0')
281 if (len > LINUX_MAX_UTSNAME) {
282 vfs_opterror(opts, "linux.osrelease too long");
283 return (ENAMETOOLONG);
285 error = linux_map_osrel(osrelease, NULL);
287 vfs_opterror(opts, "linux.osrelease format error");
291 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
292 sizeof(oss_version));
300 linux_prison_set(void *obj, void *data)
302 struct linux_prison *lpr;
303 struct prison *pr = obj;
304 struct vfsoptlist *opts = data;
305 char *osname, *osrelease;
306 int error, gotversion, jsys, len, oss_version;
308 /* Set the parameters, which should be correct. */
309 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
312 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
317 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
322 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
323 sizeof(oss_version));
331 case JAIL_SYS_INHERIT:
332 /* "linux=inherit": inherit the parent's Linux info. */
333 mtx_lock(&pr->pr_mtx);
334 osd_jail_del(pr, linux_osd_jail_slot);
335 mtx_unlock(&pr->pr_mtx);
339 * "linux=new" or "linux.*":
340 * the prison gets its own Linux info.
342 linux_alloc_prison(pr, &lpr);
344 (void)linux_map_osrel(osrelease, &lpr->pr_osrel);
345 strlcpy(lpr->pr_osrelease, osrelease,
349 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
351 lpr->pr_oss_version = oss_version;
352 mtx_unlock(&pr->pr_mtx);
358 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
359 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
360 "Jail Linux kernel OS name");
361 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
362 "Jail Linux kernel OS release");
363 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
364 "I", "Jail Linux OSS version");
367 linux_prison_get(void *obj, void *data)
369 struct linux_prison *lpr;
371 struct prison *pr = obj;
372 struct vfsoptlist *opts = data;
377 /* See if this prison is the one with the Linux info. */
378 lpr = linux_find_prison(pr, &ppr);
379 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
380 error = vfs_setopt(opts, "linux", &i, sizeof(i));
381 if (error != 0 && error != ENOENT)
384 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
385 if (error != 0 && error != ENOENT)
387 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
388 if (error != 0 && error != ENOENT)
390 error = vfs_setopt(opts, "linux.oss_version",
391 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
392 if (error != 0 && error != ENOENT)
396 * If this prison is inheriting its Linux info, report
397 * empty/zero parameters.
399 error = vfs_setopts(opts, "linux.osname", "");
400 if (error != 0 && error != ENOENT)
402 error = vfs_setopts(opts, "linux.osrelease", "");
403 if (error != 0 && error != ENOENT)
405 error = vfs_setopt(opts, "linux.oss_version", &version0,
406 sizeof(lpr->pr_oss_version));
407 if (error != 0 && error != ENOENT)
413 mtx_unlock(&ppr->pr_mtx);
419 linux_prison_destructor(void *data)
422 free(data, M_PRISON);
426 linux_osd_jail_register(void)
429 osd_method_t methods[PR_MAXMETHOD] = {
430 [PR_METHOD_CREATE] = linux_prison_create,
431 [PR_METHOD_GET] = linux_prison_get,
432 [PR_METHOD_SET] = linux_prison_set,
433 [PR_METHOD_CHECK] = linux_prison_check
436 linux_osd_jail_slot =
437 osd_jail_register(linux_prison_destructor, methods);
438 /* Copy the system Linux info to any current prisons. */
439 sx_slock(&allprison_lock);
440 TAILQ_FOREACH(pr, &allprison, pr_list)
441 linux_alloc_prison(pr, NULL);
442 sx_sunlock(&allprison_lock);
446 linux_osd_jail_deregister(void)
449 osd_jail_deregister(linux_osd_jail_slot);
453 linux_get_osname(struct thread *td, char *dst)
456 struct linux_prison *lpr;
458 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
459 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
460 mtx_unlock(&pr->pr_mtx);
464 linux_set_osname(struct thread *td, char *osname)
467 struct linux_prison *lpr;
469 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
470 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
471 mtx_unlock(&pr->pr_mtx);
477 linux_get_osrelease(struct thread *td, char *dst)
480 struct linux_prison *lpr;
482 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
483 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
484 mtx_unlock(&pr->pr_mtx);
488 linux_kernver(struct thread *td)
491 struct linux_prison *lpr;
494 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
495 osrel = lpr->pr_osrel;
496 mtx_unlock(&pr->pr_mtx);
502 linux_set_osrelease(struct thread *td, char *osrelease)
505 struct linux_prison *lpr;
508 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
509 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
511 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
512 mtx_unlock(&pr->pr_mtx);
518 linux_get_oss_version(struct thread *td)
521 struct linux_prison *lpr;
524 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
525 version = lpr->pr_oss_version;
526 mtx_unlock(&pr->pr_mtx);
532 linux_set_oss_version(struct thread *td, int oss_version)
535 struct linux_prison *lpr;
537 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
538 lpr->pr_oss_version = oss_version;
539 mtx_unlock(&pr->pr_mtx);