/
yaml.go
141 lines (130 loc) · 4.18 KB
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package yaml implements fuzzers for yaml deserialization routines in
// Kubernetes. These targets are compatible with the github.com/dvyukov/go-fuzz
// fuzzing framework.
package yaml
import (
"fmt"
"strings"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
sigyaml "sigs.k8s.io/yaml"
)
// 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 {
D metav1.Duration `json:"d"`
}
if err := sigyaml.UnmarshalStrict(b, &durationHolder); err != nil {
return 0
}
result, err := sigyaml.Marshal(&durationHolder)
if err != nil {
panic(err)
}
// Result is in the format "d: <duration>\n", so strip off the trailing
// newline and convert durationHolder.D to the expected format.
resultStr := strings.TrimSpace(string(result[:]))
inputStr := fmt.Sprintf("d: %s", durationHolder.D.Duration)
if resultStr != inputStr {
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
}
return 1
}
// 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"`
}
if err := sigyaml.UnmarshalStrict(b, µTimeHolder); err != nil {
return 0
}
result, err := sigyaml.Marshal(µTimeHolder)
if err != nil {
panic(err)
}
// Result is in the format "t: <time>\n", so strip off the trailing
// newline and convert microTimeHolder.T to the expected format. If
// time is zero, the value is marshaled to "null".
resultStr := strings.TrimSpace(string(result[:]))
var inputStr string
if microTimeHolder.T.Time.IsZero() {
inputStr = "t: null"
} else {
inputStr = fmt.Sprintf("t: %s", microTimeHolder.T.Time)
}
if resultStr != inputStr {
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
}
return 1
}
// FuzzSigYaml is a fuzz target for "sigs.k8s.io/yaml" unmarshaling.
func FuzzSigYaml(b []byte) int {
t := struct{}{}
m := map[string]interface{}{}
var out int
if err := sigyaml.Unmarshal(b, &m); err == nil {
out = 1
}
if err := sigyaml.Unmarshal(b, &t); err == nil {
out = 1
}
return out
}
// FuzzTimeStrict 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 FuzzTimeStrict(b []byte) int {
var timeHolder struct {
T metav1.Time `json:"t"`
}
if err := sigyaml.UnmarshalStrict(b, &timeHolder); err != nil {
return 0
}
result, err := sigyaml.Marshal(&timeHolder)
if err != nil {
panic(err)
}
// Result is in the format "t: <time>\n", so strip off the trailing
// newline and convert timeHolder.T to the expected format. If time is
// zero, the value is marshaled to "null".
resultStr := strings.TrimSpace(string(result[:]))
var inputStr string
if timeHolder.T.Time.IsZero() {
inputStr = "t: null"
} else {
inputStr = fmt.Sprintf("t: %s", timeHolder.T.Time)
}
if resultStr != inputStr {
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
}
return 1
}
// FuzzYamlV2 is a fuzz target for "gopkg.in/yaml.v2" unmarshaling.
func FuzzYamlV2(b []byte) int {
t := struct{}{}
m := map[string]interface{}{}
var out int
if err := yaml.Unmarshal(b, &m); err == nil {
out = 1
}
if err := yaml.Unmarshal(b, &t); err == nil {
out = 1
}
return out
}