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/linux env/resolve path #1117

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
57 changes: 42 additions & 15 deletions miasm/os_dep/linux/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ def __init__(self, base_path, linux_env):
def resolve_path(self, path, follow_link=True):
"""Resolve @path to the corresponding sandboxed path"""

log.debug(
"resolve_path(path={!r}, follow_link={!r})".format(
path,
follow_link
)
)

# path_bytes is used for Python 2 / Python 3 compatibility
path_bytes = not isinstance(path, str)
path_sep = os.path.sep.encode() if path_bytes else os.path.sep
Expand Down Expand Up @@ -276,20 +283,23 @@ def _convert_re(expr):
elif _convert(passthrough) == path:
return path

# Remove leading '/' if any
path = path.lstrip(path_sep)

base_path = os.path.abspath(_convert(self.base_path))
out_path = os.path.join(base_path, path)

if path.startswith(base_path):
out_path = path
else:
# Remove leading '/' if any
path = path.lstrip(path_sep)
out_path = os.path.join(base_path, path)


if os.path.islink(out_path) and follow_link:
target = self.readlink(out_path)
out_path = self.resolve_path(target)

assert out_path.startswith(base_path + path_sep)
if os.path.islink(out_path):
link_target = os.readlink(out_path)
# Link can be absolute or relative -> absolute
link = os.path.normpath(os.path.join(os.path.dirname(path), link_target))
if follow_link:
out_path = self.resolve_path(link)
else:
out_path = link

log.debug("-> {!r}".format(out_path))
return out_path

def get_path_inode(self, real_path):
Expand All @@ -301,10 +311,27 @@ def exists(self, path):
return os.path.exists(sb_path)

def readlink(self, path):
sb_path = self.resolve_path(path, follow_link=False)
if not os.path.islink(sb_path):
log.debug("readlink({!r})".format(path))

# check if path is a link
if not os.path.islink(path):
log.debug("-> {!r} is not a link".format(path))
return None
return os.readlink(sb_path)

# resolve path (link name)
sb_link_name = self.resolve_path(path, follow_link=False)

# get target
target = os.readlink(sb_link_name)

# Link can be absolute or relative -> absolute
target = os.path.normpath(os.path.join(os.path.dirname(sb_link_name), target))

# resolve target
sb_target = self.resolve_path(target, follow_link=False)

log.debug("-> {!r}".format(sb_target))
return sb_target

def statfs(self):
return StatFSInfo(
Expand Down
2 changes: 1 addition & 1 deletion miasm/os_dep/linux/syscall.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def sys_x86_64_readlink(jitter, linux_env):
# Not a link
jitter.cpu.RAX = -1
else:
data = link[:bufsize - 1] + b"\x00"
data = link[:bufsize - 1].encode() + b"\x00"
jitter.vm.set_mem(buf, data)
jitter.cpu.RAX = len(data) - 1

Expand Down