istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/fuzz/misc_fuzzers.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // This file has a series of fuzzers that target different 16 // parts of Istio. They are placed here because it does not 17 // make sense to place them in different files yet. 18 // The fuzzers can be moved to other files without anything 19 // breaking on the OSS-fuzz side. 20 21 package fuzz 22 23 import ( 24 fuzz "github.com/AdaLogics/go-fuzz-headers" 25 26 "istio.io/api/operator/v1alpha1" 27 "istio.io/istio/operator/pkg/apis/istio" 28 "istio.io/istio/operator/pkg/apis/istio/v1alpha1/validation" 29 "istio.io/istio/operator/pkg/controlplane" 30 "istio.io/istio/operator/pkg/name" 31 "istio.io/istio/operator/pkg/object" 32 "istio.io/istio/operator/pkg/patch" 33 "istio.io/istio/operator/pkg/translate" 34 "istio.io/istio/operator/pkg/util" 35 "istio.io/istio/operator/pkg/validate" 36 "istio.io/istio/pkg/config/analysis/diag" 37 "istio.io/istio/pkg/config/resource" 38 ) 39 40 func FuzzCheckIstioOperatorSpec(data []byte) int { 41 f := fuzz.NewConsumer(data) 42 43 ispec := &v1alpha1.IstioOperatorSpec{} 44 err := f.GenerateStruct(ispec) 45 if err != nil { 46 return 0 47 } 48 _ = validate.CheckIstioOperatorSpec(ispec, false) 49 _ = validate.CheckIstioOperatorSpec(ispec, true) 50 return 1 51 } 52 53 func FuzzV1Alpha1ValidateConfig(data []byte) int { 54 f := fuzz.NewConsumer(data) 55 56 iop := &v1alpha1.IstioOperatorSpec{} 57 err := f.GenerateStruct(iop) 58 if err != nil { 59 return 0 60 } 61 _, _ = validation.ValidateConfig(false, iop) 62 return 1 63 } 64 65 func FuzzGetEnabledComponents(data []byte) int { 66 f := fuzz.NewConsumer(data) 67 68 iopSpec := &v1alpha1.IstioOperatorSpec{} 69 err := f.GenerateStruct(iopSpec) 70 if err != nil { 71 return 0 72 } 73 _, _ = translate.GetEnabledComponents(iopSpec) 74 return 1 75 } 76 77 func FuzzUnmarshalAndValidateIOPS(data []byte) int { 78 _, _ = istio.UnmarshalAndValidateIOPS(string(data)) 79 return 1 80 } 81 82 func FuzzRenderManifests(data []byte) int { 83 f := fuzz.NewConsumer(data) 84 f.AllowUnexportedFields() 85 86 cp := &controlplane.IstioControlPlane{} 87 err := f.GenerateStruct(cp) 88 if err != nil { 89 return 0 90 } 91 _, _ = cp.RenderManifest() 92 return 1 93 } 94 95 func FuzzOverlayIOP(data []byte) int { 96 f := fuzz.NewConsumer(data) 97 98 base, err := f.GetString() 99 if err != nil { 100 return 0 101 } 102 overlay, err := f.GetString() 103 if err != nil { 104 return 0 105 } 106 _, err = util.OverlayIOP(base, overlay) 107 if err != nil { 108 return 0 109 } 110 return 1 111 } 112 113 func FuzzNewControlplane(data []byte) int { 114 f := fuzz.NewConsumer(data) 115 f.AllowUnexportedFields() 116 117 inInstallSpec := &v1alpha1.IstioOperatorSpec{} 118 err := f.GenerateStruct(inInstallSpec) 119 if err != nil { 120 return 0 121 } 122 inTranslator := &translate.Translator{} 123 err = f.GenerateStruct(inTranslator) 124 if err != nil { 125 return 0 126 } 127 if inTranslator.APIMapping == nil { 128 return 0 129 } 130 if inTranslator.KubernetesMapping == nil { 131 return 0 132 } 133 if inTranslator.GlobalNamespaces == nil { 134 return 0 135 } 136 if inTranslator.ComponentMaps == nil { 137 return 0 138 } 139 cm := &translate.ComponentMaps{} 140 err = f.GenerateStruct(cm) 141 if err != nil { 142 return 0 143 } 144 inTranslator.ComponentMaps[name.PilotComponentName] = cm 145 _, _ = controlplane.NewIstioControlPlane(inInstallSpec, inTranslator, nil, nil) 146 return 1 147 } 148 149 func FuzzResolveK8sConflict(data []byte) int { 150 f := fuzz.NewConsumer(data) 151 f.AllowUnexportedFields() 152 153 ko1 := &object.K8sObject{} 154 err := f.GenerateStruct(ko1) 155 if err != nil { 156 return 0 157 } 158 _ = ko1.ResolveK8sConflict() 159 return 1 160 } 161 162 func FuzzYAMLManifestPatch(data []byte) int { 163 f := fuzz.NewConsumer(data) 164 f.AllowUnexportedFields() 165 166 overlay := []*v1alpha1.K8SObjectOverlay{} 167 number, err := f.GetInt() 168 if err != nil { 169 return 0 170 } 171 for i := 0; i < number%20; i++ { 172 o := &v1alpha1.K8SObjectOverlay{} 173 err := f.GenerateStruct(o) 174 if err != nil { 175 return 0 176 } 177 overlay = append(overlay, o) 178 } 179 baseYAML, err := f.GetString() 180 if err != nil { 181 return 0 182 } 183 defaultNamespace, err := f.GetString() 184 if err != nil { 185 return 0 186 } 187 188 _, _ = patch.YAMLManifestPatch(baseYAML, defaultNamespace, overlay) 189 return 1 190 } 191 192 func FuzzGalleyDiag(data []byte) int { 193 f := fuzz.NewConsumer(data) 194 195 code, err := f.GetString() 196 if err != nil { 197 return 0 198 } 199 templ, err := f.GetString() 200 if err != nil { 201 return 0 202 } 203 mt := diag.NewMessageType(diag.Error, code, templ) 204 resourceIsNil, err := f.GetBool() 205 if err != nil { 206 return 0 207 } 208 parameter, err := f.GetString() 209 if err != nil { 210 return 0 211 } 212 var ri *resource.Instance 213 if resourceIsNil { 214 ri = nil 215 } else { 216 err = f.GenerateStruct(ri) 217 if err != nil { 218 return 0 219 } 220 } 221 m := diag.NewMessage(mt, ri, parameter) 222 _ = m.Unstructured(true) 223 _ = m.UnstructuredAnalysisMessageBase() 224 _ = m.Origin() 225 _, _ = m.MarshalJSON() 226 _ = m.String() 227 replStr, err := f.GetString() 228 if err == nil { 229 _ = m.ReplaceLine(replStr) 230 } 231 return 1 232 }