Skip to content
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

Add ability statically link OpenSSL #80380

Merged
merged 11 commits into from Jan 15, 2023
Merged

Conversation

kant2002
Copy link
Contributor

@kant2002 kant2002 commented Jan 9, 2023

This setup works if I apply this as local customizations, I do not sure that I use OpenSSL in most secure way, I do not competent. This setup and StaticExecutable=true allow package just EXE file + /etc/ssl/certs/ folder in Docker

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jan 9, 2023
@ghost
Copy link

ghost commented Jan 9, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

This setup works if I apply this as local customizations, I do not sure that I use OpenSSL in most secure way, I do not competent. This setup and StaticExecutable=true allow package just EXE file + /etc/ssl/certs/ folder in Docker

Author: kant2002
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

@@ -87,6 +95,8 @@ The .NET Foundation licenses this file to you under the MIT license.

<Exec Command="$(IlcHostPackagePath)/native/src/libs/System.Globalization.Native/local_build.sh $(IlcHostPackagePath)/ $(IntermediateOutputPath)" Condition="'$(StaticICULinking)' == 'true'"/>

<Exec Command="$(IlcHostPackagePath)/native/src/libs/System.Security.Cryptography.Native/local_build.sh $(IlcHostPackagePath)/ $(IntermediateOutputPath)" Condition="'$(StaticOpenSslLinking)' == 'true'"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local_build.sh file missing in the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. But it's the same as in Globalization work basically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as in Globalization

Maybe we should have just one script and pass the library to build as an argument?

@vcsjones
Copy link
Member

vcsjones commented Jan 9, 2023

@bartonjs @blowdart a heads up this is proposing a change to allow NativeAOT apps to statically link OpenSSL (libcrypto and libssl).

@blowdart
Copy link
Contributor

blowdart commented Jan 9, 2023

This change would mean any Microsoft product using aot would have to get a cryptoboard exception. Static linking openssl is arguably rather bad security. So many products out there in routers, IoT devices etc. cannot even manage to update dynamically linked OpenSSL, a static linkage makes its use even more hidden.

From a compliance and security PoV if reject this. Has there been a wider discussion I've missed, or is it just this PR?

Cc @GrabYourPitchforks

@kant2002
Copy link
Contributor Author

kant2002 commented Jan 9, 2023

For perspective why I was adding this change - I was thinking to have application as single EXE file running inside docker containers built from scratch image. If update needed, then docker container recreated. I do not consider that this would be used in different contexts.

@jkotas
Copy link
Member

jkotas commented Jan 9, 2023

This change would mean any Microsoft product using aot would have to get a cryptoboard exception.

@blowdart That's not correct. This change is not changing any default. The aot build is going to dynamically link openssl by default both before and after this change.

This change is adding option to allow statically linking. This is expert-only option. It requires acquiring extra dependencies and it mirrors what we have done for other libraries (ICU). I do not expect that we are going to document this option in official documentation.

The community has always been interested having an option to statically linking everything together to build "from scratch" container images, etc. This small change makes this niche scenario a bit easier.

Static linking openssl is arguably rather bad security.

It depends on how you orchestrate your deployment pipeline. If your deployment pipeline fully rebuilds your containers for updates, there is a little difference between dynamic and static linking security-wise. You have to always rebuild your container to pick-up OpenSSL update for both cases. The only difference between these two cases are performance characteristics.

Has there been a wider discussion I've missed, or is it just this PR?

This is follow up on #79498

@blowdart
Copy link
Contributor

blowdart commented Jan 9, 2023

Oh dear, ok, then any AOT compiled app which opts-in will need a cryptoboard exception, before or after this sigh

Does the documentation call this sort of thing out with a large warning/caveat anywhere?

# The .NET Foundation licenses this file to you under the MIT license.
#

# This script is used only for building libSystem.Security.Cryptography.Native.a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated comment

@jkotas
Copy link
Member

jkotas commented Jan 9, 2023

Oh dear, ok, then any AOT compiled app which opts-in will need a cryptoboard exception, before or after this sigh

No AOT compiled app shipped by Microsoft should be opting-in into this.

Does the documentation call this sort of thing out with a large warning/caveat anywhere?

This option is not documented at all today. You have to find in the source. We can certainly add documentation for this with a disclaimer, but it is only going to make the option more discoverable, and I expect that more people will try to use it.

if [ -d "$SHIM_SOURCE_DIR" ]; then
LOCAL_SHIM_DIR="$INTERMEDIATE_OUTPUT_PATH"/libs/$TARGET_LIBRARY/build
mkdir -p "$LOCAL_SHIM_DIR" && cd "$LOCAL_SHIM_DIR"
if [ $? -ne 0 ]; then echo "build-local.sh::ERROR: Cannot use local build directory"; exit 1; fi
Copy link
Member

@am11 am11 Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same inline syntax as before for compat. The only change needed was replacement of hardcoded name with $TARGET_LIBRARY.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I copy file from browser instead of file system

@jkotas
Copy link
Member

jkotas commented Jan 9, 2023

@kant2002 Could you please mention this next to https://github.com/dotnet/runtime/blob/main/src/coreclr/nativeaot/docs/compiling.md#using-statically-linked-icu and include a large warning/caveat ?

src/native/libs/build-local.sh Outdated Show resolved Hide resolved
@kant2002
Copy link
Contributor Author

kant2002 commented Jan 9, 2023

@jkotas is this wording, okay?

## Using statically linked OpenSSL
This feature can statically link OpenSSL libraries (such as libssl.a and libcrypto.a) into your applications at build time.
NativeAOT binaries built with this feature can run even when OpenSSL libraries are not installed.
**WARNING:** *This is scenario for advanced users, please use with extreme caution. Incorrect usage of this feature, can cause security vulnerabilities in your product*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blowdart Does this look good?

@@ -4,24 +4,25 @@
# The .NET Foundation licenses this file to you under the MIT license.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executable bit was dropped during the move: chmod +x src/native/libs/build-local.sh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@am11
Copy link
Member

am11 commented Jan 10, 2023

@kant2002, did you test it with code calling native API in System.Security.Cryptography and receive no usable version of libssl was found error? That error means we need to surround the dlopen calls with #ifndef LOCAL_BUILD in the initialization code.

(similar to 7646e76#diff-5c4725be11b8ef9741d6dba36be1244424dfcfc951ada9793862f002bcaee808)

@kant2002
Copy link
Contributor Author

I did test in Docker container while making call to https website. https://github.com/kant2002/NativeAOTDocker/tree/main/OpenSslEmbedding

I did not test built product, but rather patch existing ILC runtime nuget content.

Regarding ignoring opensslshim.c that file included if you build platformagnostic SSL, but I do not specify that define and things starts working.

if (FEATURE_DISTRO_AGNOSTIC_SSL)
list(APPEND NATIVECRYPTO_SOURCES
opensslshim.c
)

@jkotas
Copy link
Member

jkotas commented Jan 14, 2023

@kant2002 Could you please resolve the conflict?

This setup works if I apply this as local customizations, I do not sure that I use OpenSSL in most secure way, I do not competent. This setup and StaticExecutable=true allow package just EXE file + /etc/ssl/certs/ folder in Docker
kant2002 and others added 5 commits January 14, 2023 13:34
Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
…e.Unix.targets

Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
@davidfowl
Copy link
Member

Is there an issue associated with this?

@kant2002
Copy link
Contributor Author

@davidfowl Technically no, because it start as experiment to create scratch console app with ICU in #79498 and when I do not receive pushback on the scenario, I decide to make Web site on scratch container, like Go do it. Results is approximately 23MB for dotnet new web template using scratch, vs 33 MB using Chiselled Ubuntu.

If you interested, you can look at results of this experiment here https://github.com/kant2002/NativeAOTDocker

@davidfowl
Copy link
Member

Can you file one?

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

@jkotas jkotas merged commit 2eca2d3 into dotnet:main Jan 15, 2023
@kant2002 kant2002 deleted the kant/static-link-openssl branch January 15, 2023 09:15
@dotnet dotnet locked as resolved and limited conversation to collaborators Feb 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants