Blame SOURCES/Fixed-path-validation-in-drive-channel.patch

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