Coverage Report

Created: 2024-05-20 06:11

/src/FreeRDP/channels/cliprdr/cliprdr_common.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Cliprdr common
4
 *
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 * Copyright 2019 Kobi Mizrachi <kmizrachi18@gmail.com>
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 *     http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22
23
#include <winpr/crt.h>
24
#include <winpr/stream.h>
25
#include <freerdp/channels/log.h>
26
27
#define TAG CHANNELS_TAG("cliprdr.common")
28
29
#include "cliprdr_common.h"
30
31
static BOOL cliprdr_validate_file_contents_request(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
32
0
{
33
  /*
34
   * [MS-RDPECLIP] 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST).
35
   *
36
   * A request for the size of the file identified by the lindex field. The size MUST be
37
   * returned as a 64-bit, unsigned integer. The cbRequested field MUST be set to
38
   * 0x00000008 and both the nPositionLow and nPositionHigh fields MUST be
39
   * set to 0x00000000.
40
   */
41
42
0
  if (request->dwFlags & FILECONTENTS_SIZE)
43
0
  {
44
0
    if (request->cbRequested != sizeof(UINT64))
45
0
    {
46
0
      WLog_ERR(TAG, "cbRequested must be %" PRIu32 ", got %" PRIu32 "", sizeof(UINT64),
47
0
               request->cbRequested);
48
0
      return FALSE;
49
0
    }
50
51
0
    if (request->nPositionHigh != 0 || request->nPositionLow != 0)
52
0
    {
53
0
      WLog_ERR(TAG, "nPositionHigh and nPositionLow must be set to 0");
54
0
      return FALSE;
55
0
    }
56
0
  }
57
58
0
  return TRUE;
59
0
}
60
61
wStream* cliprdr_packet_new(UINT16 msgType, UINT16 msgFlags, UINT32 dataLen)
62
0
{
63
0
  wStream* s = NULL;
64
0
  s = Stream_New(NULL, dataLen + 8);
65
66
0
  if (!s)
67
0
  {
68
0
    WLog_ERR(TAG, "Stream_New failed!");
69
0
    return NULL;
70
0
  }
71
72
0
  Stream_Write_UINT16(s, msgType);
73
0
  Stream_Write_UINT16(s, msgFlags);
74
  /* Write actual length after the entire packet has been constructed. */
75
0
  Stream_Write_UINT32(s, 0);
76
0
  return s;
77
0
}
78
79
static void cliprdr_write_file_contents_request(wStream* s,
80
                                                const CLIPRDR_FILE_CONTENTS_REQUEST* request)
81
0
{
82
0
  Stream_Write_UINT32(s, request->streamId);      /* streamId (4 bytes) */
83
0
  Stream_Write_UINT32(s, request->listIndex);     /* listIndex (4 bytes) */
84
0
  Stream_Write_UINT32(s, request->dwFlags);       /* dwFlags (4 bytes) */
85
0
  Stream_Write_UINT32(s, request->nPositionLow);  /* nPositionLow (4 bytes) */
86
0
  Stream_Write_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
87
0
  Stream_Write_UINT32(s, request->cbRequested);   /* cbRequested (4 bytes) */
88
89
0
  if (request->haveClipDataId)
90
0
    Stream_Write_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
91
0
}
92
93
static INLINE void cliprdr_write_lock_unlock_clipdata(wStream* s, UINT32 clipDataId)
94
0
{
95
0
  Stream_Write_UINT32(s, clipDataId);
96
0
}
97
98
static void cliprdr_write_lock_clipdata(wStream* s,
99
                                        const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
100
0
{
101
0
  cliprdr_write_lock_unlock_clipdata(s, lockClipboardData->clipDataId);
102
0
}
103
104
static void cliprdr_write_unlock_clipdata(wStream* s,
105
                                          const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
106
0
{
107
0
  cliprdr_write_lock_unlock_clipdata(s, unlockClipboardData->clipDataId);
108
0
}
109
110
static void cliprdr_write_file_contents_response(wStream* s,
111
                                                 const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
112
0
{
113
0
  Stream_Write_UINT32(s, response->streamId); /* streamId (4 bytes) */
114
0
  Stream_Write(s, response->requestedData, response->cbRequested);
115
0
}
116
117
wStream* cliprdr_packet_lock_clipdata_new(const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
118
0
{
119
0
  wStream* s = NULL;
120
121
0
  if (!lockClipboardData)
122
0
    return NULL;
123
124
0
  s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
125
126
0
  if (!s)
127
0
    return NULL;
128
129
0
  cliprdr_write_lock_clipdata(s, lockClipboardData);
130
0
  return s;
131
0
}
132
133
wStream*
134
cliprdr_packet_unlock_clipdata_new(const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
135
0
{
136
0
  wStream* s = NULL;
137
138
0
  if (!unlockClipboardData)
139
0
    return NULL;
140
141
0
  s = cliprdr_packet_new(CB_UNLOCK_CLIPDATA, 0, 4);
142
143
0
  if (!s)
144
0
    return NULL;
145
146
0
  cliprdr_write_unlock_clipdata(s, unlockClipboardData);
147
0
  return s;
148
0
}
149
150
wStream* cliprdr_packet_file_contents_request_new(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
151
0
{
152
0
  wStream* s = NULL;
153
154
0
  if (!request)
155
0
    return NULL;
156
157
0
  s = cliprdr_packet_new(CB_FILECONTENTS_REQUEST, 0, 28);
158
159
0
  if (!s)
160
0
    return NULL;
161
162
0
  cliprdr_write_file_contents_request(s, request);
163
0
  return s;
164
0
}
165
166
wStream* cliprdr_packet_file_contents_response_new(const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
167
0
{
168
0
  wStream* s = NULL;
169
170
0
  if (!response)
171
0
    return NULL;
172
173
0
  s = cliprdr_packet_new(CB_FILECONTENTS_RESPONSE, response->common.msgFlags,
174
0
                         4 + response->cbRequested);
175
176
0
  if (!s)
177
0
    return NULL;
178
179
0
  cliprdr_write_file_contents_response(s, response);
180
0
  return s;
181
0
}
182
183
wStream* cliprdr_packet_format_list_new(const CLIPRDR_FORMAT_LIST* formatList,
184
                                        BOOL useLongFormatNames)
185
0
{
186
0
  wStream* s = NULL;
187
0
  size_t formatNameSize = 0;
188
0
  char* szFormatName = NULL;
189
0
  WCHAR* wszFormatName = NULL;
190
0
  BOOL asciiNames = FALSE;
191
0
  CLIPRDR_FORMAT* format = NULL;
192
0
  UINT32 length = 0;
193
194
0
  if (formatList->common.msgType != CB_FORMAT_LIST)
195
0
    WLog_WARN(TAG, "called with invalid type %08" PRIx32, formatList->common.msgType);
196
197
0
  if (!useLongFormatNames)
198
0
  {
199
0
    length = formatList->numFormats * 36;
200
0
    s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
201
202
0
    if (!s)
203
0
    {
204
0
      WLog_ERR(TAG, "cliprdr_packet_new failed!");
205
0
      return NULL;
206
0
    }
207
208
0
    for (UINT32 index = 0; index < formatList->numFormats; index++)
209
0
    {
210
0
      size_t formatNameLength = 0;
211
0
      format = (CLIPRDR_FORMAT*)&(formatList->formats[index]);
212
0
      Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
213
0
      formatNameSize = 0;
214
215
0
      szFormatName = format->formatName;
216
217
0
      if (asciiNames)
218
0
      {
219
0
        if (szFormatName)
220
0
          formatNameLength = strnlen(szFormatName, 32);
221
222
0
        if (formatNameLength > 31)
223
0
          formatNameLength = 31;
224
225
0
        Stream_Write(s, szFormatName, formatNameLength);
226
0
        Stream_Zero(s, 32 - formatNameLength);
227
0
      }
228
0
      else
229
0
      {
230
0
        wszFormatName = NULL;
231
232
0
        if (szFormatName)
233
0
        {
234
0
          wszFormatName = ConvertUtf8ToWCharAlloc(szFormatName, &formatNameSize);
235
236
0
          if (!wszFormatName)
237
0
          {
238
0
            Stream_Free(s, TRUE);
239
0
            return NULL;
240
0
          }
241
0
          formatNameSize += 1; /* append terminating '\0' */
242
0
        }
243
244
0
        if (formatNameSize > 15)
245
0
          formatNameSize = 15;
246
247
        /* size in bytes  instead of wchar */
248
0
        formatNameSize *= sizeof(WCHAR);
249
250
0
        if (wszFormatName)
251
0
          Stream_Write(s, wszFormatName, (size_t)formatNameSize);
252
253
0
        Stream_Zero(s, (size_t)(32 - formatNameSize));
254
0
        free(wszFormatName);
255
0
      }
256
0
    }
257
0
  }
258
0
  else
259
0
  {
260
0
    length = 0;
261
0
    for (UINT32 index = 0; index < formatList->numFormats; index++)
262
0
    {
263
0
      format = (CLIPRDR_FORMAT*)&(formatList->formats[index]);
264
0
      length += 4;
265
0
      formatNameSize = sizeof(WCHAR);
266
267
0
      if (format->formatName)
268
0
      {
269
0
        SSIZE_T size = ConvertUtf8ToWChar(format->formatName, NULL, 0);
270
0
        if (size < 0)
271
0
          return NULL;
272
0
        formatNameSize = (size_t)(size + 1) * sizeof(WCHAR);
273
0
      }
274
275
0
      length += (UINT32)formatNameSize;
276
0
    }
277
278
0
    s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
279
280
0
    if (!s)
281
0
    {
282
0
      WLog_ERR(TAG, "cliprdr_packet_new failed!");
283
0
      return NULL;
284
0
    }
285
286
0
    for (UINT32 index = 0; index < formatList->numFormats; index++)
287
0
    {
288
0
      format = (CLIPRDR_FORMAT*)&(formatList->formats[index]);
289
0
      Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
290
291
0
      if (format->formatName)
292
0
      {
293
0
        const size_t cap = Stream_Capacity(s);
294
0
        const size_t pos = Stream_GetPosition(s);
295
0
        const size_t rem = cap - pos;
296
0
        if ((cap < pos) || ((rem / 2) > INT_MAX))
297
0
        {
298
0
          Stream_Free(s, TRUE);
299
0
          return NULL;
300
0
        }
301
302
0
        const size_t len = strnlen(format->formatName, rem / sizeof(WCHAR));
303
0
        if (Stream_Write_UTF16_String_From_UTF8(s, len + 1, format->formatName, len, TRUE) <
304
0
            0)
305
0
        {
306
0
          Stream_Free(s, TRUE);
307
0
          return NULL;
308
0
        }
309
0
      }
310
0
      else
311
0
      {
312
0
        Stream_Write_UINT16(s, 0);
313
0
      }
314
0
    }
315
0
  }
316
317
0
  return s;
318
0
}
319
UINT cliprdr_read_unlock_clipdata(wStream* s, CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
320
0
{
321
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
322
0
    return ERROR_INVALID_DATA;
323
324
0
  Stream_Read_UINT32(s, unlockClipboardData->clipDataId); /* clipDataId (4 bytes) */
325
0
  return CHANNEL_RC_OK;
326
0
}
327
328
UINT cliprdr_read_format_data_request(wStream* s, CLIPRDR_FORMAT_DATA_REQUEST* request)
329
0
{
330
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
331
0
    return ERROR_INVALID_DATA;
332
333
0
  Stream_Read_UINT32(s, request->requestedFormatId); /* requestedFormatId (4 bytes) */
334
0
  return CHANNEL_RC_OK;
335
0
}
336
337
UINT cliprdr_read_format_data_response(wStream* s, CLIPRDR_FORMAT_DATA_RESPONSE* response)
338
0
{
339
0
  response->requestedFormatData = NULL;
340
341
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, response->common.dataLen))
342
0
    return ERROR_INVALID_DATA;
343
344
0
  if (response->common.dataLen)
345
0
    response->requestedFormatData = Stream_ConstPointer(s);
346
347
0
  return CHANNEL_RC_OK;
348
0
}
349
350
UINT cliprdr_read_file_contents_request(wStream* s, CLIPRDR_FILE_CONTENTS_REQUEST* request)
351
0
{
352
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
353
0
    return ERROR_INVALID_DATA;
354
355
0
  request->haveClipDataId = FALSE;
356
0
  Stream_Read_UINT32(s, request->streamId);      /* streamId (4 bytes) */
357
0
  Stream_Read_UINT32(s, request->listIndex);     /* listIndex (4 bytes) */
358
0
  Stream_Read_UINT32(s, request->dwFlags);       /* dwFlags (4 bytes) */
359
0
  Stream_Read_UINT32(s, request->nPositionLow);  /* nPositionLow (4 bytes) */
360
0
  Stream_Read_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
361
0
  Stream_Read_UINT32(s, request->cbRequested);   /* cbRequested (4 bytes) */
362
363
0
  if (Stream_GetRemainingLength(s) >= 4)
364
0
  {
365
0
    Stream_Read_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
366
0
    request->haveClipDataId = TRUE;
367
0
  }
368
369
0
  if (!cliprdr_validate_file_contents_request(request))
370
0
    return ERROR_BAD_ARGUMENTS;
371
372
0
  return CHANNEL_RC_OK;
373
0
}
374
375
UINT cliprdr_read_file_contents_response(wStream* s, CLIPRDR_FILE_CONTENTS_RESPONSE* response)
376
0
{
377
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
378
0
    return ERROR_INVALID_DATA;
379
380
0
  Stream_Read_UINT32(s, response->streamId);   /* streamId (4 bytes) */
381
0
  response->requestedData = Stream_ConstPointer(s); /* requestedFileContentsData */
382
383
0
  WINPR_ASSERT(response->common.dataLen >= 4);
384
0
  response->cbRequested = response->common.dataLen - 4;
385
0
  return CHANNEL_RC_OK;
386
0
}
387
388
UINT cliprdr_read_format_list(wStream* s, CLIPRDR_FORMAT_LIST* formatList, BOOL useLongFormatNames)
389
0
{
390
0
  UINT32 index = 0;
391
0
  size_t formatNameLength = 0;
392
0
  const char* szFormatName = NULL;
393
0
  const WCHAR* wszFormatName = NULL;
394
0
  wStream sub1buffer = { 0 };
395
0
  CLIPRDR_FORMAT* formats = NULL;
396
0
  UINT error = ERROR_INTERNAL_ERROR;
397
398
0
  const BOOL asciiNames = (formatList->common.msgFlags & CB_ASCII_NAMES) ? TRUE : FALSE;
399
400
0
  index = 0;
401
  /* empty format list */
402
0
  formatList->formats = NULL;
403
0
  formatList->numFormats = 0;
404
405
0
  wStream* sub1 =
406
0
      Stream_StaticConstInit(&sub1buffer, Stream_ConstPointer(s), formatList->common.dataLen);
407
0
  if (!Stream_SafeSeek(s, formatList->common.dataLen))
408
0
    return ERROR_INVALID_DATA;
409
410
0
  if (!formatList->common.dataLen)
411
0
  {
412
0
  }
413
0
  else if (!useLongFormatNames)
414
0
  {
415
0
    const size_t cap = Stream_Capacity(sub1);
416
0
    formatList->numFormats = (cap / 36);
417
418
0
    if ((formatList->numFormats * 36) != cap)
419
0
    {
420
0
      WLog_ERR(TAG, "Invalid short format list length: %" PRIuz "", cap);
421
0
      return ERROR_INTERNAL_ERROR;
422
0
    }
423
424
0
    if (formatList->numFormats)
425
0
      formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
426
427
0
    if (!formats)
428
0
    {
429
0
      WLog_ERR(TAG, "calloc failed!");
430
0
      return CHANNEL_RC_NO_MEMORY;
431
0
    }
432
433
0
    formatList->formats = formats;
434
435
0
    while (Stream_GetRemainingLength(sub1) >= 4)
436
0
    {
437
0
      CLIPRDR_FORMAT* format = &formats[index];
438
439
0
      Stream_Read_UINT32(sub1, format->formatId); /* formatId (4 bytes) */
440
441
      /* According to MS-RDPECLIP 2.2.3.1.1.1 formatName is "a 32-byte block containing
442
       * the *null-terminated* name assigned to the Clipboard Format: (32 ASCII 8 characters
443
       * or 16 Unicode characters)"
444
       * However, both Windows RDSH and mstsc violate this specs as seen in the following
445
       * example of a transferred short format name string: [R.i.c.h. .T.e.x.t. .F.o.r.m.a.t.]
446
       * These are 16 unicode charaters - *without* terminating null !
447
       */
448
449
0
      szFormatName = Stream_ConstPointer(sub1);
450
0
      wszFormatName = Stream_ConstPointer(sub1);
451
0
      if (!Stream_SafeSeek(sub1, 32))
452
0
        goto error_out;
453
454
0
      free(format->formatName);
455
0
      format->formatName = NULL;
456
457
0
      if (asciiNames)
458
0
      {
459
0
        if (szFormatName[0])
460
0
        {
461
          /* ensure null termination */
462
0
          format->formatName = strndup(szFormatName, 31);
463
0
          if (!format->formatName)
464
0
          {
465
0
            WLog_ERR(TAG, "malloc failed!");
466
0
            error = CHANNEL_RC_NO_MEMORY;
467
0
            goto error_out;
468
0
          }
469
0
        }
470
0
      }
471
0
      else
472
0
      {
473
0
        if (wszFormatName[0])
474
0
        {
475
0
          format->formatName = ConvertWCharNToUtf8Alloc(wszFormatName, 16, NULL);
476
0
          if (!format->formatName)
477
0
            goto error_out;
478
0
        }
479
0
      }
480
481
0
      index++;
482
0
    }
483
0
  }
484
0
  else
485
0
  {
486
0
    wStream sub2buffer = sub1buffer;
487
0
    wStream* sub2 = &sub2buffer;
488
489
0
    while (Stream_GetRemainingLength(sub1) > 0)
490
0
    {
491
0
      size_t rest = 0;
492
0
      if (!Stream_SafeSeek(sub1, 4)) /* formatId (4 bytes) */
493
0
        goto error_out;
494
495
0
      wszFormatName = Stream_ConstPointer(sub1);
496
0
      rest = Stream_GetRemainingLength(sub1);
497
0
      formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
498
499
0
      if (!Stream_SafeSeek(sub1, (formatNameLength + 1) * sizeof(WCHAR)))
500
0
        goto error_out;
501
0
      formatList->numFormats++;
502
0
    }
503
504
0
    if (formatList->numFormats)
505
0
      formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
506
507
0
    if (!formats)
508
0
    {
509
0
      WLog_ERR(TAG, "calloc failed!");
510
0
      return CHANNEL_RC_NO_MEMORY;
511
0
    }
512
513
0
    formatList->formats = formats;
514
515
0
    while (Stream_GetRemainingLength(sub2) >= 4)
516
0
    {
517
0
      size_t rest = 0;
518
0
      CLIPRDR_FORMAT* format = &formats[index];
519
520
0
      Stream_Read_UINT32(sub2, format->formatId); /* formatId (4 bytes) */
521
522
0
      free(format->formatName);
523
0
      format->formatName = NULL;
524
525
0
      wszFormatName = Stream_ConstPointer(sub2);
526
0
      rest = Stream_GetRemainingLength(sub2);
527
0
      formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
528
0
      if (!Stream_SafeSeek(sub2, (formatNameLength + 1) * sizeof(WCHAR)))
529
0
        goto error_out;
530
531
0
      if (formatNameLength)
532
0
      {
533
0
        format->formatName =
534
0
            ConvertWCharNToUtf8Alloc(wszFormatName, formatNameLength, NULL);
535
0
        if (!format->formatName)
536
0
          goto error_out;
537
0
      }
538
539
0
      index++;
540
0
    }
541
0
  }
542
543
0
  return CHANNEL_RC_OK;
544
545
0
error_out:
546
0
  cliprdr_free_format_list(formatList);
547
0
  return error;
548
0
}
549
550
void cliprdr_free_format_list(CLIPRDR_FORMAT_LIST* formatList)
551
0
{
552
0
  if (formatList == NULL)
553
0
    return;
554
555
0
  if (formatList->formats)
556
0
  {
557
0
    for (UINT32 index = 0; index < formatList->numFormats; index++)
558
0
    {
559
0
      free(formatList->formats[index].formatName);
560
0
    }
561
562
0
    free(formatList->formats);
563
0
    formatList->formats = NULL;
564
0
    formatList->numFormats = 0;
565
0
  }
566
0
}