From 27b9cb33e7437f6aef184c6b2982d004d279cc04 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Tue, 30 Oct 2018 09:17:04 +0800 Subject: [PATCH] MdeModulePkg/UdfDxe: Memory free/use after free in ResolveSymlink() REF:https://bugzilla.tianocore.org/show_bug.cgi?id=1279 For function ResolveSymlink(), the below codes: if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent, sizeof (UDF_FILE_INFO)) != 0) { CleanupFileInformation (&PreviousFile); } CopyMem ((VOID *)&PreviousFile, (VOID *)File, sizeof (UDF_FILE_INFO)); If the contents in 'PreviousFile' and 'File' are the same, call to "CleanupFileInformation (&PreviousFile);" will free the buffers in 'File' as well. This will lead to potential memory double free/use after free issues. This commit will add additional check to address the above issue. Cc: Ruiyu Ni Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Hao Wu Reviewed-by: Paulo Alcantara Reviewed-by: Star Zeng Reviewed-by: Leif Lindholm --- .../Universal/Disk/UdfDxe/FileSystemOperations.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c b/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c index 14b1deac92..d38b6c911d 100644 --- a/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c +++ b/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c @@ -2144,6 +2144,8 @@ ResolveSymlink ( UINTN Index; UINT8 CompressionId; UDF_FILE_INFO PreviousFile; + BOOLEAN NotParent; + BOOLEAN NotFile; ZeroMem ((VOID *)File, sizeof (UDF_FILE_INFO)); @@ -2298,12 +2300,18 @@ ResolveSymlink ( goto Error_Find_File; } - if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent, - sizeof (UDF_FILE_INFO)) != 0) { + NotParent = (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent, + sizeof (UDF_FILE_INFO)) != 0); + NotFile = (CompareMem ((VOID *)&PreviousFile, (VOID *)File, + sizeof (UDF_FILE_INFO)) != 0); + + if (NotParent && NotFile) { CleanupFileInformation (&PreviousFile); } - CopyMem ((VOID *)&PreviousFile, (VOID *)File, sizeof (UDF_FILE_INFO)); + if (NotFile) { + CopyMem ((VOID *)&PreviousFile, (VOID *)File, sizeof (UDF_FILE_INFO)); + } } //