1 /*-
2 * Copyright (c) 1999 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/10.4/sys/compat/linux/linux_mib.c 293527 2016-01-09 16:08:22Z dchagin $");
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 = v0 * 1000000 + v1 * 1000 + v2;
153 if (v < 1000000)
154 return (EINVAL);
155
156 *osrel = v;
157
158 return (0);
159 }
160
161 /*
162 * Find a prison with Linux info.
163 * Return the Linux info and the (locked) prison.
164 */
165 static struct linux_prison *
166 linux_find_prison(struct prison *spr, struct prison **prp)
167 {
168 struct prison *pr;
169 struct linux_prison *lpr;
170
171 if (!linux_osd_jail_slot)
172 /* In case osd_register failed. */
173 spr = &prison0;
174 for (pr = spr;; pr = pr->pr_parent) {
175 mtx_lock(&pr->pr_mtx);
176 lpr = (pr == &prison0)
177 ? &lprison0
178 : osd_jail_get(pr, linux_osd_jail_slot);
179 if (lpr != NULL)
180 break;
181 mtx_unlock(&pr->pr_mtx);
182 }
183 *prp = pr;
184
185 return (lpr);
186 }
187
188 /*
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.
191 */
192 static int
193 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
194 {
195 struct prison *ppr;
196 struct linux_prison *lpr, *nlpr;
197 int error;
198
199 /* If this prison already has Linux info, return that. */
200 error = 0;
201 lpr = linux_find_prison(pr, &ppr);
202 if (ppr == pr)
203 goto done;
204 /*
205 * Allocate a new info record. Then check again, in case something
206 * changed during the allocation.
207 */
208 mtx_unlock(&ppr->pr_mtx);
209 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
210 lpr = linux_find_prison(pr, &ppr);
211 if (ppr == pr) {
212 free(nlpr, M_PRISON);
213 goto done;
214 }
215 /* Inherit the initial values from the ancestor. */
216 mtx_lock(&pr->pr_mtx);
217 error = osd_jail_set(pr, linux_osd_jail_slot, nlpr);
218 if (error == 0) {
219 bcopy(lpr, nlpr, sizeof(*lpr));
220 lpr = nlpr;
221 } else {
222 free(nlpr, M_PRISON);
223 lpr = NULL;
224 }
225 mtx_unlock(&ppr->pr_mtx);
226 done:
227 if (lprp != NULL)
228 *lprp = lpr;
229 else
230 mtx_unlock(&pr->pr_mtx);
231
232 return (error);
233 }
234
235 /*
236 * Jail OSD methods for Linux prison data.
237 */
238 static int
239 linux_prison_create(void *obj, void *data)
240 {
241 struct prison *pr = obj;
242 struct vfsoptlist *opts = data;
243 int jsys;
244
245 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
246 jsys == JAIL_SYS_INHERIT)
247 return (0);
248 /*
249 * Inherit a prison's initial values from its parent
250 * (different from JAIL_SYS_INHERIT which also inherits changes).
251 */
252 return (linux_alloc_prison(pr, NULL));
253 }
254
255 static int
256 linux_prison_check(void *obj __unused, void *data)
257 {
258 struct vfsoptlist *opts = data;
259 char *osname, *osrelease;
260 int error, jsys, len, osrel, oss_version;
261
262 /* Check that the parameters are correct. */
263 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
264 if (error != ENOENT) {
265 if (error != 0)
266 return (error);
267 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
268 return (EINVAL);
269 }
270 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
271 if (error != ENOENT) {
272 if (error != 0)
273 return (error);
274 if (len == 0 || osname[len - 1] != '\0')
275 return (EINVAL);
276 if (len > LINUX_MAX_UTSNAME) {
277 vfs_opterror(opts, "linux.osname too long");
278 return (ENAMETOOLONG);
279 }
280 }
281 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
282 if (error != ENOENT) {
283 if (error != 0)
284 return (error);
285 if (len == 0 || osrelease[len - 1] != '\0')
286 return (EINVAL);
287 if (len > LINUX_MAX_UTSNAME) {
288 vfs_opterror(opts, "linux.osrelease too long");
289 return (ENAMETOOLONG);
290 }
291 error = linux_map_osrel(osrelease, &osrel);
292 if (error != 0) {
293 vfs_opterror(opts, "linux.osrelease format error");
294 return (error);
295 }
296 }
297 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
298 sizeof(oss_version));
299
300 if (error == ENOENT)
301 error = 0;
302 return (error);
303 }
304
305 static int
306 linux_prison_set(void *obj, void *data)
307 {
308 struct linux_prison *lpr;
309 struct prison *pr = obj;
310 struct vfsoptlist *opts = data;
311 char *osname, *osrelease;
312 int error, gotversion, jsys, len, oss_version;
313
314 /* Set the parameters, which should be correct. */
315 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
316 if (error == ENOENT)
317 jsys = -1;
318 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
319 if (error == ENOENT)
320 osname = NULL;
321 else
322 jsys = JAIL_SYS_NEW;
323 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
324 if (error == ENOENT)
325 osrelease = NULL;
326 else
327 jsys = JAIL_SYS_NEW;
328 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
329 sizeof(oss_version));
330 if (error == ENOENT)
331 gotversion = 0;
332 else {
333 gotversion = 1;
334 jsys = JAIL_SYS_NEW;
335 }
336 switch (jsys) {
337 case JAIL_SYS_INHERIT:
338 /* "linux=inherit": inherit the parent's Linux info. */
339 mtx_lock(&pr->pr_mtx);
340 osd_jail_del(pr, linux_osd_jail_slot);
341 mtx_unlock(&pr->pr_mtx);
342 break;
343 case JAIL_SYS_NEW:
344 /*
345 * "linux=new" or "linux.*":
346 * the prison gets its own Linux info.
347 */
348 error = linux_alloc_prison(pr, &lpr);
349 if (error) {
350 mtx_unlock(&pr->pr_mtx);
351 return (error);
352 }
353 if (osrelease) {
354 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
355 if (error) {
356 mtx_unlock(&pr->pr_mtx);
357 return (error);
358 }
359 strlcpy(lpr->pr_osrelease, osrelease,
360 LINUX_MAX_UTSNAME);
361 }
362 if (osname)
363 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
364 if (gotversion)
365 lpr->pr_oss_version = oss_version;
366 mtx_unlock(&pr->pr_mtx);
367 }
368
369 return (0);
370 }
371
372 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
373 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
374 "Jail Linux kernel OS name");
375 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
376 "Jail Linux kernel OS release");
377 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
378 "I", "Jail Linux OSS version");
379
380 static int
381 linux_prison_get(void *obj, void *data)
382 {
383 struct linux_prison *lpr;
384 struct prison *ppr;
385 struct prison *pr = obj;
386 struct vfsoptlist *opts = data;
387 int error, i;
388
389 static int version0;
390
391 /* See if this prison is the one with the Linux info. */
392 lpr = linux_find_prison(pr, &ppr);
393 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
394 error = vfs_setopt(opts, "linux", &i, sizeof(i));
395 if (error != 0 && error != ENOENT)
396 goto done;
397 if (i) {
398 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
399 if (error != 0 && error != ENOENT)
400 goto done;
401 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
402 if (error != 0 && error != ENOENT)
403 goto done;
404 error = vfs_setopt(opts, "linux.oss_version",
405 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
406 if (error != 0 && error != ENOENT)
407 goto done;
408 } else {
409 /*
410 * If this prison is inheriting its Linux info, report
411 * empty/zero parameters.
412 */
413 error = vfs_setopts(opts, "linux.osname", "");
414 if (error != 0 && error != ENOENT)
415 goto done;
416 error = vfs_setopts(opts, "linux.osrelease", "");
417 if (error != 0 && error != ENOENT)
418 goto done;
419 error = vfs_setopt(opts, "linux.oss_version", &version0,
420 sizeof(lpr->pr_oss_version));
421 if (error != 0 && error != ENOENT)
422 goto done;
423 }
424 error = 0;
425
426 done:
427 mtx_unlock(&ppr->pr_mtx);
428
429 return (error);
430 }
431
432 static void
433 linux_prison_destructor(void *data)
434 {
435
436 free(data, M_PRISON);
437 }
438
439 void
440 linux_osd_jail_register(void)
441 {
442 struct prison *pr;
443 osd_method_t methods[PR_MAXMETHOD] = {
444 [PR_METHOD_CREATE] = linux_prison_create,
445 [PR_METHOD_GET] = linux_prison_get,
446 [PR_METHOD_SET] = linux_prison_set,
447 [PR_METHOD_CHECK] = linux_prison_check
448 };
449
450 linux_osd_jail_slot =
451 osd_jail_register(linux_prison_destructor, methods);
452 if (linux_osd_jail_slot > 0) {
453 /* Copy the system linux info to any current prisons. */
454 sx_xlock(&allprison_lock);
455 TAILQ_FOREACH(pr, &allprison, pr_list)
456 (void)linux_alloc_prison(pr, NULL);
457 sx_xunlock(&allprison_lock);
458 }
459 }
460
461 void
462 linux_osd_jail_deregister(void)
463 {
464
465 if (linux_osd_jail_slot)
466 osd_jail_deregister(linux_osd_jail_slot);
467 }
468
469 void
470 linux_get_osname(struct thread *td, char *dst)
471 {
472 struct prison *pr;
473 struct linux_prison *lpr;
474
475 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
476 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
477 mtx_unlock(&pr->pr_mtx);
478 }
479
480 static int
481 linux_set_osname(struct thread *td, char *osname)
482 {
483 struct prison *pr;
484 struct linux_prison *lpr;
485
486 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
487 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
488 mtx_unlock(&pr->pr_mtx);
489
490 return (0);
491 }
492
493 void
494 linux_get_osrelease(struct thread *td, char *dst)
495 {
496 struct prison *pr;
497 struct linux_prison *lpr;
498
499 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
500 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
501 mtx_unlock(&pr->pr_mtx);
502 }
503
504 int
505 linux_kernver(struct thread *td)
506 {
507 struct prison *pr;
508 struct linux_prison *lpr;
509 int osrel;
510
511 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
512 osrel = lpr->pr_osrel;
513 mtx_unlock(&pr->pr_mtx);
514
515 return (osrel);
516 }
517
518 static int
519 linux_set_osrelease(struct thread *td, char *osrelease)
520 {
521 struct prison *pr;
522 struct linux_prison *lpr;
523 int error;
524
525 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
526 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
527 if (error == 0)
528 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
529 mtx_unlock(&pr->pr_mtx);
530
531 return (error);
532 }
533
534 int
535 linux_get_oss_version(struct thread *td)
536 {
537 struct prison *pr;
538 struct linux_prison *lpr;
539 int version;
540
541 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
542 version = lpr->pr_oss_version;
543 mtx_unlock(&pr->pr_mtx);
544
545 return (version);
546 }
547
548 static int
549 linux_set_oss_version(struct thread *td, int oss_version)
550 {
551 struct prison *pr;
552 struct linux_prison *lpr;
553
554 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
555 lpr->pr_oss_version = oss_version;
556 mtx_unlock(&pr->pr_mtx);
557
558 return (0);
559 }
Cache object: 14fce28b956d79a93c69fd25f3339645
|