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

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