quic-srtm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 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. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <openssl/ssl.h>
  11. #include <openssl/err.h>
  12. #include <openssl/bio.h>
  13. #include "fuzzer.h"
  14. #include "internal/quic_srtm.h"
  15. int FuzzerInitialize(int *argc, char ***argv)
  16. {
  17. FuzzerSetRand();
  18. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
  19. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
  20. ERR_clear_error();
  21. return 1;
  22. }
  23. /*
  24. * Fuzzer input "protocol":
  25. * Big endian
  26. * Zero or more of:
  27. * ADD - u8(0x00) u64(opaque) u64(seq_num) u128(token)
  28. * REMOVE - u8(0x01) u64(opaque) u64(seq_num)
  29. * CULL - u8(0x02) u64(opaque)
  30. * LOOKUP - u8(0x03) u128(token) u64(idx)
  31. */
  32. enum {
  33. CMD_ADD,
  34. CMD_REMOVE,
  35. CMD_CULL,
  36. CMD_LOOKUP
  37. };
  38. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  39. {
  40. int rc = 0;
  41. QUIC_SRTM *srtm = NULL;
  42. PACKET pkt;
  43. unsigned int cmd;
  44. uint64_t arg_opaque, arg_seq_num, arg_idx;
  45. QUIC_STATELESS_RESET_TOKEN arg_token;
  46. if ((srtm = ossl_quic_srtm_new(NULL, NULL)) == NULL) {
  47. rc = -1;
  48. goto err;
  49. }
  50. if (!PACKET_buf_init(&pkt, buf, len))
  51. goto err;
  52. while (PACKET_remaining(&pkt) > 0) {
  53. if (!PACKET_get_1(&pkt, &cmd))
  54. goto err;
  55. switch (cmd) {
  56. case CMD_ADD:
  57. if (!PACKET_get_net_8(&pkt, &arg_opaque)
  58. || !PACKET_get_net_8(&pkt, &arg_seq_num)
  59. || !PACKET_copy_bytes(&pkt, arg_token.token,
  60. sizeof(arg_token.token)))
  61. continue; /* just stop */
  62. ossl_quic_srtm_add(srtm, (void *)(uintptr_t)arg_opaque,
  63. arg_seq_num, &arg_token);
  64. ossl_quic_srtm_check(srtm);
  65. break;
  66. case CMD_REMOVE:
  67. if (!PACKET_get_net_8(&pkt, &arg_opaque)
  68. || !PACKET_get_net_8(&pkt, &arg_seq_num))
  69. continue; /* just stop */
  70. ossl_quic_srtm_remove(srtm, (void *)(uintptr_t)arg_opaque,
  71. arg_seq_num);
  72. ossl_quic_srtm_check(srtm);
  73. break;
  74. case CMD_CULL:
  75. if (!PACKET_get_net_8(&pkt, &arg_opaque))
  76. continue; /* just stop */
  77. ossl_quic_srtm_cull(srtm, (void *)(uintptr_t)arg_opaque);
  78. ossl_quic_srtm_check(srtm);
  79. break;
  80. case CMD_LOOKUP:
  81. if (!PACKET_copy_bytes(&pkt, arg_token.token,
  82. sizeof(arg_token.token))
  83. || !PACKET_get_net_8(&pkt, &arg_idx))
  84. continue; /* just stop */
  85. ossl_quic_srtm_lookup(srtm, &arg_token, (size_t)arg_idx,
  86. NULL, NULL);
  87. ossl_quic_srtm_check(srtm);
  88. break;
  89. default:
  90. /* Other bytes are treated as no-ops */
  91. continue;
  92. }
  93. }
  94. err:
  95. ossl_quic_srtm_free(srtm);
  96. return rc;
  97. }
  98. void FuzzerCleanup(void)
  99. {
  100. FuzzerClearRand();
  101. }