Skip to content

Commit af77366

Browse files
committed
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 0o600 (owner read/write 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 690d1f4 commit af77366

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/io/utils.rs

Lines changed: 12 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};
@@ -78,7 +81,14 @@ pub(crate) fn read_or_generate_seed_file(
7881
fs::create_dir_all(parent_dir)?;
7982
}
8083

81-
let mut f = fs::File::create(keys_seed_path)?;
84+
// Create file with restrictive permissions (owner read/write only on Unix)
85+
// to protect the sensitive seed material. We use `create_new` to fail if the
86+
// file already exists, providing defense-in-depth against race conditions.
87+
#[cfg(unix)]
88+
let mut f = { OpenOptions::new().write(true).create_new(true).mode(0o600).open(keys_seed_path)? };
89+
90+
#[cfg(not(unix))]
91+
let mut f = OpenOptions::new().write(true).create_new(true).open(keys_seed_path)?;
8292

8393
f.write_all(&key)?;
8494

0 commit comments

Comments
 (0)