From d6c65dc1db188ebc3d8ce27f779aa46850b47611 Mon Sep 17 00:00:00 2001 From: NikitaShyrei Date: Wed, 6 Mar 2024 18:29:53 +0100 Subject: [PATCH] - fin --- Cargo.lock | 26 ++------------- intel-sgx/dcap-ql/src/bindings/mod.rs | 5 +-- intel-sgx/enclave-runner/src/loader.rs | 7 ++-- .../src/bin/ftxsgx-elf2sgxs.rs | 17 +++++----- intel-sgx/sgxs-tools/Cargo.toml | 4 +-- intel-sgx/sgxs-tools/src/bin/sgxs-append.rs | 13 ++++---- .../sgxs-tools/src/sgx_detect/imp/linux.rs | 4 +-- intel-sgx/sgxs-tools/src/sgx_detect/main.rs | 32 +++++++++---------- .../sgxs-tools/src/sgx_detect/tests/mod.rs | 3 +- 9 files changed, 45 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 21104840..5d72a9bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -924,28 +924,6 @@ dependencies = [ "synstructure", ] -[[package]] -name = "failure" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" -dependencies = [ - "backtrace", - "failure_derive", -] - -[[package]] -name = "failure_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" -dependencies = [ - "proc-macro2 1.0.78", - "quote 1.0.35", - "syn 1.0.81", - "synstructure", -] - [[package]] name = "fake-simd" version = "0.1.2" @@ -3206,6 +3184,7 @@ name = "sgxs-tools" version = "0.8.6" dependencies = [ "aesm-client", + "anyhow", "atty", "byteorder 1.3.4", "clap", @@ -3213,8 +3192,6 @@ dependencies = [ "dcap-ql", "enclave-runner", "env_logger 0.6.2", - "failure", - "failure_derive", "fnv", "lazy_static", "log 0.4.14", @@ -3235,6 +3212,7 @@ dependencies = [ "sgxs", "sgxs-loaders", "syn 0.15.44", + "thiserror", "winapi", "yansi", ] diff --git a/intel-sgx/dcap-ql/src/bindings/mod.rs b/intel-sgx/dcap-ql/src/bindings/mod.rs index ab4bb9ec..0138c12e 100644 --- a/intel-sgx/dcap-ql/src/bindings/mod.rs +++ b/intel-sgx/dcap-ql/src/bindings/mod.rs @@ -4,7 +4,8 @@ extern crate libc; extern crate sgxs_loaders; -use failure::Error; +use anyhow::Error; +use anyhow::anyhow; use num_traits::FromPrimitive; pub use self::dcap_ql_sys::Quote3Error; @@ -76,7 +77,7 @@ pub fn is_loaded() -> bool { /// Since DCAP is being used, assume that no EINITTOKEN provider is necessary. pub fn enclave_loader() -> Result { #[cfg(not(feature = "link"))] - dl::load().map_err(failure::err_msg)?; + dl::load().map_err(|e| anyhow!(e))?; // NB. libsgx_dcap_ql.so.1 transitively links to libsgx_enclave_common.so.1 // so we should be able to find it already loaded. // We can't use the library from `mod dl` if `not(feature = "link")`, diff --git a/intel-sgx/enclave-runner/src/loader.rs b/intel-sgx/enclave-runner/src/loader.rs index b25a203c..daca85e3 100644 --- a/intel-sgx/enclave-runner/src/loader.rs +++ b/intel-sgx/enclave-runner/src/loader.rs @@ -11,8 +11,7 @@ use std::path::Path; use std::{arch, str}; use thiserror::Error as ThisError; -use anyhow::Context; -use anyhow::anyhow; +use anyhow::{Context, format_err}; #[cfg(feature = "crypto-openssl")] use openssl::{ @@ -167,7 +166,7 @@ impl<'a> EnclaveBuilder<'a> { let mut enclave = self.enclave.try_clone().unwrap(); let hash = match self.hash_enclave.take() { Some(f) => f(&mut enclave)?, - None => return Err(anyhow!("either compile with default features or use with_dummy_signature_signer()")) + None => return Err(format_err!("either compile with default features or use with_dummy_signature_signer()")) }; let mut signer = Signer::new(hash); @@ -185,7 +184,7 @@ impl<'a> EnclaveBuilder<'a> { match self.load_and_sign.take() { Some(f) => f(signer), - None => Err(anyhow!("either compile with default features or use with_dummy_signature_signer()")) + None => Err(format_err!("either compile with default features or use with_dummy_signature_signer()")) } } diff --git a/intel-sgx/fortanix-sgx-tools/src/bin/ftxsgx-elf2sgxs.rs b/intel-sgx/fortanix-sgx-tools/src/bin/ftxsgx-elf2sgxs.rs index 290ef81c..846ec0d5 100644 --- a/intel-sgx/fortanix-sgx-tools/src/bin/ftxsgx-elf2sgxs.rs +++ b/intel-sgx/fortanix-sgx-tools/src/bin/ftxsgx-elf2sgxs.rs @@ -20,6 +20,7 @@ use std::num::ParseIntError; use std::path::{Path, PathBuf}; use crate::anyhow::Context; +use anyhow::anyhow; use xmas_elf::dynamic::{Dynamic as DynEntry, Tag as DynTag}; use xmas_elf::header::Class as HeaderClass; @@ -127,13 +128,13 @@ macro_rules! read_syms { $(let mut $optional_name=None;)* for sym in $syms.iter().skip(1) { if sym.shndx()==SHN_UNDEF { - bail!("Found undefined dynamic symbol: {}", sym.get_name(&$elf).map_err(err_msg)?); - } $(else if sym.get_name(&$elf).map_err(err_msg)?==stringify!($mandatory_name) { + bail!("Found undefined dynamic symbol: {}", sym.get_name(&$elf).map_err(|e| anyhow!(e))?); + } $(else if sym.get_name(&$elf).map_err(|e| anyhow!(e))?==stringify!($mandatory_name) { if replace(&mut $mandatory_name,Some(sym)).is_some() { bail!("Found symbol twice: {}", stringify!($mandatory_name)); } })* - $(else if sym.get_name(&$elf).map_err(err_msg)?==stringify!($optional_name) { + $(else if sym.get_name(&$elf).map_err(|e| anyhow!(e))?==stringify!($optional_name) { if replace(&mut $optional_name,Some(sym)).is_some() { bail!("Found symbol twice: {}", stringify!($optional_name)); } @@ -222,7 +223,7 @@ impl<'a> LayoutInfo<'a> { .ok_or_else(|| format_err!("Could not find dynamic symbol table!"))?; let syms = - if let SectionData::DynSymbolTable64(syms) = dynsym.get_data(&elf).map_err(err_msg)? { + if let SectionData::DynSymbolTable64(syms) = dynsym.get_data(&elf).map_err(|e| anyhow!(e))? { syms } else { bail!(".dynsym section is not a dynamic symbol table!"); @@ -290,7 +291,7 @@ impl<'a> LayoutInfo<'a> { .find(|ph| ph.get_type() == Ok(PhType::Dynamic)) .ok_or_else(|| format_err!("Could not found dynamic section!"))?; - let dyns = if let SegmentData::Dynamic64(dyns) = dynh.get_data(&elf).map_err(err_msg)? { + let dyns = if let SegmentData::Dynamic64(dyns) = dynh.get_data(&elf).map_err(|e| anyhow!(e))? { dyns } else { bail!("PT_DYNAMIC segment is not a dynamic section!") @@ -300,7 +301,7 @@ impl<'a> LayoutInfo<'a> { let mut relacount = None; for dynamic in dyns { - match dynamic.get_tag().map_err(err_msg)? { + match dynamic.get_tag().map_err(|e| anyhow!(e))? { // Some entries for PLT/GOT checking are currently // commented out. I *think* that if there were an actual // PLT/GOT problem, that would be caught by the remaining @@ -347,7 +348,7 @@ impl<'a> LayoutInfo<'a> { let mut count = 0; for section in elf.section_iter() { - if let SectionData::Rela64(relas) = section.get_data(&elf).map_err(err_msg)? { + if let SectionData::Rela64(relas) = section.get_data(&elf).map_err(|e| anyhow!(e))? { count += relas.len(); for rela in relas { let shind = rela.get_symbol_table_index(); @@ -501,7 +502,7 @@ impl<'a> LayoutInfo<'a> { let base = start & !0xfff; let mut end = start + ph.mem_size(); let base_data; - if let SegmentData::Undefined(data) = ph.get_data(&self.elf).map_err(err_msg)? { + if let SegmentData::Undefined(data) = ph.get_data(&self.elf).map_err(|e| anyhow!(e))? { base_data = data; } else { // Reachable if xmas-elf changes definition of SegmentData diff --git a/intel-sgx/sgxs-tools/Cargo.toml b/intel-sgx/sgxs-tools/Cargo.toml index 798b2d38..ce369219 100644 --- a/intel-sgx/sgxs-tools/Cargo.toml +++ b/intel-sgx/sgxs-tools/Cargo.toml @@ -40,8 +40,8 @@ regex = "1" # MIT/Apache-2.0 num = "0.2" # MIT/Apache-2.0 byteorder = "1.1.0" # Unlicense/MIT openssl = "0.10" # Apache-2.0 -failure = "0.1.1" # MIT/Apache-2.0 -failure_derive = "0.1.1" # MIT/Apache-2.0 +anyhow = "1.0" # MIT/Apache-2.0 +thiserror = "1.0" # MIT/Apache-2.0 crypto-hash = "0.3" # MIT log = "0.4" # MIT/Apache-2.0 env_logger = "0.6" # MIT/Apache-2.0 diff --git a/intel-sgx/sgxs-tools/src/bin/sgxs-append.rs b/intel-sgx/sgxs-tools/src/bin/sgxs-append.rs index 3c6badb3..0cfecd6c 100644 --- a/intel-sgx/sgxs-tools/src/bin/sgxs-append.rs +++ b/intel-sgx/sgxs-tools/src/bin/sgxs-append.rs @@ -7,10 +7,8 @@ extern crate byteorder; extern crate sgx_isa; extern crate sgxs as sgxs_crate; -#[macro_use] -extern crate failure; -#[macro_use] -extern crate failure_derive; +extern crate anyhow; +extern crate thiserror; use std::borrow::Cow; use std::cell::RefCell; @@ -22,16 +20,17 @@ use std::ops::{Deref, DerefMut}; use std::rc::Rc; use byteorder::{LittleEndian, WriteBytesExt}; -use failure::{Error, ResultExt}; use sgx_isa::{PageType, SecinfoFlags}; use crate::sgxs_crate::sgxs::{ CanonicalSgxsReader, Meas, PageChunk, SecinfoTruncated, SgxsRead, SgxsWrite, }; use crate::sgxs_crate::util::size_fit_natural; +use anyhow::{Context, Error, bail, format_err}; +use thiserror::Error as ThisError; -#[derive(Debug, Fail)] -#[fail(display = "Usage error")] +#[derive(Debug, ThisError)] +#[error("Usage error")] struct UsageError(Cow<'static, str>); struct NamedFile { diff --git a/intel-sgx/sgxs-tools/src/sgx_detect/imp/linux.rs b/intel-sgx/sgxs-tools/src/sgx_detect/imp/linux.rs index 79b4f18c..1ba225cb 100644 --- a/intel-sgx/sgxs-tools/src/sgx_detect/imp/linux.rs +++ b/intel-sgx/sgxs-tools/src/sgx_detect/imp/linux.rs @@ -7,7 +7,7 @@ use std::path::PathBuf; use std::process::Command; use byteorder::{ReadBytesExt, LE}; -use failure::{Error, Fail, ResultExt}; +use anyhow::{bail, Error, Context, anyhow}; use crate::DetectError; use crate::interpret::{AesmStatus, KmodStatus}; @@ -41,7 +41,7 @@ pub fn rdmsr(address: u64) -> Result { modprobe_msr().context("Failed to load MSR kernel module")?; continue; } - Err(e) => bail!(e.context("Failed to open MSR device")), + Err(e) => bail!(anyhow!(e).context("Failed to open MSR device")), } } } diff --git a/intel-sgx/sgxs-tools/src/sgx_detect/main.rs b/intel-sgx/sgxs-tools/src/sgx_detect/main.rs index c152d2d6..52f46c2a 100644 --- a/intel-sgx/sgxs-tools/src/sgx_detect/main.rs +++ b/intel-sgx/sgxs-tools/src/sgx_detect/main.rs @@ -32,10 +32,9 @@ #[macro_use] extern crate log; -#[macro_use] -extern crate failure; -#[macro_use] -extern crate failure_derive; + +extern crate anyhow; +extern crate thiserror; #[macro_use] extern crate mopa; #[macro_use] @@ -52,7 +51,6 @@ use std::rc::Rc; use std::process::Command; use std::io::{self, BufRead, Error as IOError, ErrorKind}; use reqwest; -use failure::Error; use yansi::Paint; use aesm_client::AesmClient; use sgx_isa::{Sigstruct, Attributes, Einittoken}; @@ -63,6 +61,8 @@ use sgxs_loaders::isgx::Device as SgxDevice; use sgxs_loaders::enclaveapi::Sgx as SgxDevice; use sgxs_loaders::sgx_enclave_common::Library as EnclCommonLib; use proc_mounts::MountList; +use anyhow::{bail, Error, format_err}; +use thiserror::Error as ThisError; mod interpret; #[cfg(windows)] @@ -79,15 +79,15 @@ mod tests; use crate::interpret::*; use crate::tests::Tests; -#[derive(Debug, Fail)] +#[derive(Debug, ThisError)] enum DetectError { - #[fail(display = "CPUID leaf {:x}h is not valid", leaf)] + #[error("CPUID leaf {:x}h is not valid", leaf)] CpuidLeafInvalid { leaf: u32 }, - #[fail(display = "Failed access EFI variables")] - EfiFsError(#[cause] io::Error), - #[fail(display = "Failed to read EFI variable")] - EfiVariableError(#[cause] io::Error), - #[fail(display = "Not available when using JSON tests")] + #[error("Failed access EFI variables")] + EfiFsError(#[source] io::Error), + #[error("Failed to read EFI variable")] + EfiVariableError(#[source] io::Error), + #[error("Not available when using JSON tests")] NotAvailableInTest, } @@ -104,7 +104,7 @@ fn cpuid(eax: u32, ecx: u32) -> Result { mod detect_result { use std::rc::Rc; - use failure::{Error, err_msg}; + use anyhow::{Error, anyhow}; use serde::ser::{Serialize, Serializer}; use serde::de::{Deserialize, Deserializer}; @@ -115,7 +115,7 @@ mod detect_result { pub fn deserialize<'de, T: Deserialize<'de>, D: Deserializer<'de>>(deserializer: D) -> Result>, D::Error> { match Result::::deserialize(deserializer) { Ok(Ok(v)) => Ok(Ok(v)), - Ok(Err(e)) => Ok(Err(Rc::new(err_msg(e)))), + Ok(Err(e)) => Ok(Err(Rc::new(anyhow!(e)))), Err(e) => Err(e), } } @@ -168,7 +168,7 @@ struct FailTrace<'a>(pub &'a Error); impl<'a> fmt::Display for FailTrace<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{}", self.0)?; - for cause in self.0.iter_causes() { + for cause in self.0.chain() { write!(fmt, "\ncause: {}", cause)?; } Ok(()) @@ -267,7 +267,7 @@ impl SgxSupport { while let Some(p) = path.parent() { if let Some(mount_info) = mount_list.0.iter().find(|&x| x.dest == p) { if mount_info.options.iter().any(|o| o == "noexec") { - return Err(failure::format_err!("{:?} mounted with `noexec` option", mount_info.dest)); + return Err(format_err!("{:?} mounted with `noexec` option", mount_info.dest)); } } path = p; diff --git a/intel-sgx/sgxs-tools/src/sgx_detect/tests/mod.rs b/intel-sgx/sgxs-tools/src/sgx_detect/tests/mod.rs index f92bce0c..d62537f8 100644 --- a/intel-sgx/sgxs-tools/src/sgx_detect/tests/mod.rs +++ b/intel-sgx/sgxs-tools/src/sgx_detect/tests/mod.rs @@ -7,13 +7,14 @@ use std::io::ErrorKind; use std::io::Error as IoError; use std::process; -use failure::Error; +use anyhow::Error; use petgraph::visit::EdgeRef; use enclave_runner::EnclaveBuilder; use report_test::ReportBuilder; use sgx_isa::{Attributes, AttributesFlags, Miscselect, Sigstruct}; use sgxs::loader::Load; +use anyhow::format_err; mod debug; #[macro_use]