Skip to content

Commit 534e027

Browse files
committed
tac: remove MMAP use of untrusted source as truncation results in core dumped
1 parent 0fbc17c commit 534e027

File tree

1 file changed

+17
-48
lines changed

1 file changed

+17
-48
lines changed

src/uu/tac/src/tac.rs

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ mod error;
88

99
use clap::{Arg, ArgAction, Command};
1010
use memchr::memmem;
11-
use memmap2::Mmap;
1211
use std::ffi::OsString;
1312
use std::io::{BufWriter, Read, Write, stdin, stdout};
14-
use std::{
15-
fs::{File, read},
16-
path::Path,
17-
};
13+
use std::{fs::read, path::Path};
1814
use uucore::error::UError;
1915
use uucore::error::UResult;
2016
use uucore::{format_usage, show};
@@ -233,23 +229,17 @@ fn tac(filenames: &[OsString], before: bool, regex: bool, separator: &str) -> UR
233229
};
234230

235231
for filename in filenames {
236-
let mmap;
237232
let buf;
238233

239234
let data: &[u8] = if filename == "-" {
240-
if let Some(mmap1) = try_mmap_stdin() {
241-
mmap = mmap1;
242-
&mmap
243-
} else {
244-
let mut buf1 = Vec::new();
245-
if let Err(e) = stdin().read_to_end(&mut buf1) {
246-
let e: Box<dyn UError> = TacError::ReadError(OsString::from("stdin"), e).into();
247-
show!(e);
248-
continue;
249-
}
250-
buf = buf1;
251-
&buf
235+
let mut buf1 = Vec::new();
236+
if let Err(e) = stdin().read_to_end(&mut buf1) {
237+
let e: Box<dyn UError> = TacError::ReadError(OsString::from("stdin"), e).into();
238+
show!(e);
239+
continue;
252240
}
241+
buf = buf1;
242+
&buf
253243
} else {
254244
let path = Path::new(filename);
255245
if path.is_dir() {
@@ -264,20 +254,15 @@ fn tac(filenames: &[OsString], before: bool, regex: bool, separator: &str) -> UR
264254
continue;
265255
}
266256

267-
if let Some(mmap1) = try_mmap_path(path) {
268-
mmap = mmap1;
269-
&mmap
270-
} else {
271-
match read(path) {
272-
Ok(buf1) => {
273-
buf = buf1;
274-
&buf
275-
}
276-
Err(e) => {
277-
let e: Box<dyn UError> = TacError::ReadError(filename.clone(), e).into();
278-
show!(e);
279-
continue;
280-
}
257+
match read(path) {
258+
Ok(buf1) => {
259+
buf = buf1;
260+
&buf
261+
}
262+
Err(e) => {
263+
let e: Box<dyn UError> = TacError::ReadError(filename.clone(), e).into();
264+
show!(e);
265+
continue;
281266
}
282267
}
283268
};
@@ -296,19 +281,3 @@ fn tac(filenames: &[OsString], before: bool, regex: bool, separator: &str) -> UR
296281
}
297282
Ok(())
298283
}
299-
300-
fn try_mmap_stdin() -> Option<Mmap> {
301-
// SAFETY: If the file is truncated while we map it, SIGBUS will be raised
302-
// and our process will be terminated, thus preventing access of invalid memory.
303-
unsafe { Mmap::map(&stdin()).ok() }
304-
}
305-
306-
fn try_mmap_path(path: &Path) -> Option<Mmap> {
307-
let file = File::open(path).ok()?;
308-
309-
// SAFETY: If the file is truncated while we map it, SIGBUS will be raised
310-
// and our process will be terminated, thus preventing access of invalid memory.
311-
let mmap = unsafe { Mmap::map(&file).ok()? };
312-
313-
Some(mmap)
314-
}

0 commit comments

Comments
 (0)