sqlol (CVE-2023-32422) - a macOS TCC bypass
A debug feature in libsqlite, enabled by the SQLITE_SQLLOG_DIR environment variable, wrote copies of opened databases to an attacker-controlled directory with predictable filenames, followed symlinks, and overwrote files. An attacker could use a privileged app like Music to overwrite the user's TCC.db and gain access to protected data.
Published on gergelykalman.com
No, it's not. But, since I'm presently just ill enough to not be productive, yet well enough to write, I figured I'd chip away at my horrendous (writeup) debt while I wait for the immune fairy to arrive.
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.
Context
This write-up covers a macOS local privilege escalation and TCC bypass that the researcher calls "sqlol", tracked as CVE-2023-32422. The target is libsqlite as shipped on macOS, and specifically a debugging feature that is enabled through an environment variable. The researcher found it while analysing applications that hold the FDA (Full Disk Access) entitlement, and the bug was the first one produced by the automation framework he describes in his separate post about "lateralus".
The feature in question is SQLITE_SQLLOG_DIR. When it is set, libsqlite turns on debugging functionality that writes debug files to a directory chosen by whoever set the variable. According to the report, the files written are:
- a full copy of any opened sqlite database
- a list of SQL statements executed by the connection
- an index file mapping database names to these file names
The report notes that this functionality only exists in the code if (lib)sqlite is compiled with the SQLITE_ENABLE_SQLLOG preprocessor symbol, and points to the responsible source file, test_sqllog.c, in the sqlite source tree. The file is well documented, and the researcher quotes its own description of the output:
** OUTPUT:
**
** The SQLITE_SQLLOG_DIR is populated with three types of files:
**
** sqllog_N.db - Copies of database files. N may be any integer.
**
** sqllog_N.sql - A list of SQL statements executed by a single
** connection. N may be any integer.
**
** sqllog.idx - An index mapping from integer N to a database
** file name - indicating the full path of the
** database from which sqllog_N.db was copied
In other words, a normal production build of sqlite has no business writing copies of databases and query logs to an attacker-chosen directory, yet this debug functionality was compiled in and shipped to millions, if not billions, of devices.
Setup
The report does not describe a formal lab setup with specific macOS versions for the initial research. What it does establish as preconditions:
- A macOS system whose
libsqliteis built withSQLITE_ENABLE_SQLLOG. - An application that uses
libsqliteand holds theFDA(Full Disk Access) entitlement. The researcher usesMusicas the example, and notes that any otherFDAapplication that useslibsqlitewould work equally well. - The ability to set the
SQLITE_SQLLOG_DIRenvironment variable for the target process, and to point it at a directory the attacker controls. - The ability to write to folders belonging to the target application (for example
Music's folders), which the researcher states he had. - App sandboxing enabled. During the ticket, Apple asked whether sandboxing was enabled because they did not see it, and the researcher confirmed that it was.
The exploit code was submitted first in BASH and later rewritten in obj-c. The full exploit is published on the researcher's GitHub, linked in the original as https://github.com/gergelykalman/CVE-2023-32422-a-macOS-TCC-bypass-in-sqlite.
Finding it
While analysing FDA-entitled applications, the researcher encountered many peculiar environment variables. One stood out because its name was ominous: SQLITE_SQLLOG_DIR. He was not deeply familiar with sqlite, but he knew that logging in this way can be dangerous. A common library like libsqlite is used by many applications, including privileged ones, and because the destination directory is attacker-provided, it could lead to an escalation of privilege.
Looking at the documented behaviour confirmed the concern: the variable turns on debug output that includes a full copy of any opened database, a list of executed SQL statements, and an index file. The researcher's reaction in the report is that this obviously dangerous debug functionality somehow got compiled in and shipped.
Exploitation
The exploitation path went through two stages. The first was an information leak, which is what the initial report to Apple described. The second was a full TCC bypass, which required a trick the researcher calls "sqlite smuggling".
Step 1: information leak via symlinks
Because the functionality is exposed to any user, an attacker can leak any sqlite database. The technique is to use symlinks pointing at the sqlite databases of interest. A privileged application such as Music, which holds FDA, can then be used to leak any sensitive sqlite database, because it can read files the attacker cannot. The report notes that any other application with FDA could also be used, as long as it uses libsqlite.
The researcher describes this as a severe infoleak, but limited: it can only leak sqlite databases, of which there are many, but it is not as bad as it can get. He wanted to turn it into a TCC bypass.
Step 2: recognising the write primitives
The researcher noticed that the file writes were done with no hardening at all, which makes sense for functionality that was never meant to run on a production system. When writing the files, the open() system calls:
- had predictable filenames
- would follow symlinks
- would overwrite files
Putting these together, the writes can be used trivially to overwrite the user's TCC.db and gain complete access to all the private, TCC-protected data: databases, contacts, location, microphone, webcam, and whatever else TCC is meant to protect.
There was one obstacle: the content written was only partially controlled, and not to an extent that would have allowed overwriting the entire TCC.db with arbitrary content. Because of this, the initial report to Apple described the infoleak vector, while the researcher tried to work out how to elevate it to a full TCC bypass. At the time this seemed like a remote possibility.
Step 3: the content problem
After the report the researcher moved on to other bugs and came back to this one only occasionally, always failing to make it work. He could control bytes going into the files, but that was useless without a good target.
The only file he could presumably overwrite was the user's TCC.db, but because it is an sqlite file, a partial or garbage write would result in a crash or a corrupted database. This is unlike /etc/sudoers, which the report notes is happy to parse any kind of garbage.
He therefore had two options:
- find a privileged target file that tolerated garbage bytes, or
- control the entire content being written, and target the user's
TCC.db
For option 1, the researcher states he has no solution to this day, and invites readers who do to send him a DM, since it would turn a lot of his findings exploitable.
For option 2, the situation looked bad. There was no way to get away with partially writing the sqlite database, and no way to control the write from the first byte. At best he could overwrite sqlite databases entirely, or use the query log, perhaps with some trickery, to do a partially controlled write.
Step 4: making the source database attacker-controlled
It took time for the key insight to click: he could make the original sqlite files come from himself. Since he had access to Music's folders, nothing stopped him from overwriting Cache.db (for example), and then using the copy of that to overwrite the user's TCC.db. This way he could put whatever tables and rows he wanted into the resulting database, which ought to be enough.
He built a demo exploit, but it kept failing. Music was unhappy about not finding the tables it normally expects. Even though the user's TCC.db had been overwritten with arbitrary content, Music would reset the entire database when it realised that tables were missing.
Killing Music after the copy is made was one option, but the resulting exploit would be unreliable. Instead, he decided to smuggle a valid TCC.db into Cache.db. This works because a sqlite database can contain lots of tables, and it is unlikely that a given program would check all tables in the database or for table names to collide.
Because the table names were different, he wrote a quick and dirty PoC, and to his surprise it worked perfectly. TCC.db was overwritten, carrying with it the list of tables that a valid Cache.db would have. At that point he had full control over the contents of TCC.db, at least over the tables that matter.
Step 5: the exploit sequence
With the smuggling trick in hand, the exploit is constructed as follows:
- dump a valid
TCC.dbtotcc.sql - dump a valid
Cache.dbtocache.sql - merge the two sql files and use the result to create
payload.db - use
payload.dbto overwriteMusic'sCache.db- any other app and database would also work, as long as they do not touch the extra tables that were smuggled in
- trigger the bug with a directory location the attacker controls
- stop the program
- see what files get generated
- symlink the file that contains the database copy to
TCC.db- if
SQLITE_SQLLOG_REUSE_FILES=1is used, the filenames can be relied upon to stay the same
- if
- trigger the bug again to overwrite the target file
The report does not include the individual commands for these steps; it points to the demo video and the GitHub repository instead.
Demo
The report embeds a demo video:
The video is linked in the original as [Demo video](https://www.youtube.com/watch?v=rfGcd0YrbTM). The report does not describe the video's contents beyond presenting it as the demo of the exploit.
Root cause
The root cause is that a debugging feature of libsqlite, enabled by the SQLITE_SQLLOG_DIR environment variable and compiled in only when SQLITE_ENABLE_SQLLOG is defined, was present in production builds. The feature writes copies of opened databases, query logs and an index file to a directory chosen by the environment variable. The open() calls used for these writes had predictable filenames, followed symlinks and overwrote existing files, with no hardening.
Existing defences did not stop it. The writes happen inside a privileged, FDA-entitled process, so the process's own access rights apply to the destination. Symlinks are followed, so an attacker can redirect a write to a file the attacker cannot write directly, such as the user's TCC.db. The predictable filenames make the target of the symlink reliable, and with SQLITE_SQLLOG_REUSE_FILES=1 the filenames can be relied upon to stay the same. The report does not discuss any sandbox or TCC check that would have blocked the write; it only notes that the researcher confirmed app sandboxing was enabled when Apple asked.
The researcher describes the bug as a fairly trivial one and a clear oversight on Apple's part: the functionality should never have been compiled in on a production build, and it came as a huge surprise to him that it existed, and that nobody had discovered it much earlier.
Impact
Under the conditions above, an attacker can:
- leak any sqlite database readable by an
FDA-entitled application that useslibsqlite, by symlinking the target database and triggering the debug output - overwrite the user's
TCC.dbwith attacker-controlled content, using the smuggling technique, and thereby gain complete access to all the private, TCC-protected data: databases, contacts, location, microphone, webcam, and whatever else TCC is meant to protect
The report hedges the scope in a few places. The infoleak is limited to sqlite databases. The overwrite depends on the attacker being able to write to the target application's folders (for example Music's Cache.db) and on the application not touching the smuggled extra tables. The researcher initially thought the bug could affect iOS as well, but concluded that it could not: the environment variable cannot be set on iOS, similar to his other bug. Apple's position, quoted in the timeline, was that the issue is only exploitable on macOS.
Apple awarded $30,500 for the bug. The researcher calls this fair for a macOS TCC bypass, and asked why it was only 1/3 of the advertised maximum, which Apple explained by the macOS-only exploitability.
Fix
To fix the bug, Apple simply removed this functionality. The researcher spotted a fix in 13.4beta3, which he reported to Apple, noting that it looked solid although he had only glanced at it.
Timeline and process notes
The report includes a detailed timeline, which is useful for anyone running a similar disclosure:
- 2022-10-28: report sent to Apple, including the infoleak exploit in BASH. At this time it was not clear whether this was more than an infoleak, but the researcher knew it could also do a partially controlled file write, which he also put in the report.
- 2022-11-12: the same exploit sent again, rewritten in obj-c.
- 2022-11-14: Apple is investigating.
- 2022-11-16: Apple asks whether app sandboxing was enabled, as they did not see it. The researcher confirms it was.
- 2022-12-07: the researcher reports that his exploit is breaking now, asks whether Apple managed to reproduce it with sandboxing on at all, and whether the sqlite exploit and the sandbox escape should be separate tickets.
- 2022-12-08: Apple confirms they did not reproduce it, but are addressing the issue anyway. They say the sandbox escape should be submitted separately if it is not related to sqlite.
- 2022-12-09: the researcher asks whether this would still count as a full chain and whether he should submit separately.
- 2022-12-10: Apple says unique issues should be reported separately, says nothing about what happens if a chain is sent in that way, and reiterates that each will be paid according to the ASB's rules.
- 2022-12-15: the researcher submits the
TCC.dboverwrite exploit for the first time, and submits the sandbox escape issue separately in another ticket. Apple says they are reviewing it. - 2023-01-18: the researcher requests an update. Apple thanks him for his patience and says this will be fixed in a future release, after which it will be evaluated for bounty.
- 2023-03-06: the researcher checks the ticket and finds it silently closed. He tells Apple, and messages again saying the exploit still works on
13.3beta2. - 2023-03-07: Apple apologises about the ticket status issue, confirms the fix, asks for more time, and says the report only qualifies for the bounty after it is fixed.
- 2023-03-28: the researcher asks for an update as he saw no fix in the latest release, and tells Apple this will be public at the time of the OBTSv6 conference, in about 6 months.
- 2023-03-29: Apple says a new fix will be issued in the following months and confirms the researcher will be credited.
- 2023-04-26: the researcher spots a fix in
13.4beta3and lets Apple know; the fix looks solid but he only glanced at it. Apple thanks him and says they will reach out if there is an update. - 2023-05-15: the researcher reports that the CVE text is incorrect: it says the bug is an infoleak, possibly because of a bad report title and because the
TCC.dboverwrite was not sent initially. - 2023-05-18: Apple says the text is correct and will not be changed. The researcher asks Apple to confirm, in which case he would publish the information, and resends the earlier exploit as it seemingly fell through the cracks.
- 2023-05-19: Apple says they are retesting and thanks him for following up.
- 2023-05-22: the ticket moves to "resolved" status; the researcher asks why.
- 2023-05-23: Apple confirms they could reproduce the
TCCbypass, will update the CVE text, and will adjudicate the bug. The researcher apologises for the confusion, as the title did not sayTCCbypass, which did not occur to him when he originally filled out the report. - 2023-06-02: Apple says the CVE text got updated, with another comment to come after adjudication. The researcher says this is great news but that it became a collision with Wojciech Reguła, and asks why and when the bug will be adjudicated.
- 2023-06-08: Apple says the bug is awarded
$30,500. The researcher asks why it is only one third of the advertised maximum. Apple says the issue is only exploitable on macOS, but welcomes him to prove them wrong. - 2023-06-09: the researcher says he will look into it; Apple says he can take his time.
- 2023-06-11: the researcher reports that he got nothing substantial, as the environment variable cannot be set on iOS, like with his other bug. He accepts the bounty.
- 2023-06-12: Apple thanks him for giving it a shot, and payment details are discussed.
On the collision: months into the process, Apple deemed the bug a collision with another researcher, Wojciech Reguła. The researcher had to find this out from the Apple Security Releases, as Apple had not told him. He reached out to Wojciech, whom he describes as a great researcher and a super nice guy, and they discussed timelines; it seemed the researcher was a few months earlier. After adjudication Apple confirmed that he submitted first.
On communication, the researcher separates response time from communication quality. The fix took 180 days since the initial report, which he calls not good enough, and he feels partially responsible because the initial report was filed as an infoleak. He still thinks Apple should not have taken this long, even for "just" an infoleak. Communication, by contrast, was great: the person on the other end answered his questions and held his hand through the process, which softened an otherwise frustrating experience.
His lessons from the process:
- Always submit unique issues separately. Each qualifying bug is paid for, and squeezing multiple bugs into one ticket only introduces confusion: one bug, one impact, one PoC, one ticket. He acknowledges this is not always possible, since a higher impact may only become clear after later research, and that reporting early avoids collisions, at the cost of a potentially longer fix time if a higher impact is found later.
- Always submit an exploit with maximum impact if possible. A reliable, high-impact PoC is a good way to expedite the process, because a triager who can reproduce easily will have a short timeline. This is not a guarantee, since a bug in a particularly nasty place to fix can still take ages.
He also suggests that Apple set up a separate "ASB Support" communication channel for questions about the programme, so that triagers' time is not taken up by programme questions rather than the bug itself.
Variants to test
These are concrete directions suggested by the report's reasoning, for testing on other targets. The report does not claim any of them were tested.
- Other
FDA-entitled applications that uselibsqlite. The report states that any such application could be used, not justMusic. A positive result is that settingSQLITE_SQLLOG_DIRto an attacker-controlled directory causes the application to writesqllog_N.db,sqllog_N.sqlandsqllog.idxfiles there. - Other applications whose folders the attacker can write to, used as the source of the smuggled database. The report says any other app and database would work, as long as the app does not touch the extra tables. A positive result is that the application keeps running normally after its database is replaced with one containing the smuggled tables.
- Other privileged target files that tolerate garbage bytes, as an alternative to
TCC.db. The researcher explicitly says he has no solution for this and asks readers to contact him if they find one. A positive result would be a privileged file that parses successfully after a partially controlled write. - Other debug or logging environment variables in privileged libraries that write to attacker-chosen paths. A positive result is a write to a path the attacker cannot write directly, performed by a process with higher privileges.
- Using
SQLITE_SQLLOG_REUSE_FILES=1to stabilise filenames across runs. The report mentions this as the way to rely on the generated filenames staying the same. A positive result is that the samesqllog_N.dbname is reused on a second trigger, so a symlink planted at that name is followed again. - Checking whether the same environment variable can be set on other platforms. The researcher tried iOS and found it was not possible, similar to his other bug. A positive result on another platform would be that the variable can be set for a privileged process and the debug files appear.
What happened, in plain words
A security researcher found a bug in macOS, Apple's operating system for Mac computers. The bug let an ordinary user program trick a more powerful program into writing files wherever the attacker wanted. By chaining a few tricks, the attacker could take full control of a database that decides which apps are allowed to see your private data, such as your contacts, location, microphone and webcam. That database is called TCC.db. The bug is tracked as CVE-2023-32422, and the researcher nicknamed it "sqlol". Apple paid a bounty of $30,500 for it.
The researcher found the bug with an automated tool he built, and it was the first bug that tool ever found. The report is written for other security researchers, so it uses a lot of jargon. This version explains everything from scratch, assuming you only know how to use a computer and a browser.
Background: what is sqlite, and what is TCC?
Before the details, a few ideas you need.
sqlite is a small database engine. A database is an organised collection of data, like a very advanced spreadsheet. Many programs on your Mac use sqlite to store their settings, caches and other data. The version of sqlite that comes with macOS is a shared library called libsqlite. A library is a bundle of code that many programs can use, so a bug in a library can affect every program that uses it.
TCC stands for Transparency, Consent and Control. It is the part of macOS that asks you "Do you want to allow this app to access your camera?" and remembers your answer. The answers are stored in a database file called TCC.db. If an attacker can change that file, they can silently grant themselves access to everything TCC protects.
Full Disk Access (FDA) is a special permission that lets a program read files that normal programs cannot, including TCC.db. Some Apple programs, such as Music, have this permission because they need it to work.
Environment variables are named settings that a program reads when it starts. For example, a variable might tell a program where to find temporary files. On macOS, a user can often set environment variables for programs they launch.
A symlink (symbolic link) is a shortcut file that points to another file. If a program opens a symlink, the operating system follows the shortcut and opens the real file instead. This is useful, but dangerous if an attacker can create symlinks: they can make a program write to a file the attacker could not otherwise touch.
A local privilege escalation is when a user with limited rights finds a way to gain the rights of a more powerful user or program on the same machine. A TCC bypass is a specific kind: getting around the privacy protections that TCC enforces.
The bug: a hidden debugging feature
While looking at programs that have Full Disk Access, the researcher noticed many unusual environment variables. One had an ominous name: SQLITE_SQLLOG_DIR.
He was not an expert on sqlite, but he knew that logging can be dangerous. A common library like libsqlite is used by many programs, including powerful ones. If the place where logs are written can be chosen by an attacker, that could lead to privilege escalation.
What does the variable do?
Setting SQLITE_SQLLOG_DIR turns on a debugging feature inside libsqlite. The feature writes debug files to whatever directory the variable points to. According to the report, the files are:
- a full copy of any sqlite database that is opened
- a list of SQL statements (database commands) executed by the connection
- an index file that maps database names to these file names
This feature only exists in the code if sqlite was compiled with a special switch called SQLITE_ENABLE_SQLLOG. Compiling means turning source code into a program; a compiler switch is an option that includes or excludes parts of the code. The researcher points to the source file test_sqllog.c in the sqlite project, which documents the behaviour. He quotes that documentation:
** OUTPUT:
**
** The SQLITE_SQLLOG_DIR is populated with three types of files:
**
** sqllog_N.db - Copies of database files. N may be any integer.
**
** sqllog_N.sql - A list of SQL statements executed by a single
** connection. N may be any integer.
**
** sqllog.idx - An index mapping from integer N to a database
** file name - indicating the full path of the
** database from which sqllog_N.db was copied
Line by line:
- The first lines are a comment block from the source code, introduced by
**. SQLITE_SQLLOG_DIR is populated with three types of filesmeans the directory named by the variable will receive three kinds of file.sqllog_N.dbis a copy of a database file.Nis a number, so you might seesqllog_1.db,sqllog_2.db, and so on.sqllog_N.sqlis a text file listing the SQL commands that were run on one connection. A connection is one program's link to the database.sqllog.idxis an index: a small file that records which number corresponds to which original database file, including its full path.
In plain terms: a normal production build of sqlite should never write copies of databases and logs to a directory chosen by an attacker. Yet this debug feature was compiled in and shipped to millions, perhaps billions, of devices.
Setting the stage
The report does not describe a formal lab setup with specific macOS versions for the initial research. It does establish what is needed:
- A Mac whose
libsqlitewas built withSQLITE_ENABLE_SQLLOG. - A program that uses
libsqliteand has Full Disk Access. The researcher usesMusicas the example, and notes that any other FDA program usinglibsqlitewould work too. - The ability to set the
SQLITE_SQLLOG_DIRenvironment variable for that program, pointing it at a folder the attacker controls. - The ability to write to folders belonging to the target program (for example,
Music's folders). The researcher says he had this. - App sandboxing turned on. During the ticket, Apple asked whether sandboxing was enabled because they did not see it, and the researcher confirmed it was. A sandbox is a security box that limits what a program can do; the researcher confirmed it was on when Apple asked.
The exploit code was first sent in BASH (a command-line shell language) and later rewritten in Objective-C (a programming language used on Apple platforms). The full exploit is on the researcher's GitHub, linked in the original as https://github.com/gergelykalman/CVE-2023-32422-a-macOS-TCC-bypass-in-sqlite.
Finding it
While analysing FDA programs, the researcher saw many odd environment variables. SQLITE_SQLLOG_DIR stood out because of its name. He was not deeply familiar with sqlite, but he knew that logging in this way can be dangerous. A common library like libsqlite is used by many programs, including privileged ones, and because the destination folder is chosen by the attacker, it could lead to privilege escalation.
Looking at the documented behaviour confirmed the concern: the variable turns on debug output that includes a full copy of any opened database, a list of executed SQL commands, and an index file. His reaction in the report is that this obviously dangerous debug feature somehow got compiled in and shipped.
Exploitation
The attack had two stages. The first was an information leak, which is what the initial report to Apple described. The second was a full TCC bypass, which needed a trick the researcher calls "sqlite smuggling".
Step 1: information leak via symlinks
Because the feature is available to any user, an attacker can leak any sqlite database. The technique is to create symlinks pointing at the databases of interest. A privileged program such as Music, which has FDA, can then be used to leak any sensitive sqlite database, because it can read files the attacker cannot. The report notes that any other FDA program could also be used, as long as it uses libsqlite.
The researcher calls this a severe information leak, but limited: it can only leak sqlite databases, of which there are many, but it is not as bad as it can get. He wanted to turn it into a TCC bypass.
Step 2: recognising the write primitives
A "primitive" in security means a basic capability that can be combined with others. The researcher noticed that the file writes were done with no hardening at all, which makes sense for code that was never meant to run on a production system. When writing the files, the open() system calls:
- had predictable filenames
- would follow symlinks
- would overwrite files
open() is the operating system call a program uses to open a file. "Predictable filenames" means the names are easy to guess, such as sqllog_1.db. "Follow symlinks" means if the name is a shortcut, the program writes to the shortcut's target. "Overwrite files" means existing content is replaced.
Putting these together, the writes can be used trivially to overwrite the user's TCC.db and gain complete access to all the private, TCC-protected data: databases, contacts, location, microphone, webcam, and whatever else TCC is meant to protect.
There was one obstacle: the content written was only partially controlled, and not enough to overwrite the entire TCC.db with arbitrary content. Because of this, the initial report to Apple described the information leak, while the researcher tried to work out how to elevate it to a full TCC bypass. At the time this seemed like a remote possibility.
Step 3: the content problem
After the report the researcher moved on to other bugs and came back to this one only occasionally, always failing to make it work. He could control bytes going into the files, but that was useless without a good target.
The only file he could presumably overwrite was the user's TCC.db, but because it is an sqlite file, a partial or garbage write would result in a crash or a corrupted database. This is unlike /etc/sudoers, which the report notes is happy to parse any kind of garbage. /etc/sudoers is a configuration file that controls who can run commands as the superuser; it is more forgiving of odd content.
He therefore had two options:
- find a privileged target file that tolerated garbage bytes, or
- control the entire content being written, and target the user's
TCC.db
For option 1, the researcher states he has no solution to this day, and invites readers who do to send him a DM, since it would turn a lot of his findings exploitable.
For option 2, the situation looked bad. There was no way to get away with partially writing the sqlite database, and no way to control the write from the first byte. At best he could overwrite sqlite databases entirely, or use the query log, perhaps with some trickery, to do a partially controlled write.
Step 4: making the source database attacker-controlled
It took time for the key insight to click: he could make the original sqlite files come from himself. Since he had access to Music's folders, nothing stopped him from overwriting Cache.db (for example), and then using the copy of that to overwrite the user's TCC.db. This way he could put whatever tables and rows he wanted into the resulting database, which ought to be enough. A table is a structured section of a database, like a sheet in a spreadsheet; a row is one entry in that table.
He built a demo exploit, but it kept failing. Music was unhappy about not finding the tables it normally expects. Even though the user's TCC.db had been overwritten with arbitrary content, Music would reset the entire database when it realised that tables were missing.
Killing Music after the copy is made was one option, but the resulting exploit would be unreliable. Instead, he decided to "smuggle" a valid TCC.db into Cache.db. This works because a sqlite database can contain lots of tables, and it is unlikely that a given program would check all tables in the database or for table names to collide.
Because the table names were different, he wrote a quick and dirty proof of concept (PoC, a small demonstration that the bug is real), and to his surprise it worked perfectly. TCC.db was overwritten, carrying with it the list of tables that a valid Cache.db would have. At that point he had full control over the contents of TCC.db, at least over the tables that matter.
Step 5: the exploit sequence
With the smuggling trick in hand, the exploit is built as follows:
- dump a valid
TCC.dbtotcc.sql - dump a valid
Cache.dbtocache.sql - merge the two sql files and use the result to create
payload.db - use
payload.dbto overwriteMusic'sCache.db- any other app and database would also work, as long as they do not touch the extra tables that were smuggled in
- trigger the bug with a directory location the attacker controls
- stop the program
- see what files get generated
- symlink the file that contains the database copy to
TCC.db- if
SQLITE_SQLLOG_REUSE_FILES=1is used, the filenames can be relied upon to stay the same
- if
- trigger the bug again to overwrite the target file
A "dump" here means exporting the database's contents as SQL commands, which are text instructions that can recreate the database. Merging the two dumps combines the tables from both. SQLITE_SQLLOG_REUSE_FILES=1 is another environment variable that tells sqlite to reuse the same filenames on each run, so the attacker can predict which name to symlink.
The report does not include the individual commands for these steps; it points to the demo video and the GitHub repository instead.
Demo
The report embeds a demo video:
The video is linked in the original as [Demo video](https://www.youtube.com/watch?v=rfGcd0YrbTM). The report does not describe the video's contents beyond presenting it as the demo of the exploit.
Root cause
The root cause is that a debugging feature of libsqlite, turned on by the SQLITE_SQLLOG_DIR environment variable and compiled in only when SQLITE_ENABLE_SQLLOG is defined, was present in production builds. The feature writes copies of opened databases, query logs and an index file to a directory chosen by the environment variable. The open() calls used for these writes had predictable filenames, followed symlinks and overwrote existing files, with no hardening.
Existing defences did not stop it. The writes happen inside a privileged, FDA program, so that program's own access rights apply to the destination. Symlinks are followed, so an attacker can redirect a write to a file the attacker cannot write directly, such as the user's TCC.db. The predictable filenames make the target of the symlink reliable, and with SQLITE_SQLLOG_REUSE_FILES=1 the filenames can be relied upon to stay the same. The report does not discuss any sandbox or TCC check that would have blocked the write; it only notes that the researcher confirmed app sandboxing was enabled when Apple asked.
The researcher describes the bug as fairly trivial and a clear oversight on Apple's part: the feature should never have been compiled in on a production build, and it came as a huge surprise to him that it existed, and that nobody had discovered it much earlier.
Impact
Under the conditions above, an attacker can:
- leak any sqlite database readable by an FDA program that uses
libsqlite, by symlinking the target database and triggering the debug output - overwrite the user's
TCC.dbwith attacker-controlled content, using the smuggling technique, and thereby gain complete access to all the private, TCC-protected data: databases, contacts, location, microphone, webcam, and whatever else TCC is meant to protect
The report hedges the scope in a few places. The information leak is limited to sqlite databases. The overwrite depends on the attacker being able to write to the target program's folders (for example, Music's Cache.db) and on the program not touching the smuggled extra tables. The researcher initially thought the bug could affect iOS as well, but concluded that it could not: the environment variable cannot be set on iOS, similar to his other bug. Apple's position, quoted in the timeline, was that the issue is only exploitable on macOS.
Apple awarded $30,500 for the bug. The researcher calls this fair for a macOS TCC bypass, and asked why it was only 1/3 of the advertised maximum, which Apple explained by the macOS-only exploitability.
Fix
To fix the bug, Apple simply removed this functionality. The researcher spotted a fix in 13.4beta3, which he reported to Apple, noting that it looked solid although he had only glanced at it. The fix went out 180 days after the initial report.
Timeline and process notes
The report includes a detailed timeline, which is useful for anyone running a similar disclosure:
- 2022-10-28: report sent to Apple, including the information leak exploit in BASH. At this time it was not clear whether this was more than an information leak, but the researcher knew it could also do a partially controlled file write, which he also put in the report.
- 2022-11-12: the same exploit sent again, rewritten in Objective-C.
- 2022-11-14: Apple is investigating.
- 2022-11-16: Apple asks whether app sandboxing was enabled, as they did not see it. The researcher confirms it was.
- 2022-12-07: the researcher reports that his exploit is breaking now, asks whether Apple managed to reproduce it with sandboxing on at all, and whether the sqlite exploit and the sandbox escape should be separate tickets.
- 2022-12-08: Apple confirms they did not reproduce it, but are addressing the issue anyway. They say the sandbox escape should be submitted separately if it is not related to sqlite.
- 2022-12-09: the researcher asks whether this would still count as a full chain and whether he should submit separately.
- 2022-12-10: Apple says unique issues should be reported separately, says nothing about what happens if a chain is sent in that way, and reiterates that each will be paid according to the ASB's rules. ASB is the Apple Security Bounty programme.
- 2022-12-15: the researcher submits the
TCC.dboverwrite exploit for the first time, and submits the sandbox escape issue separately in another ticket. Apple says they are reviewing it. - 2023-01-18: the researcher requests an update. Apple thanks him for his patience and says this will be fixed in a future release, after which it will be evaluated for bounty.
- 2023-03-06: the researcher checks the ticket and finds it silently closed. He tells Apple, and messages again saying the exploit still works on
13.3beta2. - 2023-03-07: Apple apologises about the ticket status issue, confirms the fix, asks for more time, and says the report only qualifies for the bounty after it is fixed.
- 2023-03-28: the researcher asks for an update as he saw no fix in the latest release, and tells Apple this will be public at the time of the OBTSv6 conference, in about 6 months.
- 2023-03-29: Apple says a new fix will be issued in the following months and confirms the researcher will be credited.
- 2023-04-26: the researcher spots a fix in
13.4beta3and lets Apple know; the fix looks solid but he only glanced at it. Apple thanks him and says they will reach out if there is an update. - 2023-05-15: the researcher reports that the CVE text is incorrect: it says the bug is an information leak, possibly because of a bad report title and because the
TCC.dboverwrite was not sent initially. - 2023-05-18: Apple says the text is correct and will not be changed. The researcher asks Apple to confirm, in which case he would publish the information, and resends the earlier exploit as it seemingly fell through the cracks.
- 2023-05-19: Apple says they are retesting and thanks him for following up.
- 2023-05-22: the ticket moves to "resolved" status; the researcher asks why.
- 2023-05-23: Apple confirms they could reproduce the TCC bypass, will update the CVE text, and will adjudicate the bug. The researcher apologises for the confusion, as the title did not say TCC bypass, which did not occur to him when he originally filled out the report.
- 2023-06-02: Apple says the CVE text got updated, with another comment to come after adjudication. The researcher says this is great news but that it became a collision with Wojciech Reguła, and asks why and when the bug will be adjudicated.
- 2023-06-08: Apple says the bug is awarded
$30,500. The researcher asks why it is only one third of the advertised maximum. Apple says the issue is only exploitable on macOS, but welcomes him to prove them wrong. - 2023-06-09: the researcher says he will look into it; Apple says he can take his time.
- 2023-06-11: the researcher reports that he got nothing substantial, as the environment variable cannot be set on iOS, like with his other bug. He accepts the bounty.
- 2023-06-12: Apple thanks him for giving it a shot, and payment details are discussed.
On the collision: months into the process, Apple deemed the bug a collision with another researcher, Wojciech Reguła. A collision means two researchers reported the same bug. The researcher had to find this out from the Apple Security Releases, as Apple had not told him. He reached out to Wojciech, whom he describes as a great researcher and a super nice guy, and they discussed timelines; it seemed the researcher was a few months earlier. After adjudication Apple confirmed that he submitted first. Adjudication is the process of deciding who gets credit and payment.
On communication, the researcher separates response time from communication quality. The fix took 180 days since the initial report, which he calls not good enough, and he feels partially responsible because the initial report was filed as an information leak. He still thinks Apple should not have taken this long, even for "just" an information leak. Communication, by contrast, was great: the person on the other end answered his questions and held his hand through the process, which softened an otherwise frustrating experience.
His lessons from the process:
- Always submit unique issues separately. Each qualifying bug is paid for, and squeezing multiple bugs into one ticket only introduces confusion: one bug, one impact, one PoC, one ticket. He acknowledges this is not always possible, since a higher impact may only become clear after later research, and that reporting early avoids collisions, at the cost of a potentially longer fix time if a higher impact is found later.
- Always submit an exploit with maximum impact if possible. A reliable, high-impact PoC is a good way to expedite the process, because a triager who can reproduce easily will have a short timeline. A triager is the person at the vendor who reviews reports. This is not a guarantee, since a bug in a particularly nasty place to fix can still take ages.
He also suggests that Apple set up a separate "ASB Support" communication channel for questions about the programme, so that triagers' time is not taken up by programme questions rather than the bug itself.
Variants to test
These are concrete directions suggested by the report's reasoning, for testing on other targets. The report does not claim any of them were tested.
- Other FDA programs that use
libsqlite. The report states that any such program could be used, not justMusic. A positive result is that settingSQLITE_SQLLOG_DIRto an attacker-controlled directory causes the program to writesqllog_N.db,sqllog_N.sqlandsqllog.idxfiles there. - Other programs whose folders the attacker can write to, used as the source of the smuggled database. The report says any other app and database would work, as long as the app does not touch the extra tables. A positive result is that the program keeps running normally after its database is replaced with one containing the smuggled tables.
- Other privileged target files that tolerate garbage bytes, as an alternative to
TCC.db. The researcher explicitly says he has no solution for this and asks readers to contact him if they find one. A positive result would be a privileged file that parses successfully after a partially controlled write. - Other debug or logging environment variables in privileged libraries that write to attacker-chosen paths. A positive result is a write to a path the attacker cannot write directly, performed by a process with higher privileges.
- Using
SQLITE_SQLLOG_REUSE_FILES=1to stabilise filenames across runs. The report mentions this as the way to rely on the generated filenames staying the same. A positive result is that the samesqllog_N.dbname is reused on a second trigger, so a symlink planted at that name is followed again. - Checking whether the same environment variable can be set on other platforms. The researcher tried iOS and found it was not possible, similar to his other bug. A positive result on another platform would be that the variable can be set for a privileged process and the debug files appear.
Glossary
- ASB: Apple Security Bounty, Apple's programme that pays researchers for reporting security bugs.
- BASH: a command-line shell language used on Unix-like systems, including macOS.
- Bounty: a reward, usually money, paid for finding and reporting a security bug.
- CVE: Common Vulnerabilities and Exposures, a public catalogue that gives each security bug a unique identifier, such as CVE-2023-32422.
- Collision: when two or more researchers report the same bug, so credit and payment must be shared or decided.
- Compile: to turn source code into a runnable program; compiler switches can include or exclude features.
- Database: an organised collection of data, like a very advanced spreadsheet.
- Debugging feature: code included to help developers find problems, not meant for normal users.
- Dump: to export a database's contents as text commands that can recreate it.
- Entitlement: a special permission granted to a program by the operating system, such as Full Disk Access.
- Environment variable: a named setting a program reads when it starts, often used to configure behaviour.
- Exploit: code or steps that take advantage of a bug to achieve an effect.
- FDA (Full Disk Access): a macOS permission that lets a program read files normally protected from it.
- Information leak (infoleak): a bug that lets an attacker read data they should not be able to read.
- libsqlite: the shared library version of sqlite used by many programs on macOS.
- Local privilege escalation: gaining the rights of a more powerful user or program on the same machine.
- macOS: Apple's operating system for Mac computers.
- Objective-C: a programming language used on Apple platforms.
- open(): the operating system call a program uses to open a file.
- PoC (proof of concept): a small demonstration that a bug is real and exploitable.
- Primitive: a basic capability, such as writing to a file, that can be combined with others.
- Sandbox: a security mechanism that limits what a program can do.
- Sandbox escape: a bug that lets a program break out of its sandbox.
- SQL: Structured Query Language, the language used to talk to databases.
- sqlite: a small, widely used database engine.
- Symlink (symbolic link): a shortcut file that points to another file; opening it opens the target.
- Table: a structured section of a database, like a sheet in a spreadsheet.
- TCC: Transparency, Consent and Control, the macOS privacy system that asks and remembers whether apps may access private data.
- TCC bypass: a bug that gets around TCC's protections.
- TCC.db: the database file where TCC stores its decisions.
- Triager: the person at a vendor who reviews security reports and decides what happens next.
- Vulnerability: a weakness in software that can be exploited.