| 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 osstring |
| 7 |
|
| 8 |
#![no_main] |
| 9 |
use libfuzzer_sys::fuzz_target; |
| 10 |
use rand::RngExt; |
| 11 |
use rand::prelude::IndexedRandom; |
| 12 |
use std::collections::HashSet; |
| 13 |
use std::env::temp_dir; |
| 14 |
use std::ffi::{OsStr, OsString}; |
| 15 |
use std::fs; |
| 16 |
use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
| 17 |
use std::path::{Path, PathBuf}; |
| 18 |
|
| 19 |
use uufuzz::{CommandResult, run_gnu_cmd}; |
| 20 |
// Programs that typically take file/path arguments and should be tested |
| 21 |
static PATH_PROGRAMS: &[&str] = &[ |
| 22 |
// Core file operations |
| 23 |
"cat", |
| 24 |
"cp", |
| 25 |
"mv", |
| 26 |
"rm", |
| 27 |
"ln", |
| 28 |
"link", |
| 29 |
"unlink", |
| 30 |
"touch", |
| 31 |
"truncate", |
| 32 |
// Path operations |
| 33 |
"ls", |
| 34 |
"mkdir", |
| 35 |
"rmdir", |
| 36 |
"du", |
| 37 |
"stat", |
| 38 |
"mktemp", |
| 39 |
"df", |
| 40 |
"basename", |
| 41 |
"dirname", |
| 42 |
"readlink", |
| 43 |
"realpath", |
| 44 |
"pathchk", |
| 45 |
"chroot", |
| 46 |
// File processing |
| 47 |
"head", |
| 48 |
"tail", |
| 49 |
"tee", |
| 50 |
"more", |
| 51 |
"od", |
| 52 |
"wc", |
| 53 |
"cksum", |
| 54 |
"sum", |
| 55 |
"nl", |
| 56 |
"tac", |
| 57 |
"sort", |
| 58 |
"uniq", |
| 59 |
"split", |
| 60 |
"csplit", |
| 61 |
"cut", |
| 62 |
"tr", |
| 63 |
"shred", |
| 64 |
"shuf", |
| 65 |
"ptx", |
| 66 |
"tsort", |
| 67 |
// Text processing with files |
| 68 |
"chmod", |
| 69 |
"chown", |
| 70 |
"chgrp", |
| 71 |
"install", |
| 72 |
"chcon", |
| 73 |
"runcon", |
| 74 |
"comm", |
| 75 |
"join", |
| 76 |
"paste", |
| 77 |
"pr", |
| 78 |
"fmt", |
| 79 |
"fold", |
| 80 |
"expand", |
| 81 |
"unexpand", |
| 82 |
"dir", |
| 83 |
"vdir", |
| 84 |
"mkfifo", |
| 85 |
"mknod", |
| 86 |
// File I/O utilities |
| 87 |
"dd", |
| 88 |
"sync", |
| 89 |
"stdbuf", |
| 90 |
"dircolors", |
| 91 |
// Encoding/decoding utilities |
| 92 |
"base32", |
| 93 |
"base64", |
| 94 |
"basenc", |
| 95 |
"stty", |
| 96 |
"tty", |
| 97 |
"env", |
| 98 |
"nohup", |
| 99 |
"nice", |
| 100 |
"timeout", |
| 101 |
]; |
| 102 |
|
| 103 |
fn generate_non_utf8_bytes() -> Vec<u8> { |
| 104 |
let mut rng = rand::rng(); |
| 105 |
let mut bytes = Vec::new(); |
| 106 |
|
| 107 |
// Start with some valid UTF-8 to make it look like a reasonable path |
| 108 |
bytes.extend_from_slice(b"test_"); |
| 109 |
|
| 110 |
// Add some invalid UTF-8 sequences |
| 111 |
match rng.random_range(0..4) { |
| 112 |
0 => bytes.extend_from_slice(&[0xFF, 0xFE]), // Invalid UTF-8 |
| 113 |
1 => bytes.extend_from_slice(&[0xC0, 0x80]), // Overlong encoding |
| 114 |
2 => bytes.extend_from_slice(&[0xED, 0xA0, 0x80]), // UTF-16 surrogate |
| 115 |
_ => bytes.extend_from_slice(&[0xF4, 0x90, 0x80, 0x80]), // Beyond Unicode range |
| 116 |
} |
| 117 |
|
| 118 |
bytes |
| 119 |
} |
| 120 |
|
| 121 |
fn generate_non_utf8_osstring() -> OsString { |
| 122 |
OsString::from_vec(generate_non_utf8_bytes()) |
| 123 |
} |
| 124 |
|
| 125 |
fn setup_test_files() -> std::io::Result<(PathBuf, Vec<PathBuf>)> { |
| 126 |
let mut rng = rand::rng(); |
| 127 |
let temp_root = temp_dir().join(format!("utf8_test_{}", rng.random::<u64>())); |
| 128 |
fs::create_dir_all(&temp_root)?; |
| 129 |
|
| 130 |
let mut test_files = Vec::new(); |
| 131 |
|
| 132 |
// Create some files with non-UTF-8 names |
| 133 |
for i in 0..3 { |
| 134 |
let mut path_bytes = temp_root.as_os_str().as_bytes().to_vec(); |
| 135 |
path_bytes.push(b'/'); |
| 136 |
|
| 137 |
if i == 0 { |
| 138 |
// One normal UTF-8 file for comparison |
| 139 |
path_bytes.extend_from_slice(b"normal_file.txt"); |
| 140 |
} else { |
| 141 |
// Files with invalid UTF-8 names |
| 142 |
path_bytes.extend_from_slice(&generate_non_utf8_bytes()); |
| 143 |
} |
| 144 |
|
| 145 |
let file_path = PathBuf::from(OsStr::from_bytes(&path_bytes)); |
| 146 |
|
| 147 |
// Try to create the file - this may fail on some filesystems |
| 148 |
if let Ok(mut file) = fs::File::create(&file_path) { |
| 149 |
use std::io::Write; |
| 150 |
let _ = writeln!(file, "test content for file {}", i); |
| 151 |
test_files.push(file_path); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
Ok((temp_root, test_files)) |
| 156 |
} |
| 157 |
|
| 158 |
fn test_program_with_non_utf8_path(program: &str, path: &Path) -> CommandResult { |
| 159 |
let path_os = path.as_os_str(); |
| 160 |
|
| 161 |
// Use the locally built uutils binary instead of system PATH |
| 162 |
let local_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 163 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 164 |
|
| 165 |
// Build appropriate arguments for each program |
| 166 |
let local_args = match program { |
| 167 |
// Programs that need mode/permissions |
| 168 |
"chmod" => vec![ |
| 169 |
OsString::from(program), |
| 170 |
OsString::from("644"), |
| 171 |
path_os.to_owned(), |
| 172 |
], |
| 173 |
"chown" => vec![ |
| 174 |
OsString::from(program), |
| 175 |
OsString::from("root:root"), |
| 176 |
path_os.to_owned(), |
| 177 |
], |
| 178 |
"chgrp" => vec![ |
| 179 |
OsString::from(program), |
| 180 |
OsString::from("root"), |
| 181 |
path_os.to_owned(), |
| 182 |
], |
| 183 |
"chcon" => vec![ |
| 184 |
OsString::from(program), |
| 185 |
OsString::from("system_u:object_r:admin_home_t:s0"), |
| 186 |
path_os.to_owned(), |
| 187 |
], |
| 188 |
"runcon" => { |
| 189 |
let coreutils_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 190 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 191 |
vec![ |
| 192 |
OsString::from(program), |
| 193 |
OsString::from("system_u:object_r:admin_home_t:s0"), |
| 194 |
OsString::from(coreutils_binary), |
| 195 |
OsString::from("cat"), |
| 196 |
path_os.to_owned(), |
| 197 |
] |
| 198 |
} |
| 199 |
// Programs that need source and destination |
| 200 |
"cp" | "mv" | "ln" | "link" => { |
| 201 |
let dest_path = path.with_extension("dest"); |
| 202 |
vec![ |
| 203 |
OsString::from(program), |
| 204 |
path_os.to_owned(), |
| 205 |
dest_path.as_os_str().to_owned(), |
| 206 |
] |
| 207 |
} |
| 208 |
"install" => { |
| 209 |
let dest_path = path.with_extension("dest"); |
| 210 |
vec![ |
| 211 |
OsString::from(program), |
| 212 |
path_os.to_owned(), |
| 213 |
dest_path.as_os_str().to_owned(), |
| 214 |
] |
| 215 |
} |
| 216 |
// Programs that need size/truncate operations |
| 217 |
"truncate" => vec![ |
| 218 |
OsString::from(program), |
| 219 |
OsString::from("--size=0"), |
| 220 |
path_os.to_owned(), |
| 221 |
], |
| 222 |
"split" => vec![ |
| 223 |
OsString::from(program), |
| 224 |
path_os.to_owned(), |
| 225 |
OsString::from("split_prefix_"), |
| 226 |
], |
| 227 |
"csplit" => vec![ |
| 228 |
OsString::from(program), |
| 229 |
path_os.to_owned(), |
| 230 |
OsString::from("1"), |
| 231 |
], |
| 232 |
// File creation programs |
| 233 |
"mkfifo" | "mknod" => { |
| 234 |
let new_path = path.with_extension("new"); |
| 235 |
if program == "mknod" { |
| 236 |
vec![ |
| 237 |
OsString::from(program), |
| 238 |
new_path.as_os_str().to_owned(), |
| 239 |
OsString::from("c"), |
| 240 |
OsString::from("1"), |
| 241 |
OsString::from("3"), |
| 242 |
] |
| 243 |
} else { |
| 244 |
vec![OsString::from(program), new_path.as_os_str().to_owned()] |
| 245 |
} |
| 246 |
} |
| 247 |
"dd" => vec![ |
| 248 |
OsString::from(program), |
| 249 |
OsString::from(format!("if={}", path_os.to_string_lossy())), |
| 250 |
OsString::from("of=/dev/null"), |
| 251 |
OsString::from("bs=1"), |
| 252 |
OsString::from("count=1"), |
| 253 |
], |
| 254 |
// Encoding/decoding programs |
| 255 |
"base32" | "base64" | "basenc" => vec![OsString::from(program), path_os.to_owned()], |
| 256 |
"df" => vec![OsString::from(program), path_os.to_owned()], |
| 257 |
"chroot" => { |
| 258 |
// chroot needs a directory and command |
| 259 |
vec![ |
| 260 |
OsString::from(program), |
| 261 |
path_os.to_owned(), |
| 262 |
OsString::from("true"), |
| 263 |
] |
| 264 |
} |
| 265 |
"sync" => vec![OsString::from(program), path_os.to_owned()], |
| 266 |
"stty" => vec![ |
| 267 |
OsString::from(program), |
| 268 |
OsString::from("-F"), |
| 269 |
path_os.to_owned(), |
| 270 |
], |
| 271 |
"tty" => vec![OsString::from(program)], // tty doesn't take file args, but test anyway |
| 272 |
"env" => { |
| 273 |
let coreutils_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 274 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 275 |
vec![ |
| 276 |
OsString::from(program), |
| 277 |
OsString::from(coreutils_binary), |
| 278 |
OsString::from("cat"), |
| 279 |
path_os.to_owned(), |
| 280 |
] |
| 281 |
} |
| 282 |
"nohup" => { |
| 283 |
let coreutils_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 284 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 285 |
vec![ |
| 286 |
OsString::from(program), |
| 287 |
OsString::from(coreutils_binary), |
| 288 |
OsString::from("cat"), |
| 289 |
path_os.to_owned(), |
| 290 |
] |
| 291 |
} |
| 292 |
"nice" => { |
| 293 |
let coreutils_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 294 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 295 |
vec![ |
| 296 |
OsString::from(program), |
| 297 |
OsString::from(coreutils_binary), |
| 298 |
OsString::from("cat"), |
| 299 |
path_os.to_owned(), |
| 300 |
] |
| 301 |
} |
| 302 |
"timeout" => { |
| 303 |
let coreutils_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 304 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 305 |
vec![ |
| 306 |
OsString::from(program), |
| 307 |
OsString::from("1"), |
| 308 |
OsString::from(coreutils_binary), |
| 309 |
OsString::from("cat"), |
| 310 |
path_os.to_owned(), |
| 311 |
] |
| 312 |
} |
| 313 |
"stdbuf" => { |
| 314 |
let coreutils_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 315 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 316 |
vec![ |
| 317 |
OsString::from(program), |
| 318 |
OsString::from("-o0"), |
| 319 |
OsString::from(coreutils_binary), |
| 320 |
OsString::from("cat"), |
| 321 |
path_os.to_owned(), |
| 322 |
] |
| 323 |
} |
| 324 |
// Programs that work with multiple files (use just one for testing) |
| 325 |
"comm" | "join" => { |
| 326 |
// These need two files, use the same file twice for simplicity |
| 327 |
vec![ |
| 328 |
OsString::from(program), |
| 329 |
path_os.to_owned(), |
| 330 |
path_os.to_owned(), |
| 331 |
] |
| 332 |
} |
| 333 |
// Programs that typically take file input |
| 334 |
_ => vec![OsString::from(program), path_os.to_owned()], |
| 335 |
}; |
| 336 |
|
| 337 |
// Try to run the local uutils version |
| 338 |
match run_gnu_cmd(&local_binary, &local_args, false, None) { |
| 339 |
Ok(result) => result, |
| 340 |
Err(error_result) => { |
| 341 |
// Local command failed, return the error |
| 342 |
error_result |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
fn cleanup_test_files(temp_root: &PathBuf) { |
| 348 |
let _ = fs::remove_dir_all(temp_root); |
| 349 |
} |
| 350 |
|
| 351 |
fn check_for_utf8_error_and_panic(result: &CommandResult, program: &str, path: &PathBuf) { |
| 352 |
let stderr_lower = result.stderr.to_lowercase(); |
| 353 |
let is_utf8_error = stderr_lower.contains("invalid utf-8") |
| 354 |
|| stderr_lower.contains("not valid unicode") |
| 355 |
|| stderr_lower.contains("invalid utf8") |
| 356 |
|| stderr_lower.contains("utf-8 error"); |
| 357 |
|
| 358 |
if is_utf8_error { |
| 359 |
println!( |
| 360 |
"UTF-8 conversion error detected in {}: {}", |
| 361 |
program, result.stderr |
| 362 |
); |
| 363 |
println!("Path: {:?}", path); |
| 364 |
println!("Exit code: {}", result.exit_code); |
| 365 |
panic!( |
| 366 |
"FUZZER FAILURE: {} failed with UTF-8 error on non-UTF-8 path: {:?}", |
| 367 |
program, path |
| 368 |
); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
fuzz_target!(|_data: &[u8]| { |
| 373 |
let mut rng = rand::rng(); |
| 374 |
|
| 375 |
// Set up test environment |
| 376 |
let (temp_root, test_files) = match setup_test_files() { |
| 377 |
Ok(files) => files, |
| 378 |
Err(_) => return, // Skip if we can't set up test files |
| 379 |
}; |
| 380 |
|
| 381 |
// Pick multiple random programs to test in each iteration |
| 382 |
let num_programs_to_test = rng.random_range(1..=3); // Test 1-3 programs per iteration |
| 383 |
let mut tested_programs = HashSet::new(); |
| 384 |
|
| 385 |
let mut programs_tested = Vec::<String>::new(); |
| 386 |
|
| 387 |
for _ in 0..num_programs_to_test { |
| 388 |
// Pick a random program that we haven't tested yet in this iteration |
| 389 |
let available_programs: Vec<_> = PATH_PROGRAMS |
| 390 |
.iter() |
| 391 |
.filter(|p| !tested_programs.contains(*p)) |
| 392 |
.collect(); |
| 393 |
|
| 394 |
if available_programs.is_empty() { |
| 395 |
break; |
| 396 |
} |
| 397 |
|
| 398 |
let program = available_programs.choose(&mut rng).unwrap(); |
| 399 |
tested_programs.insert(*program); |
| 400 |
programs_tested.push(program.to_string()); |
| 401 |
|
| 402 |
// Test with one random file that has non-UTF-8 names (not all files to speed up) |
| 403 |
if let Some(test_file) = test_files.choose(&mut rng) { |
| 404 |
let result = test_program_with_non_utf8_path(program, test_file); |
| 405 |
|
| 406 |
// Check if the program handled the non-UTF-8 path gracefully |
| 407 |
check_for_utf8_error_and_panic(&result, program, test_file); |
| 408 |
} |
| 409 |
|
| 410 |
// Special cases for programs that need additional testing |
| 411 |
if **program == "mkdir" || **program == "mktemp" { |
| 412 |
let non_utf8_dir_name = generate_non_utf8_osstring(); |
| 413 |
let non_utf8_dir = temp_root.join(non_utf8_dir_name); |
| 414 |
|
| 415 |
let local_binary = std::env::var("CARGO_BIN_FILE_COREUTILS") |
| 416 |
.unwrap_or_else(|_| "target/release/coreutils".to_string()); |
| 417 |
let mkdir_args = vec![OsString::from("mkdir"), non_utf8_dir.as_os_str().to_owned()]; |
| 418 |
|
| 419 |
let mkdir_result = run_gnu_cmd(&local_binary, &mkdir_args, false, None); |
| 420 |
match mkdir_result { |
| 421 |
Ok(result) => { |
| 422 |
check_for_utf8_error_and_panic(&result, "mkdir", &non_utf8_dir); |
| 423 |
} |
| 424 |
Err(error) => { |
| 425 |
check_for_utf8_error_and_panic(&error, "mkdir", &non_utf8_dir); |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
println!("Tested programs: {}", programs_tested.join(", ")); |
| 432 |
|
| 433 |
// Clean up |
| 434 |
cleanup_test_files(&temp_root); |
| 435 |
}); |