| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2019 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 <string> |
| 20 |
|
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpc/support/log.h> |
| 25 |
|
| 26 |
#include "src/core/lib/surface/server.h" |
| 27 |
#include "test/core/bad_client/bad_client.h" |
| 28 |
#include "test/core/util/test_config.h" |
| 29 |
|
| 30 |
#define APPEND_BUFFER(string, to_append) \ |
| 31 |
((string).append((to_append), sizeof(to_append) - 1)) |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
void verifier(grpc_server* server, grpc_completion_queue* cq, |
| 36 |
void* /*registered_method*/) { |
| 37 |
while (grpc_core::Server::FromC(server)->HasOpenConnections()) { |
| 38 |
GPR_ASSERT(grpc_completion_queue_next( |
| 39 |
cq, grpc_timeout_milliseconds_to_deadline(20), nullptr) |
| 40 |
.type == GRPC_QUEUE_TIMEOUT); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
void FrameVerifier(const std::string& attack_vector) { |
| 45 |
grpc_bad_client_arg args[2]; |
| 46 |
args[0] = connection_preface_arg; |
| 47 |
args[1].client_validator = nullptr; |
| 48 |
args[1].client_payload = attack_vector.c_str(); |
| 49 |
args[1].client_payload_length = attack_vector.size(); |
| 50 |
grpc_run_bad_client_test(verifier, args, 2, GRPC_BAD_CLIENT_DISCONNECT); |
| 51 |
} |
| 52 |
|
| 53 |
TEST(OutOfBounds, MaxFrameSizeDataFrame) { |
| 54 |
std::string out_of_bounds_data; |
| 55 |
// Send a data frame larger than 2^14 |
| 56 |
APPEND_BUFFER(out_of_bounds_data, "\x01\x00\x00\x00\x00\x00\x00\x00\x01"); |
| 57 |
out_of_bounds_data.append(1 << 16, 'a'); |
| 58 |
FrameVerifier(out_of_bounds_data); |
| 59 |
} |
| 60 |
|
| 61 |
TEST(OutOfBounds, BadSizePriorityFrame) { |
| 62 |
std::string bad_size_priority_frame; |
| 63 |
// Priority Frame should be a length of 5 octets |
| 64 |
APPEND_BUFFER(bad_size_priority_frame, |
| 65 |
"\x00\x00\x03\x02\x00\x00\x00\x00\x01" |
| 66 |
"\x11\x11\x12"); |
| 67 |
FrameVerifier(bad_size_priority_frame); |
| 68 |
} |
| 69 |
|
| 70 |
TEST(OutOfBounds, BadSizeRstStream) { |
| 71 |
std::string bad_size_rst_stream; |
| 72 |
// Rst Stream Frame should have a length of 4 octets |
| 73 |
APPEND_BUFFER(bad_size_rst_stream, |
| 74 |
"\x00\x00\x02\x03\x00\x00\x00\x00\x01" |
| 75 |
"\x11\x11"); |
| 76 |
FrameVerifier(bad_size_rst_stream); |
| 77 |
} |
| 78 |
|
| 79 |
TEST(OutOfBounds, BadSizeSettings) { |
| 80 |
std::string bad_size_settings; |
| 81 |
// Settings Frame should have a length which is a multiple of 6 octets |
| 82 |
APPEND_BUFFER(bad_size_settings, |
| 83 |
"\x00\x00\x05\x04\x00\x00\x00\x00\x00" |
| 84 |
"\x11\x11\x11\x11\x11"); |
| 85 |
FrameVerifier(bad_size_settings); |
| 86 |
} |
| 87 |
|
| 88 |
TEST(OutOfBounds, BadSizePing) { |
| 89 |
std::string bad_size_ping; |
| 90 |
// Rst Stream Frame should have a length of 8 octets |
| 91 |
APPEND_BUFFER(bad_size_ping, |
| 92 |
"\x00\x00\x05\x06\x00\x00\x00\x00\x00" |
| 93 |
"\x11\x11\x11\x11\x11"); |
| 94 |
FrameVerifier(bad_size_ping); |
| 95 |
} |
| 96 |
|
| 97 |
TEST(OutOfBounds, WindowUpdate) { |
| 98 |
std::string bad_size_window_update; |
| 99 |
// Window Update Frame should have a length of 4 octets |
| 100 |
APPEND_BUFFER(bad_size_window_update, |
| 101 |
"\x00\x00\x01\x08\x00\x00\x00\x00\x00" |
| 102 |
"\x11"); |
| 103 |
FrameVerifier(bad_size_window_update); |
| 104 |
} |
| 105 |
|
| 106 |
} // namespace |
| 107 |
|
| 108 |
int main(int argc, char** argv) { |
| 109 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 110 |
::testing::InitGoogleTest(&argc, argv); |
| 111 |
grpc_init(); |
| 112 |
int retval = RUN_ALL_TESTS(); |
| 113 |
grpc_shutdown(); |
| 114 |
return retval; |
| 115 |
} |