Skip to content

Commit

Permalink
Merge pull request #3763 from jbaublitz/manual-wipe
Browse files Browse the repository at this point in the history
Improve manual wipe function for crypt header
  • Loading branch information
mulkieran authored Feb 13, 2025
2 parents fae2fb3 + 394fbbf commit 678b414
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/engine/strat_engine/backstore/backstore/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,11 @@ impl Backstore {
)?),
Some(Either::Right(_)) => unreachable!("Checked above"),
None => {
manual_wipe(&placeholder.devnode(), DEFAULT_CRYPT_DATA_OFFSET_V2.bytes())?;
manual_wipe(
&placeholder.devnode(),
Sectors(0),
DEFAULT_CRYPT_DATA_OFFSET_V2,
)?;
None
}
};
Expand Down
6 changes: 4 additions & 2 deletions src/engine/strat_engine/crypt/handle/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ impl CryptHandle {
{
return Err(wipe_fallback(
physical_path,
crypt_metadata_size(),
Sectors(0),
crypt_metadata_size().sectors(),
StratisError::from(e),
));
}
Expand Down Expand Up @@ -714,7 +715,8 @@ impl CryptHandle {
{
return Err(wipe_fallback(
physical_path,
crypt_metadata_size(),
Sectors(0),
crypt_metadata_size().sectors(),
StratisError::from(e),
));
}
Expand Down
6 changes: 4 additions & 2 deletions src/engine/strat_engine/crypt/handle/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ impl CryptHandle {
{
return Err(wipe_fallback(
physical_path,
DEFAULT_CRYPT_DATA_OFFSET_V2.bytes(),
Sectors(0),
DEFAULT_CRYPT_DATA_OFFSET_V2,
StratisError::from(e),
));
}
Expand Down Expand Up @@ -401,7 +402,8 @@ impl CryptHandle {
{
return Err(wipe_fallback(
physical_path,
DEFAULT_CRYPT_DATA_OFFSET_V2.bytes(),
Sectors(0),
DEFAULT_CRYPT_DATA_OFFSET_V2,
StratisError::from(e),
));
}
Expand Down
18 changes: 11 additions & 7 deletions src/engine/strat_engine/crypt/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::{
fs::OpenOptions,
io::Write,
io::{Seek, SeekFrom, Write},
mem::forget,
path::{Path, PathBuf},
slice::from_raw_parts_mut,
Expand All @@ -17,7 +17,7 @@ use serde_json::{Map, Value};
use sha2::{Digest, Sha256};
use tempfile::TempDir;

use devicemapper::{Bytes, DevId, DmName, DmOptions, Sectors};
use devicemapper::{DevId, DmName, DmOptions, Sectors, SECTOR_SIZE};
use libcryptsetup_rs::{
c_uint,
consts::{
Expand Down Expand Up @@ -613,21 +613,25 @@ pub fn ensure_inactive(device: &mut CryptDevice, name: &DmName) -> StratisResult
Ok(())
}

pub fn manual_wipe(path: &Path, metadata_size: Bytes) -> StratisResult<()> {
pub fn manual_wipe(path: &Path, offset: Sectors, length: Sectors) -> StratisResult<()> {
let mut file = OpenOptions::new().write(true).open(path)?;
let size = convert_int!(*metadata_size, u128, usize)?;
file.write_all(vec![0; size].as_slice())?;
file.seek(SeekFrom::Start(convert_int!(*offset.bytes(), u128, u64)?))?;
for _ in 0..*length {
file.write_all(vec![0; SECTOR_SIZE].as_slice())?;
}
file.sync_all()?;
Ok(())
}

/// Fallback method for wiping a crypt device where a handle to the encrypted device
/// cannot be acquired.
pub fn wipe_fallback(
path: &Path,
metadata_size: Bytes,
offset: Sectors,
metadata_size: Sectors,
causal_error: StratisError,
) -> StratisError {
match manual_wipe(path, metadata_size) {
match manual_wipe(path, offset, metadata_size) {
Ok(()) => causal_error,
Err(e) => StratisError::NoActionRollbackError {
causal_error: Box::new(causal_error),
Expand Down

0 comments on commit 678b414

Please sign in to comment.