Skip to content

build: adds switch to enable/disable randomized base address in MSVC builds - #21045

Merged
laanwj merged 1 commit into
bitcoin:masterfrom
EthanHeilman:winnoaslr
Apr 29, 2021
Merged

laanwj merged 1 commit into
bitcoin:masterfrom
EthanHeilman:winnoaslr

Conversation

@EthanHeilman

@EthanHeilman EthanHeilman commented Feb 1, 2021 •

Copy link
Copy Markdown
Contributor

In m4 builds we have the --disable-hardening switch that can be given in ./configure to turn off randomized addresses. This PR provides a simple way of turning off randomized addresses in MSVC builds.

This PR:

  • Adds this option the common-init project file so that it can be globally set across the project
  • Documents this switch in msvc build readme

I have run the following test to verify this works

I ran the msvc build with <RandomizedBaseAddress>true</RandomizedBaseAddress> then checked bitcoind.exe and bitcoin-cli.exe with dumpbin.exe:

bitcoind

> .\dumpbin.exe /headers src/bitcoind.exe
Microsoft (R) COFF/PE Dumper Version 14.16.27045.0
...
OPTIONAL HEADER VALUES
             20B magic # (PE32+)
           14.28 linker version
          AE4600 size of code
          345C00 size of initialized data
               0 size of uninitialized data
          6BED74 entry point (00000001406BED74) mainCRTStartup
            1000 base of code
       140000000 image base (0000000140000000 to 0000000140E2DFFF)
            1000 section alignment
             200 file alignment
            6.00 operating system version
            0.00 image version
            6.00 subsystem version
               0 Win32 version
          E2E000 size of image
             400 size of headers
               0 checksum
               3 subsystem (Windows CUI)
            8160 DLL characteristics
                   High Entropy Virtual Addresses
                   Dynamic base
                   NX compatible
                   Terminal Server Aware

bitcoin-cli

> .\dumpbin.exe /headers src/bitcoin-cli.exe
Microsoft (R) COFF/PE Dumper Version 14.16.27045.0
...
OPTIONAL HEADER VALUES
             20B magic # (PE32+)
           14.28 linker version
          1E3E00 size of code
           92C00 size of initialized data
               0 size of uninitialized data
          104384 entry point (0000000140104384) mainCRTStartup
            1000 base of code
       140000000 image base (0000000140000000 to 0000000140279FFF)
            1000 section alignment
             200 file alignment
            6.00 operating system version
            0.00 image version
            6.00 subsystem version
               0 Win32 version
          27A000 size of image
             400 size of headers
               0 checksum
               3 subsystem (Windows CUI)
            8160 DLL characteristics
                   High Entropy Virtual Addresses
                   Dynamic base
                   NX compatible
                   Terminal Server Aware

Then I built with <RandomizedBaseAddress>false</RandomizedBaseAddress> then checked bitcoind.exe and bitcoin-cli.exe with dumpbin.exe and observed that Dynamic base was longer listed in OPTIONAL HEADER VALUES

bitcoind

PS C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64> .\dumpbin.exe /headers C:\Users\e0\Documents\GitHub\bitcoin021noaslr/src/bitcoind.exe
Microsoft (R) COFF/PE Dumper Version 14.16.27045.0
...
OPTIONAL HEADER VALUES
             20B magic # (PE32+)
           14.28 linker version
          AE4600 size of code
          33FE00 size of initialized data
               0 size of uninitialized data
          6BED74 entry point (00000001406BED74) mainCRTStartup
            1000 base of code
       140000000 image base (0000000140000000 to 0000000140E27FFF)
            1000 section alignment
             200 file alignment
            6.00 operating system version
            0.00 image version
            6.00 subsystem version
               0 Win32 version
          E28000 size of image
             400 size of headers
               0 checksum
               3 subsystem (Windows CUI)
            8120 DLL characteristics
                   High Entropy Virtual Addresses
                   NX compatible
                   Terminal Server Aware

bitcoin-cli

> .\dumpbin.exe /headers src/bitcoin-cli.exe
Microsoft (R) COFF/PE Dumper Version 14.16.27045.0
...
OPTIONAL HEADER VALUES
             20B magic # (PE32+)
           14.28 linker version
          1E3E00 size of code
           90C00 size of initialized data
               0 size of uninitialized data
          104384 entry point (0000000140104384) mainCRTStartup
            1000 base of code
       140000000 image base (0000000140000000 to 0000000140277FFF)
            1000 section alignment
             200 file alignment
            6.00 operating system version
            0.00 image version
            6.00 subsystem version
               0 Win32 version
          278000 size of image
             400 size of headers
               0 checksum
               3 subsystem (Windows CUI)
            8120 DLL characteristics
                   High Entropy Virtual Addresses
                   NX compatible
                   Terminal Server Aware

@sipsorcery

Copy link
Copy Markdown
Contributor

@EthanHeilman any chance you could set the status on this PR to draft while you're still working on it? Example. It's slightly easier to keep track of than using [wip] in the title.

Feel free to ping me when you're ready for reviews.

@maflcko
maflcko marked this pull request as draft February 1, 2021 10:19
@EthanHeilman
EthanHeilman force-pushed the winnoaslr branch 2 times, most recently from ccd3f85 to 1ea9381 Compare February 27, 2021 20:23
@EthanHeilman EthanHeilman changed the title [wip] build: adds switch to enable/disable randomized base address in MSVC builds build: adds switch to enable/disable randomized base address in MSVC builds Feb 28, 2021
@EthanHeilman
EthanHeilman marked this pull request as ready for review February 28, 2021 00:22
@practicalswift

Copy link
Copy Markdown
Contributor

cr ACK 1ea938167cd284a7aecdec31b7f2173f5658d110: patch looks correct

Thanks for improving hardening!

@sipsorcery

Copy link
Copy Markdown
Contributor

@EthanHeilman thanks for the PR.

Explicitly calling out the RandomizedBaseAddress linker option does seem to be a little bit of superfluous given it's enabled and giving the security benefits by default. But I guess it does no harm other than the time it will take developers to read the documentation.

Rather than adding a new ItemDefinitionGroup could you add the new option to the common block that already exists at the bottom of common.init.vcxproj, e.g:

...
      <AdditionalIncludeDirectories>..\..\src;..\..\src\univalue\include;..\..\src\secp256k1\include;..\..\src\leveldb\include;..\..\src\leveldb\helpers\memenv;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <SubSystem>Console</SubSystem>
      <AdditionalDependencies>Iphlpapi.lib;ws2_32.lib;Shlwapi.lib;kernel32.lib;user32.lib;gdi32.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <RandomizedBaseAddress>true</RandomizedBaseAddress>
    </Link>
    <Lib>
      <AdditionalOptions>/ignore:4221</AdditionalOptions>
    </Lib>
...

@sipsorcery

Copy link
Copy Markdown
Contributor

ACK 9bd3f35.

@EthanHeilman

Copy link
Copy Markdown
Contributor Author

Thanks @sipsorcery that is a better place for it, I've updated the PR. Currently testing it locally.

The reason I'm adding this flag is that I was doing some bitcoin research which required predictable base addresses and it took me a while to figure out how to disable randomized addresses. I figured I'd save the next person who wants this option some time.

@EthanHeilman

Copy link
Copy Markdown
Contributor Author

Tested and confirmed the correct behavior.

@practicalswift

Copy link
Copy Markdown
Contributor

cr ACK 9bd3f35: patch looks correct

@laanwj
laanwj merged commit 298fd02 into bitcoin:master Apr 29, 2021
gwillen pushed a commit to ElementsProject/elements that referenced this pull request Jun 1, 2022
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants