address review: set the group before copying extended attributes; skip the group test when the original's group is not assignable

This commit is contained in:
Dylan M. Taylor committed 2026-11-11 00:35:32 +00:00
1 parent 47b49199af
commit 9ca45c3a1a
2 files changed
+31 -11

No files matched your search

+27 -8
View File
@@ -51,6 +51,16 @@ const XATTR_CHUNK: usize = 4 << 10;
/// Errors are the caller's to ignore: each piece is attempted on its own, and the first failure
/// is returned only so a caller that wants to log one can.
pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> {
// The group first: on Linux `chown` drops `security.capability`, so it must not follow the
// extended-attribute copy.
#[cfg(unix)]
let metadata = std::fs::metadata(from);
#[cfg(unix)]
let group = match &metadata {
Ok(m) => restore_group(m, to),
Err(_) => Ok(true),
};
// The permission bits go on last. Writing an attribute needs the destination open for
// writing, and replacing a read-only (0444) file would copy that mode onto the temporary
// file and then be unable to touch it again.
@@ -73,22 +83,31 @@ pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> {
// A save over a 0600 file must not widen it to whatever the process default is.
#[cfg(unix)]
let permissions = std::fs::metadata(from).and_then(|m| set_permissions_keeping_group(&m, to));
let permissions = metadata.and_then(|m| group.and_then(|kept| set_permissions_keeping_group(&m, to, kept)));
#[cfg(not(unix))]
let permissions = std::fs::metadata(from).and_then(|m| std::fs::set_permissions(to, m.permissions()));
attributes.and(acl).and(created).and(permissions)
}
/// Give `to` the permission bits of the replaced file (`from`), and its group when it has another:
/// group bits are access for *that* group, so on a replacement owned by a different one (a
/// setgid folder, say) they would hand the file to people the original never named. If the
/// group cannot be restored (the owner is not a member), the group bits are left off.
/// Give `to` the group of the replaced file (`from`) when it has another: group bits are access
/// for *that* group, so on a replacement owned by a different one (a setgid folder, say) they
/// would hand the file to people the original never named. `false` when the group cannot be
/// restored (the owner is not a member): [`set_permissions_keeping_group`] then leaves the group
/// bits off.
#[cfg(unix)]
fn set_permissions_keeping_group(from: &std::fs::Metadata, to: &Path) -> io::Result<()> {
use std::os::unix::fs::{MetadataExt, PermissionsExt};
fn restore_group(from: &std::fs::Metadata, to: &Path) -> io::Result<bool> {
use std::os::unix::fs::MetadataExt;
Ok(std::fs::metadata(to)?.gid() == from.gid() || std::os::unix::fs::chown(to, None, Some(from.gid())).is_ok())
}
/// Give `to` the permission bits of the replaced file (`from`), without the group bits when
/// [`restore_group`] could not give it the original's group.
#[cfg(unix)]
fn set_permissions_keeping_group(from: &std::fs::Metadata, to: &Path, group_kept: bool) -> io::Result<()> {
use std::os::unix::fs::PermissionsExt;
let mut permissions = from.permissions();
if std::fs::metadata(to)?.gid() != from.gid() && std::os::unix::fs::chown(to, None, Some(from.gid())).is_err() {
if !group_kept {
permissions.set_mode(permissions.mode() & !0o070);
}
// After the chown, which clears setuid/setgid.
+4 -3
View File
@@ -324,9 +324,10 @@ fn a_replacement_in_another_group_does_not_widen_group_access() {
// Another group this process belongs to; without one there is nothing to test.
let status = std::fs::read_to_string("/proc/self/status").unwrap_or_default();
let own = std::fs::metadata(&original).unwrap().gid();
let Some(other) =
status.lines().find_map(|l| l.strip_prefix("Groups:")).and_then(|g| g.split_whitespace().filter_map(|g| g.parse::<u32>().ok()).find(|g| *g != own))
else {
let groups: Vec<u32> =
status.lines().find_map(|l| l.strip_prefix("Groups:")).unwrap_or_default().split_whitespace().filter_map(|g| g.parse().ok()).collect();
// The original's group must be one this process can assign; a setgid parent can give it another.
let Some(other) = groups.iter().copied().find(|g| *g != own).filter(|_| groups.contains(&own)) else {
return;
};
std::os::unix::fs::chown(&replacement, None, Some(other)).unwrap();