1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
| /* * exec_mach_imgact * * Image activator for mach-o 1.0 binaries. * * Parameters; struct image_params * image parameter block * * Returns: -1 not a fat binary (keep looking) * -2 Success: encapsulated binary: reread * >0 Failure: error number * EBADARCH Mach-o binary, but with an unrecognized * architecture * ENOMEM No memory for child process after - * can only happen after vfork() * * Important: This image activator is NOT byte order neutral. * * Note: A return value other than -1 indicates subsequent image * activators should not be given the opportunity to attempt * to activate the image. * * TODO: More gracefully handle failures after vfork */ static int exec_mach_imgact(struct image_params *imgp) { struct mach_header *mach_header = (struct mach_header *)imgp->ip_vdata; proc_t p = vfs_context_proc(imgp->ip_vfs_context); int error = 0; task_t task; task_t new_task = NULL; /* protected by vfexec */ thread_t thread; struct uthread *uthread; vm_map_t old_map = VM_MAP_NULL; vm_map_t map; load_return_t lret; load_result_t load_result; struct _posix_spawnattr *psa = NULL; int spawn = (imgp->ip_flags & IMGPF_SPAWN); int vfexec = (imgp->ip_flags & IMGPF_VFORK_EXEC); int p_name_len;
/* * make sure it's a Mach-O 1.0 or Mach-O 2.0 binary; the difference * is a reserved field on the end, so for the most part, we can * treat them as if they were identical. Reverse-endian Mach-O * binaries are recognized but not compatible. */ // 检测header里面的magic,是否符合macho文件的特征 // NXSwapInt:PowerPC等平台中的二进制文件 //MH_CIGAM = 0xCEFAEDFE //MH_CIGAM_64 = 0xCFFAEDFE if ((mach_header->magic == MH_CIGAM) || (mach_header->magic == MH_CIGAM_64)) { error = EBADARCH; goto bad; }
// 检测header里面的magic,是否符合macho文件的特征 // #define MH_MAGIC 0xfeedface // #define MH_MAGIC_64 0xfeedfacf // 通用的macho二进制文件,一般遇到都是这种 if ((mach_header->magic != MH_MAGIC) && (mach_header->magic != MH_MAGIC_64)) { error = -1; goto bad; } // 检测macho的文件类型,文件类型必须是可执行文件 // 还有一些其他的常见类型 // #define MH_OBJECT 0x1 编译过程产生的obj文件 // #define MH_CORE 0x4 崩溃时的dump文件 if (mach_header->filetype != MH_EXECUTE) { error = -1; goto bad; }
// 获取macho的执行环境,cpu的平台与版本 if (imgp->ip_origcputype != 0) { /* Fat header previously had an idea about this thin file */ if (imgp->ip_origcputype != mach_header->cputype || imgp->ip_origcpusubtype != mach_header->cpusubtype) { error = EBADARCH; goto bad; } } else { imgp->ip_origcputype = mach_header->cputype; imgp->ip_origcpusubtype = mach_header->cpusubtype; }
task = current_task(); thread = current_thread(); uthread = get_bsdthread_info(thread);
if ((mach_header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64) imgp->ip_flags |= IMGPF_IS_64BIT;
/* If posix_spawn binprefs exist, respect those prefs. */ psa = (struct _posix_spawnattr *) imgp->ip_px_sa; if (psa != NULL && psa->psa_binprefs[0] != 0) { int pr = 0; for (pr = 0; pr < NBINPREFS; pr++) { cpu_type_t pref = psa->psa_binprefs[pr]; if (pref == 0) { /* No suitable arch in the pref list */ error = EBADARCH; goto bad; }
if (pref == CPU_TYPE_ANY) { /* Jump to regular grading */ goto grade; }
if (pref == imgp->ip_origcputype) { /* We have a match! */ goto grade; } } error = EBADARCH; goto bad; } grade: //检测cpu平台 if (!grade_binary(imgp->ip_origcputype, imgp->ip_origcpusubtype & ~CPU_SUBTYPE_MASK)) { error = EBADARCH; goto bad; }
/* Copy in arguments/environment from the old process */ //获取环境变量和参数 //为vfork执行macho做准备 error = exec_extract_strings(imgp); if (error) goto bad;
error = exec_add_apple_strings(imgp); if (error) goto bad;
AUDIT_ARG(argv, imgp->ip_startargv, imgp->ip_argc, imgp->ip_endargv - imgp->ip_startargv); AUDIT_ARG(envv, imgp->ip_endargv, imgp->ip_envc, imgp->ip_endenvv - imgp->ip_endargv);
/* * We are being called to activate an image subsequent to a vfork() * operation; in this case, we know that our task, thread, and * uthread are actually those of our parent, and our proc, which we * obtained indirectly from the image_params vfs_context_t, is the * new child process. */ // 通过fork,为macho生成一个新的线程 if (vfexec || spawn) { if (vfexec) { imgp->ip_new_thread = fork_create_child(task, NULL, p, FALSE, (imgp->ip_flags & IMGPF_IS_64BIT)); if (imgp->ip_new_thread == NULL) { error = ENOMEM; goto bad; } }
/* reset local idea of thread, uthread, task */ thread = imgp->ip_new_thread; uthread = get_bsdthread_info(thread); task = new_task = get_threadtask(thread); map = get_task_map(task); } else { map = VM_MAP_NULL; }
/* * We set these flags here; this is OK, since if we fail after * this point, we have already destroyed the parent process anyway. */ // 设置一些dyld需要使用的参数 task_set_dyld_info(task, MACH_VM_MIN_ADDRESS, 0); if (imgp->ip_flags & IMGPF_IS_64BIT) { task_set_64bit(task, TRUE); OSBitOrAtomic(P_LP64, &p->p_flag); } else { task_set_64bit(task, FALSE); OSBitAndAtomic(~((uint32_t)P_LP64), &p->p_flag); }
/* * Load the Mach-O file. * * NOTE: An error after this point indicates we have potentially * destroyed or overwritten some process state while attempting an * execve() following a vfork(), which is an unrecoverable condition. * We send the new process an immediate SIGKILL to avoid it executing * any instructions in the mutated address space. For true spawns, * this is not the case, and "too late" is still not too late to * return an error code to the parent process. */
/* * Actually load the image file we previously decided to load. */ //加载,映射macho文件到内存 lret = load_machfile(imgp, mach_header, thread, map, &load_result);
if (lret != LOAD_SUCCESS) { error = load_return_to_errno(lret); goto badtoolate; }
proc_lock(p); p->p_cputype = imgp->ip_origcputype; p->p_cpusubtype = imgp->ip_origcpusubtype; proc_unlock(p);
vm_map_set_user_wire_limit(get_task_map(task), p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
/* * Set code-signing flags if this binary is signed, or if parent has * requested them on exec. */ //设置了一堆标记位 //需要关心一下的是这里和code-signgin有点关系 if (load_result.csflags & CS_VALID) { imgp->ip_csflags |= load_result.csflags & (CS_VALID| CS_HARD|CS_KILL|CS_RESTRICT|CS_ENFORCEMENT|CS_REQUIRE_LV|CS_DYLD_PLATFORM| CS_EXEC_SET_HARD|CS_EXEC_SET_KILL|CS_EXEC_SET_ENFORCEMENT); } else { imgp->ip_csflags &= ~CS_VALID; }
if (p->p_csflags & CS_EXEC_SET_HARD) imgp->ip_csflags |= CS_HARD; if (p->p_csflags & CS_EXEC_SET_KILL) imgp->ip_csflags |= CS_KILL; if (p->p_csflags & CS_EXEC_SET_ENFORCEMENT) imgp->ip_csflags |= CS_ENFORCEMENT; if (p->p_csflags & CS_EXEC_SET_INSTALLER) imgp->ip_csflags |= CS_INSTALLER;
/* * Set up the system reserved areas in the new address space. */ //依据可执行文件的平台,设置合适的执行环境 vm_map_exec(get_task_map(task), task, (void *) p->p_fd->fd_rdir, cpu_type()); /* * Close file descriptors which specify close-on-exec. */ //关闭所有被标记为close-on-exec的文件 fdexec(p, psa != NULL ? psa->psa_flags : 0);
/* * deal with set[ug]id. */ //处理setuid相关的逻辑,和权限相关 error = exec_handle_sugid(imgp); if (error) { goto badtoolate; }
/* * deal with voucher on exec-calling thread. */ if (imgp->ip_new_thread == NULL) thread_set_mach_voucher(current_thread(), IPC_VOUCHER_NULL);
/* Make sure we won't interrupt ourself signalling a partial process */ if (!vfexec && !spawn && (p->p_lflag & P_LTRACED)) psignal(p, SIGTRAP); //为进程设置应用层的栈地址 if (load_result.unixproc && create_unix_stack(get_task_map(task), &load_result, p) != KERN_SUCCESS) { error = load_return_to_errno(LOAD_NOSPACE); goto badtoolate; }
if (vfexec || spawn) { old_map = vm_map_switch(get_task_map(task)); }
if (load_result.unixproc) { user_addr_t ap;
/* * Copy the strings area out into the new process address * space. */ ap = p->user_stack; error = exec_copyout_strings(imgp, &ap); if (error) { if (vfexec || spawn) vm_map_switch(old_map); goto badtoolate; } /* Set the stack */ thread_setuserstack(thread, ap); } if (load_result.dynlinker) { uint64_t ap; int new_ptr_size = (imgp->ip_flags & IMGPF_IS_64BIT) ? 8 : 4;
/* Adjust the stack */ ap = thread_adjuserstack(thread, -new_ptr_size); error = copyoutptr(load_result.mach_header, ap, new_ptr_size);
if (error) { if (vfexec || spawn) vm_map_switch(old_map); goto badtoolate; } task_set_dyld_info(task, load_result.all_image_info_addr, load_result.all_image_info_size); }
/* Avoid immediate VM faults back into kernel */ //防止立刻执行指令导致的错误,做了大量和dyld相关的事情 exec_prefault_data(p, imgp, &load_result);
if (vfexec || spawn) { vm_map_switch(old_map); } /* Set the entry point */ thread_setentrypoint(thread, load_result.entry_point);
/* Stop profiling */ stopprofclock(p);
/* * Reset signal state. */ execsigs(p, thread);
/* * need to cancel async IO requests that can be cancelled and wait for those * already active. MAY BLOCK! */ _aio_exec( p );
#if SYSV_SHM /* FIXME: Till vmspace inherit is fixed: */ if (!vfexec && p->vm_shm) shmexec(p); #endif #if SYSV_SEM /* Clean up the semaphores */ semexit(p); #endif
/* * Remember file name for accounting. */ p->p_acflag &= ~AFORK;
/* * Set p->p_comm and p->p_name to the name passed to exec */ p_name_len = sizeof(p->p_name) - 1; if(imgp->ip_ndp->ni_cnd.cn_namelen > p_name_len) imgp->ip_ndp->ni_cnd.cn_namelen = p_name_len; bcopy((caddr_t)imgp->ip_ndp->ni_cnd.cn_nameptr, (caddr_t)p->p_name, (unsigned)imgp->ip_ndp->ni_cnd.cn_namelen); p->p_name[imgp->ip_ndp->ni_cnd.cn_namelen] = '\0';
if (imgp->ip_ndp->ni_cnd.cn_namelen > MAXCOMLEN) imgp->ip_ndp->ni_cnd.cn_namelen = MAXCOMLEN; bcopy((caddr_t)imgp->ip_ndp->ni_cnd.cn_nameptr, (caddr_t)p->p_comm, (unsigned)imgp->ip_ndp->ni_cnd.cn_namelen); p->p_comm[imgp->ip_ndp->ni_cnd.cn_namelen] = '\0';
pal_dbg_set_task_name( p->task );
#if DEVELOPMENT || DEBUG /* * Update the pid an proc name for importance base if any */ task_importance_update_owner_info(p->task); #endif
memcpy(&p->p_uuid[0], &load_result.uuid[0], sizeof(p->p_uuid));
// <rdar://6598155> dtrace code cleanup needed #if CONFIG_DTRACE /* * Invalidate any predicate evaluation already cached for this thread by DTrace. * That's because we've just stored to p_comm and DTrace refers to that when it * evaluates the "execname" special variable. uid and gid may have changed as well. */ dtrace_set_thread_predcache(current_thread(), 0);
/* * Free any outstanding lazy dof entries. It is imperative we * always call dtrace_lazy_dofs_destroy, rather than null check * and call if !NULL. If we NULL test, during lazy dof faulting * we can race with the faulting code and proceed from here to * beyond the helpers cleanup. The lazy dof faulting will then * install new helpers which no longer belong to this process! */ dtrace_lazy_dofs_destroy(p);
/* * Clean up any DTrace helpers for the process. */ if (p->p_dtrace_helpers != NULL && dtrace_helpers_cleanup) { (*dtrace_helpers_cleanup)(p); } /* * Cleanup the DTrace provider associated with this process. */ proc_lock(p); if (p->p_dtrace_probes && dtrace_fasttrap_exec_ptr) { (*dtrace_fasttrap_exec_ptr)(p); } proc_unlock(p); #endif
if (kdebug_enable) { long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
/* * Collect the pathname for tracing */ kdbg_trace_string(p, &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
if (vfexec || spawn) { KERNEL_DEBUG_CONSTANT1(TRACE_DATA_EXEC | DBG_FUNC_NONE, p->p_pid ,0,0,0, (uintptr_t)thread_tid(thread)); KERNEL_DEBUG_CONSTANT1(TRACE_STRING_EXEC | DBG_FUNC_NONE, dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, (uintptr_t)thread_tid(thread)); } else { KERNEL_DEBUG_CONSTANT(TRACE_DATA_EXEC | DBG_FUNC_NONE, p->p_pid ,0,0,0,0); KERNEL_DEBUG_CONSTANT(TRACE_STRING_EXEC | DBG_FUNC_NONE, dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0); } }
/* * If posix_spawned with the START_SUSPENDED flag, stop the * process before it runs. */ if (imgp->ip_px_sa != NULL) { psa = (struct _posix_spawnattr *) imgp->ip_px_sa; if (psa->psa_flags & POSIX_SPAWN_START_SUSPENDED) { proc_lock(p); p->p_stat = SSTOP; proc_unlock(p); (void) task_suspend_internal(p->task); } }
/* * mark as execed, wakeup the process that vforked (if any) and tell * it that it now has its own resources back */ OSBitOrAtomic(P_EXEC, &p->p_flag); proc_resetregister(p); if (p->p_pptr && (p->p_lflag & P_LPPWAIT)) { proc_lock(p); p->p_lflag &= ~P_LPPWAIT; proc_unlock(p); wakeup((caddr_t)p->p_pptr); }
/* * Pay for our earlier safety; deliver the delayed signals from * the incomplete vfexec process now that it's complete. */ if (vfexec && (p->p_lflag & P_LTRACED)) { psignal_vfork(p, new_task, thread, SIGTRAP); }
goto done;
badtoolate: /* Don't allow child process to execute any instructions */ if (!spawn) { if (vfexec) { psignal_vfork(p, new_task, thread, SIGKILL); } else { psignal(p, SIGKILL); }
/* We can't stop this system call at this point, so just pretend we succeeded */ error = 0; } done: if (!spawn) { /* notify only if it has not failed due to FP Key error */ if ((p->p_lflag & P_LTERM_DECRYPTFAIL) == 0) proc_knote(p, NOTE_EXEC); }
/* Drop extra references for cases where we don't expect the caller to clean up */ if (vfexec || (spawn && error == 0)) { task_deallocate(new_task); thread_deallocate(thread); }
bad: return(error); }
|