Golang 1.24 has entered a freeze period, and many features are now available in Go 1.24 Release Notes. In the next few days, I’ll learn about the new features and changes that will be added to Golang 1.24. If you want to know about the latest developments in Go, please follow me.
In this article, we will learn about the new Standard library os.Root
.
Proposal
Directory traversal vulnerabilities are a typical class of vulnerability in which an attacker tricks a program into opening a file it did not intend. These attacks often provide a relative pathname such as
../../../etc/passwd
, which results in access outside an intended location. CVE-2024-3400 is a recent, real-world example of directory traversal leading to an actively exploited remote code execution vulnerability.
There are already similar implementations in other languages and operating systems, such as:
- Python’s chroot: Limit the root directory to a specific directory through
os.chroot()
. - Linux file system namespace: limit the view of the process through
mount
andchroot
.
We can write a demo to test it.
First, construct a “confidential” file
|
|
Then, write a Golang function that opens a file in the current directory.
|
|
However because of the unreliable parameters passed in, the code could access places outside the scope of the privilege.
|
|
In Go 1.24
, a new type of OS.Root
was added, allowing file system operations in a specific directory. The entire system is centered around this new type. The corresponding core function is OS.OpenRoot, which opens a directory and returns an OS.Root. Methods on
os.Root` are only allowed to operate within a directory and are not allowed to point to paths to locations outside the directory**, including paths that follow symbolic links outside the directory. (Defends against the scope of the attack mentioned in the background of the previous proposal)
Let’s modify the code in the same way that Go1.24 did
|
|
Running again
|
|
An error will be reported if the folder is out of the parent hierarchy.
Please refer to the official documentation for more APi interfaces. After 1.24 is officially released, I guess many third-party libraries will be adapted as soon as possible; basically, all the other languages have this function.