alts_credentials_fuzzer.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <string.h>
  19. #include <grpc/grpc.h>
  20. #include <grpc/grpc_security.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/string_util.h>
  24. #include "src/core/lib/gpr/env.h"
  25. #include "src/core/lib/security/credentials/alts/alts_credentials.h"
  26. #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
  27. #include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h"
  28. #include "test/core/util/fuzzer_util.h"
  29. using grpc_core::testing::grpc_fuzzer_get_next_byte;
  30. using grpc_core::testing::grpc_fuzzer_get_next_string;
  31. using grpc_core::testing::input_stream;
  32. // Logging
  33. bool squelch = true;
  34. bool leak_check = true;
  35. static void dont_log(gpr_log_func_args* /*args*/) {}
  36. // Add a random number of target service accounts to client options.
  37. static void read_target_service_accounts(
  38. input_stream* inp, grpc_alts_credentials_options* options) {
  39. size_t n = grpc_fuzzer_get_next_byte(inp);
  40. for (size_t i = 0; i < n; i++) {
  41. char* service_account = grpc_fuzzer_get_next_string(inp, nullptr);
  42. if (service_account != nullptr) {
  43. grpc_alts_credentials_client_options_add_target_service_account(
  44. options, service_account);
  45. gpr_free(service_account);
  46. }
  47. }
  48. // Added to improve code coverage.
  49. grpc_alts_credentials_client_options_add_target_service_account(options,
  50. nullptr);
  51. grpc_alts_credentials_client_options_add_target_service_account(
  52. nullptr, "this is service account");
  53. }
  54. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  55. char* grpc_trace_fuzzer = gpr_getenv("GRPC_TRACE_FUZZER");
  56. if (squelch && grpc_trace_fuzzer == nullptr) {
  57. gpr_set_log_function(dont_log);
  58. }
  59. gpr_free(grpc_trace_fuzzer);
  60. input_stream inp = {data, data + size};
  61. grpc_init();
  62. bool is_on_gcp = grpc_alts_is_running_on_gcp();
  63. while (inp.cur != inp.end) {
  64. bool enable_untrusted_alts = grpc_fuzzer_get_next_byte(&inp) & 0x01;
  65. char* handshaker_service_url =
  66. grpc_fuzzer_get_next_byte(&inp) & 0x01
  67. ? grpc_fuzzer_get_next_string(&inp, nullptr)
  68. : nullptr;
  69. if (grpc_fuzzer_get_next_byte(&inp) & 0x01) {
  70. // Test ALTS channel credentials.
  71. grpc_alts_credentials_options* options =
  72. grpc_alts_credentials_client_options_create();
  73. read_target_service_accounts(&inp, options);
  74. grpc_channel_credentials* cred = grpc_alts_credentials_create_customized(
  75. options, handshaker_service_url, enable_untrusted_alts);
  76. if (!enable_untrusted_alts && !is_on_gcp) {
  77. GPR_ASSERT(cred == nullptr);
  78. } else {
  79. GPR_ASSERT(cred != nullptr);
  80. }
  81. grpc_channel_credentials_release(cred);
  82. grpc_alts_credentials_options_destroy(options);
  83. } else {
  84. // Test ALTS server credentials.
  85. grpc_alts_credentials_options* options =
  86. grpc_alts_credentials_server_options_create();
  87. grpc_server_credentials* cred =
  88. grpc_alts_server_credentials_create_customized(
  89. options, handshaker_service_url, enable_untrusted_alts);
  90. if (!enable_untrusted_alts && !is_on_gcp) {
  91. GPR_ASSERT(cred == nullptr);
  92. } else {
  93. GPR_ASSERT(cred != nullptr);
  94. }
  95. grpc_server_credentials_release(cred);
  96. grpc_alts_credentials_options_destroy(options);
  97. }
  98. gpr_free(handshaker_service_url);
  99. }
  100. grpc_shutdown();
  101. return 0;
  102. }