• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 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 "src/core/lib/surface/server.h"
20 #include "test/core/bad_client/bad_client.h"
21 
22 #define PFX_STR                      \
23   "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
24   "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
25 
verifier(grpc_server * server,grpc_completion_queue * cq,void * registered_method)26 static void verifier(grpc_server* server, grpc_completion_queue* cq,
27                      void* registered_method) {
28   while (grpc_server_has_open_connections(server)) {
29     GPR_ASSERT(grpc_completion_queue_next(
30                    cq, grpc_timeout_milliseconds_to_deadline(20), nullptr)
31                    .type == GRPC_QUEUE_TIMEOUT);
32   }
33 }
34 
main(int argc,char ** argv)35 int main(int argc, char** argv) {
36   grpc_test_init(argc, argv);
37   grpc_init();
38 
39   /* partial http2 header prefixes */
40   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00",
41                            GRPC_BAD_CLIENT_DISCONNECT);
42   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00",
43                            GRPC_BAD_CLIENT_DISCONNECT);
44   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00",
45                            GRPC_BAD_CLIENT_DISCONNECT);
46   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01",
47                            GRPC_BAD_CLIENT_DISCONNECT);
48   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x00",
49                            GRPC_BAD_CLIENT_DISCONNECT);
50   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x04",
51                            GRPC_BAD_CLIENT_DISCONNECT);
52   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr, PFX_STR "\x00\x00\x00\x01\x05",
53                            GRPC_BAD_CLIENT_DISCONNECT);
54   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
55                            PFX_STR "\x00\x00\x00\x01\x04\x00",
56                            GRPC_BAD_CLIENT_DISCONNECT);
57   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
58                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00",
59                            GRPC_BAD_CLIENT_DISCONNECT);
60   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
61                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00",
62                            GRPC_BAD_CLIENT_DISCONNECT);
63   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
64                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00\x00",
65                            GRPC_BAD_CLIENT_DISCONNECT);
66   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
67                            PFX_STR "\x00\x00\x00\x01\x04\x00\x00\x00\x01",
68                            GRPC_BAD_CLIENT_DISCONNECT);
69 
70   /* test adding prioritization data */
71   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
72                            PFX_STR
73                            "\x00\x00\x01\x01\x24\x00\x00\x00\x01"
74                            "\x00",
75                            0);
76   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
77                            PFX_STR
78                            "\x00\x00\x02\x01\x24\x00\x00\x00\x01"
79                            "\x00\x00",
80                            0);
81   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
82                            PFX_STR
83                            "\x00\x00\x03\x01\x24\x00\x00\x00\x01"
84                            "\x00\x00\x00",
85                            0);
86   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
87                            PFX_STR
88                            "\x00\x00\x04\x01\x24\x00\x00\x00\x01"
89                            "\x00\x00\x00\x00",
90                            0);
91   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
92                            PFX_STR
93                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
94                            "",
95                            GRPC_BAD_CLIENT_DISCONNECT);
96   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
97                            PFX_STR
98                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
99                            "\x00",
100                            GRPC_BAD_CLIENT_DISCONNECT);
101   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
102                            PFX_STR
103                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
104                            "\x00\x00",
105                            GRPC_BAD_CLIENT_DISCONNECT);
106   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
107                            PFX_STR
108                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
109                            "\x00\x00\x00",
110                            GRPC_BAD_CLIENT_DISCONNECT);
111   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
112                            PFX_STR
113                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
114                            "\x00\x00\x00\x00",
115                            GRPC_BAD_CLIENT_DISCONNECT);
116   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
117                            PFX_STR
118                            "\x00\x00\x05\x01\x24\x00\x00\x00\x01"
119                            "\x00\x00\x00\x00\x00",
120                            GRPC_BAD_CLIENT_DISCONNECT);
121 
122   /* test looking up an invalid index */
123   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
124                            PFX_STR
125                            "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
126                            "\xfe",
127                            0);
128   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
129                            PFX_STR
130                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
131                            "\x7f\x7f\x01"
132                            "a",
133                            0);
134   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
135                            PFX_STR
136                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
137                            "\x0f\x7f\x01"
138                            "a",
139                            0);
140   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
141                            PFX_STR
142                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
143                            "\x1f\x7f\x01"
144                            "a",
145                            0);
146   /* test nvr, not indexed in static table */
147   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
148                            PFX_STR
149                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
150                            "\x01\x01"
151                            "a",
152                            GRPC_BAD_CLIENT_DISCONNECT);
153   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
154                            PFX_STR
155                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
156                            "\x11\x01"
157                            "a",
158                            GRPC_BAD_CLIENT_DISCONNECT);
159   /* illegal op code */
160   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
161                            PFX_STR
162                            "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
163                            "\x80",
164                            0);
165   /* parse some long indices */
166   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
167                            PFX_STR
168                            "\x00\x00\x02\x01\x04\x00\x00\x00\x01"
169                            "\xff\x00",
170                            0);
171   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
172                            PFX_STR
173                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
174                            "\xff\x80\x00",
175                            0);
176   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
177                            PFX_STR
178                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
179                            "\xff\x80\x80\x00",
180                            0);
181   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
182                            PFX_STR
183                            "\x00\x00\x05\x01\x04\x00\x00\x00\x01"
184                            "\xff\x80\x80\x80\x00",
185                            0);
186   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
187                            PFX_STR
188                            "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
189                            "\xff\x80\x80\x80\x80\x00",
190                            0);
191   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
192                            PFX_STR
193                            "\x00\x00\x07\x01\x04\x00\x00\x00\x01"
194                            "\xff\x80\x80\x80\x80\x80\x00",
195                            0);
196   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
197                            PFX_STR
198                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
199                            "\xff",
200                            GRPC_BAD_CLIENT_DISCONNECT);
201   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
202                            PFX_STR
203                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
204                            "\xff\x80",
205                            GRPC_BAD_CLIENT_DISCONNECT);
206   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
207                            PFX_STR
208                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
209                            "\xff\x80\x80",
210                            GRPC_BAD_CLIENT_DISCONNECT);
211   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
212                            PFX_STR
213                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
214                            "\xff\x80\x80\x80",
215                            GRPC_BAD_CLIENT_DISCONNECT);
216   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
217                            PFX_STR
218                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
219                            "\xff\x80\x80\x80\x80",
220                            GRPC_BAD_CLIENT_DISCONNECT);
221   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
222                            PFX_STR
223                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
224                            "\xff\x80\x80\x80\x80\x80",
225                            GRPC_BAD_CLIENT_DISCONNECT);
226   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
227                            PFX_STR
228                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
229                            "\xff\x80\x80\x80\x80\x80\x80",
230                            GRPC_BAD_CLIENT_DISCONNECT);
231   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
232                            PFX_STR
233                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
234                            "\xff\x80\x80\x80\x80\x80\x80\x00",
235                            0);
236   /* overflow on byte 4 */
237   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
238                            PFX_STR
239                            "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
240                            "\xff\x80\x80\x80\x80\x7f",
241                            GRPC_BAD_CLIENT_DISCONNECT);
242   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
243                            PFX_STR
244                            "\x00\x00\x06\x01\x04\x00\x00\x00\x01"
245                            "\xff\xff\xff\xff\xff\x0f",
246                            GRPC_BAD_CLIENT_DISCONNECT);
247   /* overflow after byte 4 */
248   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
249                            PFX_STR
250                            "\x00\x00\x08\x01\x04\x00\x00\x00\x01"
251                            "\xff\x80\x80\x80\x80\x80\x80\x02",
252                            0);
253   /* end of headers mid-opcode */
254   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
255                            PFX_STR
256                            "\x00\x00\x01\x01\x04\x00\x00\x00\x01"
257                            "\x01",
258                            GRPC_BAD_CLIENT_DISCONNECT);
259 
260   /* dynamic table size update: set to default */
261   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
262                            PFX_STR
263                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
264                            "\x3f\xe1\x1f",
265                            GRPC_BAD_CLIENT_DISCONNECT);
266   /* dynamic table size update: set too large */
267   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
268                            PFX_STR
269                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
270                            "\x3f\xf1\x1f",
271                            0);
272   /* dynamic table size update: set twice */
273   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
274                            PFX_STR
275                            "\x00\x00\x04\x01\x04\x00\x00\x00\x01"
276                            "\x20\x3f\xe1\x1f",
277                            GRPC_BAD_CLIENT_DISCONNECT);
278   /* dynamic table size update: set thrice */
279   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
280                            PFX_STR
281                            "\x00\x00\x03\x01\x04\x00\x00\x00\x01"
282                            "\x20\x20\x20",
283                            0);
284 
285   /* non-ending header followed by continuation frame */
286   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
287                            PFX_STR
288                            "\x00\x00\x00\x01\x00\x00\x00\x00\x01"
289                            "\x00\x00\x00\x09\x04\x00\x00\x00\x01",
290                            GRPC_BAD_CLIENT_DISCONNECT);
291   /* non-ending header followed by non-continuation frame */
292   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
293                            PFX_STR
294                            "\x00\x00\x00\x01\x00\x00\x00\x00\x01"
295                            "\x00\x00\x00\x00\x04\x00\x00\x00\x01",
296                            0);
297   /* non-ending header followed by a continuation frame for a different stream
298    */
299   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
300                            PFX_STR
301                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
302                            "\x00\x00\x00\x01\x00\x00\x00\x00\x03"
303                            "\x00\x00\x00\x09\x04\x00\x00\x00\x01",
304                            0);
305   /* opening with a continuation frame */
306   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
307                            PFX_STR "\x00\x00\x00\x09\x04\x00\x00\x00\x01", 0);
308   /* three header frames */
309   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
310                            PFX_STR
311                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
312                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01"
313                            "\x00\x00\x00\x01\x04\x00\x00\x00\x01",
314                            GRPC_BAD_CLIENT_DISCONNECT);
315 
316   /* an invalid header found with fuzzing */
317   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
318                            PFX_STR "\x00\x00\x00\x01\x39\x67\xed\x1d\x64",
319                            GRPC_BAD_CLIENT_DISCONNECT);
320 
321   /* a badly encoded timeout value */
322   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
323                            PFX_STR
324                            "\x00\x00\x19\x01\x04\x00\x00\x00\x01"
325                            "\x10\x0cgrpc-timeout\x0a"
326                            "15 seconds",
327                            GRPC_BAD_CLIENT_DISCONNECT);
328   /* a badly encoded timeout value: twice (catches caching) */
329   GRPC_RUN_BAD_CLIENT_TEST(verifier, nullptr,
330                            PFX_STR
331                            "\x00\x00\x19\x01\x04\x00\x00\x00\x01"
332                            "\x10\x0cgrpc-timeout\x0a"
333                            "15 seconds"
334                            "\x00\x00\x19\x01\x04\x00\x00\x00\x03"
335                            "\x10\x0cgrpc-timeout\x0a"
336                            "15 seconds",
337                            GRPC_BAD_CLIENT_DISCONNECT);
338 
339   grpc_shutdown();
340   return 0;
341 }
342