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

Redirect stdin #8

Merged
merged 6 commits into from
Jul 18, 2013
Merged

Redirect stdin #8

merged 6 commits into from
Jul 18, 2013

Conversation

joshcooper
Copy link
Contributor

Invoke powershell so it reads the script to execute from redirected stdin. This eliminates possible security issues resulting from powershell script being displayed on the command line or in log files, as well as the 32K character maximum length for command lines. It also resolves 'number3' in #1.

Also, we no longer create top-level constants, e.g. ::POWERSHELL. Previously, ruby would issue warnings that the constant was redefined during pluginsync #2. This is now resolved.

The examples did not specify the `powershell` provider, as a result puppet
would use the default `exec` provider on windows instead of `powershell`.
Previously, we were passing the powershell script to execute on the command
line. This is undesirable because the script contents are available for any
other process to see. Also, CreateProcess has a maximum command line length
of 32K characters.

I would have liked to create a tempfile and execute powershell with the
-File <path> option. However, powershell requires that puppet close the
file handle (or resort to native Win32 APIs to open the file with full
sharing). The former would expose puppet to TOCTOU race conditions. The
latter is hard, since ruby is the one creating the tempfile.

This commit executes powershell with redirected stdin. This solves the
original issues with passing the script on the commandline. And since
puppet never closes the tempfile handle until after powershell exits, the
file can't be modified after puppet writes the tempfile, and before
powershell writes it.

Note current versions of Windows use per-user temp directories with strong
permissions to minimize the likelihood of TOCTOU attacks on tempfiles, but
I'd rather not make (poor) assumptions, especially on Windows 2003.
Previously, the provider was defining top-level constants POWERSHELL and
PS_ARGS, which resulted in the following warning each time the agent
pluginsynced:

    warning: already initialized constant POWERSHELL
This was referenced Jul 16, 2013
@ferventcoder
Copy link
Contributor

Has @stack72 seen this?

@joshcooper
Copy link
Contributor Author

@rismoney So ruby's tempfile looks in several places when creating tempfiles https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L25 When running as LocalSystem I see the following values:

TMPDIR:
TMP: C:\Windows\TEMP
TEMP: C:\Windows\TEMP
Etc.systmpdir: C:/Windows/System32/config/systemprofile/AppData/Local/Temp

I verified that LocalSystem creates tempfiles in C:\Windows\Temp, presumably based on the System environment setting. Suprisingly, C:\Windows\Temp has fairly lax permissions:

C:\Windows\Temp>icacls .
. BUILTIN\Users:(CI)(S,WD,AD,X)
  BUILTIN\Administrators:(F)
  BUILTIN\Administrators:(OI)(CI)(IO)(F)
  NT AUTHORITY\SYSTEM:(F)
  NT AUTHORITY\SYSTEM:(OI)(CI)(IO)(F)
  CREATOR OWNER:(OI)(CI)(IO)(F)

Any authenticated user can write or append to files, so it's a good thing we are keeping the file handle open.

@rismoney
Copy link

Awesome, thanks for the good info. Looks like it does the right thing as localsystem. Kudos to you for the locking insight!

-------- Original message --------
From: Josh Cooper notifications@github.com
Date:
To: joshcooper/puppetlabs-powershell puppetlabs-powershell@noreply.github.com
Cc: "Siegel, Richard" RSiegel@ise.com
Subject: Re: [puppetlabs-powershell] Redirect stdin (#8)

@rismoneyhttps://github.com/rismoney So ruby's tempfile looks in several places when creating tempfiles https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L25 When running as LocalSystem I see the following values:

TMPDIR:
TMP: C:\Windows\TEMP
TEMP: C:\Windows\TEMP
Etc.systmpdir: C:/Windows/System32/config/systemprofile/AppData/Local/Temp

I verified that LocalSystem creates tempfiles in C:\Windows\Temp, presumably based on the System environment setting. Suprisingly, C:\Windows\Temp has fairly lax permissions:

C:\Windows\Temp>icacls .
. BUILTIN\Users:(CI)(S,WD,AD,X)
BUILTIN\Administrators:(F)
BUILTIN\Administrators:(OI)(CI)(IO)(F)
NT AUTHORITY\SYSTEM:(F)
NT AUTHORITY\SYSTEM:(OI)(CI)(IO)(F)
CREATOR OWNER:(OI)(CI)(IO)(F)

Any authenticated user can write or append to files, so it's a good thing we are keeping the file handle open.


Reply to this email directly or view it on GitHubhttps://github.com//pull/8#issuecomment-21155096.

Earlier versions of the powershell module could not execute 64-bit powershell.
This was resolved in commit 9c95b11.
The module tool generates a test/init.pp file with lines longer than 80
characters, which the puppet-lint tool warns about.
When using newer rspec, need to explicitly require rspec expectations, which
is what monkey-patches the `Object#should` method. As a result, running
rspec spec would fail with newer rspec versions.
@joshcooper joshcooper merged commit b5606da into master Jul 18, 2013
@joshcooper joshcooper deleted the redirect_stdin branch August 21, 2014 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants