Skip to content
Race condition Write-up Advanced

lateralus (CVE-2023-32407) - a macOS TCC bypass

Gergely Kalman Follow

Published on gergelykalman.com

Bounty$30,500 View original

Since I owe you guys a bunch of writeups from my talk ( Unexpected, Unreasonable, Unfixable: Filesystem Attacks on macOS), I decided that I'll tackle lateralus today.

Gergely Kalman, on gergelykalman.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

Context

This write-up covers lateralus, a macOS TCC bypass tracked as CVE-2023-32407, reported to Apple by Gergely Kalman (@gergely_kalman) and disclosed on 2023-11-14. The affected component is the Metal framework, which is a dependency of various macOS programs, most notably Music. The Music application is interesting to an attacker because it holds the FDA (Full Disk Access) entitlement.

The feature at the centre of the bug is an environment variable, MTL_DUMP_PIPELINES_TO_JSON_FILE, used by Metal. When the variable is set, Metal opens a file as the currently running application and writes debug data into it. The write is performed through NSFileManager's createFileAtPath(), which creates a file at the given path and overwrites an existing file.

Because the variable is read by a privileged application, and because the file name is fully attacker controlled, the feature turns into a file write primitive in the context of an FDA entitled process. The report turns that primitive into a full TCC bypass by racing the rename() that Metal performs after writing its temporary file.

Setup

The researcher did not use a manual audit. The bug was found with an automation framework written the previous year, which could:

  • select targets based on entitlements and library dependencies
  • run them over SSH in a dtrace harness (with SIP off)

The approach was deliberately simple but applied at scale: take a list of all FDA entitled apps on macOS, run them one by one under the dtracer while tracking system calls and certain libc calls, then analyse the resulting dataset.

The analysis grepped the dataset for getenv, and to make things simpler also for FILE, DIR, OUTPUT, LOG and similar strings. A few hits came back, and the dtracer's backtrace functionality was used to track them down. The bug described here is one of those hits.

The report does not state the macOS version tested, the exact build of Music, or the dtrace script itself.

Finding it

The hit that stood out was MTL_DUMP_PIPELINES_TO_JSON_FILE. The reasoning was that macOS is massive, that weird things happen (the report cites Music having FDA as an example), and that obvious, easy bugs likely go unnoticed. An environment variable that makes a privileged application write an attacker named file fits that theory exactly.

Setting the variable as follows:

Text
MTL_DUMP_PIPELINES_TO_JSON_FILE="path/name"

and pointing path at a valid directory triggers the bug. Watching the process with fs_usage shows the sequence:

  1. a file is open()ed, called path/.dat.nosyncXXXX.XXXXXX (X is random)
  2. one or more write()s write the contents to the file (the attacker does not control this)
  3. path/.dat.nosyncXXXX.XXXXXX is renamed()d to path/name

So the operation is a temporary file write followed by a rename() in place. The researcher notes it took a while to realise that this is not secure.

Exploitation

Step 1: understand why rename() in place is unsafe

For rename(old, new) to work, the kernel has to resolve the paths in old and new to vnode_t kernel structures. Filesystems are tricky (symlinks, hardlinks, mounts, . and .. entries, firmlinks), so the two paths are resolved separately. The report points to the xnu function renameat_internal() for the details.

Relative paths do not change this. Any path not starting with / is relative, and a leading . is shorthand for the current working directory (CWD; in the kernel, AT_FDCWD). If the CWD is /Users/hacker/, then:

Text
rename("./tmp/a", "./tmp/b")

is equivalent to:

Text
rename("/Users/hacker/tmp/a", "/Users/hacker/tmp/b")

Because the lookup happens twice, the directory /Users/hacker/tmp/ can be changed between the two lookups. Swapping tmp for a symlink at the right moment yields an attacker controlled destination:

Text
rename("/Users/hacker/test/tmp/a", "/PWNED/b")

The report adds an important caveat: there has to be a subdirectory in CWD that is attacker controlled. Renaming a file that is a direct descendant of the CWD is a special case that cannot be raced. rename("./a", "./b") is safe, rename("./tmp/a", "./tmp/b") is not. The researcher notes having previously stated that the direct descendant case was also racy, and corrects that here.

The swap can be done in several ways. The most convenient is the renamex_np(from, to, flags) system call with the RENAME_SWAP flag, which atomically swaps from and to if the filesystem supports it. Both default macOS filesystems, APFS and HFS+, do. This is a convenience, not a requirement for exploitation.

At this point the rename() is fully controlled:

  1. /PWNED/ can be replaced with anything.
  2. b came from the environment variable, so it can be changed too.

That gives total control over the destination path. The remaining piece is control over the contents of the source file. Since the rename() can be redirected anywhere on the filesystem, the attacker can point MTL_DUMP_PIPELINES_TO_JSON_FILE at a directory they own, with the filename set to the final destination filename. This way the attacker can catch the temporary file creation and control its contents (or keep an open file descriptor to it), while still controlling the filename part of the destination path.

Step 2: overwrite the user's TCC.db

The report gives the example of overwriting the user's TCC.db:

  1. create /Users/hacker/ourlink to point to /Users/hacker/Library/Application Support/com.apple.TCC/
  2. create the directory /Users/hacker/tmp/
  3. set MTL_DUMP_PIPELINES_TO_JSON_FILE=/Users/hacker/tmp/TCC.db
  4. trigger the bug by running Music with this env var
  5. catch the open() of /Users/hacker/tmp/.dat.nosyncXXXX.XXXXXX (X is random); here also open() this file for writing and hold on to the file descriptor
  6. atomically switch /Users/hacker/tmp with /Users/hacker/ourlink in a loop, to maximise the chance of winning the race, since the window is slim and losing has negligible downside
  7. wait a bit
  8. test whether the race was won; if not, run again from the top

If the race is won, the user's TCC.db has been overwritten with a file the attacker holds an open file descriptor to, which the report describes as game over.

Step 3: make the race safe to retry

There are two races:

  • race #1: catch the temporary file after it has been open()ed. This race is really easy to win.
  • race #2: swap the directory between the old and new lookups in rename(old, new). This race is harder, so a loop is used.

The worst case if race #2 goes wrong is trashing TCC.db. That is fairly inconsequential because it can be recovered with tccutil reset All, but the report calls that a dirty solution. To avoid it, the exploit only attempts race #2 after race #1 has been won. Because the new file is then always controlled, TCC.db is never overwritten with random data.

For race #2 there are only four outcomes:

Outcome 1, neither path changed:

Text
old: /Users/hacker/tmp/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/tmp/TCC.db

This is fine, it is as if nothing happened.

Outcome 2, tmp changed in old but not in new:

Text
old: /Users/hacker/Library/Application Support/com.apple.TCC/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/tmp/TCC.db

This is fine, the file in old does not exist and rename() errors out.

Outcome 3, tmp changed in new but not in old:

Text
old: /Users/hacker/tmp/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/Library/Application Support/com.apple.TCC/TCC.db

This is the winning scenario.

Outcome 4, tmp changed in both:

Text
old: /Users/hacker/Library/Application Support/com.apple.TCC/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/Library/Application Support/com.apple.TCC/TCC.db

There is no situation in which serious trouble is caused, so the swap can be looped and the exploit retried until it succeeds. The report calls this as good as a filesystem bug can get.

Step 4: demo and code

The report links a demo video:

Or watch on YouTube
From the original on gergelykalman.com

The full exploit code is published at https://github.com/gergelykalman/CVE-2023-32407-a-macOS-TCC-bypass-in-Metal.

Root cause

The root cause is stated plainly: a privileged application was relying on a complex, configurable library that in turn used an insecure file write API. Metal's createFileAtPath() based write, driven by an environment variable, gave an attacker a controlled path and a controlled filename inside an FDA entitled process. The temporary file plus rename() pattern looked safe but was not, because rename() resolves old and new separately and the intermediate directory can be swapped between the two lookups.

Existing defences did not stop it. The report notes that the dangerous createFileAtPath function in NSFileManager was not hardened. The report does not describe any other mitigation that was in place.

Impact

An attacker who can set the environment variable for a Metal using, FDA entitled application such as Music can overwrite files as that application. In the demonstrated case this means overwriting the user's TCC.db, which the report describes as game over: a TCC bypass and local privilege escalation. The report describes the result as a TCC bypass.

The conditions are explicit. The attacker needs to be able to set the environment variable for the target process, and needs an attacker controlled subdirectory in the process's CWD for the rename() race. Race #1 is easy to win; race #2 is harder and is retried in a loop. The report also notes that the bug was initially hoped to work on iOS, but both Apple and the researcher concluded that it does not, because it requires environment variable setting, which as far as the researcher knows is not possible on iOS.

Apple awarded $30,500 for the bug, which the report calls more than fair. The report notes that the bug needed much less time and effort than usual.

Fix

Apple removed the environment variable, closing the bug for good. Apple also removed several other environment variables in Metal that had similar abuse potential. The report praises this.

According to the timeline, a fix was spotted in beta2 on 2023-04-26, 42 days after the report was sent on 2023-03-15, and Apple confirmed on the same day that it was the fix. The report describes the response time and communication as great: no passive-aggressive canned responses, questions and comments were not ignored, the other person seemed invested, and the response time was fantastic.

The report also notes that the dangerous createFileAtPath function in NSFileManager was not hardened, which is presented as an observation for further research rather than as part of the fix.

Variants to test

  • Other environment variables read by privileged applications. Grep a syscall and libc dataset for getenv plus strings such as FILE, DIR, OUTPUT and LOG, as the researcher did. A positive result is an FDA entitled process that opens or writes a path derived from an environment variable.
  • Other temporary file plus rename patterns. Look for open() of a .dat.nosyncXXXX.XXXXXX style temporary file followed by rename() into place. A positive result is a rename whose destination directory can be swapped between the two lookups.
  • Other callers of createFileAtPath(). The report notes this NSFileManager function was not hardened. A positive result is a privileged process that calls it with an attacker influenced path.
  • Other renamex_np with RENAME_SWAP opportunities. The report uses RENAME_SWAP as a convenience for the atomic swap. A positive result is a filesystem and code path where the swap can be performed reliably during the race window.
  • Direct descendant versus subdirectory renames. Test rename("./a", "./b") against rename("./tmp/a", "./tmp/b"). A positive result confirms the report's caveat that only the subdirectory case is racy.

Timeline

  • 2023-03-15: report sent to Apple
  • 2023-03-28: deadline of 2023-10-09 given, as the talk was accepted at OBTSv6
  • 2023-04-26: fix spotted in beta2, seems solid
  • 2023-04-26: Apple confirms it is the fix
  • 2023-06-08: bug adjudicated for $30,500
  • 2023-06-08: researcher disputes the amount as it affected multiple platforms
  • 2023-06-08: Apple apologises for the confusion and confirms multiple platforms are affected, but deems the bug not exploitable on them (fair enough)
  • 2023-06-08: researcher promises to look into it
  • 2023-06-09: researcher tells Apple it will be done over the weekend, assuming haste is needed
  • 2023-06-10: Apple assures the researcher to take as much time as needed
  • 2023-06-11: after working over the weekend, no good vector found, as it requires environment variable setting, which as far as the researcher knows is not possible on iOS; Apple's assessment was correct
  • 2023-06-12: Apple thanks the researcher once more and payment details are discussed

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 20 min read.

What happened, in plain words

A security researcher found a way to make a normal macOS program write a file to a place it should never be allowed to write. The program he used was Apple's Music app. Music is special because Apple has given it a very broad permission called Full Disk Access (FDA). That permission lets an app read and write almost any file on the Mac, including the files that record which apps are allowed to do what.

The trick relied on a hidden setting called an environment variable. Environment variables are small pieces of text that a program reads when it starts, to learn things like where to find its files or how to behave. One of these variables, MTL_DUMP_PIPELINES_TO_JSON_FILE, tells a part of macOS called Metal to write some debugging information into a file whose name you choose.

Because Music has Full Disk Access, and because the file name comes from the environment variable, an attacker who can set that variable can make Music write a file almost anywhere. The researcher then used a timing trick (a race condition) to redirect the write into the file that stores macOS's privacy permissions, called TCC.db. Overwriting that file is what the report calls game over: a TCC bypass. That is why the bug is called a TCC bypass and a local privilege escalation.

The bug is tracked as CVE-2023-32407. The researcher is Gergely Kalman (@gergely_kalman). The report was disclosed on 2023-11-14. Apple paid $30,500 for it. Apple fixed it by removing the environment variable.

This article walks through the same steps as the technical write-up, but explains every idea from scratch. If you can use a browser and a computer, you can follow it.

Background: how the bug was found

The researcher did not sit and read code by hand. He had built an automation framework the year before: a set of scripts that run programs automatically and record what they do. The framework could:

  • select targets based on entitlements and library dependencies
  • run them over SSH in a dtrace harness (with SIP off)

A few of those words need unpacking.

An entitlement is a permission slip that Apple attaches to an app. It is not something the user grants; it is baked into the app by Apple when the app is signed. Full Disk Access (FDA) is one such entitlement. An app with FDA can read and write files that ordinary apps cannot, such as your Mail database or your Messages history.

A library dependency is another piece of software that a program needs in order to run. Think of a car: the car is the program, and the engine, the wheels and the radio are libraries it depends on. If one of those parts has a flaw, the whole car is affected, even if the car's own design is fine. Music depends on Metal, so a flaw in Metal is a flaw in Music.

SSH is a way to log in to another computer over a network and run commands on it. The researcher used it to drive his test machine remotely.

dtrace is a tool built into macOS (and other systems) that lets you watch a running program very closely: which system calls it makes, which library functions it calls, and in what order. A system call is a request from a program to the operating system kernel, for example "open this file" or "write these bytes". A library function is a reusable piece of code inside a shared library, for example getenv, which reads an environment variable.

SIP stands for System Integrity Protection. It is a macOS feature that stops even the administrator (root) from modifying certain protected parts of the system. Turning SIP off makes it easier to instrument and observe programs during research. The report says the harness ran with SIP off; it does not say anything else about the test machine.

The idea: dumb analysis, at scale

The researcher's plan was simple: do dumb analysis, but do it at scale. In other words, do not try to be clever about one program. Instead, run lots of programs automatically and look for patterns.

His reasoning was that macOS is enormous, that weird things happen (he gives the example of Music having FDA), and that obvious, easy bugs probably go unnoticed simply because nobody looks at every program.

To test the theory, he took a list of all FDA entitled apps on macOS and ran them one by one under his dtracer, recording system calls and certain libc calls. libc is the standard C library: the collection of basic functions that almost every program uses, such as reading environment variables or opening files.

When he analysed the recorded data, he searched it for getenv, the function that reads an environment variable. To make the results easier to read, he also searched for strings like FILE, DIR, OUTPUT, LOG and similar words, because environment variable names often contain those words.

A few hits came back. He used the dtracer's backtrace feature to find where each hit came from. A backtrace is a list of the function calls that led to a particular moment, like a stack of plates: the top plate is where you are now, and the plates below show how you got there. The bug in this article is one of those hits.

The report does not state which macOS version was tested, which build of Music was used, or what the dtrace script looked like.

The bug

The environment variable that stood out was MTL_DUMP_PIPELINES_TO_JSON_FILE. It is used by Metal, Apple's framework for talking to the graphics chip (the GPU). Metal is a dependency of various programs, most notably Music, which the researcher likes because it has FDA.

If the variable is set, Metal does roughly what its name suggests: it opens a file as the currently running application and writes some debug data into it. The write is triggered inside Metal by a call to NSFileManager's createFileAtPath(). NSFileManager is a macOS class for working with files, and createFileAtPath() creates a file at a given path. It also overwrites a file if one already exists, which is very useful for an attacker.

So there is a nice primitive here: any application that uses Metal can be made to write a file, and the attacker controls the file name completely. A primitive in security means a small, reliable capability that you can build bigger attacks from, like a single Lego brick that you can combine with others.

What does it do?

Set the variable like this:

Text
MTL_DUMP_PIPELINES_TO_JSON_FILE="path/name"

This line sets an environment variable named MTL_DUMP_PIPELINES_TO_JSON_FILE to the value "path/name". The quotes are there so that the shell treats the whole thing as one value. path is meant to be a directory, and name is the file name inside it.

If path is a valid directory, the bug triggers. Watching the program with fs_usage shows what happens. fs_usage is a macOS tool that prints file system activity as it happens, so you can see every file a program opens, writes or renames.

The sequence is:

  1. a file is open()ed, called path/.dat.nosyncXXXX.XXXXXX (X is random)
  2. one or more write()s write the contents to the file (the attacker does not control this)
  3. path/.dat.nosyncXXXX.XXXXXX is renamed()d to path/name

In plain words: Metal creates a temporary file with a random name, writes its debug data into it, and then renames the temporary file to the name you asked for. The random part (XXXXXX) makes the temporary name hard to guess, which is a common safety measure so that two programs do not collide.

It took the researcher a while to realise that this pattern, a temporary file followed by a rename() in place, is not secure. You might already know this, he writes, but he did not.

Why renaming in place is not safe

This is the heart of the bug, so it is worth going slowly.

When you call rename(old, new), the operating system kernel has to turn the text paths old and new into internal structures called vnode_t. A vnode is the kernel's in-memory representation of a file. Turning a path into a vnode is called resolving the path, and it is not simple, because file systems are tricky: there are symlinks (a file that points to another path, like a shortcut), hardlinks (two names for the same file), mounts (another disk attached at a directory), the special . and .. entries, and Apple's own firmlinks (a macOS-specific way of joining parts of the file system).

Because of all that, the kernel resolves old and new separately. It has to: old might be inside new, old might be a symlink, new might be a directory, they might even be the same file. The whole rename functionality is, in the researcher's words, incredibly, incredibly complicated. He points to the xnu function renameat_internal() for the details. xnu is the name of the macOS kernel.

Now, relative paths do not change this. Any path that does not start with / is relative, and a leading . is shorthand for the current working directory (CWD). In the kernel this is called AT_FDCWD, but you do not need to remember that name.

So if the CWD is /Users/hacker/, then this call:

Text
rename("./tmp/a", "./tmp/b")

is equivalent to:

Text
rename("/Users/hacker/tmp/a", "/Users/hacker/tmp/b")

The first line renames a to b inside the tmp directory, using a path relative to wherever the program currently is. The second line does exactly the same thing, but spells out the full path from the root of the disk. The kernel treats them the same way.

This does not look nearly as innocent now, because the lookup happens twice. That means the directory /Users/hacker/tmp/ can be changed between the first lookup and the second. If you swap tmp for a symlink at the right moment, between the first lookup and before the second, you end up with an attacker-controlled destination:

Text
rename("/Users/hacker/test/tmp/a", "/PWNED/b")

Here the source path still points into the attacker's own directory, but the destination has been redirected to /PWNED/b, a place the attacker chose. The kernel resolved old while tmp was a real directory, and resolved new after tmp had been replaced by a symlink pointing somewhere else.

There is one giant caveat. There has to be a subdirectory in the CWD that the attacker controls. Renaming a file that is a direct child of the CWD is a special case that cannot be raced. rename("./a", "./b") is safe, but rename("./tmp/a", "./tmp/b") is not. The researcher notes that he previously stated the direct-child case was also racy, and corrects that here.

Why the difference? When the file is a direct child of the CWD, there is no intermediate directory to swap. The kernel resolves the parent (the CWD) once, and there is nothing in between to change. When there is a subdirectory, that subdirectory is a separate component that can be replaced between the two lookups.

How do you do the swap? There are several ways, but the most convenient is the renamex_np(from, to, flags) system call with the RENAME_SWAP flag. This atomically swaps from and to if the file system supports it. Atomic means it happens in one indivisible step: no other program can see a half-finished state. Both default macOS file systems, APFS and HFS+, support it. The report stresses that this is a convenience, not a requirement for exploitation.

At this point the rename() is fully controlled:

  1. /PWNED/ can be replaced with anything.
  2. b originally came from the environment variable, so it can be changed too.

That gives total control over the destination path. The only thing left is control over the contents of the source file. Since the rename() can be redirected anywhere on the file system, the attacker can point MTL_DUMP_PIPELINES_TO_JSON_FILE at a directory they own, with the file name set to the final destination file name. This way the attacker can catch the temporary file creation and control its contents (or keep an open file descriptor to it), while still controlling the file name part of the destination path. A file descriptor is a handle the operating system gives you when you open a file; as long as you hold it, you can keep reading or writing that file even if its name changes.

The exploit

The report gives the example of overwriting the user's TCC.db. TCC stands for Transparency, Consent and Control. It is the macOS privacy system that records which apps are allowed to access your camera, microphone, contacts, disk and so on. TCC.db is the database file that stores those decisions. If an attacker can rewrite it, they can grant themselves permissions the user never approved.

The steps are:

  1. create /Users/hacker/ourlink to point to /Users/hacker/Library/Application Support/com.apple.TCC/
  2. create the directory /Users/hacker/tmp/
  3. set MTL_DUMP_PIPELINES_TO_JSON_FILE=/Users/hacker/tmp/TCC.db
  4. trigger the bug by running Music with this env var
  5. catch the open() of /Users/hacker/tmp/.dat.nosyncXXXX.XXXXXX (X is random); here also open() this file for writing and hold on to the file descriptor
  6. atomically switch /Users/hacker/tmp with /Users/hacker/ourlink in a loop, to maximise the chance of winning the race, since the window is slim and losing has negligible downside
  7. wait a bit
  8. test whether the race was won; if not, run again from the top

In plain words, step by step:

  • Step 1 creates a symlink called ourlink that points at the real TCC directory. A symlink is like a signpost: it is not the destination itself, it just points there.
  • Step 2 creates a normal directory called tmp that the attacker owns.
  • Step 3 tells Metal to write a file called TCC.db inside tmp.
  • Step 4 starts Music with that variable set, which makes Metal begin its temporary-file-then-rename dance.
  • Step 5 is the first race. The attacker watches for the temporary file to be created, opens it too, and keeps the handle. This is easy to win because the file exists for a while.
  • Step 6 is the second race. The attacker keeps swapping tmp and ourlink as fast as possible. If the swap lands between the two lookups inside rename(), the destination becomes the real TCC directory.
  • Steps 7 and 8 are just bookkeeping: wait, check, retry.

If the race is won, the user's TCC.db has been overwritten with a file the attacker holds an open file descriptor to. The report calls this game over.

Making the race safe to retry

There are two races:

  • race #1: catch the temporary file after it has been open()ed. This race is really easy to win.
  • race #2: swap the directory between the old and new lookups in rename(old, new). This race is harder, so a loop is used.

The worst case if race #2 goes wrong is trashing TCC.db. That is fairly inconsequential because it can be recovered with tccutil reset All, a macOS command that resets all privacy decisions, but the report calls that a dirty solution. To avoid it, the exploit only attempts race #2 after race #1 has been won. Because the new file is then always controlled, TCC.db is never overwritten with random data.

For race #2 there are only four possible outcomes. The report lists them with the paths spelled out.

Outcome 1, neither path changed:

Text
old: /Users/hacker/tmp/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/tmp/TCC.db

This is fine, it is as if nothing happened. The temporary file is renamed to TCC.db inside the attacker's own tmp directory.

Outcome 2, tmp changed in old but not in new:

Text
old: /Users/hacker/Library/Application Support/com.apple.TCC/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/tmp/TCC.db

This is fine, the file in old does not exist and rename() errors out. The attacker's swap happened too early, before the source was resolved.

Outcome 3, tmp changed in new but not in old:

Text
old: /Users/hacker/tmp/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/Library/Application Support/com.apple.TCC/TCC.db

This is the winning scenario. The source is the attacker's temporary file, and the destination is the real TCC database.

Outcome 4, tmp changed in both:

Text
old: /Users/hacker/Library/Application Support/com.apple.TCC/.dat.nosyncXXXX.XXXXXX
new: /Users/hacker/Library/Application Support/com.apple.TCC/TCC.db

The swap happened too early for both lookups.

There is no situation in which serious trouble is caused, so the swap can be looped and the exploit retried until it succeeds. The report calls this as good as a file system bug can get.

Demo and code

The report links a demo video:

Or watch on YouTube
From the original on gergelykalman.com

The report links a demo video.

The full exploit code is published at https://github.com/gergelykalman/CVE-2023-32407-a-macOS-TCC-bypass-in-Metal.

The fix

Apple removed the environment variable, closing the bug for good. Apple also removed several other environment variables in Metal that had similar abuse potential. The report praises this.

According to the timeline, a fix was spotted in beta2 on 2023-04-26, 42 days after the report was sent on 2023-03-15, and Apple confirmed on the same day that it was the fix. The report describes the response time and communication as great: no passive-aggressive canned responses, questions and comments were not ignored, the other person seemed invested, and the response time was fantastic.

The report also notes that the dangerous createFileAtPath function in NSFileManager was not hardened. That is presented as an observation for further research, not as part of the fix.

Root causes

The root cause is stated plainly: a privileged application was relying on a complex, configurable library that in turn used an insecure file write API. An API is a defined way for one piece of software to ask another to do something, like a menu of functions you can call. createFileAtPath() is such an API.

Metal's createFileAtPath() based write, driven by an environment variable, gave an attacker a controlled path and a controlled file name inside an FDA entitled process. The temporary file plus rename() pattern looked safe but was not, because rename() resolves old and new separately and the intermediate directory can be swapped between the two lookups.

Existing defences did not stop it. The report notes that the dangerous createFileAtPath function in NSFileManager was not hardened.

Impact

An attacker who can set the environment variable for a Metal-using, FDA entitled application such as Music can overwrite files as that application. In the demonstrated case this means overwriting the user's TCC.db, which the report describes as game over: a TCC bypass and local privilege escalation. The report describes the result as a TCC bypass.

The conditions are explicit. The attacker needs to be able to set the environment variable for the target process, and needs an attacker-controlled subdirectory in the process's CWD for the rename() race. Race #1 is easy to win; race #2 is harder and is retried in a loop. The report also notes that the bug was initially hoped to work on iOS, but both Apple and the researcher concluded that it does not, because it requires environment variable setting, which as far as the researcher knows is not possible on iOS.

Apple awarded $30,500 for the bug, which the report calls more than fair. The report notes that the bug needed much less time and effort than usual.

Further research

  • Other environment variables read by privileged applications. Grep a system call and libc dataset for getenv plus strings such as FILE, DIR, OUTPUT and LOG, as the researcher did. A positive result is an FDA entitled process that opens or writes a path derived from an environment variable.
  • Other temporary file plus rename patterns. Look for open() of a .dat.nosyncXXXX.XXXXXX style temporary file followed by rename() into place. A positive result is a rename whose destination directory can be swapped between the two lookups.
  • Other callers of createFileAtPath(). The report notes this NSFileManager function was not hardened. A positive result is a privileged process that calls it with an attacker-influenced path.
  • Other renamex_np with RENAME_SWAP opportunities. The report uses RENAME_SWAP as a convenience for the atomic swap. A positive result is a file system and code path where the swap can be performed reliably during the race window.
  • Direct child versus subdirectory renames. Test rename("./a", "./b") against rename("./tmp/a", "./tmp/b"). A positive result confirms the report's caveat that only the subdirectory case is racy.

The report also notes that research into bugs like this will be heavily hindered by the introduction of AMFI. AMFI is Apple's code-signing enforcement layer; the report says it cuts down the attack surface significantly. The report does not go into detail about how AMFI works.

Timeline

  • 2023-03-15: report sent to Apple
  • 2023-03-28: deadline of 2023-10-09 given, as the talk was accepted at OBTSv6
  • 2023-04-26: fix spotted in beta2, seems solid
  • 2023-04-26: Apple confirms it is the fix
  • 2023-06-08: bug adjudicated for $30,500
  • 2023-06-08: researcher disputes the amount as it affected multiple platforms
  • 2023-06-08: Apple apologises for the confusion and confirms multiple platforms are affected, but deems the bug not exploitable on them (fair enough)
  • 2023-06-08: researcher promises to look into it
  • 2023-06-09: researcher tells Apple it will be done over the weekend, assuming haste is needed
  • 2023-06-10: Apple assures the researcher to take as much time as needed
  • 2023-06-11: after working over the weekend, no good vector found, as it requires environment variable setting, which as far as the researcher knows is not possible on iOS; Apple's assessment was correct
  • 2023-06-12: Apple thanks the researcher once more and payment details are discussed

Glossary

  • API: a defined way for one piece of software to ask another to do something, like a menu of functions you can call.
  • APFS: Apple's default file system on modern Macs.
  • Atomic: happening in one indivisible step, so no other program can see a half-finished state.
  • Backtrace: a list of the function calls that led to a particular moment in a program, like a stack of plates.
  • CVE: a public identifier for a known security vulnerability, here CVE-2023-32407.
  • CWD (current working directory): the directory a program is currently "in", used as the starting point for relative paths.
  • dtrace: a tool that lets you watch a running program's system calls and library calls in detail.
  • Entitlement: a permission slip baked into an app by Apple when it is signed, such as Full Disk Access.
  • Environment variable: a small piece of text a program reads at start-up to learn how to behave.
  • FDA (Full Disk Access): a macOS permission that lets an app read and write almost any file on the disk.
  • File descriptor: a handle the operating system gives you when you open a file, which you keep as long as you want to read or write it.
  • Firmlink: a macOS-specific way of joining parts of the file system.
  • fs_usage: a macOS tool that prints file system activity as it happens.
  • getenv: the standard C function that reads an environment variable.
  • HFS+: an older Apple file system, still supported.
  • Hardlink: two names for the same file.
  • Kernel: the core of the operating system, which manages files, memory and processes.
  • libc: the standard C library, the collection of basic functions almost every program uses.
  • Library dependency: another piece of software a program needs in order to run.
  • Local privilege escalation: gaining higher permissions on the same machine, for example going from a normal user to an administrator.
  • Metal: Apple's framework for talking to the graphics chip (GPU).
  • Mount: attaching another disk or file system at a directory.
  • NSFileManager: a macOS class for working with files.
  • Primitive: a small, reliable capability that you can build bigger attacks from.
  • Race condition: a bug that depends on timing, where two operations happen in an order the programmer did not expect.
  • Relative path: a path that does not start with / and is interpreted from the current working directory.
  • renamex_np: a macOS system call that renames files, with extra flags such as RENAME_SWAP.
  • RENAME_SWAP: a flag that atomically swaps two paths.
  • Resolve (a path): turn a text path into the kernel's internal representation of a file.
  • SIP (System Integrity Protection): a macOS feature that stops even root from modifying certain protected parts of the system.
  • SSH: a way to log in to another computer over a network and run commands on it.
  • Symlink: a file that points to another path, like a shortcut or signpost.
  • System call: a request from a program to the operating system kernel, for example "open this file".
  • TCC (Transparency, Consent and Control): the macOS privacy system that records which apps may access which resources.
  • TCC bypass: getting around the TCC privacy system, for example by rewriting its database.
  • TCC.db: the database file that stores TCC decisions.
  • tccutil: a macOS command-line tool for managing TCC decisions, including tccutil reset All.
  • vnode_t: the kernel's in-memory representation of a file.
  • xnu: the name of the macOS kernel.

More by Gergely Kalman

View all

7 more disclosed reports, best first.

More from Apple (macOS)

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.