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