[PATCH v2 1/1] MdePkg : UefiFileHandleLib: fix buffer overrun in FileHandleReadLine()

Vladimir Olovyannikov

If the size of the supplied buffer in FileHandleReadLine(), module
UefiFileHandleLib.c, was not 0, but was not enough to fit in
the line, the size is increased, and then the Buffer of the new
size is zeroed. This size is always larger than the supplied buffer size,
causing supplied buffer overrun. Fix the issue by using the
supplied buffer size in ZeroMem().

Signed-off-by: Vladimir Olovyannikov <vladimir.olovyannikov@...>
Cc: Michael D Kinney <michael.d.kinney@...>
Cc: Liming Gao <liming.gao@...>
Cc: Zhiguang Liu <zhiguang.liu@...>
MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c b/MdePkg/=
index 28e28e5f67d5..ab34e6ccd5f4 100644
--- a/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
+++ b/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
@@ -969,6 +969,7 @@ FileHandleReadLine(
UINTN CharSize;=0D
UINTN CountSoFar;=0D
UINTN CrCount;=0D
+ UINTN OldSize;=0D
UINT64 OriginalFilePosition;=0D
if (Handle =3D=3D NULL=0D
@@ -1039,10 +1040,11 @@ FileHandleReadLine(
// if we ran out of space tell when...=0D
if ((CountSoFar+1-CrCount)*sizeof(CHAR16) > *Size){=0D
+ OldSize =3D *Size;=0D
*Size =3D (CountSoFar+1-CrCount)*sizeof(CHAR16);=0D
if (!Truncate) {=0D
- if (Buffer !=3D NULL && *Size !=3D 0) {=0D
- ZeroMem(Buffer, *Size);=0D
+ if (Buffer !=3D NULL && OldSize !=3D 0) {=0D
+ ZeroMem(Buffer, OldSize);=0D
FileHandleSetPosition(Handle, OriginalFilePosition);=0D

Join {devel@edk2.groups.io to automatically receive all group messages.