2 * SPDX-License-Identifier: BSD-3-Clause
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 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
46 #include <compat/linux/linux_mib.h>
47 #include <compat/linux/linux_misc.h>
50 char pr_osname[LINUX_MAX_UTSNAME];
51 char pr_osrelease[LINUX_MAX_UTSNAME];
56 static struct linux_prison lprison0 = {
58 .pr_osrelease = LINUX_VERSION_STR,
59 .pr_oss_version = 0x030600,
60 .pr_osrel = LINUX_VERSION_CODE
63 static unsigned linux_osd_jail_slot;
65 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0, "Linux mode");
67 static int linux_set_osname(struct thread *td, char *osname);
68 static int linux_set_osrelease(struct thread *td, char *osrelease);
69 static int linux_set_oss_version(struct thread *td, int oss_version);
72 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
74 char osname[LINUX_MAX_UTSNAME];
77 linux_get_osname(req->td, osname);
78 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
79 if (error != 0 || req->newptr == NULL)
81 error = linux_set_osname(req->td, osname);
86 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
87 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
88 0, 0, linux_sysctl_osname, "A",
89 "Linux kernel OS name");
92 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
94 char osrelease[LINUX_MAX_UTSNAME];
97 linux_get_osrelease(req->td, osrelease);
98 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
99 if (error != 0 || req->newptr == NULL)
101 error = linux_set_osrelease(req->td, osrelease);
106 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
107 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
108 0, 0, linux_sysctl_osrelease, "A",
109 "Linux kernel OS release");
112 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
117 oss_version = linux_get_oss_version(req->td);
118 error = sysctl_handle_int(oidp, &oss_version, 0, req);
119 if (error != 0 || req->newptr == NULL)
121 error = linux_set_oss_version(req->td, oss_version);
126 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
127 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
128 0, 0, linux_sysctl_oss_version, "I",
129 "Linux OSS version");
132 * Map the osrelease into integer
135 linux_map_osrel(char *osrelease, int *osrel)
137 char *sep, *eosrelease;
138 int len, v0, v1, v2, v;
140 len = strlen(osrelease);
141 eosrelease = osrelease + len;
142 v0 = strtol(osrelease, &sep, 10);
143 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
146 v1 = strtol(osrelease, &sep, 10);
147 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
150 v2 = strtol(osrelease, &sep, 10);
151 if (osrelease == sep || sep != eosrelease)
154 v = v0 * 1000000 + v1 * 1000 + v2;
165 * Find a prison with Linux info.
166 * Return the Linux info and the (locked) prison.
168 static struct linux_prison *
169 linux_find_prison(struct prison *spr, struct prison **prp)
172 struct linux_prison *lpr;
174 for (pr = spr;; pr = pr->pr_parent) {
175 mtx_lock(&pr->pr_mtx);
176 lpr = (pr == &prison0)
178 : osd_jail_get(pr, linux_osd_jail_slot);
181 mtx_unlock(&pr->pr_mtx);
189 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
190 * the Linux info and lock the prison.
193 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
196 struct linux_prison *lpr, *nlpr;
199 /* If this prison already has Linux info, return that. */
200 lpr = linux_find_prison(pr, &ppr);
204 * Allocate a new info record. Then check again, in case something
205 * changed during the allocation.
207 mtx_unlock(&ppr->pr_mtx);
208 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
209 rsv = osd_reserve(linux_osd_jail_slot);
210 lpr = linux_find_prison(pr, &ppr);
212 free(nlpr, M_PRISON);
213 osd_free_reserved(rsv);
216 /* Inherit the initial values from the ancestor. */
217 mtx_lock(&pr->pr_mtx);
218 (void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
219 bcopy(lpr, nlpr, sizeof(*lpr));
221 mtx_unlock(&ppr->pr_mtx);
226 mtx_unlock(&pr->pr_mtx);
230 * Jail OSD methods for Linux prison data.
233 linux_prison_create(void *obj, void *data)
235 struct prison *pr = obj;
236 struct vfsoptlist *opts = data;
239 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
240 jsys == JAIL_SYS_INHERIT)
243 * Inherit a prison's initial values from its parent
244 * (different from JAIL_SYS_INHERIT which also inherits changes).
246 linux_alloc_prison(pr, NULL);
251 linux_prison_check(void *obj __unused, void *data)
253 struct vfsoptlist *opts = data;
254 char *osname, *osrelease;
255 int error, jsys, len, oss_version;
257 /* Check that the parameters are correct. */
258 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
259 if (error != ENOENT) {
262 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
265 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
266 if (error != ENOENT) {
269 if (len == 0 || osname[len - 1] != '\0')
271 if (len > LINUX_MAX_UTSNAME) {
272 vfs_opterror(opts, "linux.osname too long");
273 return (ENAMETOOLONG);
276 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
277 if (error != ENOENT) {
280 if (len == 0 || osrelease[len - 1] != '\0')
282 if (len > LINUX_MAX_UTSNAME) {
283 vfs_opterror(opts, "linux.osrelease too long");
284 return (ENAMETOOLONG);
286 error = linux_map_osrel(osrelease, NULL);
288 vfs_opterror(opts, "linux.osrelease format error");
292 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
293 sizeof(oss_version));
301 linux_prison_set(void *obj, void *data)
303 struct linux_prison *lpr;
304 struct prison *pr = obj;
305 struct vfsoptlist *opts = data;
306 char *osname, *osrelease;
307 int error, gotversion, jsys, len, oss_version;
309 /* Set the parameters, which should be correct. */
310 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
313 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
318 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
323 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
324 sizeof(oss_version));
332 case JAIL_SYS_INHERIT:
333 /* "linux=inherit": inherit the parent's Linux info. */
334 mtx_lock(&pr->pr_mtx);
335 osd_jail_del(pr, linux_osd_jail_slot);
336 mtx_unlock(&pr->pr_mtx);
340 * "linux=new" or "linux.*":
341 * the prison gets its own Linux info.
343 linux_alloc_prison(pr, &lpr);
345 (void)linux_map_osrel(osrelease, &lpr->pr_osrel);
346 strlcpy(lpr->pr_osrelease, osrelease,
350 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
352 lpr->pr_oss_version = oss_version;
353 mtx_unlock(&pr->pr_mtx);
359 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
360 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
361 "Jail Linux kernel OS name");
362 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
363 "Jail Linux kernel OS release");
364 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
365 "I", "Jail Linux OSS version");
368 linux_prison_get(void *obj, void *data)
370 struct linux_prison *lpr;
372 struct prison *pr = obj;
373 struct vfsoptlist *opts = data;
378 /* See if this prison is the one with the Linux info. */
379 lpr = linux_find_prison(pr, &ppr);
380 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
381 error = vfs_setopt(opts, "linux", &i, sizeof(i));
382 if (error != 0 && error != ENOENT)
385 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
386 if (error != 0 && error != ENOENT)
388 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
389 if (error != 0 && error != ENOENT)
391 error = vfs_setopt(opts, "linux.oss_version",
392 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
393 if (error != 0 && error != ENOENT)
397 * If this prison is inheriting its Linux info, report
398 * empty/zero parameters.
400 error = vfs_setopts(opts, "linux.osname", "");
401 if (error != 0 && error != ENOENT)
403 error = vfs_setopts(opts, "linux.osrelease", "");
404 if (error != 0 && error != ENOENT)
406 error = vfs_setopt(opts, "linux.oss_version", &version0,
407 sizeof(lpr->pr_oss_version));
408 if (error != 0 && error != ENOENT)
414 mtx_unlock(&ppr->pr_mtx);
420 linux_prison_destructor(void *data)
423 free(data, M_PRISON);
427 linux_osd_jail_register(void)
430 osd_method_t methods[PR_MAXMETHOD] = {
431 [PR_METHOD_CREATE] = linux_prison_create,
432 [PR_METHOD_GET] = linux_prison_get,
433 [PR_METHOD_SET] = linux_prison_set,
434 [PR_METHOD_CHECK] = linux_prison_check
437 linux_osd_jail_slot =
438 osd_jail_register(linux_prison_destructor, methods);
439 /* Copy the system linux info to any current prisons. */
440 sx_slock(&allprison_lock);
441 TAILQ_FOREACH(pr, &allprison, pr_list)
442 linux_alloc_prison(pr, NULL);
443 sx_sunlock(&allprison_lock);
447 linux_osd_jail_deregister(void)
450 osd_jail_deregister(linux_osd_jail_slot);
454 linux_get_osname(struct thread *td, char *dst)
457 struct linux_prison *lpr;
459 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
460 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
461 mtx_unlock(&pr->pr_mtx);
465 linux_set_osname(struct thread *td, char *osname)
468 struct linux_prison *lpr;
470 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
471 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
472 mtx_unlock(&pr->pr_mtx);
478 linux_get_osrelease(struct thread *td, char *dst)
481 struct linux_prison *lpr;
483 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
484 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
485 mtx_unlock(&pr->pr_mtx);
489 linux_kernver(struct thread *td)
492 struct linux_prison *lpr;
495 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
496 osrel = lpr->pr_osrel;
497 mtx_unlock(&pr->pr_mtx);
503 linux_set_osrelease(struct thread *td, char *osrelease)
506 struct linux_prison *lpr;
509 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
510 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
512 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
513 mtx_unlock(&pr->pr_mtx);
519 linux_get_oss_version(struct thread *td)
522 struct linux_prison *lpr;
525 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
526 version = lpr->pr_oss_version;
527 mtx_unlock(&pr->pr_mtx);
533 linux_set_oss_version(struct thread *td, int oss_version)
536 struct linux_prison *lpr;
538 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
539 lpr->pr_oss_version = oss_version;
540 mtx_unlock(&pr->pr_mtx);