From d2074fcdb84bd1aef3d7919bfa67da75daa3c0fe Mon Sep 17 00:00:00 2001 From: Brendan Chang Date: Mon, 21 Oct 2019 18:54:28 -0400 Subject: [PATCH 1/4] Add fuzz targets for Duration, MicroTime, and Time --- test/fuzz/yaml/BUILD | 1 + test/fuzz/yaml/yaml.go | 60 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/test/fuzz/yaml/BUILD b/test/fuzz/yaml/BUILD index 3e47be63dd7..2e151687dd7 100644 --- a/test/fuzz/yaml/BUILD +++ b/test/fuzz/yaml/BUILD @@ -6,6 +6,7 @@ go_library( importpath = "k8s.io/kubernetes/test/fuzz/yaml", visibility = ["//visibility:private"], deps = [ + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/gopkg.in/yaml.v2:go_default_library", "//vendor/sigs.k8s.io/yaml:go_default_library", ], diff --git a/test/fuzz/yaml/yaml.go b/test/fuzz/yaml/yaml.go index 634e120655a..8661286c687 100644 --- a/test/fuzz/yaml/yaml.go +++ b/test/fuzz/yaml/yaml.go @@ -20,10 +20,51 @@ limitations under the License. package yaml import ( + "bytes" + "gopkg.in/yaml.v2" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" sigyaml "sigs.k8s.io/yaml" ) +// FuzzDuration is a fuzz target for unmarshaling Duration defined in "k8s.io/apimachinery/pkg/apis/meta/v1". +// This target also checks that the unmarshaled result can be marshaled back to the input. +func FuzzDuration(b []byte) int { + var unmarshalResult struct { + D metav1.Duration `json:"d"` + } + if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { + return 0 + } + marshalResult, err := yaml.Marshal(&unmarshalResult) + if err != nil { + panic(err) + } + if !bytes.Equal(marshalResult, b) { + panic("marshalResult != input") + } + return 1 +} + +// FuzzMicroTime is a fuzz target for unmarshaling MicroTime defined in "k8s.io/apimachinery/pkg/apis/meta/v1". +// This target also checks that the unmarshaled result can be marshaled back to the input. +func FuzzMicroTime(b []byte) int { + var unmarshalResult struct { + T metav1.MicroTime `json:"t"` + } + if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { + return 0 + } + marshalResult, err := yaml.Marshal(&unmarshalResult) + if err != nil { + panic(err) + } + if !bytes.Equal(marshalResult, b) { + panic("marshalResult != input") + } + return 1 +} + // FuzzSigYaml is a fuzz target for "sigs.k8s.io/yaml" unmarshaling. func FuzzSigYaml(b []byte) int { t := struct{}{} @@ -38,6 +79,25 @@ func FuzzSigYaml(b []byte) int { return out } +// FuzzTime is a fuzz target for unmarshaling Time defined in "k8s.io/apimachinery/pkg/apis/meta/v1". +// This target also checks that the unmarshaled result can be marshaled back to the input. +func FuzzTime(b []byte) int { + var unmarshalResult struct { + T metav1.Time `json:"t"` + } + if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { + return 0 + } + marshalResult, err := yaml.Marshal(&unmarshalResult) + if err != nil { + panic(err) + } + if !bytes.Equal(marshalResult, b) { + panic("marshalResult != input") + } + return 1 +} + // FuzzYamlV2 is a fuzz target for "gopkg.in/yaml.v2" unmarshaling. func FuzzYamlV2(b []byte) int { t := struct{}{} From 862e814f82749204489880cfed6ba0a9f9632861 Mon Sep 17 00:00:00 2001 From: Brendan Chang Date: Tue, 22 Oct 2019 16:41:49 -0400 Subject: [PATCH 2/4] Use sigyaml for the metav1 fuzz targets --- test/fuzz/yaml/yaml.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/fuzz/yaml/yaml.go b/test/fuzz/yaml/yaml.go index 8661286c687..2fcbbf6c72c 100644 --- a/test/fuzz/yaml/yaml.go +++ b/test/fuzz/yaml/yaml.go @@ -36,7 +36,7 @@ func FuzzDuration(b []byte) int { if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { return 0 } - marshalResult, err := yaml.Marshal(&unmarshalResult) + marshalResult, err := sigyaml.Marshal(&unmarshalResult) if err != nil { panic(err) } @@ -55,7 +55,7 @@ func FuzzMicroTime(b []byte) int { if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { return 0 } - marshalResult, err := yaml.Marshal(&unmarshalResult) + marshalResult, err := sigyaml.Marshal(&unmarshalResult) if err != nil { panic(err) } @@ -85,7 +85,7 @@ func FuzzTime(b []byte) int { var unmarshalResult struct { T metav1.Time `json:"t"` } - if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { + if err := sigyaml.Unmarshal(b, &unmarshalResult); err != nil { return 0 } marshalResult, err := yaml.Marshal(&unmarshalResult) From 7aa94348111785c31d86b7d09bc3ac7a82b3c5de Mon Sep 17 00:00:00 2001 From: Brendan Chang Date: Wed, 23 Oct 2019 19:16:50 -0400 Subject: [PATCH 3/4] Use strict unmarshaling for metav1 fuzz targets --- test/fuzz/yaml/yaml.go | 49 ++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/test/fuzz/yaml/yaml.go b/test/fuzz/yaml/yaml.go index 2fcbbf6c72c..2a31673a784 100644 --- a/test/fuzz/yaml/yaml.go +++ b/test/fuzz/yaml/yaml.go @@ -27,40 +27,42 @@ import ( sigyaml "sigs.k8s.io/yaml" ) -// FuzzDuration is a fuzz target for unmarshaling Duration defined in "k8s.io/apimachinery/pkg/apis/meta/v1". -// This target also checks that the unmarshaled result can be marshaled back to the input. -func FuzzDuration(b []byte) int { - var unmarshalResult struct { +// FuzzDuration is a fuzz target for strict-unmarshaling Duration defined in +// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the +// unmarshaled result can be marshaled back to the input. +func FuzzDurationStrict(b []byte) int { + var durationHolder struct { D metav1.Duration `json:"d"` } - if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { + if err := sigyaml.UnmarshalStrict(b, &durationHolder); err != nil { return 0 } - marshalResult, err := sigyaml.Marshal(&unmarshalResult) + result, err := sigyaml.Marshal(&durationHolder) if err != nil { panic(err) } - if !bytes.Equal(marshalResult, b) { - panic("marshalResult != input") + if !bytes.Equal(result, b) { + panic("result != input") } return 1 } -// FuzzMicroTime is a fuzz target for unmarshaling MicroTime defined in "k8s.io/apimachinery/pkg/apis/meta/v1". -// This target also checks that the unmarshaled result can be marshaled back to the input. -func FuzzMicroTime(b []byte) int { - var unmarshalResult struct { +// FuzzMicroTime is a fuzz target for strict-unmarshaling MicroTime defined in +// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the +// unmarshaled result can be marshaled back to the input. +func FuzzMicroTimeStrict(b []byte) int { + var microTimeHolder struct { T metav1.MicroTime `json:"t"` } - if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { + if err := sigyaml.UnmarshalStrict(b, µTimeHolder); err != nil { return 0 } - marshalResult, err := sigyaml.Marshal(&unmarshalResult) + result, err := sigyaml.Marshal(µTimeHolder) if err != nil { panic(err) } - if !bytes.Equal(marshalResult, b) { - panic("marshalResult != input") + if !bytes.Equal(result, b) { + panic("result != input") } return 1 } @@ -79,21 +81,22 @@ func FuzzSigYaml(b []byte) int { return out } -// FuzzTime is a fuzz target for unmarshaling Time defined in "k8s.io/apimachinery/pkg/apis/meta/v1". -// This target also checks that the unmarshaled result can be marshaled back to the input. +// FuzzTime is a fuzz target for strict-unmarshaling Time defined in +// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the +// unmarshaled result can be marshaled back to the input. func FuzzTime(b []byte) int { - var unmarshalResult struct { + var timeHolder struct { T metav1.Time `json:"t"` } - if err := sigyaml.Unmarshal(b, &unmarshalResult); err != nil { + if err := sigyaml.UnmarshalStrict(b, &timeHolder); err != nil { return 0 } - marshalResult, err := yaml.Marshal(&unmarshalResult) + result, err := sigyaml.Marshal(&timeHolder) if err != nil { panic(err) } - if !bytes.Equal(marshalResult, b) { - panic("marshalResult != input") + if !bytes.Equal(result, b) { + panic("result != input") } return 1 } From 9ef94b2d4786ce35ba429fa0cfe95066a6b69fcd Mon Sep 17 00:00:00 2001 From: Brendan Chang Date: Fri, 25 Oct 2019 11:44:49 -0400 Subject: [PATCH 4/4] Fixing comments FuzzDurationStrict and FuzzMicroTimeStrict --- test/fuzz/yaml/yaml.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/fuzz/yaml/yaml.go b/test/fuzz/yaml/yaml.go index 2a31673a784..da4f8ed3a92 100644 --- a/test/fuzz/yaml/yaml.go +++ b/test/fuzz/yaml/yaml.go @@ -27,8 +27,8 @@ import ( sigyaml "sigs.k8s.io/yaml" ) -// FuzzDuration is a fuzz target for strict-unmarshaling Duration defined in -// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the +// FuzzDurationStrict is a fuzz target for strict-unmarshaling Duration defined +// in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the // unmarshaled result can be marshaled back to the input. func FuzzDurationStrict(b []byte) int { var durationHolder struct { @@ -47,9 +47,9 @@ func FuzzDurationStrict(b []byte) int { return 1 } -// FuzzMicroTime is a fuzz target for strict-unmarshaling MicroTime defined in -// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the -// unmarshaled result can be marshaled back to the input. +// FuzzMicroTimeStrict is a fuzz target for strict-unmarshaling MicroTime +// defined in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks +// that the unmarshaled result can be marshaled back to the input. func FuzzMicroTimeStrict(b []byte) int { var microTimeHolder struct { T metav1.MicroTime `json:"t"`