1
0

xml_parse_fuzzer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <assert.h>
  17. #include <limits.h> // for INT_MAX
  18. #include <stdint.h>
  19. #include "expat.h"
  20. #include "siphash.h"
  21. // Macros to convert preprocessor macros to string literals. See
  22. // https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
  23. #define xstr(s) str(s)
  24. #define str(s) #s
  25. // The encoder type that we wish to fuzz should come from the compile-time
  26. // definition `ENCODING_FOR_FUZZING`. This allows us to have a separate fuzzer
  27. // binary for
  28. #ifndef ENCODING_FOR_FUZZING
  29. # error "ENCODING_FOR_FUZZING was not provided to this fuzz target."
  30. #endif
  31. // 16-byte deterministic hash key.
  32. static unsigned char hash_key[16] = "FUZZING IS FUN!";
  33. static void XMLCALL
  34. start(void *userData, const XML_Char *name, const XML_Char **atts) {
  35. (void)userData;
  36. (void)name;
  37. (void)atts;
  38. }
  39. static void XMLCALL
  40. end(void *userData, const XML_Char *name) {
  41. (void)userData;
  42. (void)name;
  43. }
  44. static void XMLCALL
  45. may_stop_character_handler(void *userData, const XML_Char *s, int len) {
  46. XML_Parser parser = (XML_Parser)userData;
  47. if (len > 1 && s[0] == 's') {
  48. XML_StopParser(parser, s[1] == 'r' ? XML_FALSE : XML_TRUE);
  49. }
  50. }
  51. static void
  52. ParseOneInput(XML_Parser p, const uint8_t *data, size_t size) {
  53. // Set the hash salt using siphash to generate a deterministic hash.
  54. struct sipkey *key = sip_keyof(hash_key);
  55. XML_SetHashSalt(p, (unsigned long)siphash24(data, size, key));
  56. (void)sip24_valid;
  57. XML_SetUserData(p, p);
  58. XML_SetElementHandler(p, start, end);
  59. XML_SetCharacterDataHandler(p, may_stop_character_handler);
  60. assert(size <= INT_MAX);
  61. XML_Parse(p, (const XML_Char *)data, (int)size, 0);
  62. if (XML_Parse(p, (const XML_Char *)data, (int)size, 1) == XML_STATUS_ERROR) {
  63. XML_ErrorString(XML_GetErrorCode(p));
  64. }
  65. XML_GetCurrentLineNumber(p);
  66. if (size % 2) {
  67. XML_ParserReset(p, NULL);
  68. }
  69. }
  70. int
  71. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  72. XML_Parser parentParser = XML_ParserCreate(xstr(ENCODING_FOR_FUZZING));
  73. assert(parentParser);
  74. ParseOneInput(parentParser, data, size);
  75. // not freed yet, but used later and freed then
  76. XML_Parser namespaceParser = XML_ParserCreateNS(NULL, '!');
  77. assert(namespaceParser);
  78. ParseOneInput(namespaceParser, data, size);
  79. XML_ParserFree(namespaceParser);
  80. XML_Parser externalEntityParser
  81. = XML_ExternalEntityParserCreate(parentParser, "e1", NULL);
  82. if (externalEntityParser != NULL) {
  83. ParseOneInput(externalEntityParser, data, size);
  84. XML_ParserFree(externalEntityParser);
  85. }
  86. XML_Parser externalDtdParser
  87. = XML_ExternalEntityParserCreate(parentParser, NULL, NULL);
  88. if (externalDtdParser != NULL) {
  89. ParseOneInput(externalDtdParser, data, size);
  90. XML_ParserFree(externalDtdParser);
  91. }
  92. // finally frees this parser which served as parent
  93. XML_ParserFree(parentParser);
  94. return 0;
  95. }