|
|
6fe37b |
From 865ba07a0fd4fbc7a8203482411aacca3bbfbb9f Mon Sep 17 00:00:00 2001
|
|
|
6fe37b |
From: akallabeth <akallabeth@posteo.net>
|
|
|
6fe37b |
Date: Mon, 24 Oct 2022 10:41:55 +0200
|
|
|
6fe37b |
Subject: [PATCH] Fixed path validation in drive channel
|
|
|
6fe37b |
|
|
|
6fe37b |
Check that canonical path is a subpath of the shared directory
|
|
|
6fe37b |
|
|
|
6fe37b |
(cherry picked from commit 844c94e6d0438fa7bd8ff8d5513c3f69c3018b85)
|
|
|
6fe37b |
---
|
|
|
6fe37b |
channels/drive/client/drive_file.c | 106 ++++++++++++++++++-----------
|
|
|
6fe37b |
channels/drive/client/drive_file.h | 8 +--
|
|
|
6fe37b |
channels/drive/client/drive_main.c | 8 +--
|
|
|
6fe37b |
3 files changed, 73 insertions(+), 49 deletions(-)
|
|
|
6fe37b |
|
|
|
6fe37b |
diff --git a/channels/drive/client/drive_file.c b/channels/drive/client/drive_file.c
|
|
|
6fe37b |
index 305438593..1ea4ab9da 100644
|
|
|
6fe37b |
--- a/channels/drive/client/drive_file.c
|
|
|
6fe37b |
+++ b/channels/drive/client/drive_file.c
|
|
|
6fe37b |
@@ -34,6 +34,7 @@
|
|
|
6fe37b |
#include <stdlib.h>
|
|
|
6fe37b |
#include <string.h>
|
|
|
6fe37b |
#include <time.h>
|
|
|
6fe37b |
+#include <assert.h>
|
|
|
6fe37b |
|
|
|
6fe37b |
#include <winpr/wtypes.h>
|
|
|
6fe37b |
#include <winpr/crt.h>
|
|
|
6fe37b |
@@ -61,10 +62,14 @@
|
|
|
6fe37b |
} while (0)
|
|
|
6fe37b |
#endif
|
|
|
6fe37b |
|
|
|
6fe37b |
-static void drive_file_fix_path(WCHAR* path)
|
|
|
6fe37b |
+static BOOL drive_file_fix_path(WCHAR* path, size_t length)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
size_t i;
|
|
|
6fe37b |
- size_t length = _wcslen(path);
|
|
|
6fe37b |
+
|
|
|
6fe37b |
+ if ((length == 0) || (length > UINT32_MAX))
|
|
|
6fe37b |
+ return FALSE;
|
|
|
6fe37b |
+
|
|
|
6fe37b |
+ assert(path);
|
|
|
6fe37b |
|
|
|
6fe37b |
for (i = 0; i < length; i++)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
@@ -75,58 +79,82 @@ static void drive_file_fix_path(WCHAR* path)
|
|
|
6fe37b |
#ifdef WIN32
|
|
|
6fe37b |
|
|
|
6fe37b |
if ((length == 3) && (path[1] == L':') && (path[2] == L'/'))
|
|
|
6fe37b |
- return;
|
|
|
6fe37b |
+ return FALSE;
|
|
|
6fe37b |
|
|
|
6fe37b |
#else
|
|
|
6fe37b |
|
|
|
6fe37b |
if ((length == 1) && (path[0] == L'/'))
|
|
|
6fe37b |
- return;
|
|
|
6fe37b |
+ return FALSE;
|
|
|
6fe37b |
|
|
|
6fe37b |
#endif
|
|
|
6fe37b |
|
|
|
6fe37b |
if ((length > 0) && (path[length - 1] == L'/'))
|
|
|
6fe37b |
path[length - 1] = L'\0';
|
|
|
6fe37b |
+
|
|
|
6fe37b |
+ return TRUE;
|
|
|
6fe37b |
}
|
|
|
6fe37b |
|
|
|
6fe37b |
static WCHAR* drive_file_combine_fullpath(const WCHAR* base_path, const WCHAR* path,
|
|
|
6fe37b |
- size_t PathLength)
|
|
|
6fe37b |
+ size_t PathWCharLength)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
- WCHAR* fullpath;
|
|
|
6fe37b |
- size_t base_path_length;
|
|
|
6fe37b |
+ BOOL ok = FALSE;
|
|
|
6fe37b |
+ WCHAR* fullpath = NULL;
|
|
|
6fe37b |
+ size_t length;
|
|
|
6fe37b |
|
|
|
6fe37b |
- if (!base_path || (!path && (PathLength > 0)))
|
|
|
6fe37b |
- return NULL;
|
|
|
6fe37b |
+ if (!base_path || (!path && (PathWCharLength > 0)))
|
|
|
6fe37b |
+ goto fail;
|
|
|
6fe37b |
|
|
|
6fe37b |
- base_path_length = _wcslen(base_path) * 2;
|
|
|
6fe37b |
- fullpath = (WCHAR*)calloc(1, base_path_length + PathLength + sizeof(WCHAR));
|
|
|
6fe37b |
+ const size_t base_path_length = _wcsnlen(base_path, MAX_PATH);
|
|
|
6fe37b |
+ length = base_path_length + PathWCharLength + 1;
|
|
|
6fe37b |
+ fullpath = (WCHAR*)calloc(length, sizeof(WCHAR));
|
|
|
6fe37b |
|
|
|
6fe37b |
if (!fullpath)
|
|
|
6fe37b |
+ goto fail;
|
|
|
6fe37b |
+
|
|
|
6fe37b |
+ CopyMemory(fullpath, base_path, base_path_length * sizeof(WCHAR));
|
|
|
6fe37b |
+ if (path)
|
|
|
6fe37b |
+ CopyMemory(&fullpath[base_path_length], path, PathWCharLength * sizeof(WCHAR));
|
|
|
6fe37b |
+
|
|
|
6fe37b |
+ if (!drive_file_fix_path(fullpath, length))
|
|
|
6fe37b |
+ goto fail;
|
|
|
6fe37b |
+
|
|
|
6fe37b |
+ /* Ensure the path does not contain sequences like '..' */
|
|
|
6fe37b |
+ const WCHAR dotdot[] = { '.', '.', '\0' };
|
|
|
6fe37b |
+ if (_wcsstr(&fullpath[base_path_length], dotdot))
|
|
|
6fe37b |
{
|
|
|
6fe37b |
- WLog_ERR(TAG, "malloc failed!");
|
|
|
6fe37b |
- return NULL;
|
|
|
6fe37b |
+ char abuffer[MAX_PATH] = { 0 };
|
|
|
6fe37b |
+ ConvertFromUnicode(CP_UTF8, 0, &fullpath[base_path_length], -1, (char**)&abuffer,
|
|
|
6fe37b |
+ ARRAYSIZE(abuffer) - 1, NULL, NULL);
|
|
|
6fe37b |
+
|
|
|
6fe37b |
+ WLog_WARN(TAG, "[rdpdr] received invalid file path '%s' from server, aborting!",
|
|
|
6fe37b |
+ &abuffer[base_path_length]);
|
|
|
6fe37b |
+ goto fail;
|
|
|
6fe37b |
}
|
|
|
6fe37b |
|
|
|
6fe37b |
- CopyMemory(fullpath, base_path, base_path_length);
|
|
|
6fe37b |
- if (path)
|
|
|
6fe37b |
- CopyMemory((char*)fullpath + base_path_length, path, PathLength);
|
|
|
6fe37b |
- drive_file_fix_path(fullpath);
|
|
|
6fe37b |
+ ok = TRUE;
|
|
|
6fe37b |
+fail:
|
|
|
6fe37b |
+ if (!ok)
|
|
|
6fe37b |
+ {
|
|
|
6fe37b |
+ free(fullpath);
|
|
|
6fe37b |
+ fullpath = NULL;
|
|
|
6fe37b |
+ }
|
|
|
6fe37b |
return fullpath;
|
|
|
6fe37b |
}
|
|
|
6fe37b |
|
|
|
6fe37b |
static BOOL drive_file_remove_dir(const WCHAR* path)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
- WIN32_FIND_DATAW findFileData;
|
|
|
6fe37b |
+ WIN32_FIND_DATAW findFileData = { 0 };
|
|
|
6fe37b |
BOOL ret = TRUE;
|
|
|
6fe37b |
- HANDLE dir;
|
|
|
6fe37b |
- WCHAR* fullpath;
|
|
|
6fe37b |
- WCHAR* path_slash;
|
|
|
6fe37b |
- size_t base_path_length;
|
|
|
6fe37b |
+ HANDLE dir = INVALID_HANDLE_VALUE;
|
|
|
6fe37b |
+ WCHAR* fullpath = NULL;
|
|
|
6fe37b |
+ WCHAR* path_slash = NULL;
|
|
|
6fe37b |
+ size_t base_path_length = 0;
|
|
|
6fe37b |
|
|
|
6fe37b |
if (!path)
|
|
|
6fe37b |
return FALSE;
|
|
|
6fe37b |
|
|
|
6fe37b |
- base_path_length = _wcslen(path) * 2;
|
|
|
6fe37b |
- path_slash = (WCHAR*)calloc(1, base_path_length + sizeof(WCHAR) * 3);
|
|
|
6fe37b |
+ base_path_length = _wcslen(path);
|
|
|
6fe37b |
+ path_slash = (WCHAR*)calloc(base_path_length + 3, sizeof(WCHAR));
|
|
|
6fe37b |
|
|
|
6fe37b |
if (!path_slash)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
@@ -134,12 +162,11 @@ static BOOL drive_file_remove_dir(const WCHAR* path)
|
|
|
6fe37b |
return FALSE;
|
|
|
6fe37b |
}
|
|
|
6fe37b |
|
|
|
6fe37b |
- CopyMemory(path_slash, path, base_path_length);
|
|
|
6fe37b |
- path_slash[base_path_length / 2] = L'/';
|
|
|
6fe37b |
- path_slash[base_path_length / 2 + 1] = L'*';
|
|
|
6fe37b |
+ CopyMemory(path_slash, path, base_path_length * sizeof(WCHAR));
|
|
|
6fe37b |
+ path_slash[base_path_length] = L'/';
|
|
|
6fe37b |
+ path_slash[base_path_length + 1] = L'*';
|
|
|
6fe37b |
DEBUG_WSTR("Search in %s", path_slash);
|
|
|
6fe37b |
dir = FindFirstFileW(path_slash, &findFileData);
|
|
|
6fe37b |
- path_slash[base_path_length / 2 + 1] = 0;
|
|
|
6fe37b |
|
|
|
6fe37b |
if (dir == INVALID_HANDLE_VALUE)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
@@ -149,7 +176,7 @@ static BOOL drive_file_remove_dir(const WCHAR* path)
|
|
|
6fe37b |
|
|
|
6fe37b |
do
|
|
|
6fe37b |
{
|
|
|
6fe37b |
- size_t len = _wcslen(findFileData.cFileName);
|
|
|
6fe37b |
+ const size_t len = _wcsnlen(findFileData.cFileName, ARRAYSIZE(findFileData.cFileName));
|
|
|
6fe37b |
|
|
|
6fe37b |
if ((len == 1 && findFileData.cFileName[0] == L'.') ||
|
|
|
6fe37b |
(len == 2 && findFileData.cFileName[0] == L'.' && findFileData.cFileName[1] == L'.'))
|
|
|
6fe37b |
@@ -157,7 +184,7 @@ static BOOL drive_file_remove_dir(const WCHAR* path)
|
|
|
6fe37b |
continue;
|
|
|
6fe37b |
}
|
|
|
6fe37b |
|
|
|
6fe37b |
- fullpath = drive_file_combine_fullpath(path_slash, findFileData.cFileName, len * 2);
|
|
|
6fe37b |
+ fullpath = drive_file_combine_fullpath(path_slash, findFileData.cFileName, len);
|
|
|
6fe37b |
DEBUG_WSTR("Delete %s", fullpath);
|
|
|
6fe37b |
|
|
|
6fe37b |
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
6fe37b |
@@ -333,13 +360,13 @@ static BOOL drive_file_init(DRIVE_FILE* file)
|
|
|
6fe37b |
return file->file_handle != INVALID_HANDLE_VALUE;
|
|
|
6fe37b |
}
|
|
|
6fe37b |
|
|
|
6fe37b |
-DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 PathLength, UINT32 id,
|
|
|
6fe37b |
- UINT32 DesiredAccess, UINT32 CreateDisposition, UINT32 CreateOptions,
|
|
|
6fe37b |
- UINT32 FileAttributes, UINT32 SharedAccess)
|
|
|
6fe37b |
+DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 PathWCharLength,
|
|
|
6fe37b |
+ UINT32 id, UINT32 DesiredAccess, UINT32 CreateDisposition,
|
|
|
6fe37b |
+ UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
DRIVE_FILE* file;
|
|
|
6fe37b |
|
|
|
6fe37b |
- if (!base_path || (!path && (PathLength > 0)))
|
|
|
6fe37b |
+ if (!base_path || (!path && (PathWCharLength > 0)))
|
|
|
6fe37b |
return NULL;
|
|
|
6fe37b |
|
|
|
6fe37b |
file = (DRIVE_FILE*)calloc(1, sizeof(DRIVE_FILE));
|
|
|
6fe37b |
@@ -359,7 +386,7 @@ DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 Pat
|
|
|
6fe37b |
file->CreateDisposition = CreateDisposition;
|
|
|
6fe37b |
file->CreateOptions = CreateOptions;
|
|
|
6fe37b |
file->SharedAccess = SharedAccess;
|
|
|
6fe37b |
- drive_file_set_fullpath(file, drive_file_combine_fullpath(base_path, path, PathLength));
|
|
|
6fe37b |
+ drive_file_set_fullpath(file, drive_file_combine_fullpath(base_path, path, PathWCharLength));
|
|
|
6fe37b |
|
|
|
6fe37b |
if (!drive_file_init(file))
|
|
|
6fe37b |
{
|
|
|
6fe37b |
@@ -714,13 +741,10 @@ BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UIN
|
|
|
6fe37b |
return FALSE;
|
|
|
6fe37b |
|
|
|
6fe37b |
fullpath = drive_file_combine_fullpath(file->basepath, (WCHAR*)Stream_Pointer(input),
|
|
|
6fe37b |
- FileNameLength);
|
|
|
6fe37b |
+ FileNameLength / sizeof(WCHAR));
|
|
|
6fe37b |
|
|
|
6fe37b |
if (!fullpath)
|
|
|
6fe37b |
- {
|
|
|
6fe37b |
- WLog_ERR(TAG, "drive_file_combine_fullpath failed!");
|
|
|
6fe37b |
return FALSE;
|
|
|
6fe37b |
- }
|
|
|
6fe37b |
|
|
|
6fe37b |
#ifdef _WIN32
|
|
|
6fe37b |
|
|
|
6fe37b |
@@ -759,7 +783,7 @@ BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UIN
|
|
|
6fe37b |
}
|
|
|
6fe37b |
|
|
|
6fe37b |
BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
|
|
|
6fe37b |
- const WCHAR* path, UINT32 PathLength, wStream* output)
|
|
|
6fe37b |
+ const WCHAR* path, UINT32 PathWCharLength, wStream* output)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
size_t length;
|
|
|
6fe37b |
WCHAR* ent_path;
|
|
|
6fe37b |
@@ -773,7 +797,7 @@ BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYT
|
|
|
6fe37b |
if (file->find_handle != INVALID_HANDLE_VALUE)
|
|
|
6fe37b |
FindClose(file->find_handle);
|
|
|
6fe37b |
|
|
|
6fe37b |
- ent_path = drive_file_combine_fullpath(file->basepath, path, PathLength);
|
|
|
6fe37b |
+ ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
|
|
|
6fe37b |
/* open new search handle and retrieve the first entry */
|
|
|
6fe37b |
file->find_handle = FindFirstFileW(ent_path, &file->find_data);
|
|
|
6fe37b |
free(ent_path);
|
|
|
6fe37b |
diff --git a/channels/drive/client/drive_file.h b/channels/drive/client/drive_file.h
|
|
|
6fe37b |
index ed789d6f0..6d3bd7045 100644
|
|
|
6fe37b |
--- a/channels/drive/client/drive_file.h
|
|
|
6fe37b |
+++ b/channels/drive/client/drive_file.h
|
|
|
6fe37b |
@@ -51,9 +51,9 @@ struct _DRIVE_FILE
|
|
|
6fe37b |
UINT32 CreateOptions;
|
|
|
6fe37b |
};
|
|
|
6fe37b |
|
|
|
6fe37b |
-DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 PathLength, UINT32 id,
|
|
|
6fe37b |
- UINT32 DesiredAccess, UINT32 CreateDisposition, UINT32 CreateOptions,
|
|
|
6fe37b |
- UINT32 FileAttributes, UINT32 SharedAccess);
|
|
|
6fe37b |
+DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 PathWCharLength,
|
|
|
6fe37b |
+ UINT32 id, UINT32 DesiredAccess, UINT32 CreateDisposition,
|
|
|
6fe37b |
+ UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess);
|
|
|
6fe37b |
BOOL drive_file_free(DRIVE_FILE* file);
|
|
|
6fe37b |
|
|
|
6fe37b |
BOOL drive_file_open(DRIVE_FILE* file);
|
|
|
6fe37b |
@@ -64,6 +64,6 @@ BOOL drive_file_query_information(DRIVE_FILE* file, UINT32 FsInformationClass, w
|
|
|
6fe37b |
BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
|
|
|
6fe37b |
wStream* input);
|
|
|
6fe37b |
BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
|
|
|
6fe37b |
- const WCHAR* path, UINT32 PathLength, wStream* output);
|
|
|
6fe37b |
+ const WCHAR* path, UINT32 PathWCharLength, wStream* output);
|
|
|
6fe37b |
|
|
|
6fe37b |
#endif /* FREERDP_CHANNEL_DRIVE_FILE_H */
|
|
|
6fe37b |
diff --git a/channels/drive/client/drive_main.c b/channels/drive/client/drive_main.c
|
|
|
6fe37b |
index 1b5422522..d3776381c 100644
|
|
|
6fe37b |
--- a/channels/drive/client/drive_main.c
|
|
|
6fe37b |
+++ b/channels/drive/client/drive_main.c
|
|
|
6fe37b |
@@ -184,8 +184,8 @@ static UINT drive_process_irp_create(DRIVE_DEVICE* drive, IRP* irp)
|
|
|
6fe37b |
|
|
|
6fe37b |
path = (const WCHAR*)Stream_Pointer(irp->input);
|
|
|
6fe37b |
FileId = irp->devman->id_sequence++;
|
|
|
6fe37b |
- file = drive_file_new(drive->path, path, PathLength, FileId, DesiredAccess, CreateDisposition,
|
|
|
6fe37b |
- CreateOptions, FileAttributes, SharedAccess);
|
|
|
6fe37b |
+ file = drive_file_new(drive->path, path, PathLength / sizeof(WCHAR), FileId, DesiredAccess,
|
|
|
6fe37b |
+ CreateDisposition, CreateOptions, FileAttributes, SharedAccess);
|
|
|
6fe37b |
|
|
|
6fe37b |
if (!file)
|
|
|
6fe37b |
{
|
|
|
6fe37b |
@@ -636,8 +636,8 @@ static UINT drive_process_irp_query_directory(DRIVE_DEVICE* drive, IRP* irp)
|
|
|
6fe37b |
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
6fe37b |
Stream_Write_UINT32(irp->output, 0); /* Length */
|
|
|
6fe37b |
}
|
|
|
6fe37b |
- else if (!drive_file_query_directory(file, FsInformationClass, InitialQuery, path, PathLength,
|
|
|
6fe37b |
- irp->output))
|
|
|
6fe37b |
+ else if (!drive_file_query_directory(file, FsInformationClass, InitialQuery, path,
|
|
|
6fe37b |
+ PathLength / sizeof(WCHAR), irp->output))
|
|
|
6fe37b |
{
|
|
|
6fe37b |
irp->IoStatus = drive_map_windows_err(GetLastError());
|
|
|
6fe37b |
}
|
|
|
6fe37b |
--
|
|
|
6fe37b |
2.37.1
|
|
|
6fe37b |
|