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