Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/channels/geometry/client/geometry_main.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Geometry tracking Virtual Channel Extension
4
 *
5
 * Copyright 2017 David Fort <contact@hardening-consulting.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <freerdp/config.h>
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include <winpr/crt.h>
27
#include <winpr/synch.h>
28
#include <winpr/print.h>
29
#include <winpr/stream.h>
30
#include <winpr/cmdline.h>
31
#include <winpr/collections.h>
32
33
#include <freerdp/addin.h>
34
#include <freerdp/client/channels.h>
35
#include <freerdp/client/geometry.h>
36
#include <freerdp/channels/log.h>
37
38
0
#define TAG CHANNELS_TAG("geometry.client")
39
40
#include "geometry_main.h"
41
42
typedef struct
43
{
44
  GENERIC_DYNVC_PLUGIN base;
45
  GeometryClientContext* context;
46
} GEOMETRY_PLUGIN;
47
48
static UINT32 mappedGeometryHash(const void* v)
49
0
{
50
0
  const UINT64* g = (const UINT64*)v;
51
0
  return (UINT32)((*g >> 32) + (*g & 0xffffffff));
52
0
}
53
54
static BOOL mappedGeometryKeyCompare(const void* v1, const void* v2)
55
0
{
56
0
  const UINT64* g1 = (const UINT64*)v1;
57
0
  const UINT64* g2 = (const UINT64*)v2;
58
0
  return *g1 == *g2;
59
0
}
60
61
static void freerdp_rgndata_reset(FREERDP_RGNDATA* data)
62
0
{
63
0
  data->nRectCount = 0;
64
0
}
65
66
static UINT32 geometry_read_RGNDATA(wLog* logger, wStream* s, UINT32 len, FREERDP_RGNDATA* rgndata)
67
0
{
68
0
  UINT32 dwSize = 0;
69
0
  UINT32 iType = 0;
70
0
  INT32 right = 0;
71
0
  INT32 bottom = 0;
72
0
  INT32 x = 0;
73
0
  INT32 y = 0;
74
0
  INT32 w = 0;
75
0
  INT32 h = 0;
76
77
0
  if (len < 32)
78
0
  {
79
0
    WLog_Print(logger, WLOG_ERROR, "invalid RGNDATA");
80
0
    return ERROR_INVALID_DATA;
81
0
  }
82
83
0
  Stream_Read_UINT32(s, dwSize);
84
85
0
  if (dwSize != 32)
86
0
  {
87
0
    WLog_Print(logger, WLOG_ERROR, "invalid RGNDATA dwSize");
88
0
    return ERROR_INVALID_DATA;
89
0
  }
90
91
0
  Stream_Read_UINT32(s, iType);
92
93
0
  if (iType != RDH_RECTANGLE)
94
0
  {
95
0
    WLog_Print(logger, WLOG_ERROR, "iType %" PRIu32 " for RGNDATA is not supported", iType);
96
0
    return ERROR_UNSUPPORTED_TYPE;
97
0
  }
98
99
0
  Stream_Read_UINT32(s, rgndata->nRectCount);
100
0
  Stream_Seek_UINT32(s); /* nRgnSize IGNORED */
101
0
  Stream_Read_INT32(s, x);
102
0
  Stream_Read_INT32(s, y);
103
0
  Stream_Read_INT32(s, right);
104
0
  Stream_Read_INT32(s, bottom);
105
0
  if ((abs(x) > INT16_MAX) || (abs(y) > INT16_MAX))
106
0
    return ERROR_INVALID_DATA;
107
0
  w = right - x;
108
0
  h = bottom - y;
109
0
  if ((abs(w) > INT16_MAX) || (abs(h) > INT16_MAX))
110
0
    return ERROR_INVALID_DATA;
111
0
  rgndata->boundingRect.x = (INT16)x;
112
0
  rgndata->boundingRect.y = (INT16)y;
113
0
  rgndata->boundingRect.width = (INT16)w;
114
0
  rgndata->boundingRect.height = (INT16)h;
115
0
  len -= 32;
116
117
0
  if (len / (4 * 4) < rgndata->nRectCount)
118
0
  {
119
0
    WLog_Print(logger, WLOG_ERROR, "not enough data for region rectangles");
120
0
    return ERROR_INVALID_DATA;
121
0
  }
122
123
0
  if (rgndata->nRectCount)
124
0
  {
125
0
    RDP_RECT* tmp = realloc(rgndata->rects, rgndata->nRectCount * sizeof(RDP_RECT));
126
127
0
    if (!tmp)
128
0
    {
129
0
      WLog_Print(logger, WLOG_ERROR, "unable to allocate memory for %" PRIu32 " RECTs",
130
0
                 rgndata->nRectCount);
131
0
      return CHANNEL_RC_NO_MEMORY;
132
0
    }
133
0
    rgndata->rects = tmp;
134
135
0
    for (UINT32 i = 0; i < rgndata->nRectCount; i++)
136
0
    {
137
0
      Stream_Read_INT32(s, x);
138
0
      Stream_Read_INT32(s, y);
139
0
      Stream_Read_INT32(s, right);
140
0
      Stream_Read_INT32(s, bottom);
141
0
      if ((abs(x) > INT16_MAX) || (abs(y) > INT16_MAX))
142
0
        return ERROR_INVALID_DATA;
143
0
      w = right - x;
144
0
      h = bottom - y;
145
0
      if ((abs(w) > INT16_MAX) || (abs(h) > INT16_MAX))
146
0
        return ERROR_INVALID_DATA;
147
0
      rgndata->rects[i].x = (INT16)x;
148
0
      rgndata->rects[i].y = (INT16)y;
149
0
      rgndata->rects[i].width = (INT16)w;
150
0
      rgndata->rects[i].height = (INT16)h;
151
0
    }
152
0
  }
153
154
0
  return CHANNEL_RC_OK;
155
0
}
156
157
/**
158
 * Function description
159
 *
160
 * @return 0 on success, otherwise a Win32 error code
161
 */
162
static UINT geometry_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
163
0
{
164
0
  UINT32 length = 0;
165
0
  UINT32 cbGeometryBuffer = 0;
166
0
  MAPPED_GEOMETRY* mappedGeometry = NULL;
167
0
  GEOMETRY_PLUGIN* geometry = NULL;
168
0
  GeometryClientContext* context = NULL;
169
0
  UINT ret = CHANNEL_RC_OK;
170
0
  UINT32 updateType = 0;
171
0
  UINT32 geometryType = 0;
172
0
  UINT64 id = 0;
173
0
  wLog* logger = NULL;
174
175
0
  geometry = (GEOMETRY_PLUGIN*)callback->plugin;
176
0
  logger = geometry->base.log;
177
0
  context = (GeometryClientContext*)geometry->base.iface.pInterface;
178
179
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
180
0
    return ERROR_INVALID_DATA;
181
182
0
  Stream_Read_UINT32(s, length); /* Length (4 bytes) */
183
184
0
  if (length < 73 || !Stream_CheckAndLogRequiredLength(TAG, s, (length - 4)))
185
0
  {
186
0
    WLog_Print(logger, WLOG_ERROR, "invalid packet length");
187
0
    return ERROR_INVALID_DATA;
188
0
  }
189
190
0
  Stream_Read_UINT32(s, context->remoteVersion);
191
0
  Stream_Read_UINT64(s, id);
192
0
  Stream_Read_UINT32(s, updateType);
193
0
  Stream_Seek_UINT32(s); /* flags */
194
195
0
  mappedGeometry = HashTable_GetItemValue(context->geometries, &id);
196
197
0
  if (updateType == GEOMETRY_CLEAR)
198
0
  {
199
0
    if (!mappedGeometry)
200
0
    {
201
0
      WLog_Print(logger, WLOG_ERROR,
202
0
                 "geometry 0x%" PRIx64 " not found here, ignoring clear command", id);
203
0
      return CHANNEL_RC_OK;
204
0
    }
205
206
0
    WLog_Print(logger, WLOG_DEBUG, "clearing geometry 0x%" PRIx64 "", id);
207
208
0
    if (mappedGeometry->MappedGeometryClear &&
209
0
        !mappedGeometry->MappedGeometryClear(mappedGeometry))
210
0
      return ERROR_INTERNAL_ERROR;
211
212
0
    if (!HashTable_Remove(context->geometries, &id))
213
0
      WLog_Print(logger, WLOG_ERROR, "geometry not removed from geometries");
214
0
  }
215
0
  else if (updateType == GEOMETRY_UPDATE)
216
0
  {
217
0
    BOOL newOne = FALSE;
218
219
0
    if (!mappedGeometry)
220
0
    {
221
0
      newOne = TRUE;
222
0
      WLog_Print(logger, WLOG_DEBUG, "creating geometry 0x%" PRIx64 "", id);
223
0
      mappedGeometry = calloc(1, sizeof(MAPPED_GEOMETRY));
224
0
      if (!mappedGeometry)
225
0
        return CHANNEL_RC_NO_MEMORY;
226
227
0
      mappedGeometry->refCounter = 1;
228
0
      mappedGeometry->mappingId = id;
229
230
0
      if (!HashTable_Insert(context->geometries, &(mappedGeometry->mappingId),
231
0
                            mappedGeometry))
232
0
      {
233
0
        WLog_Print(logger, WLOG_ERROR,
234
0
                   "unable to register geometry 0x%" PRIx64 " in the table", id);
235
0
        free(mappedGeometry);
236
0
        return CHANNEL_RC_NO_MEMORY;
237
0
      }
238
0
    }
239
0
    else
240
0
    {
241
0
      WLog_Print(logger, WLOG_DEBUG, "updating geometry 0x%" PRIx64 "", id);
242
0
    }
243
244
0
    Stream_Read_UINT64(s, mappedGeometry->topLevelId);
245
246
0
    Stream_Read_INT32(s, mappedGeometry->left);
247
0
    Stream_Read_INT32(s, mappedGeometry->top);
248
0
    Stream_Read_INT32(s, mappedGeometry->right);
249
0
    Stream_Read_INT32(s, mappedGeometry->bottom);
250
251
0
    Stream_Read_INT32(s, mappedGeometry->topLevelLeft);
252
0
    Stream_Read_INT32(s, mappedGeometry->topLevelTop);
253
0
    Stream_Read_INT32(s, mappedGeometry->topLevelRight);
254
0
    Stream_Read_INT32(s, mappedGeometry->topLevelBottom);
255
256
0
    Stream_Read_UINT32(s, geometryType);
257
0
    if (geometryType != 0x02)
258
0
      WLog_Print(logger, WLOG_DEBUG, "geometryType should be set to 0x02 and is 0x%" PRIx32,
259
0
                 geometryType);
260
261
0
    Stream_Read_UINT32(s, cbGeometryBuffer);
262
0
    if (!Stream_CheckAndLogRequiredLength(TAG, s, cbGeometryBuffer))
263
0
    {
264
      // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert ownership mappedGeometry
265
0
      return ERROR_INVALID_DATA;
266
0
    }
267
268
0
    if (cbGeometryBuffer)
269
0
    {
270
0
      ret = geometry_read_RGNDATA(logger, s, cbGeometryBuffer, &mappedGeometry->geometry);
271
0
      if (ret != CHANNEL_RC_OK)
272
0
        return ret;
273
0
    }
274
0
    else
275
0
    {
276
0
      freerdp_rgndata_reset(&mappedGeometry->geometry);
277
0
    }
278
279
0
    if (newOne)
280
0
    {
281
0
      if (context->MappedGeometryAdded &&
282
0
          !context->MappedGeometryAdded(context, mappedGeometry))
283
0
      {
284
0
        WLog_Print(logger, WLOG_ERROR, "geometry added callback failed");
285
0
        ret = ERROR_INTERNAL_ERROR;
286
0
      }
287
0
    }
288
0
    else
289
0
    {
290
0
      if (mappedGeometry->MappedGeometryUpdate &&
291
0
          !mappedGeometry->MappedGeometryUpdate(mappedGeometry))
292
0
      {
293
0
        WLog_Print(logger, WLOG_ERROR, "geometry update callback failed");
294
0
        ret = ERROR_INTERNAL_ERROR;
295
0
      }
296
0
    }
297
0
  }
298
0
  else
299
0
  {
300
0
    WLog_Print(logger, WLOG_ERROR, "unknown updateType=%" PRIu32 "", updateType);
301
0
    ret = CHANNEL_RC_OK;
302
0
  }
303
304
0
  return ret;
305
0
}
306
307
/**
308
 * Function description
309
 *
310
 * @return 0 on success, otherwise a Win32 error code
311
 */
312
static UINT geometry_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
313
0
{
314
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
315
0
  return geometry_recv_pdu(callback, data);
316
0
}
317
318
/**
319
 * Function description
320
 *
321
 * @return 0 on success, otherwise a Win32 error code
322
 */
323
static UINT geometry_on_close(IWTSVirtualChannelCallback* pChannelCallback)
324
0
{
325
0
  free(pChannelCallback);
326
0
  return CHANNEL_RC_OK;
327
0
}
328
329
static void mappedGeometryUnref_void(void* arg)
330
0
{
331
0
  MAPPED_GEOMETRY* g = (MAPPED_GEOMETRY*)arg;
332
0
  mappedGeometryUnref(g);
333
0
}
334
335
/**
336
 * Channel Client Interface
337
 */
338
339
static const IWTSVirtualChannelCallback geometry_callbacks = { geometry_on_data_received,
340
                                                             NULL, /* Open */
341
                                                             geometry_on_close, NULL };
342
343
static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, WINPR_ATTR_UNUSED rdpContext* rcontext,
344
                           rdpSettings* settings)
345
0
{
346
0
  GeometryClientContext* context = NULL;
347
0
  GEOMETRY_PLUGIN* geometry = (GEOMETRY_PLUGIN*)base;
348
349
0
  WINPR_ASSERT(base);
350
0
  WINPR_UNUSED(settings);
351
352
0
  context = (GeometryClientContext*)calloc(1, sizeof(GeometryClientContext));
353
0
  if (!context)
354
0
  {
355
0
    WLog_Print(base->log, WLOG_ERROR, "calloc failed!");
356
0
    return CHANNEL_RC_NO_MEMORY;
357
0
  }
358
359
0
  context->geometries = HashTable_New(FALSE);
360
0
  if (!context->geometries)
361
0
  {
362
0
    WLog_Print(base->log, WLOG_ERROR, "unable to allocate geometries");
363
0
    free(context);
364
0
    return CHANNEL_RC_NO_MEMORY;
365
0
  }
366
367
0
  HashTable_SetHashFunction(context->geometries, mappedGeometryHash);
368
0
  {
369
0
    wObject* obj = HashTable_KeyObject(context->geometries);
370
0
    obj->fnObjectEquals = mappedGeometryKeyCompare;
371
0
  }
372
0
  {
373
0
    wObject* obj = HashTable_ValueObject(context->geometries);
374
0
    obj->fnObjectFree = mappedGeometryUnref_void;
375
0
  }
376
0
  context->handle = (void*)geometry;
377
378
0
  geometry->context = context;
379
0
  geometry->base.iface.pInterface = (void*)context;
380
381
0
  return CHANNEL_RC_OK;
382
0
}
383
384
static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
385
0
{
386
0
  GEOMETRY_PLUGIN* geometry = (GEOMETRY_PLUGIN*)base;
387
388
0
  if (geometry->context)
389
0
    HashTable_Free(geometry->context->geometries);
390
0
  free(geometry->context);
391
0
}
392
393
/**
394
 * Function description
395
 *
396
 * @return 0 on success, otherwise a Win32 error code
397
 */
398
FREERDP_ENTRY_POINT(UINT VCAPITYPE geometry_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
399
0
{
400
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, GEOMETRY_DVC_CHANNEL_NAME,
401
0
                                        sizeof(GEOMETRY_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
402
0
                                        &geometry_callbacks, init_plugin_cb, terminate_plugin_cb);
403
0
}