[PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case


Zhiguang Liu
 

Current implementation of the Elf loader reuses the same memory range if
the desired memory range is covered by [file base, file base + file size].
However, there is a potentil bug, for example:
If the first segment is located at 0x1000, and the desired address is
0x2000. While the second segment is located at 0x2000, and the desired
address is 0x2000. When we parse and try to load the elf file, current
implementation may load the first segment at 0x2000, and override second
segment.

Cc: Guo Dong <guo.dong@...>
Cc: Ray Ni <ray.ni@...>
Cc: Maurice Ma <maurice.ma@...>
Cc: Benjamin You <benjamin.you@...>
Cc: Sean Rhodes <sean@...>

Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c b/UefiPayload=
Pkg/PayloadLoaderPeim/ElfLib/ElfLib.c
index 2a6305c67b..c7dfae14af 100644
--- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
+++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
@@ -312,6 +312,13 @@ ParseElfImage (
ElfCt->ReloadRequired =3D TRUE;=0D
}=0D
=0D
+ if (SegInfo.MemAddr !=3D ((UINTN)ElfCt->FileBase + SegInfo.Offset)) {=
=0D
+ //=0D
+ // Need to relocate if the desired address is not the current addres=
s=0D
+ //=0D
+ ElfCt->ReloadRequired =3D TRUE;=0D
+ }=0D
+=0D
if (Base > (SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1))) {=0D
Base =3D SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1);=0D
}=0D
--=20
2.32.0.windows.2


Ni, Ray
 

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Wednesday, April 6, 2022 9:52 PM
To: devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@...>; Ni, Ray <ray.ni@...>; Ma, Maurice <maurice.ma@...>; You, Benjamin
<benjamin.you@...>; Rhodes, Sean <sean@...>
Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case

Current implementation of the Elf loader reuses the same memory range if
the desired memory range is covered by [file base, file base + file size].
However, there is a potentil bug, for example:
If the first segment is located at 0x1000, and the desired address is
0x2000. While the second segment is located at 0x2000, and the desired
address is 0x2000. When we parse and try to load the elf file, current
Why the desired address for both segments is 0x2000?


Zhiguang Liu
 

Sorry, it is a typo. the example should be
The first segment is located at 0x1000, and the desired address is 0x2000.
The second segment is located at 0x2000, and the desired address is 0x1000.
The current ELF loader may copy first segment to 0x2000, which will erase the second segment.

Thanks
Zhiguang

-----Original Message-----
From: Ni, Ray <ray.ni@...>
Sent: Thursday, April 7, 2022 8:06 AM
To: Liu, Zhiguang <zhiguang.liu@...>; devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@...>; Ma, Maurice <maurice.ma@...>;
You, Benjamin <benjamin.you@...>; Rhodes, Sean
<sean@...>
Subject: RE: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in
some case



-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Wednesday, April 6, 2022 9:52 PM
To: devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@...>; Ni, Ray <ray.ni@...>; Ma,
Maurice <maurice.ma@...>; You, Benjamin
<benjamin.you@...>; Rhodes, Sean <sean@...>
Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't
relocate in some case

Current implementation of the Elf loader reuses the same memory range
if the desired memory range is covered by [file base, file base + file size].
However, there is a potentil bug, for example:
If the first segment is located at 0x1000, and the desired address is
0x2000. While the second segment is located at 0x2000, and the desired
address is 0x2000. When we parse and try to load the elf file, current
Why the desired address for both segments is 0x2000?


Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Wednesday, April 6, 2022 6:52 AM
To: devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@...>; Ni, Ray <ray.ni@...>; Ma, Maurice <maurice.ma@...>; You, Benjamin <benjamin.you@...>; Rhodes, Sean <sean@...>
Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case

Current implementation of the Elf loader reuses the same memory range if the desired memory range is covered by [file base, file base + file size].
However, there is a potentil bug, for example:
If the first segment is located at 0x1000, and the desired address is 0x2000. While the second segment is located at 0x2000, and the desired address is 0x2000. When we parse and try to load the elf file, current implementation may load the first segment at 0x2000, and override second segment.

Cc: Guo Dong <guo.dong@...>
Cc: Ray Ni <ray.ni@...>
Cc: Maurice Ma <maurice.ma@...>
Cc: Benjamin You <benjamin.you@...>
Cc: Sean Rhodes <sean@...>

Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
index 2a6305c67b..c7dfae14af 100644
--- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
+++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
@@ -312,6 +312,13 @@ ParseElfImage (
ElfCt->ReloadRequired = TRUE; } + if (SegInfo.MemAddr != ((UINTN)ElfCt->FileBase + SegInfo.Offset)) {+ //+ // Need to relocate if the desired address is not the current address+ //+ ElfCt->ReloadRequired = TRUE;+ }+ if (Base > (SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1))) { Base = SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1); }--
2.32.0.windows.2


Ni, Ray
 

With the updated commit message, Reviewed-by: Ray Ni <ray.ni@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Thursday, April 7, 2022 8:57 AM
To: Ni, Ray <ray.ni@...>; devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@...>; Ma, Maurice <maurice.ma@...>; You, Benjamin <benjamin.you@...>;
Rhodes, Sean <sean@...>
Subject: RE: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case

Sorry, it is a typo. the example should be
The first segment is located at 0x1000, and the desired address is 0x2000.
The second segment is located at 0x2000, and the desired address is 0x1000.
The current ELF loader may copy first segment to 0x2000, which will erase the second segment.

Thanks
Zhiguang


-----Original Message-----
From: Ni, Ray <ray.ni@...>
Sent: Thursday, April 7, 2022 8:06 AM
To: Liu, Zhiguang <zhiguang.liu@...>; devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@...>; Ma, Maurice <maurice.ma@...>;
You, Benjamin <benjamin.you@...>; Rhodes, Sean
<sean@...>
Subject: RE: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in
some case



-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Wednesday, April 6, 2022 9:52 PM
To: devel@edk2.groups.io
Cc: Dong, Guo <guo.dong@...>; Ni, Ray <ray.ni@...>; Ma,
Maurice <maurice.ma@...>; You, Benjamin
<benjamin.you@...>; Rhodes, Sean <sean@...>
Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't
relocate in some case

Current implementation of the Elf loader reuses the same memory range
if the desired memory range is covered by [file base, file base + file size].
However, there is a potentil bug, for example:
If the first segment is located at 0x1000, and the desired address is
0x2000. While the second segment is located at 0x2000, and the desired
address is 0x2000. When we parse and try to load the elf file, current
Why the desired address for both segments is 0x2000?