Repository navigation
test: fuzz the shim's annotation injection - #1195
Conversation
👷 Deploy request for urunc pending review.Visit the deploys page to approve it
|
cmainas
left a comment
There was a problem hiding this comment.
Hello @pocopepe ,
thank you for the PR. There is a problem with the current design. The seeds of the two new targets get garbled on their way to ClusterFuzzLite. libFuzzer stores every input as one byte string, so addStdLibCorpusToFuzzer joins a seed's arguments together without recording where each one ends, and the fuzzer later cuts that string back into equal-sized pieces. For example, the seed {} of FuzzPatchConfigJSON ends up with config {}com.uru. Only single-[]byte targets survive the conversion.
I think it would be simpler to have each target take a single []byte and fuzz just the data: the manifest in FuzzFetchUruncAnnotations, with the media type and label fixed (OCI and Docker manifests go through the same code anyway), and the config in FuzzPatchConfigJSON. The other cases, like read failures, broken streams and non-manifest media types, would fit better in a regular table test. The seeds can then be a couple of inline JSON literals, so seeds.toml and the loader wouldn't be needed.
Please, let me know your opinion.
I have also added some inline comments.
| fuzzer=FuzzGetUnikernelConfig | ||
| compile_native_go_fuzzer_v2 github.com/urunc-dev/urunc/pkg/unikontainers "$fuzzer" "$fuzzer" | ||
| # Every Fuzz function of a test file, as <package dir>:<name>. | ||
| targets=$(grep -rHoE --include='*_test.go' '^func Fuzz[A-Za-z0-9_]+' pkg internal | sed 's|/[^/]*:func |:|' | sort -u) |
There was a problem hiding this comment.
It would be better to use go test list here instead of grep The current filter will match with any function that starts with Fuzz, evne helper ones which are not actual fuzzing functions.
There was a problem hiding this comment.
changed, it now uses go test -list '^Fuzz'. I also added set -o pipefail, so a package that fails to compile can't silently drop its targets just in case.
| if !maps.Equal(out.Annotations, want) { | ||
| t.Fatalf("annotations %v, want %v", out.Annotations, want) | ||
| } | ||
| // Nothing else changes. |
There was a problem hiding this comment.
nit: The comment is not very accurate. Some more things change. Fields that are not recognized by the runtime-spec version that the shim uses will get dropped.
| } | ||
| } | ||
| annotations := map[string]string{key1: value1, key2: value2} | ||
| if err := os.WriteFile(path, config, 0o640); err != nil { //nolint:gosec // a non-default mode, to check it is kept |
There was a problem hiding this comment.
We should be careful here with umask. Even if we set 0o640, umask might still change it. To enforce permissions, it is better to use chmod.
|
Hi @cmainas, thanks for the review. I've pushed the changes along the lines you suggested. I had hit the same equal split problem before and was considering a NUL separator to split the seeds, but a single |
fd5e021 to
fbc70cc
Compare
cmainas
left a comment
There was a problem hiding this comment.
Thank you @pocopepe for the update.
I would strongly recommend to have a follow up or a PR that explains the limitations and what future fuzzing targets should keep in mind. A few examples:
- The seeds should be added with
fuzzseed.Addand notf.Add - Any new fuzz target should be added in the Makefile list
- Be careful with go cache and errors from seed conversion.
Other than that, the Pr looks ok as a first step I have added a few comments, but feel free to ignore the future notes.
| # compile_native_go_fuzzer_v2 exits 0 when it cannot find the function. | ||
| test -x "$OUT/$fuzzer" | ||
|
|
||
| # libFuzzer ignores f.Add, so the seeds are dumped to files first. |
There was a problem hiding this comment.
Just as a note for the future as a TODO/follow-up. Go's cache can prevent the corpus form growing. THis is not a bug now because we use mktemp for a directory, but future additions should be mindful of that. Furthermore, every seed that can not be read / parsed wlll get passed over unnoticed.
There was a problem hiding this comment.
Noted, I'd add it in the fuzzing doc
|
|
||
| // Add is f.Add for a single-[]byte target, the only shape libFuzzer keeps intact. When | ||
| // URUNC_SEED_DIR is set it also writes the seed there in the "go test fuzz v1" format. | ||
| func Add(f *testing.F, seed []byte) { |
There was a problem hiding this comment.
Nit: We should mark this as a helper function so any errors (e.g. when we write the file), point to the actual function.
| } | ||
| file := fmt.Sprintf("go test fuzz v1\n[]byte(%q)\n", seed) | ||
| name := fmt.Sprintf("%x", sha256.Sum256([]byte(file))) | ||
| if err := os.WriteFile(filepath.Join(dir, name), []byte(file), 0o600); err != nil { //nolint:gosec // name is a hash |
There was a problem hiding this comment.
nit: Another linter issue here is that the name comes from an environment variable.
| wantErr := json.Unmarshal(manifest, &parsed) != nil | ||
| if (err != nil) != wantErr || (err != nil && got != nil) { |
There was a problem hiding this comment.
Let's simplify these lines.
| t.Fatal(err) | ||
| } | ||
| if entries, _ := os.ReadDir(bundle); len(entries) != 1 { | ||
| t.Fatalf("%d files left in the bundle", len(entries)) |
There was a problem hiding this comment.
We can also show the error from above here.
There was a problem hiding this comment.
We need to add this file in the unit tests target in Makefile. See https://github.com/urunc-dev/urunc/blob/main/Makefile#L242
| const ( | ||
| testNamespace = "k8s.io" | ||
| testDigest = "sha256:x" | ||
| ociManifest = "application/vnd.oci.image.manifest.v1+json" |
There was a problem hiding this comment.
Use imagespec.MediaTypeImageManifest instead.
| store: fakeStore{chunks: [][]byte{[]byte(manifest[:7]), []byte(manifest[7:])}}, | ||
| want: map[string]string{labelKey: "manifest", uruncPrefix + "b": "2"}, | ||
| wantReads: 1}, | ||
| {name: "docker manifest", mediaType: "application/vnd.docker.distribution.manifest.v2+json", |
There was a problem hiding this comment.
Use images.MediaTypeDockerSchema2Manifest instead.
| store: fakeStore{chunks: [][]byte{[]byte(manifest)}}, | ||
| want: map[string]string{labelKey: "manifest", uruncPrefix + "b": "2"}, | ||
| wantReads: 1}, | ||
| {name: "index is not read", mediaType: "application/vnd.oci.image.index.v1+json", |
There was a problem hiding this comment.
Use imagespec.MediaTypeImageIndex instead.
ref urunc-dev#852 Signed-off-by: viju <avijusanjai@gmail.com>
ref urunc-dev#852 Signed-off-by: viju <avijusanjai@gmail.com>
ref urunc-dev#852 Signed-off-by: viju <avijusanjai@gmail.com>
fbc70cc to
ab23821
Compare
|
Hi @cmainas, thanks again for the review. I've pushed the fixes for the inline comments and rebased on main. I'll follow up with a separate PR that adds a short fuzzing doc to the developer guide covering it. |
Description
This adds fuzzers for
fetchUruncAnnotationsandpatchConfigJSONin the containerd shim. It also adds grepping the Fuzz targets rather than manual additions inside build.sh. Adds a new way of building the seeds from a centrailized toml file that can be updated for all the targets.Related issues
How was this tested?
It was tested by running the fuzzer on my branch. Plus a quick make fuzz was ran, nothing broke.
LLM usage
Opus 5.5
Checklist
make lint).make test_ctr,make test_nerdctl,make test_docker,make test_crictl).