out_of_bounds.cc
Go to the documentation of this file.
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 
24 
27 
28 #define APPEND_BUFFER(string, to_append) \
29  ((string).append((to_append), sizeof(to_append) - 1))
30 
31 namespace {
32 
34  void* /*registered_method*/) {
35  while (grpc_core::Server::FromC(server)->HasOpenConnections()) {
39  }
40 }
41 
42 void FrameVerifier(const std::string& attack_vector) {
45  args[1].client_validator = nullptr;
46  args[1].client_payload = attack_vector.c_str();
47  args[1].client_payload_length = attack_vector.size();
49 }
50 
51 TEST(OutOfBounds, MaxFrameSizeDataFrame) {
52  std::string out_of_bounds_data;
53  // Send a data frame larger than 2^14
54  APPEND_BUFFER(out_of_bounds_data, "\x01\x00\x00\x00\x00\x00\x00\x00\x01");
55  out_of_bounds_data.append(1 << 16, 'a');
56  FrameVerifier(out_of_bounds_data);
57 }
58 
59 TEST(OutOfBounds, BadSizePriorityFrame) {
60  std::string bad_size_priority_frame;
61  // Priority Frame should be a length of 5 octets
62  APPEND_BUFFER(bad_size_priority_frame,
63  "\x00\x00\x03\x02\x00\x00\x00\x00\x01"
64  "\x11\x11\x12");
65  FrameVerifier(bad_size_priority_frame);
66 }
67 
68 TEST(OutOfBounds, BadSizeRstStream) {
69  std::string bad_size_rst_stream;
70  // Rst Stream Frame should have a length of 4 octets
71  APPEND_BUFFER(bad_size_rst_stream,
72  "\x00\x00\x02\x03\x00\x00\x00\x00\x01"
73  "\x11\x11");
74  FrameVerifier(bad_size_rst_stream);
75 }
76 
77 TEST(OutOfBounds, BadSizeSettings) {
78  std::string bad_size_settings;
79  // Settings Frame should have a length which is a multiple of 6 octets
80  APPEND_BUFFER(bad_size_settings,
81  "\x00\x00\x05\x04\x00\x00\x00\x00\x00"
82  "\x11\x11\x11\x11\x11");
83  FrameVerifier(bad_size_settings);
84 }
85 
86 TEST(OutOfBounds, BadSizePing) {
87  std::string bad_size_ping;
88  // Rst Stream Frame should have a length of 8 octets
89  APPEND_BUFFER(bad_size_ping,
90  "\x00\x00\x05\x06\x00\x00\x00\x00\x00"
91  "\x11\x11\x11\x11\x11");
92  FrameVerifier(bad_size_ping);
93 }
94 
95 TEST(OutOfBounds, WindowUpdate) {
96  std::string bad_size_window_update;
97  // Window Update Frame should have a length of 4 octets
98  APPEND_BUFFER(bad_size_window_update,
99  "\x00\x00\x01\x08\x00\x00\x00\x00\x00"
100  "\x11");
101  FrameVerifier(bad_size_window_update);
102 }
103 
104 } // namespace
105 
106 int main(int argc, char** argv) {
107  grpc::testing::TestEnvironment env(&argc, argv);
108  ::testing::InitGoogleTest(&argc, argv);
109  grpc_init();
110  int retval = RUN_ALL_TESTS();
111  grpc_shutdown();
112  return retval;
113 }
APPEND_BUFFER
#define APPEND_BUFFER(string, to_append)
Definition: out_of_bounds.cc:28
generate.env
env
Definition: generate.py:37
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
verifier
static void verifier(grpc_server *server, grpc_completion_queue *cq, void *)
Definition: badreq.cc:31
string_util.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
grpc_completion_queue
Definition: completion_queue.cc:347
grpc_server
struct grpc_server grpc_server
Definition: grpc_types.h:65
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GRPC_BAD_CLIENT_DISCONNECT
#define GRPC_BAD_CLIENT_DISCONNECT
Definition: bad_client.h:48
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
server
Definition: examples/python/async_streaming/server.py:1
grpc_run_bad_client_test
void grpc_run_bad_client_test(grpc_bad_client_server_side_validator server_validator, grpc_bad_client_arg args[], int num_args, uint32_t flags)
Definition: bad_client.cc:193
grpc_bad_client_arg
Definition: bad_client.h:40
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_completion_queue_next
GRPCAPI grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1133
server.h
main
int main(int argc, char **argv)
Definition: out_of_bounds.cc:106
connection_preface_arg
grpc_bad_client_arg connection_preface_arg
Definition: bad_client.cc:282
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_core::CppImplOf< Server, grpc_server >::FromC
static Server * FromC(grpc_server *c_type)
Definition: cpp_impl_of.h:30
GRPC_QUEUE_TIMEOUT
@ GRPC_QUEUE_TIMEOUT
Definition: grpc_types.h:556
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
bad_client.h
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:36