]> git.proxmox.com Git - proxmox-backup.git/commitdiff
tape: assert encryption mode when using the PoolWriter
authorDominik Csapak <d.csapak@proxmox.com>
Mon, 22 Jan 2024 11:50:34 +0000 (12:50 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 22 Jan 2024 13:43:20 +0000 (14:43 +0100)
by introducing an 'assert_encryption_mode' that checks the desired
state, and bails out if it's different, called directly where we
previously set the encryption mode (which is now done automatically)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
 [ TL: add drive_ prefix and fleece in comment ]
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
pbs-tape/src/sg_tape.rs
pbs-tape/src/sg_tape/encryption.rs
src/tape/drive/lto/mod.rs
src/tape/drive/mod.rs
src/tape/pool_writer/mod.rs

index bc0c14f0366667c6828dbd56dc69ca25f3515622..44f30fdb26a141a205b5290d70aa486bb775129e 100644 (file)
@@ -9,7 +9,7 @@ use endian_trait::Endian;
 use nix::fcntl::{fcntl, FcntlArg, OFlag};
 
 mod encryption;
-pub use encryption::{drive_set_encryption, has_encryption};
+pub use encryption::{drive_set_encryption, drive_get_encryption, has_encryption};
 
 mod volume_statistics;
 use proxmox_uuid::Uuid;
index 59648954d6e1d456f7d5ae3373afc1b2978eeca6..d460c2f768bf9b4b78cacfa5742ed6df2acec8df 100644 (file)
@@ -57,6 +57,27 @@ pub fn drive_set_encryption<F: AsRawFd>(file: &mut F, key: Option<[u8; 32]>) ->
     bail!("got unexpected encryption mode {:?}", status.mode);
 }
 
+/// Returns if encryption is enabled on the drive
+pub fn drive_get_encryption<F: AsRawFd>(file: &mut F) -> Result<bool, Error> {
+    let data = match sg_spin_data_encryption_status(file) {
+        Ok(data) => data,
+        Err(_) => {
+            // Assume device does not support HW encryption
+            return Ok(false);
+        }
+    };
+    let status = decode_spin_data_encryption_status(&data)?;
+    match status.mode {
+        // these three below have all encryption enabled, and only differ in how decryption is
+        // handled
+        DataEncryptionMode::On => Ok(true),
+        DataEncryptionMode::Mixed => Ok(true),
+        DataEncryptionMode::RawRead => Ok(true),
+        // currently, the mode below is the only one that has encryption actually disabled
+        DataEncryptionMode::Off => Ok(false),
+    }
+}
+
 #[derive(Endian)]
 #[repr(C, packed)]
 struct SspSetDataEncryptionPage {
@@ -187,7 +208,7 @@ fn sg_spin_data_encryption_caps<F: AsRawFd>(file: &mut F) -> Result<Vec<u8>, Err
         .map(|v| v.to_vec())
 }
 
-#[derive(Debug)]
+#[derive(Debug, PartialEq, Eq)]
 enum DataEncryptionMode {
     On,
     Mixed,
index c30b2c94f0d30f4581696c24b757d050762c9594..d1070d03f51c2aa97613a40dd4179deec9b198eb 100644 (file)
@@ -16,6 +16,7 @@ use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
 
 use anyhow::{bail, format_err, Error};
 
+use pbs_tape::sg_tape::drive_get_encryption;
 use proxmox_uuid::Uuid;
 
 use pbs_api_types::{
@@ -311,6 +312,14 @@ impl TapeDriver for LtoTapeHandle {
             self.sg_tape.set_encryption(None)
         }
     }
+
+    fn assert_encryption_mode(&mut self, encryption_wanted: bool) -> Result<(), Error> {
+        let encryption_set = drive_get_encryption(self.sg_tape.file_mut())?;
+        if encryption_wanted != encryption_set {
+            bail!("Set encryption mode not what was desired (set: {encryption_set}, wanted: {encryption_wanted})");
+        }
+        Ok(())
+    }
 }
 
 fn run_sg_tape_cmd(subcmd: &str, args: &[&str], fd: RawFd) -> Result<String, Error> {
index 90be5f9ed270e988c15ed89357ea993e769f2873..73886f14020e4ee7ab696c049721f635b4960145 100644 (file)
@@ -224,6 +224,14 @@ pub trait TapeDriver {
         }
         Ok(())
     }
+
+    /// Asserts that the encryption mode is set to the given value
+    fn assert_encryption_mode(&mut self, encryption_wanted: bool) -> Result<(), Error> {
+        if encryption_wanted {
+            bail!("drive does not support encryption");
+        }
+        Ok(())
+    }
 }
 
 /// A boxed implementor of [`MediaChange`].
index 6fd4f72af0638baf207927b2aacdf5f0e9114318..f1224bdd6eed728ec2c37f3dc63c69e13e7b73a7 100644 (file)
@@ -270,6 +270,10 @@ impl PoolWriter {
 
         self.catalog_set.lock().unwrap().append_catalog(catalog)?;
 
+        let media_set = media.media_set_label().unwrap();
+
+        drive.assert_encryption_mode(media_set.encryption_key_fingerprint.is_some())?;
+
         self.status = Some(PoolWriterState {
             drive,
             media_uuid: media_uuid.clone(),