Remember when we were talking about subtree_check and only vaguely citing the man page? If subtree_check is not enabled and the export is not the root of a file system, it turns out that it is possible to read and write other files outside the export on the same file system. There are some preconditions for this to work, but in practice they are met quite often: The server needs to run Linux (what we see most of the time), subtree_check needs to be disabled (default on Linux), the root directory of the file system must have a predictable inode number (this is the case for many common file systems) and the export must not be the root of the file system (depends on your target). The last requirement is that the export must be accessible to the attacker, which means it must be possible to successfully mount it.
We tested and implemented the attack against ext4, xfs, btrfs in our tools. For btrfs, if subtree_check is disabled, it is possible to access data on all subvolumes and snapshots even if they are not mounted. While the attack technically also works on ZFS, only data on the same dataset as the exported directory can be accessed. When dealing with ZFS, even more nuances in server configuration are relevant. We did an internal PoC that shows that it is technically possible under the right circumstances, but due to the need for brute forcing handles and secure default configurations of appliances we looked at, we did not implement this attack as we considered the relevance in practice below the bar.
The attack works the same way on NFSv3 and NFSv4, but our tools only support NFSv3 because there are some challenges when trying to automate it on NFSv4. Therefore, this is left as an exercise to the reader.
Back to topic: To showcase the impact of this misconfiguration, let’s assume a fully patched Debian, freshly installed with all the defaults unchanged, using ext4 as the operating sytem partition. Now, we enable NFS and export /srv/ so users can store data there. If for example /var/ is on the same file system (which it is unless you explicitly decided to create a separate file system for the NFS export), then it is possible to browse logs in /var/log/, upload a web shell in /var/www/, you get the idea.
Remember we mentioned earlier that files owned by root (uid 0) are safe by default against this attack? Well, yes, but only if the gid is also 0! Well, let’s see what… oh, is that /etc/shadow belonging to the user root but the group shadow? Unfortunately, this group has gid 42 on Debian, and as that’s not 0, it is not affected by root_squash!
-rw-r----- 1 root shadow 1421 Feb 19 17:48 /etc/shadow
(Un)fortunately, the file is not writable for that group, so we can’t assign new passwords to users, but we could at least try to crack existing ones.
This attack also works on SuSE Linux. RHEL/Fedora are not affected as /etc/shadow is owned by uid and gid 0. When accessing the file using fuse_nfs it will automatically assign the correct gid to the requests and the file can be read. Our other tool nfs_analyze automatically tries to print the contents of /etc/shadow if it successfully escapes an export.
We also found that it might be possible in some circumstances that if no_root_squash is set and you’re somehow able to trigger a restart of the NFS server, you may gain access to other file systems on the NFS server, too. As this is quite a disruptive attack and requires the ability to trigger a server restart, we won’t go into detail on this.