xml_parse_fuzzer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <stdint.h>
  18. #include "expat.h"
  19. #include "siphash.h"
  20. // Macros to convert preprocessor macros to string literals. See
  21. // https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
  22. #define xstr(s) str(s)
  23. #define str(s) #s
  24. // The encoder type that we wish to fuzz should come from the compile-time
  25. // definition `ENCODING_FOR_FUZZING`. This allows us to have a separate fuzzer
  26. // binary for
  27. #ifndef ENCODING_FOR_FUZZING
  28. # error "ENCODING_FOR_FUZZING was not provided to this fuzz target."
  29. #endif
  30. // 16-byte deterministic hash key.
  31. static unsigned char hash_key[16] = "FUZZING IS FUN!";
  32. static void XMLCALL
  33. start(void *userData, const XML_Char *name, const XML_Char **atts) {
  34. (void)userData;
  35. (void)name;
  36. (void)atts;
  37. }
  38. static void XMLCALL
  39. end(void *userData, const XML_Char *name) {
  40. (void)userData;
  41. (void)name;
  42. }
  43. static void XMLCALL
  44. may_stop_character_handler(void *userData, const XML_Char *s, int len) {
  45. XML_Parser parser = (XML_Parser)userData;
  46. if (len > 1 && s[0] == 's') {
  47. XML_StopParser(parser, s[1] == 'r' ? XML_FALSE : XML_TRUE);
  48. }
  49. }
  50. static void
  51. ParseOneInput(XML_Parser p, const uint8_t *data, size_t size) {
  52. // Set the hash salt using siphash to generate a deterministic hash.
  53. struct sipkey *key = sip_keyof(hash_key);
  54. XML_SetHashSalt(p, (unsigned long)siphash24(data, size, key));
  55. (void)sip24_valid;
  56. XML_SetUserData(p, p);
  57. XML_SetElementHandler(p, start, end);
  58. XML_SetCharacterDataHandler(p, may_stop_character_handler);
  59. XML_Parse(p, (const XML_Char *)data, size, 0);
  60. if (XML_Parse(p, (const XML_Char *)data, size, 1) == XML_STATUS_ERROR) {
  61. XML_ErrorString(XML_GetErrorCode(p));
  62. }
  63. XML_GetCurrentLineNumber(p);
  64. if (size % 2) {
  65. XML_ParserReset(p, NULL);
  66. }
  67. }
  68. int
  69. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  70. XML_Parser parentParser = XML_ParserCreate(xstr(ENCODING_FOR_FUZZING));
  71. assert(parentParser);
  72. ParseOneInput(parentParser, data, size);
  73. // not freed yet, but used later and freed then
  74. XML_Parser namespaceParser = XML_ParserCreateNS(NULL, '!');
  75. assert(namespaceParser);
  76. ParseOneInput(namespaceParser, data, size);
  77. XML_ParserFree(namespaceParser);
  78. XML_Parser externalEntityParser
  79. = XML_ExternalEntityParserCreate(parentParser, "e1", NULL);
  80. assert(externalEntityParser);
  81. ParseOneInput(externalEntityParser, data, size);
  82. XML_ParserFree(externalEntityParser);
  83. XML_Parser externalDtdParser
  84. = XML_ExternalEntityParserCreate(parentParser, NULL, NULL);
  85. assert(externalDtdParser);
  86. ParseOneInput(externalDtdParser, data, size);
  87. XML_ParserFree(externalDtdParser);
  88. // finally frees this parser which served as parent
  89. XML_ParserFree(parentParser);
  90. return 0;
  91. }