1 /**
2  * xrdp: A Remote Desktop Protocol server.
3  *
4  * Copyright (C) 2021 Matt Burt
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Enforce stream primitive checking
19  */
20 
21 #if defined(HAVE_CONFIG_H)
22 #include <config_ac.h>
23 #endif
24 
25 #include <stdlib.h>
26 
27 #include "arch.h"
28 #include "parse.h"
29 #include "log.h"
30 
31 void
parser_stream_overflow_check(const struct stream * s,int n,int is_out,const char * file,int line)32 parser_stream_overflow_check(const struct stream *s, int n, int is_out,
33                              const char *file, int line)
34 {
35     /* Sanity checks */
36     if (n < 0)
37     {
38         LOG(LOG_LEVEL_ALWAYS, "%s:%d "
39             "stream primitive called with negative n=%d",
40             file, line, n);
41         abort();
42     }
43 
44     if (is_out)
45     {
46         /* Output overflow */
47         if (!s_check_rem_out(s, n))
48         {
49             LOG(LOG_LEVEL_ALWAYS, "%s:%d Stream output buffer overflow. "
50                 "Size=%d, pos=%d, requested=%d", file, line,
51                 s->size, (int)(s->p - s->data), n);
52             abort();
53         }
54     }
55     else
56     {
57         /* Input overflow */
58         if (!s_check_rem(s, n))
59         {
60             LOG(LOG_LEVEL_ALWAYS, "%s:%d Stream input buffer overflow. "
61                 "Max=%d, pos=%d, requested=%d", file, line,
62                 (int)(s->end - s->data), (int)(s->p - s->data), n);
63             abort();
64         }
65     }
66 }
67