Coverage Report

Created: 2025-10-10 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/geometry/client/geometry_main.c
Line
Count
Source
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
  WINPR_ASSERT(rgndata);
69
70
0
  if (len < 32)
71
0
  {
72
0
    WLog_Print(logger, WLOG_ERROR, "invalid RGNDATA");
73
0
    return ERROR_INVALID_DATA;
74
0
  }
75
76
0
  const UINT32 dwSize = Stream_Get_UINT32(s);
77
78
0
  if (dwSize != 32)
79
0
  {
80
0
    WLog_Print(logger, WLOG_ERROR, "invalid RGNDATA dwSize");
81
0
    return ERROR_INVALID_DATA;
82
0
  }
83
84
0
  const UINT32 iType = Stream_Get_UINT32(s);
85
86
0
  if (iType != RDH_RECTANGLE)
87
0
  {
88
0
    WLog_Print(logger, WLOG_ERROR, "iType %" PRIu32 " for RGNDATA is not supported", iType);
89
0
    return ERROR_UNSUPPORTED_TYPE;
90
0
  }
91
92
0
  rgndata->nRectCount = Stream_Get_UINT32(s);
93
0
  Stream_Seek_UINT32(s); /* nRgnSize IGNORED */
94
0
  {
95
0
    const INT32 x = Stream_Get_INT32(s);
96
0
    const INT32 y = Stream_Get_INT32(s);
97
0
    const INT32 right = Stream_Get_INT32(s);
98
0
    const INT32 bottom = Stream_Get_INT32(s);
99
0
    if ((abs(x) > INT16_MAX) || (abs(y) > INT16_MAX))
100
0
      return ERROR_INVALID_DATA;
101
0
    const INT32 w = right - x;
102
0
    const INT32 h = bottom - y;
103
0
    if ((abs(w) > INT16_MAX) || (abs(h) > INT16_MAX))
104
0
      return ERROR_INVALID_DATA;
105
0
    rgndata->boundingRect.x = (INT16)x;
106
0
    rgndata->boundingRect.y = (INT16)y;
107
0
    rgndata->boundingRect.width = (INT16)w;
108
0
    rgndata->boundingRect.height = (INT16)h;
109
0
  }
110
0
  len -= 32;
111
112
0
  if (len / (4 * 4) < rgndata->nRectCount)
113
0
  {
114
0
    WLog_Print(logger, WLOG_ERROR, "not enough data for region rectangles");
115
0
    return ERROR_INVALID_DATA;
116
0
  }
117
118
0
  if (rgndata->nRectCount)
119
0
  {
120
0
    RDP_RECT* tmp = realloc(rgndata->rects, rgndata->nRectCount * sizeof(RDP_RECT));
121
122
0
    if (!tmp)
123
0
    {
124
0
      WLog_Print(logger, WLOG_ERROR, "unable to allocate memory for %" PRIu32 " RECTs",
125
0
                 rgndata->nRectCount);
126
0
      return CHANNEL_RC_NO_MEMORY;
127
0
    }
128
0
    rgndata->rects = tmp;
129
130
0
    for (UINT32 i = 0; i < rgndata->nRectCount; i++)
131
0
    {
132
0
      RDP_RECT* rect = &rgndata->rects[i];
133
134
0
      if (!Stream_CheckAndLogRequiredLengthWLog(logger, s, 16))
135
0
        return CHANNEL_RC_NULL_DATA;
136
137
0
      const INT32 x = Stream_Get_INT32(s);
138
0
      const INT32 y = Stream_Get_INT32(s);
139
0
      const INT32 right = Stream_Get_INT32(s);
140
0
      const INT32 bottom = Stream_Get_INT32(s);
141
0
      if ((abs(x) > INT16_MAX) || (abs(y) > INT16_MAX))
142
0
        return ERROR_INVALID_DATA;
143
144
0
      const INT32 w = right - x;
145
0
      const INT32 h = bottom - y;
146
0
      if ((abs(w) > INT16_MAX) || (abs(h) > INT16_MAX))
147
0
        return ERROR_INVALID_DATA;
148
149
0
      rect->x = (INT16)x;
150
0
      rect->y = (INT16)y;
151
0
      rect->width = (INT16)w;
152
0
      rect->height = (INT16)h;
153
0
    }
154
0
  }
155
156
0
  return CHANNEL_RC_OK;
157
0
}
158
159
/**
160
 * Function description
161
 *
162
 * @return 0 on success, otherwise a Win32 error code
163
 */
164
static UINT geometry_recv_pdu(GENERIC_CHANNEL_CALLBACK* callback, wStream* s)
165
0
{
166
0
  UINT ret = CHANNEL_RC_OK;
167
168
0
  WINPR_ASSERT(callback);
169
0
  GEOMETRY_PLUGIN* geometry = (GEOMETRY_PLUGIN*)callback->plugin;
170
0
  WINPR_ASSERT(geometry);
171
172
0
  wLog* logger = geometry->base.log;
173
0
  GeometryClientContext* context = (GeometryClientContext*)geometry->base.iface.pInterface;
174
0
  WINPR_ASSERT(context);
175
176
0
  if (!Stream_CheckAndLogRequiredLengthWLog(logger, s, 4))
177
0
    return ERROR_INVALID_DATA;
178
179
0
  const UINT32 length = Stream_Get_UINT32(s); /* Length (4 bytes) */
180
181
0
  if (!Stream_CheckAndLogRequiredLengthWLog(logger, s, (length - 4)))
182
0
  {
183
0
    WLog_Print(logger, WLOG_ERROR, "invalid packet length");
184
0
    return ERROR_INVALID_DATA;
185
0
  }
186
187
0
  if (!Stream_CheckAndLogRequiredLengthWLog(logger, s, 20))
188
0
    return ERROR_INVALID_DATA;
189
190
0
  context->remoteVersion = Stream_Get_UINT32(s);
191
0
  const UINT64 id = Stream_Get_UINT64(s);
192
0
  const UINT32 updateType = Stream_Get_UINT32(s);
193
0
  Stream_Seek_UINT32(s); /* flags */
194
195
0
  MAPPED_GEOMETRY* 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
    if (!Stream_CheckAndLogRequiredLengthWLog(logger, s, 48))
245
0
    {
246
      // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert ownership mappedGeometry
247
0
      return ERROR_INVALID_DATA;
248
0
    }
249
250
0
    mappedGeometry->topLevelId = Stream_Get_UINT64(s);
251
252
0
    mappedGeometry->left = Stream_Get_INT32(s);
253
0
    mappedGeometry->top = Stream_Get_INT32(s);
254
0
    mappedGeometry->right = Stream_Get_INT32(s);
255
0
    mappedGeometry->bottom = Stream_Get_INT32(s);
256
257
0
    mappedGeometry->topLevelLeft = Stream_Get_INT32(s);
258
0
    mappedGeometry->topLevelTop = Stream_Get_INT32(s);
259
0
    mappedGeometry->topLevelRight = Stream_Get_INT32(s);
260
0
    mappedGeometry->topLevelBottom = Stream_Get_INT32(s);
261
262
0
    const UINT32 geometryType = Stream_Get_UINT32(s);
263
0
    if (geometryType != 0x02)
264
0
      WLog_Print(logger, WLOG_DEBUG, "geometryType should be set to 0x02 and is 0x%" PRIx32,
265
0
                 geometryType);
266
267
0
    const UINT32 cbGeometryBuffer = Stream_Get_UINT32(s);
268
0
    if (!Stream_CheckAndLogRequiredLengthWLog(logger, s, cbGeometryBuffer))
269
0
    {
270
      // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert ownership mappedGeometry
271
0
      return ERROR_INVALID_DATA;
272
0
    }
273
274
0
    if (cbGeometryBuffer > 0)
275
0
    {
276
0
      ret = geometry_read_RGNDATA(logger, s, cbGeometryBuffer, &mappedGeometry->geometry);
277
0
      if (ret != CHANNEL_RC_OK)
278
0
        return ret;
279
0
    }
280
0
    else
281
0
    {
282
0
      freerdp_rgndata_reset(&mappedGeometry->geometry);
283
0
    }
284
285
0
    if (newOne)
286
0
    {
287
0
      if (context->MappedGeometryAdded &&
288
0
          !context->MappedGeometryAdded(context, mappedGeometry))
289
0
      {
290
0
        WLog_Print(logger, WLOG_ERROR, "geometry added callback failed");
291
0
        ret = ERROR_INTERNAL_ERROR;
292
0
      }
293
0
    }
294
0
    else
295
0
    {
296
0
      if (mappedGeometry->MappedGeometryUpdate &&
297
0
          !mappedGeometry->MappedGeometryUpdate(mappedGeometry))
298
0
      {
299
0
        WLog_Print(logger, WLOG_ERROR, "geometry update callback failed");
300
0
        ret = ERROR_INTERNAL_ERROR;
301
0
      }
302
0
    }
303
0
  }
304
0
  else
305
0
  {
306
0
    WLog_Print(logger, WLOG_ERROR, "unknown updateType=%" PRIu32 "", updateType);
307
0
    ret = CHANNEL_RC_OK;
308
0
  }
309
310
0
  return ret;
311
0
}
312
313
/**
314
 * Function description
315
 *
316
 * @return 0 on success, otherwise a Win32 error code
317
 */
318
static UINT geometry_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
319
0
{
320
0
  GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
321
0
  return geometry_recv_pdu(callback, data);
322
0
}
323
324
/**
325
 * Function description
326
 *
327
 * @return 0 on success, otherwise a Win32 error code
328
 */
329
static UINT geometry_on_close(IWTSVirtualChannelCallback* pChannelCallback)
330
0
{
331
0
  free(pChannelCallback);
332
0
  return CHANNEL_RC_OK;
333
0
}
334
335
static void mappedGeometryUnref_void(void* arg)
336
0
{
337
0
  MAPPED_GEOMETRY* g = (MAPPED_GEOMETRY*)arg;
338
0
  mappedGeometryUnref(g);
339
0
}
340
341
/**
342
 * Channel Client Interface
343
 */
344
345
static const IWTSVirtualChannelCallback geometry_callbacks = { geometry_on_data_received,
346
                                                             NULL, /* Open */
347
                                                             geometry_on_close, NULL };
348
349
static UINT init_plugin_cb(GENERIC_DYNVC_PLUGIN* base, WINPR_ATTR_UNUSED rdpContext* rcontext,
350
                           rdpSettings* settings)
351
0
{
352
0
  GeometryClientContext* context = NULL;
353
0
  GEOMETRY_PLUGIN* geometry = (GEOMETRY_PLUGIN*)base;
354
355
0
  WINPR_ASSERT(base);
356
0
  WINPR_UNUSED(settings);
357
358
0
  context = (GeometryClientContext*)calloc(1, sizeof(GeometryClientContext));
359
0
  if (!context)
360
0
  {
361
0
    WLog_Print(base->log, WLOG_ERROR, "calloc failed!");
362
0
    return CHANNEL_RC_NO_MEMORY;
363
0
  }
364
365
0
  context->geometries = HashTable_New(FALSE);
366
0
  if (!context->geometries)
367
0
  {
368
0
    WLog_Print(base->log, WLOG_ERROR, "unable to allocate geometries");
369
0
    free(context);
370
0
    return CHANNEL_RC_NO_MEMORY;
371
0
  }
372
373
0
  HashTable_SetHashFunction(context->geometries, mappedGeometryHash);
374
0
  {
375
0
    wObject* obj = HashTable_KeyObject(context->geometries);
376
0
    obj->fnObjectEquals = mappedGeometryKeyCompare;
377
0
  }
378
0
  {
379
0
    wObject* obj = HashTable_ValueObject(context->geometries);
380
0
    obj->fnObjectFree = mappedGeometryUnref_void;
381
0
  }
382
0
  context->handle = (void*)geometry;
383
384
0
  geometry->context = context;
385
0
  geometry->base.iface.pInterface = (void*)context;
386
387
0
  return CHANNEL_RC_OK;
388
0
}
389
390
static void terminate_plugin_cb(GENERIC_DYNVC_PLUGIN* base)
391
0
{
392
0
  GEOMETRY_PLUGIN* geometry = (GEOMETRY_PLUGIN*)base;
393
394
0
  if (geometry->context)
395
0
    HashTable_Free(geometry->context->geometries);
396
0
  free(geometry->context);
397
0
}
398
399
/**
400
 * Function description
401
 *
402
 * @return 0 on success, otherwise a Win32 error code
403
 */
404
FREERDP_ENTRY_POINT(UINT VCAPITYPE geometry_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
405
0
{
406
0
  return freerdp_generic_DVCPluginEntry(pEntryPoints, TAG, GEOMETRY_DVC_CHANNEL_NAME,
407
0
                                        sizeof(GEOMETRY_PLUGIN), sizeof(GENERIC_CHANNEL_CALLBACK),
408
0
                                        &geometry_callbacks, init_plugin_cb, terminate_plugin_cb);
409
0
}