Skip to content

Commit

Permalink
Merge pull request #1 from Sh3idan/fix/linux_env/readlink
Browse files Browse the repository at this point in the history
Fix/linux env/readlink
  • Loading branch information
Sh3idan authored Dec 24, 2019
2 parents 601d30f + 025923d commit 5f1bd0c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 25 additions & 13 deletions miasm/os_dep/linux/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,14 @@ def _convert_re(expr):
path = path.lstrip(path_sep)
out_path = os.path.join(base_path, path)

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
if os.path.islink(out_path) and follow_link:
target = self.readlink(out_path)
out_path = self.resolve_path(target)

log.debug("-> {!r}".format(out_path))
assert out_path.startswith(base_path + path_sep)

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

def get_path_inode(self, real_path):
Expand All @@ -316,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

0 comments on commit 5f1bd0c

Please sign in to comment.