Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
92d3849
test: require private Brew cleanup audit authority
seonghobae Aug 12, 2026
d5ae26e
test: move Brew audit authority regression inside crate
seonghobae Aug 12, 2026
c46cad6
test: register crate-internal Brew audit authority regressions
seonghobae Aug 12, 2026
fc16c5b
test: remove external Brew audit regression harness
seonghobae Aug 12, 2026
8e676ea
security: bind Brew cleanup audit storage privately
seonghobae Aug 12, 2026
ac175c4
test: require directory-bound Brew audit publication
seonghobae Aug 12, 2026
5463267
build: expose libc for Unix audit authority
seonghobae Aug 12, 2026
9d7c06a
security: bind Brew audit publication to directory identity
seonghobae Aug 12, 2026
e266a60
test: prove Brew audit directory replacement fails closed
seonghobae Aug 12, 2026
aaba16f
test: require standard Intel Homebrew executable target
seonghobae Aug 13, 2026
b633e3e
revert: keep Intel Homebrew compatibility in a dedicated PR
seonghobae Aug 13, 2026
8d17338
Merge protected main into object-bound audit authority
seonghobae Aug 20, 2026
16c1060
test(security): reject symlinked Brew audit ancestors
seonghobae Aug 20, 2026
c3e8519
fix(security): reject Brew audit ancestor symlinks
seonghobae Aug 20, 2026
f9440c4
merge: converge immutable audit authority owner onto protected main
seonghobae Aug 20, 2026
9a3d29d
merge: converge immutable audit authority owner onto protected main
seonghobae Aug 20, 2026
eedee5e
security: bind private evidence publication to directory identity (#228)
seonghobae Aug 24, 2026
347f8d6
Merge remote-tracking branch 'origin/main' into pr-187
seonghobae Aug 29, 2026
fb274c2
style: format private audit authority
seonghobae Aug 29, 2026
fa1fead
merge: stack Brew audit security on release owner
seonghobae Sep 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 181 additions & 31 deletions src-tauri/src/brew_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,23 @@ pub fn execute(

const MAX_AUDIT_BYTES: usize = 128 * 1024;

fn validate_audit_parent_ancestors(app_data_dir: &Path, allow_missing: bool) -> Result<(), String> {
for ancestor in app_data_dir
.ancestors()
.filter(|ancestor| !ancestor.as_os_str().is_empty())
{
match std::fs::symlink_metadata(ancestor) {
Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_dir() => {
return Err("brew-cleanup-audit-parent-unsafe".into());
}
Ok(_) => {}
Err(error) if allow_missing && error.kind() == std::io::ErrorKind::NotFound => {}
Err(_) => return Err("brew-cleanup-audit-parent-unavailable".into()),
}
}
Ok(())
}

fn audit_directory(app_data_dir: &Path) -> Result<PathBuf, String> {
if !app_data_dir.is_absolute()
|| app_data_dir
Expand All @@ -478,16 +495,35 @@ fn audit_directory(app_data_dir: &Path) -> Result<PathBuf, String> {
{
return Err("brew-cleanup-audit-directory-invalid".into());
}
validate_audit_parent_ancestors(app_data_dir, true)?;
std::fs::create_dir_all(app_data_dir)
.map_err(|_| "brew-cleanup-audit-parent-create-failed".to_string())?;
validate_audit_parent_ancestors(app_data_dir, false)?;
let parent = std::fs::symlink_metadata(app_data_dir)
.map_err(|_| "brew-cleanup-audit-parent-unavailable".to_string())?;
if parent.file_type().is_symlink() || !parent.is_dir() {
return Err("brew-cleanup-audit-parent-unsafe".into());
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if parent.permissions().mode() & 0o022 != 0 {
return Err("brew-cleanup-audit-parent-writable-by-others".into());
}
}

let directory = app_data_dir.join("brew-cleanup-records");
std::fs::create_dir_all(&directory)
.map_err(|_| "brew-cleanup-audit-directory-create-failed".to_string())?;
let mut builder = std::fs::DirBuilder::new();
#[cfg(unix)]
{
use std::os::unix::fs::DirBuilderExt;
builder.mode(0o700);
}
match builder.create(&directory) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
Comment on lines +516 to +524

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Restrictive umasks suppress cleanup audits

With an owner-write-masking umask, builder.mode(0o700) creates a read-only audit directory. Every cleanup then completes without an audit record.

Prompt for agents
Make audit_directory normalize a newly created brew-cleanup-records directory to exact private, owner-writable permissions through an object-bound directory descriptor before record publication. DirBuilder mode is filtered by umask, so creation under umask 0200 currently yields mode 0500. Preserve the existing symlink, identity, and shared-writable checks, and add an isolated child-process restrictive-umask regression like private_evidence_umask.rs.
Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Err(_) => return Err("brew-cleanup-audit-directory-create-failed".into()),
}
let metadata = std::fs::symlink_metadata(&directory)
.map_err(|_| "brew-cleanup-audit-directory-unavailable".to_string())?;
if metadata.file_type().is_symlink() || !metadata.is_dir() {
Expand All @@ -496,59 +532,175 @@ fn audit_directory(app_data_dir: &Path) -> Result<PathBuf, String> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700))
.map_err(|_| "brew-cleanup-audit-directory-permissions-failed".to_string())?;
if metadata.permissions().mode() & 0o022 != 0 {
return Err("brew-cleanup-audit-directory-writable-by-others".into());
}
}
Ok(directory)
}

pub fn write_audit_record(
fn prepare_audit_record(
app_data_dir: &Path,
record: &BrewCleanupAuditRecord,
) -> Result<PathBuf, String> {
) -> Result<(PathBuf, String, PathBuf, Vec<u8>), String> {
let directory = audit_directory(app_data_dir)?;
let filename = format!(
"{:020}-{}-{}.json",
record.executed_at_ms, record.plan.plan_fingerprint, record.judgment_id
);
let path = directory.join(filename);
if filename.as_bytes().contains(&b'/') || filename == "." || filename == ".." {
return Err("brew-cleanup-audit-filename-invalid".into());
}
let path = directory.join(&filename);
let encoded = serde_json::to_vec_pretty(record)
.map_err(|_| "brew-cleanup-audit-serialization-failed".to_string())?;
if encoded.len() > MAX_AUDIT_BYTES {
return Err("brew-cleanup-audit-too-large".into());
}
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
Ok((directory, filename, path, encoded))
}

pub fn write_audit_record(
app_data_dir: &Path,
record: &BrewCleanupAuditRecord,
) -> Result<PathBuf, String> {
let (directory, filename, path, encoded) = prepare_audit_record(app_data_dir, record)?;
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
return write_audit_record_unix_with_hook(&directory, &filename, &path, &encoded, || {});
}
let mut file = options
.open(&path)
.map_err(|_| "brew-cleanup-audit-create-failed".to_string())?;
#[cfg(not(unix))]
{
let _ = (directory, filename, path, encoded);
Err("brew-cleanup-audit-platform-unsupported".into())
}
}

#[cfg(unix)]
fn write_audit_record_unix_with_hook<F>(
directory: &Path,
filename: &str,
path: &Path,
encoded: &[u8],
before_create: F,
) -> Result<PathBuf, String>
where
F: FnOnce(),
{
use std::ffi::CString;
use std::os::fd::{AsRawFd, FromRawFd};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::{MetadataExt, PermissionsExt};

let directory_name = CString::new(directory.as_os_str().as_bytes())
.map_err(|_| "brew-cleanup-audit-directory-invalid".to_string())?;
let directory_fd = unsafe {
libc::open(
directory_name.as_ptr(),
libc::O_RDONLY | libc::O_CLOEXEC | libc::O_DIRECTORY | libc::O_NOFOLLOW,
)
};
if directory_fd < 0 {
return Err("brew-cleanup-audit-directory-open-failed".into());
}
let directory_file = unsafe { std::fs::File::from_raw_fd(directory_fd) };
let opened_directory = directory_file
.metadata()
.map_err(|_| "brew-cleanup-audit-directory-metadata-failed".to_string())?;
let current_directory = std::fs::symlink_metadata(directory)
.map_err(|_| "brew-cleanup-audit-directory-identity-drift".to_string())?;
if !opened_directory.is_dir()
|| current_directory.file_type().is_symlink()
|| !current_directory.is_dir()
|| opened_directory.permissions().mode() & 0o022 != 0
|| current_directory.permissions().mode() & 0o022 != 0
|| opened_directory.dev() != current_directory.dev()
|| opened_directory.ino() != current_directory.ino()
{
return Err("brew-cleanup-audit-directory-identity-drift".into());
}

let record_name =
CString::new(filename).map_err(|_| "brew-cleanup-audit-filename-invalid".to_string())?;
before_create();
let record_fd = unsafe {
libc::openat(
directory_file.as_raw_fd(),
record_name.as_ptr(),
libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_CLOEXEC | libc::O_NOFOLLOW,
0o400,
)
};
if record_fd < 0 {
return Err("brew-cleanup-audit-create-failed".into());
}
let mut file = unsafe { std::fs::File::from_raw_fd(record_fd) };

let cleanup = || {
unsafe {
libc::unlinkat(directory_file.as_raw_fd(), record_name.as_ptr(), 0);
}
let _ = directory_file.sync_all();
};
Comment on lines +626 to +644

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Directory replacement cannot redirect records

Both openat and unlinkat use the admitted directory descriptor. Final identity checks reject a renamed directory without touching its replacement.

Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.


let result = (|| -> Result<(), String> {
file.write_all(&encoded)
file.write_all(encoded)
.and_then(|_| file.write_all(b"\n"))
.and_then(|_| file.sync_all())
.map_err(|_| "brew-cleanup-audit-write-failed".to_string())?;
let mut permissions = file
.metadata()
.map_err(|_| "brew-cleanup-audit-metadata-failed".to_string())?
.permissions();
permissions.set_readonly(true);
std::fs::set_permissions(&path, permissions)
permissions.set_mode(0o400);
file.set_permissions(permissions)
.map_err(|_| "brew-cleanup-audit-permissions-failed".to_string())?;
std::fs::File::open(&directory)
.and_then(|directory| directory.sync_all())
.map_err(|_| "brew-cleanup-audit-directory-sync-failed".to_string())
directory_file
.sync_all()
.map_err(|_| "brew-cleanup-audit-directory-sync-failed".to_string())?;

let opened_directory = directory_file
.metadata()
.map_err(|_| "brew-cleanup-audit-directory-metadata-failed".to_string())?;
let current_directory = std::fs::symlink_metadata(directory)
.map_err(|_| "brew-cleanup-audit-directory-identity-drift".to_string())?;
let opened_record = file
.metadata()
.map_err(|_| "brew-cleanup-audit-metadata-failed".to_string())?;
let current_record = std::fs::symlink_metadata(path)
.map_err(|_| "brew-cleanup-audit-directory-identity-drift".to_string())?;
if current_directory.file_type().is_symlink()
|| !current_directory.is_dir()
|| current_record.file_type().is_symlink()
|| !current_record.is_file()
|| opened_directory.dev() != current_directory.dev()
|| opened_directory.ino() != current_directory.ino()
|| opened_record.dev() != current_record.dev()
|| opened_record.ino() != current_record.ino()
{
return Err("brew-cleanup-audit-directory-identity-drift".into());
}
Ok(())
})();

if let Err(error) = result {
drop(file);
let _ = std::fs::remove_file(&path);
cleanup();
return Err(error);
}
Ok(path)
Ok(path.to_path_buf())
}

#[cfg(all(test, unix))]
pub(crate) fn write_audit_record_with_before_create_hook<F>(
app_data_dir: &Path,
record: &BrewCleanupAuditRecord,
before_create: F,
) -> Result<PathBuf, String>
where
F: FnOnce(),
{
let (directory, filename, path, encoded) = prepare_audit_record(app_data_dir, record)?;
write_audit_record_unix_with_hook(&directory, &filename, &path, &encoded, before_create)
}

#[cfg(test)]
Expand Down Expand Up @@ -681,6 +833,7 @@ mod tests {
assert_eq!(output.stdout, "object-bound\n");
}

#[cfg(unix)]
#[test]
fn audit_records_are_create_new_and_private() {
let temp = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -710,13 +863,10 @@ mod tests {
let path = write_audit_record(temp.path(), &record).unwrap();
assert!(path.exists());
assert!(write_audit_record(temp.path(), &record).is_err());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(
std::fs::metadata(path).unwrap().permissions().mode() & 0o777,
0o400
);
}
use std::os::unix::fs::PermissionsExt;
assert_eq!(
std::fs::metadata(path).unwrap().permissions().mode() & 0o777,
0o400
);
}
}
Loading
Loading