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