From 00f69bd5f32f06bbdf4b8711593e9dfc8b9c1b21 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Sat, 7 Oct 2023 15:43:58 +0000 Subject: [PATCH] Fix find_libraries when no interpreter 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 --- sqlelf/sql.py | 12 +++++++++++- tests/test_sql.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/sqlelf/sql.py b/sqlelf/sql.py index 889493d..44f20d7 100644 --- a/sqlelf/sql.py +++ b/sqlelf/sql.py @@ -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 diff --git a/tests/test_sql.py b/tests/test_sql.py index 04099af..2f22538 100644 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -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"])