Skip to content

Support for AuthorizedKeysCommand and AuthorizedPrincipal - #399

Closed
Thomas Meckel (tmeckel) wants to merge 7 commits into
PowerShell:latestw_allfrom
tmeckel:support-subprocess
Closed

Thomas Meckel (tmeckel) wants to merge 7 commits into
PowerShell:latestw_allfrom
tmeckel:support-subprocess

Conversation

@tmeckel

Copy link
Copy Markdown

Support for AuthorizedKeysCommand and AuthorizedPrincipal

This PR implements the support for spawning child processes from the SSH Daemon
(Service), so that the configuration items AuthorizedKeysCommand and
AuthorizedPrincipalCommand can be utilized under Windows as well.

The implementation not only supports standard Windows executables but is capable
of spawning PowerShell Skripts. Under all Unix derivates a special handling of
shell scripts isnt't necessary because those platforms support the start of
script as (normal) executables via the Shebang notation. This technique isn't
available under Windows, so the pull request supports using Powershell scripts
with a special handling (treatment).

The following excerpt from the sshd.log file depicts this implementation

6628 2019-10-11 17:01:37.935 debug3: subprocess: AuthorizedKeysCommand command "C:\\\\windows\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe -nop -nol -noni -file c:\\\\ProgramData\\\\ssh\\\\Find-RSAKey.ps1 devtest\\\\ee022bf" running as sshd (flags 0x6)
6628 2019-10-11 17:01:37.935 debug3: get_execpath: Powershell Core Path [C:\\Program Files\\PowerShell]
6628 2019-10-11 17:01:37.935 debug3: get_execpath: Powershell Path [C:\\windows\\System32\\WindowsPowerShell]
6628 2019-10-11 17:01:37.935 debug3: get_execpath: Checking path for Powershell reference: C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe
6628 2019-10-11 17:01:37.935 debug3: get_execpath: Found Powershell trying to find -f parameter
6628 2019-10-11 17:01:37.935 debug3: get_execpath: Found -File Powershell parameter: c:\\ProgramData\\ssh\\Find-RSAKey.ps1
6628 2019-10-11 17:01:37.935 debug3: subprocess: Checking exec path [c:\\ProgramData\\ssh\\Find-RSAKey.ps1] for secure file permissions
6628 2019-10-11 17:01:37.935 debug2: pipe - r-h:632,io:000001E7C150A710,fd:5  w-h:628,io:000001E7C1509710,fd:8
6628 2019-10-11 17:01:37.935 debug3: subprocess: Spawning process: C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe
6628 2019-10-11 17:01:37.935 debug3: spawning "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" -nop -nol -noni -file c:\\ProgramData\\ssh\\Find-RSAKey.ps1 devtest\\ee022bf

With the support of AuthorizedKeysCommand and PowerShell scripts, it is
possible to store the approved SSH Public-Keys for a user in an Azure KeyVault
or Storage Account and to transfer the keys to the SSH service via the Az
Powershell Modules, which itself uitilize an authentication via the Managed
Indentity of the virtual machine to access the information in the KeyVault or
Storage Account.

I explicitly mention Bryan Berns (@NoMoreFood) and Manoj Ampalam (@manojampalam) to request a review on this PR.

@NoMoreFood

Copy link
Copy Markdown

Before I consider diving into this, is there any spots where you could change your implementation to reduce the amount of changes outside of "contrib\win32"? That makes assimilating the upstream changes simpler and increases the likelihood this port could be merged into the upstream branch (which I imagine is still a goal).

convertToBackslash(exePathPtr);

/*
* check if av[0] starts with 'C:\Program Files\PowerShell' or 'C:\Windows\System32\WindowsPowerShell'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why check specific paths? Windows Powershell is one thing but pwsh could be installed to another drive, this needs to check PATH for the pwsh executable and in implementing that, would be easier to use it for Windows Powershell too.

@tmeckel

Copy link
Copy Markdown
Author

Before I consider diving into this, is there any spots where you could change
your implementation to reduce the amount of changes outside of
"contrib\win32"? That makes assimilating the upstream changes simpler and
increases the likelihood this port could be merged into the upstream branch
(which I imagine is still a goal).

Hi Bryan Berns (@NoMoreFood) thanks for willing to review my PR and bringing up the vital
point having an easy path for integrating the changes here to the upstream
project. So I want through my changes again. Small side note before I give my
point about the changes I applied to implement the subprocess support: I
restructured my initial commits in the way that all changes that required to
support sub-process spawning under Windows are located in ONE
commit
.
All other commits are optional stuff that could be cherry picked or not.

So coming back to you initial question about if I see any possibilities to
reduce the noise that my PR generates on the main OpenSSH source code. I think
the best way to to do is to look at the files in the main source code trunk one
by one which I changed. From my point those files are: auth.c, servconf.c,
sshfileperm.h. All other changes are located in files which are in
"contrib\win32" folder anyway.

auth.c

The auth.ccontains the method subprocess which implements spawning
subprocesses from the OpenSSH daemon/service to support the
AuthorizedKeysCommand and AuthorizedPrincipalsCommand configuration
settings. The original implementation is using the fork-exec-pattern. This
pattern isn't supported under Windows. Thus I added support for starting
sub-processes via the Posix Spawn functions which were luckily already
implemented for Windows. The main stuff is located starting at line
955.
So from my point of view this can't be changed or moved somewhere else. And I
think it's a valuable contribution to the main project anyway because the Posix
Spwan functions got introduced for environments which do not support fork-exec
like embedded systems. The code will give the upstream OpenSSH project the
ability to support the aforementioned command line options for systems that
didn’t support them up to now because the OS of those systems lack the support
for for-exec.

All other changes in auth.c could be removed. Especially the way I implemented
the get_execpath function, which implementation already got questioned by
WSLUser the way it handles the PowerShell support and which is responsible to
ensure a minimum of security of executing an external command in conjunction
with check_secure_file_permission. The only vital point here is that when
we're doing so the implementation which checks if the calling command is safe
has to be changed because the safe_path function isn't useable under Windows.
Another point to watch out is the function temporarily_use_uid which switches
the UID of the OpenSSH daemon and which is reverted back by restore_uid.
Because I hide the call to temporarily_use_uid in an #ifdef block (Line 914)
I had to introduce equal #ifdef blocks for restore_uid as well (Line 946,
951).

servconf.c

I had to change the tests for absolute paths because in the original source this
check is only performed by verifying that the path starts with / which will
obviously fail under Windows. So I changes this by feeding the questionable path
to the function path_absolute which is located in misc.cand has been
adjusted to support Windows already.

sshfileperm.h

I added the forward declaration of the newly introduced method get_execpath
here simply because I didn't find a better place. If we remove the
get_execpath function (ref. to my statements about auth.c) of change the way
this is done or we find a better place (.h file) to put the declaration than
we can remove the change here of course.

@WSLUser

WSLUser commented Oct 18, 2019 •

Copy link
Copy Markdown

I would consider changes made outside of contrib/win32 to be contributed upstream if they are best implemented in those files. In that case, you'll need to test or get assistance from those who can test platforms that don't support fork-exec-pattern can utilize your POSIX Spawn functions. Based on this: https://stackoverflow.com/questions/2731531/faster-forking-of-large-processes-on-linux, it appears most systems including embedded can use this syscall. As posix-spawn(2) can be used on Linux as a potentially faster replacement to fork() or alternative to clone(), you should test that Linux can use your implementation. If that goes through, maybe someone could contribute a patch to use it when memory consumption is too high to use fork(). For Windows, that patch wouldn't mean anything.

@WSLUser

WSLUser commented Nov 15, 2019

Copy link
Copy Markdown

Superceded by #409 so this one can be closed.

@tmeckel

Copy link
Copy Markdown
Author

If Manoj Ampalam (@manojampalam) profited from my work a short notice would've been nice at least. Anyway ... nice that finally we have support for spawning processes.

@manojampalam

Copy link
Copy Markdown

Thomas Meckel (@tmeckel) - I feel retarded I was not aware there is a PR pending for this work. I based my changes off a POC I did last year. I took a quick look at your changes and they are pretty much inline of what I have done, it double validates the underlying approach. Thanks for putting it together.

@tmeckel

Copy link
Copy Markdown
Author

Manoj Ampalam (@manojampalam) It's a bit funny that you say that you weren't aware of my PR, because you were mentioned explicitly in the description, because I know that you are a maintainer of this repository.

Whatever, I'm happy that we finally have support for this feature on Windows. I'll start migrating the installations that are running with my code currently at a customer cloud environment next week.

@manojampalam

Copy link
Copy Markdown

I know. Working on a tight schedule here. Haven’t gotten time to pay attention on this repo recently.

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.

4 participants