Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,13 @@ fn copy_file_safe(from: &Path, to_parent_fd: &DirFd, to_filename: &std::ffi::OsS
///
fn copy_file(from: &Path, to: &Path) -> UResult<()> {
use std::os::unix::fs::OpenOptionsExt;
if let Ok(to_abs) = to.canonicalize()
&& from.canonicalize()? == to_abs
{
return Err(InstallError::SameFile(from.to_path_buf(), to.to_path_buf()).into());
let mut handle = File::open(from)?;

if let Ok(to_meta) = metadata(to) {
let from_meta = handle.metadata()?;
if from_meta.dev() == to_meta.dev() && from_meta.ino() == to_meta.ino() {
return Err(InstallError::SameFile(from.to_path_buf(), to.to_path_buf()).into());
}
}

if to.is_dir() && !from.is_dir() {
Expand All @@ -970,7 +973,6 @@ fn copy_file(from: &Path, to: &Path) -> UResult<()> {
);
}

let mut handle = File::open(from)?;
// create_new provides TOCTOU protection
let mut dest = OpenOptions::new()
.write(true)
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,7 @@ fn test_install_from_stdin() {
let (at, mut ucmd) = at_and_ucmd!();
let target = "target";
let test_string = "Hello, World!\n";
let updated_string = "Updated content\n";

ucmd.arg("/dev/fd/0")
.arg(target)
Expand All @@ -2120,6 +2121,14 @@ fn test_install_from_stdin() {

assert!(at.file_exists(target));
assert_eq!(at.read(target), test_string);

new_ucmd!()
.arg("/dev/fd/0")
.arg(at.plus(target))
.pipe_in(updated_string)
.succeeds();

assert_eq!(at.read(target), updated_string);
}

#[test]
Expand Down
Loading