Stealing all your secrets using IPFS Mounts
The IPFS FUSE mount allows symlinks to point outside the mount directory, so an attacker can create a node with a symlink to a sensitive file and read it through the mount. This can leak secrets such as SSL private keys when a service exposes IPFS content.
Published on joranhonig.nl
While researching IPFS, I discovered that its mounting feature assumes safe user input, which opens up an excellent privilege escalation vector wherever IPFS mounts are used.
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
IPFS (the InterPlanetary File System) lets users distribute nodes representing files, directories and symlinks across its network. Go-ipfs, the reference implementation, includes a mounting feature that exposes IPFS and IPNS on the local file system through FUSE (Filesystem in Userspace), using the Go library brazil. Once mounted, a user can read the content of an IPFS node by accessing it at /<mountpoint>/<node_hash>. The feature is genuinely useful: it lets you print a file stored on IPFS directly, and it lets existing software, such as a reverse proxy, serve IPFS storage without any custom code.
The problem is that the path name parameters for IPFS nodes are never sanitised, and brazil does not provide secure by default settings: it assumes it is given safe input by the code calling into it. That assumption does not hold for IPFS, for two reasons:
- Any IPFS file node can be queried through the ipfs fuse mount.
- An attacker can create an IPFS file node with a symlink pointing to any point in the filesystem.
So if you cat an IPFS node that is a symlink to /etc/passwd through the mount, you end up catting the contents of /etc/passwd.
The researcher disclosed the attack vector to the IPFS Security team on 4 February 2021. The team had already been aware of the risks symlinks pose for fuse mounts since 2018 (issue ipfs/go-ipfs#5161), and at the time of writing was not planning to release a mitigation.
Setup
The proof of concept runs in a Vagrant VM based on ubuntu/groovy64, with go-ipfs v0.7.0 installed from the official distribution. The mount points /ipfs and /ipns are created and owned by the vagrant user.
Finding it
While researching IPFS, the researcher noticed that the mounting feature assumes safe user input. Because node path names are never sanitised, a node can carry a malicious payload, and because brazil does not jail symlinks by default, a symlink node can point outside the mount directory. That combination turns the mount into a privilege escalation vector wherever IPFS mounts are used.
Exploitation
Step 1: prepare a VM and install go-ipfs
$ vagrant init ubuntu/groovy64
$ vagrant up
$ vagrant ssh
$ wget https://dist.ipfs.io/go-ipfs/v0.7.0/go-ipfs_v0.7.0_linux-amd64.tar.gz
$ tar -xvzf go-ipfs_v0.7.0_linux-amd64.tar.gz
$ cd go-ipfs
$ sudo bash install.sh
This initialises a Vagrant VM from the ubuntu/groovy64 box, downloads the go-ipfs v0.7.0 linux-amd64 tarball, extracts it and runs install.sh.
Step 2: create the mount points
$ sudo mkdir /ipfs /ipns
$ sudo chown vagrant: /ipfs /ipns
The two directories that go-ipfs will mount onto are created and handed to the vagrant user.
Step 3: build a node containing a symlink that escapes the directory
$ echo "secret" > secret_file
$ mkdir poc
$ ln -s /home/vagrant/secret_file ./poc/not_so_secret
A file called secret_file holds the string secret. Inside the poc directory, a symlink named not_so_secret points at the absolute path /home/vagrant/secret_file, which is outside the directory that will be added to IPFS.
Step 4: start the daemon with the mount and publish the node
$ ipfs init
$ ipfs daemon --mount &
$ ipfs add -r ./poc
$ ipfs pin add \<hash of poc node\>
ipfs init initialises the repository, ipfs daemon --mount starts the daemon with the FUSE mount enabled, ipfs add -r ./poc adds the directory recursively, and ipfs pin add pins the resulting node.
Step 5: read the symlink through the mount
$ cat /ipfs/\<hash_of_poc_node\>/not_so_secret
secret
When the symlink node is accessed, go-ipfs resolves the symlink target and fetches its data. Because there is no limitation on what the symlink node can point to, the content of the file outside the mount is returned: secret.
Kill chain
In isolation the bug does not help an attacker pop a shell: the user executing cat already has access to /etc/passwd anyway. It becomes interesting when other software sits on top of the mount. Reverse proxies are the most straightforward example.
Assume NGINX is running with Let's Encrypt and certbot for automatic SSL certificate maintenance, and that the site lets people access IPFS file nodes, for example:
youripfsportal.com/[redacted]/readme
An attacker can create a node with a symlink that points directly where certbot normally puts the SSL private key:
certificate -> /etc/letsencrypt/live/youripfsportal.com/privkey.pem
The attacker puts that node on IPFS and queries it through youripfsportal.com; NGINX diligently fetches the key for them.
The vulnerability is not limited to reverse proxies. It arises in virtually every scenario where you have:
- An application that provides access to IPFS to users.
- A user who can influence file requests to the ipfs mount by the application and has fewer file system permissions than the application.
The author's advice is not to use ipfs file mounts unless you are absolutely certain you do not meet these conditions.
Root cause
Go-ipfs uses FUSE for user space mounting of IPFS nodes, through the brazil library. Brazil does not provide secure by default settings and assumes it is given safe input from the code calling into it. In IPFS's case the nodes that can be queried are not sanitised: the path name parameters for nodes are never sanitised and can contain malicious payloads, and when the accessed node is a symlink node, go-ipfs tries to resolve the symlink target and fetch its data with no limitation on where it points. The weakness is classified as CWE-22 and affects go-ipfs up to and including 0.8.x.
The weakness is not unique to go-ipfs and potentially affects other projects too. Brazil does not provide an option to jail symlinks by default, whereas other FUSE implementations such as osxfuse do implement this behaviour (the jail_symlinks mount option).
Impact
An attacker can create symlinks on an IPFS mount that reference other potentially sensitive files, and leak information from nodes through go-ipfs's fuse mount capabilities. This affects any user who leverages the mount functionality to expose access to potentially unsafe IPFS nodes. The original report rates severity as Medium with the vector CVSS:3.0/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:N/A:N.
Fix
At the time of the report the IPFS team was not planning on releasing a mitigation; the issue had been known since 2018 and was reassessed as won't fix on 4 August 2021. The report proposes two directions: adding symlink jailing functionality to the brazil library (the researcher planned to approach its maintainers, and to share the report with them if acceptable), or, failing that, sanitising node data so that only symlinks pointing within the mount are accepted.
Timeline
Feb/04/2021 initial disclosure vendor
Mar/25/2021 vendor finishes triage and assigns low priority
Aug/4/2021 vendor reassesses priority as won't fix and shares the bug was previously known https://github.com/ipfs/go-ipfs/issues/5161
Aug/24/2021 full disclosure of attack vector and report
Variants to test
- Reverse proxy in front of an IPFS mount. Publish a node whose symlink points at a path the proxy process can read but the requester cannot, such as a TLS private key, then request it through the proxy.
- Any service that maps user-influenced paths onto an IPFS mount. Look for applications where the requester can choose which node or path is fetched and the application runs with broader file system permissions. A positive result is reading a file outside the mount directory.
- Other FUSE-based projects using brazil. Check whether the project sanitises node data or enables symlink jailing. A positive result is a symlink node resolving to a target outside the mount.
What happened, in plain words
IPFS is a way of storing and sharing files across many computers instead of one central server. Each file or folder you put into IPFS gets a long code (a hash) that identifies it, and anyone who knows that code can ask the network for the file. Go-ipfs is the main program people run to take part in that network, and it has a feature that lets you browse IPFS files as if they were ordinary files on your own computer. That feature is called a mount.
The researcher found that this mount feature trusts whatever it is given. If someone puts a special kind of file into IPFS, one that is really a pointer to somewhere else on the computer (a symlink), then reading that file through the mount makes go-ipfs follow the pointer and hand back whatever it points at. The pointer can aim anywhere on the machine, including files the person asking should never see, such as a web server's private encryption key.
On its own that is not very exciting: if you can already read a file on your own computer, a trick that reads it for you gains nothing. It becomes serious when other software sits on top of the mount, for example a web server that lets visitors fetch IPFS files. Then a visitor can ask for a file that the web server can read but the visitor cannot, and the web server fetches it for them.
The researcher reported this to the IPFS security team on 4 February 2021. The team already knew about the risk of symlinks in these mounts from 2018, and at the time of writing was not planning to release a fix.
Some words you will meet
Before going further, a few ideas from the report, explained from scratch.
- IPFS (the InterPlanetary File System) is a network for storing and sharing files. Instead of an address like a website name, a file is found by a fingerprint of its contents, called a hash. If the contents change, the hash changes, so the same hash always means the same file.
- A node in IPFS is one entry in that network: it can be a file, a folder, or a pointer to another place (a symlink).
- A symlink (symbolic link) is a file that is really a signpost. It holds a path, and when a program opens the symlink the operating system quietly opens the path it names instead. Think of a note on a door saying "the key is in the drawer": you asked for the note, but you end up with the key.
- A mount makes a remote or special storage look like an ordinary folder on your computer. After mounting, you can use normal commands such as
caton it. The report's mount points are/ipfsand/ipns. - FUSE (Filesystem in Userspace) is the part of the operating system that lets an ordinary program, rather than the kernel, provide a folder like this. It is what makes the IPFS mount possible without special kernel code.
- brazil is a Go library, that is a reusable piece of code written in the Go programming language, that helps programs create FUSE mounts. Go-ipfs uses it.
- Sanitising means cleaning input before using it: checking it against what is allowed and rejecting anything strange. The report says IPFS node path names are never sanitised.
- Privilege escalation means getting access to something you were not supposed to have, usually by making a more powerful program act on your behalf.
- A reverse proxy is a web server that sits in front of other services and forwards visitors' requests to them. Visitors talk to the proxy; the proxy talks to the service behind it.
- A private key is the secret half of the pair of keys used to prove a website's identity and to decrypt traffic. If it leaks, someone else can impersonate the site.
- certbot is a tool that obtains and renews free certificates from Let's Encrypt, a certificate authority, and stores the private key on disk.
- CWE-22 is a catalogue label for a class of bug called path traversal: using paths to reach files outside the area you are supposed to be confined to.
- CVSS is a scoring system for the severity of a security bug. The vector string in the report is a compact way of writing down the score's ingredients.
Mounting
Go-ipfs lets you mount IPFS and IPNS on your file system. IPNS is the naming side of IPFS, where a name can be updated to point at a new hash. Mounting lets you do neat things, such as printing the contents of a file on IPFS:
$ cat /ipfs/[redacted]/readme
Line by line: cat is the standard command that prints a file's contents to the screen. The path starts at /ipfs, the mount point, then the hash [redacted] identifies one node, and readme is a file inside it. Because the mount is in place, the operating system treats this like any other file path.
You can also use the mount to make existing software, such as a reverse proxy, work with IPFS storage without any custom code. All in all, it is a great compatibility feature: software that knows nothing about IPFS can serve IPFS content simply by reading files from a folder.
They use FUSE and brazil. Unfortunately, brazil does not provide secure by default settings and assumes that it is provided with safe input from the code calling into it. That assumption is not valid in IPFS's case for two reasons:
- Any IPFS file node can be queried through the ipfs fuse mount.
- An attacker can create an IPFS file node with a symlink pointing to any point in the filesystem.
So if you cat an IPFS node, which is a symlink to /etc/passwd, through the mount, then you will end up catting the contents of /etc/passwd. /etc/passwd is a standard file on Linux systems that lists user accounts; it is a common stand-in for "a file the attacker should not get".
Proof of Concept
The following commands allow you to see how the bug works:
$ vagrant init ubuntu/groovy64
$ vagrant up
$ vagrant ssh
$ wget https://dist.ipfs.io/go-ipfs/v0.7.0/go-ipfs_v0.7.0_linux-amd64.tar.gz
$ tar -xvzf go-ipfs_v0.7.0_linux-amd64.tar.gz
$ cd go-ipfs
$ sudo bash install.sh
$ sudo mkdir /ipfs /ipns
$ sudo chown vagrant: /ipfs /ipns
$ echo "secret" > secret_file
$ mkdir poc
$ ln -s /home/vagrant/secret_file ./poc/not_so_secret
$ ipfs init
$ ipfs daemon --mount &
$ ipfs add -r ./poc
$ ipfs pin add \<hash of poc node\>
$ cat /ipfs/\<hash_of_poc_node\>/not_so_secret
secret
This is a lot at once, so here is what each part does and why it is there.
vagrant init ubuntu/groovy64,vagrant up,vagrant ssh: Vagrant is a tool that creates a disposable virtual machine from a description. The first command writes a description for an Ubuntu Groovy machine, the second starts it, and the third opens a shell inside it.wget https://dist.ipfs.io/go-ipfs/v0.7.0/go-ipfs_v0.7.0_linux-amd64.tar.gz: downloads the official go-ipfs version 0.7.0 package for 64-bit Linux.wgetis a command-line downloader.tar -xvzf go-ipfs_v0.7.0_linux-amd64.tar.gz: unpacks that archive. The letters telltarto extract, be verbose, decompress with gzip and read from a file.cd go-ipfsandsudo bash install.sh: move into the unpacked folder and run the installer it ships with.sudoruns a command with administrator rights.sudo mkdir /ipfs /ipns: creates the two empty folders that will later be the mount points.sudo chown vagrant: /ipfs /ipns: hands ownership of those folders to thevagrantuser.echo "secret" > secret_file: writes the wordsecretinto a file. This stands in for any sensitive file on the machine.mkdir poc: creates a folder to hold the node that will be published.ln -s /home/vagrant/secret_file ./poc/not_so_secret: creates a symlink insidepoccallednot_so_secretthat points at the absolute path ofsecret_file. The name is a joke: the file is not so secret if this works.ipfs init: sets up a fresh IPFS repository for this user.ipfs daemon --mount &: starts the IPFS background service with the mount feature switched on. The&puts it in the background so the shell stays usable.ipfs add -r ./poc: adds thepocfolder to IPFS, recursing into it. The resulting node's hash is what the placeholder<hash of poc node>stands for.ipfs pin add \<hash of poc node\>: pins the node.cat /ipfs/\<hash_of_poc_node\>/not_so_secret: reads the symlink through the mount. The output issecret, the contents of the file outside the published folder. That is the bug: the mount followed a pointer that led out of the area it was supposed to serve.
Kill Chain
The example above might not seem that useful to an attacker. The user executing cat already has access to /etc/passwd after all. You would be right: in isolation, this bug will not help you pop a shell, meaning it will not by itself give an attacker command-line control of the machine.
It is when there is other software that this bug becomes interesting. Let us explore reverse proxies, as they are a very straightforward example of how this bug can be used to leak critical information.
Assume you are running NGINX, a popular web server, and that you are using Let's Encrypt with certbot for automatic SSL certificate maintenance. You have a website which lets people access IPFS file nodes. For example:
youripfsportal.com/[redacted]/readme
Now an attacker might create a node with a cleverly crafted symlink that points directly where certbot would normally put your SSL private key:
certificate -> /etc/letsencrypt/live/youripfsportal.com/privkey.pem
Now all the attacker has to do is put that node on IPFS and query it through youripfsportal.com, and NGINX will diligently get it for them. The web server has permission to read the key, the visitor does not, and the mount makes the server do the reading on the visitor's behalf.
This vulnerability does not just impact reverse proxies. It will arise in virtually every scenario where you have:
- An application that provides access to IPFS to users.
- A user who can influence file requests to the ipfs mount by the application and has fewer file system permissions than the application.
The second condition is the heart of it: the requester must be able to choose what gets read, and must have less access than the program doing the reading. Do not use ipfs file mounts unless you are absolutely certain that you are not meeting these conditions.
The original report
The researcher attached the original report for documentation purposes. It is reproduced below.
Ipfs Fuse mount allows for symlinks outside the mount directory
Affected Versions: go-ipfs ⇐ 0.8.x
Vulnerability Class: CWE-22
Severity: Medium, with the vector CVSS:3.0/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:N/A:N
That vector reads, piece by piece: the attack can come over the network (AV:N), needs no special conditions (AC:L), needs no account on the target (PR:N), needs the victim to do something such as open a link (UI:R), affects only the vulnerable component (S:U), has a high impact on confidentiality, meaning secrets can leak (C:H), and no impact on integrity or availability (I:N/A:N), meaning data is not altered and the service is not knocked offline.
Summary
Go-ipfs uses fuse for user space mounting of ipfs nodes. It uses the Brazil library for this. Brazil does not provide secure by default settings and assumes that it is provided with safe input from the code calling into it. This assumption is not valid in IPFS's case, where the nodes that can be queried are not sanitised. This allows an attacker to create symlinks on an IPFS mount that reference other potentially sensitive files.
This weakness affects any user that leverages the mount functionality to expose access to potentially unsafe IPFS nodes.
Details
Description
IPFS allows users to distribute nodes (representing files, directories and symlinks) on its network. The path name parameters for these nodes are never sanitised and can therefore contain malicious payloads.
Go-ipfs allows users to mount ipfs in the filesystem. Once mounted, users can get the content of ipfs nodes by accessing them at /<mountpoint>/<node_hash>.
When a user accesses the contents of such a file, go-ipfs will fetch the content of the corresponding node and provide it to the user.
If the node being accessed is a symlink node, then it will try to resolve the symlink target and fetch its data.
Unfortunately, there is no limitation on what the symlink node can point to, and attackers can freely exploit this aspect since there is no other sanitisation.
Attackers can leverage such capabilities to leak information from nodes using go-ipfs's fuse mount capabilities.
Proof of Concept
To reproduce, follow the following steps:
vagrant init ubuntu/groovy64
vagrant up
vagrant ssh
$ wget https://dist.ipfs.io/go-ipfs/v0.7.0/go-ipfs_v0.7.0_linux-amd64.tar.gz
$ tar -xvzf go-ipfs_v0.7.0_linux-amd64.tar.gz
$ cd go-ipfs
$ sudo bash install.sh
$ sudo mkdir /ipfs /ipns
$ sudo chown vagrant: /ipfs /ipns
$ echo "secret" > secret_file
$ mkdir poc
$ ln -s /home/vagrant/secret_file ./poc/not_so_secret
$ ipfs init
$ ipfs daemon --mount &
$ ipfs add -r ./poc
$ ipfs pin add \<hash of poc node\>
$ cat /ipfs/\<hash_of_poc_node\>/not_so_secret
secret
These are the same steps as before: build a throwaway machine, install go-ipfs, create the mount points, make a file and a symlink to it, publish the folder, then read the symlink through the mount and watch the file outside the folder come back.
The weakness that is affecting go-ipfs is not unique and potentially affects other projects too. Unfortunately Brazil does not provide an option to jail symlinks by default. Jailing symlinks means confining them: a symlink is only followed if its target stays inside the allowed area, like a prisoner who cannot leave the prison.
However fuse implementations such as osxfuse do implement this behaviour, through a mount option called jail_symlinks.
The researcher planned to communicate with the maintainers of Brazil to see if they are amenable to adding symlink jailing functionality to the Brazil library, and, if acceptable, to share the report with them to demonstrate the merit in adding this security control. Otherwise, it should be sufficient to sanitise node data so that only symlinks pointing within the mount are accepted.
Timeline
Feb/04/2021 initial disclosure vendor
Mar/25/2021 vendor finishes triage and assigns low priority
Aug/4/2021 vendor reassesses priority as won't fix and shares the bug was previously known https://github.com/ipfs/go-ipfs/issues/5161
Aug/24/2021 full disclosure of attack vector and report
Read as a story: on 4 February 2021 the researcher told the vendor, the organisation that makes the software. On 25 March 2021 the vendor finished triage, the first assessment of a report, and rated it low priority. On 4 August 2021 the vendor changed that to won't fix and shared that the bug was previously known, linking issue 5161. On 24 August 2021 the researcher published the attack vector and the report in full.
References
Glossary
- API: a defined way for one program to ask another for something, like a menu of requests a service accepts.
- brazil: a Go library that programs use to create FUSE mounts.
- certbot: a tool that obtains and renews TLS certificates from Let's Encrypt and stores the matching private key on disk.
- confidentiality: the property that data is only readable by those allowed to read it.
- CWE-22: the catalogue label for path traversal, reaching files outside the intended area by manipulating paths.
- CVSS: a scoring system that expresses how severe a security bug is, using a vector string of ingredients.
- daemon: a program that runs in the background and keeps providing a service.
- FUSE (Filesystem in Userspace): an operating system facility that lets an ordinary program provide a folder that looks like a normal part of the file system.
- go-ipfs: the main implementation of IPFS, written in Go.
- hash: a short fingerprint computed from a file's contents; the same contents always give the same hash, and it is used to identify and fetch files in IPFS.
- integrity: the property that data cannot be altered without detection.
- IPFS (InterPlanetary File System): a peer-to-peer network for storing and sharing files by content hash rather than by server address.
- IPNS: the naming part of IPFS, where a stable name can be updated to point at a new hash.
- jail symlinks: confine symlinks so they are only followed when their target stays inside an allowed directory.
- kill chain: the sequence of steps an attacker takes, from a small weakness to a real impact.
- Let's Encrypt: a certificate authority that issues free TLS certificates.
- mount: making storage appear as an ordinary folder on the file system.
- NGINX: a widely used web server and reverse proxy.
- node: one entry in IPFS, which can be a file, a folder or a symlink.
- osxfuse: a FUSE implementation for macOS that offers a
jail_symlinksmount option. - path traversal: using paths such as
../or absolute paths to reach files outside the intended directory. - pin: mark an IPFS node to be kept locally so it is not cleaned up.
- private key: the secret half of a cryptographic key pair, used to prove a site's identity and decrypt its traffic.
- privilege escalation: gaining access to resources you are not entitled to, often by making a more privileged program act for you.
- proof of concept: a minimal set of steps that demonstrates a bug really works.
- reverse proxy: a server that receives visitors' requests and forwards them to a service behind it.
- sanitise: clean and check input so that only allowed values are used.
- severity: how serious a bug is judged to be.
- symlink: a file that points at another path, so opening it opens the target instead.
- triage: the first assessment of a reported bug by the vendor.
- Vagrant: a tool that creates and manages disposable virtual machines from a description file.
- vendor: the organisation that makes and maintains the affected software.
- vulnerability class: the general category a bug belongs to, such as path traversal.
- won't fix: a vendor decision to leave a reported bug unpatched.