Repository navigation
Return EFAULT instead of crashing on an unreadable code-signing fcntl struct - #17
Open
willmcginnis wants to merge 1 commit into
Open
willmcginnis wants to merge 1 commit into
willmcginnis wants to merge 1 commit into
Conversation
The code-signing fcntls the runtime answers itself used the guest's struct through raw pointers: F_ADDFILESIGS_RETURN and F_ADDFILESIGS_INFO read fs_blob_start and wrote fs_file_start, and F_GETSIGSINFO, for a file under /usr/lib or /System, wrote fg_sig_is_platform. A struct the guest cannot read in full could therefore crash the runtime where macOS returns EFAULT, or get success when only the fields the runtime touched were readable, and F_ADDFILESIGS_RETURN answered a NULL struct with success. The five commands answered in one arm (F_ADDSIGS, F_ADDFILESIGS, F_ADDFILESIGS_RETURN, F_ADDFILESIGS_INFO and F_CHECK_LV) did not check the descriptor either: a bad descriptor or a pipe got success or the same crash where macOS returns EBADF. (F_GETSIGSINFO already got EBADF there, from the host.) Those five now follow XNU's order (sys_fcntl_nocancel): an invalid descriptor, or one that is not a vnode (a pipe or a socket, say), is EBADF first. The runtime asks the host for F_GETPATH, as the F_GETSIGSINFO path already does, because XNU makes the same two checks for it first. Then F_ADDFILESIGS_RETURN and F_ADDFILESIGS_INFO copy the struct's input part in whole with guestmem::read (24 and 32 bytes), as F_GETSIGSINFO now also does (16 bytes) for the files it answers itself, and a struct that cannot be read, NULL included, is EFAULT. F_ADDSIGS, F_ADDFILESIGS and F_CHECK_LV still do not read their struct. The answer is written with guestmem::write, and that write is guarded too (EFAULT, the runtime's own answer), but it has no test row, because natively a struct that can be read but not written gets past the copy-in. tests/guest/bad_fcntl_pointers.c checks twelve rows against the native answers: unreadable and partly readable structs for F_ADDFILESIGS_RETURN, F_ADDFILESIGS_INFO and F_GETSIGSINFO, and an unmapped and a NULL one for F_ADDFILESIGS_RETURN (EFAULT); a bad descriptor or a pipe for F_ADDFILESIGS_RETURN, and a bad descriptor for F_CHECK_LV (EBADF). Every row is refused natively before the kernel looks for a signature, so the test adds none to any file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
F_ADDFILESIGS_RETURN,F_ADDFILESIGS_INFOandF_GETSIGSINFO(for a file under/usr/libor/System) used the guest's struct through raw pointers, so a guest that passed one it cannot read in full could kill the whole translated process (SIGBUSorSIGSEGV) where macOS returnsEFAULT, or get success when only the fields the runtime touched were readable. This is the fcntl counterpart of #12: as XNU does, the runtime now copies the struct's input part in whole withguestmem::read(24 bytes forF_ADDFILESIGS_RETURN, 32 forF_ADDFILESIGS_INFO, 16 forF_GETSIGSINFO) and answersEFAULTif that fails. ANULLstruct isEFAULTtoo: for the twoF_ADDFILESIGScommands that is new (F_ADDFILESIGS_RETURNused to answer aNULLstruct with success), whileF_GETSIGSINFOwithNULLgoes to the host kernel as before, which answersEFAULT. It writes its answer withguestmem::writeand answersEFAULTif that fails too; that answer is the runtime's own and has no test row, because natively a struct that can be read but not written gets past the copy-in, on to looking up or adding a signature. In the start-up measured below, dyld passed a struct that could be read and written on every call, so this matters for programs that call these fcntls themselves: for a struct they cannot read, they now get the error macOS gives instead of losing the process or getting a false success.It also checks the descriptor first, as XNU's
sys_fcntl_nocanceldoes, for the five commands the runtime answers in one arm (F_ADDSIGS,F_ADDFILESIGS,F_ADDFILESIGS_RETURN,F_ADDFILESIGS_INFO,F_CHECK_LV): an invalid descriptor, or one that is not a vnode (a pipe or a socket, say), isEBADFbefore the struct is looked at. Until now those five answered success, or crashed, for any descriptor (F_GETSIGSINFOalready gotEBADFfor such descriptors from the host). The check asks the host forF_GETPATH, which theF_GETSIGSINFOpath already does and for which XNU makes the same two checks first.Native answers for bad descriptors
Natively on macOS 27.0.1,
F_GETPATH,F_ADDFILESIGS_RETURN,F_ADDFILESIGS_INFOandF_GETSIGSINFOansweredEBADFfor the same descriptors (-1, a closed one, a pipe, a socket) and went on for a regular file or a directory;F_ADDSIGSandF_CHECK_LVwere measured only on-1(EBADF) and on a regular file with an unreadable struct (EFAULT), andF_ADDFILESIGSonly on the latter.For a readable, writable struct on a file the answers are as before; each call to those five now makes one extra host
F_GETPATHcall. That path has no row of its own (natively it would look up or add a signature); dyld's start-up is its positive control: with the dyld and shared cache below,tests/guest/hellostarts and prints its line, and the 13F_ADDFILESIGS_RETURNcalls dyld makes on the way all return 0, as onmain. To keep this change small,F_ADDSIGS,F_ADDFILESIGSandF_CHECK_LVstill do not read their struct.tests/guest/bad_fcntl_pointers.cruns 12 rows, each in its own child process, and compares them with the native answers: unreadable and partly readable structs for all three commands, an unmapped and aNULLone forF_ADDFILESIGS_RETURN(EFAULT), and a bad descriptor or a pipe forF_ADDFILESIGS_RETURNand a bad descriptor forF_CHECK_LV(EBADF). Every row is refused natively before the kernel looks for a signature, so running it adds no signature to any file. Natively and with this change all 12 rows pass, in 5 runs each for arm64 and arm64e. Onmain(2d2f5b3) none pass in any run: 6 rows kill the process and 6 succeed where macOS fails.cargo test -p maclator(18 tests) and the workspace tests pass, andbad_name_pointers.cstill passes 7/7.Tested on an Apple Silicon Mac on macOS 27.0.1, running the x86_64 build under Rosetta 2 with a macOS 26.6.2 (25G83) arm64e shared cache as the sysroot and dyld from 26.7.1,
--no-aot.This is one of a few small, independent PRs I'm sending today; they can be reviewed in any order, and there's no rush on any of them.