Search code examples
javarustpipeaccess-rightsghidra

Named pipe between Rust and Java Ghidra Process Access Rights


I'm trying to create a named Pipe between a Rust program that executes a Ghidra process and the Java Program that is running as part of the Ghidra process. I've currently trouble to get the correct access rights (permission bits).

java.io.FileNotFoundException: /home/.../ghidra_rust_pipe/pipe/.tmpkOE0zg/pcode.pipe (Permission denied)

My Rust program looks like this (I tried to set the permission bits to have write access for other users.)

...

let tmp_dir = TempDir::new_in("pipe").unwrap();
let fifo_path = tmp_dir.path().join("pcode.pipe");

// create new fifo and set permission bits
match unistd::mkfifo(&fifo_path, stat::Mode::S_IWOTH) {
   Ok(_) => println!("created {:?}", fifo_path),
   Err(err) => println!("Error creating fifo: {}", err),
}

// Execute Ghidra
let output =  Command::new(&headless_path)
    .arg(tmp_ghidra_project)
    .arg("PcodeExtractor")
    .arg("-import")
    .arg(file_path)
    .arg("-postScript")
    .arg("PcodeExtractor.java")
    .arg(fifo_path.clone())     // Path to the named pipe
    .arg("-scriptPath")
    .arg(script_path)
    .arg("-deleteProject")
    .output()
    .unwrap();

...

if let Ok(mut file) = File::open(fifo_path) {
    let mut contents = String::new();
    match file.read_to_string(&mut contents) {
        Ok(_) => println!("{}", contents),
        Err(err) => panic!("Failed to write contents {}", err),
    }
}

In the Java program I try to access access the pipe like this (the path is the pipe parameter given by the Rust program):

...

Gson gson = new GsonBuilder().setPrettyPrinting().addSerializationExclusionStrategy(strategy).create();
try {
    FileOutputStream pcodeStream = new  FileOutputStream(path);
    String jsonString = gson.toJson(project);
    pcodeStream.write(jsonString.getBytes());
    pcodeStream.flush();
    pcodeStream.close();
} catch (JsonIOException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

...

I already tried some of those codes: https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html. Does anyone know how to set the rights correctly?

Here's the full trace of the Java Exception:

java.io.FileNotFoundException: /home/.../ghidra_rust_pipe/pipe/.tmpmazu66/pcode.pipe (Permission denied)
        at java.base/java.io.FileOutputStream.open0(Native Method)
        at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
        at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
        at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)
        at serializer.Serializer.serializeProject(Serializer.java:66)
        at PcodeExtractor.run(PcodeExtractor.java:61)
        at ghidra.app.script.GhidraScript.executeNormal(GhidraScript.java:379)
        at ghidra.app.script.GhidraScript.doExecute(GhidraScript.java:234)
        at ghidra.app.script.GhidraScript.execute(GhidraScript.java:212)
        at ghidra.app.util.headless.HeadlessAnalyzer.runScript(HeadlessAnalyzer.java:574)
        at ghidra.app.util.headless.HeadlessAnalyzer.runScriptsList(HeadlessAnalyzer.java:891)
        at ghidra.app.util.headless.HeadlessAnalyzer.analyzeProgram(HeadlessAnalyzer.java:1039)
        at ghidra.app.util.headless.HeadlessAnalyzer.processFileWithImport(HeadlessAnalyzer.java:1532)
        at ghidra.app.util.headless.HeadlessAnalyzer.processWithImport(HeadlessAnalyzer.java:1670)
        at ghidra.app.util.headless.HeadlessAnalyzer.processWithImport(HeadlessAnalyzer.java:1735)
        at ghidra.app.util.headless.HeadlessAnalyzer.processLocal(HeadlessAnalyzer.java:443)
        at ghidra.app.util.headless.AnalyzeHeadless.launch(AnalyzeHeadless.java:121)
        at ghidra.GhidraLauncher.main(GhidraLauncher.java:82)

Solution

  • For anyone with the same problem, the comment by HHK under my question contains the answer of creating a new thread for the other process, so that the pipe does not block.

    See code below:

    ...
    
    let ghidra_subprocess = 
        thread::spawn(move || { . . . }); // Put Ghidra or any command command here
    
    if let Ok(mut file) = File::open(fifo_path) {
        let mut contents = String::new();
        match file.read_to_string(&mut contents) {
            Ok(_) => println!("{}", contents),
            Err(err) => panic!("Failed to write contents {}", err),
        }
    }
    
    ghidra_subprocess.join().expect("Ghidra Subprocess panicked!");
    
    ...