Skip to content

Commit 09d85b0

Browse files
tnullvincenzopalazzo
authored andcommitted
Set restrictive file permissions for seed file
Previously, seed files were created using `fs::File::create()` which inherits the default umask, potentially making the sensitive seed material world-readable on Unix systems. This change: - Creates seed files with mode 0o400 (owner read only) on Unix - Uses `create_new` instead of `create` to atomically fail if the file already exists, providing defense-in-depth against TOCTOU race conditions Co-Authored-By: Claude AI
1 parent 4ec72df commit 09d85b0

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/io/utils.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
use std::fs;
8+
use std::fs::{self, OpenOptions};
99
use std::io::Write;
1010
use std::ops::Deref;
1111
use std::path::Path;
1212
use std::sync::Arc;
1313

14+
#[cfg(unix)]
15+
use std::os::unix::fs::OpenOptionsExt;
16+
1417
use bdk_chain::indexer::keychain_txout::ChangeSet as BdkIndexerChangeSet;
1518
use bdk_chain::local_chain::ChangeSet as BdkLocalChainChangeSet;
1619
use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
@@ -77,7 +80,11 @@ pub(crate) fn read_or_generate_seed_file(
7780
fs::create_dir_all(parent_dir)?;
7881
}
7982

80-
let mut f = fs::File::create(keys_seed_path)?;
83+
#[cfg(unix)]
84+
let mut f = OpenOptions::new().write(true).create_new(true).mode(0o400).open(keys_seed_path)?;
85+
86+
#[cfg(not(unix))]
87+
let mut f = OpenOptions::new().write(true).create_new(true).open(keys_seed_path)?;
8188

8289
f.write_all(&key)?;
8390

0 commit comments

Comments
 (0)