• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2016 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/event_engine/event_engine.h>
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_security.h>
23 
24 #include "absl/log/check.h"
25 #include "absl/synchronization/notification.h"
26 #include "src/core/lib/event_engine/default_event_engine.h"
27 #include "src/core/lib/security/credentials/credentials.h"
28 #include "src/core/lib/security/security_connector/security_connector.h"
29 #include "src/core/util/notification.h"
30 #include "test/core/test_util/mock_endpoint.h"
31 #include "test/core/test_util/test_config.h"
32 #include "test/core/test_util/tls_utils.h"
33 
34 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
35 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
36 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
37 
38 using grpc_core::HandshakerArgs;
39 using grpc_event_engine::experimental::EventEngine;
40 using grpc_event_engine::experimental::GetDefaultEventEngine;
41 
42 bool squelch = true;
43 // ssl has an array of global gpr_mu's that are never released.
44 // Turning this on will fail the leak check.
45 bool leak_check = false;
46 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)47 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
48   if (squelch) {
49     grpc_disable_all_absl_logs();
50   }
51   grpc_init();
52   {
53     grpc_core::ExecCtx exec_ctx;
54 
55     auto engine = GetDefaultEventEngine();
56     auto mock_endpoint_controller =
57         grpc_event_engine::experimental::MockEndpointController::Create(engine);
58     mock_endpoint_controller->TriggerReadEvent(
59         grpc_event_engine::experimental::Slice::FromCopiedBuffer(
60             reinterpret_cast<const char*>(data), size));
61     mock_endpoint_controller->NoMoreReads();
62 
63     // Load key pair and establish server SSL credentials.
64     std::string ca_cert = grpc_core::testing::GetFileContents(CA_CERT_PATH);
65     std::string server_cert =
66         grpc_core::testing::GetFileContents(SERVER_CERT_PATH);
67     std::string server_key =
68         grpc_core::testing::GetFileContents(SERVER_KEY_PATH);
69     grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
70                                                     server_cert.c_str()};
71     grpc_server_credentials* creds = grpc_ssl_server_credentials_create(
72         ca_cert.c_str(), &pem_key_cert_pair, 1, 0, nullptr);
73 
74     // Create security connector
75     grpc_core::RefCountedPtr<grpc_server_security_connector> sc =
76         creds->create_security_connector(grpc_core::ChannelArgs());
77     CHECK(sc != nullptr);
78     grpc_core::Timestamp deadline =
79         grpc_core::Duration::Seconds(1) + grpc_core::Timestamp::Now();
80 
81     auto handshake_mgr =
82         grpc_core::MakeRefCounted<grpc_core::HandshakeManager>();
83     auto channel_args =
84         grpc_core::ChannelArgs().SetObject<EventEngine>(std::move(engine));
85     sc->add_handshakers(channel_args, nullptr, handshake_mgr.get());
86     absl::Notification handshake_completed;
87     handshake_mgr->DoHandshake(grpc_core::OrphanablePtr<grpc_endpoint>(
88                                    mock_endpoint_controller->TakeCEndpoint()),
89                                channel_args, deadline, nullptr /* acceptor */,
90                                [&](absl::StatusOr<HandshakerArgs*> result) {
91                                  // The fuzzer should not pass the handshake.
92                                  CHECK(!result.ok());
93                                  handshake_completed.Notify();
94                                });
95     grpc_core::ExecCtx::Get()->Flush();
96 
97     // If the given string happens to be part of the correct client hello, the
98     // server will wait for more data. Explicitly fail the server by shutting
99     // down the handshake manager.
100     if (!handshake_completed.WaitForNotificationWithTimeout(absl::Seconds(3))) {
101       handshake_mgr->Shutdown(
102           absl::DeadlineExceededError("handshake did not fail as expected"));
103     }
104 
105     sc.reset(DEBUG_LOCATION, "test");
106     grpc_server_credentials_release(creds);
107     grpc_core::ExecCtx::Get()->Flush();
108   }
109 
110   grpc_shutdown();
111   return 0;
112 }
113