yaml.go 4.2 KB

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