Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-own /nix/var to 0:0, except for the per-user profiles #1337

Merged
Merged
Changes from 3 commits
Commits
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
76 changes: 76 additions & 0 deletions src/action/common/provision_nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ impl Action for ProvisionNix {
"Will update existing files in the Nix Store to use the Nix build group ID {nix_store_gid}"
)],
));
buf.push(ActionDescription::new(
"Synchronize /nix/var ownership".to_string(),
vec![format!(
"Will update existing files in /nix/var to be owned by User ID 0, Group ID 0"
)],
));

buf
}
Expand Down Expand Up @@ -116,6 +122,8 @@ impl Action for ProvisionNix {
.await
.map_err(Self::error)?;

ensure_nix_var_ownership().await.map_err(Self::error)?;

Ok(())
}

Expand Down Expand Up @@ -229,3 +237,71 @@ async fn ensure_nix_store_group(desired_nix_build_group_id: u32) -> Result<(), A

Ok(())
}

/// Everything under /nix/var (with two deprecated exceptions below) should be owned by 0:0.
///
/// * /nix/var/nix/profiles/per-user/*
/// * /nix/var/nix/gcroots/per-user/*
///
/// This function walks /nix/var and makes sure that is true.
async fn ensure_nix_var_ownership() -> Result<(), ActionErrorKind> {
let entryiter = walkdir::WalkDir::new("/nix/var")
.follow_links(false)
.same_file_system(true)
.contents_first(true)
.into_iter()
.filter_entry(|entry| {
let parent = entry.path().parent();

if parent == Some(std::path::Path::new("/nix/var/nix/profiles/per-user"))
|| parent == Some(std::path::Path::new("/nix/var/nix/gcroots/per-user"))
{
// False means do *not* descend into this directory
// ...which we don't want to do, because the per-user subdirectories are usually owned by that user.
return false;
}

true
})
.filter_map(|entry| match entry {
Ok(entry) => Some(entry),
Err(e) => {
tracing::warn!(%e, "Enumerating /nix/var");
grahamc marked this conversation as resolved.
Show resolved Hide resolved
None
},
})
.filter_map(|entry| match entry.metadata() {
Ok(metadata) => Some((entry, metadata)),
Err(e) => {
tracing::warn!(
path = %entry.path().to_string_lossy(),
%e,
"Reading ownership and mode data"
grahamc marked this conversation as resolved.
Show resolved Hide resolved
);
None
},
})
.filter_map(|(entry, metadata)| {
// Dirents that are already 0:0 are to be skipped
if metadata.uid() == 0 && metadata.gid() == 0 {
cole-h marked this conversation as resolved.
Show resolved Hide resolved
return None;
}

Some((entry, metadata))
});
for (entry, _metadata) in entryiter {
tracing::info!(
grahamc marked this conversation as resolved.
Show resolved Hide resolved
path = %entry.path().to_string_lossy(),
"Re-owning path to 0:0"
);

if let Err(e) = std::os::unix::fs::lchown(entry.path(), Some(0), Some(0)) {
tracing::warn!(
path = %entry.path().to_string_lossy(),
%e,
"Failed to set the owner:group to 0:0"
);
}
}
Ok(())
}
Loading