Skip to content

MdeModulePkg: DebugImageInfoTable: Fix Array Maintenance #11013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025

Conversation

os-d
Copy link
Contributor

@os-d os-d commented Apr 25, 2025

Description

The DebugImageInfoTable contains an array of image info structures. The current implementation removes an entry by freeing the info structure and putting NULL in that entry of the array. It then decrements the table size tracked in the table. However, the array is invalid at this point, it contains a NULL entry, which the UEFI spec does not envision and it contains a valid entry past the end of the array as tracked in the spec defined config table. If the table is consumed at this point it can lead to an invalid assessment of the image state, which defeats the purpose of the table.

When a new info structure is added, it then scans for the first NULL entry adds a pointer to the new info structure there and increments the table size to cover the entrythat was formerly past the end of the array.

The current implementation requires that once an unload happens, more loads happen than unloads and that the last operation is not an unload (which won't be true in the shell, e.g.). This is needlessly complex, as the order of the table doesn't matter (and in fact this implementation doesn't preserve image loading order either).

This patch updates the removal function to free the desired info structure, move the last entry of the array to this freed spot, mark the last entry as NULL, and decrement the table count. The entry addition function then just always puts a new entry at the end of the array, expanding it as necessary. This simplifies the logic and covers the gaps that were present.

  • Breaking change?
    • Breaking change - Does this PR cause a break in build or boot behavior?
    • Examples: Does it add a new library class or move a module to a different repo.
  • Impacts security?
    • Security - Does this PR have a direct security impact?
    • Examples: Crypto algorithm change or buffer overflow fix.
  • Includes tests?
    • Tests - Does this PR include any explicit test code?
    • Examples: Unit tests or integration tests.

How This Was Tested

Tested by running a shell app that uses the DebugImageInfoTable and confirmed it could access all expected entries.

Integration Instructions

N/A.

The DebugImageInfoTable contains an array of image info
structures. The current implementation removes an entry by
freeing the info structure and putting NULL in that entry of
the array. It then decrements the table size tracked in the table.
However, the array is invalid at this point, it contains a NULL
entry, which the UEFI spec does not envision and it contains a valid
entry past the end of the array as tracked in the spec defined config
table. If the table is consumed at this point it can lead to an
invalid assessment of the image state, which defeats the purpose of
the table.

When a new info structure is added, it then scans for the first NULL
entry adds a pointer to the new info structure there and increments
the table size to cover the entrythat was formerly past the end of
the array.

The current implementation requires that once an unload happens,
more loads happen than unloads and that the last operation is not
an unload (which won't be true in the shell, e.g.). This is
needlessly complex, as the order of the table doesn't matter
(and in fact this implementation doesn't preserve image loading
order either).

This patch updates the removal function to free the desired
info structure, move the last entry of the array to this freed
spot, mark the last entry as NULL, and decrement the table count.
The entry addition function then just always puts a new entry at
the end of the array, expanding it as necessary. This simplifies
the logic and covers the gaps that were present.

Signed-off-by: Oliver Smith-Denny <[email protected]>
@xypron
Copy link
Contributor

xypron commented Apr 28, 2025

Tested-by @xypron

@xypron
Copy link
Contributor

xypron commented Apr 28, 2025

Having the entries in load order would be convenient for debugging, but this feature was neither provided by the previous implementation nor is it required by the UEFI specification, cf #11019.

@os-d
Copy link
Contributor Author

os-d commented Apr 28, 2025

Having the entries in load order would be convenient for debugging, but this feature was neither provided by the previous implementation nor is it required by the UEFI specification, cf #11019.

Yeah, my reasoning was that image load order is fairly easy to get from the log and as you said, neither the spec nor the existing implementation provided for it, so I thought speed was more important here (especially as on most boots this table will not be used, but still produced).

@os-d
Copy link
Contributor Author

os-d commented May 5, 2025

@lgao4 can you please review this PR as it was put up before the soft freeze?

@lgao4 lgao4 merged commit 5bc52de into tianocore:master May 7, 2025
124 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants