This is an archive of the discontinued LLVM Phabricator instance.

[LLD][COFF] Add load config checks to warn if incorrect for CFGuard
ClosedPublic

Authored by alvinhochun on Sep 1 2022, 1:40 AM.

Details

Summary

Control Flow Guard requires specific flags and VA's be included in the
load config directory to be functional. In case CFGuard is enabled via
linker flags, we can check to make sure this is the case and give the
user a warning if otherwise.

MSVC provides a proper _load_config_used by default, so this is more
relevant for the MinGW target in which current versions of mingw-w64
does not provide this symbol.

The checks (only if CFGuard is enabled) include:

  • The _load_config_used struct shall exist.
  • Alignment of the _load_config_used struct (shall be aligned to pointer size.)
  • The _load_config_used struct shall be large enough to contain the required fields.
  • The values of the following fields are checked against the expected values:
    • GuardCFFunctionTable
    • GuardCFFunctionCount
    • GuardFlags
    • GuardAddressTakenIatEntryTable
    • GuardAddressTakenIatEntryCount
    • GuardLongJumpTargetTable
    • GuardLongJumpTargetCount
    • GuardEHContinuationTable
    • GuardEHContinuationCount

Diff Detail

Event Timeline

alvinhochun created this revision.Sep 1 2022, 1:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2022, 1:40 AM
alvinhochun requested review of this revision.Sep 1 2022, 1:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 1 2022, 1:40 AM

This looks like a good idea IMO! Especially the check for the basic condition that _load_config_used must exist if this flag is enabled; I was surprised to see that you don't get any diagnostics for this combination today.

Other than that, the rest of the checks are certainly useful when implementing these things. (I didn't check all the details of what is verified.) On the other hand, since LLD is quite focused on performance, LLD omits pedantic checks of all inputs where it's relevant for performance (i.e. it's considered ok to not handle corrupted inputs cleanly) - but these checks shouldn't really have any performance impact I think, so they could be considered fine in that aspect.

lld/COFF/Writer.cpp
2131

Do you think it would be worthwhile to check that the section chunk itself also has got the right alignment? Since even if the section doesn't flag the right required alignment, we can accidentally end up on a correctly aligned RVA anyway.

alvinhochun added inline comments.Sep 7 2022, 6:47 AM
lld/COFF/Writer.cpp
2131

Initially I was only concerned about giving the user some insight to help with the mysterious "This app can't run on your PC" message that appears when you try to run the executable. My thinking is that if it happens to end up aligned correctly then the final executable will still work and the warning isn't needed...

A check for the actual alignment of the section chunk would be nice to have, but I suspect it may be a bit tricky to implement correctly.

Moved some code around and added a check for the actual alignment of the section chunk

rnk accepted this revision.Sep 19 2022, 10:26 AM

This seems like a nice QoI improvement!

This revision is now accepted and ready to land.Sep 19 2022, 10:26 AM
This revision was landed with ongoing or failed builds.Sep 20 2022, 1:03 AM
This revision was automatically updated to reflect the committed changes.
chapuni added inline comments.
lld/test/COFF/guard-warnings.s
5

It fails if x86 is not configured.

mstorsjo added inline comments.Sep 20 2022, 2:41 AM
lld/test/COFF/guard-warnings.s
5

Fixed, sorry about that!