fuzz_binary_decode.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "custom_memory_manager.h"
  5. #include <ua_types.h>
  6. #include "ua_server_internal.h"
  7. #include "ua_config_default.h"
  8. #include "ua_log_stdout.h"
  9. #include "ua_types_encoding_binary.h"
  10. static UA_Boolean tortureEncoding(const uint8_t *data, size_t size, size_t *newOffset) {
  11. *newOffset = 0;
  12. if (size <= 2)
  13. return UA_FALSE;
  14. // get some random type
  15. uint16_t typeIndex = (uint16_t)(data[0] | data[1] << 8);
  16. data += 2;
  17. size -= 2;
  18. if (typeIndex >= UA_TYPES_COUNT)
  19. return UA_FALSE;
  20. void *dst = UA_new(&UA_TYPES[typeIndex]);
  21. const UA_ByteString binary = {
  22. size, //length
  23. (UA_Byte *) (void *) data
  24. };
  25. UA_StatusCode ret = UA_decodeBinary(&binary, newOffset, dst, &UA_TYPES[typeIndex], NULL);
  26. if (ret == UA_STATUSCODE_GOOD) {
  27. // copy the datatype to test
  28. void *dstCopy = UA_new(&UA_TYPES[typeIndex]);
  29. UA_copy(dst, dstCopy, &UA_TYPES[typeIndex]);
  30. UA_delete(dstCopy, &UA_TYPES[typeIndex]);
  31. // now also test encoding
  32. UA_ByteString encoded;
  33. UA_ByteString_allocBuffer(&encoded, *newOffset);
  34. const UA_Byte *end = &encoded.data[*newOffset];
  35. UA_Byte *pos = encoded.data;
  36. ret = UA_encodeBinary(dst, &UA_TYPES[typeIndex], &pos, &end, NULL, NULL);
  37. if (ret == UA_STATUSCODE_GOOD) {
  38. // do nothing
  39. }
  40. UA_ByteString_deleteMembers(&encoded);
  41. }
  42. UA_delete(dst, &UA_TYPES[typeIndex]);
  43. return UA_TRUE;
  44. }
  45. static UA_Boolean tortureExtensionObject(const uint8_t *data, size_t size, size_t *newOffset) {
  46. *newOffset = 0;
  47. // check if there is still enough data to create an extension object
  48. // we need at least a nodeid.numeric which equals to 4 bytes
  49. if (size < 4)
  50. return UA_FALSE;
  51. UA_UInt32 identifier = (UA_UInt32)(data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24);
  52. size-= 4;
  53. UA_NodeId objectId = UA_NODEID_NUMERIC(0, identifier);
  54. UA_ExtensionObject obj;
  55. UA_ExtensionObject_init(&obj);
  56. obj.encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
  57. obj.content.encoded.typeId = objectId;
  58. obj.content.encoded.body.length = size;
  59. obj.content.encoded.body.data = (UA_Byte*)(void*)data; // discard const. We are sure that we don't change it
  60. const UA_DataType *type = UA_findDataTypeByBinary(&obj.content.encoded.typeId);
  61. UA_StatusCode ret = UA_STATUSCODE_GOOD;
  62. if (type) {
  63. void *dstCopy = UA_new(type);
  64. ret = UA_decodeBinary(&obj.content.encoded.body, newOffset, dstCopy, type, NULL);
  65. if (ret == UA_STATUSCODE_GOOD) {
  66. UA_Variant var;
  67. UA_Variant_init(&var);
  68. UA_Variant_setScalar(&var, dstCopy, type);
  69. }
  70. UA_delete(dstCopy, type);
  71. }
  72. return ret==UA_STATUSCODE_GOOD;
  73. }
  74. /*
  75. ** Main entry point. The fuzzer invokes this function with each
  76. ** fuzzed input.
  77. */
  78. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  79. if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
  80. return 0;
  81. size -= 4;
  82. size_t offset;
  83. if (!tortureEncoding(data, size, &offset)) {
  84. return 0;
  85. }
  86. if (offset >= size)
  87. return 0;
  88. tortureExtensionObject(&data[offset], size-offset, &offset);
  89. return 0;
  90. }