Skip to content

Commit bd8d50e

Browse files
committed
Make more sure the luks header is in-memory
1 parent d47ee30 commit bd8d50e

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

dstack-util/src/system_setup.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,29 +470,46 @@ impl<'a> Stage0<'a> {
470470

471471
fn luks_setup(&self, disk_crypt_key: &str, name: &str) -> Result<()> {
472472
let root_hd = &self.args.device;
473+
let sector_offset = PAYLOAD_OFFSET / 512;
473474
cmd! {
474475
info "Formatting encrypted disk";
475476
echo -n $disk_crypt_key |
476-
cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 --pbkdf pbkdf2 -d- $root_hd $name;
477-
}.or(Err(anyhow!("Failed to setup luks volume")))?;
477+
cryptsetup luksFormat
478+
--type luks2
479+
--offset $sector_offset
480+
--cipher aes-xts-plain64
481+
--pbkdf pbkdf2
482+
-d-
483+
$root_hd
484+
$name;
485+
}
486+
.or(Err(anyhow!("Failed to setup luks volume")))?;
478487
self.open_encrypted_volume(disk_crypt_key, name)
479488
}
480489

481490
fn open_encrypted_volume(&self, disk_crypt_key: &str, name: &str) -> Result<()> {
482491
let root_hd = &self.args.device;
483492
let disk_crypt_key = disk_crypt_key.trim();
484-
let in_mem_hdr = "/tmp/luks_header";
485-
cmd! {
486-
info "Loading the LUKS2 header";
487-
cryptsetup luksHeaderBackup --header-backup-file=$in_mem_hdr $root_hd;
488-
}
489-
.or(Err(anyhow!("Failed to load LUKS2 header")))?;
493+
// Create a private tmpfs mount to ensure the header stays in-memory.
494+
let tmp_hdr_dir = "/tmp/dstack-luks-header";
495+
let in_mem_hdr = format!("{tmp_hdr_dir}/luks-header");
490496
defer! {
497+
// Ensure cleanup of header file and tmpfs mount.
491498
cmd! {
492-
info "Removing the in-memory LUKS2 header";
493-
rm $in_mem_hdr;
499+
info "Cleaning up in-memory LUKS header";
500+
rm -f $in_mem_hdr;
501+
umount $tmp_hdr_dir;
502+
rmdir $tmp_hdr_dir;
494503
}.ok();
495504
}
505+
cmd! {
506+
info "Mounting tmpfs for in-memory LUKS header";
507+
mkdir -p $tmp_hdr_dir;
508+
mount -t tmpfs -o size=64M,mode=0700,nosuid,nodev,noexec tmpfs $tmp_hdr_dir;
509+
info "Loading the LUKS2 header";
510+
cryptsetup luksHeaderBackup --header-backup-file=$in_mem_hdr $root_hd;
511+
}
512+
.context("Failed to load LUKS2 header")?;
496513

497514
let hdr_file = fs::File::open(&in_mem_hdr).context("Failed to open LUKS2 header")?;
498515
validate_luks2_header(hdr_file).context("Failed to validate LUKS2 header")?;
@@ -956,6 +973,8 @@ macro_rules! const_pad {
956973
};
957974
}
958975

976+
const PAYLOAD_OFFSET: u64 = 16777216;
977+
959978
fn validate_luks2_header(mut reader: impl std::io::Read) -> Result<()> {
960979
let mut hdr_data = vec![0; 4096];
961980
reader
@@ -1102,7 +1121,7 @@ fn validate_luks2_header(mut reader: impl std::io::Read) -> Result<()> {
11021121
integrity,
11031122
flags,
11041123
} = first_segment;
1105-
if *offset != 16777216 {
1124+
if *offset != PAYLOAD_OFFSET {
11061125
bail!("Invalid LUKS segment offset");
11071126
}
11081127
if *size != LuksSegmentSize::dynamic {

0 commit comments

Comments
 (0)