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

Codesign apphosts on Mac #53913

Merged
merged 11 commits into from
Jun 17, 2021
Merged

Codesign apphosts on Mac #53913

merged 11 commits into from
Jun 17, 2021

Conversation

mateoatr
Copy link
Contributor

@mateoatr mateoatr commented Jun 9, 2021

Use codesign for signing apphosts on macOS. Related to #50375.

@mateoatr mateoatr added the area-HostModel Microsoft.NET.HostModel issues label Jun 9, 2021
@mateoatr mateoatr requested review from agocke, VSadov and vitek-karas June 9, 2021 01:58
@ghost
Copy link

ghost commented Jun 9, 2021

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

Issue Details

Use codesign for signing apphosts on macOS. Related to #50375.

Author: mateoatr
Assignees: -
Labels:

area-HostModel

Milestone: -

@@ -233,6 +239,19 @@ void FindBundleHeader()
return headerOffset != 0;
}

private static void CodeSign(string args)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Copy link
Member

Choose a reason for hiding this comment

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

I'd throw or assert here. If this ever gets called on a non-mac, that's a bug, not a recoverable failure

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine to assert, but I don't think we should throw. Especially since we build the entire feature as "nice to have" - it only works on mac. If I build on Linux and target mac I will have to codesign manually. So if I build on Linux and for some reason we try to codesign, it should not fail the build as the signing is not expected or it's optional.

Unfortunately we don't have a way to write to the msbuild log. Ideally we would produce at least an Info message about this so that it's easier to diagnose. I filed #53923 to hopefully improve on this one day.

Copy link
Member

Choose a reason for hiding this comment

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

If we eventually intend to implement codesigning on non-Mac that makes sense, but otherwise I'm actually fine with failing if we're on a Mac and the codesign tool is not found. As far as I can tell it's available on every mac back to 10.4 -- that looks like it should be plenty for all our releases of CoreCLR.

return;

using (var p = Process.Start(codesign, args))
p.WaitForExit();
Copy link
Member

Choose a reason for hiding this comment

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

I think we should check the result - what if signing fails for whatever reason?
Can we make it fail to test this? (maybe give it a nonMACHO input?)

Ideally this would print out the error from the tool (or there would at least be a way to get that) and also fail the build.

Please also consider making codesigning optional even on mac - maybe just an internal MSBuild property (_EnableMacOSCodeSign or something like that). To have a workaround for cases where this may cause issues.

Another consideration is publish. Since build runs as part of publish, the executable will get signed and then it will be published. What happens if the developer then tries to sign the executable again? This time with a real cert.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the approach so we have an argument to enable the signing in macOS. This will be set from within the SDK in the CreateAppHost task.

Can we make it fail to test this? (maybe give it a nonMACHO input?)

I added a test case in which we sign it two times, causing codesign to fail, thus throwing an exception with the error printed by the tool. The idea here is to catch the exception in the CreateAppHost and use the SDK logging mechanism for printing the error.

What happens if the developer then tries to sign the executable again? This time with a real cert.

If signing with codesign -s <cert> <executable> it will complain that the app was already sign; this can be worked around by forcing the signing (codesign -s <cert> -f <executable>).

Modify apphost exceptions inheritance
{
p.WaitForExit();
if (p.ExitCode != 0)
throw new AppHostSigningException(p.StandardError.ReadToEnd());
Copy link
Member

Choose a reason for hiding this comment

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

Ideally the exception would also record the exit code (And it would be shown as part of the error message from SDK).

Comment on lines +147 to +148
if (enableMacOSCodeSign && RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
CodeSign(appHostDestinationFilePath);
Copy link
Member

Choose a reason for hiding this comment

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

Either here or in the SDK we should handle the case where we're on macOS but we're building for a different platform (Windows or Linux). In that case we should not attempt signing (as it will fail).

I "think" this should be done in the SDK since here we don't have a good way to detect the target platform (we kind of rely on the SDK to tell us the right things).

@ghost ghost locked as resolved and limited conversation to collaborators Jul 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-HostModel Microsoft.NET.HostModel issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants