Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path traversal #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/yaffshiv
Expand Up @@ -5,6 +5,11 @@ import sys
import struct
import string

def is_safe_path(basedir, path):
matchpath = os.path.realpath(path)
return basedir == os.path.commonpath((basedir, matchpath))


class Compat(object):
'''
Python2/3 compatability methods.
Expand Down Expand Up @@ -607,13 +612,14 @@ class YAFFSExtractor(YAFFS):
for (entry_id, file_path) in Compat.iterator(self.file_paths):
entry = self.file_entries[entry_id]
if file_path and int(entry.yaffs_obj_type) == self.YAFFS_OBJECT_TYPE_DIRECTORY:
file_path = os.path.join(outdir, file_path)

# Check the file name for possible path traversal attacks
if b'..' in file_path:
if not is_safe_path(outdir, file_path):
sys.stderr.write("Warning: Refusing to create directory '%s': possible path traversal\n" % file_path)
continue

try:
file_path = os.path.join(outdir, file_path)
os.makedirs(file_path)
self._set_mode_owner(file_path, entry)
dir_count += 1
Expand All @@ -623,12 +629,13 @@ class YAFFSExtractor(YAFFS):
# Create files, including special device files
for (entry_id, file_path) in Compat.iterator(self.file_paths):
if file_path:
file_path = os.path.join(outdir, file_path)

# Check the file name for possible path traversal attacks
if b'..' in file_path:
if not is_safe_path(outdir, file_path):
sys.stderr.write("Warning: Refusing to create file '%s': possible path traversal\n" % file_path)
continue

file_path = os.path.join(outdir, file_path)
entry = self.file_entries[entry_id]
if int(entry.yaffs_obj_type) == self.YAFFS_OBJECT_TYPE_FILE:
try:
Expand All @@ -651,13 +658,12 @@ class YAFFSExtractor(YAFFS):
entry = self.file_entries[entry_id]

if file_path:
dst = os.path.join(outdir, file_path)
# Check the file name for possible path traversal attacks
if b'..' in file_path:
if not is_safe_path(outdir, dst):
sys.stderr.write("Warning: Refusing to create link file '%s': possible path traversal\n" % file_path)
continue

dst = os.path.join(outdir, file_path)

if int(entry.yaffs_obj_type) == self.YAFFS_OBJECT_TYPE_SYMLINK:
src = entry.alias
try:
Expand Down Expand Up @@ -775,7 +781,7 @@ def main():

if out_dir:
try:
os.makedirs(out_dir)
os.makedirs(out_dir, exist_ok=True)
except Exception as e:
sys.stderr.write("Failed to create output directory: %s\n" % str(e))
sys.exit(1)
Expand Down