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

Attempting to use ProtectedData.Protect with LocalMachine scope fails when run on non-Windows system as a non-root user #2437

Closed
TheCakeIsNaOH opened this issue Nov 1, 2021 · 0 comments · Fixed by #2438
Assignees
Milestone

Comments

@TheCakeIsNaOH
Copy link
Member

What You Are Seeing?

Issue first found here:
#2435 (comment)

When running choco as a non-root user, choco install can fail.

First, it outputs this message

package not installed. An error occurred during installation:
 Data protection failed.

Then later on, it exits with this error

Sharing violation on path chocolateyInstall/lib/package/.chocolateyPending

What is Expected?

That a debug build of choco can install (portable) packages as non-root.

How Did You Get This To Happen? (Steps to Reproduce)

  1. Build choco on Linux
  2. Run mono choco.exe install curl as a non-root user from the build_output/chocolatey folder.

What is really going on

ProtectedData.Protect stores it's keypair for the LocalMachine scope inside /usr/share/.mono by default. However, that folder is not writable by non-root users. So, when ProtectedData.Protect tries to generate a keypair there and choco is not running as a root user, it throws an error.

This specifically an issue when it is called inside ChocolateyPackageService.capture_arguments, as the error is caught by a try/catch block in NugetService.install_run, so it just outputs the outer exception message and continues. That catch block re-runs ChocolateyPackageService.handle_package_result which then re-runs ChocolateyPackageService.set_pending, which then tries and fails to create the .chocolateyPending file. This results in the misleading error message.

Output Log

Full Log Output

mono ./build_output/chocolatey/choco.exe install curl --verbose --debug
Chocolatey v0.10.14-beta-446-g9ecd2988 (DEBUG BUILD)
Chocolatey is running on Linux v 4.19.104.0
Attempting to delete file "/home/user/choco/build_output/chocolatey/chocolatey.dll.old".
Attempting to delete file "/home/user/choco/build_output/chocolatey/choco.exe.old".
Command line: /home/user/choco/build_output/chocolatey/choco.exe install curl --verbose --debug
Received arguments: install curl --verbose --debug
RemovePendingPackagesTask is now ready and waiting for PreRunMessage.
Sending message 'PreRunMessage' out if there are subscribers...
[Pending] Removing all pending packages that should not be considered installed...
Directory '/home/user/choco/build_output/chocolatey/lib' does not exist.
Performing validation checks.
Global Configuration Validation Checks:
 - Package Exit Code / Exit On Reboot = Checked
System State Validation Checks:
The source 'https://community.chocolatey.org/api/v2/' evaluated to a 'normal' source type

NOTE: Hiding sensitive configuration data! Please double and triple
 check to be sure no sensitive data is shown, especially if copying
 output to a gist for review.
Configuration: CommandName='install'|CacheLocation='/tmp/chocolatey'|
ContainsLegacyPackageInstalls='True'|
CommandExecutionTimeoutSeconds='2700'|WebRequestTimeoutSeconds='30'|
Sources='https://community.chocolatey.org/api/v2/'|SourceType='normal'|
Debug='True'|Verbose='True'|Trace='False'|Force='False'|Noop='False'|
HelpRequested='False'|UnsuccessfulParsing='False'|RegularOutput='True'|
QuietOutput='False'|PromptForConfirmation='True'|AcceptLicense='False'|
AllowUnofficialBuild='True'|Input='curl'|AllVersions='False'|
SkipPackageInstallProvider='False'|PackageNames='curl'|
Prerelease='False'|ForceX86='False'|OverrideArguments='False'|
NotSilent='False'|ApplyPackageParametersToDependencies='False'|
ApplyInstallArgumentsToDependencies='False'|IgnoreDependencies='False'|
AllowMultipleVersions='False'|AllowDowngrade='False'|
ForceDependencies='False'|Information.PlatformType='Linux'|
Information.PlatformVersion='4.19.104.0'|
Information.PlatformName='Linux'|
Information.ChocolateyVersion='0.12.0.0'|
Information.ChocolateyProductVersion='0.10.14-beta-446-g9ecd2988'|
Information.FullName='chocolatey, Version=0.12.0.0, Culture=neutral, PublicKeyToken=fd112f53c3ab578c'|

Information.Is64BitOperatingSystem='True'|
Information.Is64BitProcess='True'|Information.IsInteractive='False'|
Information.UserName='user'|Information.UserDomainName='REDACTED'|
Information.IsUserAdministrator='False'|
Information.IsUserSystemAccount='False'|
Information.IsUserRemoteDesktop='False'|
Information.IsUserRemote='True'|
Information.IsProcessElevated='False'|
Information.IsLicensedVersion='False'|Information.LicenseType='Foss'|
Information.CurrentDirectory='/home/user/choco'|
Features.AutoUninstaller='True'|Features.ChecksumFiles='True'|
Features.AllowEmptyChecksums='False'|
Features.AllowEmptyChecksumsSecure='True'|
Features.FailOnAutoUninstaller='False'|
Features.FailOnStandardError='False'|Features.UsePowerShellHost='True'|
Features.LogEnvironmentValues='False'|Features.LogWithoutColor='False'|
Features.VirusCheck='False'|
Features.FailOnInvalidOrMissingLicense='False'|
Features.IgnoreInvalidOptionsSwitches='True'|
Features.UsePackageExitCodes='True'|
Features.UseEnhancedExitCodes='False'|
Features.UseFipsCompliantChecksums='False'|
Features.ShowNonElevatedWarnings='True'|
Features.ShowDownloadProgress='True'|
Features.StopOnFirstPackageFailure='False'|
Features.UseRememberedArgumentsForUpgrades='False'|
Features.IgnoreUnfoundPackagesOnUpgradeOutdated='False'|
Features.SkipPackageUpgradesWhenNotInstalled='False'|
Features.RemovePackageInformationOnUninstall='False'|
Features.ExitOnRebootDetected='False'|
Features.LogValidationResultsOnWarnings='True'|
Features.UsePackageRepositoryOptimizations='True'|
Features.ScriptsCheckLastExitCode='False'|
ListCommand.LocalOnly='False'|
ListCommand.IdOnly='False'|ListCommand.IncludeRegistryPrograms='False'|
ListCommand.PageSize='25'|ListCommand.Exact='False'|
ListCommand.ByIdOnly='False'|ListCommand.ByTagOnly='False'|
ListCommand.IdStartsWith='False'|ListCommand.OrderByPopularity='False'|
ListCommand.ApprovedOnly='False'|
ListCommand.DownloadCacheAvailable='False'|
ListCommand.NotBroken='False'|
ListCommand.IncludeVersionOverrides='False'|
UpgradeCommand.FailOnUnfound='False'|
UpgradeCommand.FailOnNotInstalled='False'|
UpgradeCommand.NotifyOnlyAvailableUpgrades='False'|
UpgradeCommand.ExcludePrerelease='False'|
NewCommand.AutomaticPackage='False'|
NewCommand.UseOriginalTemplate='False'|SourceCommand.Command='unknown'|
SourceCommand.Priority='0'|SourceCommand.BypassProxy='False'|
SourceCommand.AllowSelfService='False'|
SourceCommand.VisibleToAdminsOnly='False'|
FeatureCommand.Command='unknown'|ConfigCommand.Command='unknown'|
ApiKeyCommand.Remove='False'|PinCommand.Command='unknown'|
OutdatedCommand.IgnorePinned='False'|
ExportCommand.IncludeVersionNumbers='False'|Proxy.BypassOnLocal='True'|

Chocolatey is not an official build (bypassed with --allow-unofficial).
 If you are seeing this message and it is not expected, your system may
 now be in a bad state. Only official builds are to be trusted.

_ Chocolatey:ChocolateyInstallCommand - Normal Run Mode _
Installing the following packages:
curl
By installing, you accept licenses for the packages.
Attempting to create directory "/home/user/choco/build_output/chocolatey/lib".
Using 'https://community.chocolatey.org/api/v2/'.
- Supports prereleases? 'True'.
- Is ServiceBased? 'True'.
Package 'curl' found on source 'https://community.chocolatey.org/api/v2/'
[NuGet] Installing 'curl 7.79.1'.
[NuGet] Added file 'cacert.pem' to folder 'curl/tools'.
[NuGet] Added file 'chocolateyInstall.ps1' to folder 'curl/tools'.
[NuGet] Added file 'curl-7.79.1-win32-mingw.zip' to folder 'curl/tools'.
[NuGet] Added file 'curl-7.79.1-win64-mingw.zip' to folder 'curl/tools'.
[NuGet] Added file 'LICENSE.txt' to folder 'curl/legal'.
[NuGet] Added file 'VERIFICATION.txt' to folder 'curl/legal'.
[NuGet] Added file 'curl.nupkg' to folder 'curl'.
[NuGet] Added file 'curl.nuspec' to folder 'curl'.
[NuGet] Successfully installed 'curl 7.79.1'.

curl v7.79.1 [Approved]
curl package files install completed. Performing other installation steps.
 Skipping Powershell and shimgen portions of the install due to non-Windows.
curl not installed. An error occurred during installation:
 Data protection failed.

Chocolatey installed 0/0 packages.
 See the log for details (/home/user/choco/build_output/chocolatey/logs/chocolatey.log).

Are you ready for the ultimate experience? Check out Pro / Business!
 https://chocolatey.org/compare
Sending message 'PostRunMessage' out if there are subscribers...
Chocolatey had an error occur:
System.IO.IOException: Sharing violation on path /home/user/choco/build_output/chocolatey/lib/curl/.chocolateyPending
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0019e] in <533173d24dae460899d2b10975534bb0>:0
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <533173d24dae460899d2b10975534bb0>:0
  at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at chocolatey.infrastructure.filesystem.DotNetFileSystem.get_file_encoding (System.String filePath) [0x0000e] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.filesystem.DotNetFileSystem.write_file (System.String filePath, System.String fileText) [0x00012] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.services.ChocolateyPackageService.set_pending (chocolatey.infrastructure.results.PackageResult packageResult, chocolatey.infrastructure.app.configuration.ChocolateyConfiguration config) [0x000a7] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.services.ChocolateyPackageService.handle_package_result (chocolatey.infrastructure.results.PackageResult packageResult, chocolatey.infrastructure.app.configuration.ChocolateyConfiguration config, chocolatey.infrastructure.app.domain.CommandNameType commandName) [0x00008] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.services.ChocolateyPackageService+<>c__DisplayClass35_0.<install_run>b__0 (chocolatey.infrastructure.results.PackageResult packageResult) [0x00000] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.services.NugetService.install_run (chocolatey.infrastructure.app.configuration.ChocolateyConfiguration config, System.Action`1[T] continueAction) [0x00811] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.services.ChocolateyPackageService+<>c__DisplayClass35_1.<install_run>b__1 (chocolatey.infrastructure.app.services.ISourceRunner r) [0x00000] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.services.ChocolateyPackageService.perform_source_runner_function[T] (chocolatey.infrastructure.app.configuration.ChocolateyConfiguration config, System.Func`2[T,TResult] function) [0x00035] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.services.ChocolateyPackageService.install_run (chocolatey.infrastructure.app.configuration.ChocolateyConfiguration config) [0x00139] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.commands.ChocolateyInstallCommand.run (chocolatey.infrastructure.app.configuration.ChocolateyConfiguration configuration) [0x0000e] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.runners.GenericRunner.run (chocolatey.infrastructure.app.configuration.ChocolateyConfiguration config, SimpleInjector.Container container, System.Boolean isConsole, System.Action`1[T] parseArgs) [0x00105] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.infrastructure.app.runners.ConsoleApplication.run (System.String[] args, chocolatey.infrastructure.app.configuration.ChocolateyConfiguration config, SimpleInjector.Container container) [0x000f5] in <e95da0370894475f8a7dc17271b38d16>:0
  at chocolatey.console.Program.Main (System.String[] args) [0x003c4] in <d333536bf96140f09b19e3bff6b8f194>:0
Exiting with 1
Exiting with 1
Press enter to continue...

TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Nov 2, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Nov 2, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Nov 2, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Nov 2, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Nov 2, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Nov 3, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
TheCakeIsNaOH added a commit to TheCakeIsNaOH/choco that referenced this issue Nov 5, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
gep13 pushed a commit to TheCakeIsNaOH/choco that referenced this issue Nov 12, 2021
When running on mono on a non-Windows system, the ProtectedData methods
use /usr/local/.mono/keypair as the folder to save keypairs. This
folder is not normally writable by non-root users, thus erroring when
used by a normal user.

This catches that error and falls back to using the CurrentUser scope.
Also, during data unprotection, it will try to fall back to CurrentUser
scope if the decryption fails for other reasons, so as to attempt to
decrypt data encrypted with CurrentUser if the LocalSystem scope starts
working.
gep13 added a commit that referenced this issue Nov 12, 2021
(#2437) Fall back to CurrentUser scope when permission denied
@gep13 gep13 added 4 - Done and removed 3 - Review labels Nov 12, 2021
@gep13 gep13 added this to the 0.12.0 milestone Jan 17, 2022
@gep13 gep13 changed the title Mono ProtectedData.Protect with LocalMachine scope fails when run as a non-root user Attempting to use ProtectedData.Protect with LocalMachine scope fails when run on non-Windows system as a non-root user Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants