From 0b89652c6a0056a4052119f3c83a6896e3af17a2 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 20 Jul 2026 21:28:03 +0100 Subject: [PATCH] Add fsetxattr safe rule --- rule-preprocessor/src/semantic.rs | 1 + rules/Cargo.toml | 1 + rules/src/modules.rs | 2 ++ rules/xattr/tgt_refcount.rs | 32 +++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 rules/xattr/tgt_refcount.rs diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 44abc1b6..c7e44a18 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -53,6 +53,7 @@ fn build_rustc_args(crate_root: &Path) -> Vec { "rustls_ffi", "nix", "jiff", + "xattr", ] { if let Some(rlib) = find_rlib(deps.as_path(), dep) { args.push("--extern".to_string()); diff --git a/rules/Cargo.toml b/rules/Cargo.toml index 0c8d3841..0efe0e4a 100644 --- a/rules/Cargo.toml +++ b/rules/Cargo.toml @@ -15,3 +15,4 @@ brotli-sys = "0.3" rustls-ffi = { version = "0.15.3", default-features = false } nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] } jiff = "0.2" +xattr = "1" diff --git a/rules/src/modules.rs b/rules/src/modules.rs index f2260e69..f4404a32 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -162,5 +162,7 @@ pub mod unistd_tgt_unsafe; pub mod vector_tgt_refcount; #[path = r#"../vector/tgt_unsafe.rs"#] pub mod vector_tgt_unsafe; +#[path = r#"../xattr/tgt_refcount.rs"#] +pub mod xattr_tgt_refcount; #[path = r#"../xattr/tgt_unsafe.rs"#] pub mod xattr_tgt_unsafe; diff --git a/rules/xattr/tgt_refcount.rs b/rules/xattr/tgt_refcount.rs new file mode 100644 index 00000000..8447db58 --- /dev/null +++ b/rules/xattr/tgt_refcount.rs @@ -0,0 +1,32 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +#[cfg(target_os = "linux")] +fn f1(a0: i32, a1: Ptr, a2: AnyPtr, a3: usize, a4: i32) -> i32 { + match a4 { + 0 => {} + __f => panic!("fsetxattr: unsupported flags {}", __f), + } + let __name = a1.to_rust_string(); + match FdRegistry::with_fd(a0, |__fd| nix::unistd::dup(__fd)) { + Ok(__dup) => { + let __file = ::std::fs::File::from(__dup); + match a2 + .reinterpret_cast::() + .with_slice(a3, |__v| xattr::FileExt::set_xattr(&__file, &__name, __v)) + { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO)); + -1 + } + } + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } +}