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
19 #include <grpc/credentials.h>
20 #include <grpc/grpc.h>
21 #include <grpc/grpc_security.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/string_util.h>
24 #include <string.h>
25
26 #include "absl/log/check.h"
27 #include "src/core/lib/security/credentials/alts/alts_credentials.h"
28 #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
29 #include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h"
30 #include "src/core/util/crash.h"
31 #include "src/core/util/env.h"
32 #include "test/core/test_util/fuzzer_util.h"
33 #include "test/core/test_util/test_config.h"
34
35 using grpc_core::testing::grpc_fuzzer_get_next_byte;
36 using grpc_core::testing::grpc_fuzzer_get_next_string;
37 using grpc_core::testing::input_stream;
38
39 // Logging
40 bool squelch = true;
41 bool leak_check = true;
42
43 // Add a random number of target service accounts to client options.
read_target_service_accounts(input_stream * inp,grpc_alts_credentials_options * options)44 static void read_target_service_accounts(
45 input_stream* inp, grpc_alts_credentials_options* options) {
46 size_t n = grpc_fuzzer_get_next_byte(inp);
47 for (size_t i = 0; i < n; i++) {
48 char* service_account = grpc_fuzzer_get_next_string(inp, nullptr);
49 if (service_account != nullptr) {
50 grpc_alts_credentials_client_options_add_target_service_account(
51 options, service_account);
52 gpr_free(service_account);
53 }
54 }
55 // Added to improve code coverage.
56 grpc_alts_credentials_client_options_add_target_service_account(options,
57 nullptr);
58 grpc_alts_credentials_client_options_add_target_service_account(
59 nullptr, "this is service account");
60 }
61
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
63 if (squelch && !grpc_core::GetEnv("GRPC_TRACE_FUZZER").has_value()) {
64 grpc_disable_all_absl_logs();
65 }
66 input_stream inp = {data, data + size};
67 grpc_init();
68 bool is_on_gcp = grpc_alts_is_running_on_gcp();
69 while (inp.cur != inp.end) {
70 bool enable_untrusted_alts = grpc_fuzzer_get_next_byte(&inp) & 0x01;
71 char* handshaker_service_url =
72 grpc_fuzzer_get_next_byte(&inp) & 0x01
73 ? grpc_fuzzer_get_next_string(&inp, nullptr)
74 : nullptr;
75 if (grpc_fuzzer_get_next_byte(&inp) & 0x01) {
76 // Test ALTS channel credentials.
77 grpc_alts_credentials_options* options =
78 grpc_alts_credentials_client_options_create();
79 read_target_service_accounts(&inp, options);
80 grpc_channel_credentials* cred = grpc_alts_credentials_create_customized(
81 options, handshaker_service_url, enable_untrusted_alts);
82 if (!enable_untrusted_alts && !is_on_gcp) {
83 CHECK_EQ(cred, nullptr);
84 } else {
85 CHECK_NE(cred, nullptr);
86 }
87 grpc_channel_credentials_release(cred);
88 grpc_alts_credentials_options_destroy(options);
89 } else {
90 // Test ALTS server credentials.
91 grpc_alts_credentials_options* options =
92 grpc_alts_credentials_server_options_create();
93 grpc_server_credentials* cred =
94 grpc_alts_server_credentials_create_customized(
95 options, handshaker_service_url, enable_untrusted_alts);
96 if (!enable_untrusted_alts && !is_on_gcp) {
97 CHECK_EQ(cred, nullptr);
98 } else {
99 CHECK_NE(cred, nullptr);
100 }
101 grpc_server_credentials_release(cred);
102 grpc_alts_credentials_options_destroy(options);
103 }
104 gpr_free(handshaker_service_url);
105 }
106 grpc_shutdown();
107 return 0;
108 }
109