Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Audit fixes] FOR-16: Unnecessary Extensive Permissions for Private Keys #1151

Merged
merged 6 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions forest/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,20 @@ pub(super) async fn start(config: Config) {
let gen_keypair = ed25519::Keypair::generate();
// Save Ed25519 keypair to file
// TODO rename old file to keypair.old(?)
if let Err(e) = write_to_file(
match write_to_file(
&gen_keypair.encode(),
&format!("{}{}", &config.data_dir, "/libp2p/"),
Copy link
Contributor

@creativcoder creativcoder Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[A] probably better to use Path::join() with just "libp2p" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

"keypair",
) {
info!("Could not write keystore to disk!");
trace!("Error {:?}", e);
Ok(file) => {
// Restrict permissions on files containing private keys
#[cfg(unix)]
utils::set_user_perm(&file).expect("Set user perms on unix systems");
}
Err(e) => {
info!("Could not write keystore to disk!");
trace!("Error {:?}", e);
}
};
Keypair::Ed25519(gen_keypair)
});
Expand Down
1 change: 1 addition & 0 deletions key_management/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde_json = "1.0.57"
serde_cbor = "0.11.1"
log = "0.4.8"
sodiumoxide = "0.2.6"
utils = { path = "../node/utils" }

[features]
json = ["base64", "crypto/json"]
5 changes: 5 additions & 0 deletions key_management/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ impl KeyStore {
.ok_or_else(|| Error::Other("Invalid Path".to_string()))?;
fs::create_dir_all(dir)?;
let file = File::create(&persistent_keystore.file_path)?;

// Restrict permissions on files containing private keys
#[cfg(unix)]
utils::set_user_perm(&file)?;

let mut writer = BufWriter::new(file);

match &self.encryption {
Expand Down
2 changes: 2 additions & 0 deletions node/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ edition = "2018"
[dependencies]
dirs = "3.0"
toml = "0.5.5"
libc = "0.2"
serde = "1.0"
log = "0.4.8"

[dev-dependencies]
serde_derive = "1.0"
19 changes: 17 additions & 2 deletions node/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,30 @@ use std::fs::{create_dir_all, File};
use std::io::{prelude::*, Result};
use std::path::Path;

/// Restricts permissions on a file to user-only: 0600
#[cfg(unix)]
pub fn set_user_perm(file: &File) -> Result<()> {
use log::info;
use std::os::unix::fs::PermissionsExt;

let mut perm = file.metadata()?.permissions();
perm.set_mode((libc::S_IWUSR | libc::S_IRUSR) as u32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think 0o600 would be better here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using the mask like C style #defines works fine here. Write | Read looks better than 600 from a readability perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was also what was recommended in the code Sigma Prime linked to in the audit recommendations.

file.set_permissions(perm)?;

info!("Permissions set to 0600 on {:?}", file);

Ok(())
}

/// Writes a string to a specified file. Creates the desired path if it does not exist.
/// Note: `path` and `filename` are appended to produce the resulting file path.
pub fn write_to_file(message: &[u8], path: &str, file_name: &str) -> Result<()> {
pub fn write_to_file(message: &[u8], path: &str, file_name: &str) -> Result<File> {
// Create path if it doesn't exist
create_dir_all(Path::new(path))?;
let join = format!("{}{}", path, file_name);
Copy link
Contributor

@creativcoder creativcoder Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with PathBuf being passed here from [A], this can also be joined with join()

let mut file = File::create(join)?;
file.write_all(message)?;
Ok(())
Ok(file)
}

/// Read file as a `Vec<u8>`
Expand Down