1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <errno.h> 27 #include <fcntl.h> 28 #include <dirent.h> 29 #include <stddef.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <strings.h> 33 #include <unistd.h> 34 #include <thread.h> 35 #include <sys/auxv.h> 36 #include <sys/brand.h> 37 #include <sys/inttypes.h> 38 #include <sys/lwp.h> 39 #include <sys/syscall.h> 40 #include <sys/systm.h> 41 #include <sys/utsname.h> 42 #include <sys/systeminfo.h> 43 #include <sys/zone.h> 44 #include <sys/stat.h> 45 #include <sys/mntent.h> 46 #include <sys/ctfs.h> 47 #include <sys/priv.h> 48 #include <sys/acctctl.h> 49 #include <libgen.h> 50 #include <bsm/audit.h> 51 #include <sys/crypto/ioctl.h> 52 #include <sys/fs/zfs.h> 53 #include <sys/zfs_ioctl.h> 54 #include <sys/ucontext.h> 55 #include <sys/mntio.h> 56 #include <sys/mnttab.h> 57 #include <sys/attr.h> 58 #include <atomic.h> 59 60 #include <s10_brand.h> 61 #include <s10_misc.h> 62 63 /* 64 * Principles of emulation 101. 65 * 66 * 67 * *** Setting errno 68 * 69 * Just don't do it. This emulation library is loaded onto a 70 * seperate link map from the application who's address space we're 71 * running in. We have our own private copy of libc, so there for, 72 * the errno value accessible from here is is also private and changing 73 * it will not affect any errno value that the processes who's address 74 * space we are running in will see. To return an error condition we 75 * should return the negated errno value we'd like the system to return. 76 * For more information about this see the comment in s10_handler(). 77 * Basically, when we return to the caller that initiated the system 78 * call it's their responsibility to set errno. 79 * 80 * 81 * *** Recursion Considerations 82 * 83 * When emulating system calls we need to be very careful about what 84 * library calls we invoke. Library calls should be kept to a minimum. 85 * One issue is that library calls can invoke system calls, so if we're 86 * emulating a system call and we invoke a library call that depends on 87 * that system call we will probably enter a recursive loop, which would 88 * be bad. 89 * 90 * 91 * *** Return Values. 92 * 93 * When declaring new syscall emulation functions, it is very important 94 * to to set the proper RV_* flags in the s10_sysent_table. Upon failure, 95 * syscall emulation fuctions should return an errno value. Upon success 96 * syscall emulation functions should return 0 and set the sysret_t return 97 * value parameters accordingly. 98 * 99 * There are five possible syscall macro wrappers used in the kernel's system 100 * call sysent table. These turn into the following return values: 101 * SYSENT_CL -> SYSENT_C or SYSENT_CI 102 * SYSENT_C SE_64RVAL RV_DEFAULT 103 * SYSENT_CI SE_32RVAL1 RV_DEFAULT 104 * SYSENT_2CI SE_32RVAL1|SE_32RVAL2 RV_32RVAL2 105 * SYSENT_AP SE_64RVAL RV_64RVAL 106 * 107 * 108 * *** Agent lwp considerations 109 * 110 * It is currently impossible to do any emulation for these system call 111 * when they are being invoked on behalf of an agent lwp. To understand why 112 * it's impossible you have to understand how agent lwp syscalls work. 113 * 114 * The agent lwp syscall process works as follows: 115 * 1 The controlling process stops the target. 116 * 2 The controlling process injects an agent lwp which is also stopped. 117 * This agent lwp assumes the userland stack and register values 118 * of another stopped lwp in the current process. 119 * 3 The controlling process configures the agent lwp to start 120 * executing the requested system call. 121 * 4 The controlling process configure /proc to stop the agent lwp when 122 * it enters the requested system call. 123 * 5 The controlling processes allows the agent lwp to start executing. 124 * 6 The agent lwp traps into the kernel to perform the requested system 125 * call and immediately stop. 126 * 7 The controlling process copies all the arguments for the requested 127 * system call onto the agent lwp's stack. 128 * 8 The controlling process configures /proc to stop the agent lwp 129 * when it completes the requested system call. 130 * 9 The controlling processes allows the agent lwp to start executing. 131 * 10 The agent lwp executes the system call and then stop before returning 132 * to userland. 133 * 11 The controlling process copies the return value and return arguments 134 * back from the agent lwps stack. 135 * 12 The controlling process destroys the agent lwp and restarts 136 * the target process. 137 * 138 * The fundamental problem is that when the agent executes the request 139 * system call in step 5, if we're emulating that system call then the 140 * lwp is redirected back to our emulation layer without blocking 141 * in the kernel. But our emulation layer can't access the arguments 142 * for the system call because they haven't been copied to the stack 143 * yet and they still only exist in the controlling processes address 144 * space. This prevents us from being able to do any emulation of 145 * agent lwp system calls. Hence, currently our brand trap interposition 146 * callback (s10_brand_syscall_callback_common) will detect if a system 147 * call is being made by an agent lwp, and if this is the case it will 148 * never redirect the system call to this emulation library. 149 * 150 * In the future, if this proves to be a problem the the easiest solution 151 * would probably be to replace the branded versions of these application 152 * with their native counterparts. Ie, truss, plimit, and pfiles could be 153 * replace with wrapper scripts that execute the native versions of these 154 * applications. In the case of plimit and pfiles this should be pretty 155 * strait forward. Truss would probably be more tricky since it can 156 * execute applications which would be branded applications, so in that 157 * case it might be necessary to create a loadable library which could 158 * be LD_PRELOADed into truss and this library would interpose on the 159 * exec() system call to allow truss to correctly execute branded 160 * processes. It should be pointed out that this solution could work 161 * because "native agent lwps" (ie, agent lwps created by native 162 * processes) can be treated differently from "branded aged lwps" (ie, 163 * agent lwps created by branded processes), since native agent lwps 164 * would presumably be making native system calls and hence not need 165 * any interposition. 166 * 167 */ 168 169 static zoneid_t zoneid; 170 static boolean_t emul_global_zone = B_FALSE; 171 static s10_emul_bitmap_t emul_bitmap; 172 pid_t zone_init_pid; 173 174 /* 175 * S10_FEATURE_IS_PRESENT is a macro that helps facilitate conditional 176 * emulation. For each constant N defined in the s10_emulated_features 177 * enumeration in usr/src/uts/common/brand/solaris10/s10_brand.h, 178 * S10_FEATURE_IS_PRESENT(N) is true iff the feature/backport represented by N 179 * is present in the Solaris 10 image hosted within the zone. In other words, 180 * S10_FEATURE_IS_PRESENT(N) is true iff the file /usr/lib/brand/solaris10/M, 181 * where M is the enum value of N, was present in the zone when the zone booted. 182 * 183 * 184 * *** Sample Usage 185 * 186 * Suppose that you need to backport a fix to Solaris 10 and there is 187 * emulation in place for the fix. Suppose further that the emulation won't be 188 * needed if the fix is backported (i.e., if the fix is present in the hosted 189 * Solaris 10 environment, then the brand won't need the emulation). Then if 190 * you add a constant named "S10_FEATURE_X" to the end of the 191 * s10_emulated_features enumeration that represents the backported fix and 192 * S10_FEATURE_X evaluates to four, then you should create a file named 193 * /usr/lib/brand/solaris10/4 as part of your backport. Additionally, you 194 * should retain the aforementioned emulation but modify it so that it's 195 * performed only when S10_FEATURE_IS_PRESENT(S10_FEATURE_X) is false. Thus the 196 * emulation function should look something like the following: 197 * 198 * static int 199 * my_emul_function(sysret_t *rv, ...) 200 * { 201 * if (S10_FEATURE_IS_PRESENT(S10_FEATURE_X)) { 202 * // Don't emulate 203 * return (__systemcall(rv, ...)); 204 * } else { 205 * // Emulate whatever needs to be emulated when the 206 * // backport isn't present in the Solaris 10 image. 207 * } 208 * } 209 */ 210 #define S10_FEATURE_IS_PRESENT(s10_emulated_features_constant) \ 211 ((emul_bitmap[(s10_emulated_features_constant) >> 3] & \ 212 (1 << ((s10_emulated_features_constant) & 0x7))) != 0) 213 214 #define EMULATE(cb, args) { (sysent_cb_t)(cb), (args) } 215 #define NOSYS EMULATE(s10_unimpl, (0 | RV_DEFAULT)) 216 217 typedef long (*sysent_cb_t)(); 218 typedef struct s10_sysent_table { 219 sysent_cb_t st_callc; 220 uintptr_t st_args; 221 } s10_sysent_table_t; 222 s10_sysent_table_t s10_sysent_table[]; 223 224 #define S10_UTS_RELEASE "5.10" 225 #define S10_UTS_VERSION "Generic_Virtual" 226 227 /*LINTED: static unused*/ 228 static volatile int s10_abort_err; 229 /*LINTED: static unused*/ 230 static volatile const char *s10_abort_msg; 231 /*LINTED: static unused*/ 232 static volatile const char *s10_abort_file; 233 /*LINTED: static unused*/ 234 static volatile int s10_abort_line; 235 236 extern int errno; 237 238 /*ARGSUSED*/ 239 void 240 _s10_abort(int err, const char *msg, const char *file, int line) 241 { 242 sysret_t rval; 243 244 /* Save the error message into convenient globals */ 245 s10_abort_err = err; 246 s10_abort_msg = msg; 247 s10_abort_file = file; 248 s10_abort_line = line; 249 250 /* kill ourselves */ 251 abort(); 252 253 /* If abort() didn't work, try something stronger. */ 254 (void) __systemcall(&rval, SYS_lwp_kill + 1024, _lwp_self(), SIGKILL); 255 } 256 257 static int 258 s10_uucopy(const void *from, void *to, size_t size) 259 { 260 sysret_t rval; 261 262 if (__systemcall(&rval, SYS_uucopy + 1024, from, to, size) != 0) 263 return (EFAULT); 264 return (0); 265 } 266 267 /* 268 * ATTENTION: uucopystr() does NOT ensure that string are null terminated! 269 */ 270 static int 271 s10_uucopystr(const void *from, void *to, size_t size) 272 { 273 sysret_t rval; 274 275 if (__systemcall(&rval, SYS_uucopystr + 1024, from, to, size) != 0) 276 return (EFAULT); 277 return (0); 278 } 279 280 /* 281 * Figures out the PID of init for the zone. Also returns a boolean 282 * indicating whether this process currently has that pid: if so, 283 * then at this moment, we are init. 284 */ 285 static boolean_t 286 get_initpid_info(void) 287 { 288 pid_t pid; 289 sysret_t rval; 290 int err; 291 292 /* 293 * Determine the current process PID and the PID of the zone's init. 294 * We use care not to call getpid() here, because we're not supposed 295 * to call getpid() until after the program is fully linked-- the 296 * first call to getpid() is a signal from the linker to debuggers 297 * that linking has been completed. 298 */ 299 if ((err = __systemcall(&rval, SYS_brand, 300 B_S10_PIDINFO, &pid, &zone_init_pid)) != 0) { 301 s10_abort(err, "Failed to get init's pid"); 302 } 303 304 /* 305 * Note that we need to be cautious with the pid we get back-- 306 * it should not be stashed and used in place of getpid(), since 307 * we might fork(2). So we keep zone_init_pid and toss the pid 308 * we otherwise got. 309 */ 310 if (pid == zone_init_pid) 311 return (B_TRUE); 312 313 return (B_FALSE); 314 } 315 316 /* 317 * This function is defined to be NOSYS but it won't be called from the 318 * the kernel since the NOSYS system calls are not enabled in the kernel. 319 * Thus, the only time this function is called is directly from within the 320 * indirect system call path. 321 */ 322 /*ARGSUSED*/ 323 static long 324 s10_unimpl(sysret_t *rv, uintptr_t p1) 325 { 326 sysret_t rval; 327 328 /* 329 * We'd like to print out some kind of error message here like 330 * "unsupported syscall", but we can't because it's not safe to 331 * assume that stderr or STDERR_FILENO actually points to something 332 * that is a terminal, and if we wrote to those files we could 333 * inadvertantly write to some applications open files, which would 334 * be bad. 335 * 336 * Normally, if an application calls an invalid system call 337 * it get a SIGSYS sent to it. So we'll just go ahead and send 338 * ourselves a signal here. Note that this is far from ideal since 339 * if the application has registered a signal handler, that signal 340 * handler may recieve a ucontext_t as the third parameter to 341 * indicate the context of the process when the signal was 342 * generated, and in this case that context will not be what the 343 * application is expecting. Hence, we should probably create a 344 * brandsys() kernel function that can deliver the signal to us 345 * with the correct ucontext_t. 346 */ 347 (void) __systemcall(&rval, SYS_lwp_kill + 1024, _lwp_self(), SIGSYS); 348 return (ENOSYS); 349 } 350 351 #if defined(__sparc) && !defined(__sparcv9) 352 /* 353 * Yuck. For 32-bit sparc applications, handle indirect system calls. 354 * Note that we declare this interface to use the maximum number of 355 * system call arguments. If we recieve a system call that uses less 356 * arguments, then the additional arguments will be garbage, but they 357 * will also be ignored so that should be ok. 358 */ 359 static long 360 s10_indir(sysret_t *rv, int code, 361 uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, 362 uintptr_t a5, uintptr_t a6, uintptr_t a7) 363 { 364 s10_sysent_table_t *sst = &(s10_sysent_table[code]); 365 366 s10_assert(code < NSYSCALL); 367 switch (sst->st_args & NARGS_MASK) { 368 case 0: 369 return ((sst->st_callc)(rv)); 370 case 1: 371 return ((sst->st_callc)(rv, a0)); 372 case 2: 373 return ((sst->st_callc)(rv, a0, a1)); 374 case 3: 375 return ((sst->st_callc)(rv, a0, a1, a2)); 376 case 4: 377 return ((sst->st_callc)(rv, a0, a1, a2, a3)); 378 case 5: 379 return ((sst->st_callc)(rv, a0, a1, a2, a3, a4)); 380 case 6: 381 return ((sst->st_callc)(rv, rv, a0, a1, a2, a3, a4, a5)); 382 case 7: 383 return ((sst->st_callc)(rv, a0, a1, a2, a3, a4, a5, a6)); 384 case 8: 385 return ((sst->st_callc)(rv, a0, a1, a2, a3, a4, a5, a6, a7)); 386 } 387 s10_abort(0, "invalid entry in s10_sysent_table"); 388 return (EINVAL); 389 } 390 #endif /* __sparc && !__sparcv9 */ 391 392 /* Free the thread-local storage provided by mntfs_get_mntentbuf(). */ 393 static void 394 mntfs_free_mntentbuf(void *arg) 395 { 396 struct mntentbuf *embufp = arg; 397 398 if (embufp == NULL) 399 return; 400 if (embufp->mbuf_emp) 401 free(embufp->mbuf_emp); 402 if (embufp->mbuf_buf) 403 free(embufp->mbuf_buf); 404 bzero(embufp, sizeof (struct mntentbuf)); 405 free(embufp); 406 } 407 408 /* Provide the thread-local storage required by mntfs_ioctl(). */ 409 static struct mntentbuf * 410 mntfs_get_mntentbuf(size_t size) 411 { 412 static mutex_t keylock; 413 static thread_key_t key; 414 static int once_per_keyname = 0; 415 void *tsd = NULL; 416 struct mntentbuf *embufp; 417 418 /* Create the key. */ 419 if (!once_per_keyname) { 420 (void) mutex_lock(&keylock); 421 if (!once_per_keyname) { 422 if (thr_keycreate(&key, mntfs_free_mntentbuf)) { 423 (void) mutex_unlock(&keylock); 424 return (NULL); 425 } else { 426 once_per_keyname++; 427 } 428 } 429 (void) mutex_unlock(&keylock); 430 } 431 432 /* 433 * The thread-specific datum for this key is the address of a struct 434 * mntentbuf. If this is the first time here then we allocate the struct 435 * and its contents, and associate its address with the thread; if there 436 * are any problems then we abort. 437 */ 438 if (thr_getspecific(key, &tsd)) 439 return (NULL); 440 if (tsd == NULL) { 441 if (!(embufp = calloc(1, sizeof (struct mntentbuf))) || 442 !(embufp->mbuf_emp = malloc(sizeof (struct extmnttab))) || 443 thr_setspecific(key, embufp)) { 444 mntfs_free_mntentbuf(embufp); 445 return (NULL); 446 } 447 } else { 448 embufp = tsd; 449 } 450 451 /* Return the buffer, resizing it if necessary. */ 452 if (size > embufp->mbuf_bufsize) { 453 if (embufp->mbuf_buf) 454 free(embufp->mbuf_buf); 455 if ((embufp->mbuf_buf = malloc(size)) == NULL) { 456 embufp->mbuf_bufsize = 0; 457 return (NULL); 458 } else { 459 embufp->mbuf_bufsize = size; 460 } 461 } 462 return (embufp); 463 } 464 465 /* 466 * The MNTIOC_GETMNTENT command in this release differs from that in early 467 * versions of Solaris 10. 468 * 469 * Previously, the command would copy a pointer to a struct extmnttab to an 470 * address provided as an argument. The pointer would be somewhere within a 471 * mapping already present within the user's address space. In addition, the 472 * text to which the struct's members pointed would also be within a 473 * pre-existing mapping. Now, the user is required to allocate memory for both 474 * the struct and the text buffer, and to pass the address of each within a 475 * struct mntentbuf. In order to conceal these details from a Solaris 10 client 476 * we allocate some thread-local storage in which to create the necessary data 477 * structures; this is static, thread-safe memory that will be cleaned up 478 * without the caller's intervention. 479 * 480 * MNTIOC_GETEXTMNTENT and MNTIOC_GETMNTANY are new in this release; they should 481 * not work for older clients. 482 */ 483 int 484 mntfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg) 485 { 486 int err; 487 struct stat statbuf; 488 struct mntentbuf *embufp; 489 static size_t bufsize = MNT_LINE_MAX; 490 491 492 /* Do not emulate mntfs commands from up-to-date clients. */ 493 if (S10_FEATURE_IS_PRESENT(S10_FEATURE_ALTERED_MNTFS_IOCTL)) 494 return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg)); 495 496 /* Do not emulate mntfs commands directed at other file systems. */ 497 if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &statbuf)) != 0) 498 return (err); 499 if (strcmp(statbuf.st_fstype, MNTTYPE_MNTFS) != 0) 500 return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg)); 501 502 if (cmd == MNTIOC_GETEXTMNTENT || cmd == MNTIOC_GETMNTANY) 503 return (EINVAL); 504 505 if ((embufp = mntfs_get_mntentbuf(bufsize)) == NULL) 506 return (ENOMEM); 507 508 /* 509 * MNTIOC_GETEXTMNTENT advances the file pointer once it has 510 * successfully copied out the result to the address provided. We 511 * therefore need to check the user-supplied address now since the 512 * one we'll be providing is guaranteed to work. 513 */ 514 if (s10_uucopy(&embufp->mbuf_emp, (void *)arg, sizeof (void *)) != 0) 515 return (EFAULT); 516 517 /* 518 * Keep retrying for as long as we fail for want of a large enough 519 * buffer. 520 */ 521 for (;;) { 522 if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, 523 MNTIOC_GETEXTMNTENT, embufp)) != 0) 524 return (err); 525 526 if (rval->sys_rval1 == MNTFS_TOOLONG) { 527 /* The buffer wasn't large enough. */ 528 (void) atomic_swap_ulong((unsigned long *)&bufsize, 529 2 * embufp->mbuf_bufsize); 530 if ((embufp = mntfs_get_mntentbuf(bufsize)) == NULL) 531 return (ENOMEM); 532 } else { 533 break; 534 } 535 } 536 537 if (s10_uucopy(&embufp->mbuf_emp, (void *)arg, sizeof (void *)) != 0) 538 return (EFAULT); 539 540 return (0); 541 } 542 543 /* 544 * Assign the structure member value from the s (source) structure to the 545 * d (dest) structure. 546 */ 547 #define struct_assign(d, s, val) (((d).val) = ((s).val)) 548 549 /* 550 * The CRYPTO_GET_FUNCTION_LIST parameter structure crypto_function_list_t 551 * changed between S10 and Nevada, so we have to emulate the old S10 552 * crypto_function_list_t structure when interposing on the ioctl syscall. 553 */ 554 typedef struct s10_crypto_function_list { 555 boolean_t fl_digest_init; 556 boolean_t fl_digest; 557 boolean_t fl_digest_update; 558 boolean_t fl_digest_key; 559 boolean_t fl_digest_final; 560 561 boolean_t fl_encrypt_init; 562 boolean_t fl_encrypt; 563 boolean_t fl_encrypt_update; 564 boolean_t fl_encrypt_final; 565 566 boolean_t fl_decrypt_init; 567 boolean_t fl_decrypt; 568 boolean_t fl_decrypt_update; 569 boolean_t fl_decrypt_final; 570 571 boolean_t fl_mac_init; 572 boolean_t fl_mac; 573 boolean_t fl_mac_update; 574 boolean_t fl_mac_final; 575 576 boolean_t fl_sign_init; 577 boolean_t fl_sign; 578 boolean_t fl_sign_update; 579 boolean_t fl_sign_final; 580 boolean_t fl_sign_recover_init; 581 boolean_t fl_sign_recover; 582 583 boolean_t fl_verify_init; 584 boolean_t fl_verify; 585 boolean_t fl_verify_update; 586 boolean_t fl_verify_final; 587 boolean_t fl_verify_recover_init; 588 boolean_t fl_verify_recover; 589 590 boolean_t fl_digest_encrypt_update; 591 boolean_t fl_decrypt_digest_update; 592 boolean_t fl_sign_encrypt_update; 593 boolean_t fl_decrypt_verify_update; 594 595 boolean_t fl_seed_random; 596 boolean_t fl_generate_random; 597 598 boolean_t fl_session_open; 599 boolean_t fl_session_close; 600 boolean_t fl_session_login; 601 boolean_t fl_session_logout; 602 603 boolean_t fl_object_create; 604 boolean_t fl_object_copy; 605 boolean_t fl_object_destroy; 606 boolean_t fl_object_get_size; 607 boolean_t fl_object_get_attribute_value; 608 boolean_t fl_object_set_attribute_value; 609 boolean_t fl_object_find_init; 610 boolean_t fl_object_find; 611 boolean_t fl_object_find_final; 612 613 boolean_t fl_key_generate; 614 boolean_t fl_key_generate_pair; 615 boolean_t fl_key_wrap; 616 boolean_t fl_key_unwrap; 617 boolean_t fl_key_derive; 618 619 boolean_t fl_init_token; 620 boolean_t fl_init_pin; 621 boolean_t fl_set_pin; 622 623 boolean_t prov_is_hash_limited; 624 uint32_t prov_hash_threshold; 625 uint32_t prov_hash_limit; 626 } s10_crypto_function_list_t; 627 628 typedef struct s10_crypto_get_function_list { 629 uint_t fl_return_value; 630 crypto_provider_id_t fl_provider_id; 631 s10_crypto_function_list_t fl_list; 632 } s10_crypto_get_function_list_t; 633 634 /* 635 * The structure returned by the CRYPTO_GET_FUNCTION_LIST ioctl on /dev/crypto 636 * increased in size due to: 637 * 6482533 Threshold for HW offload via PKCS11 interface 638 * between S10 and Nevada. This is a relatively simple process of filling 639 * in the S10 structure fields with the Nevada data. 640 * 641 * We stat the device to make sure that the ioctl is meant for /dev/crypto. 642 * 643 */ 644 static int 645 crypto_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg) 646 { 647 int err; 648 s10_crypto_get_function_list_t s10_param; 649 crypto_get_function_list_t native_param; 650 static dev_t crypto_dev = (dev_t)-1; 651 struct stat sbuf; 652 653 if (crypto_dev == (dev_t)-1) { 654 if ((err = __systemcall(rval, SYS_stat + 1024, "/dev/crypto", 655 &sbuf)) != 0) 656 goto nonemuioctl; 657 crypto_dev = major(sbuf.st_rdev); 658 } 659 if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &sbuf)) != 0) 660 return (err); 661 /* Each open fd of /dev/crypto gets a new minor device. */ 662 if (major(sbuf.st_rdev) != crypto_dev) 663 goto nonemuioctl; 664 665 if (s10_uucopy((const void *)arg, &s10_param, sizeof (s10_param)) != 0) 666 return (EFAULT); 667 struct_assign(native_param, s10_param, fl_provider_id); 668 if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, 669 &native_param)) != 0) 670 return (err); 671 672 struct_assign(s10_param, native_param, fl_return_value); 673 struct_assign(s10_param, native_param, fl_provider_id); 674 675 struct_assign(s10_param, native_param, fl_list.fl_digest_init); 676 struct_assign(s10_param, native_param, fl_list.fl_digest); 677 struct_assign(s10_param, native_param, fl_list.fl_digest_update); 678 struct_assign(s10_param, native_param, fl_list.fl_digest_key); 679 struct_assign(s10_param, native_param, fl_list.fl_digest_final); 680 681 struct_assign(s10_param, native_param, fl_list.fl_encrypt_init); 682 struct_assign(s10_param, native_param, fl_list.fl_encrypt); 683 struct_assign(s10_param, native_param, fl_list.fl_encrypt_update); 684 struct_assign(s10_param, native_param, fl_list.fl_encrypt_final); 685 686 struct_assign(s10_param, native_param, fl_list.fl_decrypt_init); 687 struct_assign(s10_param, native_param, fl_list.fl_decrypt); 688 struct_assign(s10_param, native_param, fl_list.fl_decrypt_update); 689 struct_assign(s10_param, native_param, fl_list.fl_decrypt_final); 690 691 struct_assign(s10_param, native_param, fl_list.fl_mac_init); 692 struct_assign(s10_param, native_param, fl_list.fl_mac); 693 struct_assign(s10_param, native_param, fl_list.fl_mac_update); 694 struct_assign(s10_param, native_param, fl_list.fl_mac_final); 695 696 struct_assign(s10_param, native_param, fl_list.fl_sign_init); 697 struct_assign(s10_param, native_param, fl_list.fl_sign); 698 struct_assign(s10_param, native_param, fl_list.fl_sign_update); 699 struct_assign(s10_param, native_param, fl_list.fl_sign_final); 700 struct_assign(s10_param, native_param, fl_list.fl_sign_recover_init); 701 struct_assign(s10_param, native_param, fl_list.fl_sign_recover); 702 703 struct_assign(s10_param, native_param, fl_list.fl_verify_init); 704 struct_assign(s10_param, native_param, fl_list.fl_verify); 705 struct_assign(s10_param, native_param, fl_list.fl_verify_update); 706 struct_assign(s10_param, native_param, fl_list.fl_verify_final); 707 struct_assign(s10_param, native_param, fl_list.fl_verify_recover_init); 708 struct_assign(s10_param, native_param, fl_list.fl_verify_recover); 709 710 struct_assign(s10_param, native_param, 711 fl_list.fl_digest_encrypt_update); 712 struct_assign(s10_param, native_param, 713 fl_list.fl_decrypt_digest_update); 714 struct_assign(s10_param, native_param, fl_list.fl_sign_encrypt_update); 715 struct_assign(s10_param, native_param, 716 fl_list.fl_decrypt_verify_update); 717 718 struct_assign(s10_param, native_param, fl_list.fl_seed_random); 719 struct_assign(s10_param, native_param, fl_list.fl_generate_random); 720 721 struct_assign(s10_param, native_param, fl_list.fl_session_open); 722 struct_assign(s10_param, native_param, fl_list.fl_session_close); 723 struct_assign(s10_param, native_param, fl_list.fl_session_login); 724 struct_assign(s10_param, native_param, fl_list.fl_session_logout); 725 726 struct_assign(s10_param, native_param, fl_list.fl_object_create); 727 struct_assign(s10_param, native_param, fl_list.fl_object_copy); 728 struct_assign(s10_param, native_param, fl_list.fl_object_destroy); 729 struct_assign(s10_param, native_param, fl_list.fl_object_get_size); 730 struct_assign(s10_param, native_param, 731 fl_list.fl_object_get_attribute_value); 732 struct_assign(s10_param, native_param, 733 fl_list.fl_object_set_attribute_value); 734 struct_assign(s10_param, native_param, fl_list.fl_object_find_init); 735 struct_assign(s10_param, native_param, fl_list.fl_object_find); 736 struct_assign(s10_param, native_param, fl_list.fl_object_find_final); 737 738 struct_assign(s10_param, native_param, fl_list.fl_key_generate); 739 struct_assign(s10_param, native_param, fl_list.fl_key_generate_pair); 740 struct_assign(s10_param, native_param, fl_list.fl_key_wrap); 741 struct_assign(s10_param, native_param, fl_list.fl_key_unwrap); 742 struct_assign(s10_param, native_param, fl_list.fl_key_derive); 743 744 struct_assign(s10_param, native_param, fl_list.fl_init_token); 745 struct_assign(s10_param, native_param, fl_list.fl_init_pin); 746 struct_assign(s10_param, native_param, fl_list.fl_set_pin); 747 748 struct_assign(s10_param, native_param, fl_list.prov_is_hash_limited); 749 struct_assign(s10_param, native_param, fl_list.prov_hash_threshold); 750 struct_assign(s10_param, native_param, fl_list.prov_hash_limit); 751 752 return (s10_uucopy(&s10_param, (void *)arg, sizeof (s10_param))); 753 754 nonemuioctl: 755 return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg)); 756 } 757 758 /* 759 * The process contract CT_TGET and CT_TSET parameter structure ct_param_t 760 * changed between S10 and Nevada, so we have to emulate the old S10 761 * ct_param_t structure when interposing on the ioctl syscall. 762 */ 763 typedef struct s10_ct_param { 764 uint32_t ctpm_id; 765 uint32_t ctpm_pad; 766 uint64_t ctpm_value; 767 } s10_ct_param_t; 768 769 /* 770 * We have to emulate process contract ioctls for init(1M) because the 771 * ioctl parameter structure changed between S10 and Nevada. This is 772 * a relatively simple process of filling Nevada structure fields, 773 * shuffling values, and initiating a native system call. 774 * 775 * For now, we'll assume that all consumers of CT_TGET and CT_TSET will 776 * need emulation. We'll issue a stat to make sure that the ioctl 777 * is meant for the contract file system. 778 * 779 */ 780 static int 781 ctfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg) 782 { 783 int err; 784 s10_ct_param_t s10param; 785 ct_param_t param; 786 struct stat statbuf; 787 788 if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &statbuf)) != 0) 789 return (err); 790 if (strcmp(statbuf.st_fstype, MNTTYPE_CTFS) != 0) 791 return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg)); 792 793 if (s10_uucopy((const void *)arg, &s10param, sizeof (s10param)) != 0) 794 return (EFAULT); 795 param.ctpm_id = s10param.ctpm_id; 796 param.ctpm_size = sizeof (uint64_t); 797 param.ctpm_value = &s10param.ctpm_value; 798 if ((err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, ¶m)) 799 != 0) 800 return (err); 801 802 if (cmd == CT_TGET) 803 return (s10_uucopy(&s10param, (void *)arg, sizeof (s10param))); 804 805 return (0); 806 } 807 808 typedef struct s10_zfs_cmd { 809 char zc_name[MAXPATHLEN]; 810 char zc_value[MAXPATHLEN * 2]; 811 char zc_string[MAXNAMELEN]; 812 uint64_t zc_guid; 813 uint64_t zc_nvlist_conf; /* really (char *) */ 814 uint64_t zc_nvlist_conf_size; 815 uint64_t zc_nvlist_src; /* really (char *) */ 816 uint64_t zc_nvlist_src_size; 817 uint64_t zc_nvlist_dst; /* really (char *) */ 818 uint64_t zc_nvlist_dst_size; 819 uint64_t zc_cookie; 820 uint64_t zc_objset_type; 821 uint64_t zc_perm_action; 822 uint64_t zc_history; /* really (char *) */ 823 uint64_t zc_history_len; 824 uint64_t zc_history_offset; 825 uint64_t zc_obj; 826 /* Solaris Next added zc_iflags member here */ 827 zfs_share_t zc_share; 828 dmu_objset_stats_t zc_objset_stats; 829 struct drr_begin zc_begin_record; 830 zinject_record_t zc_inject_record; 831 } s10_zfs_cmd_t; 832 833 /* 834 * There is a difference in the zfs_cmd_t ioctl parameter between S10 and 835 * Solaris Next so we need to translate between the two structures when 836 * making ZFS ioctls. 837 */ 838 static int 839 zfs_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg) 840 { 841 int err; 842 s10_zfs_cmd_t s10_param; 843 zfs_cmd_t native_param; 844 static dev_t zfs_dev = (dev_t)-1; 845 struct stat sbuf; 846 847 if (zfs_dev == (dev_t)-1) { 848 if ((err = __systemcall(rval, SYS_stat + 1024, "/dev/zfs", 849 &sbuf)) != 0) 850 goto nonemuioctl; 851 zfs_dev = major(sbuf.st_rdev); 852 } 853 if ((err = __systemcall(rval, SYS_fstat + 1024, fdes, &sbuf)) != 0) 854 return (err); 855 if (major(sbuf.st_rdev) != zfs_dev) 856 goto nonemuioctl; 857 858 if (s10_uucopy((const void *)arg, &s10_param, sizeof (s10_param)) != 0) 859 return (EFAULT); 860 861 bcopy((const void *)s10_param.zc_name, (void *)native_param.zc_name, 862 sizeof (s10_param.zc_name)); 863 bcopy((const void *)s10_param.zc_value, (void *)native_param.zc_value, 864 sizeof (s10_param.zc_value)); 865 bcopy((const void *)s10_param.zc_string, (void *)native_param.zc_string, 866 sizeof (s10_param.zc_string)); 867 struct_assign(native_param, s10_param, zc_guid); 868 struct_assign(native_param, s10_param, zc_nvlist_conf); 869 struct_assign(native_param, s10_param, zc_nvlist_conf_size); 870 struct_assign(native_param, s10_param, zc_nvlist_src); 871 struct_assign(native_param, s10_param, zc_nvlist_src_size); 872 struct_assign(native_param, s10_param, zc_nvlist_dst); 873 struct_assign(native_param, s10_param, zc_nvlist_dst_size); 874 struct_assign(native_param, s10_param, zc_cookie); 875 struct_assign(native_param, s10_param, zc_objset_type); 876 struct_assign(native_param, s10_param, zc_perm_action); 877 struct_assign(native_param, s10_param, zc_history); 878 struct_assign(native_param, s10_param, zc_history_len); 879 struct_assign(native_param, s10_param, zc_history_offset); 880 struct_assign(native_param, s10_param, zc_obj); 881 native_param.zc_iflags = 0; 882 struct_assign(native_param, s10_param, zc_share); 883 struct_assign(native_param, s10_param, zc_objset_stats); 884 struct_assign(native_param, s10_param, zc_begin_record); 885 struct_assign(native_param, s10_param, zc_inject_record); 886 887 err = __systemcall(rval, SYS_ioctl + 1024, fdes, cmd, &native_param); 888 889 bcopy((const void *)native_param.zc_name, (void *)s10_param.zc_name, 890 sizeof (s10_param.zc_name)); 891 bcopy((const void *)native_param.zc_value, (void *)s10_param.zc_value, 892 sizeof (s10_param.zc_value)); 893 bcopy((const void *)native_param.zc_string, (void *)s10_param.zc_string, 894 sizeof (s10_param.zc_string)); 895 struct_assign(s10_param, native_param, zc_guid); 896 struct_assign(s10_param, native_param, zc_nvlist_conf); 897 struct_assign(s10_param, native_param, zc_nvlist_conf_size); 898 struct_assign(s10_param, native_param, zc_nvlist_src); 899 struct_assign(s10_param, native_param, zc_nvlist_src_size); 900 struct_assign(s10_param, native_param, zc_nvlist_dst); 901 struct_assign(s10_param, native_param, zc_nvlist_dst_size); 902 struct_assign(s10_param, native_param, zc_cookie); 903 struct_assign(s10_param, native_param, zc_objset_type); 904 struct_assign(s10_param, native_param, zc_perm_action); 905 struct_assign(s10_param, native_param, zc_history); 906 struct_assign(s10_param, native_param, zc_history_len); 907 struct_assign(s10_param, native_param, zc_history_offset); 908 struct_assign(s10_param, native_param, zc_obj); 909 struct_assign(s10_param, native_param, zc_share); 910 struct_assign(s10_param, native_param, zc_objset_stats); 911 struct_assign(s10_param, native_param, zc_begin_record); 912 struct_assign(s10_param, native_param, zc_inject_record); 913 914 (void) s10_uucopy(&s10_param, (void *)arg, sizeof (s10_param)); 915 return (err); 916 917 nonemuioctl: 918 return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg)); 919 } 920 921 int 922 s10_ioctl(sysret_t *rval, int fdes, int cmd, intptr_t arg) 923 { 924 switch (cmd) { 925 case CRYPTO_GET_FUNCTION_LIST: 926 return (crypto_ioctl(rval, fdes, cmd, arg)); 927 case CT_TGET: 928 /*FALLTHRU*/ 929 case CT_TSET: 930 return (ctfs_ioctl(rval, fdes, cmd, arg)); 931 case MNTIOC_GETMNTENT: 932 /*FALLTHRU*/ 933 case MNTIOC_GETEXTMNTENT: 934 /*FALLTHRU*/ 935 case MNTIOC_GETMNTANY: 936 return (mntfs_ioctl(rval, fdes, cmd, arg)); 937 } 938 939 if ((cmd & 0xff00) == ZFS_IOC) 940 return (zfs_ioctl(rval, fdes, cmd, arg)); 941 942 return (__systemcall(rval, SYS_ioctl + 1024, fdes, cmd, arg)); 943 } 944 945 /* 946 * Unfortunately, pwrite()'s behavior differs between S10 and Nevada when 947 * applied to files opened with O_APPEND. The offset argument is ignored and 948 * the buffer is appended to the target file in S10, whereas the current file 949 * position is ignored in Nevada (i.e., pwrite() acts as though the target file 950 * wasn't opened with O_APPEND). This is a result of the fix for CR 6655660 951 * (pwrite() must ignore the O_APPEND/FAPPEND flag). 952 * 953 * We emulate the old S10 pwrite() behavior by checking whether the target file 954 * was opened with O_APPEND. If it was, then invoke the write() system call 955 * instead of pwrite(); otherwise, invoke the pwrite() system call as usual. 956 */ 957 static int 958 s10_pwrite(sysret_t *rval, int fd, const void *bufferp, size_t num_bytes, 959 off_t offset) 960 { 961 int err; 962 963 if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0) 964 return (err); 965 if (rval->sys_rval1 & O_APPEND) 966 return (__systemcall(rval, SYS_write + 1024, fd, bufferp, 967 num_bytes)); 968 return (__systemcall(rval, SYS_pwrite + 1024, fd, bufferp, num_bytes, 969 offset)); 970 } 971 972 #ifndef _LP64 973 /* 974 * This is the large file version of the pwrite() system call for 32-bit 975 * processes. This exists for the same reason that s10_pwrite() exists; see 976 * the comment above s10_pwrite(). 977 */ 978 static int 979 s10_pwrite64(sysret_t *rval, int fd, const void *bufferp, size32_t num_bytes, 980 uint32_t offset_1, uint32_t offset_2) 981 { 982 int err; 983 984 if ((err = __systemcall(rval, SYS_fcntl + 1024, fd, F_GETFL)) != 0) 985 return (err); 986 if (rval->sys_rval1 & O_APPEND) 987 return (__systemcall(rval, SYS_write + 1024, fd, bufferp, 988 num_bytes)); 989 return (__systemcall(rval, SYS_pwrite64 + 1024, fd, bufferp, 990 num_bytes, offset_1, offset_2)); 991 } 992 #endif /* !_LP64 */ 993 994 /* 995 * These are convenience macros that s10_getdents_common() uses. Both treat 996 * their arguments, which should be character pointers, as dirent pointers or 997 * dirent64 pointers and yield their d_name and d_reclen fields. These 998 * macros shouldn't be used outside of s10_getdents_common(). 999 */ 1000 #define dirent_name(charptr) ((charptr) + name_offset) 1001 #define dirent_reclen(charptr) \ 1002 (*(unsigned short *)(uintptr_t)((charptr) + reclen_offset)) 1003 1004 /* 1005 * This function contains code that is common to both s10_getdents() and 1006 * s10_getdents64(). See the comment above s10_getdents() for details. 1007 * 1008 * rval, fd, buf, and nbyte should be passed unmodified from s10_getdents() 1009 * and s10_getdents64(). getdents_syscall_id should be either SYS_getdents 1010 * or SYS_getdents64. name_offset should be the the byte offset of 1011 * the d_name field in the dirent structures passed to the kernel via the 1012 * syscall represented by getdents_syscall_id. reclen_offset should be 1013 * the byte offset of the d_reclen field in the aforementioned dirent 1014 * structures. 1015 */ 1016 static int 1017 s10_getdents_common(sysret_t *rval, int fd, char *buf, size_t nbyte, 1018 int getdents_syscall_id, size_t name_offset, size_t reclen_offset) 1019 { 1020 int err; 1021 size_t buf_size; 1022 char *local_buf; 1023 char *buf_current; 1024 1025 /* 1026 * Use a special brand operation, B_S10_ISFDXATTRDIR, to determine 1027 * whether the specified file descriptor refers to an extended file 1028 * attribute directory. If it doesn't, then SYS_getdents won't 1029 * reveal extended file attributes, in which case we can simply 1030 * hand the syscall to the native kernel. 1031 */ 1032 if ((err = __systemcall(rval, SYS_brand + 1024, B_S10_ISFDXATTRDIR, 1033 fd)) != 0) 1034 return (err); 1035 if (rval->sys_rval1 == 0) 1036 return (__systemcall(rval, getdents_syscall_id + 1024, fd, buf, 1037 nbyte)); 1038 1039 /* 1040 * The file descriptor refers to an extended file attributes directory. 1041 * We need to create a dirent buffer that's as large as buf into which 1042 * the native SYS_getdents will store the special extended file 1043 * attribute directory's entries. We can't dereference buf because 1044 * it might be an invalid pointer! 1045 */ 1046 if (nbyte > MAXGETDENTS_SIZE) 1047 nbyte = MAXGETDENTS_SIZE; 1048 local_buf = (char *)malloc(nbyte); 1049 if (local_buf == NULL) { 1050 /* 1051 * getdents(2) doesn't return an error code indicating a memory 1052 * allocation error and it doesn't make sense to return any of 1053 * its documented error codes for a malloc(3C) failure. We'll 1054 * use ENOMEM even though getdents(2) doesn't use it because it 1055 * best describes the failure. 1056 */ 1057 (void) S10_TRUSS_POINT_3(rval, getdents_syscall_id, ENOMEM, fd, 1058 buf, nbyte); 1059 rval->sys_rval1 = -1; 1060 rval->sys_rval2 = 0; 1061 return (EIO); 1062 } 1063 1064 /* 1065 * Issue a native SYS_getdents syscall but use our local dirent buffer 1066 * instead of buf. This will allow us to examine the returned dirent 1067 * structures immediately and copy them to buf later. That way the 1068 * calling process won't be able to see the dirent structures until 1069 * we finish examining them. 1070 */ 1071 if ((err = __systemcall(rval, getdents_syscall_id + 1024, fd, local_buf, 1072 nbyte)) != 0) { 1073 free(local_buf); 1074 return (err); 1075 } 1076 buf_size = rval->sys_rval1; 1077 if (buf_size == 0) { 1078 free(local_buf); 1079 return (0); 1080 } 1081 1082 /* 1083 * Look for SUNWattr_ro (VIEW_READONLY) and SUNWattr_rw 1084 * (VIEW_READWRITE) in the directory entries and remove them 1085 * from the dirent buffer. 1086 */ 1087 for (buf_current = local_buf; 1088 (size_t)(buf_current - local_buf) < buf_size; /* cstyle */) { 1089 if (strcmp(dirent_name(buf_current), VIEW_READONLY) != 0 && 1090 strcmp(dirent_name(buf_current), VIEW_READWRITE) != 0) { 1091 /* 1092 * The dirent refers to an attribute that should 1093 * be visible to Solaris 10 processes. Keep it 1094 * and examine the next entry in the buffer. 1095 */ 1096 buf_current += dirent_reclen(buf_current); 1097 } else { 1098 /* 1099 * We found either SUNWattr_ro (VIEW_READONLY) 1100 * or SUNWattr_rw (VIEW_READWRITE). Remove it 1101 * from the dirent buffer by decrementing 1102 * buf_size by the size of the entry and 1103 * overwriting the entry with the remaining 1104 * entries. 1105 */ 1106 buf_size -= dirent_reclen(buf_current); 1107 (void) memmove(buf_current, buf_current + 1108 dirent_reclen(buf_current), buf_size - 1109 (size_t)(buf_current - local_buf)); 1110 } 1111 } 1112 1113 /* 1114 * Copy local_buf into buf so that the calling process can see 1115 * the results. 1116 */ 1117 if ((err = s10_uucopy(local_buf, buf, buf_size)) != 0) { 1118 free(local_buf); 1119 rval->sys_rval1 = -1; 1120 rval->sys_rval2 = 0; 1121 return (err); 1122 } 1123 rval->sys_rval1 = buf_size; 1124 free(local_buf); 1125 return (0); 1126 } 1127 1128 /* 1129 * Solaris Next added two special extended file attributes, SUNWattr_ro and 1130 * SUNWattr_rw, which are called "extended system attributes". They have 1131 * special semantics (e.g., a process cannot unlink SUNWattr_ro) and should 1132 * not appear in solaris10-branded zones because no Solaris 10 applications, 1133 * including system commands such as tar(1), are coded to correctly handle these 1134 * special attributes. 1135 * 1136 * This emulation function solves the aforementioned problem by emulating 1137 * the getdents(2) syscall and filtering both system attributes out of resulting 1138 * directory entry lists. The emulation function only filters results when 1139 * the given file descriptor refers to an extended file attribute directory. 1140 * Filtering getdents(2) results is expensive because it requires dynamic 1141 * memory allocation; however, the performance cost is tolerable because 1142 * we don't expect Solaris 10 processes to frequently examine extended file 1143 * attribute directories. 1144 * 1145 * The brand's emulation library needs two getdents(2) emulation functions 1146 * because getdents(2) comes in two flavors: non-largefile-aware getdents(2) 1147 * and largefile-aware getdents64(2). s10_getdents() handles the non-largefile- 1148 * aware case for 32-bit processes and all getdents(2) syscalls for 64-bit 1149 * processes (64-bit processes use largefile-aware interfaces by default). 1150 * See s10_getdents64() below for the largefile-aware getdents64(2) emulation 1151 * function for 32-bit processes. 1152 */ 1153 static int 1154 s10_getdents(sysret_t *rval, int fd, struct dirent *buf, size_t nbyte) 1155 { 1156 return (s10_getdents_common(rval, fd, (char *)buf, nbyte, SYS_getdents, 1157 offsetof(struct dirent, d_name), 1158 offsetof(struct dirent, d_reclen))); 1159 } 1160 1161 #ifndef _LP64 1162 /* 1163 * This is the largefile-aware version of getdents(2) for 32-bit processes. 1164 * This exists for the same reason that s10_getdents() exists. See the comment 1165 * above s10_getdents(). 1166 */ 1167 static int 1168 s10_getdents64(sysret_t *rval, int fd, struct dirent64 *buf, size_t nbyte) 1169 { 1170 return (s10_getdents_common(rval, fd, (char *)buf, nbyte, 1171 SYS_getdents64, offsetof(struct dirent64, d_name), 1172 offsetof(struct dirent64, d_reclen))); 1173 } 1174 #endif /* !_LP64 */ 1175 1176 #define S10_AC_PROC (0x1 << 28) 1177 #define S10_AC_TASK (0x2 << 28) 1178 #define S10_AC_FLOW (0x4 << 28) 1179 #define S10_AC_MODE(x) ((x) & 0xf0000000) 1180 #define S10_AC_OPTION(x) ((x) & 0x0fffffff) 1181 1182 /* 1183 * The mode shift, mode mask and option mask for acctctl have changed. The 1184 * mode is currently the top full byte and the option is the lower 3 full bytes. 1185 */ 1186 int 1187 s10_acctctl(sysret_t *rval, int cmd, void *buf, size_t bufsz) 1188 { 1189 int mode = S10_AC_MODE(cmd); 1190 int option = S10_AC_OPTION(cmd); 1191 1192 switch (mode) { 1193 case S10_AC_PROC: 1194 mode = AC_PROC; 1195 break; 1196 case S10_AC_TASK: 1197 mode = AC_TASK; 1198 break; 1199 case S10_AC_FLOW: 1200 mode = AC_FLOW; 1201 break; 1202 default: 1203 return (S10_TRUSS_POINT_3(rval, SYS_acctctl, EINVAL, cmd, buf, 1204 bufsz)); 1205 } 1206 1207 return (__systemcall(rval, SYS_acctctl + 1024, mode | option, buf, 1208 bufsz)); 1209 } 1210 1211 /* 1212 * The Audit Policy parameters have changed due to: 1213 * 6466722 audituser and AUDIT_USER are defined, unused, undocumented and 1214 * should be removed. 1215 * 1216 * In S10 we had the following flag: 1217 * #define AUDIT_USER 0x0040 1218 * which doesn't exist in Solaris Next where the subsequent flags are shifted 1219 * down. For example, in S10 we had: 1220 * #define AUDIT_GROUP 0x0080 1221 * but on Solaris Next we have: 1222 * #define AUDIT_GROUP 0x0040 1223 * AUDIT_GROUP has the value AUDIT_USER had in S10 and all of the subsequent 1224 * bits are also shifted one place. 1225 * 1226 * When we're getting or setting the Audit Policy parameters we need to 1227 * shift the outgoing or incoming bits into their proper positions. Since 1228 * S10_AUDIT_USER was always unused, we always clear that bit on A_GETPOLICY. 1229 * 1230 * The command we care about, BSM_AUDITCTL, passes the most parameters (3), 1231 * so declare this function to take up to 4 args and just pass them on. 1232 * The number of parameters for s10_auditsys needs to be equal to the BSM_* 1233 * subcommand that has the most parameters, since we want to pass all 1234 * parameters through, regardless of which subcommands we interpose on. 1235 * 1236 * Note that the auditsys system call uses the SYSENT_AP macro wrapper instead 1237 * of the more common SYSENT_CI macro. This means the return value is a 1238 * SE_64RVAL so the syscall table uses RV_64RVAL. 1239 */ 1240 1241 #define S10_AUDIT_HMASK 0xffffffc0 1242 #define S10_AUDIT_LMASK 0x3f 1243 1244 int 1245 s10_auditsys(sysret_t *rval, int bsmcmd, intptr_t a0, intptr_t a1, intptr_t a2) 1246 { 1247 int err; 1248 uint_t m; 1249 1250 if (bsmcmd != BSM_AUDITCTL) 1251 return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, a1, 1252 a2)); 1253 1254 if ((int)a0 == A_GETPOLICY) { 1255 if ((err = __systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, 1256 &m, a2)) != 0) 1257 return (err); 1258 m = ((m & S10_AUDIT_HMASK) << 1) | (m & S10_AUDIT_LMASK); 1259 if (s10_uucopy(&m, (void *)a1, sizeof (m)) != 0) 1260 return (EFAULT); 1261 return (0); 1262 1263 } else if ((int)a0 == A_SETPOLICY) { 1264 if (s10_uucopy((const void *)a1, &m, sizeof (m)) != 0) 1265 return (EFAULT); 1266 m = ((m >> 1) & S10_AUDIT_HMASK) | (m & S10_AUDIT_LMASK); 1267 return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, &m, 1268 a2)); 1269 } 1270 1271 return (__systemcall(rval, SYS_auditsys + 1024, bsmcmd, a0, a1, a2)); 1272 } 1273 1274 /* 1275 * Determine whether the executable passed to SYS_exec or SYS_execve is a 1276 * native executable. The s10_npreload.so invokes the B_S10_NATIVE brand 1277 * operation which patches up the processes exec info to eliminate any trace 1278 * of the wrapper. That will make pgrep and other commands that examine 1279 * process' executable names and command-line parameters work properly. 1280 */ 1281 static int 1282 s10_exec_native(sysret_t *rval, const char *fname, const char **argp, 1283 const char **envp) 1284 { 1285 const char *filename = fname; 1286 char path[64]; 1287 int err; 1288 1289 /* Get a copy of the executable we're trying to run */ 1290 path[0] = '\0'; 1291 (void) s10_uucopystr(filename, path, sizeof (path)); 1292 1293 /* Check if we're trying to run a native binary */ 1294 if (strncmp(path, "/.SUNWnative/usr/lib/brand/solaris10/s10_native", 1295 sizeof (path)) != 0) 1296 return (0); 1297 1298 /* Skip the first element in the argv array */ 1299 argp++; 1300 1301 /* 1302 * The the path of the dynamic linker is the second parameter 1303 * of s10_native_exec(). 1304 */ 1305 if (s10_uucopy(argp, &filename, sizeof (char *)) != 0) 1306 return (EFAULT); 1307 1308 /* If an exec call succeeds, it never returns */ 1309 err = __systemcall(rval, SYS_brand + 1024, B_EXEC_NATIVE, filename, 1310 argp, envp, NULL, NULL, NULL); 1311 s10_assert(err != 0); 1312 return (err); 1313 } 1314 1315 /* 1316 * Interpose on the SYS_exec syscall to detect native wrappers. 1317 */ 1318 int 1319 s10_exec(sysret_t *rval, const char *fname, const char **argp) 1320 { 1321 int err; 1322 1323 if ((err = s10_exec_native(rval, fname, argp, NULL)) != 0) 1324 return (err); 1325 1326 /* If an exec call succeeds, it never returns */ 1327 err = __systemcall(rval, SYS_exec + 1024, fname, argp); 1328 s10_assert(err != 0); 1329 return (err); 1330 } 1331 1332 /* 1333 * Interpose on the SYS_execve syscall to detect native wrappers. 1334 */ 1335 int 1336 s10_execve(sysret_t *rval, const char *fname, const char **argp, 1337 const char **envp) 1338 { 1339 int err; 1340 1341 if ((err = s10_exec_native(rval, fname, argp, envp)) != 0) 1342 return (err); 1343 1344 /* If an exec call succeeds, it never returns */ 1345 err = __systemcall(rval, SYS_execve + 1024, fname, argp, envp); 1346 s10_assert(err != 0); 1347 return (err); 1348 } 1349 1350 /* 1351 * S10's issetugid() syscall is now a subcode to privsys(). 1352 */ 1353 static int 1354 s10_issetugid(sysret_t *rval) 1355 { 1356 return (__systemcall(rval, SYS_privsys + 1024, PRIVSYS_ISSETUGID, 1357 0, 0, 0, 0, 0)); 1358 } 1359 1360 /* 1361 * New last arg "block" flag should be zero. The block flag is used by 1362 * the Opensolaris AIO implementation, which is now part of libc. 1363 */ 1364 static int 1365 s10_sigqueue(sysret_t *rval, pid_t pid, int signo, void *value, int si_code) 1366 { 1367 return (__systemcall(rval, SYS_sigqueue + 1024, pid, signo, value, 1368 si_code, 0)); 1369 } 1370 1371 static long 1372 s10_uname(sysret_t *rv, uintptr_t p1) 1373 { 1374 struct utsname un, *unp = (struct utsname *)p1; 1375 int rev, err; 1376 1377 if ((err = __systemcall(rv, SYS_uname + 1024, &un)) != 0) 1378 return (err); 1379 1380 rev = atoi(&un.release[2]); 1381 s10_assert(rev >= 11); 1382 bzero(un.release, _SYS_NMLN); 1383 (void) strlcpy(un.release, S10_UTS_RELEASE, _SYS_NMLN); 1384 bzero(un.version, _SYS_NMLN); 1385 (void) strlcpy(un.version, S10_UTS_VERSION, _SYS_NMLN); 1386 1387 /* copy out the modified uname info */ 1388 return (s10_uucopy(&un, unp, sizeof (un))); 1389 } 1390 1391 int 1392 s10_sysinfo(sysret_t *rv, int command, char *buf, long count) 1393 { 1394 char *value; 1395 int len; 1396 1397 /* 1398 * We must interpose on the sysinfo(2) commands SI_RELEASE and 1399 * SI_VERSION; all others get passed to the native sysinfo(2) 1400 * command. 1401 */ 1402 switch (command) { 1403 case SI_RELEASE: 1404 value = S10_UTS_RELEASE; 1405 break; 1406 1407 case SI_VERSION: 1408 value = S10_UTS_VERSION; 1409 break; 1410 1411 default: 1412 /* 1413 * The default action is to pass the command to the 1414 * native sysinfo(2) syscall. 1415 */ 1416 return (__systemcall(rv, SYS_systeminfo + 1024, 1417 command, buf, count)); 1418 } 1419 1420 len = strlen(value) + 1; 1421 if (count > 0) { 1422 if (s10_uucopystr(value, buf, count) != 0) 1423 return (EFAULT); 1424 1425 /* Assure NULL termination of buf as s10_uucopystr() doesn't. */ 1426 if (len > count && s10_uucopy("\0", buf + (count - 1), 1) != 0) 1427 return (EFAULT); 1428 } 1429 1430 /* 1431 * On success, sysinfo(2) returns the size of buffer required to hold 1432 * the complete value plus its terminating NULL byte. 1433 */ 1434 (void) S10_TRUSS_POINT_3(rv, SYS_systeminfo, 0, command, buf, count); 1435 rv->sys_rval1 = len; 1436 rv->sys_rval2 = 0; 1437 return (0); 1438 } 1439 1440 #ifdef __x86 1441 #ifdef __amd64 1442 /* 1443 * 64-bit x86 LWPs created by SYS_lwp_create start here if they need to set 1444 * their %fs registers to the legacy Solaris 10 selector value. 1445 * 1446 * This function does three things: 1447 * 1448 * 1. Trap to the kernel so that it can set %fs to the legacy Solaris 10 1449 * selector value. 1450 * 2. Read the LWP's true entry point (the entry point supplied by libc 1451 * when SYS_lwp_create was invoked) from %r14. 1452 * 3. Eliminate this function's stack frame and pass control to the LWP's 1453 * true entry point. 1454 * 1455 * See the comment above s10_lwp_create_correct_fs() (see below) for the reason 1456 * why this function exists. 1457 */ 1458 /*ARGSUSED*/ 1459 static void 1460 s10_lwp_create_entry_point(void *ulwp_structp) 1461 { 1462 sysret_t rval; 1463 1464 /* 1465 * The new LWP's %fs register is initially zero, but libc won't 1466 * function correctly when %fs is zero. Change the LWP's %fs register 1467 * via SYS_brand. 1468 */ 1469 (void) __systemcall(&rval, SYS_brand + 1024, B_S10_FSREGCORRECTION); 1470 1471 /* 1472 * Jump to the true entry point, which is stored in %r14. 1473 * Remove our stack frame before jumping so that 1474 * s10_lwp_create_entry_point() won't be seen in stack traces. 1475 * 1476 * NOTE: s10_lwp_create_entry_point() pushes %r12 onto its stack frame 1477 * so that it can use it as a temporary register. We don't restore %r12 1478 * in this assembly block because we don't care about its value (and 1479 * neither does _lwp_start()). Besides, the System V ABI AMD64 1480 * Actirecture Processor Supplement doesn't specify that %r12 should 1481 * have a special value when LWPs start, so we can ignore its value when 1482 * we jump to the true entry point. Furthermore, %r12 is a callee-saved 1483 * register, so the true entry point should push %r12 onto its stack 1484 * before using the register. We ignore %r14 after we read it for 1485 * similar reasons. 1486 * 1487 * NOTE: The compiler will generate a function epilogue for this 1488 * function despite the fact that the LWP will never execute it. 1489 * We could hand-code this entire function in assembly to eliminate 1490 * the epilogue, but the epilogue is only three or four instructions, 1491 * so we wouldn't save much space. Besides, why would we want 1492 * to create yet another ugly, hard-to-maintain assembly function when 1493 * we could write most of it in C? 1494 */ 1495 __asm__ __volatile__( 1496 "movq %0, %%rdi\n\t" /* pass ulwp_structp as arg1 */ 1497 "movq %%rbp, %%rsp\n\t" /* eliminate the stack frame */ 1498 "popq %%rbp\n\t" 1499 "jmp *%%r14\n\t" /* jump to the true entry point */ 1500 : : "r" (ulwp_structp)); 1501 /*NOTREACHED*/ 1502 } 1503 1504 /* 1505 * The S10 libc expects that %fs will be nonzero for new 64-bit x86 LWPs but the 1506 * Nevada kernel clears %fs for such LWPs. Unforunately, new LWPs do not issue 1507 * SYS_lwp_private (see s10_lwp_private() below) after they are created, so 1508 * we must ensure that new LWPs invoke a brand operation that sets %fs to a 1509 * nonzero value immediately after their creation. 1510 * 1511 * The easiest way to do this is to make new LWPs start at a special function, 1512 * s10_lwp_create_entry_point() (see its definition above), that invokes the 1513 * brand operation that corrects %fs. We'll store the entry points of new LWPs 1514 * in their %r14 registers so that s10_lwp_create_entry_point() can find and 1515 * call them after invoking the special brand operation. %r14 is a callee-saved 1516 * register; therefore, any functions invoked by s10_lwp_create_entry_point() 1517 * and all functions dealing with signals (e.g., sigacthandler()) will preserve 1518 * %r14 for s10_lwp_create_entry_point(). 1519 * 1520 * The Nevada kernel can safely work with nonzero %fs values because the kernel 1521 * configures per-thread %fs segment descriptors so that the legacy %fs selector 1522 * value will still work. See the comment in lwp_load() regarding %fs and 1523 * %fsbase in 64-bit x86 processes. 1524 * 1525 * This emulation exists thanks to CRs 6467491 and 6501650. 1526 */ 1527 static int 1528 s10_lwp_create_correct_fs(sysret_t *rval, ucontext_t *ucp, int flags, 1529 id_t *new_lwp) 1530 { 1531 ucontext_t s10_uc; 1532 1533 /* 1534 * Copy the supplied ucontext_t structure to the local stack 1535 * frame and store the new LWP's entry point (the value of %rip 1536 * stored in the ucontext_t) in the new LWP's %r14 register. 1537 * Then make s10_lwp_create_entry_point() the new LWP's entry 1538 * point. 1539 */ 1540 if (s10_uucopy(ucp, &s10_uc, sizeof (s10_uc)) != 0) 1541 return (EFAULT); 1542 s10_uc.uc_mcontext.gregs[REG_R14] = s10_uc.uc_mcontext.gregs[REG_RIP]; 1543 s10_uc.uc_mcontext.gregs[REG_RIP] = (greg_t)s10_lwp_create_entry_point; 1544 1545 /* 1546 * Issue SYS_lwp_create to create the new LWP. We pass the 1547 * modified ucontext_t to make sure that the new LWP starts at 1548 * s10_lwp_create_entry_point(). 1549 */ 1550 return (__systemcall(rval, SYS_lwp_create + 1024, &s10_uc, 1551 flags, new_lwp)); 1552 } 1553 #endif /* __amd64 */ 1554 1555 /* 1556 * This function is invoked on x86 systems when SYS_lwp_create is issued but no 1557 * %fs register correction is necessary. 1558 * 1559 * See the comment above s10_lwp_create_correct_fs() above for more details. 1560 */ 1561 static int 1562 s10_lwp_create(sysret_t *rval, ucontext_t *ucp, int flags, id_t *new_lwp) 1563 { 1564 return (__systemcall(rval, SYS_lwp_create + 1024, ucp, flags, new_lwp)); 1565 } 1566 1567 /* 1568 * SYS_lwp_private is issued by libc_init() to set %fsbase in 64-bit x86 1569 * processes. The Nevada kernel sets %fs to zero but the S10 libc expects 1570 * %fs to be nonzero. We'll pass the issued system call to the kernel untouched 1571 * and invoke a brand operation to set %fs to the legacy S10 selector value. 1572 * 1573 * This emulation exists thanks to CRs 6467491 and 6501650. 1574 */ 1575 static int 1576 s10_lwp_private(sysret_t *rval, int cmd, int which, uintptr_t base) 1577 { 1578 #ifdef __amd64 1579 int err; 1580 1581 /* 1582 * The current LWP's %fs register should be zero. Determine whether the 1583 * Solaris 10 libc with which we're working functions correctly when %fs 1584 * is zero by calling thr_main() after issuing the SYS_lwp_private 1585 * syscall. If thr_main() barfs (returns -1), then change the LWP's %fs 1586 * register via SYS_brand and patch s10_sysent_table so that issuing 1587 * SYS_lwp_create executes s10_lwp_create_correct_fs() rather than the 1588 * default s10_lwp_create(). s10_lwp_create_correct_fs() will 1589 * guarantee that new LWPs will have correct %fs values. 1590 */ 1591 if ((err = __systemcall(rval, SYS_lwp_private + 1024, cmd, which, 1592 base)) != 0) 1593 return (err); 1594 if (thr_main() == -1) { 1595 /* 1596 * SYS_lwp_private is only issued by libc_init(), which is 1597 * executed when libc is first loaded by ld.so.1. Thus we 1598 * are guaranteed to be single-threaded at this point. Even 1599 * if we were multithreaded at this point, writing a 64-bit 1600 * value to the st_callc field of a s10_sysent_table 1601 * entry is guaranteed to be atomic on 64-bit x86 chips 1602 * as long as the field is not split across cache lines 1603 * (It shouldn't be.). See chapter 8, section 1.1 of 1604 * "The Intel 64 and IA32 Architectures Software Developer's 1605 * Manual," Volume 3A for more details. 1606 */ 1607 s10_sysent_table[SYS_lwp_create].st_callc = 1608 (sysent_cb_t)s10_lwp_create_correct_fs; 1609 return (__systemcall(rval, SYS_brand + 1024, 1610 B_S10_FSREGCORRECTION)); 1611 } 1612 return (0); 1613 #else /* !__amd64 */ 1614 return (__systemcall(rval, SYS_lwp_private + 1024, cmd, which, base)); 1615 #endif /* !__amd64 */ 1616 } 1617 #endif /* __x86 */ 1618 1619 /* 1620 * The Opensolaris versions of lwp_mutex_timedlock() and lwp_mutex_trylock() 1621 * add an extra argument to the interfaces, a uintptr_t value for the mutex's 1622 * mutex_owner field. The Solaris 10 libc assigns the mutex_owner field at 1623 * user-level, so we just make the extra argument be zero in both syscalls. 1624 */ 1625 1626 static int 1627 s10_lwp_mutex_timedlock(sysret_t *rval, lwp_mutex_t *lp, timespec_t *tsp) 1628 { 1629 return (__systemcall(rval, SYS_lwp_mutex_timedlock + 1024, lp, tsp, 0)); 1630 } 1631 1632 static int 1633 s10_lwp_mutex_trylock(sysret_t *rval, lwp_mutex_t *lp) 1634 { 1635 return (__systemcall(rval, SYS_lwp_mutex_trylock + 1024, lp, 0)); 1636 } 1637 1638 /* 1639 * If the emul_global_zone flag is set then emulate some aspects of the 1640 * zone system call. In particular, emulate the global zone ID on the 1641 * ZONE_LOOKUP subcommand and emulate some of the global zone attributes 1642 * on the ZONE_GETATTR subcommand. If the flag is not set or we're performing 1643 * some other operation, simply pass the calls through. 1644 */ 1645 int 1646 s10_zone(sysret_t *rval, int cmd, void *arg1, void *arg2, void *arg3, 1647 void *arg4) 1648 { 1649 char *aval; 1650 int len; 1651 zoneid_t zid; 1652 int attr; 1653 char *buf; 1654 size_t bufsize; 1655 1656 /* 1657 * We only emulate the zone syscall for a subset of specific commands, 1658 * otherwise we just pass the call through. 1659 */ 1660 if (!emul_global_zone) 1661 return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2, 1662 arg3, arg4)); 1663 1664 switch (cmd) { 1665 case ZONE_LOOKUP: 1666 (void) S10_TRUSS_POINT_1(rval, SYS_zone, 0, cmd); 1667 rval->sys_rval1 = GLOBAL_ZONEID; 1668 rval->sys_rval2 = 0; 1669 return (0); 1670 1671 case ZONE_GETATTR: 1672 zid = (zoneid_t)(uintptr_t)arg1; 1673 attr = (int)(uintptr_t)arg2; 1674 buf = (char *)arg3; 1675 bufsize = (size_t)arg4; 1676 1677 /* 1678 * If the request is for the global zone then we're emulating 1679 * that, otherwise pass this thru. 1680 */ 1681 if (zid != GLOBAL_ZONEID) 1682 goto passthru; 1683 1684 switch (attr) { 1685 case ZONE_ATTR_NAME: 1686 aval = GLOBAL_ZONENAME; 1687 break; 1688 1689 case ZONE_ATTR_BRAND: 1690 aval = NATIVE_BRAND_NAME; 1691 break; 1692 default: 1693 /* 1694 * We only emulate a subset of the attrs, use the 1695 * real zone id to pass thru the rest. 1696 */ 1697 arg1 = (void *)(uintptr_t)zoneid; 1698 goto passthru; 1699 } 1700 1701 (void) S10_TRUSS_POINT_5(rval, SYS_zone, 0, cmd, zid, attr, 1702 buf, bufsize); 1703 1704 len = strlen(aval) + 1; 1705 if (len > bufsize) 1706 return (ENAMETOOLONG); 1707 1708 if (buf != NULL) { 1709 if (len == 1) { 1710 if (s10_uucopy("\0", buf, 1) != 0) 1711 return (EFAULT); 1712 } else { 1713 if (s10_uucopystr(aval, buf, len) != 0) 1714 return (EFAULT); 1715 1716 /* 1717 * Assure NULL termination of "buf" as 1718 * s10_uucopystr() does NOT. 1719 */ 1720 if (s10_uucopy("\0", buf + (len - 1), 1) != 0) 1721 return (EFAULT); 1722 } 1723 } 1724 1725 rval->sys_rval1 = len; 1726 rval->sys_rval2 = 0; 1727 return (0); 1728 1729 default: 1730 break; 1731 } 1732 1733 passthru: 1734 return (__systemcall(rval, SYS_zone + 1024, cmd, arg1, arg2, arg3, 1735 arg4)); 1736 } 1737 1738 /* 1739 * Close a libc file handle, but don't actually close the underlying 1740 * file descriptor. 1741 */ 1742 static void 1743 s10_close_fh(FILE *file) 1744 { 1745 int fd, fd_new; 1746 1747 if (file == NULL) 1748 return; 1749 1750 if ((fd = fileno(file)) < 0) 1751 return; 1752 1753 fd_new = dup(fd); 1754 if (fd_new == -1) 1755 return; 1756 1757 (void) fclose(file); 1758 (void) dup2(fd_new, fd); 1759 (void) close(fd_new); 1760 } 1761 1762 /*ARGSUSED*/ 1763 int 1764 s10_init(int argc, char *argv[], char *envp[]) 1765 { 1766 sysret_t rval; 1767 s10_brand_reg_t reg; 1768 s10_elf_data_t sed; 1769 auxv_t *ap; 1770 uintptr_t *p; 1771 int i, err; 1772 char *bname; 1773 1774 /* Sanity check our translation table return value codes */ 1775 for (i = 0; i < NSYSCALL; i++) { 1776 s10_sysent_table_t *est = &(s10_sysent_table[i]); 1777 s10_assert(BIT_ONLYONESET(est->st_args & RV_MASK)); 1778 } 1779 1780 /* 1781 * We need to shutdown all libc stdio. libc stdio normally goes to 1782 * file descriptors, but since we're actually part of a another 1783 * process we don't own these file descriptors and we can't make 1784 * any assumptions about their state. 1785 */ 1786 s10_close_fh(stdin); 1787 s10_close_fh(stdout); 1788 s10_close_fh(stderr); 1789 1790 /* 1791 * Cache the pid of the zone's init process and determine if 1792 * we're init(1m) for the zone. Remember: we might be init 1793 * now, but as soon as we fork(2) we won't be. 1794 */ 1795 (void) get_initpid_info(); 1796 1797 /* get the current zoneid */ 1798 err = __systemcall(&rval, SYS_zone, ZONE_LOOKUP, NULL); 1799 s10_assert(err == 0); 1800 zoneid = (zoneid_t)rval.sys_rval1; 1801 1802 /* Get the zone's emulation bitmap. */ 1803 if ((err = __systemcall(&rval, SYS_zone, ZONE_GETATTR, zoneid, 1804 S10_EMUL_BITMAP, emul_bitmap, sizeof (emul_bitmap))) != 0) { 1805 s10_abort(err, "The zone's patch level is unsupported"); 1806 /*NOTREACHED*/ 1807 } 1808 1809 bname = basename(argv[0]); 1810 1811 /* 1812 * In general we want the S10 commands that are zone-aware to continue 1813 * to behave as they normally do within a zone. Since these commands 1814 * are zone-aware, they should continue to "do the right thing". 1815 * However, some zone-aware commands aren't going to work the way 1816 * we expect them to inside the branded zone. In particular, the pkg 1817 * and patch commands will not properly manage all pkgs/patches 1818 * unless the commands think they are running in the global zone. For 1819 * these commands we want to emulate the global zone. 1820 * 1821 * We don't do any emulation for pkgcond since it is typically used 1822 * in pkg/patch postinstall scripts and we want those scripts to do 1823 * the right thing inside a zone. 1824 * 1825 * One issue is the handling of hollow pkgs. Since the pkgs are 1826 * hollow, they won't use pkgcond in their postinstall scripts. These 1827 * pkgs typically are installing drivers so we handle that by 1828 * replacing add_drv and rem_drv in the s10_boot script. 1829 */ 1830 if (strcmp("pkgadd", bname) == 0 || strcmp("pkgrm", bname) == 0 || 1831 strcmp("patchadd", bname) == 0 || strcmp("patchrm", bname) == 0) 1832 emul_global_zone = B_TRUE; 1833 1834 /* 1835 * Register our syscall emulation table with the kernel. 1836 * Note that we don't have to do invoke (syscall_number + 1024) 1837 * until we've actually establised a syscall emulation callback 1838 * handler address, which is what we're doing with this brand 1839 * syscall. 1840 */ 1841 reg.sbr_version = S10_VERSION; 1842 #ifdef __x86 1843 reg.sbr_handler = (caddr_t)s10_handler_table; 1844 #else /* !__x86 */ 1845 reg.sbr_handler = (caddr_t)s10_handler; 1846 #endif /* !__x86 */ 1847 1848 if ((err = __systemcall(&rval, SYS_brand, B_REGISTER, ®)) != 0) { 1849 s10_abort(err, "Failed to brand current process"); 1850 /*NOTREACHED*/ 1851 } 1852 1853 /* Get data about the executable we're running from the kernel. */ 1854 if ((err = __systemcall(&rval, SYS_brand + 1024, 1855 B_ELFDATA, (void *)&sed)) != 0) { 1856 s10_abort(err, 1857 "Failed to get required brand ELF data from the kernel"); 1858 /*NOTREACHED*/ 1859 } 1860 1861 /* 1862 * Find the aux vector on the stack. 1863 */ 1864 p = (uintptr_t *)envp; 1865 while (*p != NULL) 1866 p++; 1867 1868 /* 1869 * p is now pointing at the 0 word after the environ pointers. 1870 * After that is the aux vectors. 1871 * 1872 * The aux vectors are currently pointing to the brand emulation 1873 * library and associated linker. We're going to change them to 1874 * point to the brand executable and associated linker (or to no 1875 * linker for static binaries). This matches the process data 1876 * stored within the kernel and visible from /proc, which was 1877 * all setup in s10_elfexec(). We do this so that when a debugger 1878 * attaches to the process it sees the process as a normal solaris 1879 * process, this brand emulation library and everything on it's 1880 * link map will not be visible, unless our librtld_db plugin 1881 * is used. Note that this is very different from how Linux 1882 * branded processes are implemented within lx branded zones. 1883 * In that situation, the primary linkmap of the process is the 1884 * brand emulation libraries linkmap, not the Linux applications 1885 * linkmap. 1886 * 1887 * We also need to clear the AF_SUN_NOPLM flag from the AT_SUN_AUXFLAGS 1888 * aux vector. This flag told our linker that we don't have a 1889 * primary link map. Now that our linker is done initializing, we 1890 * want to clear this flag before we transfer control to the 1891 * applications copy of the linker, since we want that linker to have 1892 * a primary link map which will be the link map for the application 1893 * we're running. 1894 */ 1895 p++; 1896 for (ap = (auxv_t *)p; ap->a_type != AT_NULL; ap++) { 1897 switch (ap->a_type) { 1898 case AT_BASE: 1899 /* Hide AT_BASE if static binary */ 1900 if (sed.sed_base == NULL) { 1901 ap->a_type = AT_IGNORE; 1902 ap->a_un.a_val = NULL; 1903 } else { 1904 ap->a_un.a_val = sed.sed_base; 1905 } 1906 break; 1907 case AT_ENTRY: 1908 ap->a_un.a_val = sed.sed_entry; 1909 break; 1910 case AT_PHDR: 1911 ap->a_un.a_val = sed.sed_phdr; 1912 break; 1913 case AT_PHENT: 1914 ap->a_un.a_val = sed.sed_phent; 1915 break; 1916 case AT_PHNUM: 1917 ap->a_un.a_val = sed.sed_phnum; 1918 break; 1919 case AT_SUN_AUXFLAGS: 1920 ap->a_un.a_val &= ~AF_SUN_NOPLM; 1921 break; 1922 case AT_SUN_EMULATOR: 1923 /* 1924 * ld.so.1 inspects AT_SUN_EMULATOR to see if 1925 * if it is the linker for the brand emulation 1926 * library. Hide AT_SUN_EMULATOR, as the 1927 * linker we are about to jump to is the linker 1928 * for the binary. 1929 */ 1930 ap->a_type = AT_IGNORE; 1931 ap->a_un.a_val = NULL; 1932 break; 1933 case AT_SUN_LDDATA: 1934 /* Hide AT_SUN_LDDATA if static binary */ 1935 if (sed.sed_lddata == NULL) { 1936 ap->a_type = AT_IGNORE; 1937 ap->a_un.a_val = NULL; 1938 } else { 1939 ap->a_un.a_val = sed.sed_lddata; 1940 } 1941 break; 1942 default: 1943 break; 1944 } 1945 } 1946 1947 s10_runexe(argv, sed.sed_ldentry); 1948 /*NOTREACHED*/ 1949 s10_abort(0, "s10_runexe() returned"); 1950 return (-1); 1951 } 1952 1953 /* 1954 * This table must have at least NSYSCALL entries in it. 1955 * 1956 * The second parameter of each entry in the s10_sysent_table 1957 * contains the number of parameters and flags that describe the 1958 * syscall return value encoding. See the block comments at the 1959 * top of this file for more information about the syscall return 1960 * value flags and when they should be used. 1961 */ 1962 s10_sysent_table_t s10_sysent_table[] = { 1963 #if defined(__sparc) && !defined(__sparcv9) 1964 EMULATE(s10_indir, 9 | RV_64RVAL), /* 0 */ 1965 #else /* !__sparc || __sparcv9 */ 1966 NOSYS, /* 0 */ 1967 #endif /* !__sparc || __sparcv9 */ 1968 NOSYS, /* 1 */ 1969 NOSYS, /* 2 */ 1970 NOSYS, /* 3 */ 1971 NOSYS, /* 4 */ 1972 NOSYS, /* 5 */ 1973 NOSYS, /* 6 */ 1974 NOSYS, /* 7 */ 1975 NOSYS, /* 8 */ 1976 NOSYS, /* 9 */ 1977 NOSYS, /* 10 */ 1978 EMULATE(s10_exec, 2 | RV_DEFAULT), /* 11 */ 1979 NOSYS, /* 12 */ 1980 NOSYS, /* 13 */ 1981 NOSYS, /* 14 */ 1982 NOSYS, /* 15 */ 1983 NOSYS, /* 16 */ 1984 NOSYS, /* 17 */ 1985 NOSYS, /* 18 */ 1986 NOSYS, /* 19 */ 1987 NOSYS, /* 20 */ 1988 NOSYS, /* 21 */ 1989 NOSYS, /* 22 */ 1990 NOSYS, /* 23 */ 1991 NOSYS, /* 24 */ 1992 NOSYS, /* 25 */ 1993 NOSYS, /* 26 */ 1994 NOSYS, /* 27 */ 1995 NOSYS, /* 28 */ 1996 NOSYS, /* 29 */ 1997 NOSYS, /* 30 */ 1998 NOSYS, /* 31 */ 1999 NOSYS, /* 32 */ 2000 NOSYS, /* 33 */ 2001 NOSYS, /* 34 */ 2002 NOSYS, /* 35 */ 2003 NOSYS, /* 36 */ 2004 NOSYS, /* 37 */ 2005 NOSYS, /* 38 */ 2006 NOSYS, /* 39 */ 2007 NOSYS, /* 40 */ 2008 NOSYS, /* 41 */ 2009 NOSYS, /* 42 */ 2010 NOSYS, /* 43 */ 2011 NOSYS, /* 44 */ 2012 NOSYS, /* 45 */ 2013 NOSYS, /* 46 */ 2014 NOSYS, /* 47 */ 2015 NOSYS, /* 48 */ 2016 NOSYS, /* 49 */ 2017 NOSYS, /* 50 */ 2018 NOSYS, /* 51 */ 2019 NOSYS, /* 52 */ 2020 NOSYS, /* 53 */ 2021 EMULATE(s10_ioctl, 3 | RV_DEFAULT), /* 54 */ 2022 NOSYS, /* 55 */ 2023 NOSYS, /* 56 */ 2024 NOSYS, /* 57 */ 2025 NOSYS, /* 58 */ 2026 EMULATE(s10_execve, 3 | RV_DEFAULT), /* 59 */ 2027 NOSYS, /* 60 */ 2028 NOSYS, /* 61 */ 2029 NOSYS, /* 62 */ 2030 NOSYS, /* 63 */ 2031 NOSYS, /* 64 */ 2032 NOSYS, /* 65 */ 2033 NOSYS, /* 66 */ 2034 NOSYS, /* 67 */ 2035 NOSYS, /* 68 */ 2036 NOSYS, /* 69 */ 2037 NOSYS, /* 70 */ 2038 EMULATE(s10_acctctl, 3 | RV_DEFAULT), /* 71 */ 2039 NOSYS, /* 72 */ 2040 NOSYS, /* 73 */ 2041 NOSYS, /* 74 */ 2042 EMULATE(s10_issetugid, 0 | RV_DEFAULT), /* 75 */ 2043 NOSYS, /* 76 */ 2044 NOSYS, /* 77 */ 2045 NOSYS, /* 78 */ 2046 NOSYS, /* 79 */ 2047 NOSYS, /* 80 */ 2048 EMULATE(s10_getdents, 3 | RV_DEFAULT), /* 81 */ 2049 NOSYS, /* 82 */ 2050 NOSYS, /* 83 */ 2051 NOSYS, /* 84 */ 2052 NOSYS, /* 85 */ 2053 NOSYS, /* 86 */ 2054 NOSYS, /* 87 */ 2055 NOSYS, /* 88 */ 2056 NOSYS, /* 89 */ 2057 NOSYS, /* 90 */ 2058 NOSYS, /* 91 */ 2059 NOSYS, /* 92 */ 2060 NOSYS, /* 93 */ 2061 NOSYS, /* 94 */ 2062 NOSYS, /* 95 */ 2063 NOSYS, /* 96 */ 2064 NOSYS, /* 97 */ 2065 NOSYS, /* 98 */ 2066 NOSYS, /* 99 */ 2067 NOSYS, /* 100 */ 2068 NOSYS, /* 101 */ 2069 NOSYS, /* 102 */ 2070 NOSYS, /* 103 */ 2071 NOSYS, /* 104 */ 2072 NOSYS, /* 105 */ 2073 NOSYS, /* 106 */ 2074 NOSYS, /* 107 */ 2075 NOSYS, /* 108 */ 2076 NOSYS, /* 109 */ 2077 NOSYS, /* 110 */ 2078 NOSYS, /* 111 */ 2079 NOSYS, /* 112 */ 2080 NOSYS, /* 113 */ 2081 NOSYS, /* 114 */ 2082 NOSYS, /* 115 */ 2083 NOSYS, /* 116 */ 2084 NOSYS, /* 117 */ 2085 NOSYS, /* 118 */ 2086 NOSYS, /* 119 */ 2087 NOSYS, /* 120 */ 2088 NOSYS, /* 121 */ 2089 NOSYS, /* 122 */ 2090 NOSYS, /* 123 */ 2091 NOSYS, /* 124 */ 2092 NOSYS, /* 125 */ 2093 NOSYS, /* 126 */ 2094 NOSYS, /* 127 */ 2095 NOSYS, /* 128 */ 2096 NOSYS, /* 129 */ 2097 NOSYS, /* 130 */ 2098 NOSYS, /* 131 */ 2099 NOSYS, /* 132 */ 2100 NOSYS, /* 133 */ 2101 NOSYS, /* 134 */ 2102 EMULATE(s10_uname, 1 | RV_DEFAULT), /* 135 */ 2103 NOSYS, /* 136 */ 2104 NOSYS, /* 137 */ 2105 NOSYS, /* 138 */ 2106 EMULATE(s10_sysinfo, 3 | RV_DEFAULT), /* 139 */ 2107 NOSYS, /* 140 */ 2108 NOSYS, /* 141 */ 2109 NOSYS, /* 142 */ 2110 NOSYS, /* 143 */ 2111 NOSYS, /* 144 */ 2112 NOSYS, /* 145 */ 2113 NOSYS, /* 146 */ 2114 NOSYS, /* 147 */ 2115 NOSYS, /* 148 */ 2116 NOSYS, /* 149 */ 2117 NOSYS, /* 150 */ 2118 NOSYS, /* 151 */ 2119 NOSYS, /* 152 */ 2120 NOSYS, /* 153 */ 2121 NOSYS, /* 154 */ 2122 NOSYS, /* 155 */ 2123 NOSYS, /* 156 */ 2124 NOSYS, /* 157 */ 2125 NOSYS, /* 158 */ 2126 #ifdef __x86 2127 EMULATE(s10_lwp_create, 3 | RV_DEFAULT), /* 159 */ 2128 #else /* !__x86 */ 2129 NOSYS, /* 159 */ 2130 #endif /* !__x86 */ 2131 NOSYS, /* 160 */ 2132 NOSYS, /* 161 */ 2133 NOSYS, /* 162 */ 2134 NOSYS, /* 163 */ 2135 NOSYS, /* 164 */ 2136 NOSYS, /* 165 */ 2137 #ifdef __x86 2138 EMULATE(s10_lwp_private, 3 | RV_DEFAULT), /* 166 */ 2139 #else /* !__x86 */ 2140 NOSYS, /* 166 */ 2141 #endif /* !__x86 */ 2142 NOSYS, /* 167 */ 2143 NOSYS, /* 168 */ 2144 NOSYS, /* 169 */ 2145 NOSYS, /* 170 */ 2146 NOSYS, /* 171 */ 2147 NOSYS, /* 172 */ 2148 NOSYS, /* 173 */ 2149 EMULATE(s10_pwrite, 4 | RV_DEFAULT), /* 174 */ 2150 NOSYS, /* 175 */ 2151 NOSYS, /* 176 */ 2152 NOSYS, /* 177 */ 2153 NOSYS, /* 178 */ 2154 NOSYS, /* 179 */ 2155 NOSYS, /* 180 */ 2156 NOSYS, /* 181 */ 2157 NOSYS, /* 182 */ 2158 NOSYS, /* 183 */ 2159 NOSYS, /* 184 */ 2160 NOSYS, /* 185 */ 2161 EMULATE(s10_auditsys, 4 | RV_64RVAL), /* 186 */ 2162 NOSYS, /* 187 */ 2163 NOSYS, /* 188 */ 2164 NOSYS, /* 189 */ 2165 EMULATE(s10_sigqueue, 4 | RV_DEFAULT), /* 190 */ 2166 NOSYS, /* 191 */ 2167 NOSYS, /* 192 */ 2168 NOSYS, /* 193 */ 2169 NOSYS, /* 194 */ 2170 NOSYS, /* 195 */ 2171 NOSYS, /* 196 */ 2172 NOSYS, /* 197 */ 2173 NOSYS, /* 198 */ 2174 NOSYS, /* 199 */ 2175 NOSYS, /* 200 */ 2176 NOSYS, /* 201 */ 2177 NOSYS, /* 202 */ 2178 NOSYS, /* 203 */ 2179 NOSYS, /* 204 */ 2180 NOSYS, /* 205 */ 2181 NOSYS, /* 206 */ 2182 NOSYS, /* 207 */ 2183 NOSYS, /* 208 */ 2184 NOSYS, /* 209 */ 2185 EMULATE(s10_lwp_mutex_timedlock, 2 | RV_DEFAULT), /* 210 */ 2186 NOSYS, /* 211 */ 2187 NOSYS, /* 212 */ 2188 #ifdef _LP64 2189 NOSYS, /* 213 */ 2190 #else /* !_LP64 */ 2191 EMULATE(s10_getdents64, 3 | RV_DEFAULT), /* 213 */ 2192 #endif /* !_LP64 */ 2193 NOSYS, /* 214 */ 2194 NOSYS, /* 215 */ 2195 NOSYS, /* 216 */ 2196 NOSYS, /* 217 */ 2197 NOSYS, /* 218 */ 2198 NOSYS, /* 219 */ 2199 NOSYS, /* 220 */ 2200 NOSYS, /* 221 */ 2201 NOSYS, /* 222 */ 2202 #ifdef _LP64 2203 NOSYS, /* 223 */ 2204 #else /* !_LP64 */ 2205 EMULATE(s10_pwrite64, 5 | RV_DEFAULT), /* 223 */ 2206 #endif /* !_LP64 */ 2207 NOSYS, /* 224 */ 2208 NOSYS, /* 225 */ 2209 NOSYS, /* 226 */ 2210 EMULATE(s10_zone, 5 | RV_DEFAULT), /* 227 */ 2211 NOSYS, /* 228 */ 2212 NOSYS, /* 229 */ 2213 NOSYS, /* 230 */ 2214 NOSYS, /* 231 */ 2215 NOSYS, /* 232 */ 2216 NOSYS, /* 233 */ 2217 NOSYS, /* 234 */ 2218 NOSYS, /* 235 */ 2219 NOSYS, /* 236 */ 2220 NOSYS, /* 237 */ 2221 NOSYS, /* 238 */ 2222 NOSYS, /* 239 */ 2223 NOSYS, /* 240 */ 2224 NOSYS, /* 241 */ 2225 NOSYS, /* 242 */ 2226 NOSYS, /* 243 */ 2227 NOSYS, /* 244 */ 2228 NOSYS, /* 245 */ 2229 NOSYS, /* 246 */ 2230 NOSYS, /* 247 */ 2231 NOSYS, /* 248 */ 2232 NOSYS, /* 249 */ 2233 NOSYS, /* 250 */ 2234 EMULATE(s10_lwp_mutex_trylock, 1 | RV_DEFAULT), /* 251 */ 2235 NOSYS, /* 252 */ 2236 NOSYS, /* 253 */ 2237 NOSYS, /* 254 */ 2238 NOSYS /* 255 */ 2239 }; 2240