Skip to content

libpsl-native: Fix _FORTIFY_SOURCE macros - #88

Merged
Andy Jordan (andyleejordan) merged 2 commits into
PowerShell:masterfrom
disconnect3d:fix-fortify-source-macros
Jan 5, 2023
Merged

Andy Jordan (andyleejordan) merged 2 commits into
PowerShell:masterfrom
disconnect3d:fix-fortify-source-macros

Conversation

@disconnect3d

@disconnect3d Disconnect3d (disconnect3d) commented Dec 27, 2022 •

Copy link
Copy Markdown
Contributor

This commit fixes the mistake in the _FORTIFY_SOURCE macro where it was not prefixed with underscore while it has to be (see e.g. https://github.com/search?q=repo%3Abminor%2Fglibc%20FORTIFY_SOURCE&type=code).

Additionally, to make this macro add extra security, one has to enable optimizations. I am not sure if the build system enables them, but it is worth double checking that as well.

Overall, I would recommend using -D_FORTIFY_SOURCE=3 with -O2 or -O3. (The fortify source level 3 was added recently and you can read more about it here: https://developers.redhat.com/blog/2021/04/16/broadening-compiler-checks-for-buffer-overflows-in-_fortify_source).

You can also see the result of the correct vs incorrect macro along with optimizations and no optimizations on this screenshot (source): image

This commit fixes the mistake in the `_FORTIFY_SOURCE` macro where it was not prefixed with underscore while it has to be (see e.g. https://github.com/search?q=repo%3Abminor%2Fglibc%20FORTIFY_SOURCE&type=code).

Additionally, to make this macro add extra security, one has to enable optimizations. I am not sure if the build system enables them, but it is worth double checking that as well.

Overall, I would recommend using `-D_FORTIFY_SOURCE=3` with  `-O2` or `-O3`. (The fortify source level 3 was added recently and you can read more about it here: https://developers.redhat.com/blog/2021/04/16/broadening-compiler-checks-for-buffer-overflows-in-_fortify_source).

You can also see the result of the correct vs incorrect macro along with optimizations and no optimizations on this screenshot ([source](https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGIM6SuADJ4DJgAcj4ARpjEIABspAAOqAqETgwe3r7%2ByanpAiFhkS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4gt%2BoPAGEKHG4uxi4yGFEyV5FRgIPxRaGAFsxSTElTiN4EoRfvbFBNaZkuScza%2BqDOl0p5k/fmr937CysmLIpP9OXS15R5f%2Bgr1iSFMpIDQCJmlSEkGaPU8qkpKpKVyspyD20KowX/BBIBGnnBii0sVGh1gInnesbWrMs1rjxC6W%2Bu6N3yyzU%2BtdI7SB9gas4yQQA%3D%3D%3D)): ![image](https://user-images.githubusercontent.com/10009354/209678087-977d494f-272c-4779-a3a5-994cf6da097a.png)
@adityapatwardhan

Copy link
Copy Markdown
Member

Andy Jordan (@andschwa) - can you help with this?

@andyleejordan

Copy link
Copy Markdown
Member
@andyleejordan

Copy link
Copy Markdown
Member

Ok, I'm confirming that the generated build files (for a release build) are currently including:

-DFORTIFY_SOURCE=2 -O2 -O3 -DNDEBUG

so I think we should edit this PR to remove the -O2 specification, as CMake is already adding -O3 for release builds.

Disconnect3d (@disconnect3d) is correct and this macro is currently doing nothing, as it needs the prepended underscore.

I like the idea of setting _FORTIFY_SOURCE=3 but with it being so new, I don't think it's worth the risk of unforeseen consequences for now.

Since CMake adds `-O3` for `Release` builds anyway.
@andyleejordan

Copy link
Copy Markdown
Member

For reference Aditya Patwardhan (@adityapatwardhan), from the CMakeCache.txt file:

//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG

//Flags used by the CXX compiler during RELWITHDEBINFO builds.
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

and I confirmed with the commit I just added to Disconnect3d (@disconnect3d)'s branch that the generated flags are now:

CXX_FLAGSarm64 =  -std=c++11 -Wall -Werror -fstack-protector-strong -fpie -D_FORTIFY_SOURCE=2 -O3 -DNDEBUG -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -fPIC -g -mmacosx-version-min=10.9

so no more superfluous -O2 and the macro is correctly specified.

@andyleejordan
Andy Jordan (andyleejordan) merged commit af5359b into PowerShell:master Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants