diff --git a/app/src/main/java/org/matrix/TEESimulator/pki/NativeCertGen.kt b/app/src/main/java/org/matrix/TEESimulator/pki/NativeCertGen.kt index 8d64def..2ba5cc1 100644 --- a/app/src/main/java/org/matrix/TEESimulator/pki/NativeCertGen.kt +++ b/app/src/main/java/org/matrix/TEESimulator/pki/NativeCertGen.kt @@ -84,10 +84,6 @@ object NativeCertGen { private external fun initLogging(verbose: Boolean, logDir: String): Boolean - private external fun dumpLogs(): String? - - fun dump(): String? = if (isAvailable) dumpLogs() else null - fun parseNativeResult(bytes: ByteArray): Pair> { val buf = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN) diff --git a/native-certgen/Cargo.lock b/native-certgen/Cargo.lock index e256cb3..efc0537 100644 --- a/native-certgen/Cargo.lock +++ b/native-certgen/Cargo.lock @@ -2,12 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "adler2" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" - [[package]] name = "aho-corasick" version = "1.1.4" @@ -23,15 +17,6 @@ version = "1.0.102" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" -[[package]] -name = "arbitrary" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" -dependencies = [ - "derive_arbitrary", -] - [[package]] name = "autocfg" version = "1.5.0" @@ -53,12 +38,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "bumpalo" -version = "3.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" - [[package]] name = "bytes" version = "1.11.1" @@ -83,7 +62,6 @@ dependencies = [ "const-oid", "der", "jni", - "libc", "pkcs8", "rand", "ring", @@ -93,7 +71,6 @@ dependencies = [ "tracing", "tracing-subscriber", "x509-cert", - "zip", ] [[package]] @@ -133,21 +110,6 @@ dependencies = [ "libc", ] -[[package]] -name = "crc32fast" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - [[package]] name = "crypto-common" version = "0.1.7" @@ -191,17 +153,6 @@ dependencies = [ "powerfmt", ] -[[package]] -name = "derive_arbitrary" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e567bd82dcff979e4b03460c307b3cdc9e96fde3d73bed1496d2bc75d9dd62a" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "digest" version = "0.10.7" @@ -213,23 +164,6 @@ dependencies = [ "crypto-common", ] -[[package]] -name = "displaydoc" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - [[package]] name = "find-msvc-tools" version = "0.1.9" @@ -242,16 +176,6 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7ac824320a75a52197e8f2d787f6a38b6718bb6897a35142d749af3c0e8f4fe" -[[package]] -name = "flate2" -version = "1.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" -dependencies = [ - "crc32fast", - "miniz_oxide", -] - [[package]] name = "generic-array" version = "0.14.7" @@ -273,22 +197,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "hashbrown" -version = "0.16.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" - -[[package]] -name = "indexmap" -version = "2.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" -dependencies = [ - "equivalent", - "hashbrown", -] - [[package]] name = "itoa" version = "1.0.17" @@ -306,7 +214,7 @@ dependencies = [ "combine", "jni-sys", "log", - "thiserror 1.0.69", + "thiserror", "walkdir", "windows-sys 0.45.0", ] @@ -359,16 +267,6 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" -[[package]] -name = "miniz_oxide" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" -dependencies = [ - "adler2", - "simd-adler32", -] - [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -674,12 +572,6 @@ dependencies = [ "rand_core", ] -[[package]] -name = "simd-adler32" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2" - [[package]] name = "smallvec" version = "1.15.1" @@ -725,16 +617,7 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl 1.0.69", -] - -[[package]] -name = "thiserror" -version = "2.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" -dependencies = [ - "thiserror-impl 2.0.18", + "thiserror-impl", ] [[package]] @@ -748,17 +631,6 @@ dependencies = [ "syn", ] -[[package]] -name = "thiserror-impl" -version = "2.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "thread_local" version = "1.1.9" @@ -1130,37 +1002,8 @@ dependencies = [ "syn", ] -[[package]] -name = "zip" -version = "2.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabe6324e908f85a1c52063ce7aa26b68dcb7eb6dbc83a2d148403c9bc3eba50" -dependencies = [ - "arbitrary", - "crc32fast", - "crossbeam-utils", - "displaydoc", - "flate2", - "indexmap", - "memchr", - "thiserror 2.0.18", - "zopfli", -] - [[package]] name = "zmij" version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" - -[[package]] -name = "zopfli" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249" -dependencies = [ - "bumpalo", - "crc32fast", - "log", - "simd-adler32", -] diff --git a/native-certgen/Cargo.toml b/native-certgen/Cargo.toml index 484efb1..2e7e4af 100644 --- a/native-certgen/Cargo.toml +++ b/native-certgen/Cargo.toml @@ -20,8 +20,6 @@ time = { version = "0.3", features = ["std"] } anyhow = "1.0" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } -libc = "0.2" -zip = { version = "2.2", default-features = false, features = ["deflate"] } serde_json = "1.0" [profile.release] diff --git a/native-certgen/src/lib.rs b/native-certgen/src/lib.rs index b61fdca..0c20b4e 100644 --- a/native-certgen/src/lib.rs +++ b/native-certgen/src/lib.rs @@ -9,7 +9,7 @@ pub mod certbuilder; pub mod logging; use jni::objects::{JByteArray, JClass, JIntArray, JObject, JString}; -use jni::sys::{jboolean, jbyteArray, jstring}; +use jni::sys::{jboolean, jbyteArray}; use jni::JNIEnv; use crate::error::{CertGenError, Result}; @@ -140,44 +140,6 @@ fn init_logging_inner(env: &mut JNIEnv, verbose: jboolean, log_dir: &JString) -> Ok(()) } -// --------------------------------------------------------------------------- -// JNI entry: dumpLogs -// --------------------------------------------------------------------------- - -#[no_mangle] -pub extern "system" fn Java_org_matrix_TEESimulator_pki_NativeCertGen_dumpLogs( - mut env: JNIEnv, - _class: JClass, -) -> jstring { - let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - dump_logs_inner(&mut env) - })); - - match result { - Ok(Ok(raw)) => raw, - Ok(Err(e)) => { - tracing::error!(%e, "dumpLogs failed"); - std::ptr::null_mut() - } - Err(_) => { - tracing::error!("dumpLogs panicked"); - std::ptr::null_mut() - } - } -} - -fn dump_logs_inner(env: &mut JNIEnv) -> Result { - logging::dump::execute_dump() - .map_err(|e| CertGenError::Jni(format!("dump failed: {e}")))?; - - // Read the dump path written by execute_dump - let path = std::fs::read_to_string("/data/adb/tricky_store/.dump_path") - .map_err(|e| CertGenError::Jni(format!("read dump path: {e}")))?; - - let jpath = env.new_string(&path)?; - Ok(jpath.into_raw()) -} - // --------------------------------------------------------------------------- // Config extraction from Java CertGenConfig object // --------------------------------------------------------------------------- diff --git a/native-certgen/src/logging/dump.rs b/native-certgen/src/logging/dump.rs deleted file mode 100644 index 95cc621..0000000 --- a/native-certgen/src/logging/dump.rs +++ /dev/null @@ -1,208 +0,0 @@ -use std::fs::{self, File}; -use std::io::{Read, Write}; -use std::path::Path; -use std::process::Command; -use std::time::{SystemTime, UNIX_EPOCH}; - -const DUMP_DIR: &str = "/sdcard/Download"; -const LOCK_PATH: &str = "/data/adb/tricky_store/.dump_lock"; -const DUMP_PATH_FILE: &str = "/data/adb/tricky_store/.dump_path"; -const LOG_DIR: &str = "/data/adb/tricky_store/logs"; -const BASE_DIR: &str = "/data/adb/tricky_store"; -const LOGCAT_SIZE_LIMIT: usize = 2 * 1024 * 1024; - -struct FlockGuard { - _file: File, -} - -impl FlockGuard { - fn acquire() -> Result> { - if let Some(parent) = Path::new(LOCK_PATH).parent() { - fs::create_dir_all(parent)?; - } - let file = File::create(LOCK_PATH)?; - let fd = { - use std::os::unix::io::AsRawFd; - file.as_raw_fd() - }; - let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) }; - if ret != 0 { - return Err("dump already in progress".into()); - } - Ok(Self { _file: file }) - } -} - -impl Drop for FlockGuard { - fn drop(&mut self) { - // flock released automatically when file descriptor closes - } -} - -fn random_name(len: usize) -> String { - use rand::Rng; - let mut rng = rand::thread_rng(); - (0..len) - .map(|_| { - let idx = rng.gen_range(0..36u8); - if idx < 10 { - (b'0' + idx) as char - } else { - (b'a' + idx - 10) as char - } - }) - .collect() -} - -fn collect_logcat(tag: &str) -> Vec { - let output = Command::new("logcat") - .args(["-d", "-s", tag]) - .output(); - - match output { - Ok(o) => { - let mut data = o.stdout; - data.truncate(LOGCAT_SIZE_LIMIT); - data - } - Err(_) => Vec::new(), - } -} - -fn collect_device_info() -> String { - let mut info = String::new(); - - if let Ok(output) = Command::new("uname").arg("-a").output() { - info.push_str(&format!( - "uname={}\n", - String::from_utf8_lossy(&output.stdout).trim() - )); - } - - for (key, prop) in [ - ("device", "ro.product.device"), - ("build", "ro.build.display.id"), - ("android", "ro.build.version.release"), - ] { - if let Ok(output) = Command::new("getprop").arg(prop).output() { - info.push_str(&format!( - "{}={}\n", - key, - String::from_utf8_lossy(&output.stdout).trim() - )); - } - } - - // KSU version - if let Ok(ver) = fs::read_to_string("/data/adb/ksu/version") { - info.push_str(&format!("ksu={}\n", ver.trim())); - } - - // Module version from module.prop - if let Ok(prop) = fs::read_to_string("/data/adb/modules/tricky_store/module.prop") { - for line in prop.lines() { - if let Some(ver) = line.strip_prefix("version=") { - info.push_str(&format!("module={}\n", ver.trim())); - break; - } - } - } - - info -} - -fn read_file_bytes(path: &str) -> Option> { - let mut buf = Vec::new(); - File::open(path).ok()?.read_to_end(&mut buf).ok()?; - Some(buf) -} - -fn epoch_millis() -> u64 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|d| d.as_millis() as u64) - .unwrap_or(0) -} - -pub fn execute_dump() -> Result<(), Box> { - let _lock = FlockGuard::acquire()?; - - let _ = fs::create_dir_all(DUMP_DIR); - let zip_name = format!("{}.zip", random_name(8)); - let zip_path = format!("{}/{}", DUMP_DIR, zip_name); - - let zip_file = File::create(&zip_path)?; - let mut zip = zip::ZipWriter::new(zip_file); - let options = - zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); - - let mut file_count = 0u32; - - // Log files - let log_files = [ - "certgen.log", - "certgen.log.1", - "certgen.log.2", - "certgen.log.3", - "certgen.log.4", - ]; - for name in &log_files { - let path = format!("{}/{}", LOG_DIR, name); - if let Some(data) = read_file_bytes(&path) { - zip.start_file(*name, options)?; - zip.write_all(&data)?; - file_count += 1; - } - } - - // Logcat - let logcat = collect_logcat("TEESimulator"); - if !logcat.is_empty() { - zip.start_file("logcat-teesimulator.log", options)?; - zip.write_all(&logcat)?; - file_count += 1; - } - - // Config files - for name in ["tee_status.txt", "security_patch.txt"] { - let path = format!("{}/{}", BASE_DIR, name); - if let Some(data) = read_file_bytes(&path) { - zip.start_file(name, options)?; - zip.write_all(&data)?; - file_count += 1; - } - } - - // Device info - let device_info = collect_device_info(); - if !device_info.is_empty() { - zip.start_file("device-info.txt", options)?; - zip.write_all(device_info.as_bytes())?; - file_count += 1; - } - - // Manifest - let manifest = serde_json::json!({ - "timestamp": epoch_millis(), - "version": env!("CARGO_PKG_VERSION"), - "files": file_count, - }); - zip.start_file("manifest.json", options)?; - zip.write_all(manifest.to_string().as_bytes())?; - - zip.finish()?; - - let zip_size = fs::metadata(&zip_path).map(|m| m.len()).unwrap_or(0); - fs::write(DUMP_PATH_FILE, &zip_path)?; - - let result = serde_json::json!({ - "zip": zip_path, - "size": zip_size, - "files": file_count + 1, // +1 for manifest - }); - println!("{}", result); - - tracing::info!(path = %zip_path, size = zip_size, "diagnostic dump created"); - - Ok(()) -} diff --git a/native-certgen/src/logging/mod.rs b/native-certgen/src/logging/mod.rs index c4e43b2..ea31395 100644 --- a/native-certgen/src/logging/mod.rs +++ b/native-certgen/src/logging/mod.rs @@ -1,7 +1,5 @@ mod kmsg; mod rotating; -pub mod sysfs; -pub mod dump; use std::path::Path; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; diff --git a/native-certgen/src/logging/sysfs.rs b/native-certgen/src/logging/sysfs.rs deleted file mode 100644 index a96307c..0000000 --- a/native-certgen/src/logging/sysfs.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::fs; -use std::path::Path; - -const VERBOSE_MARKER: &str = "/data/adb/tricky_store/.verbose"; - -pub fn is_verbose() -> bool { - Path::new(VERBOSE_MARKER).exists() -} - -pub fn set_verbose_marker(enabled: bool) -> Result<(), Box> { - if enabled { - if let Some(parent) = Path::new(VERBOSE_MARKER).parent() { - fs::create_dir_all(parent)?; - } - fs::write(VERBOSE_MARKER, "")?; - } else if Path::new(VERBOSE_MARKER).exists() { - fs::remove_file(VERBOSE_MARKER)?; - } - Ok(()) -} - -pub fn enable() -> Result<(), Box> { - set_verbose_marker(true)?; - tracing::info!("verbose logging enabled via marker file"); - Ok(()) -} - -pub fn disable() -> Result<(), Box> { - set_verbose_marker(false)?; - tracing::info!("verbose logging disabled, marker file removed"); - Ok(()) -} - -pub fn status() -> Result<(), Box> { - let state = if is_verbose() { "enabled" } else { "disabled" }; - tracing::info!(verbose = state, "verbose marker status"); - Ok(()) -}