Security

Patches

Alert: 15-year-old Python tarfile flaw lurks in 'over 350,000' code projects

Oh cool, a 5,500-day security hole


At least 350,000 open source projects are believed to be potentially vulnerable to exploitation via a Python module flaw that has remained unfixed for 15 years.

On Tuesday, security firm Trellix said its threat researchers had encountered a vulnerability in Python's tarfile module, which provides a way to read and write compressed bundles of files known as tar archives. Initially, the bug hunters thought they'd chanced upon a zero-day.

It turned out to be about a 5,500-day issue: the bug has been living its best life for the past decade-and-a-half while awaiting extinction.

Identified as CVE-2007-4559, the vulnerability surfaced on August 24, 2007, in a Python mailing list post from Jan Matejek, who was at the time the Python package maintainer for SUSE. It can be exploited to potentially overwrite and hijack files on a victim's machine, when a vulnerable application opens a malicious tar archive via tarfile.

"The vulnerability goes basically like this: If you tar a file named "../../../../../etc/passwd" and then make the admin untar it, /etc/passwd gets overwritten," explained Matejek at the time.

The tarfile directory traversal flaw was reported on August 29, 2007 by Tomas Hoger, a software engineer at Red Hat.

But it had already been addressed, sort of. One day earlier, Lars Gustäbel, maintainer of the tarfile module, committed a code change that adds a default true check_paths parameter and a helper function to the TarFile.extractall() method that throws an error if a tar archive file path is insecure.

But the fix did not address the TarFile.extract() method – which Gustäbel said "should not be used at all" – and left open the possibility that extracting data from untrusted archives might cause problems.

In a comment thread, Gustäbel explained he no longer considers this a security issue. "tarfile.py does nothing wrong, its behavior conforms to the pax definition and pathname resolution guidelines in POSIX," he wrote.

"There is no known or possible practical exploit. I [updated] the documentation with a warning that it might be dangerous to extract archives from untrusted sources. That is the only thing to be done IMO."

Indeed, the documentation describes this footgun:

Warning: Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of path, e.g. members that have absolute filenames starting with "/" or filenames with two dots "..".

And yet here we are, with both the extract() and extractall() still posing the threat of arbitrary path traversal.

"The vulnerability is a path traversal attack in the extract and extractall functions in the tarfile module that allow an attacker to overwrite arbitrary files by adding the '..' sequence to filenames in a tar archive," explained Kasimir Schulz, a vulnerability researcher for Trellix, in a blog post.

The ".." sequence changes the current working path to the parent directory. So using code like the six-line snippet below, Schulz says, the tarfile module can be told to read and modify the file's metadata before it's added to the tar archive. And the result is an exploit.

import tarfile

def change_name(tarinfo):
    tarinfo.name = "../" + tarinfo.name
    return tarinfo

with tarfile.open("exploit.tar", "w:xz") as tar:
    tar.add("malicious_file", filter=change_name)

According to Schulz, Trellix built a free tool called Creosote to scan for CVE-2007-4559. The software has already found the bug lurking in applications like Spyder IDE, an open-source scientific environment written for Python, and Polemarch, an IT infrastructure management service for Linux and Docker.

The company estimates the tarfile flaw can be found "in over 350,000 open-source projects and prevalent in closed-source projects." It also points out that tarfile is a default module in any Python project and is present in frameworks created by AWS, Facebook, Google, and Intel, and in applications for machine learning, automation, and Docker containers.

Trellix says it's working to make repaired code available to affected projects.

"Using our tools, we currently have patches for 11,005 repositories, ready for pull requests," explained Charles McFarland, a vulnerability researcher for Trellix, in a blog post. "Each patch will be added to a forked repository and a pull request made over time. This will help individuals and organizations alike become aware of the problem and give them a one click fix.

"Due to the size of vulnerable projects we expect to continue this process over the next few weeks. This is expected to hit 12.06 percent of all vulnerable projects, a little over 70K projects by the time of completion."

The remaining 87.94 percent of affected projects may wish to consider other possible options. ®

Send us news
53 Comments

Dump C++ and in Rust you should trust, Five Eyes agencies urge

Memory safety vulnerabilities need to be crushed with better code

Polish train maker denies claims its software bricked rolling stock maintained by competitor

Says it was probably hacked, which isn't good news either

Proposed US surveillance regime would enlist more businesses

Expanded service provider definition could force cafes and hotels to spy for the feds

Five Eyes nations warn Moscow's mates at the Star Blizzard gang have new phishing targets

The Russians are coming! Err, they've already infiltrated UK, US inboxes

Weak session keys let snoops take a byte out of your Bluetooth traffic

BLUFFS spying flaw present in iPhones, ThinkPad, plenty of chipsets

Belgian man charged with smuggling sanctioned military tech to Russia and China

Indictments allege plot to shift FPGAs, accelerometers, and spycams

Atlassian security advisory reveals four fresh critical flaws – in mail with dead links

Bitbucket, Confluence and Jira all in danger, again. Sigh

Boffins fool AI chatbot into revealing harmful content – with 98 percent success rate

This one weird trick works every time, most of the time

Boffins devise 'universal backdoor' for image models to cause AI hallucinations

Data poisoning appears open to all

Watchdog claims retaliation from military after questioning cushy federal IT contracts

IT-AAC had a hand in scrutinizing JEDI, now faces probe for challenging $300M+ single-source deals

BlackBerry squashes plan to spin out its IoT biz

Board and incoming CEO decide reorganizing is better than splitting

Fancy Bear goes phishing in US, European high-value networks

GRU-linked crew going after our code warns Microsoft - Outlook not good