|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +// spell-checker:ignore lseek seekable |
| 7 | + |
| 8 | +use nix::fcntl::{FcntlArg, OFlag, fcntl}; |
| 9 | +use nix::unistd::{Whence, lseek}; |
| 10 | +use std::os::fd::AsFd; |
| 11 | +use uucore::fs::FileInformation; |
| 12 | + |
| 13 | +/// An unsafe overwrite occurs when the same nonempty file is used as both stdin and stdout, |
| 14 | +/// and the file offset of stdin is positioned earlier than that of stdout. |
| 15 | +/// In this scenario, bytes read from stdin are written to a later part of the file |
| 16 | +/// via stdout, which can then be read again by stdin and written again by stdout, |
| 17 | +/// causing an infinite loop and potential file corruption. |
| 18 | +pub fn is_unsafe_overwrite<I: AsFd, O: AsFd>(input: &I, output: &O) -> bool { |
| 19 | + // `FileInformation::from_file` returns an error if the file descriptor is closed, invalid, |
| 20 | + // or refers to a non-regular file (e.g., socket, pipe, or special device). |
| 21 | + let Ok(input_info) = FileInformation::from_file(input) else { |
| 22 | + return false; |
| 23 | + }; |
| 24 | + let Ok(output_info) = FileInformation::from_file(output) else { |
| 25 | + return false; |
| 26 | + }; |
| 27 | + if input_info != output_info || output_info.file_size() == 0 { |
| 28 | + return false; |
| 29 | + } |
| 30 | + if is_appending(output) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + // `lseek` returns an error if the file descriptor is closed or it refers to |
| 34 | + // a non-seekable resource (e.g., pipe, socket, or some devices). |
| 35 | + let Ok(input_pos) = lseek(input.as_fd(), 0, Whence::SeekCur) else { |
| 36 | + return false; |
| 37 | + }; |
| 38 | + let Ok(output_pos) = lseek(output.as_fd(), 0, Whence::SeekCur) else { |
| 39 | + return false; |
| 40 | + }; |
| 41 | + input_pos < output_pos |
| 42 | +} |
| 43 | + |
| 44 | +/// Whether the file is opened with the `O_APPEND` flag |
| 45 | +fn is_appending<F: AsFd>(file: &F) -> bool { |
| 46 | + let flags_raw = fcntl(file.as_fd(), FcntlArg::F_GETFL).unwrap_or_default(); |
| 47 | + let flags = OFlag::from_bits_truncate(flags_raw); |
| 48 | + flags.contains(OFlag::O_APPEND) |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use crate::platform::unix::{is_appending, is_unsafe_overwrite}; |
| 54 | + use std::fs::OpenOptions; |
| 55 | + use std::io::{Seek, SeekFrom, Write}; |
| 56 | + use tempfile::NamedTempFile; |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn test_is_appending() { |
| 60 | + let temp_file = NamedTempFile::new().unwrap(); |
| 61 | + assert!(!is_appending(&temp_file)); |
| 62 | + |
| 63 | + let read_file = OpenOptions::new().read(true).open(&temp_file).unwrap(); |
| 64 | + assert!(!is_appending(&read_file)); |
| 65 | + |
| 66 | + let write_file = OpenOptions::new().write(true).open(&temp_file).unwrap(); |
| 67 | + assert!(!is_appending(&write_file)); |
| 68 | + |
| 69 | + let append_file = OpenOptions::new().append(true).open(&temp_file).unwrap(); |
| 70 | + assert!(is_appending(&append_file)); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_is_unsafe_overwrite() { |
| 75 | + // Create two temp files one of which is empty |
| 76 | + let empty = NamedTempFile::new().unwrap(); |
| 77 | + let mut nonempty = NamedTempFile::new().unwrap(); |
| 78 | + nonempty.write_all(b"anything").unwrap(); |
| 79 | + nonempty.seek(SeekFrom::Start(0)).unwrap(); |
| 80 | + |
| 81 | + // Using a different file as input and output does not result in an overwrite |
| 82 | + assert!(!is_unsafe_overwrite(&empty, &nonempty)); |
| 83 | + |
| 84 | + // Overwriting an empty file is always safe |
| 85 | + assert!(!is_unsafe_overwrite(&empty, &empty)); |
| 86 | + |
| 87 | + // Overwriting a nonempty file with itself is safe |
| 88 | + assert!(!is_unsafe_overwrite(&nonempty, &nonempty)); |
| 89 | + |
| 90 | + // Overwriting an empty file opened in append mode is safe |
| 91 | + let empty_append = OpenOptions::new().append(true).open(&empty).unwrap(); |
| 92 | + assert!(!is_unsafe_overwrite(&empty, &empty_append)); |
| 93 | + |
| 94 | + // Overwriting a nonempty file opened in append mode is unsafe |
| 95 | + let nonempty_append = OpenOptions::new().append(true).open(&nonempty).unwrap(); |
| 96 | + assert!(is_unsafe_overwrite(&nonempty, &nonempty_append)); |
| 97 | + |
| 98 | + // Overwriting a file opened in write mode is safe |
| 99 | + let mut nonempty_write = OpenOptions::new().write(true).open(&nonempty).unwrap(); |
| 100 | + assert!(!is_unsafe_overwrite(&nonempty, &nonempty_write)); |
| 101 | + |
| 102 | + // Overwriting a file when the input and output file descriptors are pointing to |
| 103 | + // different offsets is safe if the input offset is further than the output offset |
| 104 | + nonempty_write.seek(SeekFrom::Start(1)).unwrap(); |
| 105 | + assert!(!is_unsafe_overwrite(&nonempty_write, &nonempty)); |
| 106 | + assert!(is_unsafe_overwrite(&nonempty, &nonempty_write)); |
| 107 | + } |
| 108 | +} |
0 commit comments