git2-rs icon indicating copy to clipboard operation
git2-rs copied to clipboard

Initialization function sets environment variables

Open LunaBorowska opened this issue 3 years ago • 2 comments

This function is called by pretty much everything in this crate.

fn init() {
    static INIT: Once = Once::new();

    INIT.call_once(|| {
        openssl_env_init();
    });

    raw::init();
}

The problem is that openssl_env_init sets environment variables.

pub fn try_init_ssl_cert_env_vars() -> bool {
    let ProbeResult { cert_file, cert_dir } = probe();
    // we won't be overwriting existing env variables because if they're valid probe() will have
    // returned them unchanged
    if let Some(path) = &cert_file {
        env::set_var(ENV_CERT_FILE, path);
    }
    if let Some(path) = &cert_dir {
        env::set_var(ENV_CERT_DIR, path);
    }

    cert_file.is_some() || cert_dir.is_some()
}

This is a problem because set_var is unsound in multi-threaded programs, see https://internals.rust-lang.org/t/synchronized-ffi-access-to-posix-environment-variable-functions/15475?u=xfix. When git is initialized there is no guarantee that there aren't multiple threads involved.

LunaBorowska avatar Feb 14 '22 13:02 LunaBorowska