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
14 changes: 9 additions & 5 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,15 @@ 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()?;
// Refuse to replace the source file with itself; otherwise removing
// the destination below would delete the opened input before copying.
if from_meta.dev() == to_meta.dev() && from_meta.ino() == to_meta.ino() {
Comment thread
puneetdixit200 marked this conversation as resolved.
return Err(InstallError::SameFile(from.to_path_buf(), to.to_path_buf()).into());
}
}

if to.is_dir() && !from.is_dir() {
Expand All @@ -970,7 +975,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
2 changes: 1 addition & 1 deletion tests/by-util/test_mkfifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn test_mkfifo_permission_unchanged_when_failed() {
.arg("-m")
.arg("666")
.fails()
.stderr_is(err_msg.as_str());
.stderr_contains(err_msg.as_str());
Comment thread
puneetdixit200 marked this conversation as resolved.
let metadata = std::fs::metadata(at.subdir.join(file_name)).unwrap();
let permissions = display_permissions(&metadata, true);
let expected = "-rw-------";
Expand Down
Loading