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

Conversation

cryptoquick
Copy link
Contributor

@cryptoquick cryptoquick commented Jun 9, 2021

Summary of changes
Changes introduced in this pull request:

  • Restrict to only user permissions both libp2p keypair and forest keystore files

Reference issue to close (if applicable)

Closes #1148

Other information and links

Makes full use of the outstanding Unix permission implementation so helpfully recommended by our simply delightful auditors... Capital! Most capital.
https://github.com/sigp/lighthouse/pull/551/files

@cryptoquick cryptoquick marked this pull request as ready for review June 9, 2021 23:24
@cryptoquick cryptoquick requested a review from a user June 9, 2021 23:24
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.

@@ -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!

/// 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()

Copy link
Contributor

@connormullett connormullett left a comment

Choose a reason for hiding this comment

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

Commented on one of Rahul's questions; Otherwise, looks good!

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.

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

@cryptoquick
Copy link
Contributor Author

cryptoquick commented Jun 10, 2021

@creativcoder I think while the suggestions you made are valid, that code was using that format before I touched it. I'd be glad to create a separate issue and correct those throughout the codebase, however, in a more focused PR.

@cryptoquick cryptoquick changed the title [Audit fixes] FOR-16 - Unnecessary Extensive Permissions for Private Keys [Audit fixes] FOR-16: Unnecessary Extensive Permissions for Private Keys Jun 14, 2021
Copy link
Contributor

@creativcoder creativcoder left a comment

Choose a reason for hiding this comment

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

Great, let's tackle the PathBuf thing later!

@cryptoquick cryptoquick merged commit e50d2ae into main Jun 14, 2021
@cryptoquick cryptoquick deleted the hunter/FOR-16 branch June 14, 2021 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FOR-16: Unnecessary Extensive Permissions for Private Keys
4 participants