xml_parsebuffer_fuzzer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * http://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 <string.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. void *buf = XML_GetBuffer(p, size);
  61. assert(buf);
  62. memcpy(buf, data, size);
  63. XML_ParseBuffer(p, size, 0);
  64. buf = XML_GetBuffer(p, size);
  65. if (buf == NULL) {
  66. return;
  67. }
  68. memcpy(buf, data, size);
  69. if (XML_ParseBuffer(p, size, 1) == XML_STATUS_ERROR) {
  70. XML_ErrorString(XML_GetErrorCode(p));
  71. }
  72. XML_GetCurrentLineNumber(p);
  73. if (size % 2) {
  74. XML_ParserReset(p, NULL);
  75. }
  76. }
  77. int
  78. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  79. if (size == 0)
  80. return 0;
  81. XML_Parser parentParser = XML_ParserCreate(xstr(ENCODING_FOR_FUZZING));
  82. assert(parentParser);
  83. ParseOneInput(parentParser, data, size);
  84. // not freed yet, but used later and freed then
  85. XML_Parser namespaceParser = XML_ParserCreateNS(NULL, '!');
  86. assert(namespaceParser);
  87. ParseOneInput(namespaceParser, data, size);
  88. XML_ParserFree(namespaceParser);
  89. XML_Parser externalEntityParser
  90. = XML_ExternalEntityParserCreate(parentParser, "e1", NULL);
  91. assert(externalEntityParser);
  92. ParseOneInput(externalEntityParser, data, size);
  93. XML_ParserFree(externalEntityParser);
  94. XML_Parser externalDtdParser
  95. = XML_ExternalEntityParserCreate(parentParser, NULL, NULL);
  96. assert(externalDtdParser);
  97. ParseOneInput(externalDtdParser, data, size);
  98. XML_ParserFree(externalDtdParser);
  99. // finally frees this parser which served as parent
  100. XML_ParserFree(parentParser);
  101. return 0;
  102. }