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

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