400 points · by arunc · 4y ago · 174 comments
Seems like there is a lot behind this summary of the hack.
How does an image file trick the iPhone into all that?
https://googleprojectzero.blogspot.com/2021/12/a-deep-dive-i...
edit, previous discussion:
1. iMessage has a feature to send and receive GIFs
2. These GIFs are copied to a specific path early in the message processing pipeline (even before the message is displayed)
3. But the copy code doesn't just copy the GIF. It uses the CoreGraphics APIs _renders_ the image to a new GIF file at the destination path.
4. The code uses the ImageIO lib to guess the image format, ignoring the .gif file extension. So you can trick this code to accept a non-GIF file.
5. You can use the above to invoke one of over 20 image codecs that were not intended to be invoked in this code, including the CoreGraphics PDF parser.
6. CoreGraphics PDF parser has a very specific vulnerability in its JBIG2 image codec.
7. JBIG2 takes an image of text, identifies repeating glyphs and uses that fact for better compression. To avoid confusing slightly differing glyphs in things like images of poor quality prints (think e and é, or 3 and 8), it has a way of applying a diff over each instance of an identified repeating glyph.
8. This logic has an integer overflow bug: the 'number of symbols' variable is a 32-bit integer, which can be overflowed using a carefully crafted file. Now the attacker can can set the buffer for symbols to a much smaller value.
9. Making a long story short, this allows overwriting heap memory, setting arbitrary values in the objects used in the JBIG2 logic.
10. The JBIG2 logic uses AND, OR, XOR and XNOR operations when iterating through these objects (to apply the 'diff' on glyphs). The attacker can craft a file that strings together these logic operations so that it basically forms a software logic circuit.
11. So this exploit basically emulates a computer architecture inside an image codec, which can be used to operate on arbitrary memory!
Is that right? If so, this is mind-blowing.
Let's go to the technical part: If you are asked to implement the binary algorithm with your favorite programming language, how do you verify your code? Unit-tests. How many test cases you will need? More than 10. Binary search implementations are easy to suffer integer overflow bugs(remember the one in JDK?), as long as you have enough tests, your don't need to worry too much. But how much is enough? People can't implement binary search correctly in decades is not because we don't know the algorithm enough or we don't have excellent software engineers, it is because w don't know how to test our code thoroughly. Any non-trivial C/C++ function may need tens of thousands test cases. Simply you can't write them by hand.
You need the right tools: fuzzing and static analysis.
At Microsoft, every file parser should go through fuzzing, which basically is you generate some random input, then you run your tests with the random inputs. Not very fantastic. But there is another kind of fuzzing: symbolic execution, which tries to find all the possible execution paths of your code. If you run symbolic execution with your binary search code, you can get 100% test coverage. And it is guaranteed bug-free. It is like a math proof. Please note the advantage is based on human just had surprising great advancement on SAT solvers in the last 20 years. And often you need to make some compromises between your business goal and security. Most functions can't reach 100% test coverage. You need to simplify them. See https://github.com/klee/klee to get a quickstart. Though C/C++ is often considered unsafe, they have the best fuzzer.
Then it is about SAL annotation and static analyzer. In C, whenever you pass a pointer of an array to another function, you should also pass its length with it. And in the callee function you should check the length. If you forgot it, your static code analyzer will give you a warning. In such a sense, if you didn't allocate enough memory, it will only result an error code being returned instead of undefined behavior.
The last thing: Use safeint wrapping your malloc function. https://docs.microsoft.com/en-us/cpp/safeint/safeint-library...
When we move off the binary search toy example to a real code base, clearly you can see how much extra effort is needed to make the code safe. Please pardon me, most OSS libraries don't have the resource. Many famous OSS projects are "Mom-and-pop" shops. They don't have any compliance rule. They invest very little on fuzzing. So the big companies really should help them. Now you see an integer overflow bug was found in Apple's image render, but was the code written by Apple? Not necessarily. Now we all see the importance of the Open Source movement. It's time to think how to harden their security. For example, even I want to spend my free time on adding SAL annotations to an OSS project I love, would the maintainers accept it?
Now, we've gotten a bit smarter about this sort of thing since. Both at a high level architecturally (don't process image files in the kernel) and at a lower level (use a language that takes measures to constrain its buffers). But the basic scenario hasn't been entirely eliminated. There could be a parser bug somewhere in your web browser for example that allows a properly crafted input to hijack the browser process.
0: https://news.ycombinator.com/item?id=30322715
1: https://www.seattletimes.com/business/rights-group-verifies-...
^2: For instance, the Coinbase Super bowl ad that was only a bouncing QR code would have been a very interesting way to start WW3 if it were Russia hacking millions upon millions of americans' phones, exfiltrating any potentially sensitive information (company emails, etc) in an instant, and/or destroying the device via some exploit chain that destroys the OS and requires a full firmware factory reset to recover.
https://en.wikipedia.org/wiki/JailbreakMe#JailbreakMe_1.0_(i...
Rich multimedia parsing display systems in messaging apps are a very tempting attack surface for entities such as NSO.
One is fly-by attacks by random viruses and ransomware. For those cases, I would not worry about pictures.
Other is when you are targeted by regimes with essentially unlimited budget. In that case yes, the picture can be a spyware.
https://en.wikipedia.org/wiki/Goodtimes_virus
I remember telling lots of people at the time that this was impossible, because images weren't executable code, and viruses spread through running programs, not through viewing images.
Unfortunately, this elegant, straightforward distinction didn't hold up over time. :-(
I'm amazed at the tech giants with all their funding and they still cant build secure operating systems or have the resources to reduce attack vectors within their own OS'es.
I wonder if she had a preexisting relationship w the group or how she was connected with them.
Presumably her work included a network of journalist protection orgs. But had she had a device “probed” like this before?
It’s such an important point of contact I wonder about the details of this.
https://en.wikipedia.org/wiki/Loujain_al-Hathloul
Her husband is another exceptionally courageous person and it is inspiring to see their cheer and steadfast courage despite all the hardship they face. It is sad to see Saudi Arabia continuing to harass this couple.
I haven’t researched the exploits in detail but it seems to me Apple can develop better protection against such zero-click exploits.
At the very least, iMessages shouldn’t preview images from unknown contacts.
No, only real security helps, some dude reviewing GUI or business related rules help with nothing, and Apple security automated checks are week, stronger sandboxes are needed in wall gardens or walled gardens with a gate.
A walled garden could make it simpler to enforce that only software proved correct can be installed, but without the proof, it does not guarantee much,,only makes certain things less probable.
Writing provably correct software us now a rare and expensive engineering feat. Most consumer OSes have nothing of thus sort, sadly. And I mean just the limited set of system software, not third-party installable apps, of course.
There are more cost efficient and proven ways to effectively address these kinds of vulnerabilities, like limiting complexity, using programming language features to eliminate classes of bugs, using other verifiable engineering disciplines, and compartmentalisation. Proofs also have their place locally in verifying small components. But making this commercially viable would large numbers of customers and users who are willing to make big compromises in functionality, features, third party apps etc.
Things like image format decoders are easiest to produce formal analysis and proofs for. It's sadly still too expensive and slow to produce.
Fuzzing as a mandatory testing step could be useful in some cases.
I agree about bugs in specifications though :(
Also, I wonder if Google Pixel is more secure?
Incase anyone was wondering, yes the amount they spent on that particular feature vastly eclipsed their spend on the kinds of teams that could have caught this.
“security researchers find a blank Animoji file that ended up being a security exploit wrapped around an OS written with image files”
Apple’s security teams aren’t public facing; I suspect if a corporate or university customer had security issues, they would be addressed.
It really doesn't matter how many people they behead or cut to pieces in their embassies, if you are not US citizen you mean little to nothing and its explicitly stated in US laws. I guess common americans have no idea how much this pisses large parts of the world.
https://www.google.com/search?client=firefox-b-1-d&q=loujain...
https://en.wikipedia.org/wiki/Loujain_al-Hathloul
Let's not forget that the present Saudi regime (MBS) is responsible for luring political opponents/journalists, killing them, and dismembering them with bone saws.
https://en.wikipedia.org/wiki/Assassination_of_Jamal_Khashog...
It's still a very oppressive country where being a dissident means getting threatening phone calls from the security agencies.
Remember 2011 when there were popular protests all around the Arab world? Well they tried to do one in Saudi as well but the intimidation was so high, that it never got off the ground.
I get it, legacy crap has momentum and you can’t ignore that. What’s not ok is the mountain of people who pretend that’s not a problem.
The industry will only really change when pushed to do so.
Question here is: can they fix potholes faster than new ones show up?
Seems answer is no for tech. And construction, these days.
The move to a large majority of software being run in a sandboxed environment has drastically reduced this sort of thing.
Eventually this will be standard in software as well.
Remember Diginotar?
Who knows how many lives were affected in Iran...
That has never happened to software developers.