1
0

xml_parsebuffer_fuzzer.c 3.6 KB

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