From a56e05cf630aae95b2ad946966170a2e656bd50b Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 18 Feb 2023 21:44:24 +0100 Subject: [PATCH 1/9] add fuzzing for the date function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Work done RafaƂ Mikrut --- src/uu/date/fuzz/.gitignore | 3 +++ src/uu/date/fuzz/Cargo.toml | 26 +++++++++++++++++++ .../date/fuzz/fuzz_targets/fuzz_target_1.rs | 13 ++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/uu/date/fuzz/.gitignore create mode 100644 src/uu/date/fuzz/Cargo.toml create mode 100644 src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs diff --git a/src/uu/date/fuzz/.gitignore b/src/uu/date/fuzz/.gitignore new file mode 100644 index 000000000..a0925114d --- /dev/null +++ b/src/uu/date/fuzz/.gitignore @@ -0,0 +1,3 @@ +target +corpus +artifacts diff --git a/src/uu/date/fuzz/Cargo.toml b/src/uu/date/fuzz/Cargo.toml new file mode 100644 index 000000000..8853ede90 --- /dev/null +++ b/src/uu/date/fuzz/Cargo.toml @@ -0,0 +1,26 @@ +# spell-checker:ignore libfuzzer + +[package] +name = "uu_date-fuzz" +version = "0.0.0" +publish = false +edition = "2021" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +libfuzzer-sys = "0.4" + +[dependencies.uu_date] +path = ".." + +# Prevent this from interfering with workspaces +[workspace] +members = ["."] + +[[bin]] +name = "fuzz_target_1" +path = "fuzz_targets/fuzz_target_1.rs" +test = false +doc = false diff --git a/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs b/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs new file mode 100644 index 000000000..432a90994 --- /dev/null +++ b/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs @@ -0,0 +1,13 @@ +// spell-checker:ignore libfuzzer + +#![no_main] +use libfuzzer_sys::fuzz_target; + +use std::ffi::OsString; +use uu_date::uumain; + +fuzz_target!(|data: &[u8]| { + let iter: Vec = [""].into_iter().map(|e| OsString::from(e)).collect(); + let it2 = iter.into_iter(); + uumain(it2); +}); From 69f420cb01c46fc45356a03fdd6e8118ad4bc2a6 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 18 Feb 2023 21:45:19 +0100 Subject: [PATCH 2/9] run the fuzzer in the CI for 60 seconds --- .github/workflows/CICD.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 9e48eb804..bfb7bf683 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -136,6 +136,25 @@ jobs: S=$(cargo fmt -- --check) && printf "%s\n" "$S" || { printf "%s\n" "$S" ; printf "%s\n" "$S" | sed -E -n -e "s/^Diff[[:space:]]+in[[:space:]]+${PWD//\//\\/}\/(.*)[[:space:]]+at[[:space:]]+[^0-9]+([0-9]+).*$/::${fault_type} file=\1,line=\2::${fault_prefix}: \`cargo fmt\`: style violation (file:'\1', line:\2; use \`cargo fmt -- \"\1\"\`)/p" ; fault=true ; } if [ -n "${{ steps.vars.outputs.FAIL_ON_FAULT }}" ] && [ -n "$fault" ]; then exit 1 ; fi + fuzz: + name: Run the Fuzzer + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: Swatinem/rust-cache@v2 + - name: Install `rust` toolchain + run: | + rustup toolchain install nightly --no-self-update --profile minimal + rustup default nightly + - name: Install `cargo-fuzz` + run: cargo install cargo-fuzz + - name: Run the fuzzer on date for 60 seconds + shell: bash + run: | + ## Run it + cd src/uu/date + cargo +nightly fuzz run fuzz_target_1 -- -max_total_time=60 -detect_leaks=0 + style_lint: name: Style/lint runs-on: ${{ matrix.job.os }} From 234ef07abd9f8aa8486f53b7753d6b0e0cf0c3ee Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 7 Mar 2023 13:40:59 +0100 Subject: [PATCH 3/9] Try to fuzz for real --- src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs b/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs index 432a90994..0596e2a5f 100644 --- a/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs +++ b/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs @@ -7,7 +7,10 @@ use std::ffi::OsString; use uu_date::uumain; fuzz_target!(|data: &[u8]| { - let iter: Vec = [""].into_iter().map(|e| OsString::from(e)).collect(); - let it2 = iter.into_iter(); - uumain(it2); + let delim: u8 = 0; // Null byte + let args = data + .split(|b| *b == delim) + .filter_map(|e| std::str::from_utf8(e).ok()) + .map(|e| OsString::from(e)); + uumain(args); }); From e553a4a21c8fd71c7f1a176b83bdb33e79107f7f Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 7 Mar 2023 23:23:07 +0100 Subject: [PATCH 4/9] move the fuzz_date fuzzers --- {src/uu/date/fuzz => fuzz}/.gitignore | 0 {src/uu/date/fuzz => fuzz}/Cargo.toml | 11 +++++++---- .../fuzz_targets/fuzz_date.rs | 0 3 files changed, 7 insertions(+), 4 deletions(-) rename {src/uu/date/fuzz => fuzz}/.gitignore (100%) rename {src/uu/date/fuzz => fuzz}/Cargo.toml (67%) rename src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs => fuzz/fuzz_targets/fuzz_date.rs (100%) diff --git a/src/uu/date/fuzz/.gitignore b/fuzz/.gitignore similarity index 100% rename from src/uu/date/fuzz/.gitignore rename to fuzz/.gitignore diff --git a/src/uu/date/fuzz/Cargo.toml b/fuzz/Cargo.toml similarity index 67% rename from src/uu/date/fuzz/Cargo.toml rename to fuzz/Cargo.toml index 8853ede90..01ca79843 100644 --- a/src/uu/date/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -1,7 +1,7 @@ # spell-checker:ignore libfuzzer [package] -name = "uu_date-fuzz" +name = "uucore-fuzz" version = "0.0.0" publish = false edition = "2021" @@ -12,15 +12,18 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" +[dependencies.uucore] +path = "../src/uucore/" + [dependencies.uu_date] -path = ".." +path = "../src/uu/date/" # Prevent this from interfering with workspaces [workspace] members = ["."] [[bin]] -name = "fuzz_target_1" -path = "fuzz_targets/fuzz_target_1.rs" +name = "fuzz_date" +path = "fuzz_targets/fuzz_date.rs" test = false doc = false diff --git a/src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs b/fuzz/fuzz_targets/fuzz_date.rs similarity index 100% rename from src/uu/date/fuzz/fuzz_targets/fuzz_target_1.rs rename to fuzz/fuzz_targets/fuzz_date.rs From ef0b177e1872d36852d70ab0b5e528e27c9be871 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 7 Mar 2023 23:24:13 +0100 Subject: [PATCH 5/9] add more fuzzers Many thanks to Jemma Nelson @fwip --- fuzz/Cargo.toml | 18 ++++++++++++++++++ fuzz/fuzz_targets/fuzz_parse_glob.rs | 12 ++++++++++++ fuzz/fuzz_targets/fuzz_parse_size.rs | 12 ++++++++++++ fuzz/fuzz_targets/fuzz_parse_time.rs | 12 ++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 fuzz/fuzz_targets/fuzz_parse_glob.rs create mode 100644 fuzz/fuzz_targets/fuzz_parse_size.rs create mode 100644 fuzz/fuzz_targets/fuzz_parse_time.rs diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 01ca79843..2509d7e57 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -27,3 +27,21 @@ name = "fuzz_date" path = "fuzz_targets/fuzz_date.rs" test = false doc = false + +[[bin]] +name = "fuzz_parse_glob" +path = "fuzz_targets/fuzz_parse_glob.rs" +test = false +doc = false + +[[bin]] +name = "fuzz_parse_size" +path = "fuzz_targets/fuzz_parse_size.rs" +test = false +doc = false + +[[bin]] +name = "fuzz_parse_time" +path = "fuzz_targets/fuzz_parse_time.rs" +test = false +doc = false diff --git a/fuzz/fuzz_targets/fuzz_parse_glob.rs b/fuzz/fuzz_targets/fuzz_parse_glob.rs new file mode 100644 index 000000000..8215e7124 --- /dev/null +++ b/fuzz/fuzz_targets/fuzz_parse_glob.rs @@ -0,0 +1,12 @@ +// spell-checker:ignore libfuzzer + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use uucore::parse_glob; + +fuzz_target!(|data: &[u8]| { + if let Ok(s) = std::str::from_utf8(data) { + _ = parse_glob::from_str(s) + } +}); diff --git a/fuzz/fuzz_targets/fuzz_parse_size.rs b/fuzz/fuzz_targets/fuzz_parse_size.rs new file mode 100644 index 000000000..e67a2c566 --- /dev/null +++ b/fuzz/fuzz_targets/fuzz_parse_size.rs @@ -0,0 +1,12 @@ +// spell-checker:ignore libfuzzer + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use uucore::parse_size::parse_size; + +fuzz_target!(|data: &[u8]| { + if let Ok(s) = std::str::from_utf8(data) { + _ = parse_size(s); + } +}); diff --git a/fuzz/fuzz_targets/fuzz_parse_time.rs b/fuzz/fuzz_targets/fuzz_parse_time.rs new file mode 100644 index 000000000..0a4bbbd84 --- /dev/null +++ b/fuzz/fuzz_targets/fuzz_parse_time.rs @@ -0,0 +1,12 @@ +// spell-checker:ignore libfuzzer + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use uucore::parse_time; + +fuzz_target!(|data: &[u8]| { + if let Ok(s) = std::str::from_utf8(data) { + _ = parse_time::from_str(s); + } +}); From 9dcd3192d2fcd2af164b78fc5017e589077caaee Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 7 Mar 2023 23:26:32 +0100 Subject: [PATCH 6/9] run all the fuzzer in the CI for XX seconds --- .github/workflows/CICD.yml | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index bfb7bf683..737586d60 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -139,6 +139,8 @@ jobs: fuzz: name: Run the Fuzzer runs-on: ubuntu-latest + env: + RUN_FOR: 60 steps: - uses: actions/checkout@v3 - uses: Swatinem/rust-cache@v2 @@ -148,12 +150,36 @@ jobs: rustup default nightly - name: Install `cargo-fuzz` run: cargo install cargo-fuzz - - name: Run the fuzzer on date for 60 seconds + - name: Run fuzz_date for XX seconds + # TODO: fix the issues + continue-on-error: true shell: bash run: | ## Run it - cd src/uu/date - cargo +nightly fuzz run fuzz_target_1 -- -max_total_time=60 -detect_leaks=0 + cd fuzz + cargo +nightly fuzz run fuzz_date -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0 + - name: Run fuzz_parse_glob for XX seconds + # TODO: fix the issues + continue-on-error: true + shell: bash + run: | + ## Run it + cd fuzz + cargo +nightly fuzz run fuzz_parse_glob -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0 + - name: Run fuzz_parse_size for XX seconds + shell: bash + run: | + ## Run it + cd fuzz + cargo +nightly fuzz run fuzz_parse_size -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0 + - name: Run fuzz_parse_time for XX seconds + # TODO: fix the issues + continue-on-error: true + shell: bash + run: | + ## Run it + cd fuzz + cargo +nightly fuzz run fuzz_parse_time -- -max_total_time=${{ env.RUN_FOR }} -detect_leaks=0 style_lint: name: Style/lint From 1bc9980d14214a32895fb8dd841ccb2bb4fca7a6 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 10 Mar 2023 00:02:32 +0100 Subject: [PATCH 7/9] use workspaces --- fuzz/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 2509d7e57..bea090a9d 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -13,10 +13,10 @@ cargo-fuzz = true libfuzzer-sys = "0.4" [dependencies.uucore] -path = "../src/uucore/" +uucore = { workspace = true } [dependencies.uu_date] -path = "../src/uu/date/" +uu_date = { workspace = true } # Prevent this from interfering with workspaces [workspace] From 24e78cb027b9d4daae8d1476fc3d46b3e7c57cb1 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 10 Mar 2023 00:02:35 +0100 Subject: [PATCH 8/9] add libfuzzer as a word --- .vscode/cspell.dictionaries/acronyms+names.wordlist.txt | 1 + fuzz/Cargo.toml | 2 -- fuzz/fuzz_targets/fuzz_date.rs | 2 -- fuzz/fuzz_targets/fuzz_parse_glob.rs | 2 -- fuzz/fuzz_targets/fuzz_parse_size.rs | 2 -- fuzz/fuzz_targets/fuzz_parse_time.rs | 2 -- 6 files changed, 1 insertion(+), 10 deletions(-) diff --git a/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt b/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt index 81bc3bc5f..8711913d9 100644 --- a/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt +++ b/.vscode/cspell.dictionaries/acronyms+names.wordlist.txt @@ -50,6 +50,7 @@ Gmail GNU Illumos Irix +libfuzzer MS-DOS MSDOS MacOS diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index bea090a9d..89c6bc4ef 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -1,5 +1,3 @@ -# spell-checker:ignore libfuzzer - [package] name = "uucore-fuzz" version = "0.0.0" diff --git a/fuzz/fuzz_targets/fuzz_date.rs b/fuzz/fuzz_targets/fuzz_date.rs index 0596e2a5f..96c56cc6b 100644 --- a/fuzz/fuzz_targets/fuzz_date.rs +++ b/fuzz/fuzz_targets/fuzz_date.rs @@ -1,5 +1,3 @@ -// spell-checker:ignore libfuzzer - #![no_main] use libfuzzer_sys::fuzz_target; diff --git a/fuzz/fuzz_targets/fuzz_parse_glob.rs b/fuzz/fuzz_targets/fuzz_parse_glob.rs index 8215e7124..061569bc4 100644 --- a/fuzz/fuzz_targets/fuzz_parse_glob.rs +++ b/fuzz/fuzz_targets/fuzz_parse_glob.rs @@ -1,5 +1,3 @@ -// spell-checker:ignore libfuzzer - #![no_main] use libfuzzer_sys::fuzz_target; diff --git a/fuzz/fuzz_targets/fuzz_parse_size.rs b/fuzz/fuzz_targets/fuzz_parse_size.rs index e67a2c566..23b3b5ea4 100644 --- a/fuzz/fuzz_targets/fuzz_parse_size.rs +++ b/fuzz/fuzz_targets/fuzz_parse_size.rs @@ -1,5 +1,3 @@ -// spell-checker:ignore libfuzzer - #![no_main] use libfuzzer_sys::fuzz_target; diff --git a/fuzz/fuzz_targets/fuzz_parse_time.rs b/fuzz/fuzz_targets/fuzz_parse_time.rs index 0a4bbbd84..a643c6d80 100644 --- a/fuzz/fuzz_targets/fuzz_parse_time.rs +++ b/fuzz/fuzz_targets/fuzz_parse_time.rs @@ -1,5 +1,3 @@ -// spell-checker:ignore libfuzzer - #![no_main] use libfuzzer_sys::fuzz_target; From 51c3e76abf391a30c72fc829086542f35e5e343b Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 10 Mar 2023 00:21:35 +0100 Subject: [PATCH 9/9] we have now several fuzzers --- .github/workflows/CICD.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 737586d60..f5ddefc4f 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -137,7 +137,7 @@ jobs: if [ -n "${{ steps.vars.outputs.FAIL_ON_FAULT }}" ] && [ -n "$fault" ]; then exit 1 ; fi fuzz: - name: Run the Fuzzer + name: Run the fuzzers runs-on: ubuntu-latest env: RUN_FOR: 60