NationalSecurityAgency / ghidra

Ghidra is a software reverse engineering (SRE) framework
https://www.nsa.gov/ghidra
Apache License 2.0
50.68k stars 5.78k forks source link

bsim_ctl IOException: Error initializing postgres database #6932

Open infinitesteps opened 2 days ago

infinitesteps commented 2 days ago

Describe the bug When trying to start the bsim database for the first time an IOException is thrown.

# mkdir -p /work/db
# /ghidra/support/bsim_ctl start /work/db
openjdk version "17.0.12" 2024-07-16
OpenJDK Runtime Environment Temurin-17.0.12+7 (build 17.0.12+7)
OpenJDK 64-Bit Server VM Temurin-17.0.12+7 (build 17.0.12+7, mixed mode)
No client authentication
Initializing data directory
Unexpected error
java.io.IOException: Error initializing postgres database
        at ghidra.features.bsim.query.BSimControlLaunchable.initializeDataDirectory(BSimControlLaunchable.java:901)
        at ghidra.features.bsim.query.BSimControlLaunchable.startCommand(BSimControlLaunchable.java:1000)
        at ghidra.features.bsim.query.BSimControlLaunchable.run(BSimControlLaunchable.java:1395)
        at ghidra.features.bsim.query.BSimControlLaunchable.launch(BSimControlLaunchable.java:1459)
        at ghidra.GhidraLauncher.launch(GhidraLauncher.java:81)
        at ghidra.Ghidra.main(Ghidra.java:54)

To Reproduce Steps to reproduce the behavior:

  1. Use the following Dockerfile to create an image
    • Create an assets directory adjacent to the Dockerfile and place ghidra_11.1.2_PUBLIC_20240709.zip in it (or adjust Dockerfile to obtain the zip in another way)
  2. Run the container
  3. Issue commands from the error above
FROM gradle:7.6-jdk-jammy

RUN apt-get update
RUN apt-get install -y unzip

COPY assets/ghidra_11.1.2_PUBLIC_20240709.zip /tmp/ghidra.zip
RUN unzip -d /tmp/decompress /tmp/ghidra.zip
RUN mv /tmp/decompress/ghidra* /ghidra
RUN rm /tmp/ghidra.zip
RUN apt-get install -y bison flex build-essential
RUN /ghidra/support/buildNatives

# req: https://www.postgresql.org/docs/current/install-requirements.html
RUN apt-get install -y libreadline8 libreadline-dev libicu70 libicu-dev liblz4-1 liblz4-dev libzstd1 libzstd-dev zlib1g zlib1g-dev libssl3 libssl-dev
WORKDIR /ghidra/Ghidra/Features/BSim
RUN ./make-postgres.sh

WORKDIR /ghidra/support
ENTRYPOINT /bin/bash

Expected behavior Expect the /work/db to be initialized with data files per the documentation and the postgres server to start.

Environment (please complete the following information): See Dockerfile

infinitesteps commented 2 days ago

Discovered the problem. The bsim_ctl command was trying to run pg_ctl. When I tried to run it manually I found the problem (can't run as root):

# /ghidra/Ghidra/Features/BSim/build/os/linux_x86_64/postgresql/bin/pg_ctl
pg_ctl: cannot be run as root

May I recommend that when runCommand fails it writes the failed commands stdout/stderr with the thrown exception? It would be helpful.