fuzz_binary_decode.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <open62541/plugin/log_stdout.h>
  6. #include <open62541/server_config_default.h>
  7. #include <open62541/types.h>
  8. #include "ua_server_internal.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. if (!dst)
  22. return UA_FALSE;
  23. const UA_ByteString binary = {
  24. size, //length
  25. (UA_Byte *) (void *) data
  26. };
  27. UA_StatusCode ret = UA_decodeBinary(&binary, newOffset, dst, &UA_TYPES[typeIndex], NULL);
  28. if (ret == UA_STATUSCODE_GOOD) {
  29. // copy the datatype to test
  30. void *dstCopy = UA_new(&UA_TYPES[typeIndex]);
  31. if (!dstCopy)
  32. return UA_FALSE;
  33. UA_copy(dst, dstCopy, &UA_TYPES[typeIndex]);
  34. UA_delete(dstCopy, &UA_TYPES[typeIndex]);
  35. // now also test encoding
  36. UA_ByteString encoded;
  37. UA_ByteString_allocBuffer(&encoded, *newOffset);
  38. const UA_Byte *end = &encoded.data[*newOffset];
  39. UA_Byte *pos = encoded.data;
  40. ret = UA_encodeBinary(dst, &UA_TYPES[typeIndex], &pos, &end, NULL, NULL);
  41. if (ret == UA_STATUSCODE_GOOD) {
  42. // do nothing
  43. }
  44. UA_ByteString_deleteMembers(&encoded);
  45. }
  46. UA_delete(dst, &UA_TYPES[typeIndex]);
  47. return UA_TRUE;
  48. }
  49. static UA_Boolean tortureExtensionObject(const uint8_t *data, size_t size, size_t *newOffset) {
  50. *newOffset = 0;
  51. // check if there is still enough data to create an extension object
  52. // we need at least a nodeid.numeric which equals to 4 bytes
  53. if (size < 4)
  54. return UA_FALSE;
  55. UA_UInt32 identifier = (UA_UInt32)(data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24);
  56. size-= 4;
  57. UA_NodeId objectId = UA_NODEID_NUMERIC(0, identifier);
  58. UA_ExtensionObject obj;
  59. UA_ExtensionObject_init(&obj);
  60. obj.encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
  61. obj.content.encoded.typeId = objectId;
  62. obj.content.encoded.body.length = size;
  63. obj.content.encoded.body.data = (UA_Byte*)(void*)data; // discard const. We are sure that we don't change it
  64. const UA_DataType *type = UA_findDataTypeByBinary(&obj.content.encoded.typeId);
  65. UA_StatusCode ret = UA_STATUSCODE_GOOD;
  66. if (type) {
  67. void *dstCopy = UA_new(type);
  68. if (!dstCopy)
  69. return UA_FALSE;
  70. ret = UA_decodeBinary(&obj.content.encoded.body, newOffset, dstCopy, type, NULL);
  71. if (ret == UA_STATUSCODE_GOOD) {
  72. UA_Variant var;
  73. UA_Variant_init(&var);
  74. UA_Variant_setScalar(&var, dstCopy, type);
  75. }
  76. UA_delete(dstCopy, type);
  77. }
  78. return ret==UA_STATUSCODE_GOOD;
  79. }
  80. /*
  81. ** Main entry point. The fuzzer invokes this function with each
  82. ** fuzzed input.
  83. */
  84. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  85. if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
  86. return 0;
  87. size -= 4;
  88. size_t offset;
  89. if (!tortureEncoding(data, size, &offset)) {
  90. return 0;
  91. }
  92. if (offset >= size)
  93. return 0;
  94. tortureExtensionObject(&data[offset], size-offset, &offset);
  95. return 0;
  96. }