Skip to content

Commit 322ac05

Browse files
hwu25mergify[bot]
authored andcommitted
MdeModulePkg/PiDxeS3BootScriptLib: Fix potential numeric truncation (CVE-2019-14563)
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2001 For S3BootScriptLib APIs: S3BootScriptSaveIoWrite S3BootScriptSaveMemWrite S3BootScriptSavePciCfgWrite S3BootScriptSavePciCfg2Write S3BootScriptSaveSmbusExecute S3BootScriptSaveInformation S3BootScriptSaveInformationAsciiString S3BootScriptLabel (happen in S3BootScriptLabelInternal()) possible numeric truncations will happen that may lead to S3 boot script entry with improper size being returned to store the boot script data. This commit will add checks to prevent this kind of issue. Please note that the remaining S3BootScriptLib APIs: S3BootScriptSaveIoReadWrite S3BootScriptSaveMemReadWrite S3BootScriptSavePciCfgReadWrite S3BootScriptSavePciCfg2ReadWrite S3BootScriptSaveStall S3BootScriptSaveDispatch2 S3BootScriptSaveDispatch S3BootScriptSaveMemPoll S3BootScriptSaveIoPoll S3BootScriptSavePciPoll S3BootScriptSavePci2Poll S3BootScriptCloseTable S3BootScriptExecute S3BootScriptMoveLastOpcode S3BootScriptCompare are not affected by such numeric truncation. Signed-off-by: Hao A Wu <[email protected]> Reviewed-by: Laszlo Ersek <[email protected]> Reviewed-by: Eric Dong <[email protected]> Acked-by: Jian J Wang <[email protected]>
1 parent 1333d8c commit 322ac05

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @file
22
Save the S3 data to S3 boot script.
33
4-
Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
4+
Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.<BR>
55
66
SPDX-License-Identifier: BSD-2-Clause-Patent
77
@@ -1006,6 +1006,14 @@ S3BootScriptSaveIoWrite (
10061006
EFI_BOOT_SCRIPT_IO_WRITE ScriptIoWrite;
10071007

10081008
WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1009+
1010+
//
1011+
// Truncation check
1012+
//
1013+
if ((Count > MAX_UINT8) ||
1014+
(WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_IO_WRITE))) {
1015+
return RETURN_OUT_OF_RESOURCES;
1016+
}
10091017
Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte * Count));
10101018

10111019
Script = S3BootScriptGetEntryAddAddress (Length);
@@ -1102,6 +1110,14 @@ S3BootScriptSaveMemWrite (
11021110
EFI_BOOT_SCRIPT_MEM_WRITE ScriptMemWrite;
11031111

11041112
WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1113+
1114+
//
1115+
// Truncation check
1116+
//
1117+
if ((Count > MAX_UINT8) ||
1118+
(WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_MEM_WRITE))) {
1119+
return RETURN_OUT_OF_RESOURCES;
1120+
}
11051121
Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE) + (WidthInByte * Count));
11061122

11071123
Script = S3BootScriptGetEntryAddAddress (Length);
@@ -1206,6 +1222,14 @@ S3BootScriptSavePciCfgWrite (
12061222
}
12071223

12081224
WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1225+
1226+
//
1227+
// Truncation check
1228+
//
1229+
if ((Count > MAX_UINT8) ||
1230+
(WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE))) {
1231+
return RETURN_OUT_OF_RESOURCES;
1232+
}
12091233
Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE) + (WidthInByte * Count));
12101234

12111235
Script = S3BootScriptGetEntryAddAddress (Length);
@@ -1324,6 +1348,14 @@ S3BootScriptSavePciCfg2Write (
13241348
}
13251349

13261350
WidthInByte = (UINT8) (0x01 << (Width & 0x03));
1351+
1352+
//
1353+
// Truncation check
1354+
//
1355+
if ((Count > MAX_UINT8) ||
1356+
(WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE))) {
1357+
return RETURN_OUT_OF_RESOURCES;
1358+
}
13271359
Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE) + (WidthInByte * Count));
13281360

13291361
Script = S3BootScriptGetEntryAddAddress (Length);
@@ -1549,6 +1581,12 @@ S3BootScriptSaveSmbusExecute (
15491581
return Status;
15501582
}
15511583

1584+
//
1585+
// Truncation check
1586+
//
1587+
if (BufferLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)) {
1588+
return RETURN_OUT_OF_RESOURCES;
1589+
}
15521590
DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + BufferLength);
15531591

15541592
Script = S3BootScriptGetEntryAddAddress (DataSize);
@@ -1736,6 +1774,12 @@ S3BootScriptSaveInformation (
17361774
UINT8 *Script;
17371775
EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
17381776

1777+
//
1778+
// Truncation check
1779+
//
1780+
if (InformationLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_INFORMATION)) {
1781+
return RETURN_OUT_OF_RESOURCES;
1782+
}
17391783
Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);
17401784

17411785
Script = S3BootScriptGetEntryAddAddress (Length);
@@ -2195,6 +2239,12 @@ S3BootScriptLabelInternal (
21952239
UINT8 *Script;
21962240
EFI_BOOT_SCRIPT_INFORMATION ScriptInformation;
21972241

2242+
//
2243+
// Truncation check
2244+
//
2245+
if (InformationLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_INFORMATION)) {
2246+
return RETURN_OUT_OF_RESOURCES;
2247+
}
21982248
Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);
21992249

22002250
Script = S3BootScriptGetEntryAddAddress (Length);

0 commit comments

Comments
 (0)