From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 399356BF53 for ; Thu, 28 Jan 2021 15:11:38 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 297DD2E226 for ; Thu, 28 Jan 2021 15:11:38 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 907FC2E21B for ; Thu, 28 Jan 2021 15:11:21 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 432534612C for ; Thu, 28 Jan 2021 15:11:21 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 28 Jan 2021 15:11:20 +0100 Message-Id: <20210128141120.6955-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.244 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup] tape/drive/linux_tape: fix and refactor usage of sg-tape-cmd X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jan 2021 14:11:38 -0000 when executing this code as non-root, we use sg-tape-cmd (a setuid binary) to execute various ioctls on the tape device we give the command the open tape device fd as stdin, but did not dup it, so the std::process:Stdio handle closed it on drop, which let subsequent operation on that file fail (since it was closed) fix it by dup'ing it before giving it to the command, and also refactor the calling code, so that we do not forget to do this Signed-off-by: Dominik Csapak --- src/tape/drive/linux_tape.rs | 52 ++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/tape/drive/linux_tape.rs b/src/tape/drive/linux_tape.rs index a41b3183..e3c4918c 100644 --- a/src/tape/drive/linux_tape.rs +++ b/src/tape/drive/linux_tape.rs @@ -2,7 +2,7 @@ use std::fs::{OpenOptions, File}; use std::os::unix::fs::OpenOptionsExt; -use std::os::unix::io::{AsRawFd, FromRawFd}; +use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::convert::TryFrom; use anyhow::{bail, format_err, Error}; @@ -51,6 +51,17 @@ use crate::{ } }; +fn run_sg_tape_cmd(subcmd: &str, args: &[&str], fd: RawFd) -> Result { + let mut command = std::process::Command::new( + "/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd"); + command.args(&[subcmd]); + command.args(&["--stdin"]); + command.args(args); + let device_fd = nix::unistd::dup(fd)?; + command.stdin(unsafe { std::process::Stdio::from_raw_fd(device_fd)}); + run_command(command, None) +} + /// Linux tape drive status #[derive(Debug)] pub struct LinuxDriveStatus { @@ -351,12 +362,7 @@ impl LinuxTapeHandle { return read_mam_attributes(&mut self.file); } - let mut command = std::process::Command::new( - "/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd"); - command.args(&["cartridge-memory"]); - command.args(&["--stdin"]); - command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())}); - let output = run_command(command, None)?; + let output = run_sg_tape_cmd("cartridge-memory", &[], self.file.as_raw_fd())?; let result: Result, String> = serde_json::from_str(&output)?; result.map_err(|err| format_err!("{}", err)) } @@ -371,12 +377,7 @@ impl LinuxTapeHandle { return read_volume_statistics(&mut self.file); } - let mut command = std::process::Command::new( - "/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd"); - command.args(&["volume-statistics"]); - command.args(&["--stdin"]); - command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())}); - let output = run_command(command, None)?; + let output = run_sg_tape_cmd("volume-statistics", &[], self.file.as_raw_fd())?; let result: Result = serde_json::from_str(&output)?; result.map_err(|err| format_err!("{}", err)) } @@ -533,12 +534,7 @@ impl TapeDriver for LinuxTapeHandle { return read_tape_alert_flags(&mut self.file); } - let mut command = std::process::Command::new( - "/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd"); - command.args(&["tape-alert-flags"]); - command.args(&["--stdin"]); - command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())}); - let output = run_command(command, None)?; + let output = run_sg_tape_cmd("tape-alert-flags", &[], self.file.as_raw_fd())?; let result: Result = serde_json::from_str(&output)?; result .map_err(|err| format_err!("{}", err)) @@ -585,17 +581,15 @@ impl TapeDriver for LinuxTapeHandle { } } - let mut command = std::process::Command::new( - "/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd"); - command.args(&["encryption"]); - if let Some((fingerprint, uuid)) = key_fingerprint { + let output = if let Some((fingerprint, uuid)) = key_fingerprint { let fingerprint = crate::tools::format::as_fingerprint(fingerprint.bytes()); - command.args(&["--fingerprint", &fingerprint]); - command.args(&["--uuid", &uuid.to_string()]); - } - command.args(&["--stdin"]); - command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())}); - let output = run_command(command, None)?; + run_sg_tape_cmd("encryption", &[ + "--fingerprint", &fingerprint, + "--uuid", &uuid.to_string(), + ], self.file.as_raw_fd())? + } else { + run_sg_tape_cmd("encryption", &[], self.file.as_raw_fd())? + }; let result: Result<(), String> = serde_json::from_str(&output)?; result.map_err(|err| format_err!("{}", err)) } -- 2.20.1