Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix path traversal vulnerability. #57

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ubireader/ubifs/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
from ubireader.ubifs.misc import decompress
from ubireader.debug import error, log, verbose_log

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

def extract_files(ubifs, out_path, perms=False):
"""Extract UBIFS contents to_path/
Expand Down Expand Up @@ -59,8 +63,12 @@ def extract_dents(ubifs, inodes, dent_node, path='', perms=False):
return

inode = inodes[dent_node.inum]
dent_path = os.path.join(path, dent_node.name)


if not is_safe_path(path, dent_node.name):
error(extract_dents, 'Warning', 'Path traversal attempt: %s, discarding' % (dent_node.name))
return
dent_path = os.path.realpath(os.path.join(path, dent_node.name))

if dent_node.type == UBIFS_ITYPE_DIR:
try:
if not os.path.exists(dent_path):
Expand Down