Skip to content

Return EFAULT instead of crashing on an unreadable code-signing fcntl struct - #17

Open
willmcginnis wants to merge 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-codesign-fcntl-args-v3
Open

willmcginnis wants to merge 1 commit into
linuxkid473:mainfrom
willmcginnis:fix-unreadable-codesign-fcntl-args-v3

Conversation

@willmcginnis

Copy link
Copy Markdown
Contributor

F_ADDFILESIGS_RETURN, F_ADDFILESIGS_INFO and F_GETSIGSINFO (for a file under /usr/lib or /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 (SIGBUS or SIGSEGV) where macOS returns EFAULT, 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 with guestmem::read (24 bytes for F_ADDFILESIGS_RETURN, 32 for F_ADDFILESIGS_INFO, 16 for F_GETSIGSINFO) and answers EFAULT if that fails. A NULL struct is EFAULT too: for the two F_ADDFILESIGS commands that is new (F_ADDFILESIGS_RETURN used to answer a NULL struct with success), while F_GETSIGSINFO with NULL goes to the host kernel as before, which answers EFAULT. It writes its answer with guestmem::write and answers EFAULT if 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_nocancel does, 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), is EBADF before the struct is looked at. Until now those five answered success, or crashed, for any descriptor (F_GETSIGSINFO already got EBADF for such descriptors from the host). The check asks the host for F_GETPATH, which the F_GETSIGSINFO path 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_INFO and F_GETSIGSINFO answered EBADF for the same descriptors (-1, a closed one, a pipe, a socket) and went on for a regular file or a directory; F_ADDSIGS and F_CHECK_LV were measured only on -1 (EBADF) and on a regular file with an unreadable struct (EFAULT), and F_ADDFILESIGS only 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_GETPATH call. 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/hello starts and prints its line, and the 13 F_ADDFILESIGS_RETURN calls dyld makes on the way all return 0, as on main. To keep this change small, F_ADDSIGS, F_ADDFILESIGS and F_CHECK_LV still do not read their struct.

tests/guest/bad_fcntl_pointers.c runs 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 a NULL one for F_ADDFILESIGS_RETURN (EFAULT), and 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 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. On main (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, and bad_name_pointers.c still 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant