by goranmoomin · 3y ago · 0 comments
As a result, this means that… if you create a Korean-named file yourself in Finder: then the file name is normalized in Unicode NFD (decomposed) form, but if you create a Korean-named file in a zsh shell, the file name is normalized in Unicode NFC (composed) form.
You don’t realize the difference until you try an `ls | xxd`, but the bytes are different, even though they look the same.
It’s an invisible mess that people don’t realize until they do. And a lot of programs gets subtly crazy when fopen(A/B) succeeds but listdir(A) doesn’t list B.
Basically I had a Git repository and didn’t know about `git config core.precomposeUnicode`. And when the repository is synchronized to a Linux system, on the Linux side it can sometimes have 2 files with the same-looking file name but different normalizations. (Because I think ext4 doesn’t normalize Unicode?) That took me about an afternoon to fix.
Yeah, native Linux filesystems don't really know anything about unicode at all at their heart. They really work more on raw bytes, simply reserving 0x00 (NUL) and 0x2F ('/'). Anything else goes in a filename as far as the kernel is concerned (including evil stuff like incomplete multibyte sequences, or other invalid UTF-8 byte sequences). User space is welcome to and encouraged to treat filenames as UTF-8 on modern systems, but that's not really enforced anywhere strongly.
https://en.wikipedia.org/wiki/Code_page
What I needed to eventually do is to go on Windows, install WSL there, then run some script that recursively renamed them from the first to the other (on the FAT volume), and only then I could copy them from FAT to APFS, on the Mac.
(I could probably skip the WSL and do it with some Windows-only tooling, but that would take me even longer.)
so this little exercise involved mac, windows, linux (and plan 9, if you count the wsl filesystem shenanigans)
Well, it turns out that Unicode can represent a lot of characters by way of different possible encodings (for example é could be a single codepoint, or the codepoint of e followed by the codepoint of an acute accent).
Be faithful and preserve what was entered rather than trying to "second-guess" the user.
UNICODE details like normalization can be handled up in the application layer.
Historically, you configured your OS to match (what you thought was) the encoding used on your disk, but that starts to break when you have network disks or when users exchange floppies.
In practice, I think about every Unix/Linux file system at least assumes ASCII encoding for the printable ASCII range. How else does, for example, your shell find “ls” on your disk when you type those characters?
So, not even in Unix/Linux is a file name “a bag of bytes”
I think file systems should enforce an encoding, and provide a way for users of the file system to find out what it is.
Apple’s HFS, for example, stored explicit ‘code page’ ID so that the Finder knew, for example, to use the Apple Cyrillic encoding to interpret the bytes describing file names on disk for that disk.
The modern way is to just say file names are valid Unicode strings, with UTF-8 being the popular choice. Unfortunately that introduces the normalization problem.
Czech language has Á, Č, Ď, Ě, É, Í, Ň, Ó, Ř, Š, Ť, Ů, Ú, Ý
Á can be represented in Unicode by its spesific codepoint, or can be made up of two - Letter A and the squiggly bit.
So when a user tries to fOpen a file called ÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁÁ
Should by application make 2^32 requests to the file systen to open a file for every possible combination of bytes this steing could be represented as?
Command line argument is treated as (unicode) string, and file name are (sometimes) treated as bag of byte .....
What is the threat model that makes having both versions exploitable? Historically, many vulnerabilities (e.g. several git vulns) have come from filesystems doing normalization rather than the lack of it.
Buy generally anything that leads to unexpected behaviour can be exploited in some way, if not technical then to mislead the user.
The question is, why is 'failing that' happening here? And if there's 2 files encoded differently with the same name, how would the user differentiate them?