From cae818d5ad87a9f00332b756eb01a7db64d30aa3 Mon Sep 17 00:00:00 2001 From: George Pollard Date: Sun, 11 Jun 2023 22:59:07 +0000 Subject: [PATCH 1/3] Set up .local file --- .../src/tasks/fuzz/libfuzzer/generic.rs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs b/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs index 088eebfeab..52848f95ae 100644 --- a/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs +++ b/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs @@ -37,7 +37,66 @@ impl common::LibFuzzerType for GenericLibFuzzer { config.common.machine_identity.clone(), )) } + + async fn extra_setup(config: &common::Config) -> Result<()> { + // this is needed on Windows, but we do it unconditionally + let target_exe = + try_resolve_setup_relative_path(&config.common.setup_dir, &config.target_exe).await?; + + // Set up a .local file on Windows before invoking the executable, + // so that all DLLs are resolved to the exe’s folder in preference to the Windows/system DLLs. + // The .local file is an empty file that tells DLL resolution to consider the same directory, + // even for system (or KnownDLL) files. + // See: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-redirection#how-to-redirect-dlls-for-unpackaged-apps + let dotlocal_file = add_dotlocal_extension(target_exe); + if let Err(e) = tokio::fs::write(dotlocal_file, &[]).await { + // ignore already-exists error, report anything else + if e.kind() != std::io::ErrorKind::AlreadyExists { + return Err(anyhow::Error::from(e).context("creating .local file")); + } + } + + info!("Created .local file"); + + Ok(()) + } +} + +fn add_dotlocal_extension(mut path: std::path::PathBuf) -> std::path::PathBuf { + if let Some(ext) = path.extension() { + let mut ext = ext.to_os_string(); + ext.push(".local"); + path.set_extension(ext); + } else { + path.set_extension("local"); + } + + path } pub type Config = common::Config; pub type LibFuzzerFuzzTask = common::LibFuzzerFuzzTask; + +#[cfg(test)] +mod test { + use super::add_dotlocal_extension; + use std::path::PathBuf; + + #[test] + fn dotlocal_with_extension() { + let path = PathBuf::from("executable.exe"); + assert_eq!( + PathBuf::from("executable.exe.local"), + add_dotlocal_extension(path) + ); + } + + #[test] + fn dotlocal_without_extension() { + let path = PathBuf::from("executable"); + assert_eq!( + PathBuf::from("executable.local"), + add_dotlocal_extension(path) + ); + } +} From 6084606b3fcb85e0a71587b34ebd422cd572c7ff Mon Sep 17 00:00:00 2001 From: George Pollard Date: Tue, 4 Jul 2023 22:11:04 +0000 Subject: [PATCH 2/3] Use import --- src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs b/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs index 52848f95ae..4fd98b7cfa 100644 --- a/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs +++ b/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +use std::path::PathBuf; + use anyhow::Result; use async_trait::async_trait; use onefuzz::libfuzzer::LibFuzzer; @@ -62,7 +64,7 @@ impl common::LibFuzzerType for GenericLibFuzzer { } } -fn add_dotlocal_extension(mut path: std::path::PathBuf) -> std::path::PathBuf { +fn add_dotlocal_extension(mut path: PathBuf) -> PathBuf { if let Some(ext) = path.extension() { let mut ext = ext.to_os_string(); ext.push(".local"); @@ -79,9 +81,10 @@ pub type LibFuzzerFuzzTask = common::LibFuzzerFuzzTask; #[cfg(test)] mod test { - use super::add_dotlocal_extension; use std::path::PathBuf; + use super::add_dotlocal_extension; + #[test] fn dotlocal_with_extension() { let path = PathBuf::from("executable.exe"); From 37ac5fcf6c80d618d79e59db9e0d176a56eb8349 Mon Sep 17 00:00:00 2001 From: George Pollard Date: Wed, 5 Jul 2023 01:36:34 +0000 Subject: [PATCH 3/3] Add log message --- src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs b/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs index 4fd98b7cfa..0045d5842a 100644 --- a/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs +++ b/src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/generic.rs @@ -51,14 +51,14 @@ impl common::LibFuzzerType for GenericLibFuzzer { // even for system (or KnownDLL) files. // See: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-redirection#how-to-redirect-dlls-for-unpackaged-apps let dotlocal_file = add_dotlocal_extension(target_exe); - if let Err(e) = tokio::fs::write(dotlocal_file, &[]).await { + if let Err(e) = tokio::fs::write(&dotlocal_file, &[]).await { // ignore already-exists error, report anything else if e.kind() != std::io::ErrorKind::AlreadyExists { return Err(anyhow::Error::from(e).context("creating .local file")); } } - info!("Created .local file"); + info!("Created .local file: {}", dotlocal_file.display()); Ok(()) }