[edk2-devel] [PATCH] MdePkg/BasePeCoffLib: Deal with broken debug directories

Ard Biesheuvel posted 1 patch 11 months, 3 weeks ago
Failed in applying to current master (apply log)
MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
[edk2-devel] [PATCH] MdePkg/BasePeCoffLib: Deal with broken debug directories
Posted by Ard Biesheuvel 11 months, 3 weeks ago
Older versions of GenFw put the wrong value in the debug directory size
field in the PE/COFF header: instead of putting the combined size of all
the entries, it puts the size of the only entry it creates, but adds the
size of the NB10 payload that the entry points to. This confuses the
loader now that we started using additional debug directory entries to
describe DLL characteristics.

GenFw was fixed in commit 60e85a39fe49071, but the binaries that were
generated with it still need to be supported.

So let's detect this condition, and check whether the size of the debug
directory is consistent with the NB10 payload: if we should expect
additional directory entries where we observe the NB10 payload, the size
field is clearly wrong, and we can break from the loop.

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4425
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
index 4b71176a0c7c2ed0..27f8526370fa3859 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
@@ -585,6 +585,7 @@ PeCoffLoaderGetImageInfo (
   UINTN                                Size;
   UINTN                                ReadSize;
   UINTN                                Index;
+  UINTN                                NextIndex;
   UINTN                                DebugDirectoryEntryRva;
   UINTN                                DebugDirectoryEntryFileOffset;
   UINTN                                SectionHeaderOffset;
@@ -755,6 +756,19 @@ PeCoffLoaderGetImageInfo (
               ImageContext->ImageSize += DebugEntry.SizeOfData;
             }
 
+            //
+            // Implementations of GenFw before commit 60e85a39fe49071 will
+            // concatenate the debug directory entry and the codeview entry,
+            // and erroneously put the combined size into the debug directory
+            // entry's size field. If this is the case, no other relevant
+            // directory entries can exist, and we can terminate here.
+            //
+            NextIndex = Index + sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
+            if ((NextIndex < DebugDirectoryEntry->Size) &&
+                (DebugEntry.FileOffset == (DebugDirectoryEntryFileOffset + NextIndex))) {
+              break;
+            }
+
             continue;
           }
 
-- 
2.39.2



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#104027): https://edk2.groups.io/g/devel/message/104027
Mute This Topic: https://groups.io/mt/98685272/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH] MdePkg/BasePeCoffLib: Deal with broken debug directories
Posted by Michael Kubacki 11 months, 3 weeks ago
Acked-by: Michael Kubacki <michael.kubacki@microsoft.com>

On 5/4/2023 10:47 AM, Ard Biesheuvel wrote:
> Older versions of GenFw put the wrong value in the debug directory size
> field in the PE/COFF header: instead of putting the combined size of all
> the entries, it puts the size of the only entry it creates, but adds the
> size of the NB10 payload that the entry points to. This confuses the
> loader now that we started using additional debug directory entries to
> describe DLL characteristics.
> 
> GenFw was fixed in commit 60e85a39fe49071, but the binaries that were
> generated with it still need to be supported.
> 
> So let's detect this condition, and check whether the size of the debug
> directory is consistent with the NB10 payload: if we should expect
> additional directory entries where we observe the NB10 payload, the size
> field is clearly wrong, and we can break from the loop.
> 
> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4425
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> ---
>   MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 14 ++++++++++++++
>   1 file changed, 14 insertions(+)
> 
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> index 4b71176a0c7c2ed0..27f8526370fa3859 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> @@ -585,6 +585,7 @@ PeCoffLoaderGetImageInfo (
>     UINTN                                Size;
> 
>     UINTN                                ReadSize;
> 
>     UINTN                                Index;
> 
> +  UINTN                                NextIndex;
> 
>     UINTN                                DebugDirectoryEntryRva;
> 
>     UINTN                                DebugDirectoryEntryFileOffset;
> 
>     UINTN                                SectionHeaderOffset;
> 
> @@ -755,6 +756,19 @@ PeCoffLoaderGetImageInfo (
>                 ImageContext->ImageSize += DebugEntry.SizeOfData;
> 
>               }
> 
>   
> 
> +            //
> 
> +            // Implementations of GenFw before commit 60e85a39fe49071 will
> 
> +            // concatenate the debug directory entry and the codeview entry,
> 
> +            // and erroneously put the combined size into the debug directory
> 
> +            // entry's size field. If this is the case, no other relevant
> 
> +            // directory entries can exist, and we can terminate here.
> 
> +            //
> 
> +            NextIndex = Index + sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
> 
> +            if ((NextIndex < DebugDirectoryEntry->Size) &&
> 
> +                (DebugEntry.FileOffset == (DebugDirectoryEntryFileOffset + NextIndex))) {
> 
> +              break;
> 
> +            }
> 
> +
> 
>               continue;
> 
>             }
> 
>   
> 


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#104070): https://edk2.groups.io/g/devel/message/104070
Mute This Topic: https://groups.io/mt/98685272/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH] MdePkg/BasePeCoffLib: Deal with broken debug directories
Posted by Ard Biesheuvel 11 months, 2 weeks ago
On Fri, 5 May 2023 at 03:56, Michael Kubacki
<mikuback@linux.microsoft.com> wrote:
>
> Acked-by: Michael Kubacki <michael.kubacki@microsoft.com>
>

Merged as #4340

Thanks all


> On 5/4/2023 10:47 AM, Ard Biesheuvel wrote:
> > Older versions of GenFw put the wrong value in the debug directory size
> > field in the PE/COFF header: instead of putting the combined size of all
> > the entries, it puts the size of the only entry it creates, but adds the
> > size of the NB10 payload that the entry points to. This confuses the
> > loader now that we started using additional debug directory entries to
> > describe DLL characteristics.
> >
> > GenFw was fixed in commit 60e85a39fe49071, but the binaries that were
> > generated with it still need to be supported.
> >
> > So let's detect this condition, and check whether the size of the debug
> > directory is consistent with the NB10 payload: if we should expect
> > additional directory entries where we observe the NB10 payload, the size
> > field is clearly wrong, and we can break from the loop.
> >
> > BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4425
> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > ---
> >   MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 14 ++++++++++++++
> >   1 file changed, 14 insertions(+)
> >
> > diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> > index 4b71176a0c7c2ed0..27f8526370fa3859 100644
> > --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> > +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> > @@ -585,6 +585,7 @@ PeCoffLoaderGetImageInfo (
> >     UINTN                                Size;
> >
> >     UINTN                                ReadSize;
> >
> >     UINTN                                Index;
> >
> > +  UINTN                                NextIndex;
> >
> >     UINTN                                DebugDirectoryEntryRva;
> >
> >     UINTN                                DebugDirectoryEntryFileOffset;
> >
> >     UINTN                                SectionHeaderOffset;
> >
> > @@ -755,6 +756,19 @@ PeCoffLoaderGetImageInfo (
> >                 ImageContext->ImageSize += DebugEntry.SizeOfData;
> >
> >               }
> >
> >
> >
> > +            //
> >
> > +            // Implementations of GenFw before commit 60e85a39fe49071 will
> >
> > +            // concatenate the debug directory entry and the codeview entry,
> >
> > +            // and erroneously put the combined size into the debug directory
> >
> > +            // entry's size field. If this is the case, no other relevant
> >
> > +            // directory entries can exist, and we can terminate here.
> >
> > +            //
> >
> > +            NextIndex = Index + sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
> >
> > +            if ((NextIndex < DebugDirectoryEntry->Size) &&
> >
> > +                (DebugEntry.FileOffset == (DebugDirectoryEntryFileOffset + NextIndex))) {
> >
> > +              break;
> >
> > +            }
> >
> > +
> >
> >               continue;
> >
> >             }
> >
> >
> >
>
>
> 
>
>


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#104106): https://edk2.groups.io/g/devel/message/104106
Mute This Topic: https://groups.io/mt/98685272/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
[edk2-devel] 回复: [PATCH] MdePkg/BasePeCoffLib: Deal with broken debug directories
Posted by gaoliming via groups.io 11 months, 3 weeks ago
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
Tested-by: Liming Gao <gaoliming@byosoft.com.cn>

> -----邮件原件-----
> 发件人: Ard Biesheuvel <ardb@kernel.org>
> 发送时间: 2023年5月4日 22:48
> 收件人: devel@edk2.groups.io
> 抄送: gaoliming@byosoft.com.cn; michael.kubacki@microsoft.com; Ard
> Biesheuvel <ardb@kernel.org>
> 主题: [PATCH] MdePkg/BasePeCoffLib: Deal with broken debug directories
> 
> Older versions of GenFw put the wrong value in the debug directory size
> field in the PE/COFF header: instead of putting the combined size of all
> the entries, it puts the size of the only entry it creates, but adds the
> size of the NB10 payload that the entry points to. This confuses the
> loader now that we started using additional debug directory entries to
> describe DLL characteristics.
> 
> GenFw was fixed in commit 60e85a39fe49071, but the binaries that were
> generated with it still need to be supported.
> 
> So let's detect this condition, and check whether the size of the debug
> directory is consistent with the NB10 payload: if we should expect
> additional directory entries where we observe the NB10 payload, the size
> field is clearly wrong, and we can break from the loop.
> 
> BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4425
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> ---
>  MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
> 
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> index 4b71176a0c7c2ed0..27f8526370fa3859 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> @@ -585,6 +585,7 @@ PeCoffLoaderGetImageInfo (
>    UINTN                                Size;
> 
>    UINTN                                ReadSize;
> 
>    UINTN                                Index;
> 
> +  UINTN                                NextIndex;
> 
>    UINTN                                DebugDirectoryEntryRva;
> 
>    UINTN
> DebugDirectoryEntryFileOffset;
> 
>    UINTN                                SectionHeaderOffset;
> 
> @@ -755,6 +756,19 @@ PeCoffLoaderGetImageInfo (
>                ImageContext->ImageSize += DebugEntry.SizeOfData;
> 
>              }
> 
> 
> 
> +            //
> 
> +            // Implementations of GenFw before commit
> 60e85a39fe49071 will
> 
> +            // concatenate the debug directory entry and the codeview
> entry,
> 
> +            // and erroneously put the combined size into the debug
> directory
> 
> +            // entry's size field. If this is the case, no other relevant
> 
> +            // directory entries can exist, and we can terminate here.
> 
> +            //
> 
> +            NextIndex = Index + sizeof
> (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
> 
> +            if ((NextIndex < DebugDirectoryEntry->Size) &&
> 
> +                (DebugEntry.FileOffset ==
> (DebugDirectoryEntryFileOffset + NextIndex))) {
> 
> +              break;
> 
> +            }
> 
> +
> 
>              continue;
> 
>            }
> 
> 
> 
> --
> 2.39.2





-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#104069): https://edk2.groups.io/g/devel/message/104069
Mute This Topic: https://groups.io/mt/98697508/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-