| 1 |
# Fuzzing jsonpickle |
| 2 |
|
| 3 |
[][oss-fuzz-issue-tracker] |
| 4 |
|
| 5 |
This directory contains files related to jsonpickle's suite of fuzz tests that are executed daily on automated |
| 6 |
infrastructure provided by [OSS-Fuzz][oss-fuzz-repo]. This document aims to provide necessary information for working |
| 7 |
with fuzzing in jsonpickle. |
| 8 |
|
| 9 |
The latest details regarding OSS-Fuzz test status, including build logs and coverage reports, is available |
| 10 |
on [the Open Source Fuzzing Introspection website](https://introspector.oss-fuzz.com/project-profile?project=jsonpickle). |
| 11 |
|
| 12 |
## How to Contribute |
| 13 |
|
| 14 |
There are many ways to contribute to jsonpickle's fuzzing efforts! Contributions are welcomed through issues, |
| 15 |
discussions, or pull requests on this repository. |
| 16 |
|
| 17 |
Areas that are particularly appreciated include: |
| 18 |
|
| 19 |
- **Tackling the existing backlog of open issues**. While fuzzing is an effective way to identify bugs, that information |
| 20 |
isn't useful unless they are fixed. If you are not sure where to start, the issues tab is a great place to get ideas! |
| 21 |
- **Improvements to this (or other) documentation** make it easier for new contributors to get involved, so even small |
| 22 |
improvements can have a large impact over time. If you see something that could be made easier by a documentation |
| 23 |
update of any size, please consider suggesting it! |
| 24 |
|
| 25 |
For everything else, such as expanding test coverage, optimizing test performance, or enhancing error detection |
| 26 |
capabilities, jump into the "Getting Started" section below. |
| 27 |
|
| 28 |
## Getting Started with Fuzzing jsonpickle |
| 29 |
|
| 30 |
> [!TIP] |
| 31 |
> **New to fuzzing or unfamiliar with OSS-Fuzz?** |
| 32 |
> |
| 33 |
> These resources are an excellent place to start: |
| 34 |
> |
| 35 |
> - [OSS-Fuzz documentation][oss-fuzz-docs] - Continuous fuzzing service for open source software. |
| 36 |
> - [Google/fuzzing][google-fuzzing-repo] - Tutorials, examples, discussions, research proposals, and other resources |
| 37 |
related to fuzzing. |
| 38 |
> - [CNCF Fuzzing Handbook](https://github.com/cncf/tag-security/blob/main/security-fuzzing-handbook/handbook-fuzzing.pdf) - |
| 39 |
A comprehensive guide for fuzzing open source software. |
| 40 |
> - [Efficient Fuzzing Guide by The Chromium Project](https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/efficient_fuzzing.md) - |
| 41 |
Explores strategies to enhance the effectiveness of your fuzz tests, recommended for those looking to optimize their |
| 42 |
testing efforts. |
| 43 |
|
| 44 |
### Setting Up Your Local Environment |
| 45 |
|
| 46 |
Before contributing to fuzzing efforts, ensure Python and Docker are installed on your machine. Docker is required for |
| 47 |
running fuzzers in containers provided by OSS-Fuzz. [Install Docker](https://docs.docker.com/get-docker/) following the official guide if you do not already have it. |
| 48 |
|
| 49 |
### Understanding Existing Fuzz Targets |
| 50 |
|
| 51 |
Review the `fuzz-targets/` directory to familiarize yourself with how existing tests are implemented. See |
| 52 |
the [Files & Directories Overview](#files--directories-overview) for more details on the directory structure. |
| 53 |
|
| 54 |
### Contributing to Fuzz Tests |
| 55 |
|
| 56 |
Start by reviewing the [Atheris documentation][atheris-repo] and the section |
| 57 |
on [Running Fuzzers Locally](#running-fuzzers-locally) to begin writing or improving fuzz tests. |
| 58 |
|
| 59 |
## Files & Directories Overview |
| 60 |
|
| 61 |
The `fuzzing/` directory is organized into three key areas: |
| 62 |
|
| 63 |
### Fuzz Targets (`fuzz-targets/`) |
| 64 |
|
| 65 |
Contains Python files for each fuzz test. |
| 66 |
|
| 67 |
**Things to Know**: |
| 68 |
|
| 69 |
- Each fuzz test targets a specific part of jsonpickle's functionality. |
| 70 |
- Test files adhere to the naming convention: `fuzz_<API Under Test>.py`, where `<API Under Test>` indicates the |
| 71 |
functionality targeted by the test. |
| 72 |
- Any functionality that involves performing operations on input data is a possible candidate for fuzz testing, but |
| 73 |
features that involve processing untrusted user input or parsing operations are typically going to be the most |
| 74 |
interesting. |
| 75 |
- The goal of these tests is to identify previously unknown or unexpected error cases caused by a given input. For that |
| 76 |
reason, fuzz tests should gracefully handle anticipated exception cases with a `try`/`except` block to avoid false |
| 77 |
positives that halt the fuzzing engine. |
| 78 |
|
| 79 |
### Dictionaries (`dictionaries/`) |
| 80 |
|
| 81 |
Provides hints to the fuzzing engine about inputs that might trigger unique code paths. Each fuzz target may have a |
| 82 |
corresponding `.dict` file. For information about dictionary syntax, refer to |
| 83 |
the [LibFuzzer documentation on the subject](https://llvm.org/docs/LibFuzzer.html#dictionaries). |
| 84 |
|
| 85 |
**Things to Know**: |
| 86 |
|
| 87 |
- OSS-Fuzz loads dictionary files per fuzz target if one exists with the same name, all others are ignored. |
| 88 |
- Most entries in the dictionary files found here are escaped byte values that were recommended by the fuzzing |
| 89 |
engine after previous runs. |
| 90 |
- A default set of dictionary entries are created for all fuzz targets as part of the build process, regardless of an |
| 91 |
existing file here. |
| 92 |
- Development or updates to dictionaries should reflect the varied formats and edge cases relevant to the |
| 93 |
functionalities under test. |
| 94 |
- Example dictionaries (some of which are used to build the default dictionaries mentioned above) can be found here: |
| 95 |
- [AFL++ dictionary repository](https://github.com/AFLplusplus/AFLplusplus/tree/stable/dictionaries#readme) |
| 96 |
- [Google/fuzzing dictionary repository](https://github.com/google/fuzzing/tree/master/dictionaries) |
| 97 |
|
| 98 |
### OSS-Fuzz Scripts (`oss-fuzz-scripts/`) |
| 99 |
|
| 100 |
Includes scripts for building and integrating fuzz targets with OSS-Fuzz: |
| 101 |
|
| 102 |
- **`container-environment-bootstrap.sh`** - Sets up the execution environment. It is responsible for fetching default |
| 103 |
dictionary entries and ensuring all required build dependencies are installed and up-to-date. |
| 104 |
- **`build.sh`** - Executed within the Docker container, this script builds fuzz targets with necessary instrumentation |
| 105 |
and prepares seed corpora and dictionaries for use. |
| 106 |
|
| 107 |
**Where to learn more:** |
| 108 |
|
| 109 |
- [OSS-Fuzz documentation on the build.sh](https://google.github.io/oss-fuzz/getting-started/new-project-guide/#buildsh) |
| 110 |
- [See jsonpickle's build.sh and Dockerfile in the OSS-Fuzz repository](https://github.com/google/oss-fuzz/tree/master/projects/jsonpickle) |
| 111 |
|
| 112 |
## Running Fuzzers Locally |
| 113 |
|
| 114 |
This approach uses Docker images provided by OSS-Fuzz for building and running fuzz tests locally. It offers |
| 115 |
comprehensive features but requires a local clone of the OSS-Fuzz repository and sufficient disk space for Docker |
| 116 |
containers. |
| 117 |
|
| 118 |
### Build the Execution Environment |
| 119 |
|
| 120 |
Clone the OSS-Fuzz repository and prepare the Docker environment: |
| 121 |
|
| 122 |
```shell |
| 123 |
git clone --depth 1 https://github.com/google/oss-fuzz.git oss-fuzz |
| 124 |
cd oss-fuzz |
| 125 |
python infra/helper.py build_image jsonpickle |
| 126 |
python infra/helper.py build_fuzzers --sanitizer address jsonpickle |
| 127 |
``` |
| 128 |
|
| 129 |
> [!TIP] |
| 130 |
> The `build_fuzzers` command above accepts a local file path pointing to your jsonpickle repository clone as the last |
| 131 |
> argument. |
| 132 |
> This makes it easy to build fuzz targets you are developing locally in this repository without changing anything in |
| 133 |
> the OSS-Fuzz repo! |
| 134 |
> For example, if you have cloned this repository (or a fork of it) into: `~/code/jsonpickle` |
| 135 |
> Then running this command would build new or modified fuzz targets using the `~/code/jsonpickle/fuzzing/fuzz-targets` |
| 136 |
> directory: |
| 137 |
> ```shell |
| 138 |
> python infra/helper.py build_fuzzers --sanitizer address jsonpickle ~/code/jsonpickle |
| 139 |
> ``` |
| 140 |
|
| 141 |
Verify the build of your fuzzers with the optional `check_build` command: |
| 142 |
|
| 143 |
```shell |
| 144 |
python infra/helper.py check_build jsonpickle |
| 145 |
``` |
| 146 |
|
| 147 |
### Run a Fuzz Target |
| 148 |
|
| 149 |
Setting an environment variable for the fuzz target argument of the execution command makes it easier to quickly select |
| 150 |
a different target between runs: |
| 151 |
|
| 152 |
```shell |
| 153 |
# specify the fuzz target without the .py extension: |
| 154 |
export FUZZ_TARGET=fuzz_unpickle |
| 155 |
``` |
| 156 |
|
| 157 |
Execute the desired fuzz target: |
| 158 |
|
| 159 |
```shell |
| 160 |
python infra/helper.py run_fuzzer jsonpickle $FUZZ_TARGET -- -max_total_time=60 -print_final_stats=1 |
| 161 |
``` |
| 162 |
|
| 163 |
> [!TIP] |
| 164 |
> In the example above, the "`-- -max_total_time=60 -print_final_stats=1`" portion of the command is optional but quite |
| 165 |
> useful. |
| 166 |
> |
| 167 |
> Every argument provided after "`--`" in the above command is passed to the fuzzing engine directly. In this case: |
| 168 |
> - `-max_total_time=60` tells the LibFuzzer to stop execution after 60 seconds have elapsed. |
| 169 |
> - `-print_final_stats=1` tells the LibFuzzer to print a summary of useful metrics about the target run upon |
| 170 |
completion. |
| 171 |
> |
| 172 |
> But almost any [LibFuzzer option listed in the documentation](https://llvm.org/docs/LibFuzzer.html#options) should |
| 173 |
> work as well. |
| 174 |
|
| 175 |
#### Next Steps |
| 176 |
|
| 177 |
For detailed instructions on advanced features like reproducing OSS-Fuzz issues or using the Fuzz Introspector, refer |
| 178 |
to [the official OSS-Fuzz documentation][oss-fuzz-docs]. |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
[oss-fuzz-repo]: https://github.com/google/oss-fuzz |
| 183 |
|
| 184 |
[oss-fuzz-docs]: https://google.github.io/oss-fuzz |
| 185 |
|
| 186 |
[oss-fuzz-issue-tracker]: https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:jsonpickle |
| 187 |
|
| 188 |
[google-fuzzing-repo]: https://github.com/google/fuzzing |
| 189 |
|
| 190 |
[atheris-repo]: https://github.com/google/atheris |