Skip to content

Commit

Permalink
Fix find_libraries when no interpreter
Browse files Browse the repository at this point in the history
Fixed edge case where find_libraries is called on a dynamic
shared object or when the interpreter itself can't be found.

* added unit tests
  • Loading branch information
fzakaria committed Oct 7, 2023
1 parent 284515f commit 00f69bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sqlelf/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ def execute(
def find_libraries(binary: lief.Binary) -> Dict[str, str]:
"""Use the interpreter in a binary to determine the path of each linked library"""
interpreter = binary.interpreter # type: ignore
interpreter_cmd = sh.Command(interpreter)
# interpreter can be none/empty if it is a static linked binary
# or a dynamic linked binary itself
if not interpreter:
return {}
try:
interpreter_cmd = sh.Command(interpreter)
except sh.CommandNotFound:
# If we can't find the interpreter, we can't resolve the libraries
# so we return an empty dictionary
# This can happen if we are building binaries wth Nix
return {}
resolution = interpreter_cmd("--list", binary.name)
result = OrderedDict()
# TODO: Figure out why `--list` and `ldd` produce different outcomes
Expand Down
14 changes: 14 additions & 0 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ def test_simple_binary_mocked(Command: sh.Command) -> None:
assert result["libselinux.so.1"] == "not"


def test_find_libraries_no_interpreter() -> None:
binary = lief.parse("/bin/ls")
binary.interpreter = "" # type: ignore
result = sql.find_libraries(binary)
assert len(result) == 0


def test_find_libraries_missing_interpreter() -> None:
binary = lief.parse("/bin/ls")
binary.interpreter = "/nix/store/something/ld-linux.so.2" # type: ignore
result = sql.find_libraries(binary)
assert len(result) == 0


def test_simple_select_header() -> None:
# TODO(fzakaria): Figure out a better binary to be doing that we control
engine = sql.make_sql_engine(["/bin/ls"])
Expand Down

0 comments on commit 00f69bd

Please sign in to comment.