How normalisation and case folding in APFS led to vulnerabilities

One of the most unfortunate features of Unicode is that some identical characters have more than one encoding. These include common accented characters such as é and é, as well as some that are used extensively in non-English languages like Å, and many in some non-Roman languages from South-East Asia.

As a result, some everyday features including search and string comparison need to take those into account, and names used in file systems also need to deal with them consistently. In HFS+ the solution is to perform normalisation, so only one of those encodings is used by the file system. When Apple first released APFS for iOS, it didn’t normalise, instead just storing names in the way that they came as a ‘bag of bytes’, and leaving it to developers to ensure they used the correct operating system calls to avoid adverse consequences.

That nearly proved disastrous, and even Apple’s apps were caught out with severe problems. By the release of APFS in High Sierra, Apple had been forced to change, and APFS had been given a normalisation layer to rescue it from chaos. This seems to have been so embarrassing that, while it’s addressed in detail in early documentation, there’s no mention of this in its current equivalent or the APFS Reference.

While normalisation problems can result in frustration and data loss, because they mainly involve accented characters, they should seldom have significant impact on security.

Another peculiarity of APFS is that it can be sensitive or insensitive to case, with Macs preferring the latter, except for Time Machine backup storage which is required to be case-sensitive. As some apps can’t cope with case sensitivity, most APFS volumes in Macs are case-insensitive and don’t allow, for example, the coexistence of two directories or files with names that differ only in case, such as myFile.txt and myfile.txt.

Case-insensitivity relies on case fold mapping to determine which characters are considered to differ only in their case. You might be surprised to learn that includes ss, SS and combinations, ß and its capital form ẞ, which are all defined as being different cases of the same character.

To see case fold mapping at work, in a regular case-insensitive APFS volume try creating two folders side by side with the names strasse and straße. Case-insensitive APFS will refuse the second of those, but repeat that in a case-sensitive APFS volume, where you’ll find they can happily coexist just as this and This can.

In a case-insensitive APFS volume, the names müßig, müßig, müssig, müssig and their variants using capitals are treated as identical despite containing two variants of the ü (c3 bc or 75 cc 88), and two variants of the ß/ss (c3 9f or 73 73), etc. Thus, case-insensitive APFS has to perform both normalisation and case fold mapping of names before hashing them.

Although ss and ß (and variants) are the best-known examples of case folding, there are similar effects with ligatures, where fl (ef ac 82) is mapped to fl (66 6c), for example, and there are more obscure instances, some of which are language-dependent.

Last week the inevitable happened when this behaviour was reported to be involved in multiple security vulnerabilities, although not in APFS or macOS as such. As has been known all along, and has been demonstrated in APFS over the last nine years, software that performs simple character-by-character string comparisons fails to anticipate how APFS handles that string.

A vulnerable installer might check the files it’s going to install against a list of protected paths and files. For example, it might need to preserve a vital file named noAccess.config. Suppose a malicious package contained a file to be installed as noAcceß.config. Using simple string matching, that wouldn’t be flagged as replacing noAccess.config as their names differ, even in their lengths. But if noAcceß.config is copied to a case-insensitive APFS volume, it will replace noAccess.config and could then be exploited.

This is another demonstration of how complexity in normalisation and case folding leads to coding errors.

I’m very grateful to Jim for pointing out this vulnerability report.